Is decoding memory-bound? A roofline estimate

During decoding every weight is read from GPU memory for each step, but only a handful of math operations happen per byte. The roofline model says a step takes the longer of its compute time and its memory time.

Write decode_step_time(params, batch, bytes_per_param, peak_flops, mem_bw). For one decode step of a dense model:
- flops = 2 * params * batch
- bytes = params * bytes_per_param (weights are read once per step, shared by the batch; ignore the KV cache)
- compute time = flops / peak_flops, memory time = bytes / mem_bw
Return (step_seconds, bound) where bound is "memory" or "compute", whichever is larger (ties count as "compute").

Signaturedef decode_step_time(params, batch, bytes_per_param, peak_flops, mem_bw)

Understand the idea

The roofline model says a GPU step takes as long as the slower of two jobs: doing the math, or moving the data. During decoding, each step reads every weight from memory once, shared by the whole batch, but does only about 2 operations per parameter per sequence. So at small batches the GPU mostly waits for memory. Worked example with made-up hardware: 7e9 params, 2 bytes each, peak 1e15 FLOP/s, memory bandwidth 2e12 B/s. Batch 1: flops = 2 x 7e9 x 1 = 1.4e10, compute time 1.4e-5 s; bytes = 1.4e10, memory time 7e-3 s. Step = 7 ms, memory bound. Batch 64: compute time 8.96e-4 s, still under 7 ms, so the step time does not change, and you get 64 tokens for the price of 1. Rule: flops = 2 params batch; bytes = params bytes_per_param; step = max(flops / peak, bytes / bw); a tie counts as compute. This is why LLM servers batch requests aggressively.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: 7B fp16, batch 1decode_step_time(7e9, 1, 2, 1e15, 3.35e12)
  • Sample 2: batch 512decode_step_time(7e9, 512, 2, 1e15, 3.35e12)