§ 6.4. Matrix Unit¶
MatrixUnit<N> is the accelerator: a chapter-5 register device with a
command queue, a DMA master port, three buffers, an N × N systolic array
(6.2) and N requantizers (6.1). The processor writes
commands; the unit loads operands, multiplies, requantizes and stores
results.
| Component | Description | Structural | Behavioral |
|---|---|---|---|
MatrixUnit<N> |
The whole device, N = 4, 8, 16 | Yes | Yes (its groups compiled, compact children) |
Catalog device.matrix-unit.N. Parameters:
| Parameter | Meaning | Default |
|---|---|---|
act_rows |
Activation buffer rows, a power of two 4–4096 | 256 |
acc_rows |
Accumulator rows, a power of two 4–4096 | 64 |
queue_depth |
Commands waiting, 1–7 | 4 |
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
CLK, RST, REQ_*, RSP_* |
— | — | The register port, as every chapter-5 device (§ 5.4.1) |
DMA_REQ_*, DMA_RSP_READY, DMA_CLK, DMA_RST |
Output | — | The copy engine's master port: a requester on the bus |
DMA_REQ_READY, DMA_RSP_* |
Input | — | Its answers |
Behavior¶
Registers¶
Device MATRIX, window 0x100 bytes; the guest header defines MATRIX_<NAME>.
| Name | Offset | Access | Bits | Meaning |
|---|---|---|---|---|
COMMAND |
0x00 | write-only data | 4 | Queue a command with the current ARG0–ARG3: 1 LOAD, 2 STORE, 3 SHIFT_WEIGHTS, 4 MATMUL, 5 ACTIVATE |
ARG0–ARG3 |
0x04–0x10 | read-write | 32 | The next command's arguments |
STATUS |
0x14 | read-only | 3 | Bit 0 busy (a command runs or waits), bit 1 error, bit 2 queue full |
DONE |
0x18 | read-only | 32 | Commands finished since reset |
CLEAR |
0x1c | command | 1 | Clear the error flag |
SIZE |
0x20 | setting | 32 | N |
ACT_ROWS, ACC_ROWS |
0x24, 0x28 | setting | 32 | Buffer sizes |
A write while the queue is full waits at the port until a command leaves
the queue (the front end's READY). A command code outside 1–5 finishes at
once with the error flag; a value wider than 4 bits is refused with status
Invalid (6) and queues nothing.
Buffers¶
| Buffer | Shape | Written by | Read by |
|---|---|---|---|
| Activations | act_rows rows × N bytes, one 8-bit memory per column |
LOAD (target 0), ACTIVATE | MATMUL (one row per edge), STORE |
| Weights | N rows × N bytes, one memory per column | LOAD (target 1) | SHIFT_WEIGHTS |
| Bias | N int32 registers | LOAD (target 2) | MATMUL without accumulate |
| Accumulators | acc_rows rows × N int32, one memory per column |
MATMUL | MATMUL (accumulate), ACTIVATE |
The activation, weight and accumulator buffers are chapter-3 flip-flop
memories (memory.ff.device) with an unknown initial fill, as the
gate-level memory requires; reset does not clear them. The bias is 4N byte
registers, cleared by reset.
Commands¶
| Code | Command | Arguments | Effect |
|---|---|---|---|
| 1 | LOAD | ARG0 memory address; ARG1 = P << 16 | first row; ARG2 = R << 24 | B << 16 | S; ARG3 = target << 30 | DR << 16 | T | Copy P patches of R rows of B bytes into buffer target (0 activations, 1 weights, 2 bias bytes) (6.3) |
| 2 | STORE | as LOAD (target ignored) | Copy from the activation buffer to memory |
| 3 | SHIFT_WEIGHTS | — | N edges: weight-buffer row N−1−k enters the array at the k-th |
| 4 | MATMUL | ARG0 = P << 16 | first activation row; ARG1 = first accumulator row << 16 | row stride; ARG2 bit 0 = accumulate | Stream P activation rows (stride apart) through the array; result p goes to accumulator row first + p, added to its old value or to the bias |
| 5 | ACTIVATE | ARG0 = P << 16 | first accumulator row; ARG1 = first activation row; ARG2 = RELU << 24 | SHIFT << 16 | SCALE | Requantize P accumulator rows into activation rows, one row per edge |
Commands run one at a time, in order. DONE counts each one when it
finishes; a program queues commands and waits until DONE reaches the
number it queued.
One layer, N outputs at a time. For inputs X (rows × K) and weights W
(K × outputs), the driver (6.5) gathers the rows of X into
activation rows (K/N rows each), then for each block of N outputs: LOAD the
bias, and for each block of N inputs LOAD the weight tile, SHIFT_WEIGHTS and
MATMUL (the first block without accumulate, the rest with it); then
ACTIVATE and STORE. Weight tiles are laid out in memory in that order
([output block][input block][row][column]), so each tile is one LOAD.
Unknown inputs and timing¶
When K is not a multiple of N, the last buffer row of each input is partly unwritten; the weights in those rows are zero, and a product with a zero weight is zero, so the unknown bytes never reach a result. Other unknown data (a buffer read before anything was loaded, an X on the bus) is not given a meaning: it can reach sums and stored bytes.
Everything is clocked by CLK on its rising edge, the register port and
the DMA port included (DMA_CLK and DMA_RST pass CLK and RST on).
Reset empties the queue, stops the running command, clears DONE, the
error flag, the bias registers and the array's registers; it does not clear
the activation, weight and accumulator memories. A DMA request is held until
the bus takes it, and one answer is outstanding at a time.
Implementation¶
Structural implementation¶
| Part | Responsibility |
|---|---|
PORT, REGISTERS |
The register port and the front end built from the register table |
QUEUE_OP, QUEUE_ARG0–3 |
Five SynchronousFifos pushed together by a COMMAND write |
SEQUENCER |
Takes a command when idle, starts its engine, waits for its DONE; the done counter and the error flag |
COPY_ENGINE |
LOAD/STORE: counters, address adders, the held word, the bus request (6.3) |
SHIFT_ENGINE |
N edges of SHIFT, the weight row to read |
MATMUL_ENGINE |
Issue counter and row address; a chain of 2N − 1 valid bits that marks when each result reaches the accumulator write |
ACTIVATE_ENGINE |
One accumulator row per edge into one activation row |
BUFFER_PORTS |
Which engine owns each buffer's address and write enables this cycle |
ACT_BANK_j, WEIGHT_BANK_j, ACC_BANK_j |
One memory per column |
BIAS |
4N byte registers |
ARRAY |
SystolicArray<N> |
REQUANTIZE_j |
Requantizer |
LANE_j |
Column j: the array input (the activation byte while issuing, else 0), the accumulator update (bias or old value, plus the array output), the activation write (copy byte or requantized byte) |
STORE_SELECT |
The byte a STORE sends: the bank its address names |
Timing of a MATMUL. A row read from the activation banks enters the array at the edge it is issued; its result is at the array's outputs 2N − 2 edges later and is added into the accumulators at the next edge. P rows take P + 2N − 1 edges.
In the simulator. The unit is drawn left to right as a command moves:
PORT above REGISTERS; the five queues above SEQUENCER; the engines it
starts, with BUFFER_PORTS and STORE_SELECT below them; then the data
path, one column per array column j: WEIGHT_BANK_j and ACT_BANK_j at
the top, BIAS across, the ARRAY, then LANE_j, ACC_BANK_j and
REQUANTIZE_j. Splits, joins and constants sit in the bottom row.
The control blocks¶
Each block below is gates and registers of its own (one compiled gate list in the compact unit). Their pins are internal: the parent wires them as the table above describes. The copy engine is in 6.3.1.
MatrixSequencer (SEQUENCER)¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
CLK, RST |
Input | 1 | Clock and reset |
Q_VALID, Q_OP |
Input | 1, 8 | A command waits in QUEUE_OP; its code |
Q_READY |
Input | 1 | The queue has room (for STATUS bit 2) |
CLEAR |
Input | 1 | A write to the CLEAR register |
COPY_DONE, COPY_FAILED, SHIFT_DONE, MATMUL_DONE, ACTIVATE_DONE |
Input | 1 | The engines' end signals |
POP |
Output | 1 | Take the waiting command (all five queues) |
START_COPY, STORE_OP, START_SHIFT, START_MATMUL, START_ACTIVATE |
Output | 1 | Start one engine; STORE_OP tells the copy engine it is a STORE |
COPY_ACTIVE, SHIFT_ACTIVE, MATMUL_ACTIVE, ACTIVATE_ACTIVE |
Output | 1 | Which engine owns the unit |
STATUS |
Output | 3 | Bit 0 busy, bit 1 error, bit 2 queue full |
DONE_COUNT |
Output | 32 | Commands finished since reset |
Four flags record which engine runs; the sequencer is idle when none is
set. When idle and a command waits, it pops the command and, in the same
edge, sets the flag of the engine its code names (1 and 2 the copy engine,
3 shift, 4 multiply, 5 activate). The flag stays set until that engine's
end signal, which also adds one to DONE_COUNT. Any other code is popped
and counted at once and sets the error flag, as does a copy that ends with
COPY_FAILED; CLEAR resets the error flag. Busy is "an engine runs or a
command waits", so a program that waits for STATUS bit 0 to fall sees
the whole queue finished.
MatrixShiftEngine<N> (SHIFT_ENGINE)¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
CLK, RST, START |
Input | 1 | Clock, reset, begin SHIFT_WEIGHTS |
SHIFT |
Output | 1 | The array's SHIFT: high for N edges |
ROW |
Output | 16 | The weight row to read: N − 1 − k at the k-th edge |
DONE |
Output | 1 | The last of the N edges |
MatrixMultiplyEngine<N> (MATMUL_ENGINE)¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
CLK, RST, START |
Input | 1 | Clock, reset, begin MATMUL |
ARG0–ARG2 |
Input | 32 | The command's arguments (row count, first rows, stride, accumulate) |
ISSUE |
Output | 1 | An activation row enters the array at this edge |
ROW |
Output | 16 | The activation row being issued |
WRITE, WRITE_ROW |
Output | 1, 16 | A result is added into this accumulator row at this edge |
ACCUMULATE |
Output | 1 | Add to the old accumulator value (else to the bias) |
DONE |
Output | 1 | Nothing left to issue and no result in flight |
It issues one row per edge, stride apart, and shifts a valid bit through a chain of 2N − 1 registers, the array's latency plus the write edge, so it knows when each row's result arrives without any feedback from the array.
MatrixActivateEngine (ACTIVATE_ENGINE)¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
CLK, RST, START |
Input | 1 | Clock, reset, begin ACTIVATE |
ARG0–ARG2 |
Input | 32 | Row count, first accumulator and activation rows, scale, shift, ReLU |
RUN |
Output | 1 | A row is converted at this edge |
ACC_ROW, ACT_ROW |
Output | 16 | The accumulator row read and the activation row written |
SCALE, SHIFT, RELU |
Output | 16, 5, 1 | Held for the requantizers |
DONE |
Output | 1 | Not running |
MatrixBufferPorts<N> (BUFFER_PORTS)¶
Gates only. Inputs: which engine is active, the copy engine's TARGET,
STORING, LOCAL_ADDR and LOCAL_WRITE, and the rows each engine names.
Outputs: ACT_ADDR, ACC_ADDR, WEIGHT_ADDR (32 bits each, one address
per buffer, shared by its N banks), ACT_WE and WEIGHT_WE (N bits, one
per bank) and BIAS_WE (4N bits, one per bias byte). A buffer byte address
splits into its bank (the low log2 N bits) and its row (the rest). The copy
engine owns the activation address while it runs, then the multiply engine,
then the activate engine; the accumulators follow the multiply engine's
write row, else the activate engine's read row; the weights follow the
shift engine while it shifts, else the copy engine. A LOAD writes the one
bank or bias byte its address names; ACTIVATE writes all activation banks.
MatrixLane (LANE_j)¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
ACT_READ, ISSUE |
Input | 8, 1 | Activation bank j's byte; a row is being issued |
ACC_READ, BIAS, Y, ACCUMULATE |
Input | 32, 32, 32, 1 | Accumulator j's old value, bias j, array output j, which base to use |
REQUANTIZED, COPY_BYTE, ACTIVATE_RUN |
Input | 8, 8, 1 | Requantizer j's byte, the copy engine's byte, which to write |
X |
Output | 8 | Array input j: ACT_READ while issuing, else 0 |
ACC_WRITE |
Output | 32 | (ACCUMULATE ? ACC_READ : BIAS) + Y |
ACT_WRITE |
Output | 8 | REQUANTIZED during ACTIVATE, else COPY_BYTE |
Holding the array's input at 0 between issues keeps zeros, not stale bytes, flowing behind the last row.
MatrixBias<N> (BIAS) and MatrixStoreSelect<N> (STORE_SELECT)¶
BIAS: 4N byte registers (CLK, RST, BYTE, WE in; BIAS_0 …
BIAS_N−1, 32 bits each, out), cleared by reset; a bias LOAD writes them a
byte at a time. STORE_SELECT: an N-to-1 byte multiplexer tree (LOCAL_ADDR
and BANK_0 … in; BYTE out) that hands STORE the activation bank its
address names.
Behavioral implementation¶
The compact unit has the same parts. Each control group (sequencer,
engines, ports, lanes, register front end) runs as one compiled gate list
of its own gates; the queues are compact FIFOs, the buffers compact
flip-flop memories, the array and requantizers their compact forms
(6.1, 6.2). MatrixUnitTest runs the same program on
both and requires the same results and the same number of cycles.
Examples¶
A 2 × 4 → 3 layer (random int8 operands and biases, scale 1, shift 10, no
ReLU) on MatrixUnit<4> with 8 activation and 4 accumulator rows. A host
memory answers the DMA port one edge after each request. The checkpoints
are each command's COMMAND write being accepted (queue-1-load …
queue-7-store: inputs, bias, weights, SHIFT_WEIGHTS, MATMUL, ACTIVATE,
STORE); each of the six bytes STORE writes, with its word address, lane
mask and value taken from the integer reference (−1, 11, 16, −2, 26, 0 at
0x15C–0x161); then DONE = 7 and STATUS = 0. The scenario opens in the
Logic profile, so every block above can be opened down to its gates: 79,030
parts, about half a minute to load in a browser and about 1 GB of its
memory. The Gate profile is beyond the browser (the native session alone
passes 1.5 GB); Compact (62 parts) shows the unit as one closed box.
All 17 recorded rows of MatrixUnitScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-logic-v1 / structural (3c048584c5332bb2). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | CLKInput · 1 bit |
DMA_REQ_READYInput · 1 bit |
DMA_RSP_READ_DATAInput · 32 bit |
DMA_RSP_STATUSInput · 3 bit |
DMA_RSP_VALIDInput · 1 bit |
REQ_ADDRInput · 32 bit |
REQ_OPInput · 3 bit |
REQ_VALIDInput · 1 bit |
REQ_WRITE_DATAInput · 32 bit |
REQ_WRITE_MASKInput · 4 bit |
RSP_READYInput · 1 bit |
RSTInput · 1 bit |
DMA_CLKExpected output · 1 bit |
DMA_REQ_ADDRExpected output · 32 bit |
DMA_REQ_OPExpected output · 3 bit |
DMA_REQ_VALIDExpected output · 1 bit |
DMA_REQ_WRITE_DATAExpected output · 32 bit |
DMA_REQ_WRITE_MASKExpected output · 4 bit |
DMA_RSP_READYExpected output · 1 bit |
DMA_RSTExpected output · 1 bit |
REQ_READYExpected output · 1 bit |
RSP_READ_DATAExpected output · 32 bit |
RSP_STATUSExpected output · 3 bit |
RSP_VALIDExpected output · 1 bit |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
#0reset | 52 | 0 |
0 |
0x00000000 |
0x0 |
0 |
0x00000000 |
0x0 |
0 |
0x00000000 |
0x0 |
0 |
1 |
not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked |
#1ready | 64 | 0 |
0 |
0x00000000 |
0x0 |
0 |
0x00000000 |
0x0 |
0 |
0x00000000 |
0x0 |
0 |
0 |
not checked | not checked | not checked | 0 |
not checked | not checked | not checked | not checked | not checked | not checked | not checked | 0 |
#2queue-1-load | 365 | 0 |
1 |
0x00000000 |
0x0 |
0 |
0x00000000 |
0x1 |
0 |
0x00000001 |
0xF |
1 |
0 |
not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | 0x0 |
1 |
#3queue-2-load | 706 | 0 |
0 |
0xE8576485 |
0x0 |
1 |
0x00000000 |
0x1 |
0 |
0x00000001 |
0xF |
1 |
0 |
not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | 0x0 |
1 |
#4queue-3-load | 1041 | 0 |
1 |
0x00000059 |
0x0 |
0 |
0x00000000 |
0x1 |
0 |
0x00000001 |
0xF |
1 |
0 |
not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | 0x0 |
1 |
#5queue-4-shift-weights | 1378 | 0 |
1 |
0x000008DC |
0x0 |
0 |
0x00000000 |
0x1 |
0 |
0x00000003 |
0xF |
1 |
0 |
not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | 0x0 |
1 |
#6queue-5-matmul | 1686 | 0 |
1 |
0x00000000 |
0x0 |
0 |
0x00000000 |
0x1 |
0 |
0x00000004 |
0xF |
1 |
0 |
not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | 0x0 |
1 |
#7queue-6-activate | 2001 | 0 |
1 |
0x00EE5042 |
0x0 |
0 |
0x00000000 |
0x1 |
0 |
0x00000005 |
0xF |
1 |
0 |
not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | 0x0 |
1 |
#8queue-7-store | 2340 | 0 |
0 |
0x00E2B9C4 |
0x0 |
1 |
0x00000000 |
0x1 |
0 |
0x00000002 |
0xF |
1 |
0 |
not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | 0x0 |
1 |
#9store-byte-0 | 3153 | 0 |
1 |
0x00E2B9C4 |
0x0 |
0 |
0x00000018 |
0x0 |
0 |
0x00000000 |
0xF |
0 |
0 |
not checked | 0x0000015C |
0x1 |
1 |
0xFFFFFFFF |
0x1 |
not checked | not checked | not checked | not checked | not checked | not checked |
#10store-byte-1 | 3197 | 0 |
1 |
0x00000000 |
0x0 |
0 |
0x00000018 |
0x0 |
0 |
0x00000000 |
0xF |
0 |
0 |
not checked | 0x0000015C |
0x1 |
1 |
0x0B0B0B0B |
0x2 |
not checked | not checked | not checked | not checked | not checked | not checked |
#11store-byte-2 | 3246 | 0 |
1 |
0x00000000 |
0x0 |
0 |
0x00000018 |
0x0 |
0 |
0x00000000 |
0xF |
0 |
0 |
not checked | 0x0000015C |
0x1 |
1 |
0x10101010 |
0x4 |
not checked | not checked | not checked | not checked | not checked | not checked |
#12store-byte-3 | 3295 | 0 |
1 |
0x00000000 |
0x0 |
0 |
0x00000018 |
0x0 |
0 |
0x00000000 |
0xF |
0 |
0 |
not checked | 0x0000015C |
0x1 |
1 |
0xFEFEFEFE |
0x8 |
not checked | not checked | not checked | not checked | not checked | not checked |
#13store-byte-4 | 3340 | 0 |
1 |
0x00000000 |
0x0 |
0 |
0x00000018 |
0x0 |
0 |
0x00000000 |
0xF |
0 |
0 |
not checked | 0x00000160 |
0x1 |
1 |
0x1A1A1A1A |
0x1 |
not checked | not checked | not checked | not checked | not checked | not checked |
#14store-byte-5 | 3389 | 0 |
1 |
0x00000000 |
0x0 |
0 |
0x00000018 |
0x0 |
0 |
0x00000000 |
0xF |
0 |
0 |
not checked | 0x00000160 |
0x1 |
1 |
0x00000000 |
0x2 |
not checked | not checked | not checked | not checked | not checked | not checked |
#15done-7 | 3500 | 0 |
1 |
0x00000000 |
0x0 |
0 |
0x00000018 |
0x0 |
0 |
0x00000000 |
0xF |
1 |
0 |
not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | 0x00000007 |
0x0 |
1 |
#16status-idle | 3559 | 0 |
1 |
0x00000000 |
0x0 |
0 |
0x00000014 |
0x0 |
0 |
0x00000000 |
0xF |
1 |
0 |
not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | not checked | 0x00000000 |
0x0 |
1 |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
Source and tests¶
The unit: groups, engines and catalog entry, interface, register table, device tests and scenario.
| Test | Coverage |
|---|---|
MatrixUnitTest |
Programmed through its registers with a host memory on the DMA port, results against an independent integer reference: dense layers (one and two output blocks, bias, ReLU), a convolution gathered as patches, at N = 4 (compact), 8 and 16 (compact); one layer on the gate-level N = 4 unit with the same cycle count as the compact unit; unknown command, CLEAR, a DMA error, empty commands, a refused 5-bit command |
MatrixUnitScenarioTest |
The recorded example above, on both available fidelities, every register access and DMA transfer on the timeline |
The gate-level unit runs at about one cycle per second here, so the gate-level comparison is one small layer; the larger programs run on the compact unit only.