Systolic engine

SystolicEngine (waveflow/linalg/systolic_engine.py) multiplies complex fixed-point matrices on an output-stationary array of R × C processing elements:

  • C = q(A·B) (operation MUL),
  • C = q(Aᴴ·B) (operation MUL_AH, the conjugate transpose of A), or
  • C = q(Aᴴ·A) (operation GRAM, the Gram matrix: MUL_AH with B = A, its one operand read once).

Every product and every sum is exact, and C is rounded and saturated once, to its own format, bit for bit like the model waveflow.linalg.matmul.matmul. Operands and results travel on streams: it holds a product’s operands on chip, up to Mmax × Pmax·Kmax × Nmax. For larger matrices, in memory, SystolicEngineMem runs it in an outer loop. How a systolic array works explains the array, the dimensions used here and what bounds its time. For what the linear-algebra components share – formats, lane groups, the build step – see Linear-Algebra Conventions.

All numbers here: Vitis HLS / Vivado xsim 2025.1, xczu48dr-ffvg1517-2-e, 4 ns, the centre configuration below, 12-bit operands.

What it computes

The values are the stored integers of each format. A is m × K (K × m for MUL_AH), B is K × n, and C is m × n. The sum of K products is held exactly at accumulator width, which covers Pmax · Kmax products.

The conjugate transpose. For MUL_AH the array holds X = Aᵀ, transposed but not conjugated, and computes Aᴴ·B = conj(X·conj(B)): it negates B’s imaginary part as B enters the array, in one more bit, and negates the exact imaginary sum before the rounding. The one bit matters: the conjugate of −2^(W−1) is +2^(W−1), which W bits cannot hold. The A loader does the transpose.

The Gram matrix. GRAM is MUL_AH with B = A: one burst a panel, the stored A (K × m), which the receiver forwards to both loaders in the same cycle, so the block crosses s_in once. The array computes it as MUL_AH does; the model is matmul(A, A, adjoint=True).

The multiply form. form=4 uses four real multiplies per complex product; form=3 the three-multiply (Gauss) form of waveflow.utils.complexutils.cmult3. The values are identical; the forms differ in DSPs and LUTs.

Parameters

Parameter Centre Meaning Constraint
Mmax, Kmax, Nmax 8, 8, 32 the largest m, panel kp and n R divides Mmax, C divides Nmax
Pmax 4 the most panels summed into one output: K ≤ Pmax · Kmax  
R, C 4, 8 the array: R rows by C columns of processing elements R ≤ L
L 4 lanes: outputs written per step a power of two dividing C
form 4 the complex product: 4 or 3 real multiplies 3 or 4
a, b, c required the formats of A, B and C signed; each at most lane_bits wide
word_bits 64 the stream word 32, 64, 128, 256 or 512; its elements (LW) must divide C
lane_bits 16 the width of each part of a memory element 2·lane_bits divides word_bits
sob_depth 2 blocks in each stream-of-blocks buffer  
clk 250 MHz the clock of the pysim timing  

The integer parameters are HwParams; they reach the C++ bodies as template arguments, and the formats through the format id (Linear-Algebra Conventions). The command and status ports stay 64 bits whatever word_bits (endpoint_port_widths); the data ports take the word.

The command

On s_in, a command is one EngineCmd burst, then its operands, one burst each:

Field Meaning
tx_id the command’s id, echoed in every Resp and its Status
op MUL, MUL_AH or GRAM
m, kp, n the output’s rows, a panel’s inner dimension, the output’s columns
npanels panels summed into each output: K = npanels · kp
nb outputs: each its own B (and its own A, unless keep_a)
keep_a one A for all nb outputs (one panel only)

The operands follow in the order the array takes them: with keep_a, A then the nb matrices B; for GRAM, per output and per panel, the one A; otherwise, per output and per panel, A then B. A is m × kp (kp × m for MUL_AH and GRAM) and B is kp × n, row-major memory elements (waveflow.linalg.lanes.to_words). Each output is C = q(Σ_p A_p·B_p), rounded once, and leaves on s_out as an EngineResp (tx_id, index, m, n) followed by C. After its outputs, the command’s EngineStatus (tx_id, status) goes to m_status.

The rules. 1 ≤ m ≤ Mmax and a multiple of R; 1 ≤ n ≤ Nmax and a multiple of C; 1 ≤ kp ≤ Kmax; 1 ≤ npanels ≤ Pmax; keep_a only with one panel; GRAM only with n = m, no keep_a, and on an engine whose a and b are one format (BAD_OP otherwise). A command that breaks one is answered BAD_OP or BAD_DIMS, and the receiver stops taking input until a reset – an error is fatal, the default command-response rule. The engine checks a command the way its receiver will:

from waveflow.linalg.matmul import MatmulOp
from waveflow.linalg.systolic_engine import SystolicEngine, command
from waveflow.simulation.simulation import Simulation
from waveflow.utils.fixputils import Format, OMode, QMode


def reg(W, I):                     # W bits, I of them integer; rounded and saturated
    return Format(W, I, True, QMode.AP_RND, OMode.AP_SAT)


eng = SystolicEngine(name="systolic_engine", sim=Simulation(), Mmax=8, Kmax=8, Nmax=32, Pmax=4,
                     R=4, C=8, L=4, word_bits=128, a=reg(12, 3), b=reg(12, 4), c=reg(12, 5))
for c in (command(1, MatmulOp.MUL, 8, 8, 32, npanels=4),    # K = 32: four panels, summed on chip
          command(2, MatmulOp.MUL, 8, 8, 32, npanels=5),    # K = 40: more panels than Pmax
          command(3, MatmulOp.MUL, 16, 8, 32),              # m = 16: more rows than Mmax
          command(4, MatmulOp.MUL_AH, 4, 3, 8, nb=2, keep_a=True)):  # one A for two outputs
    print(int(c.tx_id), eng.status(c).name)
1 OK
2 BAD_DIMS
3 BAD_DIMS
4 OK

To run another shape, pad with zeros: zero rows of A give zero rows of C, zero columns of B zero columns of C, and zero columns of A with matching zero rows of B add nothing.

Panels: a long inner dimension

Panels: A cut into column strips, B into row strips, C the rounded sum of their products

The buffers hold kp ≤ Kmax of the inner dimension at a time. A longer K is split into npanels panels: A into column strips A₀ … A_(P−1), each m × kp, and B into row strips B₀ … B_(P−1), each kp × n, so that

C = q( A₀·B₀ + A₁·B₁ + … + A_(P−1)·B_(P−1) )

Each panel is a product of the kind the theory describes; the panels’ sums add exactly, in a partial-sum store (Mmax × Nmax values at accumulator width, in LUTRAM), and C is rounded once, after the last. The accumulator is wide enough for Pmax · Kmax products, so npanels ≤ Pmax.

How the panels are iterated. A command has nb outputs of npanels panels each:

for each output:                          the receiver forwards, per panel, A_p and then B_p
    for each panel p:
        the A loader fills an X block, the B loader a B block   (double-buffered: the next
                                                                 panel's load overlaps this sweep)
        the core sweeps every tile of the panel -- row tiles, then column tiles -- P steps a tile:
            p = 0:          each tile starts from zero
            p > 0:          each tile starts from its partial sum in the store
            p < last:       each tile's sums drain into the store
            p = last:       each tile's sums are rounded once and drain out as C
    the collector places C in a block; the store writes the Resp, then C

The array runs on from one panel to the next: the next panel’s first tile starts the step after this panel’s last (the array, continuous). With keep_a there is one panel and one A for all nb outputs, the A loader repeating it.

Architecture

SystolicEngine's six tasks and the channels between them

Six free-running tasks, one command per firing each, every loop at II 1 (waveflow/build/systolic_engine_*_task.h).

The receiver checks the command, sends the job to the loaders and the core and the reply to the store, and forwards the operand bursts – A to one loader, B to the other, GRAM’s one burst to both – each output’s in one pipelined loop, a word a cycle. A refused command is answered with its status alone and stops the receiver.

Two loaders, banked buffers, no copy. Each loader writes its operand straight into a banked stream-of-blocks buffer in the layout the array reads: X (A, or Aᵀ) as [Mmax][Kmax], a bank per row; B as [Kmax][Nmax/C][C/LW][LW], a bank per array column, laid out so a word’s LW values land in banks the compiler can tell apart. The channels declare it (StreamOfBlocksIF.bank(shape, partition)) and the generated top partitions the stream_of_blocks, so Vitis builds a ping-pong per bank and the array reads every bank a cycle (witness). There are two loaders because the RTL ping-pong lets a writer commit a block only after the reader frees the one before, and the core frees A and B together: one loader filling both in turn would load the next B only after the sweep. The A channel holds a whole A, so the receiver moves straight on to B while the A loader – a value a cycle – drains it. At 256 and 512 bits the A loader takes a transposed block (MUL_AH, GRAM) whose rows fill whole words a word a cycle: a stored row’s LW values belong to LW different rows of X, a bank each (Feeding a large array).

The array, continuous. Tiles follow each other every P = max(kp, R·C/L, L) steps (why). At tile step kk the edges read X[i][kk] for every row and B[kk][j] for every column at once; delay lines hand row i its value i steps later and column j its value j steps later, so element (i, j) meets them together and a panel’s reads all fall within its own T · P steps. A first flag rides with the row values: on it an element latches its sum into a shadow register and starts the next tile – from zero on an output’s first panel, from its partial sum (an accumulator store in LUTRAM) on the others, or, with one tile, from the sum it holds. Tile g drains while tile g + 1 runs: group (i, cg) at L + n steps after tile g + 1 starts, n = i + cg·L its diagonal – into the store, or on an output’s last panel rounded once and out on c_out. The array’s state carries across a command’s panels and outputs, so the next tile starts the step after this one ends. Two waits remain: a gap before a panel whose tiles would read partial sums the drain has not yet stored (2 or 3 tiles at the centre), and one empty tile (L + R·C/L steps) at the end of each command, which runs its last tile out. The core is one copy of the array (137 DSP at the centre).

The collector takes C’s lane groups in the array’s drain order – row tiles, column tiles, diagonals (drain_order) – and places each in a C block. The store writes, per output, the Resp and then C from that block, a word a cycle, and after the outputs the command’s Status.

The flat pysim

SystolicEngine(pysim="flat") keeps the six tasks – their ports, their bodies, their place in the generated top, so the RTL is the same – but makes them passive, and runs one process for the engine: read a command and each output’s operands at s_in, compute the output with the bit-exact model (matmul_exact per panel, the sums added exactly, one rounding), write it at s_out. The internal channels, and the packing and unpacking at each, are gone.

The timing is the tasks’ when nothing inside stalls. Each panel starts when its operands have arrived (a cycle behind their last word) and the previous panel is done, and takes the core’s calibrated phase; an output leaves when its last panel is done, one after another; the command’s Status after its last output. On every workload, at 64- and 128-bit words, it gives the model’s words and the tasks’ run length within 2% (tests/examples/test_systolic.py), with fewer than half the SimPy events and Python calls. SystolicEngineMem(pysim="flat") passes the choice to its engine; its own tasks run as tasks.

Use "tasks" (the default) to see inside – a channel that fills, an output that is back-pressured, each task’s timing – and for the calibration, which fits the core’s phase on its own events; use "flat" for long system runs.

Performance

Measured at RTL at the centre (the example): bit-exact on every path of the array, and 8×8×32 products back to back at 171 cycles each with 64-bit words, 94 with 128-bit. At 64 bits the input, kp·(m + n) values a product, sets the pace (the bounds), and with short panels the output; at 128 bits the core’s own floor – its 64 tile steps and ~30 cycles a command – ties with the input. A command with several outputs (nb) spreads those fixed costs: four outputs a command run at 81 cycles an output, the input’s bound. The harness, workloads and gates and resources are in the example.

At larger arrays (examples/systolic/aoa_points.py, the emitter example’s search: a 16 × 16 Gram matrix over 256 samples, then a 16 × 256 scan; 128-bit words; Vitis HLS / xsim 2025.1): a 16 × 16 array at 16 lanes meets 4 ns in csynth’s estimate (2.92 ns) with 1,049 DSP, 170 k LUT and a 9.5-minute csynth; 8 × 8 at 8 lanes 281 DSP. Both are bound by their input, not their tiles: about 265 cycles a 16-step panel at either size, so the search’s two commands take 5,160 cycles back to back at 16 × 16 and 5,912 at 8 × 8, against 256 and 1,024 cycles of tiles a command. The measurements are not filed as cost-model records (plans/aoa_devices_rtl.md, Part D).

Feeding a large array

Three options feed a large array at a memory’s rate (plans/systolic_engine_fed.md). Each is a value of a parameter or an operation; unused, the engine is as it was – its generated tops byte-identical (tests/build/test_system_top_unchanged.py), its bodies compiling to today’s code.

Option What it is What it needs
Wide words word_bits 256 or 512: 8 or 16 values a word. The B loader takes a word a cycle, as at every width; the A loader takes a transposed block (MUL_AH, GRAM) whose rows fill whole words a word a cycle too. MUL’s A stays a value a cycle: a word’s values share a row’s bank. LW divides C: at 512 bits C ≥ 16
The Gram matrix read once GRAM (above): one burst a panel, to both loaders a == b, n = m, no keep_a
Long panels Kmax up to 256, so a k of 256 is one panel; with Pmax 1 the accumulator stays at Kmax · Pmax = 256 products the blocks in block RAM: X Mmax × Kmax and B Kmax × Nmax values, two of each, and the A loader’s copy for keep_a (Mmax × Kmax)
GRAM’s own output format c_gram, a class attribute of a subclass: GRAM rounds to it, every other command to c – one engine for a Gram matrix and a product that round at different places c_gram as wide as c (its bits travel as c’s)
A wider output c wider than lane_bits: C travels in lanes of 32 or 64 bits (c_lane_bits), the operands in lane_bits – (before, such a c was refused)

The last two are for the emitter example’s search unit (examples/aoa_detect/search_unit.py): the Gram matrix Q rounds 8 bits below the scan W’s place relative to its own product, and W is 24 bits of 16-bit operands. A subclass names the format, and the composite hands it to its core:

from typing import ClassVar

from waveflow.linalg.systolic_engine import SystolicEngine
from waveflow.simulation.simulation import Simulation
from waveflow.utils.fixputils import Format, OMode, QMode


def reg(W, I):
    return Format(W, I, True, QMode.AP_RND, OMode.AP_SAT)


class UnitEngine(SystolicEngine):          # c: W (24 bits); GRAM: Q at its own place, the same width
    c_gram: ClassVar = reg(24, 32)


eng = UnitEngine(name="eng", sim=Simulation(), Mmax=16, Kmax=256, Nmax=256, Pmax=1, R=16, C=16, L=16,
                 word_bits=512, a=reg(16, 16), b=reg(16, 16), c=reg(24, 35))
print(eng.c_lane_bits, eng.out_format(3) == reg(24, 32), eng.out_format(1) == reg(24, 35))
32 True True

The core takes the format’s traits as one more template argument, appended only when the engine names one, so every other engine’s top is the same.

At the emitter example’s search (examples/systolic/aoa_points.py, r16c16l16_fed: a 16 × 16 array at 16 lanes, 512-bit words, Kmax 256, a = b; Vitis HLS / xsim 2025.1), the Gram matrix of a 256 × 16 block is one GRAM command of one panel and the scan one MUL. At RTL the Gram matrix alone takes 590 cycles (4,246 as built), the scan alone 842 (2,436), and a search back to back 612 (5,160): the input – 256 words for the block, 272 for the scan – now overlaps the array, and the pace is the array’s own 576 tile steps a search (two commands of 256 steps and a 32-step run-out each). Bit-exact against pysim at RTL; pysim (the core’s phase calibrated at this point) runs 3.0 % fast on the pair. The 16 × 16 engine meets 4 ns in csynth’s estimate (2.92 ns) with 1,044 DSP, 174 k LUT, 218 k FF and 329 BRAM_18K (84 as built): the B blocks (192), the X blocks (32), the A loader’s copy (32), C (22) and two FIFOs (51). Each option alone, the cost model at these points (DSP and BRAM counted exactly, LUT and FF within 10 % on the mean) and the core’s timing records at them are in examples/systolic/fed_points.json and plans/systolic_engine_fed.md.

Implemented (Vivado 2025.1 out of context at 4 ns, examples/systolic/fed_impl.py): the 16 × 16 fed engine does not close – WNS −0.847 ns (about 206 MHz), 85.9 k LUT, 149 k FF, 1,043 DSP, 266 BRAM_18K – its worst path the step loop’s control fanning out to the partial-sum store, which at Pmax 1 is never written; the 8 × 8 fed engines close (+0.21 and +0.23 ns, 25 to 28 k LUT, 37 k FF, 276 DSP). csynth’s LUT and FF are about twice the implemented. At 8 × 8 the search takes 2,118 cycles back to back (5,912 as built).

Timing against the array’s size

The array is systolic – operands and flags move neighbour to neighbour – but three things in the core are driven to all R × C elements at once, so their wires grow with the array, and at 16 × 16 they set the clock: the partial-sum store and its write control, the drain (one mux of every shadow register), and the step loop’s stall and enable. Three options make each local (CORE_OPTIONS, plans/aoa_devices_rtl.md); each is off by default – without them the engine is as it was: its tops byte-identical, its core’s Verilog the same, its calibration records its own.

Option What changes What it costs, a command
no_store Pmax 1 only: no partial-sum store, no S_STORE path in the elements the step loop one stage deeper: +1 cycle
systolic_drain a group drains from its own row: into the store, or into the row’s hold register and down an output bus, a register a row the run-out R steps longer (+16 cycles at 16 × 16); 9 to 42 k FF for the holds and the bus
free_run the step loop free-running (PIPELINE style=frp); a command zeroes only the control −1 cycle

An engine class with options, one class per set:

from waveflow.linalg.systolic_engine import SystolicEngine, with_core_options
from waveflow.simulation.simulation import Simulation
from waveflow.utils.fixputils import Format, OMode, QMode


def reg(W, I):
    return Format(W, I, True, QMode.AP_RND, OMode.AP_SAT)


Scaled = with_core_options(SystolicEngine, "no_store", "systolic_drain", "free_run")
eng = Scaled(name="eng", sim=Simulation(), Mmax=16, Kmax=256, Nmax=256, Pmax=1, R=16, C=16, L=16,
             word_bits=512, a=reg(12, 4), b=reg(12, 4), c=reg(16, 5))
print(Scaled.__name__, eng.core.core_options, eng.core.core_option_bits)
print(eng.core.steps(16, 256, 16, last=True, run_out=True))
SystolicEngineFreeRunNoStoreSystolicDrain ('free_run', 'no_store', 'systolic_drain') 7
304

The core takes them as one template argument (OPT, a bit each), named only when an option is on. The bit-exact model is the same; pysim adds the drain’s run-out to a command’s steps, and the stages come with the core’s phase, calibrated per option set (6 + 1.00·steps for all three at the fed points). The emitter search’s unit takes them too (examples/aoa_detect/search_unit.py: search_device(R, A, opts)).

What each buys (Vivado 2025.1 out of context at 4 ns, the search’s fed points; examples/systolic/engine_scale.py -> engine_scale.json). At 16 × 16 the store is what fails: no_store alone closes the engine (+0.096 ns, from −0.847) and the search unit (+0.089 ns, from −1.108), with fewer LUT and FF (77 k and 93 k against 86 k and 149 k), at +2 cycles a search. The drain alone moves the worst path to the loop’s enable (−0.276 ns), and the free-running loop alone changes nothing while the store is there (−0.866 ns). At 8 × 8 every set closes, +0.10 to +0.31 ns. All three together do not give a flat line:

array (all three options) WNS LUT FF DSP
4 × 4, 128-bit +0.362 ns 7.1 k 11.8 k 89
8 × 8, 256-bit +0.307 ns 29.2 k 38.0 k 276
16 × 16, 512-bit −0.589 ns 92.5 k 181 k 1,043

At 16 × 16 the free-running loop’s stage valid, which reaches every state register, fails on the drain’s 42 k hold and bus registers (its worst path into a hold register’s enable). What stays global is that valid and Vitis’s hand-off of the loop’s state (it keeps a pipelined loop’s state in the calling module, each register loaded under one enable): a pipeline style cannot reach it, and binding the products at DSP latency 0 only moves it (plans/aoa_devices_rtl.md). 32 × 32 was not measured: csynth’s estimate exceeds the part’s LUTs.

Limits

  • One timed phase. The core’s pysim time is one phase per panel (b_blk.first → b_blk.last), calibrated at RTL as 5 + 1.00·steps cycles, exact on held-out panels (the example); the receiver’s command handling and the core’s restart between commands are not phases yet, so pysim runs 5 to 12% fast.
  • Fixed costs per command. The core’s ~30 cycles a command beyond its tile steps (the empty tile that runs the last one out, the restart) and the receiver’s ~9 are paid once a command: batch outputs into a command (nb) to spread them. Carrying the array’s state from one command to the next would remove the core’s, but both ways tried – static registers, and a loop over commands in one firing – made csynth run out of memory (6.7 and 7.0 GB, against 0.4).
  • R ≤ L. The drain needs distinct diagonals; R > L is refused at construction, so a 16 × 16 array takes 16 lanes (and no 24 × 24: L is a power of two).
  • Larger than 16 × 16. With the scaling options the store and the drain are local, but the step loop’s state is handed back to the calling module under one enable (Vitis 2025.1), and that enable is global: a 32 × 32 array’s csynth took 3 h 54 min and estimated 2.4 times the part’s LUTs (timing against the array’s size).
  • Words up to 512 bits. At 32 to 128 bits a large array’s input, not its tiles, sets its pace (above); at 256 and 512 bits it is fed. MUL’s A loads a value a cycle at every width.
  • Ordered, not addressed. Operands are reused in order (keep_a), not by address. Addressed regions would need T2P BRAMs shared between tasks (T2pBram, LockedT2pMemIF).
  • Back-pressure. Every RTL test drains s_out with an always-ready sink.