๐งฑ 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.
| Cliff | Size | What 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.
| Stage | Weights loaded once, used byโฆ | AI | Wall |
|---|---|---|---|
| Prefill | thousands of prompt tokens | high | ๐ฅ compute |
| Decode (batch 1) | 1 token | โ 1 | ๐ง memory |
| Decode (batch 8) | 8 tokens | โ 8 | ๐ง still memory |
Two escape routes from the memory wall:
- Move fewer bytes โ quantization
- 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
- 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.
- 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ร.
- 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 โ
| H100 | H200 | |
|---|---|---|
| Die | GH100 | GH100 (same) |
| FP16 dense | 989 TFLOPS | 989 TFLOPS (same) |
| HBM | 80 GB @ 3.35 TB/s | 141 GB @ 4.8 TB/s |
| Ridge point | 295 | 206 |
So when someone says the H200 is faster, they mean bandwidth. That's exactly what decode needs.
๐ Ridge-point cheat sheet (dense FP16 / BF16) โ
| GPU | Ridge (FLOP/byte) |
|---|---|
| A100 | 153 |
| H200 | 206 |
| B200 | ~292 (7.7 TB/s sheet; 281 with 8 TB/s) |
| H100 | 295 |
| MI355X | ~315 |
| L4 | 403 |
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_valueRead 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.
| Model | Layers | KV heads | Per token (FP16) | 128K-token convo |
|---|---|---|---|---|
| Llama 3 8B | 32 | 8 | 128 KB | ~17 GB (> the 16 GB of weights!) |
| Llama 3 70B | 80 | 8 | 320 KB | ~42 GB |
| Llama 3 405B | 126 | 8 | 504 KB | ~66 GB |
| 70B with MHA (64 KV heads) | 80 | 64 | 2.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 Utilization | MBU: Model Bandwidth Utilization | |
|---|---|---|
| Use for | ๐ Prefill (compute-bound) | โ๏ธ Decode (memory-bound) |
| Formula | 2 ร params ร tokens/s รท peak dense FLOP/s | (weights + KV bytes) รท TPOT รท peak bandwidth |
| Example | 8B, 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) โ
| Measurement | Result |
|---|---|
| Pure decode, batch 1 | MFU 0.22%, MBU 84%, AI โ 1 (385ร below ridge) |
| Concurrency 1 โ 32 | 16 โ 382 tok/s (23ร) |
| ITL 1 โ 32 | 60 ms โ 79 ms (only ~โ worse) |
| TTFT 1 โ 32 | 0.1 s โ 1.3 s (13ร worse, queueing behind a full batch) |
| Bytes per output token | 15.3 GB โ ~0.55 GB (28ร fewer) |
| nvidia-smi across all 9 runs | 100%, 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
- MBU = 122%: built-in warm-up traffic was counted in the counters but not in the duration โ run warm-up as a separate command.
- Reused seed โ 94% prefix-cache hit โ fake-fast prefill โ new seed per run.
- 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).
