Unit 1 of 13 · about 4 hours · explain, derive
Model identity and the architecture fields that set serving cost
Serving numbers mean nothing without the exact model they describe. A changed revision, template or head count moves every later estimate.
You'll be able to
- Record the model, tokenizer, chat template and revision that define a serving run, and reject a run whose identity differs.
- Derive parameter count and KV width from architecture fields for dense and MoE models, including query and KV head counts.
- Explain how hybrid attention and state-space layers change what the cache stores.
Covers
Read
In this order, only the parts listed
0 of 5 done
- Qwen2.5-0.5B architecture and configHands-On LLM Serving and Optimization - Chapters 2 and 5
Chapter 2, Decoder-Only Transformer Architecture with the Qwen2.5-0.5B config dump (pp 37-42): hidden 896, layers 24, heads 14, intermediate 4864, vocab 151936, total 494,032,768 params
- LLaMA shapes and MoE parameter walksHow to Scale Your Model - Inference
What about memory? with the LLaMA 2-13B parameter table, and Worked Problems Questions 1 and 5 (invented 64-layer model at 18.4e9 params; MoE total versus activated)
- GQA, MLA, CLA, hybrid and state-space vocabularyStanford CS336 - Inference Lecture
37:48-43:35 GQA, 43:47-45:47 MLA, 46:01-47:36 CLA, 47:50-50:26 local and hybrid attention, 52:47-1:01:10 SSMs with Mamba and AI21's 52B Mamba-transformer hybrid at 56:43 (named Jamba at 1:03:03)
Watch for vocabulary only. Skip 1:17:37-1:21:22 continuous batching and PagedAttention here; those return in later sections.
- Configuration attributes and revisionHugging Face Transformers - model configuration and revision
PreTrainedConfigcommon attributesvocab_size,hidden_size,num_attention_heads,num_hidden_layers;from_pretrainedrevisionas branch, tag or commit id with defaultmain - Chat templates and generation promptHugging Face Transformers - chat templates
apply_chat_templatewith role and content dicts, Mistral versus Zephyr control-token examples,add_generation_promptfor starting an assistant reply
Focus on
- Identity is model id plus
revisionplus tokenizer plus chat template plus precision. The savedconfig.jsonfields set shapes;revisionpins weights. - Dense cost follows total params; MoE per-token cost follows activated params. Attention KV width follows KV heads and head dim, not query heads.
- Hybrid layers mix full and local attention; state-space layers keep constant state. Neither takes the full KV formula.
- Template control tokens count as input tokens. The wrong template shifts token IDs, lengths and stop behavior.
Common mistakes · 3
- Treating the model name as identity. The same name at another
revision, tokenizer or template gives different token IDs and lengths. - Reading MoE total params as per-token cost. Only activated experts plus attention run per token; total params set weight bytes.
- Applying the MHA cache formula to GQA, MLA or state-space layers. Use KV heads, latent width or constant state as the layer defines.
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.Two runs use the same model name but different
revisionvalues. Can their token IDs be compared?AnswerNo. Weights, tokenizer or template may differ. Record repo id,
revision, tokenizer and template before comparing any count.2.A MoE model reports 212e9 total params and 31.2e9 activated. Which sets per-token dense work?Answer
Activated params. Total params set weight bytes; per-token dense work follows the activated path plus attention.
3.A layer uses 8 KV heads with head dim 128 in bf16. What is its per-token KV width factor?Answer
The width uses KV heads: values per layer before layer count and bytes. Query head count does not enter.
4.Why does the wrong chat template change serving cost even when the user text is fixed?Answer
Control tokens become input tokens. They add prefill tokens, change IDs and can change where generation stops.
Practice
- 100 Days of LLM Inference Day 02a: Verify parameter count
day02/02-a-whats-inside-a-model.ipynb, Verify: Reconstruct Parameter Count from SafeTensors shapes (expect 124,439,808 for GPT-2)Sum
wte,wpe, attention, MLP and norm shapes by hand, then compare with the saved header before trusting any config dump.
You're done when
Your identity card lists repo, revision, tokenizer, template and precision, and your hand-derived 18.4e9 count matches the book answer.
Only when you can do this without notes.
Leads to
Notes
Resources · 5 for this unit, 13 more in the section
- How to Scale Your Model - InferenceJAX Team · primary
Read the complete inference chapter for model, KV, batching, parallelism and capacity arithmetic. Reproduce every equation for one fixed model and workload, then use the public serving companion and book overview to connect the quantities to the request lifecycle.
- Hands-On LLM Serving and Optimization - Chapters 2 and 5Chi Wang and Peiheng Hu (O'Reilly) · required
Read Chapter 2 for the decoder walk-through, the KV cache and the prefill and decode phases, and the Chapter 5 sections that estimate model size and KV-cache size. Run the Chapter 2 companion notebooks on a small model and redo every size estimate for the model you will serve.
- Stanford CS336 - Inference LectureStanford University · support
Watch the inference lecture once after the arithmetic reading. Pause to reproduce its model-memory and phase-bottleneck calculations; advanced serving mechanisms are routed to their later stages.
- Hugging Face Transformers - model configuration and revisionHugging Face · reference
Read PreTrainedConfig common attributes (
vocab_size,hidden_size,num_attention_heads,num_hidden_layers) andfrom_pretrainedrevision(branch, tag or commit id, defaultmain). Use the savedconfig.jsonas the identity record beside the book config dumps. - Hugging Face Transformers - chat templatesHugging Face · reference
Read
apply_chat_templatewith role and content dicts, the Mistral versus Zephyr control-token examples, andadd_generation_promptfor starting an assistant reply. Compare the template output token count with the raw prompt count.
Also in this section
- Transformer Inference ArithmeticKipply · required
Re-derive weight bytes, KV bytes per token, prefill work, decode work and arithmetic intensity with explicit units. Replace every example constant with your model configuration and compare the estimates with the book.
- LLM Inference: From scratch, one token at a time - Modules 01, 02 and 04Tushar Amrit · support
Work the "Math by hand" and checkpoint parts of The Autoregressive Loop, The KV Cache and Metrics and the Roofline. Redo each worked number for your own model before reading the solution.
- Machine Learning Engineering Open Book - InferenceStas Bekman · required
Use inference/README.md for the prefill/decode distinction, memory formulas, MHA/GQA/MQA/MLA comparison and current glossary. Treat unfinished sections as a reference boundary, not as the main course.
- LLM Inference Handbook - Key metrics for LLM inferenceModular · reference
Use the TTFT, TPOT and ITL definitions, and the difference between request-weighted average TPOT and token-weighted average ITL, when you define your own metrics.
- 100 Days of LLM Inference - Days 1, 2, 5 and 6Elizabeth Thomas · support
Run only the four named notebooks for request anatomy, model configuration, KV sizing and roofline orientation. Verify every formula independently because this public learning journal is a useful practice supplement, not the authority.
- TensorTonic Language Modeling from Scratch - Plan KV-Cache CapacityTensorTonic · support
Solve after the KV-bytes unit, then check the answer against your own worksheet for the same configuration.
- AI Performance Engineering - Minimum Mental ModelWafer · reference
A curated, dated index. Use Start Here only to find a primary source the other entries lack.
- LLM Inference Systems Laboratory - Workload and BaselineRoadmap-Final · support
Start the immutable workload manifest, deterministic fake server, request-state trace and arithmetic planner. Keep this same repository through INF-9 so later comparisons preserve the contract.
- Etalon: Holistic Performance Evaluation Framework for LLM Inference SystemsAgrawal et al. · support
Read Sections 2.2, 3.1 and 4.1 with Algorithm 1 and Figures 1 to 3 for TTFT, TBT, TPOT, normalized latency, capacity and fluidity-index; Section 4.2 for black-box and capacity recipes; Section 5.1 for hourly runs over 24 hours and LMSys-Chat-1M medians (417 median, 1418 p90 prefill tokens).
- 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 (13B model on one A100 reaches about 1.6 requests per second at 90% attainment).
- Nano-vLLM tiny serving engineGeeeekExplorer · reference
Read
nanovllm/engine/sequence.pyfor request state,scheduler.pyfor waiting and running sets,block_manager.pyfor allocate, append, deallocate and prefix-hash reuse,model_runner.pyfor prefill and decode steps, andllm_engine.pyfor the generate path. Trace one request through these files only; do not adopt the engine for load measurement. - Inference EngineeringPhilip Kiely (Baseten Books) · support
Read Sections 1.4 to 1.4.2 for TTFT, TPS, percentiles and end-to-end versus inference-only time; Section 4.5 with 4.5.1 for benchmark dimensions, tooling and eval inputs; Section 6.1 for vision encoders, image tokens and longer-context effects.
- Gil Tene - How NOT to Measure Latency (talk)Strange Loop Conference via YouTube · reference
Use for coordinated-omission vocabulary only: closed-loop testers that wait for each reply, the missing samples under queueing, and the correction concept of recording intended send time. Do not adopt
wrk,heyorab; write the small Python generator in the load-generator unit instead.