CS 152 Computer Architecture and Engineering
CS252A Graduate Computer Architecture Lecture 23 - Spatial Architectures and Trainium

Chris Fletcher
UC Berkeley, Spring 2026

Why Spatial Architectures?

$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 rskewed B rows adg beh cfi P Q RS T UV 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

CycleWhat happens
0j enters PE(0,0) → a·j down
1PE(0,0): a·k; PE(0,1): d·j; PE(1,0): a·j + b·m
2PE(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
3Q = a·k + b·n + c·q; S = d·j + e·m + f·p
4R = a·l + b·o + c·r; T = d·k + e·n + f·q; V = g·j + h·m + i·p
5U = d·l + e·o + f·r; W = g·k + h·n + i·q
6X = g·l + h·o + i·r

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

EngineRoleReads/writes
Tensor (128 × 128 systolic)MatMulSBUF → PSUM only
Vector (128-wide)Elementwise, reductionsSBUF, PSUM
Scalar (128-wide)Non-linear functions (ReLU)SBUF, PSUM
GPSIMD (128-wide)General programmable SIMDSBUF

The Bandwidth Gap

LevelCapacityBandwidth
Host DRAM1 TB16 GB/s
Device HBM16 GB0.5 TB/s
SBUF + PSUM24 MB + 2 MB10 TB/s

$\dfrac{10 \text{ TB/s}}{0.5 \text{ TB/s}} = 20\times$

Basic NKI MatMul

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

$M_t K_t + N_t K_t + M_t N_t \le \text{SBUF size}$

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)

PatchRowsSum+B
(0,0)1 + 7 + 0813
(0,1)10 + 10 + 264651
(1,0)13 + 16 + 305964
(1,1)−2 + 21 + 42328

$O = \begin{pmatrix}13&51\\64&28\end{pmatrix}$

Deep Learning Convolution Tensors

TensorShape
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

  1. One dot product: fix tap (fh, fw) and pixel (oh, ow); summing over IC is a dot product → IC = contraction dim K
  2. Stack OC: 128 filters → matrix-vector [IC, OC].T @ [IC, 1]; weights are the 128 × 128 stationary tile
  3. Stack OW (< 512) as moving columns → full MatMul per tap:

$[IC, OC]^T \times [IC, OW] = [128, 128]^T \times [128, OW]$

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.