S1447 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

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

P=min⁡(F,I⋅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=round(w/Δ) with step Δ=max⁡|w|/127. Rounding adds error that's roughly uniform on ±Δ/2, with RMS Δ/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.

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
  1. Batch 1. 33.6 MFLOP over 16.8 MB of weights: I=33.6/16.8=2FLOP/byte 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=124FLOP/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.

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
  1. Step. Δ=0.5/127=0.00394
  2. Rounding noise. RMS=0.00394/12=0.00114
  3. Reading the weights. At about 160 pJ per byte (640 pJ per 32-bit word): 16.8MB×160pJ=2.7mJ
  4. The multiplies. 16.8 million 8-bit multiplies at 0.2 pJ take 16.8×106×0.2pJ=3.4μ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).

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
  1. Batch 1. Each step reads all 7 GB of weights: 7GB/(1TB/s)=7ms so batch 1 gives 143 tokens/s.
  2. Batch 32. The same read serves 32 sequences, and the arithmetic, 2×7×109×32=4.5×1011operations 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: 2B=100B=50 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

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°.

Explain why

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

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.