---
title: Domain-specific architectures
heading: Accelerators for one kind of work, systolic arrays, reduced precision, utilization and the TPU
eyebrow: S4 · Lesson 40 · Domain-specific architectures
xp: 50
---

::: hook
With Dennard scaling gone and general-purpose cores near their limits, the way to make one kind of computation much faster and more efficient is to build hardware for that computation alone. Neural-network inference is the famous example: almost all of its work is multiplying matrices, so Google's first Tensor Processing Unit (2015) spent most of its area on a 256 × 256 grid of 8-bit multiply-accumulate units fed in a rhythmic, systolic pattern, with no caches, no branch prediction and no out-of-order logic. It ran inference far faster per watt than CPUs and GPUs of its day. The same principles guide every accelerator: use dedicated memories, keep parallelism simple, cut precision to what the application needs, and keep the giant array busy.
:::

::: play
- Utilization of an n × n weight-stationary systolic array that streams B input vectors through one loaded set of weights. Inputs enter skewed, so the pass takes $B + 2(n - 1)$ cycles to fill, run and drain, and utilization is $\frac{B}{B + 2(n - 1)}$.
- A big array needs a big batch to be busy: a 256 × 256 array fed 200 vectors is only 28% utilized in such a pass.
- The readout shows the other common loss: a layer whose size d doesn't divide evenly into the array leaves the last tile partly empty.
:::
:::

```sim plot
view: graph
x: B = 1..10000 log
y: u = 0..1
slider: n = 8..512 = 256 log
slider: d = 64..1024 = 300
plot: B/(B + 2*(n - 1)) | samples: 300 | label: utilization during the pass
readout: tiling efficiency for a layer of size d = d/(n*ceil(d/n - 0.0001))
readout: peak at 700 MHz, TOPS (8-bit MACs) = n*n*2*0.7/1000
```

::: predict
## A 256 × 256 array of 8-bit multiply-accumulate units runs at 700 MHz. What's its peak rate, counting a multiply-accumulate as two operations?

- [ ] 0.36 TOPS
- [ ] 46 TOPS
- [x] 92 TOPS
- [ ] 180 TOPS

::: reveal
65,536 MACs × 2 × 700 MHz = 92 trillion operations a second, the first TPU's figure. Its weight memory delivered 34 GB/s, so its roofline ridge was about 2700 operations per byte: only layers that reuse each weight thousands of times (large batches, convolutions) could approach the peak, and many production models ran far below it because they were memory-bound.
:::
:::

::: nameit
## Guidelines for accelerators

1. **Dedicated memories** (scratchpads managed by software) instead of caches, placed where the data flows.
2. **Spend area on arithmetic:** many simple units, little control.
3. **The simplest parallelism** that fits the domain: SIMD, systolic arrays or VLIW, statically scheduled.
4. **Smaller data types:** 8-bit integers or 16-bit floats cut energy, area and bandwidth by large factors where accuracy allows.
5. **A domain-specific language** (TensorFlow, Halide) so software can target the hardware.

## Systolic arrays

Data pulses through a grid of processing elements, each doing one multiply-accumulate and passing its operands to its neighbors, so each value fetched from memory is reused n times without extra memory traffic. In a weight-stationary design each PE holds one weight. Utilization suffers from:

- **fill and drain:** a pass over B vectors takes $B + 2(n - 1)$ cycles;
- **tiling:** a dimension d occupies $\lceil d/n \rceil$ tiles, with efficiency $\frac{d}{n\lceil d/n \rceil}$;
- **memory bandwidth:** weights must be reloaded for each new tile; with batch B each weight byte feeds B multiply-accumulates.

Peak rate = number of MACs × 2 × clock.
:::

::: example
## For a 256 × 256 array, find the utilization of a pass with batches of 200 and 1000, and the tiling efficiency of a 300-wide layer.

::: solution
1. **Batch 200.**
   $$200/(200 + 510) = 28\%$$
2. **Batch 1000.**
   $$1000/1510 = 66\%$$
3. **A 300-wide layer.** It needs 2 tiles of 256:
   $$300/512 = 58.6\%\ \text{full}$$
4. **In practice.** Real systems overlap the drain of one pass with the fill of the next and pick layer sizes that fit, but small batches, which low-latency inference needs, remain the hard case.

**Answer:** utilization is 28% at batch 200 and 66% at batch 1000, and the 300-wide layer tiles at 58.6% efficiency.
:::
:::

::: example
## The array peaks at 91.75 TOPS with 34 GB/s of weight bandwidth. Where's the ridge, and what does a fully connected layer with batch 10 attain if each 8-bit weight is used once per input vector?

::: solution
1. **Ridge.**
   $$\frac{91.75 \times 10^{12}}{34 \times 10^9} = 2700\ \text{ops/byte}$$
2. **Ops per weight byte.** With batch 10, each weight byte does 10 MACs, 20 ops.
3. **Attainable rate.**
   $$34\ \text{GB/s} \times 20 = 0.68\ \text{TOPS}$$
   under 1% of peak.
4. **The fix.** That's why the TPU's successors added high-bandwidth memory.

**Answer:** the ridge is at 2700 ops/byte, and the batch-10 layer attains only 0.68 TOPS, under 1% of peak.
:::
:::

::: example
## A weight-stationary systolic array has 128 × 128 multiply-accumulators at 940 MHz. What is its peak, and how busy is a pass over a batch of 64?

::: solution
1. **Peak.**
   $$\begin{aligned} R &= 16\,384 \times 2 \times 0.94\ \text{GHz} \\ &= 30.8\ \text{TOPS} \end{aligned}$$
2. **One pass.** Filling, streaming B = 64 vectors and draining takes
   $$64 + 2 \times 127 = 318\ \text{cycles}$$
3. **Utilization.** The array does useful work 64 of those 318 cycles, 20% of the time.
4. **Bigger batches.** Batches in the thousands push that above 80%, which is why inference servers batch requests.

**Answer:** the peak is 30.8 TOPS, and a pass over a batch of 64 keeps the array busy 20% of the time.
:::
:::

::: check
given: n = 64 | 128 | 256; f = 0.7 | 1 | 1.4
answer: n*n*2*f/1000
unit: TOPS
tolerance: 0.5%

## An {n} × {n} array of multiply-accumulate units runs at {f} GHz. What's its peak rate, counting a MAC as two operations?

::: right
Right. $n^2 \times 2 \times f$ = {answer} TOPS.
:::

::: misses
- [n*n*f/1000] A multiply-accumulate is two operations.
- [n*2*f/1000] The array has $n^2$ units.
- [any] Not yet. Units × 2 × clock.
:::
:::

::: check
given: n = 32 | 128 | 256; B = 64 | 512 | 4096
answer: 100*B/(B + 2*(n - 1))
unit: %
tolerance: 0.5%

## An {n} × {n} systolic array streams {B} input vectors through one set of weights, taking $B + 2(n - 1)$ cycles. What's its utilization?

::: right
Right. {answer}%.
:::

::: misses
- [100*B/(B + n)] Filling and draining each take n − 1 cycles.
- [any] Not yet. Useful cycles over total cycles.
:::
:::

::: check
given: d = 300 | 520 | 1000; n = 128 | 256
answer: 100*d/(n*ceil(d/n - 0.0001))
unit: %
tolerance: 0.5%

## A layer dimension of {d} is mapped onto an array of width {n}. What fraction of the tiles' width is used?

::: right
Right. $\frac{d}{n\lceil d/n \rceil}$ = {answer}%.
:::

::: misses
- [100*d/n] The layer takes whole tiles, rounded up.
- [any] Not yet. Divide by the width of all the tiles needed.
:::
:::

::: check
given: P = 50 | 100 | 400; W = 30 | 300 | 1200
answer: P*1000/W
tolerance: 0.5%

## An accelerator peaks at {P} TOPS with {W} GB/s of memory bandwidth. Where's its ridge point, in operations per byte?

::: right
Right. {answer} ops/byte.
:::

::: misses
- [P/W] Convert TOPS and GB/s to the same scale.
- [any] Not yet. Peak over bandwidth.
:::
:::

::: check
given: W = 30 | 300; B = 4 | 16 | 128; P = 90 | 400
answer: (P + W*2*B/1000 - abs(P - W*2*B/1000))/2
unit: TOPS
tolerance: 0.5%

## An accelerator peaks at {P} TOPS with {W} GB/s of weight bandwidth. A layer uses each 8-bit weight for a batch of {B} vectors (2 operations each). What does it attain?

::: right
Right. min(peak, bandwidth × 2B) = {answer} TOPS.
:::

::: misses
- [any] Not yet. Each weight byte supports 2B operations: compare with the peak.
:::
:::

::: check
given: b = 32 | 16; q = 8 | 4
answer: b/q
tolerance: 0

## Weights stored as {b}-bit numbers are quantized to {q} bits. By what factor does the memory traffic for weights fall?

::: right
Right. {answer}×.
:::

::: misses
- [any] Not yet. Ratio of the bit widths.
:::
:::

::: check
kind: parts
given: n = 128 | 256; f = 0.7 | 1; B = 64 | 512; d = 300 | 1000

## A weight-stationary systolic array is {n} × {n} at {f} GHz; a pass streams a batch of {B} vectors, and a layer is {d} wide.

::: part
answer: n^2*2*f/1000
unit: TOPS
tolerance: 0.5%

### What is its peak?

::: misses
- [n^2*f/1000] Each MAC counts as two operations.
- [any] MACs × 2 × clock.
:::
:::

::: part
answer: B/(B + 2*(n - 1))*100
unit: %
tolerance: 0.5%

### How busy is it during a pass?

::: misses
- [any] $B/(B + 2(n - 1))$.
:::
:::

::: part
answer: d/(n*ceil(d/n - 1e-9))*100
unit: %
tolerance: 0.5%

### What is the layer's tiling efficiency?

::: misses
- [any] $d/(n\lceil d/n\rceil)$.
:::
:::
:::

::: check
kind: derivation
vars: P = 1e12..1e15, W = 1e9..1e12, B = 1..4096

## An accelerator peaks at P operations a second and loads weights at W bytes a second; each 8-bit weight is reused across a batch of B.

::: step
answer: 2*B

### What is the arithmetic intensity, in operations per weight byte?
:::

::: step
answer: 2*B*W - (2*B*W - P)*step(2*B*W - P)

### What rate does it attain?
:::

::: right
Right. Batching raises the intensity until the array, not the weight bandwidth, sets the speed.
:::
:::

::: explain
## Why can a systolic array do so much arithmetic with so little memory bandwidth?

::: model
In a systolic array, each value read from memory enters at the edge and is passed from one processing element to the next, and every element it passes uses it in a multiply-accumulate. A weight held in place is used once for every input vector that flows by, and an input value is used by every element along its row. So a value fetched once does n operations in an n-wide array, without being read from memory or even from a register file again. The array needs only enough bandwidth to feed its edges, which grows with n, while its arithmetic grows with n², so the ratio of operations to bytes, its arithmetic intensity, grows with the size of the array.
:::
:::

::: practice

::: check
given: n = 128 | 512; f = 0.9 | 1.1
answer: n*n*2*f/1000
unit: TOPS
tolerance: 0.5%

## Peak of an {n} × {n} array at {f} GHz?

::: misses
- [any] $2n^2f$.
:::
:::

::: check
given: n = 64 | 256; B = 100 | 2000
answer: 100*B/(B + 2*(n - 1))
unit: %
tolerance: 0.5%

## Utilization: {n} × {n} array, batch {B}?

::: misses
- [any] $B/(B + 2(n - 1))$.
:::
:::

::: check
given: d = 200 | 700; n = 128 | 256
answer: 100*d/(n*ceil(d/n - 0.0001))
unit: %
tolerance: 0.5%

## Tiling efficiency: dimension {d}, width {n}?

::: misses
- [any] $d/(n\lceil d/n \rceil)$.
:::
:::
:::
