openTPU README

项目的 README,来自 github.com/FeSens/openTPU。合并为一个 markdown 文件:README.md。

openTPU

An open-source AI accelerator, developed by AI.

openTPU brings the lessons of auto-arch-tournament to AI accelerators. It asks two questions: how far can AI agents go at hardware design, and can they build the chip that runs their own inference?

otpu-chat on LFM2.5-230M with otpu-smi watching the card

otpu-chat running LFM2.5-230M on the FPGA card (left), with otpu-smi showing the card's utilization and DRAM bandwidth (right).

A place to learn

openTPU is also a learning project. The whole accelerator lives in one small monorepo that you can read end to end: the hardware design (SystemVerilog), the instruction set, a bit-exact simulator, a kernel language and its compiler, and the host software that drives a real PCIe card. If you want to understand how an AI accelerator works, from a matmul in Python down to the wires, this is a good place to start.

Results

The design runs ten modern models with their real weights on an Inspur YPCB-00338 card (Xilinx Kintex-7 xc7k480t, two DDR3 channels), and the card produces the same tokens as the simulator, bit for bit.

Model Weights Decode, device Decode, wall Prefill, device DRAM while decoding
LFM2.5-230M int8 59.0 tok/s 52.3 tok/s 295.6 tok/s 14.5 GB/s (85% of peak)
LFM2.5-230M 4-bit, int8 head 85.8 tok/s 82.1 tok/s 335.4 tok/s 14.1 GB/s (82%)
Qwen3-0.6B int8 21.6 tok/s 21.3 tok/s 92.1 tok/s 14.4 GB/s (84%)
Qwen3-0.6B 4-bit, int8 head 31.3 tok/s 30.7 tok/s 103.4 tok/s 13.9 GB/s (82%)
Qwen3.5-0.8B int8 17.6 tok/s 16.3 tok/s 61.4 tok/s 14.5 GB/s (85%)
Qwen3.5-0.8B 4-bit, int8 head 24.5 tok/s 23.3 tok/s 66.7 tok/s 14.1 GB/s (83%)
Gemma 4 E2B 4-bit, int8 head 10.57 tok/s 10.53 tok/s 32.1 tok/s 15.6 GB/s (92%)
Gemma 4 E2B 4-bit, 4-bit head 12.14 tok/s 12.09 tok/s 29.9 tok/s 15.5 GB/s (91%)
LFM2-2.6B int8 6.05 tok/s 6.03 tok/s 21.4 tok/s 16.1 GB/s (94%)
LFM2-2.6B 4-bit, int8 head 10.96 tok/s 10.93 tok/s 20.6 tok/s 15.8 GB/s (93%)
SmolLM3-3B int8 5.00 tok/s 4.99 tok/s 21.1 tok/s 16.0 GB/s (94%)
SmolLM3-3B 4-bit, int8 head 8.74 tok/s 8.72 tok/s 22.8 tok/s 15.7 GB/s (92%)
Phi-4-mini (3.8B) int8 3.99 tok/s 3.98 tok/s 13.8 tok/s 16.0 GB/s (94%)
Phi-4-mini (3.8B) 4-bit, int8 head 6.56 tok/s 6.55 tok/s 15.0 tok/s 15.8 GB/s (92%)
Qwen3.5-2B int8 8.02 tok/s 8.00 tok/s 38.2 tok/s 16.0 GB/s (94%)
Qwen3.5-2B 4-bit, int8 head 12.09 tok/s 12.03 tok/s 41.7 tok/s 15.8 GB/s (92%)
Qwen3.5-4B 4-bit, int8 head 5.88 tok/s 5.87 tok/s 12.9 tok/s 15.7 GB/s (92%)
Gemma 4 E4B int8, 4-bit head and down 0-23 3.78 tok/s 3.75 tok/s 14.8 tok/s 16.0 GB/s (94%)

Measured on the card: the first three models on 2026-09-29 with the production image deploy_champ_e698dcd7. LFM2-2.6B, SmolLM3-3B and Phi-4-mini on 2026-09-30, and Qwen3.5-2B and 4B and Gemma 4 on 2026-10-01, with build B, deploy_fused133c_79c5707a, production since then. Build B decodes LFM2-2.6B, SmolLM3 and Phi-4-mini 8-9% faster than e698dcd7 (Gemma 4 E2B 10%), at 91-94% of the DRAM peak instead of 82-87%. Qwen3.5-4B's int8 image is over 4 GiB.

With the logits streamed back while the card runs (tools/decode_profile.py, 96 tokens), 4-bit decode is faster, in device / wall tok/s:

The previous production image, se-cand3, was built with the Xilinx MIG, a two-column matrix unit and a 120.755 MHz clock. Measured the same way, the new image:

The earlier images and their numbers are in docs/board.md, section 5.

Mixture-of-experts models bigger than the card's 4 GiB run with their experts streamed from host storage (docs/offload.md, section 10). The card routes each token and computes every expert, and it keeps the experts in per-layer slots in its DRAM. The host only copies missing experts from a pool file into those slots, at the link's rate (section 10.1). Measured on 2026-10-01 with build B (79c5707a), the card's own decode loop picking every token, 4-bit experts, int8 head:

otpu-chat chats with both, and with gemma-4-26B-A4B-it (--model lfm2-8b, qwen35-35b, gemma4-26b), with the same engine and settings. Each model's experts are packed once into a pool file beside its checkpoint, and the chat checks the pool before it loads (docs/offload.md, section 14).

4-bit weights (docs/quant.md) use FP4 values with two-level block scales, 4.25 bits per weight, and keep the LM head in int8 for accuracy. They cut the bytes per token by about a third and raise decode speed by 40% (Qwen3.5) to 45% (Qwen3, LFM2), at a measurable cost in perplexity that docs/quant.md reports per model.

The host is nearly out of the way. For LFM2 and Qwen3 the card runs one decode program compiled once, which reads the position from a register and looks up its own embedding and RoPE rows, and the logits stream back while the card is still running: the host adds 0.17 to 0.30 ms per token on the build host (0.45 to 1.3 ms on the card host). Qwen3.5 runs the same way for decode; its prefill still compiles each chunk's program on the host, ahead of the card.

On a BCU1525 (Virtex UltraScale+ VU9P)

The same design builds for the BCU1525: a VU9P with four DDR4-2400 DIMMs (76.8 GB/s at peak) and PCIe Gen3 x16. Here the large models run with all of their weights on the card, and the host stores, moves data and tokenizes. One slice at 150 MHz on AMD's DDR4 controller (target bcu1525-wg:bhw16), real weights, eight tokens and their logits equal to the simulator's bit for bit:

Model Decode, device DRAM while decoding
Qwen3.6-35B-A3B 34.7 tok/s 88.5% of peak
Qwen3.8-27B 4.77 tok/s 90.6% of peak
GPT-OSS-20B 27.1 tok/s (zero-weight image) about 88% of peak

As a chat, the 35B keeps its whole 262,144-token context on the card and decodes at 33.3 tok/s, with a first token after 1.1 to 1.6 s on short prompts (docs/bcu1525_wg.md).

Two cards as one machine. Two BCU1525s joined by QSFP28 cables at 25 Gb/s run four slices tensor-parallel from one compile and one bitstream (target bcu1525-x2:st16, docs/x2.md, docs/x2_link.md). Every qualification run ends with instruction counts, logits and DRAM bytes equal to the simulator's. The 35B's chat decodes at 65.1 tok/s (1.96x one card) and reads a 2,689-token paste at 97.6 prompt tokens a second (2.27x). One difference is open: at a 30,870-token context the reply on two cards departs from one card's (docs/x2.md, section 8).

Several requests at once. With four sequence slots on two cards, the device steps four requests at about 103 tok/s in all; by the wall clock four 256-token requests summed to 81.3 tok/s (docs/batch35.md, docs/serve.md).

What it took is written up beside the code: weights read in groups of eight columns (docs/weight_groups.md), memory channels native to the matrix unit (docs/native_channels.md), a stream engine of its own for recurrent state (docs/se_engine.md), an instruction cache (docs/icache.md), and the studies that did not pay (docs/pf500.md, docs/wide_mxu.md).

How it works

  Kernels in ol              mlp, attention, full model layers
        |  @ol.jit
  Language + compiler        layouts, affine loop addressing, fusion
        |
  ISA                        8 x 32-bit words per instruction
        |
  ISA simulator  <======>  RTL          same bits, checked by the tests
  (Python)                 (SystemVerilog)
                            |  Vivado bitstream
                           FPGA card    Kintex-7 xc7k480t
                            |  PCIe
                           Host         otpu-chat, otpu-smi, otpu-lens

The machine is deliberately simple. A sequencer issues one instruction per cycle to a few units: DMA moves data, the matrix unit multiplies int8 weights streamed from DRAM, the vector unit does fp32 math, and a quantizer turns results back into int8. There is no cache and no hidden scheduling: every data movement is an instruction, so a trace shows exactly where the cycles go. docs/isa.md describes the whole instruction set.

A kernel looks like this:

from opentpu import language as ol

@ol.jit
def mlp(h, gamma, w_gate, w_up, w_down, out, eps):   # simplified; see kernels/mlp.py
    x = ol.load(h)
    xs = ol.quantize(rmsnorm(x, ol.load(gamma), eps))
    g = ol.dot(xs, w_gate)
    u = ol.dot(xs, w_up)
    a = ol.all_gather(silu(g) * u)
    y = ol.all_gather(ol.dot(a, w_down))
    if ol.program_id() == 0:
        ol.store(out, x + y)

Because every data movement is an instruction, a trace of a run explains its speed. Lens, the profiler, records a run from the RTL, the simulator or the card and opens it in the browser, with a roofline, a timeline and per-instruction tables (docs/lens.md).

Lens replaying a Qwen3 decode step on the floorplan

Lens replaying part of a Qwen3 decode step. Colours show what each unit is doing in each cycle: busy, waiting on DRAM, or waiting on another instruction.

ASIC flow

Besides the FPGA card, openTPU also builds as a chip layout: an ASIC block on the open NanGate45 (45 nm) cell library, with 33 SRAM macros and ~560k standard cells. It is built end-to-end by AI agents, from the SystemVerilog source to a routed layout, on open-source tools only: Yosys for synthesis, OpenROAD for place and route, and OpenLayout's agent skills for the physical-design decisions.

RTL → synthesis → floorplan → macro placement → place / CTS / route → timing (STA)
 ↑                           (OpenLayout agents)   (timing repair)          │
 └──────────── change the RTL, verify it bit-exact ←────────────────────────┘

What each step does

Use it on your own design. docs/asic_timing_loop.md describes the loop step by step, includes a ready-to-fill prompt that hands the whole loop to a coding agent (any design, any node), and records the openTPU run and the pitfalls met on the way.

Try it

Everything except the card runs on a laptop.

pip install -e .
pip install pytest torch transformers
python3 -m pytest -q          # RTL tests also need Verilator 5

hf download LiquidAI/LFM2.5-230M --local-dir models/LFM2.5-230M
otpu-chat --model lfm2 --backend isa    # chat on the simulator

With a card, build the bitstream (make bit in boards/ypcb-00338), load it over JTAG, then run sudo otpu-setup and otpu-chat --backend board. docs/board.md walks through the bring-up.

On a BCU1525: make wg in boards/bcu1525 builds the one-card image and make x2 the two-card one (boards/bcu1525/README.md, docs/bcu1525.md). With more than one card in a host, otpu-chat, otpu-serve and otpu-smi take --dev or OTPU_DEV; without it they use the only card that answers as openTPU. The 35B's front ends for one card or both are in boards/bcu1525/chat35.

Command What it does
otpu-chat chat with Qwen3-0.6B, LFM2.5-230M (--model lfm2), Qwen3.5-0.8B (--model qwen35), LFM2-2.6B (lfm2-2.6b), SmolLM3-3B (smollm3), Phi-4-mini (phi4-mini) or Qwen3.5-2B / 4B (qwen35-2b, qwen35-4b), Gemma 4 E2B / E4B (gemma4, gemma4-e4b: the instruct checkpoints, gemma-4-E2B-it / E4B-it); MoE models bigger than the card with their experts streamed: LFM2.5-8B-A1B (lfm2-8b), Qwen3.5-35B-A3B (qwen35-35b), gemma-4-26B-A4B-it (gemma4-26b); a checkpoint without a chat template (a base model) only with --raw
otpu-serve an OpenAI-style API server (/v1/chat/completions, /v1/completions, streamed or not; SGLang's /generate) for many clients at once: their contexts kept on the card and in host RAM / on disk (a prompt continuing a saved one feeds only the rest), their decode steps batched on the card for Qwen3 and the Llama-likes, every token picked on the card (Qwen3-0.6B: 78 tok/s with 4 clients, 25 alone); docs/serve.md
otpu-smi temperature, power, DRAM bandwidth and per-unit utilization; -l --csv logs a time series
otpu-lens record a run and open it in the profiler (summary prints the overview in the terminal)
otpu-selftest, otpu-diag check that the card works

Validating against Hugging Face

tools/validate.py checks a device's greedy tokens and logits against a CPU golden: the same checkpoint in Hugging Face transformers, fp32 on the CPU, with openTPU's quantization. The golden's weights are the values the matrix unit multiplies (the model image's own quantizer, in the formats you pick), and its activations are rounded to int8 per 128 values wherever the device's quantizer rounds them: every matmul input, and in attention the query, K, V and the softmax weights. The device is the ISA simulator, the RTL (Verilator) or the card, and --against adds a second device that must give the same tokens and bit-identical logits. It is measured on Qwen3-0.6B, LFM2.5-230M, Qwen3.5-0.8B and Gemma 4 E2B (base and instruct); the MoE models are not supported.

python3 tools/validate.py --model qwen3                                    # ISA simulator, int8
python3 tools/validate.py --model lfm2 --wformat fp4 --head-format int8   # 4-bit layers
python3 tools/validate.py --model gemma4                                   # E2B-it: 16 GB (below)

# the RTL against the ISA simulator, bit for bit: slow, so one prompt and a few tokens
# (this one takes 4 minutes on a 16-core host, the Verilator build included)
python3 tools/validate.py --model lfm2 --backend rtl --against isa --tokens 3 "The capital of France is"

# the card against the ISA simulator, bit for bit, on the card host
otpu-lock -- python3 tools/validate.py --model qwen3 --backend board --against isa

# or only the card's run under the lock, and the rest on another host: the ISA simulator then
# runs in the configuration the card ran in
otpu-lock -- python3 tools/validate.py --model qwen3 --backend board --no-golden --save card.npz
python3 tools/validate.py --model qwen3 --against card.npz

Each prompt (eight by default, 16 tokens each) prints the device's continuation and the goldens'. Then the device's own tokens are fed to the golden, so that every step compares the same context, and the tool prints the top-1 agreement, the mean and largest KL divergence, the largest logit difference and the lowest cosine. The summary has four rows:

Row What it is
device vs W+A the device against the quantized golden: the check
device vs fp32 the device against the plain checkpoint
W+A vs fp32 what the quantization alone costs
W+A~ vs W+A the golden against itself with its inputs nudged by one fp32 ulp: the floor

The golden cannot round exactly as the device does: its sums, norms and exponentials differ in the last bits. Once one int8 value rounds the other way, the difference spreads through the layers after it. So on a real model the device sits about as far from the quantized golden as the golden sits from itself after a one-ulp nudge, and a healthy device is near that floor. With 4-bit weights the floor is far below the fp32 rows. Measured on the ISA simulator, with the eight default prompts and 16 tokens each:

Model Weights device vs W+A: top-1, KL floor KL device vs fp32: top-1, KL W+A vs fp32 KL
LFM2.5-230M int8 96.2%, 0.0038 0.0039 96.2%, 0.0055 0.0050
LFM2.5-230M 4-bit, int8 head 94.6%, 0.0045 0.0041 80.6%, 0.152 0.157
Qwen3-0.6B int8 96.1%, 0.0088 0.0109 96.1%, 0.011 0.012
Qwen3-0.6B 4-bit, int8 head 96.9%, 0.0123 0.0163 85.9%, 0.193 0.195
Qwen3.5-0.8B int8 96.1%, 0.0020 0.0017 97.7%, 0.0033 0.0029
Qwen3.5-0.8B 4-bit, int8 head 96.9%, 0.0020 0.0021 88.3%, 0.076 0.077
Gemma 4 E2B (base) int8, 4-bit PLE table 100.0%, 0.0036 0.0035 99.2%, 0.015 0.017
Gemma 4 E2B (base) 4-bit, int8 head and PLE table 99.2%, 0.0029 0.0036 96.9%, 0.045 0.043
Gemma 4 E2B-it int8, 4-bit PLE table 100.0%, 0.0071 0.0078 97.5%, 0.014 0.0085
Gemma 4 E2B-it 4-bit, int8 head and PLE table 98.6%, 0.0095 0.0136 93.1%, 0.102 0.093
Gemma 4 E2B-it the base's mix, int8 head and PLE table 100.0%, 0.0135 0.0075 98.6%, 0.028 0.036

KL is the mean KL(golden || device) in nats per token. The device's KL from the quantized golden is 0.70 to 1.16 times the floor's, and its distance from fp32 is what the quantization alone predicts. A run takes 2 to 14 minutes on a 16-core host (Gemma 4 E2B: 16 and 26, the host shared with other jobs; E2B-it 8 to 14 on the build host alone) and peaks at 2.5 GB (LFM2) to 16 GB (Gemma 4 E2B). Gemma 4 E2B-it, the instruct checkpoint otpu-chat runs (2026-10-09, tree aa2166b1), answers most prompts in a few tokens and stops at its end of turn (<turn|>): its rows have 71 to 81 positions against the base's 128, so one position weighs more. The mix's 1.80x is one position of 71, a near tie in def fibonacci(n):'s (KL 0.69, the floor's 0.17 there); without it, 0.75x. The prompts are plain text after <bos>, as the base's, which the instruct tokenizer does not add (gemma4.text_ids).

Qwen3's activation rounding costs more than the other models': its k_norm gains have outlier channels (Qwen3-0.6B: up to 42 times their median), and K is rounded to int8 with one scale per token, head and 128 values, so the other channels, the ones the attention scores depend on, were left with a few levels. Its int8 W+A golden was 0.042 from fp32, against 0.0050 for LFM2.5-230M and 0.0029 for Qwen3.5-0.8B. The image now moves the outliers' magnitude from K into q (a power of two per channel in the q_norm and k_norm gains, which leaves q . K unchanged; docs/quant.md, "K smoothing"): 0.012 in the table. On two fixed texts (2,046 positions, paired) it takes the int8 W+A golden's KL from 0.059 to 0.015 and the 4-bit one's from 0.221 to 0.179, and the ISA simulator's on 512 of them from 0.064 to 0.019 (int8) and from 0.228 to 0.195 (4-bit, int8 head). The 4-bit row's 128 greedy positions are too few to show that drop (each run is on its own tokens, and a few positions dominate).

Gemma 4 runs in its image's formats, which follow the card's fit (E2B's int8 image keeps its per-layer-embedding table in 4-bit). The golden takes its embedding rows from the LM head, as the device gathers them, its per-layer embeddings from the device's PLE records, and it compares the logits after the soft cap. E2B in fp32 is about 20 GB, so Hugging Face loads the language model alone, without its 9.4 GB PLE table (the golden reads the rows it needs from the checkpoint), and the golden keeps the bf16 checkpoint's weights in bf16, which is exact, and widens them a matrix at a time for its fp32 passes. An E2B run peaks at 16 GB: give it a host with 22 GB free.

A run passes when the device agrees with the quantized golden on at least 80% of the top-1 tokens and its mean KL is at most three times the floor's (--min-top1, --kl-ratio), and, with --against, when both devices give the same tokens and bit-identical logits. The exit status is 0 for PASS and 1 for FAIL, and --json writes every step for scripts. --weights-only drops the activation rounding from the golden (then only the top-1 bound holds), and --no-fp32 skips the fp32 rows.

On the card (build 1025907f, 2026-10-07: --no-golden --save under the lock, --against on a build host), all six runs of Qwen3-0.6B, LFM2.5-230M and Qwen3.5-0.8B, in int8 and in 4-bit with an int8 head, gave the ISA simulator's tokens and bit-identical logits on every prompt (0 ulp over 103 to 128 steps a run). The card's mean KL from the quantized golden was 0.94 to 1.39 times the floor, with 94.5% to 97.7% top-1 agreement. Qwen3-0.6B int8's KL from fp32 on the card went from 0.052 (build 84989047, before K smoothing) to 0.012.

Where to start reading

  1. docs/isa.md: the instruction set. Everything else is built on it.
  2. opentpu/kernels and docs/compiler.md: how a kernel becomes instructions.
  3. opentpu/isasim.py: the simulator, which is the spec. docs/bend.md: the same simulator in Bend, with laws about its arithmetic and 10-15x isasim's speed.
  4. rtl/: the hardware, starting from rtl/top/otpu_top.sv.
  5. docs/lfm2.md, docs/qwen35.md, docs/llama.md, docs/benchmarks.md: whole models and where their cycles go.
  6. docs/board.md: the physical card, from clocks to PCIe.
  7. docs/targets.md: how one design builds for several boards and chips (a target file per build: targets/*.toml).
  8. docs/bcu1525.md, docs/bcu1525_wg.md and docs/bcu1525_s2.md: the VU9P card, its memory path at 88 to 91% of the DIMMs, and two slices. docs/x2.md: two cards as one machine.

What's next

Contributing

Issues and pull requests are welcome, and most of the work needs only Python and Verilator, not an FPGA. Changes to the ISA, the simulator or the RTL must keep python3 -m pytest -q passing, and performance claims should say how they were measured.

License

Apache License 2.0. See LICENSE.

openTPU (FeSens/openTPU)

正在搜索数据库…

这是什么?openTPU 是一个开源 AI 加速器,其硬件设计、指令集、模拟器、编译器和分析器全部由 AI 智能体打造,它能在 FPGA PCIe 板卡上运行 Qwen3、LFM2.5 等真实的大语言模型。
这是一款芯片设计、一个模拟器,还是可以安装的东西?所有这些都在同一个 monorepo 里:芯片的 SystemVerilog RTL、位精确的 Python 模拟器、带内核编译器的 ISA,以及驱动真实 PCIe 板卡的主机工具(otpu-chat、otpu-smi、otpu-lens)。
怎么试一试?除实体板卡外的一切都能通过模拟器在你的笔记本上运行。如果有板卡:在 boards/ypcb-00338 中用 'make bit' 构建比特流,通过 JTAG 烧录,运行 'sudo otpu-setup',然后运行 'otpu-chat --backend board'。
使用它需要 FPGA 板卡吗?只有想跑真实硬件时才需要——大多数开发只需要 Python 和 Verilator。硬件推理方面,项目针对的是配备 Xilinx Kintex-7(xc7k480t)和两条 DDR3 通道的浪潮 YPCB-00338 板卡。
主要的瓶颈或坑是什么?解码速度受 DRAM 带宽限制(达到 DDR3-1066 峰值的 82-94%),时钟在 133.33 MHz 下才勉强满足时序(WNS +0.032 ns),而超出板卡约 4 GiB DRAM 的混合专家模型必须从主机存储流式载入专家。
为什么要让 AI 智能体来设计 AI 加速器?这是一项实验兼学习项目,用来测试 AI 智能体在硬件设计上能走多远,以及它们能否造出一块能运行自身推理的芯片。
现在 openTPU 能运行哪些模型?它目前能运行十个真实模型——Qwen3-0.6B、Qwen3.5(0.8B/2B/4B)、LFM2.5-230M、LFM2-2.6B、SmolLM3-3B、Phi-4-mini 和 Gemma 4——视量化情况,速度约为 3 到 85 tok/s。
Lens 是什么?Lens 是 openTPU 内置的分析器;它会记录 RTL、模拟器或实体板卡上的一次运行,并在浏览器中打开,展示 roofline 图、时间线和逐指令表格。
它能运行超出板卡内存的混合专家模型吗?可以——像 Qwen3.5-35B-A3B(347 亿参数)这样的模型会把缺失的专家从主机存储流式载入设备上的 DRAM 槽位,槽位命中率视模型而定可达 62-98%。
作者
FeSens
大小
20.5 MB
星标
991
更新
提交Nov 11
时长
2个月
提交 · 90 天