Phase 5 — Everything that isn’t matmul

4 weeks · Part of Zero to NPU · Prev: phase-4-memory · Next: phase-6-sequencer

Amdahl's ambush

You made matmul 5× faster. The other 10% is now 35% of runtime. This is the phase your week-4 profile warned you about.


5.1 The vector unit

Build a small shared datapath for element-wise work — ~12 DSPs and a few BRAMs for lookup tables.

OpApproachNote
RMSNormSum of squares → reciprocal-sqrt via LUT + one Newton–Raphson stepNeeds int32 accumulate; watch overflow on the sum
SoftmaxSubtract row max, exp via 256-entry LUT, sum, reciprocalTwo-pass, or implement online softmax (Milakov & Gimelshein) for one pass
SiLU / SwiGLU256-entry int8→int8 LUTOne BRAM. Free.
RoPEPrecompute sin/cos tables per position in BRAMCompute once at init, not per token
  • RMSNorm
  • Softmax
  • SiLU / SwiGLU
  • RoPE

Done when: each op matches the golden model bit-exactly, and end-to-end runtime drops by the amount your profiler predicted.

Gotcha — softmax overflow

Softmax without max-subtraction overflows and gives you NaN-equivalents in fixed point.

Do not skip that step because “the values are small.” in int32 saturates faster than your intuition expects.