---
title: Running neural networks on hardware
heading: Counting the operations and bytes of a network, arithmetic intensity and the roofline model, why small-batch inference is limited by memory bandwidth, quantizing weights to 8 and 4 bits with its rounding noise, and the energy cost of moving data compared with computing on it
eyebrow: S14 · Lesson 47 · Running neural networks on hardware
xp: 50
---

::: hook
A trained network is a long list of multiply-accumulates, and running it is an engineering problem with the same constraints as any other digital system: how many operations per second, how many bytes per second from memory, how many joules. S4's roofline model applies directly, and it gives a surprising answer for most deployed networks: they're limited by how fast weights can be read, not by arithmetic. That's why accelerators are built around memory, why batching matters so much, and why weights are squeezed from 32-bit floats down to 8 or even 4 bits.
:::

::: play
- The roofline: attainable performance against **arithmetic intensity**, the FLOPs done per byte moved from memory. Below the ridge point, performance is bandwidth × intensity; above it, the compute peak.
- A batch-1 matrix-vector product with 8-bit weights does 2 FLOPs per weight byte, far left of the ridge on any modern accelerator.
- Batching B inputs reuses each weight B times, sliding the operating point right until it hits the compute roof.
:::

```sim plot
view: graph
x: I = 0.1..2000 log
y: P = 0.01..1000 log
slider: F = 1..500 = 100 log
slider: W = 0.05..5 = 1 log
plot: (F + I*W - abs(F - I*W))/2 | samples: 400 | label: attainable TFLOP/s
marker: I = F/W | label: ridge point
readout: ridge point, FLOP per byte = F/W | digits: 4
readout: batch-1 int8 performance, TFLOP/s = (F + 2*W - abs(F - 2*W))/2 | digits: 4
```

::: predict
## A 7-billion-parameter language model with 8-bit weights runs at batch size 1 on a device with 100 GB/s of memory bandwidth. Roughly how many tokens per second can it produce at most?

- [ ] About 1,000
- [ ] About 100
- [x] About 14
- [ ] About 1

::: reveal
Every generated token needs every weight once: 7 GB read from memory. At 100 GB/s that's at most 100/7 = 14 tokens per second, however fast the arithmetic is. With 16-bit weights it halves to 7; with 4-bit weights it doubles to 28.
:::
:::

::: nameit
## Counting work

A dense layer from D inputs to H outputs does DH multiply-accumulates per input, which is 2DH floating-point operations (FLOPs). Its weights take DH × (bits/8) bytes: 4 for 32-bit floats, 2 for 16-bit, 1 for int8, 0.5 for int4.

## The roofline

With peak compute F and memory bandwidth W, a kernel with arithmetic intensity I (FLOPs per byte of memory traffic) runs at

$$P = \min(F, I \cdot W)$$

The **ridge point** F/W separates memory-bound kernels from compute-bound ones; on current accelerators it's in the hundreds of FLOPs per byte. A batch-1 layer with int8 weights has I ≈ 2. A batch of B reuses each weight B times, giving I ≈ 2B (ignoring activation traffic). For generation at batch 1, tokens per second ≤ W/(model bytes).

## Quantization

Symmetric int8 quantization stores $q = \text{round}(w/\Delta)$ with step $\Delta = \max|w|/127$. Rounding adds error that's roughly uniform on ±Δ/2, with RMS $\Delta/\sqrt{12}$. Networks usually tolerate 8-bit weights with little accuracy loss; 4-bit needs care (per-channel or per-group scales, outlier handling).

## Energy

Moving data costs far more than computing on it. For 45 nm CMOS, Horowitz (ISSCC 2014) reported about 0.2 pJ for an 8-bit integer multiply, 3.7 pJ for a 32-bit float multiply and about 640 pJ to read a 32-bit word from DRAM. Accelerators therefore keep weights in on-chip SRAM, reuse each fetched value many times (systolic arrays, S4) and use the narrowest numbers that keep accuracy.
:::

::: example
## A 4096 × 4096 int8 layer runs at batch 1 and at batch 64 on a chip with F = 100 TFLOP/s and W = 1 TB/s. Is it memory- or compute-bound?

::: solution
1. **Batch 1.** 33.6 MFLOP over 16.8 MB of weights:
   $$\begin{aligned} I &= 33.6/16.8 \\ &= 2\ \text{FLOP/byte} \end{aligned}$$
   far below the ridge at 100, so it runs at 2 TFLOP/s, 2% of peak.
2. **Batch 64.** 2.15 GFLOP over the same weights plus 64 × 2 × 4096 bytes of activations:
   $$I = 124\ \text{FLOP/byte}$$
   past the ridge, so it can reach the 100 TFLOP/s roof.

**Answer:** at batch 1 the layer is memory-bound, running at 2% of peak; at batch 64 it is compute-bound.
:::
:::

::: example
## Weights range up to ±0.5. What's the int8 step and rounding noise? And how does the energy of reading the 4096 × 4096 layer's weights from DRAM compare with its multiplies?

::: solution
1. **Step.**
   $$\begin{aligned} \Delta &= 0.5/127 \\ &= 0.00394 \end{aligned}$$
2. **Rounding noise.**
   $$\begin{aligned} \text{RMS} &= 0.00394/\sqrt{12} \\ &= 0.00114 \end{aligned}$$
3. **Reading the weights.** At about 160 pJ per byte (640 pJ per 32-bit word):
   $$16.8\ \text{MB} \times 160\ \text{pJ} = 2.7\ \text{mJ}$$
4. **The multiplies.** 16.8 million 8-bit multiplies at 0.2 pJ take
   $$16.8 \times 10^6 \times 0.2\ \text{pJ} = 3.4\ \mu\text{J}$$
5. **Compare.** The data movement costs about 800 times more.

**Answer:** the step is 0.00394 and the rounding noise 0.00114 RMS; reading the weights (2.7 mJ) costs about 800 times as much as the multiplies (3.4 µJ).
:::
:::

::: example
## A 7-billion-parameter model with int8 weights runs on a chip with 1 TB/s of memory bandwidth and 100 TFLOP/s. How many tokens per second does it generate at batch 1 and at batch 32, and at what batch does compute take over?

::: solution
1. **Batch 1.** Each step reads all 7 GB of weights:
   $$7\ \text{GB} / (1\ \text{TB/s}) = 7\ \text{ms}$$
   so batch 1 gives 143 tokens/s.
2. **Batch 32.** The same read serves 32 sequences, and the arithmetic,
   $$\begin{aligned} &2 \times 7 \times 10^9 \times 32 \\ &= 4.5 \times 10^{11}\ \text{operations} \end{aligned}$$
   takes 4.5 ms, still less than the read: 4600 tokens/s for the same hardware.
3. **Where compute takes over.** When the arithmetic intensity, 2B operations per byte, reaches the chip's 100 operations per byte:
   $$\begin{aligned} 2B &= 100 \\ B &= 50 \end{aligned}$$
   Beyond it throughput stays at 7100 tokens/s.
4. **Why batch.** Serving systems batch users together for exactly this reason.

**Answer:** 143 tokens/s at batch 1 and 4600 at batch 32; compute takes over at batch 50, where throughput levels off at 7100 tokens/s.
:::
:::

::: check
given: D = 256 | 1024 | 4096; H = 256 | 4096; B = 1 | 32
answer: 2*B*D*H
tolerance: 0.5%

## A dense layer maps {D} inputs to {H} outputs for a batch of {B}. How many FLOPs does it take?

::: right
Right. $2BDH$ = {answer}.
:::

::: misses
- [B*D*H] That counts multiply-accumulates; each is two FLOPs.
- [any] Not yet. Two FLOPs per weight per input.
:::
:::

::: check
given: P = 1e8 | 7e9 | 7e10; b = 4 | 8 | 16
answer: P*b/8/1e9
tolerance: 0.5%

## A model has {P} parameters stored at {b} bits each. How many gigabytes do its weights take?

::: right
Right. $P \times b/8$ bytes = {answer} GB.
:::

::: misses
- [P*b/1e9] Divide bits by 8 to get bytes.
- [any] Not yet. Parameters times bytes per parameter.
:::
:::

::: check
given: P = 1e9 | 7e9 | 7e10; b = 4 | 8 | 16; W = 50 | 400 | 3000
answer: W/(P*b/8/1e9)
tolerance: 0.5%

## A {P}-parameter model with {b}-bit weights generates text at batch size 1 on a device with {W} GB/s of memory bandwidth. What's the upper limit on tokens per second?

::: right
Right. Bandwidth over model size: {answer} tokens per second.
:::

::: misses
- [any] Not yet. Every token reads every weight once.
:::
:::

::: check
given: F = 10 | 100 | 1000; W = 0.5 | 2 | 8; I = 2 | 20 | 300
answer: (F + I*W - abs(F - I*W))/2
tolerance: 0.5%

## A chip peaks at {F} TFLOP/s with {W} TB/s of memory bandwidth. What performance can a kernel with arithmetic intensity {I} FLOP/byte reach, in TFLOP/s?

::: right
Right. min(F, I·W) = {answer} TFLOP/s.
:::

::: misses
- [any] Not yet. The smaller of the compute peak and intensity times bandwidth.
:::
:::

::: check
given: m = 0.1 | 0.5 | 2; Q = 127 | 7
answer: m/Q/sqrt(12)
tolerance: 0.5%

## Weights up to ±{m} are quantized symmetrically with {Q} positive levels. What's the RMS rounding error?

::: right
Right. $\Delta/\sqrt{12}$ with $\Delta = \max|w|/Q$: {answer}.
:::

::: misses
- [m/Q/2] That's the largest error, half a step; the RMS of a uniform error is $\Delta/\sqrt{12}$.
- [any] Not yet. Step size, then the RMS of a uniform error.
:::
:::

::: check
given: N = 1e7 | 1e9; e = 0.2 | 3.7; M = 1e6 | 1e8; d = 160 | 640
answer: (N*e + M*d)*1e-12
unit: J
tolerance: 0.5%

## An inference does {N} multiplies at {e} pJ each and reads {M} values from DRAM at {d} pJ each. How much energy does it take?

::: right
Right. $(Ne_\text{mult} + Me_\text{DRAM})$ = {answer} J.
:::

::: misses
- [N*e*1e-12] Include the memory reads, which usually dominate.
- [any] Not yet. Add the compute and memory energies.
:::
:::

::: check
kind: parts
given: P = 1 | 7 | 70; b = 4 | 8; F = 100 | 1000; W = 1 | 3

## A {P}-billion-parameter model with {b}-bit weights runs on a chip with {F} TFLOP/s and {W} TB/s.

::: part
answer: F*b/(16*W)
tolerance: 0.5%

### At what batch size does it become compute-bound?

::: misses
- [F/W] Each weight byte serves 2B operations at batch B, and a weight takes b/8 bytes.
- [any] Set $16B/b$ operations per byte equal to F/W.
:::
:::

::: part
answer: F*1e12/(2*P*1e9)
unit: tokens/s
tolerance: 0.5%

### What throughput does it reach once compute-bound?

::: misses
- [any] Peak operations over 2P per token.
:::
:::
:::

::: check
kind: derivation
vars: m = 0.01..10, Q = 7..32767

## Values over ±m are quantized symmetrically with Q positive levels.

::: step
answer: m/Q

### What is the step size?
:::

::: step
answer: m/(Q*sqrt(12))

### What is the RMS rounding error?
:::

::: step
answer: 10*log(4*Q^2)

### What signal-to-noise ratio does a full-scale uniformly distributed signal get, in dB?
:::

::: right
Right. That's 6 dB per bit, as for any uniform quantizer.
:::
:::

::: explain
## Why is small-batch neural network inference usually limited by memory bandwidth rather than arithmetic?

::: model
At batch size 1, each weight is read from memory, used in one multiply-accumulate, and never touched again until the next input. That's two operations per weight, so an 8-bit model does about two FLOPs per byte fetched. Modern accelerators can do hundreds of FLOPs in the time it takes to fetch one byte from DRAM, so the multipliers sit idle waiting for weights, and speed is set by bandwidth divided by model size. Batching many inputs reuses each fetched weight many times, raising the arithmetic intensity, and quantization shrinks the bytes per weight; both move the workload toward the compute roof.
:::
:::

::: practice

::: check
given: D = 64 | 8192; H = 64 | 8192; B = 1 | 512
answer: 2*B*D*H
tolerance: 0.5%

## FLOPs: D = {D}, H = {H}, B = {B}?

::: misses
- [any] $2BDH$.
:::
:::

::: check
given: P = 1e8 | 4e11; b = 4 | 32; W = 20 | 8000
answer: W/(P*b/8/1e9)
tolerance: 0.5%

## Tokens/s bound: {P} parameters, {b} bits, {W} GB/s?

::: misses
- [any] $W/\text{model bytes}$.
:::
:::

::: check
given: F = 1 | 2000; W = 0.1 | 10; I = 0.5 | 1000
answer: (F + I*W - abs(F - I*W))/2
tolerance: 0.5%

## Roofline: F = {F}, W = {W}, I = {I}?

::: misses
- [any] min(F, I·W).
:::
:::
:::
