Unit 6 of 7 · about 4 hours · derive
Counting parameters and FLOPs
Every cost claim in later sections multiplies these counts. A wrong parameter or FLOP formula silently moves every budget, schedule and scaling conclusion.
You'll be able to
- Derive embedding, attention, feed-forward and output parameter counts for MHA, MQA and GQA, with tied versus untied heads.
- Derive forward, backward and training FLOPs from shapes, and state sequence, vocabulary and batch scaling terms.
- Fit a bounded IsoFLOPs curve from offline points and predict the compute-optimal size without running the training API.
Covers
Read
In this order, only the parts listed
0 of 5 done
- Modern LLM Notebook Appendix B Section 1: parameter countingModern LLM Notebook - evaluation and FLOP accounting
Section 1 ( symbols, GQA shapes, SwiGLU with rationale, per-layer , global plus , tied versus untied, SmolLM2-135M validation with
sum(p.numel())) - Modern LLM Notebook Appendix B Sections 3 and 4: FLOPs and training ruleModern LLM Notebook - evaluation and FLOP accounting
Section 3 (matmul , attention plus , FFN as , global with unembedding) and Section 4 (backward 2 times forward, rule with SmolLM2 135M on 2T and 7B on 1.5T wall-clock)
- CS336 Lecture 2: napkin math, FLOPs and gradientsStanford CS336 - resource accounting, inference and evaluation
motivating_questions()with at MFU 0.5 and the bound,tensor_operations_flops()with and MFU,gradients_flops()with forward , backward and total - Raschka FLOPS analysis: thop benchmarks and MFULLMs from Scratch companion - GPT FLOP analysis
Fixed-batch forward with thop profile and MACS times 2 on gpt-small to gpt-xl (vocab 50257, context 1024), plus the MFU section with observed over theoretical tokens per second and forward plus backward as 3 times forward
- CS336 Assignment 3: IsoFLOPs method and offline curvesStanford CS336 Assignment 3 - IsoFLOPs scaling
Section 2.1 Scaling Laws from IsoFLOPs profiles with about , quadratic loss versus size per budget, optima per profile then power laws and ; Problem
chinchilla_isoflopswithdata/isoflops_curves.jsonfieldsparameters,compute_budgetandfinal_loss, fitted offline without the training API
Focus on
- Count each projection separately under GQA: stays by times , while and shrink to by times . MHA is the special case equals .
- One matmul by costs . Sum , , , , and plus gate, up and down plus unembedding , and do not drop the term at large .
- Backward costs about twice forward, so training is about 6 times params times tokens for short contexts. Long adds the term.
- IsoFLOPs fixes and varies , finds the loss minimum per budget, then fits and power laws. Fit the offline points, and do not burn the hosted budget.
Common mistakes · 3
- Counting MACs as FLOPs. One multiply-accumulate is two FLOPs, so double the thop MACS number.
- Comparing runs by steps at different batch sizes. Convert to tokens first: batch times steps times length.
- Fitting IsoFLOPs on the hosted API when the exercise gives offline curves. The bounded task uses
isoflops_curves.jsononly.
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.What is the largest AdamW model on 8 H100s at 12 bytes per param?Answer
. It is an upper bound because activations are not counted.
2.What does plus give for LLaMA-2 70B?Answer
, about 64.9B. The real model has about 69B: GQA shrinks and , but its SwiGLU width of 28672 is well above , which more than makes up for it. The shortcut assumes MHA and a -equivalent FFN.
3.Why is the training rule ?Answer
Forward is and backward is (one matmul for plus one for per op), so training is . Raschka measures forward with thop then triples it for forward plus backward.
4.What are the IsoFLOPs fitting steps?Answer
Group runs by , take the min-loss per budget (or a quadratic fit), fit and power laws in , then extrapolate to the target budget such as 1e23 or 1e24 in exercise units.
Practice
- Modern LLM Notebook Appendix B Exercise 1: LLaMA-2 70B parameters and FP16 memory
Exercise 1 with 8192 80 64 8 32000; compute as plus and FP16 bytes as ; the cells assert within 1 percent
- CS336 Assignment 3:
chinchilla_isoflops(bounded, offline)Problem (
chinchilla_isoflops) in Section 2.1; fit power laws fromdata/isoflops_curves.jsonoffline (fieldsparameters,compute_budget,final_loss) without the training API
You're done when
You derive the 64.9B estimate and the 6.3e24 total from shapes alone, and your IsoFLOPs script predicts optimal sizes from the offline JSON.
Only when you can do this without notes.
Leads to
Notes
Resources · 4 for this unit, 2 more in the section
- Stanford CS336 - resource accounting, inference and evaluationStanford University · primary
Use Spring 2026 Lectures 2, 10 and 12 for FLOPs, memory, arithmetic intensity, inference and evaluation. Reproduce the small calculations locally. Assignment 3 IsoFLOPs analysis is optional; do not require its hosted training API or costly experiments.
- LLMs from Scratch companion - GPT FLOP analysisSebastian Raschka · required
Use ch04/02_performance-analysis/flops-analysis.ipynb as a concrete calculator, then rebuild the formulas from the model configuration and reconcile parameters and at least one memory or operation estimate with runtime state.
- Modern LLM Notebook - evaluation and FLOP accountingWalking Labs · required
Use English notebook 25-evaluation and Appendix B FLOP and memory accounting. Build a 20-example evaluator with two slices, raw outputs and bootstrap intervals, and deliberately create one parser, prompt or judge failure.
- Stanford CS336 Assignment 3 - IsoFLOPs scalingStanford University · optional
Use Section 2.1 and Problem
chinchilla_isoflopswithdata/isoflops_curves.jsonfor the bounded curve-fitting exercise only. Do not use the hosted training API or costly experiments.
Also in this section
- LLM Foundations Checkpoint - evaluation and arithmetic sliceRoadmap-Final · support
Use the contamination, evaluator-bias, parameter, FLOP, activation, state and KV requirements as the assessment contract. Keep raw outputs and distinguish a verified task result from a broad model capability claim.
- Language Model Evaluation HarnessEleutherAI · optional
After the small evaluator passes, inspect task versioning, request construction and result schemas to understand production evaluation plumbing. Do not use the harness as a substitute for owning the task and metric contract.