CS 152 Computer Architecture and Engineering CS252A Graduate Computer Architecture
Lecture 23 - Spatial Architectures and Trainium
Chris FletcherUC Berkeley, Spring 2026
Why Spatial Architectures?
Deep learning is dominated by matrix multiplication and 2D convolution
CPUs and SIMT GPUs still pay fetch, decode, register-file, and scheduling cost per operation
For ops per watt and per mm²: spatial architectures , above all the systolic array
$Z_{m,n} = \sum_{k=0}^{K-1} A_{m,k} \cdot B_{k,n}$
$A$: $M \times K$, $B$: $K \times N$, $Z$: $M \times N$; $K$ = contraction dimension
Dataflows: Which Tensor Stays Put?
Output-stationary
for m ...:
for n ...:
for k ...:
Z[m, n] += A[m, k] * B[k, n]
Z[m, n] is fixed in the inner loop
A-stationary (weight-stationary)
for m ...:
for k ...:
for n ...:
Z[m, n] += A[m, k] * B[k, n]
A[m, k] is fixed in the inner loop
3×3 Weight-Stationary Systolic Array
partial sums in = 0
j k l · m n o · · p q r skewed B rows
a d g
b e h
c f i
P Q R S T U V W X
blue: B moves right · red: partial sums move down
$$\begin{pmatrix}P&Q&R\\S&T&U\\V&W&X\end{pmatrix} = \begin{pmatrix}a&b&c\\d&e&f\\g&h&i\end{pmatrix}\begin{pmatrix}j&k&l\\m&n&o\\p&q&r\end{pmatrix}$$
Each PE: out = B_in × A + psum_in
Nearest-neighbor wires only
A is stored transposed : column 0 holds a, b, c
B rows enter skewed 1 cycle per row
Systolic Array: Cycle by Cycle
Cycle What happens
0 j enters PE(0,0) → a·j down
1 PE(0,0): a·k; PE(0,1): d·j; PE(1,0): a·j + b·m
2 PE(2,0) outputs P = a·j + b·m + c·p ; PE(1,0) a·k + b·n; PE(1,1) d·j + e·m; PE(0,·) a·l, d·k, g·j
3 Q = a·k + b·n + c·q; S = d·j + e·m + f·p
4 R = a·l + b·o + c·r; T = d·k + e·n + f·q; V = g·j + h·m + i·p
5 U = d·l + e·o + f·r; W = g·k + h·n + i·q
6 X = g·l + h·o + i·r
Every PE uses only local wires; A is reused across all of B, each B element across all of A; no instruction overhead
Check Your Understanding
1. Why are rows of the moving matrix B skewed by one cycle per row?
Partial sums move down one row per cycle; delaying row r by r cycles makes B[r, ·] meet the partial sum from the PE above at the same cycle.
2. Why is the stationary A stored transposed in the PE grid?
P, Q, R all use a, b, c and accumulate down one PE column, so row 0 of A must sit down column 0.
AWS Trainium: NeuronCore-v2
Chip: HBM, host PCIe, DMA, collective communication, NeuronLink-v2, NeuronCores
Four engines, each with its own sequencer, synchronized by compiler-inserted instructions:
Engine Role Reads/writes
Tensor (128 × 128 systolic)MatMul SBUF → PSUM only
Vector (128-wide) Elementwise, reductions SBUF, PSUM
Scalar (128-wide) Non-linear functions (ReLU) SBUF, PSUM
GPSIMD (128-wide) General programmable SIMD SBUF
SBUF (24 MB): partition (128 lanes) × free; tensor.shape[0] always maps to partitions
PSUM (2 MB): 128 partitions, free dimension ≤ 512
The Bandwidth Gap
Level Capacity Bandwidth
Host DRAM 1 TB 16 GB/s
Device HBM 16 GB 0.5 TB/s
SBUF + PSUM 24 MB + 2 MB 10 TB/s
$\dfrac{10 \text{ TB/s}}{0.5 \text{ TB/s}} = 20\times$
Without reuse in SBUF/PSUM, HBM starves the Tensor Engine ~95% of the time
Basic NKI MatMul
dst = nisa.nc_matmul(stationary, moving) computes stationary.T @ moving
Limits: stationary M ≤ 128, K ≤ 128 (array); moving K ≤ 128, N ≤ 512 (PSUM depth)
lhsT_SBUF = nl.ndarray(lhsT.shape, ..., buffer=nl.sbuf) # 1
rhs_SBUF = nl.ndarray(rhs.shape, ..., buffer=nl.sbuf)
nisa.dma_copy(dst=lhsT_SBUF, src=lhsT) # 2
nisa.dma_copy(dst=rhs_SBUF, src=rhs)
output_PSUM = nl.ndarray(..., dtype=nl.float32, buffer=nl.psum)# 3
output_PSUM = nisa.nc_matmul(lhsT_SBUF, rhs_SBUF) # 4
output_SBUF = nl.ndarray(..., buffer=nl.sbuf) # 5
nisa.tensor_copy(dst=output_SBUF, src=output_PSUM) # 6
output = nl.ndarray((M, N), ..., buffer=nl.shared_hbm) # 7
nisa.dma_copy(dst=output, src=output_SBUF) # 8
Tiling for Large Matrices
Tile for engine size : stationary in 128 × 128 blocks, moving in 128 × 512
Each 128 × 512 output block accumulates over K in PSUM
Naive tiling has low reuse
The same stationary block is reloaded from HBM 8× for 8 output blocks in a row
Tile for SBUF capacity : keep big input tiles and the output tile resident
$M_t K_t + N_t K_t + M_t N_t \le \text{SBUF size}$
Stationary tile + moving tile + output tile; inside, the engine walks 128 × 128 / 128 × 512 sub-tiles at 10 TB/s, so each block leaves HBM only a few times
Check Your Understanding
1. What limits the stationary tile to 128 × 128 and the moving tile to 128 × 512?
The 128 × 128 systolic array (M, K ≤ 128) and the PSUM bank depth of 512 (N ≤ 512).
2. Why does the SBUF capacity constraint include the output tile?
PSUM is only 2 MB and results are copied into SBUF before going to HBM, so SBUF must hold the output tile alongside both inputs to avoid spilling.
2D Convolution Example
$X = \begin{pmatrix}1&0&2&4\\3&5&1&0\\7&6&2&9\\5&8&8&3\end{pmatrix}$, $W = \begin{pmatrix}-1&3&1\\2&0&1\\-2&1&4\end{pmatrix}$, $B = 5$
$OW = IW - FW + 1 = 2$, $OH = 2$ (stride 1)
Patch Rows Sum +B
(0,0) 1 + 7 + 0 8 13
(0,1) 10 + 10 + 26 46 51
(1,0) 13 + 16 + 30 59 64
(1,1) −2 + 21 + 4 23 28
$O = \begin{pmatrix}13&51\\64&28\end{pmatrix}$
Deep Learning Convolution Tensors
Input channels (IC) : e.g., RGB; multiply [IC, FH, FW] filter by input patch and sum over IC
Output channels (OC) : OC distinct filters; never sum across filters
Batch (BS) : many images, same filters
Tensor Shape
Input X [BS, IC, IH, IW]
Weights W [OC, IC, FH, FW]
Bias B [OC]
Output O [BS, OC, OH, OW]
Trainium prefers IC % 128 == 0 and OC % 128 == 0.
Convolution as Systolic MatMul
One dot product : fix tap (fh, fw) and pixel (oh, ow); summing over IC is a dot product → IC = contraction dim K
Stack OC : 128 filters → matrix-vector [IC, OC].T @ [IC, 1]; weights are the 128 × 128 stationary tile
Stack OW (< 512) as moving columns → full MatMul per tap:
$[IC, OC]^T \times [IC, OW] = [128, 128]^T \times [128, OW]$
Loop (fh, fw) over the filter, shifting the input pointer, accumulating every nc_matmul into the same PSUM tile
W_tiled = nl.ndarray(shape=(128, OC // 128, 128, IC // 128, FH, FW),
dtype=W.dtype, buffer=nl.sbuf) # dim 0 = partitions
Check Your Understanding
1. IH = IW = 32, FH = FW = 5, stride 1. Output size?
OH = OW = 32 − 5 + 1 = 28.
2. Mapping convolution onto
[128, 128].T @ [128, OW]: which dimension is the contraction K, and how are FH, FW handled?
IC is K. Loop over each (fh, fw) tap, shift the input window, accumulate into PSUM.