S4 · Lesson 42 · Accelerator lab
Roofline analysis, a systolic array's cycle model and GPU occupancy, in Python
Choosing or designing an accelerator starts with three quick models: the roofline says whether a kernel is limited by arithmetic or by memory; a cycle model of a systolic array says how much of its peak a matrix multiply of a given shape can use; and an occupancy calculator says how many warps a GPU kernel keeps resident, which decides whether memory latency is hidden. This lab writes all three and applies them to the shapes that come up in practice.
- The curve is the utilization of an n × n weight-stationary systolic array on a matrix multiply with M rows streamed per tile, , counting n cycles to load each tile's weights and to fill and drain.
- Small batches waste most of a big array: with M = 64 a 256 × 256 array is busy under 10% of the time.
- The lab adds tiling over the other two dimensions, which can waste more.
Predict
A 256 × 256 systolic array multiplies a 64 × 256 matrix by a 256 × 256 weight matrix: one tile, 64 rows streamed. Loading the tile's weights takes 256 cycles and filling and draining 510. What fraction of its peak does it use?
The useful work is multiply-accumulates; the array could do per cycle, and the pass takes cycles: . The weights fit perfectly, but 64 rows can't amortize loading them and pushing data through 256 stages.
Name it
Roofline
For a kernel of W operations moving Q bytes on a machine with peak P and bandwidth B: intensity , attainable rate , and time ; memory-bound if .
Systolic tiling
An M × K by K × N multiply on an n × n weight-stationary array needs weight tiles. Each takes n cycles to load, then M cycles to stream the rows plus to fill and drain:
GPU occupancy
A block of T threads is warps. An SM fits as many blocks as its registers, its shared memory, its block slots and its warp slots all allow:
for r registers per thread, s bytes of shared memory per block and warps per block; occupancy is resident warps over .
Check
Write roofline(P, B, ops, nbytes): with P in GFLOP/s and B in GB/s, return (attainable GFLOP/s, 'memory' or 'compute', run time in seconds).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write systolic(M, K, N, n): return (cycles, utilization) for an M × K by K × N multiply on an n × n weight-stationary array, by the tiling model.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write occupancy(regs, smem, threads, R=65536, S=102400, Wmax=64, Bmax=32): the fraction of an SM's warp slots a kernel fills, for regs registers per thread, smem bytes of shared memory per block (0 for none) and threads per block.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why can a systolic array that matches a layer's weight matrix exactly still run at a small fraction of its peak?
A weight-stationary array must first load a tile of weights into its cells, then stream input rows through, and data takes about 2n cycles to enter, cross and leave an n × n array. Those load, fill and drain cycles are fixed costs per tile, while useful work grows with the number of rows streamed. If the batch is small, say 64 rows on a 256 × 256 array, the fixed costs are ten times the useful cycles, so the array is idle most of the time even though every cell holds a real weight. Large batches, or designs that overlap loading the next tile with streaming the current one, amortize those costs.