Skip to content

๐Ÿงฑ Chapter 2 โ€” The Two Walls: Compute vs Memory โ€‹

Episodes 08, 10 โ€“ 13, 17 ยท โญ "Don't skip episode 8"

A GPU can only run out of two things: ๐Ÿงฎ compute or ๐Ÿ“ฆ memory bandwidth. Everything else in the course is about walking up to one of these two walls.


1. The memory hierarchy (H100) โ€‹

Rule: the closer to compute, the faster, and the less of it you get.

CliffSizeWhat it causes
Off the chip (SRAM โ†’ HBM)~10ร—Attention's big Nร—N grid spills to HBM โ†’ motivates FlashAttention
Off the card (HBM โ†’ host)~52ร—Offloading the KV cache to host RAM only pays when the alternative is recomputing the prompt

๐Ÿ”Œ NVLink-C2C (Grace-Hopper style): 900 GB/s bidirectional vs PCIe's 128 GB/s bidirectional, about 7ร—. On those machines cliff 2 is just a step.


2. Arithmetic intensity vs the ridge point โญ โ€‹

The roofline picture โ€‹

Why decode โ‰ˆ 1 FLOP/byte (on any model) โ€‹

For an 8B model in FP16: weights = 16 GB.

  • Work: ~2 FLOPs per weight (multiply + add)
  • Bytes: 2 bytes per weight
  • โ†’ AI โ‰ˆ 1, against the H100's 295, so ~300ร— short

Weights and work both scale with parameter count, so 70B and 405B also give โ‰ˆ 1. This comes from generating one token at a time, not from model size.

StageWeights loaded once, used byโ€ฆAIWall
Prefillthousands of prompt tokenshigh๐Ÿ”ฅ compute
Decode (batch 1)1 tokenโ‰ˆ 1๐ŸงŠ memory
Decode (batch 8)8 tokensโ‰ˆ 8๐ŸงŠ still memory

Two escape routes from the memory wall:

  1. Move fewer bytes โ†’ quantization
  2. More tokens per byte โ†’ batching (it takes far more batching than people expect to reach 295)

3. Reading spec sheets without getting fooled ๐Ÿ“„ โ€‹

๐Ÿ” Three traps

  1. Mixed-chip comparisons: "19 TB/s SRAM vs 3.35 TB/s HBM" puts an A100 figure next to an H100 figure. Done on one chip, it's still about 10:1.
  2. Sparsity asterisk: B200 "2.25 / 4.5 PFLOPS" means without / with 2:4 structured sparsity (2 of every 4 neighboring weights must be exactly zero, which needs pruning + retraining). Your checkpoint uses the smaller (dense) number. Measured sparse speedup on Hopper averaged 1.42ร—, not 2ร—.
  3. Comparison tables mix dense and sparse rows โ†’ up to 2ร— wrong. If a table doesn't say dense, treat the number as unusable.

H100 vs H200: same compute, different memory โ€‹

H100H200
DieGH100GH100 (same)
FP16 dense989 TFLOPS989 TFLOPS (same)
HBM80 GB @ 3.35 TB/s141 GB @ 4.8 TB/s
Ridge point295206

So when someone says the H200 is faster, they mean bandwidth. That's exactly what decode needs.

๐Ÿ“Š Ridge-point cheat sheet (dense FP16 / BF16) โ€‹

GPURidge (FLOP/byte)
A100153
H200206
B200~292 (7.7 TB/s sheet; 281 with 8 TB/s)
H100295
MI355X~315
L4403

A ridge point belongs to a precision, not a chip. At FP8 the H100's becomes about 590.


4. KV cache: the bytes that eat your GPU ๐Ÿฝ๏ธ โ€‹

The formula โ€‹

KV bytes per token = 2 (K and V) ร— layers ร— kv_heads ร— head_dim ร— bytes_per_value

Read num_hidden_layers, num_key_value_heads and head_dim (or hidden_size / num_attention_heads) from config.json.

โš ๏ธ Use the KV head count, not the attention head count

Llama-3-8B has 32 attention heads but only 8 KV heads. Using 32 makes your estimate 4ร— too big.

ModelLayersKV headsPer token (FP16)128K-token convo
Llama 3 8B328128 KB~17 GB (> the 16 GB of weights!)
Llama 3 70B808320 KB~42 GB
Llama 3 405B1268504 KB~66 GB
70B with MHA (64 KV heads)80642.5 MB~340 GB ๐Ÿ˜ฑ
DeepSeek V3 (MLA)โ€”โ€”~70 KB~9 GB

Model is 50ร— bigger, but cost per token is only ~4ร— bigger. The KV cache follows the model's shape (layers ร— KV heads), not its parameter count.

Three properties of the KV cache โ€‹

๐Ÿงฎ Capacity worksheet: H100 + Llama 3 8B โ€‹

Raising the supported context length isn't a config change. It's a memory purchase. Price it before you say yes.


5. MFU vs MBU: "busy" isn't "used" โ€‹

MFU: Model FLOPs UtilizationMBU: Model Bandwidth Utilization
Use for๐Ÿ“– Prefill (compute-bound)โœ๏ธ Decode (memory-bound)
Formula2 ร— params ร— tokens/s รท peak dense FLOP/s(weights + KV bytes) รท TPOT รท peak bandwidth
Example8B, 30K tok/s in โ†’ 480 TFLOP/s รท 989 = ~49%7B FP16 (14 GB) / 14 ms = 1 TB/s รท 2 TB/s = 50% (Databricks)
Trap โš ๏ธUsing the sparse peak halves your MFU (49% โ†’ 24%)Blending MFU+MBU into one number means nothing
Trap โš ๏ธCounting padding tokens inflates MFUโ€”
  • Model FLOPs = math the model owes for delivered tokens. Hardware FLOPs also count recomputation, which flatters you.
  • There's no published "healthy" value. Baseline yours and alert on drops.

๐Ÿš€ The decode speed ceiling (batch 1, 100% MBU) โ€‹

max tok/s = bandwidth รท weight bytes. For an 8B model in FP16 (16 GB):

Realistic MBU is 60โ€“80%, so an H100 gives about 125โ€“170 tok/s. FP8 = exactly 2ร—: half the bytes, twice the tokens.


6. ๐Ÿฉบ Clinic: "100% GPU but still slow" โ€‹

๐Ÿ”ฆ nvidia-smi "GPU-Util" is a light switch with a stopwatch

NVIDIA's definition: percent of time during which one or more kernels was executing. One kernel on 1 of 132 SMs reads 100%. Use it for "is anything running", memory used and temperature. Never use it for capacity.

The batch-size sweep (hold prompt/output fixed, walk batch up) โ€‹

Pick your window first: upper bound = min(ridge point, KV-cache capacity). Test 4โ€“5 points inside it.

Single-request MBU test:

  • MBU > ~60% and throughput won't rise โ†’ truly bandwidth-saturated, so move fewer bytes
  • MBU low with GPU going idle between kernels โ†’ software-bound. Quantizing won't help.

7. ๐Ÿงช Lab: vLLM on one L4 (Qwen3-8B, FP16, ridge 403) โ€‹

MeasurementResult
Pure decode, batch 1MFU 0.22%, MBU 84%, AI โ‰ˆ 1 (385ร— below ridge)
Concurrency 1 โ†’ 3216 โ†’ 382 tok/s (23ร—)
ITL 1 โ†’ 3260 ms โ†’ 79 ms (only ~โ…“ worse)
TTFT 1 โ†’ 320.1 s โ†’ 1.3 s (13ร— worse, queueing behind a full batch)
Bytes per output token15.3 GB โ†’ ~0.55 GB (28ร— fewer)
nvidia-smi across all 9 runs100%, spread = 0 (while output varied 36ร—)

๐ŸŽฏ Two reference points: 16 (latency-constrained: 225 tok/s, TTFT ~0.75 s) and 32 (throughput: 382 tok/s, TTFT ~1.3 s). Judge every later optimization against both.

๐Ÿชค Three traps that gave plausible but wrong numbers

  1. MBU = 122%: built-in warm-up traffic was counted in the counters but not in the duration โ†’ run warm-up as a separate command.
  2. Reused seed โ†’ 94% prefix-cache hit โ†’ fake-fast prefill โ†’ new seed per run.
  3. FLOP counters read exactly 0: a server flag was needed for them to exist. Zero looks like an idle GPU.

Caveats: vLLM's MFU is estimated from tensor shapes. MBU is a floor (it excludes KV traffic). The preemption "knee" was never reached (preemptions = 0 at batch 32).

<<>> with โ™ฅ๏ธ by S@Nchit