/model
The cycle-accurate model
The model computes C = AB (A is M × K, B is K × N) on an R × C array. A frame is the state of every register after the clock edge that ends a cycle, computed only from the previous frame and the values entering at the edges, so data moves exactly one PE per cycle. A PE holds up to four registers: s, the stationary operand; h, the operand moving right; v, what moves down (a partial sum, or in output-stationary an operand); and acc, output-stationary's accumulator. Each register holds a tag naming the element of A, B or C it carries, so the captions can say which value is where. Source: reference/systolic.py and its exact TypeScript port src/lib/sa/model.ts.
Dataflows and closed forms
The array block each dataflow needs, what stays in it, where and when each product happens, the cycles of one pass (load, compute and drain) and the partial sums moved between PEs. The tests count all of these from the frames.
| Dataflow | Block | Stationary | Product | Cycles | Partial-sum hops |
|---|---|---|---|---|---|
| weight-stationary | K × N | B (weights) | aₘₖ in PE(k, n) at m + k + n | K + (M + K + N − 2) | M N (K − 1) |
| output-stationary | M × N | C (outputs) | aₘₖ, bₖₙ in PE(m, n) at k + m + n | (K + M + N − 2) + M | N M (M − 1) / 2 |
| input-stationary | K × M | A (inputs) | bₖₙ in PE(k, m) at n + k + m | K + (N + K + M − 2) | M N (K − 1) |
For a 128 × 128 × 128 problem on a 128 × 128 array:
| Dataflow | Cycles | Buffer reads | Buffer writes | PE-to-PE hops | Partial-sum hops | Register writes |
|---|---|---|---|---|---|---|
| weight-stationary | 510 | 32,768 | 16,384 | 5,201,920 | 2,080,768 | 5,251,072 |
| output-stationary | 510 | 32,768 | 16,384 | 5,201,920 | 1,040,384 | 7,331,840 |
| input-stationary | 510 | 32,768 | 16,384 | 5,201,920 | 2,080,768 | 5,251,072 |
The RTL cross-check
The design is a parameterised N × N output-stationary array with internal skew registers, from the author's Interview RTL challenge (modules pe and systolic_array, vendored unchanged at commit b37984f). rtl/tb_trace.sv drives one matrix multiply as the challenge's own testbench does and prints every accumulator after every rising edge; rtl/check_rtl.py builds it in Verilator 5.020, runs it and compares. Edge p is the model's compute cycle p − 1; after the last product (edge K + 2(N − 1)) the RTL holds its results and pulses valid_out, so there the check requires C = AB. Run with --shift 1, comparing each edge with the next cycle, it reports mismatches in every case: the comparison can fail. The same testbench in Vivado xsim 2025.2 gives byte-identical traces (run locally; CI has no Vivado).
| Case | N | K | Values in | Edges | Accumulators compared | Mismatches | valid_out at edge |
|---|---|---|---|---|---|---|---|
| n4_k4_small | 4 | 4 | -4 … 5 | 14 | 224 | 0 | 10 |
| n4_k7_int8 | 4 | 7 | -128 … 127 | 17 | 272 | 0 | 13 |
| n3_k5_small | 3 | 5 | -4 … 5 | 13 | 117 | 0 | 9 |
The weight-stationary array has its own RTL (below). The input-stationary dataflow is not checked against RTL; it is checked against a direct matrix multiply, its textbook timing and its closed forms.
The processing element against its RTL
Chapter 6's PE is the author's three-stage mixed-precision MAC unit (modules fp16_mul_to_fp32, fp32_add and mac_unit_mixed_precision of the MAC unit challenge, vendored unchanged at commit b37984f as rtl/mac_unit.sv). rtl/tb_mac_trace.sv drives one operation per cycle and prints every pipeline register after every rising edge; the same script compares them with reference/pe.py, which models the unit register for register (FP16 mode: the stage-1 operands, the exact FP32 product, the FP32 accumulator, valid and clear; INT8 mode: the operand bytes, the INT16 product and the INT32 accumulator). With --shift 1 every case reports mismatches. The same five runs in Vivado xsim 2025.2 give byte-identical traces (run locally). The site's bfloat16 mode has no RTL counterpart and is checked against NumPy.
| Case | Mode | Operations | Valid | Dot products | Edges | Registers compared | Mismatches |
|---|---|---|---|---|---|---|---|
| mac_fp16_demo | FP16 | 8 | 8 | 2 | 11 | 99 | 0 |
| mac_int8_demo | INT8 | 8 | 8 | 2 | 11 | 99 | 0 |
| mac_fp16_wide | FP16 | 400 | 331 | 25 | 403 | 3,627 | 0 |
| mac_fp16_long | FP16 | 400 | 316 | 4 | 403 | 3,627 | 0 |
| mac_int8_rand | INT8 | 400 | 319 | 25 | 403 | 3,627 | 0 |
The weight-stationary array against its RTL
rtl/systolic_array_ws.sv is chapter 5's machine in SystemVerilog, written for this site: an R × C grid of PEs, each with a stationary weight, a shadow weight, an activation and a partial-sum register (valid bit, 2-bit tile tag, value). Weights shift down each column's shadow chain; in load step l rows 0 … l shift and the rows below hold (a per-column step counter restarted by w_first). A PE latches an arriving activation only if its stationary or shadow weight carries the same tile tag, and swaps the shadow weight in on that edge. Row 0 starts each partial sum at zero; a tile of k rows completes its sums in row k − 1. rtl/tb_ws_trace.sv replays the model's edge inputs (the weights entering each column and the skewed activations entering each row, from simulate_ws_stream) and prints every register after every rising edge; edge t + 1 is the model's cycle t. The check compares all four registers of every PE (an empty register's stale bits are not compared), reads the results back from each tile's last row and requires them to equal AB, and records the negative control: every case mismatches against the model one cycle late. The testbench drives the schedule, so this checks the array, not a controller. Two mutations (shifting every shadow row on every load step; latching activations whatever their tile) were each caught by the check when it was written.
| Case | M × K × N | Array | Weights | Tiles | Edges | Registers compared | Mismatches | Shifted one cycle | C = AB |
|---|---|---|---|---|---|---|---|---|---|
| ws_demo | 6 × 6 × 6 | 3 × 3 | shadow | 4 | 31 | 1,116 | 0 | 511 | yes |
| ws_ragged | 5 × 6 × 6 | 4 × 4 | shadow | 4 | 26 | 1,664 | 0 | 500 | yes |
| ws_int8 | 8 × 8 × 8 | 4 × 4 | shadow | 4 | 42 | 2,688 | 0 | 1,275 | yes |
| ws_m1 | 1 × 6 × 6 | 3 × 3 | shadow | 4 | 17 | 612 | 0 | 249 | yes |
| ws_single | 6 × 6 × 6 | 3 × 3 | single | 4 | 52 | 1,872 | 0 | 578 | yes |
| ws_hbm | 6 × 6 × 6 | 3 × 3 | shadow, HBM 1 w/cycle, 1 buffer | 4 | 64 | 2,304 | 0 | 542 | yes |
| ws_nine | 12 × 10 × 9 | 4 × 4 | shadow | 9 | 113 | 7,232 | 0 | 2,497 | yes |
The panel draws the first case from the simulator's VCD (rtl/ws_wave.json, written by the same script; it checks that the VCD and the printed trace agree) beside the TypeScript model.
Loading the waveform…
Synthesis
rtl/synth_yosys.py synthesises the RTL with Yosys 0.69 (the YoWASP WebAssembly build, pinned in rtl/requirements-synth.txt, so it gives the same netlist on any machine; CI repeats it and fails if rtl/synth.json changes). The MAC unit goes through sv2v 0.0.13 first, because Yosys's front end does not accept its break. Two flows:
- Generic gates:
synth -flatten; ltp -noff; dfflegalize -cell $_DFF_P_ 0; stat -tech cmos. Gates are Yosys's internal two-input cells and MUX; depth is the number of gates on the longest path between flip-flops or ports (ltp); transistors is Yosys's estimate for static CMOS after every flip-flop becomes a plain D flip-flop (its enable and reset become gates). It is an area proxy with no standard-cell library, no wires, no process node and no timing. - AMD 7-series:
synth_xilinx -family xc7 -flatten; ltp -noff. Yosys's own FPGA mapping, not Vivado's, with no placement or routing, so no Fmax. Multiplies map to DSP48E1 slices; the output-stationary PE's multiply-accumulate folds into one DSP48 with its accumulator, which is why that array needs almost no LUTs.
| Design | Gates | Flip-flops | Depth | Transistors (est.) | 7-series LUTs | FFs | DSP48 | CARRY4 |
|---|---|---|---|---|---|---|---|---|
| Weight-stationary PE (INT8, shadow weight) | 869 | 65 | 66 | 9,070 | 13 | 33 | 1 | 0 |
| Output-stationary PE (INT8) | 852 | 49 | 62 | 8,010 | 1 | 17 | 1 | 0 |
| MAC unit, INT8 mode only | 741 | 69 | 51 | 7,258 | 66 | 37 | 1 | 9 |
| MAC unit, FP16 mode only | 2,495 | 100 | 174 | 19,652 | 705 | 101 | 1 | 51 |
| MAC unit, both modes | 3,238 | 148 | 176 | 26,056 | 812 | 133 | 2 | 62 |
| Design | Gates | Flip-flops | Depth | Transistors (est.) | 7-series LUTs | FFs | DSP48 | CARRY4 |
|---|---|---|---|---|---|---|---|---|
| Weight-stationary array 4 × 4 | 9,130 | 872 | 66 | 102,010 | 228 | 540 | 16 | 4 |
| Weight-stationary array 8 × 8 | 46,000 | 3,832 | 68 | 493,390 | 912 | 2,144 | 64 | 8 |
| Weight-stationary array 16 × 16 | 203,392 | 16,000 | 68 | 2,145,552 | 3,671 | 8,528 | 256 | 32 |
| Output-stationary array 4 × 4 | 13,594 | 816 | 63 | 128,870 | 2 | 176 | 16 | 0 |
| Output-stationary array 8 × 8 | 54,370 | 3,456 | 64 | 520,718 | 2 | 1,152 | 64 | 0 |
| Output-stationary array 16 × 16 | 217,287 | 14,208 | 64 | 2,092,474 | 2 | 5,504 | 256 | 0 |
| Design | Gates | Flip-flops | Depth | Transistors (est.) | 7-series LUTs | FFs | DSP48 | CARRY4 |
|---|---|---|---|---|---|---|---|---|
| Unsigned 4 × 4 multiplier | 69 | 0 | 14 | 480 | 22 | 0 | 0 | 2 |
| Unsigned 8 × 8 multiplier | 343 | 0 | 30 | 2,452 | 0 | 0 | 1 | 0 |
| Unsigned 11 × 11 multiplier | 680 | 0 | 42 | 4,844 | 0 | 0 | 1 | 0 |
| Unsigned 16 × 16 multiplier | 1,510 | 0 | 63 | 10,756 | 0 | 0 | 1 | 0 |
| Unsigned 24 × 24 multiplier | 3,456 | 0 | 91 | 24,520 | 0 | 0 | 2 | 0 |
The arrays scale with their PE count while the logic depth hardly moves: a systolic array's wires are all neighbour to neighbour. The 16 × 16 output-stationary array has 16 times the 4 × 4's PEs and 16.2 times its transistors. The weight-stationary array grows faster (21.0 times), because its top row's PEs need no adder (their partial sums start at zero): a quarter of a 4 × 4, a sixteenth of a 16 × 16. Both arrays expose every PE's registers as ports, as the trace check needs, so no register is optimised away.
Tiles back to back
Chapter 5 adds a shadow weight register to every PE and runs a GEMM's weight-stationary tiles back to back (simulateWsStream). The model checks, every cycle, that a PE swaps in the weight of the tile whose activation has just arrived and that every partial sum meets its next product in step; moving one tile's load or stream a cycle earlier makes it stop with an error. Its cycle counts equal the closed form Sj+1 = max(Sj + M, Lj+1 + kj+1), and with the shadow registers off they equal chapter 4's tiles in sequence.
The lowering against Torch_Sim_Frontend
Chapter 9 lowers a small ONNX model onto the array. scripts/check_simfront.py runs the author's Torch_Sim_Frontend (commit de3acd4) on the same file: its ONNX front end and GEMM rule give the same shapes, and its cycle-approximate array formula the same numbers, on a 8 × 8 array.
| Node | simfront category | GEMM (batch, M, K, N) | simfront cycles | Agree |
|---|---|---|---|---|
| Conv | matmul | 1 × 36 × 27 × 8 | 151 | yes |
| Relu | elementwise | none | – | yes |
| Flatten | view | none | – | yes |
| Gemm | matmul | 1 × 1 × 288 × 10 | 592 | yes |
Row-stationary, as a reference
For a convolution, Eyeriss's row-stationary dataflow gives PE(i, j) filter row i and input row i + j; each PE slides its filter row along its input row, one MAC per cycle, and each column of PEs adds its rows into one output row. The model runs it too (simulateRs): a 6 × 7 input and a 3 × 3 filter take 17 cycles on 3 × 4 PEs, and the output equals a direct convolution. Chapter 8 animates it.