§ 6.5. Networks¶
Two small networks recognise handwritten digits from MNIST (Y. LeCun, C. Cortes, C. Burges: 60,000 training and 10,000 test images of 28 × 28 pixels). They are trained on the host, turned into 8-bit integers, and run as C programs on the accelerator machine: by the CPU alone, or by the CPU with the matrix unit.
The networks¶
| Network | Layers | Multiply-adds per image |
|---|---|---|
| MLP | 784 → 64 (ReLU) → 10 | 50,816 |
| CNN | 3 × 3 convolution, 8 filters, stride 2 (28 × 28 → 13 × 13 × 8, ReLU) → 3 × 3 convolution, 16 filters, stride 2 (→ 6 × 6 × 16, ReLU) → 576 → 10 | 59,400 |
The CNN uses strided convolutions instead of pooling layers: each layer halves the image itself. A stride keeps the patches of one output row equally far apart in memory, which is exactly the pattern the unit's copy engine gathers (6.3).
tools/accelerator/model.py trains both in plain numpy (seeded, a few
epochs of Adam), so a rerun gives the same files.
From floating point to bytes¶
Each number x is stored as a byte q with a scale s: x ≈ s · q.
| Quantity | Format | Scale |
|---|---|---|
| Input pixels | int8, 0–127 | 1/127 (pixel/255 = q/127) |
| Weights | int8, −127…127 | the layer's largest weight / 127 |
| Sums | int32 | input scale × weight scale |
| Bias | int32 | the same as the sums |
| Outputs | int8 | the layer's 99.99th-percentile output (training images) / 127 |
A layer's sums are in units of s_in · s_w; the next layer wants units of
s_out. The requantizer (6.1) multiplies by SCALE / 2^SHIFT ≈
s_in · s_w / s_out, a 16-bit fixed-point number chosen per layer, rounds,
clamps and applies ReLU. The last layer's 10 outputs are the scores; the
largest is the answer.
Accuracy over all 10,000 test images (the host's integer inference follows the unit bit for bit):
| Network | Floating point | int8 |
|---|---|---|
| MLP | 96.87 % | 96.79 % |
| CNN | 97.39 % | 97.34 % |
Eight bits cost under a tenth of a percentage point here.
The guest programs¶
tools/accelerator/guest/ holds the C side; build-guests.py builds each
workload for the CPU alone and for N = 4, 8, 16 into
tests/fixtures/accelerator/.
| File | Contents |
|---|---|
system.h |
Generated from systems/accelerator-machine.json: MATRIX_COMMAND, MATRIX_DONE, TIMER_COUNT, … |
matrix.h, matrix.c |
cpu_layer, cpu_conv (the CPU's loops) and matrix_layer, matrix_conv (the same results through the unit) |
workloads.c |
WORKLOAD 1 matrix multiply, 2 MLP, 3 CNN, 4 the demo of the machine scenario |
model_cpu.h, model_tiled_nN.h, images.h |
Weights in natural and tile order, test images, the expected scores (generated) |
The driver turns one layer into commands as 6.4 describes:
void matrix_layer(const int8_t* x, uint32_t rows, uint32_t inputs, uint32_t outputs,
const int8_t* tiles, const int32_t* bias, int8_t* out,
uint32_t scale, uint32_t shift, int relu) {
...
command(LOAD, (uint32_t)(x + first * inputs), count << 16, /* rows of x → activation rows */
chunks << 24 | n << 16 | n, chunks << 16 | inputs | TO_ACTIVATIONS);
for each block of n outputs:
command(LOAD, bias + t * n, ...TO_BIAS);
for each block c of n inputs:
command(LOAD, tiles + ..., ...TO_WEIGHTS); /* one n × n tile */
command(SHIFT_WEIGHTS, 0, 0, 0, 0);
command(MATMUL, count << 16 | c, chunks, c ? 1 : 0, 0); /* accumulate after the first */
command(ACTIVATE, count << 16, out_row, relu << 24 | shift << 16 | scale, 0);
command(STORE, out + t * n, ...);
}
int matrix_wait(void) { while (MATRIX_DONE != queued) {} ... }
Each program writes a report page at 0x3f000: a magic word, its status (nonzero when the unit reported an error or two results differed), the cycle counts of its timed sections read from the timer, and its outputs. The host compares the outputs with its own reference.
| Workload | Timed sections | Checked against |
|---|---|---|
| Matrix multiply | Three dense layers: 4 × 32 → 16, 8 × 64 → 32, 16 × 64 → 64 (random int8, bias, scale 1, shift 8) | The host's integer layer on the same xorshift operands |
| MLP | Each image, then (with the unit) the same images as one batch | The expected scores of model.json; the batch must equal the single images |
| CNN | Each image | The expected scores of model.json |
The machine and its runner¶
systems/accelerator-machine.json (§ 6); the runner
(src/runtime/AcceleratorGuest.cpp) builds it with the chosen unit size,
memory technology and options, loads the program, clocks it until EBREAK,
and returns the cycles, retired instructions, console text and report
page. Unlike the chapter-5 game runner it does not check every instruction
against an ISA model, because the unit writes memory behind the core's
back; the outputs are checked instead.
| Option | Effect |
|---|---|
matrix |
4, 8, 16, or 0 for accelerator-machine-cpu (no unit) |
memory |
ff, sram (default) or dram, set up for the clock period |
dram_retention |
DRAM row retention in ticks (the memory's retention_ticks); 0: the profile's (64 ms) |
array_multiplier |
The core's multiplier=array (6.1.2); default on, off gives the iterative multiplier |
pipelined_bus |
The pipelined bus, arbiters and overlapped fetch (6.3.2); default on, off gives the chapter-5 bus |
float_core |
The RV32IMF core of § 7.7 instead of RV32IM (off by default) |
stall_cycles |
Stop with an error when no instruction retires for this many cycles (default 1,000,000; 0: never) |
A run also stops at once when the core's commit signal turns unknown (X), for example after the core fetched code from a DRAM row kept past its retention. Both errors list the handshake and address pins of the machine's top three levels.
accelerator_experiment --program NAME … [--stall-cycles C] [--dram-retention TICKS] runs one program and prints the
run as JSON; 6.6 is made from such runs.
Examples¶
The whole machine with MatrixUnit<4> runs the demo program (WORKLOAD
4): one 4 × 16 → 8 layer of random int8 values, first by the CPU's loops,
then by the unit, after which the program compares the two results and
prints done. Its checkpoints are reset released, console: done and
halted (69,671 cycles on the chapter-5 bus). It opens in the Compact
profile, as the chapter-5 machines do; the Gate profile is refused because
the main memory is beyond the structural storage limit, and Logic or Visual
would put the gate-level matrix unit (6.4) under all
69,671 cycles: the native recording alone passes 1.6 GB. The recording
keeps the whole timeline, so every command the program writes and every
transfer of the unit's DMA port can be followed on the bus. Opening it
runs the whole program first: about 3½ minutes in the browser (2 minutes
natively) before the machine appears.
There is no generated example table: like the chapter-5 machine scenarios, this is a program recording checked by its runner (clean halt, console text, status 0 on the report page), not a list of pin checkpoints.
Source and tests¶
Runner, machines, guest programs, host model and quantization, machine tests and scenario.
| Test | Coverage |
|---|---|
AcceleratorMachineTest |
demo-n4, matmul-n16, mlp-n16, cnn-n16 on the machine (N = 4 and 8: the 6.6 campaign checks the same outputs), and demo-cpu with the array multiplier: clean halt, status 0, outputs equal the host references, no setup violations |
AcceleratorMachineScenarioTest |
The demo program on the machine (compact), recorded for the simulator: one layer on the CPU, the same layer on the unit, compared by the program |