# fesens/opentpu architecture

> Architecture diagram of the GitHub repository fesens/opentpu, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fesens/opentpu
- Repository: https://github.com/fesens/opentpu

## Overview

openTPU is an open-source AI accelerator stack for running language models on a PCIe FPGA card, with a bit-exact simulator and kernel compiler. The principal workflow is a user request through host chat and model generation, into compiled accelerator programs and the board runtime, which returns generated tokens. Model families, quantized weights, MoE expert offload, diagnostics, and calibration are meaningful supporting capabilities. The README documents the overall stack; source-index references and excerpts support internal links. The hardware RTL and some host routes are not present in the supplied tree, so those connections are omitted.

Main entry points: [`opentpu/llm/generate.py`](https://github.com/fesens/opentpu/blob/main/opentpu/llm/generate.py) (Token generation), [`opentpu/llm/qwen3.py`](https://github.com/fesens/opentpu/blob/main/opentpu/llm/qwen3.py) (Dense models), [`opentpu/host/board.py`](https://github.com/fesens/opentpu/blob/main/opentpu/host/board.py) (Board driver), [`opentpu/kernels`](https://github.com/fesens/opentpu/tree/main/opentpu/kernels) (Accelerator kernels)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_models["Models and kernels"]
  node_generation["Token generation<br/>[generate.py]"]
  node_dense_models["Dense models<br/>[qwen3.py]"]
  node_hybrid_models["Hybrid models<br/>[qwen35.py]"]
  node_gemma["Gemma 4<br/>[gemma4.py]"]
  node_moe["MoE routing<br/>[moe.py]"]
  node_kernels["Accelerator kernels"]
end

subgraph group_compiler["Kernel compilation"]
  node_language["Kernel language<br/>[language.py]"]
  node_compiler_core["Compiler<br/>[compiler.py]"]
  node_isa["Instruction set<br/>[isa.py]"]
end

subgraph group_host["Host and device"]
  node_chat["Chat interface<br/>[chat.py]"]
  node_runtime["Execution runtime<br/>[runtime.py]"]
  node_board["Board driver<br/>[board.py]"]
  node_offload["Expert offload<br/>[offload.py]"]
  node_calibration["DDR calibration<br/>[selfcal.py]"]
  node_lens["Tracing and inspection<br/>[lens.py]"]
end

subgraph group_simulation["Simulation and formats"]
  node_quant["Weight quantization<br/>[quant.py]"]
  node_formats["Model formats<br/>[formats.py]"]
  node_simulator["ISA simulator<br/>[isasim.py]"]
  node_fp32["FP32 arithmetic<br/>[fp32.py]"]
end

node_user(("User"))
node_fpga{{"FPGA card"}}
node_tokens["Generated tokens"]

node_user -->|"submits prompt"| node_chat
node_chat -.->|"requests generation"| node_generation
node_generation -->|"runs model"| node_dense_models
node_generation -->|"runs model"| node_hybrid_models
node_generation -->|"runs model"| node_gemma
node_dense_models -->|"uses kernels"| node_kernels
node_hybrid_models -->|"uses kernels"| node_kernels
node_gemma -->|"uses kernels"| node_kernels
node_dense_models -->|"resolves formats"| node_formats
node_gemma -->|"resolves formats"| node_formats
node_dense_models -->|"quantizes weights"| node_quant
node_hybrid_models -->|"uses routing"| node_moe
node_moe -->|"requests experts"| node_offload
node_kernels -->|"expressed in"| node_language
node_language -->|"traced by"| node_compiler_core
node_compiler_core -->|"emits instructions"| node_isa
node_isa -->|"executed by"| node_simulator
node_simulator -->|"uses arithmetic"| node_fp32
node_generation -.->|"launches programs"| node_runtime
node_runtime -.->|"executes on"| node_board
node_board -->|"transfers over PCIe"| node_fpga
node_fpga -->|"returns results"| node_tokens
node_board -->|"services expert requests"| node_offload
node_board -->|"supports calibration"| node_calibration
node_lens -->|"inspects execution"| node_simulator
node_lens -->|"inspects runtime"| node_runtime

click node_chat "https://github.com/fesens/opentpu/blob/main/opentpu/host/chat.py"
click node_generation "https://github.com/fesens/opentpu/blob/main/opentpu/llm/generate.py"
click node_dense_models "https://github.com/fesens/opentpu/blob/main/opentpu/llm/qwen3.py"
click node_hybrid_models "https://github.com/fesens/opentpu/blob/main/opentpu/llm/qwen35.py"
click node_gemma "https://github.com/fesens/opentpu/blob/main/opentpu/llm/gemma4.py"
click node_moe "https://github.com/fesens/opentpu/blob/main/opentpu/llm/moe.py"
click node_kernels "https://github.com/fesens/opentpu/tree/main/opentpu/kernels"
click node_language "https://github.com/fesens/opentpu/blob/main/opentpu/language.py"
click node_compiler_core "https://github.com/fesens/opentpu/blob/main/opentpu/compiler.py"
click node_isa "https://github.com/fesens/opentpu/blob/main/opentpu/isa.py"
click node_quant "https://github.com/fesens/opentpu/blob/main/opentpu/quant.py"
click node_formats "https://github.com/fesens/opentpu/blob/main/opentpu/llm/formats.py"
click node_simulator "https://github.com/fesens/opentpu/blob/main/opentpu/isasim.py"
click node_fp32 "https://github.com/fesens/opentpu/blob/main/opentpu/fp32.py"
click node_runtime "https://github.com/fesens/opentpu/blob/main/opentpu/runtime.py"
click node_board "https://github.com/fesens/opentpu/blob/main/opentpu/host/board.py"
click node_offload "https://github.com/fesens/opentpu/blob/main/opentpu/host/offload.py"
click node_calibration "https://github.com/fesens/opentpu/blob/main/opentpu/host/selfcal.py"
click node_lens "https://github.com/fesens/opentpu/blob/main/opentpu/lens.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_generation,node_dense_models,node_hybrid_models,node_gemma,node_moe,node_kernels,node_user toneBlue
class node_language,node_compiler_core,node_isa toneAmber
class node_chat,node_runtime,node_board,node_offload,node_calibration,node_lens toneMint
class node_quant,node_formats,node_simulator,node_fp32 toneRose
class node_fpga,node_tokens toneIndigo
```

## Components

### Models and kernels

- [Token generation](https://github.com/fesens/opentpu/blob/main/opentpu/llm/generate.py): `opentpu/llm/generate.py`
- [Dense models](https://github.com/fesens/opentpu/blob/main/opentpu/llm/qwen3.py): `opentpu/llm/qwen3.py`
- [Hybrid models](https://github.com/fesens/opentpu/blob/main/opentpu/llm/qwen35.py): `opentpu/llm/qwen35.py`
- [Gemma 4](https://github.com/fesens/opentpu/blob/main/opentpu/llm/gemma4.py): `opentpu/llm/gemma4.py`
- [MoE routing](https://github.com/fesens/opentpu/blob/main/opentpu/llm/moe.py): `opentpu/llm/moe.py`
- [Accelerator kernels](https://github.com/fesens/opentpu/tree/main/opentpu/kernels): `opentpu/kernels`

### Kernel compilation

- [Kernel language](https://github.com/fesens/opentpu/blob/main/opentpu/language.py): `opentpu/language.py`
- [Compiler](https://github.com/fesens/opentpu/blob/main/opentpu/compiler.py): `opentpu/compiler.py`
- [Instruction set](https://github.com/fesens/opentpu/blob/main/opentpu/isa.py): `opentpu/isa.py`

### Host and device

- [Chat interface](https://github.com/fesens/opentpu/blob/main/opentpu/host/chat.py): `opentpu/host/chat.py`
- [Execution runtime](https://github.com/fesens/opentpu/blob/main/opentpu/runtime.py): `opentpu/runtime.py`
- [Board driver](https://github.com/fesens/opentpu/blob/main/opentpu/host/board.py): `opentpu/host/board.py`
- [Expert offload](https://github.com/fesens/opentpu/blob/main/opentpu/host/offload.py): `opentpu/host/offload.py`
- [DDR calibration](https://github.com/fesens/opentpu/blob/main/opentpu/host/selfcal.py): `opentpu/host/selfcal.py`
- [Tracing and inspection](https://github.com/fesens/opentpu/blob/main/opentpu/lens.py): `opentpu/lens.py`

### Simulation and formats

- [Weight quantization](https://github.com/fesens/opentpu/blob/main/opentpu/quant.py): `opentpu/quant.py`
- [Model formats](https://github.com/fesens/opentpu/blob/main/opentpu/llm/formats.py): `opentpu/llm/formats.py`
- [ISA simulator](https://github.com/fesens/opentpu/blob/main/opentpu/isasim.py): `opentpu/isasim.py`
- [FP32 arithmetic](https://github.com/fesens/opentpu/blob/main/opentpu/fp32.py): `opentpu/fp32.py`

### Other components

- **User**
- **FPGA card**
- **Generated tokens**

## Connections

- User → Chat interface: submits prompt
- Chat interface → Token generation: requests generation
- Token generation → Dense models: runs model (evidence: `opentpu/llm/generate.py`)
- Token generation → Hybrid models: runs model (evidence: `opentpu/llm/qwen35.py`)
- Token generation → Gemma 4: runs model (evidence: `opentpu/llm/gemma4.py`)
- Dense models → Accelerator kernels: uses kernels (evidence: `opentpu/llm/qwen3.py`)
- Hybrid models → Accelerator kernels: uses kernels (evidence: `opentpu/llm/qwen35.py`)
- Gemma 4 → Accelerator kernels: uses kernels (evidence: `opentpu/llm/gemma4.py`)
- Dense models → Model formats: resolves formats (evidence: `opentpu/llm/qwen3.py`)
- Gemma 4 → Model formats: resolves formats (evidence: `opentpu/llm/gemma4.py`)
- Dense models → Weight quantization: quantizes weights (evidence: `opentpu/llm/qwen3.py`)
- Hybrid models → MoE routing: uses routing (evidence: `opentpu/llm/qwen35.py`)
- MoE routing → Expert offload: requests experts (evidence: `opentpu/llm/moe.py`)
- Accelerator kernels → Kernel language: expressed in (evidence: `opentpu/kernels/lib.py`)
- Kernel language → Compiler: traced by (evidence: `opentpu/language.py`)
- Compiler → Instruction set: emits instructions (evidence: `opentpu/compiler.py`)
- Instruction set → ISA simulator: executed by (evidence: `opentpu/isasim.py`)
- ISA simulator → FP32 arithmetic: uses arithmetic (evidence: `opentpu/isasim.py`)
- Token generation → Execution runtime: launches programs
- Execution runtime → Board driver: executes on
- Board driver → FPGA card: transfers over PCIe
- FPGA card → Generated tokens: returns results
- Board driver → Expert offload: services expert requests (evidence: `opentpu/host/offload.py`)
- Board driver → DDR calibration: supports calibration (evidence: `opentpu/host/selfcal.py`)
- Tracing and inspection → ISA simulator: inspects execution (evidence: `opentpu/lens.py`)
- Tracing and inspection → Execution runtime: inspects runtime (evidence: `opentpu/lens.py`)
