Unit 3 of 10 · about 4 hours · derive
Parameter, activation, workspace and KV-cache memory
Memory decides the maximum batch before any timing starts. A fit that forgets precision, head grouping or allocator overhead fails at load time, not at arithmetic time.
You'll be able to
- Derive parameter bytes under storage versus execution precision for a named model.
- Derive key-value bytes per token and per request for MHA, GQA and MQA from layer, head and precision entries.
- Fit a batch under a capacity bound with an explicit overhead line, and name what the fit ignores (activations, workspace, allocator).
Covers
Read
In this order, only the parts listed
0 of 3 done
- LLaMA-2-13B bytes: parameters, activations, key-value cacheHow to Scale Your Model - Inference
What about memory? and Modeling throughput and latency for LLaMA 2-13B, including the parameter table and both batch tables
The tables label memory GiB but divide decimal gigabytes by bytes per second (32.7e9 bytes at 6.56e12 bytes/s gives 4.98 ms). Recompute in bytes and keep GB apart from GiB.
- Weights, key-value bytes per token and the capacity fitTransformer Inference Arithmetic
kv cache: the bytes-per-token formula; capacity
Recompute both byte lines: 52e9 parameters at 2 bytes is 104e9 bytes (about 104 GB), and exact key-value bytes per token are 2,097,152. The article's rounded 0.002 GB per token gives about 28000 tokens on 4 GPUs, not the printed 23000; use your own number with a stated overhead.
- Intermediate and workspace bytesTransformer Inference Arithmetic
intermediate memory costs: tensor contractions, statistical normalizations and element-wise operators, with GPU Kernel Fusion
Focus on
- Parameters cost bytes-per-value times count under the stored precision; execution precision can differ (int8 weights with bf16 compute) and changes later intensities, not this line.
- Key-value bytes per token are ; the shortcut assumes key-value width equals model width and breaks under GQA and MQA.
- Activations are megabytes where the cache is gigabytes at 8k context (80 MB against 6.7 GB in the 13B example), but they still set the workspace and fragmentation overhead you must state.
- Capacity fits subtract weights first, then divide by per-request cache; always add an overhead line for allocator, workspace and graphs instead of absorbing it into rounding.
Common mistakes · 3
- Reading the inference chapter's tables as GiB. The step times divide decimal gigabytes by bytes per second; recompute in bytes and keep GB apart from GiB.
- Copying the article's 23000-token fit. Its own rounded 0.002 GB per token gives about 28000 (about 26700 with exact bytes); the printed 23000 carries an unstated discount, so publish your own overhead instead.
- Calling activations negligible everywhere. They are negligible beside an 8k cache; at short context with large batch they set the workspace line.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.How many key-value bytes per token does the invented model (, , ) use in bf16?Answer
bytes (about 0.5 MB); in int8 it is 262,144. Full multi-head attention with would need 2,097,152 bytes in bf16.
2.How many per token for a 40-layer, 8 key-value head, 128-dim model in int8?Answer
bytes (80 kB).
3.Three 40 GB cards hold the 52B weights (104 GB). Can they serve batch 4 at 2048 tokens each?Answer
No, though the article says it can. 16 GB remain, and tokens fit against needed; even the rounded 8000 falls short. Serve batch 3, shorten context, add a card, or shrink the cache.
4.When does the per-token shortcut fail?Answer
Whenever key-value width differs from model width: GQA and MQA need the explicit and entries.
Practice
- How to Scale Your Model inference Question 2: largest batch at 128k context
Worked Problems, Question 2, including the variant (reproduce batch 7, about 56 with one key-value head)
You're done when
Your closed-book byte lines match 6.7e9, 81,920 and the 150-request fit, and Question 2 reproduces 7 (about 56 with one key-value head).
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 7 more in the section
- How to Scale Your Model - InferenceJAX Scaling Book · primary
Work through the single-request, batching, key-value memory and parallelism arithmetic. Recompute every quantity for the small model and workload used by the track project, keeping symbols, units and assumptions visible.
- Transformer Inference ArithmeticKipply · required
Reproduce the FLOP, parameter-byte, key-value-byte and communication estimates for dense transformer inference. Compare the formulas with an instrumented framework trace and explain model residuals instead of fitting constants after measurement.
Also in this section
- AI Performance Engineering - Start HereWafer · reference
A curated, dated index. Its Start Here list names the readings this section already assigns; use the later sections only to find a primary source for a later section, never as the reading itself.
- Efficiently Scaling Transformer InferenceMLSys · required
Study the latency, memory and parallelism model. Use the paper to derive a batch and parallelism crossover for your workload, while reserving distributed execution for GPU-9.
- EtalonEtalon Authors · support
Read the latency metric definitions, their pitfalls and fluidity-index. Add TTFT and TPOT SLOs to a small load generator, and show why peak token throughput can rank two systems differently from an SLO-qualified rate.
- GPU Microbenchmark and Kernel SuiteRoadmap-Final · support
Adopt the project harness schema, architecture constraints and hardware tiers now. Implement only the environment manifest, independent references, raw-trial storage and the initial operation and byte models in this stage.
- How to Scale Your Model - TrainingJAX Scaling Book · required
Work Some Problems to Work Questions 1 to 3 for training-step parameters, Adam and activation bytes and step time; read Data Parallelism and Fully-Sharded Data Parallelism for the comms-bound batch rule.
- Amdahl's lawWikipedia · reference
Read Definition and Derivation for the overall-speedup formula with the worked share examples; apply it to kernel time shares at fixed workload.
- DistServe: Disaggregating Prefill and Decoding for Goodput-optimized LLM ServingZhong et al. (OSDI 2024) · support
Read Section 1 with Figure 1 for per-GPU goodput: the highest request rate served while a target share of requests meets the TTFT and TPOT SLOs.