S14 · Lesson 47 · Running neural networks on hardware
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
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.
- 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.
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?
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.
Name it
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
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 with step . Rounding adds error that's roughly uniform on ±Δ/2, with RMS . 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.
Worked 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?
Show the solution
- Batch 1. 33.6 MFLOP over 16.8 MB of weights: far below the ridge at 100, so it runs at 2 TFLOP/s, 2% of peak.
- Batch 64. 2.15 GFLOP over the same weights plus 64 × 2 × 4096 bytes of activations: 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.
Worked 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?
Show the solution
- Step.
- Rounding noise.
- Reading the weights. At about 160 pJ per byte (640 pJ per 32-bit word):
- The multiplies. 16.8 million 8-bit multiplies at 0.2 pJ take
- 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).
Worked 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?
Show the solution
- Batch 1. Each step reads all 7 GB of weights: so batch 1 gives 143 tokens/s.
- Batch 32. The same read serves 32 sequences, and the arithmetic, takes 4.5 ms, still less than the read: 4600 tokens/s for the same hardware.
- Where compute takes over. When the arithmetic intensity, 2B operations per byte, reaches the chip's 100 operations per byte: Beyond it throughput stays at 7100 tokens/s.
- 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
A dense layer maps 1024 inputs to 256 outputs for a batch of 32. How many FLOPs does it take?
Check
A model has 7 × 10⁹ parameters stored at 4 bits each. How many gigabytes do its weights take?
Check
A 7 × 10⁹-parameter model with 4-bit weights generates text at batch size 1 on a device with 3000 GB/s of memory bandwidth. What's the upper limit on tokens per second?
Check
A chip peaks at 100 TFLOP/s with 0.5 TB/s of memory bandwidth. What performance can a kernel with arithmetic intensity 300 FLOP/byte reach, in TFLOP/s?
Check
Weights up to ±0.5 are quantized symmetrically with 127 positive levels. What's the RMS rounding error?
Check
An inference does 1 × 10⁷ multiplies at 0.2 pJ each and reads 1 × 10⁸ values from DRAM at 640 pJ each. How much energy does it take?
Write units the way you normally would: 1.5 J, 1500 mJ and 0.0015 kJ are the same answer.
Check
A 7-billion-parameter model with 4-bit weights runs on a chip with 1000 TFLOP/s and 3 TB/s.
(a)
At what batch size does it become compute-bound?
(b)
What throughput does it reach once compute-bound?
Check
Values over ±m are quantized symmetrically with Q positive levels.
Step 1
What is the step size?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the RMS rounding error?
Step 3
What signal-to-noise ratio does a full-scale uniformly distributed signal get, in dB?
Explain why
Why is small-batch neural network inference usually limited by memory bandwidth rather than arithmetic?
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
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.