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

Embedding, attention, feed-forward and output parameter countsForward, backward and training FLOP approximationsSequence length, vocabulary and batch scaling termsBounded IsoFLOPs curve-fitting exercise
1

Read

In this order, only the parts listed

0 of 5 done

  1. 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()))

  2. 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)

  3. 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

  4. 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

  5. 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_isoflops with data/isoflops_curves.json fields parameters, compute_budget and final_loss, fitted offline without the training API

2

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.json only.
3

Try it yourself

Closed-book, before checking anything

Derive the LLaMA-2 70B count from 8192 80 32000 as plus equals 64.9B, then the 70B 15T training total 6.3e24 FLOPs and the 144-day estimate on 1024 H100s at MFU 0.5.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 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. 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. 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. 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.

5

Practice

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_isoflops with data/isoflops_curves.json for 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.