Course contents 17 / 32

17 / 32 · Parallelism and Performance

Domain-specific architectures and the TPU

Why processors are now built for one domain: the end of free speed-ups, Hennessy and Patterson's guidelines, Google's first TPU with its 256 by 256 systolic array of 8-bit multiply-accumulate units, arithmetic intensity, later TPUs and how they differ from GPUs.

On this page 9

The previous note took one matrix product on one core of a general-purpose processor from 0.55 to 14.6 GFLOP/s by rewriting loops, and a vendor library reached 255 GFLOP/s. Every step fitted the computation better to hardware designed to run any program. This note goes the other way: hardware designed for one kind of program. It covers why such domain-specific architectures (DSAs) became attractive, the guidelines for designing one, and the best documented example, Google’s first Tensor Processing Unit, whose heart is a grid of 65,536 multipliers that does nothing but matrix products. This note is a branch: the rest of the course does not depend on it.

Notation. A layer of a neural network computes Y=XWY = XW: XX is M×KM \times K (a batch of MM inputs with KK features each), WW is K×NK \times N (the weights) and YY is M×NM \times N. A MAC is one multiply-accumulate, s←s+wxs \leftarrow s + w x; the product does MKNMKN of them, counted as 2MKN2MKN operations. TOPS means 101210^{12} operations per second. As in the roofline model, PP is the peak arithmetic rate, BB the memory bandwidth and II the arithmetic intensity, operations per byte moved.

Why specialize?#

For decades a program got faster without being changed: each generation of chips had more transistors, and Dennard scaling let them switch faster at the same power per square millimetre. That ended in the mid-2000s, when the supply voltage stopped falling. Multicore chips kept using the extra transistors, but Amdahl’s law limits what more cores give a program with any serial part, and power limits how many of them can run at full speed at once.

A general-purpose core also spends most of its energy on things other than arithmetic. To execute one addition it fetches and decodes an instruction, renames registers, schedules the instruction out of order, predicts branches, and reads operands from register files and caches. A chip that does one computation can drop most of that machinery and spend the transistors and the energy on arithmetic and on memory close to it. Narrow numbers help further: an 8-bit integer multiplier takes about 6 times less energy and 6 times less area than a 16-bit floating-point one, and an 8-bit integer adder 13 times less energy and 38 times less area (Jouppi et al., 2017, §1, citing Dally).

Specialization pays only for a domain that is large and regular. Deep neural networks became one around 2013. Google projected then that if people used voice search for 3 minutes a day, speech-recognition networks would require its datacenters to double (Jouppi et al., 2017, §2). It started a custom chip for inference, the use of a trained network, and deployed it 15 months later, in 2015.

Baseline: a general-purpose processor runs a neural network#

Most of the work of a neural network is matrix products: a fully connected layer is Y=XWY = XW followed by an elementwise nonlinearity, as in the neural-networks note. The natural baseline is the server hardware of 2015. The TPU paper compares with an 18-core Intel Haswell server CPU (2.3 GHz, 145 W, 2.6 TOPS peak in 8-bit integers) and an NVIDIA K80 GPU die (2.8 TOPS, 160 GB/s of memory bandwidth), both deployed in the same datacenters (Jouppi et al., 2017, Table 2). The baseline breaks in two ways.

  • Energy. Each multiply-add pays for the general-purpose machinery around it, and the operands travel from caches or register files for every operation.
  • Response time. Inference serves users, and the application in the paper’s Table 4 had to answer 99 % of requests within 7 ms. Larger batches use the hardware better, because each weight read from memory is used for more inputs, but they take longer. Within 7 ms the CPU could run batches of 16 at 42 % of its best throughput, and the GPU batches of 16 at 37 %. Caches, out-of-order execution and speculation improve the average case and make the worst case harder to predict.

Five guidelines for domain-specific architectures#

Hennessy and Patterson distil the designs of several DSAs into five guidelines (Hennessy and Patterson, 2019, §7.2). For each, how the first TPU follows it:

GuidelineWhyIn the TPU
Use dedicated memories to minimize the distance data movesmoving data costs more energy than computing on it; software knows the access pattern, so caches are not neededa 24 MiB Unified Buffer and 4 MiB of accumulators, managed by software
Spend the resources saved on more arithmetic or bigger memoriesno out-of-order logic, branch predictors or caches65,536 MACs; control is 2 % of the die
Use the easiest form of parallelism that matches the domainmatrix products are data-parallela 2-D array where one instruction drives every multiplier
Reduce data size and type to the simplest needednarrow integers are cheaper in energy, area and bandwidth8-bit integer weights and activations, 32-bit sums
Use a domain-specific programming languageprograms written for a CPU do not port to an unusual machinemodels written in TensorFlow, compiled for the TPU

The low precision is the subject of the quantization note: an int8 × int8 product accumulated in int32 and rescaled once at the end gives almost the accuracy of floating point for inference.

The first TPU#

The TPU is a coprocessor on the PCIe bus of a server, like a GPU, but it fetches no instructions of its own: the host sends them. It has about a dozen instructions in the CISC style, each of which can run for thousands of cycles, and a four-stage pipeline that overlaps them so that the matrix unit is kept busy (Jouppi et al., 2017, §2). The die is made in a 28 nm process, runs at 700 MHz, and has a thermal design power of 75 W; it drew 40 W when busy (Table 2).

Figure

The main blocks of the first TPU and the path of one layer

Block diagram of TPU v1Block diagram of the TPU. At the left the host interface, with arrow 1 to the 24 MiB Unified Buffer. From the buffer, through a data-setup block, arrow 3 leads to the 256 by 256 matrix unit. At the top the 8 GiB DDR3 weight memory sends weights along arrow 2 into the weight FIFO and on into the matrix unit. Below the matrix unit are the 4 MiB accumulators, with arrow 4 to the activation block and back into the buffer. Arrow 5 returns results from the buffer to the host.host interfacePCIe Gen3 ×16Unified Buffer24 MiB, activationsdata setupskews the rowsMatrix MultiplyUnit256 × 256 MACs, 8-bitWeight FIFO4 tiles of 64 KiBWeight Memory8 GiB DDR3, 34 GB/saccumulators4 MiB, 32-bitactivation, poolingReLU, sigmoid, …12345Block diagram of TPU v1Block diagram of the TPU. At the left the host interface, with arrow 1 to the 24 MiB Unified Buffer. From the buffer, through a data-setup block, arrow 3 leads to the 256 by 256 matrix unit. At the top the 8 GiB DDR3 weight memory sends weights along arrow 2 into the weight FIFO and on into the matrix unit. Below the matrix unit are the 4 MiB accumulators, with arrow 4 to the activation block and back into the buffer. Arrow 5 returns results from the buffer to the host.Weight Memory8 GiB DDR3, 34 GB/sWeight FIFO4 tiles of 64 KiBMatrix MultiplyUnit256 × 256 MACs, 8-bitdata setupaccumulators4 MiB, 32-bitactivation, poolingReLU, sigmoid, …Unified Buffer24 MiB, activationshost interfacePCIe Gen3 ×1623415
The first TPU is a coprocessor on the PCIe bus; the host sends it instructions. The numbers on the arrows are its five main instructions: (1) Read_Host_Memory copies inputs into the Unified Buffer; (2) Read_Weights brings a 256 × 256 tile of weights from the off-chip DDR3 memory through the weight FIFO; (3) MatrixMultiply streams rows of activations through the array of 65,536 8-bit multiply-accumulate units, 256 values per cycle; (4) Activate applies the nonlinearity to the 32-bit sums in the accumulators and writes the result back to the buffer, where it becomes the input of the next layer; (5) Write_Host_Memory returns the result to the host. The chip has 28 MiB of on-chip memory: the 24 MiB buffer and 4 MiB of accumulators. Peak: 65,536 · 2 operations · 700 MHz ≈ 92 trillion operations per second.

One layer passes through it as follows, the numbers matching the arrows of the figure:

  1. Read_Host_Memory copies the inputs from the host’s memory into the 24 MiB Unified Buffer, which holds the activations of a whole model as they go from layer to layer.
  2. Read_Weights fetches a tile of 256×256256 \times 256 8-bit weights, 64 KiB, from an off-chip 8 GiB DDR3 Weight Memory at 34 GB/s into the Weight FIFO, four tiles deep. Inside the matrix unit, a tile in use and the next one are held at once, so that loading a new tile, which takes 256 cycles to shift in, overlaps with work on the current one.
  3. MatrixMultiply streams MM rows of 256 activations from the buffer through the Matrix Multiply Unit, 256×256=65,536256 \times 256 = 65{,}536 MACs on 8-bit signed or unsigned integers, one row per cycle. The products are added into accumulators: 4 MiB, 4,096 vectors of 256 32-bit sums. With 16-bit operands the unit runs at half or a quarter of the speed.
  4. Activate applies the nonlinearity (ReLU, sigmoid, and also pooling) to the accumulated sums and writes the results back to the Unified Buffer, as the input of the next layer.
  5. Write_Host_Memory returns the final results to the host.

Worked example. The peak is 65,536 MACs×2×700×106 s−1=91.75≈9265{,}536 \text{ MACs} \times 2 \times 700 \times 10^6\ \text{s}^{-1} = 91.75 \approx 92 TOPS. One weight tile takes 65,536 B/(34×109 B/s)=1.9365{,}536\ \text{B} / (34 \times 10^9\ \text{B/s}) = 1.93 µs to fetch, which is 1,349 cycles at 700 MHz. A MatrixMultiply with a batch of M=200M = 200 rows keeps the array busy for only 200 cycles. Unless a tile is reused for at least about 1,350 rows, the array waits for weights. The paper’s choice of 4,096 accumulators comes from the same number: about 1,350 rows rounded up to 2,048, doubled so that one half can be filled while the other is read (Jouppi et al., 2017, §2).

The die shows where the transistors went: the buffers are 37 % of its area, the computation 30 %, input and output 10 %, and control only 2 % (Figure 2 of the paper). The TPU has none of the features that make a CPU or GPU fast on average: no caches, no branch prediction, no out-of-order execution, no multithreading. Its execution time is deterministic, which suits a 99th-percentile response-time limit.

How a systolic array multiplies matrices#

A systolic array (Kung, 1982) is a grid of identical cells, each connected only to its neighbours, through which data pulses in step with the clock, as blood does with the heartbeat. Every cell does the same small operation each cycle and passes its data on, so a number read once from memory is used by a whole row or column of cells.

The TPU’s array is weight-stationary: each cell holds one weight while activations and partial sums flow past it (Jouppi et al., 2017, §2 and §9). For Y=XWY = XW on a K×NK \times N array:

  • Cell (k,n)(k, n) holds wknw_{kn}, loaded before the computation.
  • Row kk of the array receives column kk of XX from the left edge: xmkx_{mk} enters at cycle m+k+1m + k + 1. Each row starts one cycle after the row above, so the inputs arrive as a staircase.
  • Each cycle, every cell takes the xx from its left neighbour and the partial sum ss from the cell above, computes s+wknxs + w_{kn} x, and passes xx to the right and the new sum down.
  • Cell (k,n)(k, n) therefore sees xmkx_{mk} at cycle m+k+n+1m + k + n + 1, together with the partial sum ∑j<kxmjwjn\sum_{j < k} x_{mj} w_{jn} from above. After the bottom row, the sum is complete:
  ymn=∑k=0K−1xmk wknleaves column n at cycle m+n+K  \boxed{\;y_{mn} = \sum_{k=0}^{K-1} x_{mk}\, w_{kn}\quad\text{leaves column } n \text{ at cycle } m + n + K\;}

Worked example. Take

X=(123456789),W=(102110021),Y=XW=(38591714152623).X = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{pmatrix}, \qquad W = \begin{pmatrix} 1 & 0 & 2 \\ 1 & 1 & 0 \\ 0 & 2 & 1 \end{pmatrix}, \qquad Y = XW = \begin{pmatrix} 3 & 8 & 5 \\ 9 & 17 & 14 \\ 15 & 26 & 23 \end{pmatrix}.

Follow y00y_{00} down column 0. At cycle 1, cell (0,0)(0, 0) receives x00=1x_{00} = 1 and computes 0+1⋅1=10 + 1 \cdot 1 = 1. At cycle 2, cell (1,0)(1, 0) receives x01=2x_{01} = 2 (it entered row 1 one cycle later) and the sum 1 from above: 1+2⋅1=31 + 2 \cdot 1 = 3. At cycle 3, cell (2,0)(2, 0) computes 3+3⋅0=33 + 3 \cdot 0 = 3, and y00=3y_{00} = 3 leaves the array. Meanwhile x00=1x_{00} = 1 has moved right, and at cycle 2 cell (0,1)(0, 1) starts y01y_{01} with 1⋅0=01 \cdot 0 = 0. The number of busy cells per cycle is 1, 3, 6, 7, 6, 3, 1: the wave fills the array, crosses it and drains. The last output, y22=23y_{22} = 23, leaves at cycle 2+2+3=7=M+N+K−22 + 2 + 3 = 7 = M + N + K - 2, after 27 MACs on 9 cells, so the cells were busy 27/63=43 %27/63 = 43\,\% of the time.

Figure

A 3 × 3 systolic array multiplies two matrices, cycle by cycle

Eight states of a 3 by 3 systolic arrayEight panels. The first shows the weights in the nine cells. The next seven show cycles 1 to 7: each cell shows the x passing through it and the partial sum; the x values not yet entered wait at the left, and finished outputs collect below. At cycle 4 seven cells are busy; outputs appear from cycle 3 to cycle 7.weights loaded102110021147258369cycle 1 · 1 MAC1147258369cycle 2 · 3 MACs441023758369cycle 3 · 6 MACs7740125922338693cycle 4 · 7 MACs7048815552269389398cycle 5 · 6 MACs71488589156173539158175cycle 6 · 3 MACs814926614391581726514cycle 7 · 1 MAC92339158172651423green: x, moving rightblue: partial sum, moving downred: finished outputs yamber: weights wEight states of a 3 by 3 systolic arrayEight panels. The first shows the weights in the nine cells. The next seven show cycles 1 to 7: each cell shows the x passing through it and the partial sum; the x values not yet entered wait at the left, and finished outputs collect below. At cycle 4 seven cells are busy; outputs appear from cycle 3 to cycle 7.weights loaded102110021147258369cycle 1 · 1 MAC1147258369cycle 2 · 3 MACs441023758369cycle 3 · 6 MACs7740125922338693cycle 4 · 7 MACs7048815552269389398cycle 5 · 6 MACs71488589156173539158175cycle 6 · 3 MACs814926614391581726514cycle 7 · 1 MAC92339158172651423green: x, moving rightblue: partial sum, moving downred: finished outputs yamber: weights w
Y = XW for X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] and W = [[1, 0, 2], [1, 1, 0], [0, 2, 1]] on a weight-stationary array, as in the TPU. Cell (k, n) holds w_kn (first panel). Row k of X enters from the left k cycles late (the staircase of green numbers waiting at the left); every cycle each x moves one cell right (the green number at the top of a cell), and each cell adds w · x to the partial sum from the cell above and passes it down (the blue number at the bottom). Finished outputs y_mn leave at the bottom (red). The first output, y00 = 1·1 + 2·1 + 3·0 = 3, leaves at cycle 3, the last, y22 = 23, at cycle 7 = M + K + N − 2: 27 multiply-accumulates on 9 cells in 7 cycles.

The fill and the drain cost K+N−2K + N - 2 cycles. For one operation on the 256×256256 \times 256 array they are 510 cycles, against MM cycles of steady work: a batch of M=200M = 200 alone would keep the array busy only 200/710=28 %200/710 = 28\,\% of the time. The TPU hides most of this by pipelining: a new tile of weights takes effect as the wave of the next block of data reaches it, so consecutive operations follow each other through the array without waiting for it to empty, and the instruction takes MM pipelined cycles.

Why this saves energy: for M=200M = 200, K=N=256K = N = 256, the array does MKN=13,107,200MKN = 13{,}107{,}200 MACs while reading only MK+KN=116,736MK + KN = 116{,}736 values from its buffers, 112 MACs per value read. Each activation is read once and used by 256 cells along its row; each weight is read once and used for all MM rows; each partial sum stays inside the array until it is complete. A general-purpose core reads two operands from its register file or cache for every MAC.

Arithmetic intensity and the roofline of the TPU#

The roofline model bounds attainable performance by min⁡(P,I⋅B)\min(P, I \cdot B). For the TPU the slow memory is the weight memory: the activations stay on chip in the Unified Buffer. So the paper measures intensity in operations per byte of weights read from DRAM. A fully connected layer with batch MM uses each 8-bit weight once per input, MM MACs per weight byte; a convolutional layer reuses each weight at many positions of an image and reaches much higher intensity. With P=65,536×700P = 65{,}536 \times 700 MHz MACs per second and B=34B = 34 GB/s, the ridge point is

I∗=65,536×700×106 MAC/s34×109 B/s=1,349 MACs per weight byte,I^* = \frac{65{,}536 \times 700 \times 10^6\ \text{MAC/s}}{34 \times 10^9\ \text{B/s}} = 1{,}349 \ \text{MACs per weight byte},

the “1350 operations per byte” of the paper, and the same 1,349 cycles that one tile takes to arrive. Below it a layer is memory-bound. The six production networks of the paper, which made up 95 % of the TPU’s workload, against this roofline (intensity from Table 1, measured TOPS from Table 3; the bound is min⁡(91.75,2⋅I⋅0.034)\min(91.75, 2 \cdot I \cdot 0.034) TOPS):

NetworkMACs per weight byteRoofline boundMeasuredShare of the bound
MLP020013.6 TOPS12.3 TOPS90 %
MLP116811.49.785 %
LSTM0644.43.785 %
LSTM1966.52.843 %
CNN02,88891.886.094 %
CNN11,75091.814.115 %

The four MLPs and LSTMs, 90 % of the workload, are memory-bound: their matrix unit was active only 8–13 % of the cycles and waited for weights during about half of them (Table 3). CNN0 runs near the peak. CNN1 has the intensity to do so but uses its array badly: some of its layers are too shallow to fill the 256 rows with useful weights, and its fully connected layers are memory-bound.

Even so, the TPU is far ahead. Including the host’s overhead, one TPU die was 14.5 times as fast as a Haswell die on the geometric mean of the six networks and 29.2 times on the mean weighted by their share of the workload; the K80 die was 1.1 and 1.9 times (Table 6). Within the 7 ms limit, the TPU ran MLP0 with batches of 200 at 80 % of its best throughput, against batches of 16 on the CPU and the GPU (Table 4). Counting only the accelerators’ power, the TPU server delivered 41 to 83 times the performance per watt of the CPU server (§5). The authors call this the “Cornucopia Corollary” to Amdahl’s law: low utilization of a huge, cheap resource can still deliver high, cost-effective performance (§10).

The table also says what to fix. The paper’s performance model of a hypothetical TPU with the K80’s faster GDDR5 memory moves the ridge point from 1,350 to 250 and speeds up the six networks by 2.6 times on the geometric mean and 3.9 on the weighted mean, while a faster clock alone gives almost nothing (§7). A larger array would not help either: a 512×512512 \times 512 array needs 4 steps for the 600×600600 \times 600 matrix of LSTM1 instead of the 9 of a 256×256256 \times 256 array, but each step takes four times longer, so the matrix takes 32 µs instead of 18 µs.

DNN inference suits a TPU because it is dominated by large matrix products, whose intensity grows with the batch size, and the rest of the work, the activations and pooling, happens on data already on the chip. Operations with little reuse have no such escape. The softmax of attention in a Transformer reads and writes every score once and is memory-bound on any hardware, which is why the efficient-Transformers note reorganizes attention to keep its scores in on-chip memory.

Later TPUs: training and bfloat16#

The first TPU did inference only, in integers. Training needs a wider range: gradients and updates span many orders of magnitude, and 8-bit integers cannot hold them. The second (2017) and third (2018) generations were built for training and introduced bfloat16, a 16-bit format with float32’s 8-bit exponent and a 7-bit fraction, described in the floating-point note. Their matrix units are 128×128128 \times 128 systolic arrays that multiply in bfloat16 and accumulate in float32 (Google Cloud, 2019). They replaced the DDR3 weight memory by high-bandwidth memory (HBM) and connected chips into pods by a dedicated network, so that one model trains on hundreds of chips.

The arithmetic of the arrays gives the peak rates of the published chips (Jouppi et al., 2023, Table 4):

TPU v1 (2015)TPU v3 (2018)TPU v4 (2020)
Matrix units per chipone 256×256256 \times 2562 cores × 2 of 128×128128 \times 1282 cores × 4 of 128×128128 \times 128
Number formatint8bf16bf16 or int8
Clock700 MHz940 MHz1,050 MHz
Peak92 TOPS123 TFLOPS275 TFLOPS
Memory8 GiB DDR3, 34 GB/s32 GiB HBM, 900 GB/s32 GiB HBM2, 1,200 GB/s
Largest system4 chips per server1,024 chips4,096 chips

For example, TPU v4: 2⋅4⋅12822 \cdot 4 \cdot 128^2 MACs × 2×1.05×109 s−1=275.3\times\, 2 \times 1.05 \times 10^9\ \text{s}^{-1} = 275.3 TFLOPS. Its ridge point against HBM is 275/1.2=229275 / 1.2 = 229 FLOP per byte. TPU v4 was 2.1 times faster than TPU v3 and 2.7 times better in performance per watt, and its 4,096 chips are connected through optical circuit switches that change the network topology for each job.

GPUs and TPUs#

A GPU, as the data-level parallelism note describes, is a general-purpose parallel processor: many streaming multiprocessors, each running thousands of threads, with caches, a large register file and memory designed for bandwidth. Since the Volta generation (2017), NVIDIA GPUs also contain tensor cores, small matrix-multiply units inside each streaming multiprocessor. A TPU puts a few large systolic arrays under the control of a compiler.

GPU (e.g. NVIDIA A100)TPU (e.g. TPU v4)
Matrix hardwaretensor cores, small units in each of 108 SMs8 systolic arrays of 128×128128 \times 128 per chip
Programmingthreads in CUDA, plus libraries for matrix kernelsa compiler from TensorFlow, JAX or PyTorch graphs
On-chip memorycaches, shared memory and registers, partly automaticbuffers managed entirely by software
Hiding memory latencyswitching among thousands of threadsscheduling decided ahead of time by the compiler
Peak in 16-bit312 TFLOPS275 TFLOPS
Memory bandwidth1.6–2.0 TB/s (40 or 80 GB of HBM)1.2 TB/s (HBM2)
What else it runsgraphics, scientific computing, any parallel kernelmainly dense tensor algebra

The peak figures say nothing about which is faster on a given model; that depends on how well the model and its compiler fill the matrix units. Google’s own MLPerf comparison found TPU v4 1.2–1.7 times faster than the A100 at similar system sizes, with 1.3–1.9 times less power (Jouppi et al., 2023); these are a vendor’s measurements on its chosen benchmarks. The two designs have also converged: GPUs added matrix units and low-precision formats, and TPUs added vector units and dataflow processors for embeddings, the work around the matrix products.

What changed#

General-purpose coreTPU v1
Unit of workone instruction, a few operationsone instruction, up to millions of MACs
Arithmetic64-bit integers and floats, a few to tens per cycle with SIMD65,536 8-bit MACs per cycle
On-chip memoryhardware-managed cachessoftware-managed buffers, 28 MiB
Data movementoperands from registers or caches for every operationeach value read once, passed between neighbouring cells
Speculation, out-of-order, cachesyes, for average-case speednone; deterministic timing
What it runsany programneural-network inference

Limitations#

  • Narrow by design. A DSA runs its domain well and little else. If the domain changes (sparse models, new layer types, larger embeddings), the chip ages fast; later TPUs added hardware for embeddings for this reason.
  • Memory-bound most of the time. The first TPU’s large array waited for weights on most of its workload; the peak rate is rarely the achieved rate, and the roofline must be checked for each model.
  • Dependence on the software stack. A DSA is usable only through a compiler and a framework that map the model onto it; without them it is idle silicon.
  • Cost. A custom chip needs a large, stable market to repay its design. Google could justify the TPU by the size of its own datacenters.
  • Evidence from the vendor. Most published TPU numbers come from Google’s own papers, on Google’s chosen workloads.

What you should remember#

  • With Dennard scaling gone and Amdahl’s law limiting multicore, large gains come from hardware specialized for a domain that is big and regular.
  • Hennessy and Patterson’s guidelines: dedicated memories, resources into arithmetic and memory, the simplest parallelism that fits, the narrowest data types that suffice, a domain-specific language.
  • TPU v1: a PCIe coprocessor with a 256×256256 \times 256 array of 8-bit MACs (92 TOPS at 700 MHz), a 24 MiB Unified Buffer, 4 MiB of 32-bit accumulators and weights streamed from 8 GiB of DDR3 at 34 GB/s.
  • A weight-stationary systolic array computes ymn=∑kxmkwkny_{mn} = \sum_k x_{mk} w_{kn} with activations moving right and partial sums moving down; an MM-row operation on a K×NK \times N array takes M+K+N−2M + K + N - 2 cycles alone, and each value read from the buffers serves many MACs.
  • The roofline with the TPU’s ridge point of about 1,350 MACs per weight byte explains why its MLPs and LSTMs were memory-bound and its CNNs compute-bound.
  • Later TPUs train in bfloat16 with float32 accumulation on 128×128128 \times 128 arrays fed by HBM, in pods of thousands of chips. GPUs reach similar peaks with tensor cores inside a general-purpose design.

Code#

The systolic array, simulated one clock cycle at a time for the worked example: it prints the multiply-accumulates of each cycle and the outputs that leave the array, then the product, a check against the definition, and the cycle count (Python, standard library):

def systolic(X, W):
    """Y = X W on a K x N weight-stationary systolic array, simulated one clock cycle at a time.

    Cell (k, n) holds the weight W[k][n]. Row k of X enters the array from the left, skewed by k cycles;
    activations move one cell right per cycle and partial sums one cell down. Returns Y and the cycle count."""
    M, K, N = len(X), len(W), len(W[0])
    x = [[None] * N for _ in range(K)]            # activation register of each cell
    s = [[None] * N for _ in range(K)]            # partial-sum register of each cell: (m, value)
    Y = [[None] * N for _ in range(M)]
    t = macs = 0
    while any(v is None for row in Y for v in row):
        t += 1
        new_x = [[None] * N for _ in range(K)]
        new_s = [[None] * N for _ in range(K)]
        for k in range(K):
            for n in range(N):
                if n == 0:                        # the left edge reads X[m][k] at cycle m + k + 1
                    m = t - 1 - k
                    new_x[k][n] = (m, X[m][k]) if 0 <= m < M else None
                else:                             # otherwise the value the left neighbour held last cycle
                    new_x[k][n] = x[k][n - 1]
                if new_x[k][n] is None:
                    continue
                m, xv = new_x[k][n]
                above = 0 if k == 0 else s[k - 1][n][1]   # the partial sum the cell above held last cycle
                new_s[k][n] = (m, above + W[k][n] * xv)   # one multiply-accumulate
                macs += 1
                if k == K - 1:
                    Y[m][n] = new_s[k][n][1]      # the bottom row emits a finished output
        x, s = new_x, new_s
        active = sum(v is not None for row in s for v in row)
        print(f'cycle {t}: {active} MACs', 'outputs:', [(m, n, Y[m][n]) for m in range(M) for n in range(N)
                                                        if Y[m][n] is not None and m + n + K == t])
    return Y, t, macs

X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]            # three inputs (rows) of three features
W = [[1, 0, 2], [1, 1, 0], [0, 2, 1]]            # weights: 3 features in, 3 outputs
Y, cycles, macs = systolic(X, W)
# cycle 1: 1 MACs outputs: []
# cycle 2: 3 MACs outputs: []
# cycle 3: 6 MACs outputs: [(0, 0, 3)]
# cycle 4: 7 MACs outputs: [(0, 1, 8), (1, 0, 9)]
# cycle 5: 6 MACs outputs: [(0, 2, 5), (1, 1, 17), (2, 0, 15)]
# cycle 6: 3 MACs outputs: [(1, 2, 14), (2, 1, 26)]
# cycle 7: 1 MACs outputs: [(2, 2, 23)]
print(Y, Y == [[sum(X[m][k] * W[k][n] for k in range(3)) for n in range(3)] for m in range(3)])
# [[3, 8, 5], [9, 17, 14], [15, 26, 23]] True
print(cycles, 'cycles,', macs, 'MACs on', 9, 'cells:', f'{macs / (9 * cycles):.0%} busy')
# 7 cycles, 27 MACs on 9 cells: 43% busy
for M in (3, 200, 2000):                         # rows streamed through a 256 x 256 array, fill and drain included
    print(M, M + 256 + 256 - 2, f'{M / (M + 510):.0%}')
# 3 513 1% | 200 710 28% | 2000 2510 80%

The numbers of the TPU sections: peak and ridge point, the time to fetch a weight tile, the roofline against the measured rates of the six networks, the reuse in the array, and the peaks of later chips:

# The first TPU, from Jouppi et al. (2017): 256 x 256 MACs at 700 MHz, weights from DDR3 at 34 GB/s
macs, clock, bw = 256 * 256, 700e6, 34e9
print(macs, round(2 * macs * clock / 1e12, 2), round(macs * clock / bw))   # peak TOPS (2 ops per MAC); ridge in MACs per weight byte
# 65536 91.75 1349
tile = 256 * 256                                                            # one tile of 8-bit weights, bytes
print(tile // 1024, round(tile / bw * 1e6, 2), round(tile / bw * clock))    # KiB, microseconds and cycles to fetch it
# 64 1.93 1349

# roofline: an FC layer with batch size B does B MACs per byte of weights; attainable = min(peak, 2 * I * bandwidth)
apps = {'MLP0': (200, 12.3), 'MLP1': (168, 9.7), 'LSTM0': (64, 3.7), 'LSTM1': (96, 2.8), 'CNN0': (2888, 86.0), 'CNN1': (1750, 14.1)}
for name, (intensity, measured) in apps.items():                           # intensity: MACs per weight byte (Table 1); measured TOPS (Table 3)
    bound = min(2 * macs * clock, 2 * intensity * bw) / 1e12
    print(f'{name:5} {intensity:5} {bound:6.1f} {measured:5.1f} {measured / bound:4.0%}')
# MLP0    200   13.6  12.3  90%
# MLP1    168   11.4   9.7  85%
# LSTM0    64    4.4   3.7  85%
# LSTM1    96    6.5   2.8  43%
# CNN0   2888   91.8  86.0  94%
# CNN1   1750   91.8  14.1  15%

# reads from the buffers for Y = X W, X of M x K, W of K x N, on a weight-stationary array
M, K, N = 200, 256, 256
print(M * K * N, M * K + K * N, round(M * K * N / (M * K + K * N)))       # MACs; values read; MACs per value read
# 13107200 116736 112

# later chips: peak 16-bit TFLOPS from the array sizes, and the ridge point against HBM
for name, cores, mxus, mhz, hbm in (('TPU v3', 2, 2, 940, 900e9), ('TPU v4', 2, 4, 1050, 1200e9)):
    peak = cores * mxus * 128 * 128 * 2 * mhz * 1e6
    print(name, round(peak / 1e12, 1), round(peak / hbm))                   # TFLOPS, FLOP per byte of HBM
# TPU v3 123.2 137
# TPU v4 275.3 229

Next#

The TPU ends the hardware half of the course where it started: with the cost of moving data. Caches, pipelines, vector units and systolic arrays all exist to keep arithmetic fed. So far every program has had the machine to itself. A real computer runs many programs at once, each believing it owns the processor and the memory, and something has to share the hardware among them, protect them from each other and give them access to devices. The next note opens the second half of the course with that program: the operating system.

References#