New Material: Parameter, Memory, and Compute Arithmetic
Agent-Assisted Engineering Check
Prerequisites
Day 47 block accounting, Day 48 weight tying, Day 50 AdamW, and Day 55 checkpoints.
Learning Objective
Count GPT parameters exactly, convert them to inference and training memory, and estimate training FLOPs, hours, and cost with the 6ND rule.
Instructions
This is the only new topic today; the rest of the day reviews Week 8. Days 58, 59, 64 and 65 reuse these formulas.
1. Parameters (d = n_embd, V = vocab, L = context): embeddings V*d + L*d; per block 12*d^2 (attention 4*d^2, MLP 8*d^2) + 4*d (two LayerNorms); final LayerNorm 2*d; a tied head adds nothing. This formula leaves out linear-layer biases: GPT-2 has 9*d of them per block (QKV 3d, attention output d, MLP 4d + d). Check your count with PyTorch's sum(p.numel()); the same formula gives GPT-2 Medium (350M: d = 1,024, 24 blocks) 354,601,984, or 354,823,168 with biases.
2. Weights-only memory: bytes per parameter is 4 (FP32), 2 (FP16/BF16), 1 (INT8), 0.5 (INT4).
3. Mixed-precision AdamW training holds 16 bytes per parameter (ZeRO paper): FP16 weights 2 + FP16 gradients 2 + FP32 master weights, first moment and second moment 4 + 4 + 4. Activations come on top and grow with batch size and context; add about 1 GB for the CUDA context.
4. Compute: C = 6 * N * D FLOPs (2ND forward, 4ND backward). Hours = C / (num_gpus * peak FLOP/s * MFU * 3600), with MFU usually 30-50%.
Practice & Prediction Log
1. GPT-2 Small: V = 50,257, L = 1,024, n_layer = 12, n_embd = 768, tied head. Count the parameters with the formula above, and again with GPT-2's linear biases. How many bytes are the weights in FP16?
2. A 1B-parameter model trains with mixed-precision AdamW and needs 8 GB of activations at your batch size. Does it fit on a 24 GB GPU?
3. Train a 125M model on 10B tokens on one A100 (312 TFLOP/s BF16 peak) at 40% MFU and $1.50/hour. Give FLOPs, hours, and cost.
Per block: 12 * 768^2 + 4 * 768. Tied embeddings are counted once. Static training memory is N * 16 bytes. Effective throughput is 312e12 * 0.40 FLOP/s.
1. Per block = 12 * 768^2 + 4 * 768 = 7,080,960; 12 blocks = 84,971,520. Embeddings = 50,257 * 768 + 1,024 * 768 = 39,383,808. Final LayerNorm = 2 * 768 = 1536. Total N = 124,356,864. Adding the linear biases (12 * 9 * 768 = 82,944) gives 124,439,808, the count PyTorch reports for GPT-2 with bias=True. FP16 weights for N = 124,356,864: 248,713,728 bytes = 237.2 MiB (248.7 MB).
2. Static = 1e9 * 16 bytes = 16 GB. Total = 16 + 8 (activations) + 1 (CUDA context) = 25 GB > 24 GB, so it runs out of memory unless you use gradient checkpointing, a smaller micro-batch with gradient accumulation, or ZeRO offload.
3. C = 6 * N * D = 6 * 125e6 * 10e9 = 7.5e18 FLOPs. Effective throughput = 1.248e14 FLOP/s. Time = 60,096 s = 16.69 hours. Cost = 16.69 * $1.50 = $25.04.
Learner reproduces the exact parameter count with and without linear biases, applies the 16 bytes/parameter rule to predict out-of-memory, and turns 6ND plus MFU into hours and cost.
Authoritative Source Resources & Provenance
Scaling Laws for Neural Language Models
Relevant Section / Range: Section 2.1 Parameter and Compute Scaling of Transformers (C ≈ 6N FLOPs per token)
Verified Version & Date: arXiv:2001.08361v1 (Verified: 2026-10-30)
Foundational empirical study formulating power laws governing cross-entropy loss with respect to model parameters N, dataset size D, and compute C.
Basic calculus, logarithms, and parameter counting fundamentals.
Scaling formulas, power-law exponents, and parameter compute trade-offs are derived in the lesson text.
Compared Candidate Sources & Selection Analysis (2)
Early formulation of power-law scaling; Kaplan focuses explicitly on autoregressive transformer language models.
General vision/translation scaling; Kaplan specifically establishes modern decoder-only LLM compute budgeting.
Transformer Math 101
Relevant Section / Range: Compute Requirements (C = 6PD) and Memory Requirements: Training
Verified Version & Date: EleutherAI Research Blog (Verified: 2026-10-30)
Widely cited, practical industry guide deriving 6ND compute FLOPs, activation memory, KV cache size, and VRAM estimation.
Transformer matrix dimensions and matrix multiplication FLOP counting.
The 6ND derivation (2ND forward, 4ND backward) and peak VRAM estimation models are fully worked out in the curriculum.
Compared Candidate Sources & Selection Analysis (2)
Rigorous academic formulation; Transformer Math 101 provides accessible step-by-step arithmetic equations.
Original 6ND approximation; EleutherAI expands on activation memory and KV cache.
ZeRO: Memory Optimizations Toward Training Trillion Parameter Models
Relevant Section / Range: Section 3.1 Model States: Optimizer States, Gradients and Parameters (2Ψ + 2Ψ + KΨ = 16Ψ bytes, K = 12)
Verified Version & Date: SC20 / arXiv:1910.02054v3 (Verified: 2026-10-30)
Source of the 16 bytes per parameter accounting for mixed-precision Adam that the lesson's memory arithmetic uses.
Adam optimizer mechanics from Day 34 and parameter memory arithmetic.
Complete analytical formulas for bytes-per-parameter across AdamW vs SGD and mixed precision are fully provided.
Compared Candidate Sources & Selection Analysis (2)
Focuses on tensor slicing and inter-GPU communication; ZeRO provides the standard breakdown of optimizer and parameter memory footprints.
Foundational mixed-precision paper; ZeRO formalizes the exact 16-byte Adam state footprint.