# tenstorrent/tt-llk architecture

> Architecture diagram of the GitHub repository tenstorrent/tt-llk, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tenstorrent/tt-llk
- Repository: https://github.com/tenstorrent/tt-llk

## Overview

##### Project type and purpose
- TT-LLK is a header-only C++17 library of low-level compute kernels (“LLKs”) for Tenstorrent AI chips (Blackhole, Wormhole).
- It provides foundational primitives consumed by higher-level stacks (e.g., tt-metal) and includes a rich test environment (RISC-V firmware + Python harness).

##### File structure
- Top-level directories:
- .github / infra – CI pipelines, Dockerfiles, build scripts
- docs – multi-level documentation (intro, top-level overview, programming model)
- tests – two sub-environments:
– tests/firmware (RISC-V assembler + include headers, tmu-crt0 startup)
– tests/python_tests (pytest harness, helpers, config files)
- tt_llk_blackhole & tt_llk_wormhole_b0 – for each chip:
– common/inc – all kernel headers (ckernel_*.h, SFPU ops)
– instructions/assembly.yaml – instruction definitions, mapping to kernels
– llk_lib – generic kernel templates, math primitives, pack/unpack logic
- Key patterns: header-only design, code-generation via SFPI compiler driven by instructions YAML, separation by chip target, unified llk_lib API.

##### README
- Confirms header-only LLK library, dependent on SFPI compiler, targeted at ML kernel writers and runtime integration.
- Points to docs for architecture and programming model.
- Emphasizes test environment for validation.

a. Main components to show:
1. LLK Source Modules (per chip target)
– common/inc (kernel headers)
– instructions YAML (defines instruction ↔ kernel mapping)
– llk_lib templates
2. SFPI Compiler / Code Generator
3. Consumer Runtime (tt-metal / higher-level ML stacks)
4. Test Environment
– Python Test Harness (pytest scripts, helpers)
– RISC-V Firmware (tmu-crt0, address maps)
5. CI/CD Infrastructure
– GitHub Actions, Docker Build Scripts, pre-commit hooks
6. Documentation Portal (Sphinx or MkDocs)
b. Relationships & interactions:
– LLK headers + instructions → SFPI Compiler → generated kernels for each chip
– Consumer Runtime links against LLK headers + compiled output → runs on real hardware or simulator
– Python Harness invokes kernels via hardware controller API → flashes RISC-V firmware → collects metrics
– CI pipelines automate build, test (firmware & Python), doc generation, Docker image publication
c. Architectural patterns & design principles:
– Header-only, template-based code for zero linking overhead
– Code generation via external compiler (SFPI) driven by declarative YAML
– Multi-target support (chip variants) via separate include trees
– Clear separation of core library vs. test harness vs. infra
d. Key technologies & frameworks:
– C++17 headers
– SFPI compiler (external)
– Python 3.10 + pytest
– RISC-V firmware + linker scripts (ld/*.ld)
– GitHub Actions, Docker, pre-commit

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% LLK Source Modules
    subgraph "LLK Source Modules" 
        direction TB
        subgraph "Blackhole Variant" 
            direction TB
            BH_INC["tt_llk_blackhole/common/inc"]:::library
            BH_YAML["tt_llk_blackhole/instructions/assembly.yaml"]:::library
            BH_LIB["tt_llk_blackhole/llk_lib"]:::library
        end
        subgraph "Wormhole B0 Variant"
            direction TB
            WH_INC["tt_llk_wormhole_b0/common/inc"]:::library
            WH_YAML["tt_llk_wormhole_b0/instructions/assembly.yaml"]:::library
            WH_LIB["tt_llk_wormhole_b0/llk_lib"]:::library
        end
    end

    %% Code Generation
    SFPI["SFPI Compiler / Code Generator"]:::codegen

    %% Consumer Runtime
    CR["Consumer Runtime (tt-metal / higher-level ML stacks)"]:::runtime

    %% Test Environment
    subgraph "Test Environment"
        direction TB
        subgraph "Python Test Harness" 
            direction TB
            PY_HARNESS["tests/python_tests"]:::tests
            PY_HELPERS["tests/python_tests/helpers"]:::tests
        end
        subgraph "RISC-V Firmware"
            direction TB
            FW_ROOT["tests/firmware"]:::tests
            FW_COMMON["tests/firmware/riscv/common"]:::tests
            BH_MAP["Blackhole Address Map"]:::tests
            WH_MAP["Wormhole Address Map"]:::tests
            FW_HELPERS["tests/helpers"]:::tests
        end
    end

    %% CI/CD Infrastructure
    subgraph "CI/CD Infrastructure"
        direction TB
        GHA["GitHub Actions (Workflows)"]:::infra
        DOCKER["Docker Builds & Scripts"]:::infra
    end

    %% Documentation
    subgraph "Documentation"
        direction TB
        DOC_PORTAL["docs/llk"]:::infra
        DOC_COMMON["docs/common & README.md"]:::infra
    end

    %% Relationships
    BH_INC -->|"feeds definitions to"| SFPI
    BH_YAML -->|"feeds definitions to"| SFPI
    BH_LIB -->|"template library for"| SFPI

    WH_INC -->|"feeds definitions to"| SFPI
    WH_YAML -->|"feeds definitions to"| SFPI
    WH_LIB -->|"template library for"| SFPI

    SFPI -->|"generates kernels for"| CR
    SFPI -->|"provides kernels to"| PY_HARNESS

    PY_HARNESS -->|"flashes & validates on"| FW_ROOT
    FW_ROOT -->|"uses headers"| FW_COMMON
    FW_COMMON -->|"provides maps to"| BH_MAP
    FW_COMMON -->|"provides maps to"| WH_MAP
    PY_HARNESS -->|"utilizes helpers"| PY_HELPERS
    FW_ROOT -->|"utilizes scripts"| FW_HELPERS

    GHA -->|"runs build via"| SFPI
    GHA -->|"runs tests via"| PY_HARNESS
    GHA -->|"runs tests via"| FW_ROOT
    GHA -->|"runs docs via"| DOC_PORTAL

    DOCKER -->|"builds images for CI"| GHA
    DOCKER -->|"provides environment for"| SFPI

    DOC_COMMON -->|"source for portal"| DOC_PORTAL

    %% Click Events
    click BH_INC "https://github.com/tenstorrent/tt-llk/tree/main/tt_llk_blackhole/common/inc"
    click BH_YAML "https://github.com/tenstorrent/tt-llk/blob/main/tt_llk_blackhole/instructions/assembly.yaml"
    click BH_LIB "https://github.com/tenstorrent/tt-llk/tree/main/tt_llk_blackhole/llk_lib"
    click WH_INC "https://github.com/tenstorrent/tt-llk/tree/main/tt_llk_wormhole_b0/common/inc"
    click WH_YAML "https://github.com/tenstorrent/tt-llk/blob/main/tt_llk_wormhole_b0/instructions/assembly.yaml"
    click WH_LIB "https://github.com/tenstorrent/tt-llk/tree/main/tt_llk_wormhole_b0/llk_lib"
    click SFPI "https://github.com/tenstorrent/tt-llk/blob/main/.github/scripts/install-tests-dependencies.sh"
    click CR "https://github.com/tenstorrent/tt-llk/blob/main/.github/workflows/tt-metal-integration-tests.yml"
    click PY_HARNESS "https://github.com/tenstorrent/tt-llk/tree/main/tests/python_tests"
    click PY_HELPERS "https://github.com/tenstorrent/tt-llk/tree/main/tests/python_tests/helpers"
    click FW_ROOT "https://github.com/tenstorrent/tt-llk/tree/main/tests/firmware"
    click FW_COMMON "https://github.com/tenstorrent/tt-llk/tree/main/tests/firmware/riscv/common"
    click BH_MAP "https://github.com/tenstorrent/tt-llk/blob/main/tests/firmware/riscv/blackhole/l1_address_map.h"
    click WH_MAP "https://github.com/tenstorrent/tt-llk/blob/main/tests/firmware/riscv/wormhole/l1_address_map.h"
    click FW_HELPERS "https://github.com/tenstorrent/tt-llk/tree/main/tests/helpers"
    click GHA "https://github.com/tenstorrent/tt-llk/tree/main/.github/workflows"
    click DOCKER "https://github.com/tenstorrent/tt-llk/blob/main/.github/Dockerfile.ci"
    click DOCKER "https://github.com/tenstorrent/tt-llk/tree/main/.github/scripts"
    click DOC_PORTAL "https://github.com/tenstorrent/tt-llk/tree/main/docs/llk"
    click DOC_COMMON "https://github.com/tenstorrent/tt-llk/tree/main/docs/common"

    %% Styles
    classDef library fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef codegen fill:#ffe5cc,stroke:#333,stroke-width:1px
    classDef runtime fill:#ccffcc,stroke:#333,stroke-width:1px
    classDef tests fill:#e5ccff,stroke:#333,stroke-width:1px
    classDef infra fill:#e0e0e0,stroke:#333,stroke-width:1px
```
