harishsg993010/tiny-NPU
opensource NPU for LLM inference (this run gpt2)
What it solves
NPU architectures from NVIDIA (Tensor Cores), Google (TPU), and Apple (Neural Engine) are proprietary, so there is almost nothing to learn from at the hardware (RTL) level. Existing open-source options are either research papers or complex implementations that prioritize completeness over readability. tiny-npu fills that gap with a minimal, fully synthesizable neural processing unit in SystemVerilog, purpose-built for understanding how NPUs actually work from the ground up.
How it works
tiny-npu is a chip that executes neural network inference layer by layer. It has two execution modes selected by a control register:
- LLM Mode (default) runs real transformer models (GPT-2, LLaMA, Mistral, Qwen2) using INT8 quantized weights from HuggingFace. A host CPU loads weights into external DDR, writes a microcode program into on-chip SRAM, and starts a microcode controller that fetches 128-bit instructions, decodes them, and dispatches them to hardware engines via a scoreboard. A barrier instruction stalls the controller until all engines finish, which is needed between dependent operations.
- Graph Mode compiles ONNX models into graph ISA instructions, tensor descriptors, and a DDR image. A 3-stage graph pipeline (fetch, decode, dispatch) walks the computation graph automatically, looking up tensor descriptors from a 256-entry table and dispatching to 13 hardware engines.
Both modes share the same compute core and on-chip SRAM. The core compute unit is a 16x16 weight-stationary systolic array with dual MAC units per processing element, supporting both INT8 (INT32 accumulation) and FP16 (FP32 accumulation) datatypes. Matrices larger than 16x16 are tiled automatically, and a post-processing unit requantizes results back to INT8 to minimize SRAM bandwidth.
Memory is split between two on-chip SRAM banks (SRAM0 at 64KB for weights and activations, SRAM1 at 8KB for LayerNorm betas and residuals) plus external DDR for all-layer weights. In LLM Mode, QKV weights use a head-blocked layout so each attention head's weights can be addressed directly, and per-head attention buffers are reused across heads. In Graph Mode, the ONNX compiler allocates SRAM0 dynamically with a first-fit memory planner.
The 13 dedicated hardware engines are: GEMM (dual INT8/FP16 MAC), Softmax (3-pass fixed-point), Reduce (Sum/Max/Mean), Math (Exp/Log/Sqrt/Rsqrt via LUTs), Gather, Slice, Concat, AvgPool2D, MaxPool2D, Pad, Resize Nearest, Cast, plus inline element-wise ops. Graph Mode also supports async dispatch with scoreboard-based DMA+GEMM overlap and hardware performance counters.
Who it's for
Students, engineers, and researchers who want to understand how NPUs work at the hardware level — how systolic arrays multiply matrices, how softmax and layer normalization are computed without floating point, how a microcode controller sequences a full transformer block, how KV-cache makes autoregressive decoding faster, and how a hardware FSM walks an ONNX graph. It is explicitly optimized for learning rather than production completeness.
Highlights
- Runs real GPT-2, LLaMA, Mistral, and Qwen2 inference on actual HuggingFace weights through INT8 quantized inference with multi-head attention
- Two execution modes: LLM Mode (transformer inference) and Graph Mode (ONNX models) sharing the same compute core
- 13 dedicated hardware engines covering GEMM, Softmax, Reduce, Math LUTs, Gather, Slice, Concat, pooling, padding, resizing, and casting
- 16x16 systolic array with dual INT8/FP16 MAC units, parameterizable to 24x24
- KV-cache optimization for faster autoregressive decoding
- ONNX compiler supporting Gemm, Conv, Reduce, Exp/Log/Sqrt, Gather, Slice, Concat, BatchNorm, AvgPool, MaxPool, Pad, Resize, Cast and more
- Async dispatch with scoreboard-based DMA+GEMM overlap and hardware performance counters
- Verified across 50 fuzz-generated random graphs, cycle-accurate against Python and C++ golden models
- Full Verilator simulation with cycle-accurate verification and fully documented RTL
- Fully synthesizable, with mode switching requiring only a register write (no reset)
Related
- Project
- Project
- Project
- Project