Workbook — JAX Scaling Book Companion

Scaling Book Workbook

Practice problems in the spirit of the JAX Scaling Book (How to Scale Your Model). Work them with pencil and paper, then check them here.

Units: 1 GB = 109 bytes and 1 MB = 106 bytes everywhere, the same as vendor bandwidth figures. If a vendor lists capacity in GiB (230 bytes), we convert it.

Prerequisites: Basic linear algebra + Python familiarity. That's it.
12
Chapters
51
Exercises
0
Hand-Waving

Chapter 0: Roofline Exercises

You are an ML engineer staring at a GPU profiler trace. A matrix multiply that should take 0.14 ms is taking 0.8 ms. Is the bottleneck memory bandwidth? Compute throughput? Launch overhead? Without a mental model, you are guessing. The roofline model gives you that mental model.

Before you can optimize anything, you need to know what limits performance. Is your operation waiting for data to arrive from memory, or waiting for the arithmetic units to finish crunching? The roofline model answers this question with a single number: arithmetic intensity.

Arithmetic intensity (AI) is the ratio of floating-point operations to bytes moved from memory. If you do many FLOPs per byte loaded, you are compute-bound. If you do few FLOPs per byte, you are memory-bound. The dividing line is the hardware's ridge point — the arithmetic intensity where the processor's compute throughput exactly matches its memory bandwidth.

The roofline in one sentence. If your operation's arithmetic intensity is below the ridge point, adding more compute won't help — you're starved for data. If it's above, adding more memory bandwidth won't help — the ALUs are the bottleneck.

Key Formulas

Arithmetic Intensity = FLOPs / Bytes Transferred
Ridge Point = Peak FLOP/s / Peak Bandwidth (bytes/s)
Attainable FLOP/s = min(Peak FLOP/s, AI × Peak Bandwidth)

For a matrix multiplication C = A × B where A is [M, K] and B is [K, N]:

FLOPs = 2 × M × K × N // multiply + add per output element
Bytes = (M×K + K×N + M×N) × bytes_per_element
AI = 2MKN / ((MK + KN + MN) × bytes_per_element)
Exercise 0.1: Matmul Arithmetic Intensity

Compute the arithmetic intensity for a square matmul: [4096, 4096] × [4096, 4096] in BF16 (2 bytes per element).

Hint: FLOPs = 2 × 40963. Bytes = 3 × 40962 × 2.

FLOPs/byte
Show derivation
FLOPs = 2 × 4096 × 4096 × 4096 = 137,438,953,472 ≈ 1.37 × 1011
Bytes = (40962 + 40962 + 40962) × 2 = 3 × 16,777,216 × 2 = 100,663,296
AI = 137,438,953,472 / 100,663,296 ≈ 1365.3 FLOPs/byte

For square matmuls, AI simplifies to 2N/6 = N/3 when element size = 2 bytes. So 4096/3 ≈ 1365. This is deeply compute-bound on any modern accelerator.

Exercise 0.2: H100 Ridge Point

The H100 SXM has ~990 TFLOP/s BF16 Tensor Core throughput and ~3.35 TB/s HBM bandwidth. What is the ridge point?

FLOPs/byte
Show derivation
Ridge Point = 990 × 1012 / (3.35 × 1012) ≈ 295.5 FLOPs/byte

Any operation with AI above ~296 is compute-bound on the H100. Our matmul from Exercise 0.1 (AI ≈ 1365) is well above this — solidly compute-bound. Element-wise ops like ReLU (AI = 0.125 in FP32, see Exercise 0.5) are deeply memory-bound.

Exercise 0.3: Matmul Throughput Prediction

Given the [4096, 4096] × [4096, 4096] BF16 matmul is compute-bound on the H100, predict the theoretical execution time assuming 990 TFLOP/s peak.

ms
Show derivation
Time = FLOPs / Peak FLOP/s = 1.374 × 1011 / (990 × 1012) = 0.0001388 s ≈ 0.139 ms

In practice, you will see ~0.15-0.2 ms due to kernel launch overhead and imperfect occupancy. The ratio of achieved to theoretical is your MFU (Model FLOPs Utilization). Getting above 50% MFU on real workloads is considered good.

Exercise 0.4: Is Self-Attention Memory-Bound?

Consider the QKT computation in self-attention: Q and K are both [batch, heads, seq, d_head]. With batch=1, heads=32, seq=2048, d_head=128, BF16. What is the arithmetic intensity of the QKT matmul (treating each head independently)?

Hint: Per head, this is a [2048, 128] × [128, 2048] matmul. Compute AI for one head.

FLOPs/byte
Show derivation

Per head: M=2048, K=128, N=2048, element_size=2 bytes.

FLOPs = 2 × 2048 × 128 × 2048 = 1,073,741,824
Bytes = (2048×128 + 128×2048 + 2048×2048) × 2
= (262,144 + 262,144 + 4,194,304) × 2 = 9,437,184 bytes
AI = 1,073,741,824 / 9,437,184 ≈ 113.8 FLOPs/byte

113.8 is below the H100 ridge point (~296), so even this one matmul is memory-bound. The output S = QKT [2048, 2048] is most of the bytes. Standard attention then reads S again for the softmax and the AV matmul, so the AI drops further. FlashAttention keeps S on chip: it computes attention in tiles and fuses QKT, softmax and AV into one kernel.

Exercise 0.5: Element-wise Op Arithmetic Intensity

ReLU activation on a tensor of 10M elements in FP32 (4 bytes). ReLU does one comparison per element (1 FLOP). What is the arithmetic intensity?

FLOPs/byte
Show derivation
FLOPs = 10,000,000 (one comparison per element)
Bytes = 10,000,000 × 4 (read) + 10,000,000 × 4 (write) = 80,000,000
AI = 10,000,000 / 80,000,000 = 0.125 FLOPs/byte

This is 2,364× below the H100's ridge point of 296. ReLU is absurdly memory-bound. This is why optimized code usually fuses activation functions into the matmul before or after them.

Exercise 0.6: Tall-and-Skinny Matmul

During autoregressive decode, the main operation per layer is a matrix-vector product: [1, d] × [d, d] where d=8192. BF16. What is the arithmetic intensity? Is it compute-bound or memory-bound on H100?

FLOPs/byte
Show derivation
FLOPs = 2 × 1 × 8192 × 8192 = 134,217,728
Bytes = (1 × 8192 + 8192 × 8192 + 1 × 8192) × 2
= (8192 + 67,108,864 + 8192) × 2 ≈ 134,250,496
AI = 134,217,728 / 134,250,496 ≈ 1.0 FLOPs/byte

AI ≈ 1 — deeply memory-bound (ridge point is 296). The weight matrix dominates the byte count. This is why decode is memory-bandwidth-limited: each token requires reading the entire weight matrix for a trivial number of FLOPs. Batching helps by amortizing the weight read across B tokens, raising AI to ~B.

The roofline lesson. Large square matmuls are always compute-bound. Attention (without FlashAttention) is memory-bound because the intermediate S matrix forces extra HBM traffic. Element-wise ops are always memory-bound. Decode (matrix-vector) is memory-bound until batch size reaches the ridge point. The roofline tells you where to focus your optimization effort.
An operation has arithmetic intensity of 50 FLOPs/byte running on an H100 (ridge point ~296). Which statement is true?

Chapter 1: TPU Architecture

Google's Tensor Processing Units (TPUs) are custom ASICs designed for one thing: dense matrix multiplication at massive scale. Unlike GPUs, which evolved from graphics workloads and carry a lot of general-purpose flexibility, TPUs are laser-focused on the matmul-softmax-matmul pipeline that dominates transformer training and inference.

The heart of a TPU is the MXU (Matrix Multiply Unit): a 128×128 systolic array of multiply-accumulate (MAC) cells. Each cycle, one MXU does 128×128 = 16,384 MACs, and each MAC is 2 FLOPs. A TPU v5e chip has one TensorCore with 4 MXUs (Scaling Book, TPU chapter).

Systolic array intuition. Picture a 128×128 grid of tiny calculators. Data flows in from the left (A matrix rows) and from the top (B matrix columns). Each calculator multiplies its two inputs, adds to its running sum, and passes data to its neighbor. After the array fills, results pour out from the bottom. No random access, no caches — just a river of numbers.
Exercise 1.1: The Clock Behind the Peak

A TPU v5e chip has 4 MXUs, each 128×128, and a peak of 197 TFLOP/s in BF16. What clock speed does that peak imply?

Hint: each MXU does 128 × 128 MACs per cycle. Each MAC = 2 FLOPs (multiply + add).

GHz
Show derivation
FLOPs/cycle per MXU = 128 × 128 × 2 = 32,768
FLOPs/cycle per chip = 4 × 32,768 = 131,072
Clock = 197 × 1012 / 131,072 ≈ 1.50 × 109 = 1.5 GHz

So one MXU gives about 49 TFLOP/s. An H100 gives ~990 TFLOP/s dense BF16 from 132 SMs. A v5e chip is much smaller, and a pod holds 256 of them.

Exercise 1.2: TPU v5e Ridge Point

TPU v5e specs: 197 TFLOP/s peak BF16 (4 MXUs) and 819 GB/s HBM bandwidth. What is the ridge point?

FLOPs/byte
Show derivation
Ridge = 197 × 1012 / (819 × 109) ≈ 240.5 FLOPs/byte

The TPU v5e has a lower ridge point than the H100 (~296), meaning operations become compute-bound at lower arithmetic intensity. This is because the TPU's ratio of compute-to-bandwidth is more balanced — relatively more bandwidth per FLOP.

Exercise 1.3: Memory Bandwidth Utilization

You run a layer normalization on a tensor of shape [2048, 8192] in BF16 on a TPU v5e (819 GB/s bandwidth). LayerNorm reads the input once, writes the output once, plus reads/writes for mean and variance (negligible for large tensors). Approximate the minimum execution time.

Hint: LayerNorm is memory-bound. Total bytes = one read + one write = 2 × tensor bytes.

ms
Show derivation
Tensor bytes = 2048 × 8192 × 2 = 33,554,432 bytes = 33.55 MB
Total transfer = 2 × 33.55 MB = 67.1 MB (read + write)
Time = 67.1 × 106 / (819 × 109) = 0.0000819 s ≈ 0.082 ms

Real runs take longer: there is launch overhead, and LayerNorm also computes a reduction (mean, variance). The memory transfer still dominates. Fusing LayerNorm into the operation before or after it removes one full round-trip to HBM.

Exercise 1.4: TPU v5e vs H100 Comparison

Fill in the comparison table from memory, then check your answers.

SpecTPU v5eH100 SXM
BF16 peak TFLOP/s~197~990
HBM bandwidth~819 GB/s~3,350 GB/s
HBM capacity16 GiB (17.2 GB)80 GB
Ridge point~240~296
Interconnect (per chip, both directions)ICI (400 GB/s)NVLink (900 GB/s)

Sources: Google Cloud TPU v5e page; the Scaling Book GPU chapter for the H100.

How many BF16 parameters fit in one TPU v5e's HBM (weights only)?

B parameters
Show derivation
HBM = 16 GiB = 16 × 230 bytes = 17.18 × 109 bytes
Max params in BF16 = 17.18 × 109 / 2 bytes ≈ 8.59 × 109

That is weights only: no KV cache, no activations. A 7B model leaves little room, so you shard it over several chips. A 256-chip v5e pod has 256 × 17.18 GB ≈ 4.4 TB of HBM.

TPU vs GPU mental model. GPUs: many small cores, massive parallelism, flexible. TPUs: fewer but larger systolic arrays, less flexible, cheaper per FLOP for matmul-heavy workloads. The ridge point comparison (TPU v5e ~240 vs H100 ~296) tells you TPUs have relatively better bandwidth per compute unit, making them friendlier to slightly memory-bound workloads.
A TPU's MXU is a 128x128 systolic array. If you need to multiply a [64, 128] matrix by a [128, 64] matrix, what happens to the utilization of the MXU?

Chapter 2: Sharded Matrix Multiply

A single accelerator can only hold so much data in HBM. A 70B parameter model in BF16 needs 140 GB just for weights — far more than an 80 GB H100 holds. The solution: shard the matrices across multiple devices and coordinate the computation.

There are three fundamental ways to shard a matmul C = A × B where A is [M, K] and B is [K, N]:

StrategyShard A alongShard B alongCommunication needed
M-sharded (split the batch)M (rows)ReplicatedNone (outputs are independent)
N-sharded (split output columns)ReplicatedN (columns)None (outputs are independent)
K-sharded (split the inner dim)K (inner dim)K (inner dim)All-reduce on partial sums
The tradeoff. M-sharding and N-sharding need no communication, but each replicates one full matrix. K-sharding splits both matrices, but it needs an all-reduce to sum the partial products. Megatron-LM combines them in the MLP: the first linear is N-sharded (Megatron calls it "column-parallel"), and the second is K-sharded ("row-parallel"). Only the second needs an all-reduce.
Exercise 2.1: Sharding Strategy

You have a weight matrix W of shape [8192, 32768] and input X of shape [2048, 8192]. You want to compute Y = X × W across 8 devices with no communication. Which sharding does that and uses the least memory per device?

Show reasoning

N-sharding: replicate X (2048×8192×2 = 33.6 MB per device), shard W into 8 chunks of [8192, 4096] (67.1 MB each). Each device computes Y_chunk = X × W_chunk with no communication. Y comes out split by columns.

M-sharding: shard X into rows (each device gets [256, 8192]), replicate W (8192×32768×2 = 537 MB per device). Also no communication, but it wastes memory on copies of the big weight matrix.

K-sharding: needs an all-reduce, so it fails the no-communication rule.

N-sharding wins: zero communication, and the smaller matrix (X) is the one replicated.

Exercise 2.2: All-Reduce Volume

You shard a [8192, 8192] × [8192, 8192] BF16 matmul along K across 8 devices. Each device computes a partial [8192, 8192] result that must be summed via all-reduce. Using the ring all-reduce algorithm, how many bytes does each device send in total?

Hint: Ring all-reduce sends 2 × (P-1)/P × tensor_size bytes per device, where P = number of devices.

MB
Show derivation
Tensor size = 8192 × 8192 × 2 bytes = 134,217,728 bytes = 134.2 MB
All-reduce volume per device = 2 × (8-1)/8 × 134.2 MB
= 2 × 0.875 × 134.2 = 234.9 MB

The ring all-reduce has two phases: reduce-scatter (each device sends (P-1)/P of the data) and all-gather (the same volume again). As P grows, the total approaches 2× the tensor size. No all-reduce can send less per device, so the ring is bandwidth-optimal.

Exercise 2.3: Communication-to-Computation Ratio

Using the same K-sharded matmul from 2.2 on 8 devices, each device computes a [8192, 1024] × [1024, 8192] matmul. Each device sends 234.9 MB in the all-reduce. Each device's matmul takes 0.139 ms (compute-bound at 990 TFLOP/s). Each device sends at 450 GB/s (NVLink, one direction). What is the communication time? Is the operation communication-bound?

ms (comm time)
Show derivation
Compute time = 2 × 8192 × 1024 × 8192 / (990 × 1012) = 1.374 × 1011 / (990 × 1012) ≈ 0.139 ms
Comm time = 234.9 × 106 / (450 × 109) ≈ 0.522 ms
Ratio = 0.522 / 0.139 ≈ 3.8×

Yes: communication takes 3.8× longer than compute. Each device does only 1/8 of the FLOPs but still all-reduces the full [8192, 8192] output. M-sharding or N-sharding avoids this for a single matmul, at the cost of replicating one matrix.

Exercise 2.4: FSDP All-Gather vs TP All-Reduce

Consider one transformer layer of LLaMA 3 70B. The layer has ~856M parameters in BF16 = 1.71 GB of weights. Compare two strategies on 8 devices:

(A) FSDP: all-gather the full layer weights before forward. Volume = (P-1)/P × layer_size per device.
(B) TP: two all-reduces per layer (attention + MLP) on output tensors of shape [batch×seq, d]. With batch=4, seq=4096, d=8192, BF16.

Which transfers fewer bytes per device?

Show derivation

FSDP all-gather:

Volume = (8-1)/8 × 1.71 GB = 0.875 × 1.71 = 1.50 GB per device

TP all-reduce (per operation):

Tensor size = 4 × 4096 × 8192 × 2 = 268 MB
Per all-reduce = 2 × (8-1)/8 × 268 MB = 470 MB
Two all-reduces = 2 × 470 = 940 MB = 0.94 GB

FSDP transfers 1.50 GB, TP transfers 0.94 GB, so TP wins on volume here. But TP's all-reduces sit on the critical path of every layer, so TP needs fast NVLink inside a node. FSDP's all-gather can be prefetched one layer ahead, so it tolerates slower links between nodes. That is why TP usually stays inside a node and FSDP spans nodes.

In a standard Transformer MLP (two linear layers: up-projection then down-projection), Megatron-LM uses column-parallel for the first linear and row-parallel for the second. How many all-reduce operations are needed per MLP block?

Chapter 3: Transformer Parameter Counting

Before you can estimate training cost, inference memory, or sharding strategy, you need to count parameters precisely. No approximations, no hand-waving. Every weight matrix, every bias, every embedding. This is the foundation of all scaling analysis.

The Standard Transformer Block

One transformer block contains a self-attention layer and an MLP (feed-forward) layer, each with layer norms. Here is the parameter breakdown:

Self-Attention:
Q projection: d × d // = d × (n_heads × d_head)
K projection: d × d_kv // d_kv = n_kv_heads × d_head for GQA
V projection: d × d_kv
O projection: d × d

MLP (SwiGLU):
Gate projection: d × d_ff
Up projection: d × d_ff
Down projection: d_ff × d

Layer Norms:
2 × d // weight only, no bias in modern transformers

Embeddings:
Token embedding: vocab × d
(Positional: usually RoPE = 0 params)
Exercise 3.1: GPT-2 Parameter Count

GPT-2 (small): 12 layers, d=768, 12 heads, d_head=64, d_ff=3072 (4×d), vocab=50257. Standard MHA (not GQA), standard MLP (not SwiGLU, so 2 projections not 3), with biases on all linear layers, and separate LN weight+bias.

Count total parameters. Include: 4 attention projections + biases, 2 MLP projections + biases, 2 LayerNorms (weight+bias each), token embedding, position embedding (1024×768), and final LN.

parameters
Show derivation

Per transformer block:

Attention: 4 × (768 × 768 + 768) = 4 × 590,592 = 2,362,368
MLP: (768 × 3072 + 3072) + (3072 × 768 + 768) = 2,362,368 + 2,360,064 = 4,722,432
LayerNorms: 2 × (768 + 768) = 3,072
Total per block = 2,362,368 + 4,722,432 + 3,072 = 7,087,872

Global:

12 blocks = 12 × 7,087,872 = 85,054,464
Token embedding = 50,257 × 768 = 38,597,376
Position embedding = 1,024 × 768 = 786,432
Final LN = 768 + 768 = 1,536
Total = 85,054,464 + 38,597,376 + 786,432 + 1,536 = 124,439,808 ≈ 124M

The released GPT-2 small checkpoint has exactly this count (the GPT-2 paper listed it as 117M). GPT-2 ties the output head to the token embedding, so the head adds no parameters.

Exercise 3.2: LLaMA 3 70B Parameter Count

LLaMA 3 70B: 80 layers, d=8192, 64 attention heads, 8 KV heads (GQA), d_head=128, d_ff=28672 (SwiGLU, so 3 projections), vocab=128256. No biases anywhere. RoPE (0 params). RMSNorm (weight only, no bias).

Count total parameters in billions.

B params
Show derivation

Per block attention:

Q: 8192 × 8192 = 67,108,864 (64 heads × 128)
K: 8192 × 1024 = 8,388,608 (8 KV heads × 128)
V: 8192 × 1024 = 8,388,608
O: 8192 × 8192 = 67,108,864
Attention total = 150,994,944

Per block MLP (SwiGLU):

Gate: 8192 × 28672 = 234,881,024
Up: 8192 × 28672 = 234,881,024
Down: 28672 × 8192 = 234,881,024
MLP total = 704,643,072

Per block norms:

2 × 8192 = 16,384 (RMSNorm, weight only)

Total per block:

150,994,944 + 704,643,072 + 16,384 = 855,654,400

Global:

80 blocks = 80 × 855,654,400 = 68,452,352,000
Token embedding = 128,256 × 8,192 = 1,050,673,152
Output head = 8,192 × 128,256 = 1,050,673,152 (untied)
Final RMSNorm = 8,192
Total = 68,452,352,000 + 1,050,673,152 + 1,050,673,152 + 8,192
= 70,553,706,496 ≈ 70.6B
Exercise 3.3: Forward Pass FLOPs Per Token

The standard approximation for forward pass FLOPs per token is 2N (where N = number of parameters), because each parameter participates in one multiply-add (2 FLOPs). For LLaMA 3 70B (N ≈ 70.6B), how many FLOPs per token in the forward pass?

×1011 FLOPs
Show derivation
Forward FLOPs/token ≈ 2N = 2 × 70.6 × 109 = 1.412 × 1011

This counts the weight matmuls only. The attention scores (QKT) and the weighted sum over V add about 2 × n_layers × seq_len × d FLOPs per token (Kaplan et al., 2020). At seq = 8192 that is 2 × 80 × 8192 × 8192 ≈ 1.07 × 1010, about 8% of 2N. For back-of-envelope work, 2N is enough.

Exercise 3.4: Training FLOPs (6ND)

The standard training FLOP estimate is 6ND: forward (2N) + backward (4N, because gradients require ~2× the forward FLOPs), times D tokens. LLaMA 3 70B was trained on 15T tokens. How many total FLOPs?

×1024 FLOPs
Show derivation
Total FLOPs = 6 × N × D = 6 × 70.6 × 109 × 15 × 1012
= 6 × 70.6 × 15 × 1021 = 6,354 × 1021 = 6.354 × 1024

About 6.35 × 1024 FLOPs. Meta's model card reports 6.4M H100-hours for Llama 3 70B; you will compare against that in Exercise 5.1.

Exercise 3.5: KV Cache Sizing

LLaMA 3 70B: 80 layers, GQA with 8 KV heads, d_head=128, batch=32, seq=8192, BF16. How many GB for the KV cache?

Formula: 2 (K and V) × n_layers × n_kv_heads × d_head × seq_len × batch × bytes_per_element

GB
Show derivation
KV cache = 2 × 80 × 8 × 128 × 8192 × 32 × 2 bytes
= 2 × 80 × 8 × 128 × 8192 × 32 × 2
= 2 × 80 × 8 × 128 × 8192 × 64
= 2 × 80 × 8 × 128 × 524,288
= 2 × 80 × 536,870,912 = 85,899,345,920 bytes
= 85.9 GB (2.68 GB per sequence)

GQA helps a lot here. With 64 KV heads instead of 8, the cache would be 687 GB, 8× larger. The weights are 141 GB in BF16, so at this batch and length the cache is already 60% the size of the model.

Exercise 3.6: MFU Calculation

You are training LLaMA 3 70B on 2048 H100s. You measure 1000 tokens/second/GPU. Each H100 has 990 TFLOP/s BF16 peak. What is your Model FLOPs Utilization (MFU)?

MFU = (actual model FLOPs/s per GPU) / (peak FLOPs/s per GPU). Model FLOPs per token = 6N (forward + backward).

%
Show derivation
Model FLOPs per token = 6N = 6 × 70.6 × 109 = 4.236 × 1011
Throughput per GPU = 1000 tokens/s
FLOPs/s per GPU = 1000 × 4.236 × 1011 = 4.236 × 1014 = 423.6 TFLOP/s
MFU = 423.6 / 990 = 0.428 = 42.8%

Training MFU always uses 6N per token: the hardware does the backward pass too. Sanity check: an answer above 100% means a wrong FLOP count. The Llama 3 paper reports 38–43% BF16 MFU for its 405B runs. The gap to 100% comes from communication, pipeline bubbles and memory-bound ops (norms, softmax).

GQA (Grouped-Query Attention) reduces the number of KV heads from n_heads to n_kv_heads. Which component does this NOT reduce?

Chapter 4: Training Parallelism

You cannot train a 70B model on one GPU. You need parallelism — splitting the work across many devices. There are four main flavors, and real training runs combine all of them.

StrategyWhat's splitCommunicationMemory saving
Data Parallel (DP)BatchAll-reduce gradientsNone (each GPU has full model)
FSDP (ZeRO-3)Batch + params/grads/optimizerAll-gather params, reduce-scatter grads~P× (P = # GPUs)
Tensor Parallel (TP)Individual weight matricesAll-reduce per layer~TP_degree×
Pipeline Parallel (PP)Layers across stagesPoint-to-point activations~PP_degree×
The memory equation. Per-GPU memory during training: model_params + gradients + optimizer_states + activations. With Adam in BF16 mixed precision: params (2 bytes) + grads (2 bytes) + optimizer (master weights 4B + momentum 4B + variance 4B = 12 bytes) = 16 bytes per parameter, plus activation memory. FSDP shards all of these across GPUs.
Exercise 4.1: FSDP Memory Per GPU

70B model, Adam optimizer, BF16 training. Without FSDP, per-GPU memory for params+grads+optimizer = 16 bytes × N. With FSDP across 64 GPUs, how much memory per GPU for these components?

GB
Show derivation
Without FSDP: 16 × 70 × 109 = 1,120 GB (impossible on one GPU!)
With FSDP on 64 GPUs: 1,120 / 64 = 17.5 GB per GPU

This is just model state. Activation memory is additional and depends on sequence length, batch size, and whether you use activation checkpointing. An H100 has 80 GB HBM, so 17.5 GB for model state leaves ~62 GB for activations — comfortable.

Exercise 4.2: Tensor Parallelism Communication

In tensor-parallel attention with TP=8, each device computes 8 of 64 attention heads for LLaMA 3 70B. The output projection produces a partial result that requires an all-reduce. The output tensor per token has shape [1, 8192] in BF16. For a microbatch of 4 sequences, each of length 8192, how many bytes are transferred in the all-reduce?

Hint: all-reduce on the output of shape [batch × seq, d]. Use the ring formula: 2 × (P-1)/P × tensor_bytes.

MB per device
Show derivation
Output tensor shape = [4 × 8192, 8192] = [32768, 8192] in BF16
Tensor bytes = 32768 × 8192 × 2 = 536,870,912 bytes = 536.9 MB
All-reduce per device = 2 × (8-1)/8 × 536.9 MB = 2 × 0.875 × 536.9 = 939.5 MB

The MLP output needs a second all-reduce of the same size, so a full block sends about 1.88 GB per device in the forward pass. At 450 GB/s per direction that is about 4.2 ms per block, which is why TP stays inside a node on NVLink.

Exercise 4.3: Pipeline Bubble Fraction

Pipeline parallelism with PP=4 stages. If you use m microbatches, the bubble fraction (wasted time) is (PP-1)/(m+PP-1). What is the smallest m that keeps the bubble fraction strictly below 5%?

microbatches (minimum)
Show derivation
Bubble fraction = (PP-1) / (m + PP - 1) < 0.05
3 / (m + 3) < 0.05
3 < 0.05 × (m + 3)
60 < m + 3
m > 57, so the smallest whole number is m = 58

At m = 57 the bubble is exactly 3/60 = 5%, which is not below 5%. So you need 58 microbatches. Your global batch must be at least 58 × microbatch_size. This is why PP works best with large global batches.

Exercise 4.4: Parallel Config for Llama 3 405B on 16,384 H100s

Meta pre-trained Llama 3 405B on 16,384 H100s, 8 GPUs per node. Rules: TP must stay inside a node, PP adds bubbles, data parallelism (DP) covers the rest. TP × PP × DP = 16,384. Which configuration did the Llama 3 paper use?

Show reasoning

The Llama 3 paper (Table 4) used TP=8, PP=16, DP=128 on 16,384 GPUs, at 41% BF16 MFU.

TP=8 uses all 8 GPUs of a node over NVLink. TP=16 (option B) would put the per-layer all-reduces on the slower links between nodes.

PP=16 is needed for memory: 405B BF16 weights are 810 GB, so TP=8 alone leaves 101 GB per GPU, more than 80 GB. With PP=16 each GPU holds about 6.3 GB of weights.

DP=128 covers the rest: 16,384 / (8 × 16) = 128. Option C would gather all 405B weights over the network for every layer. Option D needs DP to shard the 101 GB of weights per GPU, again with all-gathers across nodes.

Exercise 4.5: Activation Memory

Activation memory per layer (no activation checkpointing): you must store the input to each layer for the backward pass. For LLaMA 3 70B with batch=4, seq=4096, d=8192, BF16: how much activation memory per layer?

Simplified: store the input tensor [batch × seq, d] plus the attention intermediate [batch, n_heads, seq, seq] (for the backward through attention).

GB per layer
Show derivation
Input activation: 4 × 4096 × 8192 × 2 = 268,435,456 bytes = 0.27 GB
Attention scores: 4 × 64 × 4096 × 4096 × 2 = 8,589,934,592 bytes = 8.59 GB
Total per layer ≈ 8.86 GB

The attention scores dominate. For 80 layers: 80 × 8.86 ≈ 709 GB. Three fixes: (1) Activation checkpointing: keep only each layer's input and recompute the rest in the backward pass, for about one extra forward pass (~33% more compute). (2) FlashAttention: never stores the full score matrix, so the 8.59 GB term disappears. (3) Sequence parallelism: shard the activations across the TP group.

FSDP (ZeRO-3) shards parameters, gradients, AND optimizer states across GPUs. Before each forward/backward computation, it must all-gather the full parameters. What is the cost of this compared to standard data parallelism?

Chapter 5: Training Cost Estimation

Now we combine everything: parameter count, FLOPs, hardware specs, and utilization to answer the question every ML lead asks: "How long will this take, and how much will it cost?"

The Core Formula

Training time = 6ND / (n_GPUs × peak_FLOPs × MFU)

Where N = parameters, D = tokens, n_GPUs = GPU count, peak_FLOPs = per-GPU peak FLOP/s, MFU = model FLOPs utilization (typically 0.35-0.50).

Exercise 5.1: Training Time for LLaMA 3 70B

LLaMA 3 70B: N=70.6B, D=15T tokens, 2048 H100s at 990 TFLOP/s peak BF16, MFU=0.40. How many days to train?

days
Show derivation
Total FLOPs = 6 × 70.6 × 109 × 15 × 1012 = 6.354 × 1024
Aggregate throughput = 2048 × 990 × 1012 × 0.40 = 8.11 × 1017 FLOP/s
Time = 6.354 × 1024 / 8.11 × 1017 = 7.83 × 106 seconds
= 7,830,000 s / 86400 s/day ≈ 90.7 days

That is 2048 × 90.7 × 24 ≈ 4.46M GPU-hours. Meta's model card reports 6.4M H100-hours for Llama 3 70B. If all of those hours went into 6.35 × 1024 FLOPs, the real MFU was about 28%. Some of the hours likely went to work outside the main run, so treat that as a lower bound.

Exercise 5.2: Dollar Cost

Using your answer from 5.1 (~91 days), at $2/GPU-hour for H100 cloud pricing, what is the total training cost?

USD
Show derivation
GPU-hours = 2048 GPUs × 91 days × 24 hours/day = 4,472,832 GPU-hours
Cost = 4,472,832 × $2 = $8,945,664

Roughly $9M for the final run alone. A real project also pays for failed runs, hyperparameter sweeps, evaluations and data processing, so the total is higher.

Exercise 5.3: Chinchilla-Optimal Tokens

The Chinchilla scaling law says the compute-optimal number of tokens D is about 20 × N. For a 70B model, what is the Chinchilla-optimal token count?

T tokens
Show derivation
Doptimal = 20 × N = 20 × 70 × 109 = 1.4 × 1012 = 1.4T tokens

But Meta trained on 15T tokens — more than 10× the Chinchilla optimal! Why? Because Chinchilla optimizes for training compute efficiency (best model per FLOP). But inference cost depends on model size. A smaller model trained on more tokens (the "over-trained" regime) gives better inference cost per quality. LLaMA's philosophy: spend more on training (one-time cost) to get a smaller, cheaper-to-serve model. This is the inference-optimal scaling regime.

Exercise 5.4: Compute-Optimal Model Size

You have a fixed compute budget C = 1022 FLOPs. Under Chinchilla scaling (C = 6ND, D = 20N), what is the compute-optimal model size?

Hint: Substitute D = 20N into C = 6ND.

B parameters
Show derivation
C = 6ND = 6N × 20N = 120N2
N2 = C / 120 = 1022 / 120 = 8.33 × 1019
N = √(8.33 × 1019) = 9.13 × 109 = 9.13B

A 9B model trained on 183B tokens (20 × 9.13B). This is remarkably close to LLaMA 3 8B, which Meta trained on 15T tokens (way past Chinchilla-optimal, for inference efficiency).

Exercise 5.5: Learning Rate Scaling

One common rule for learning rate versus batch size: when you double the batch size, multiply the LR by √2 (square-root scaling). If your base LR is 3 × 10-4 at batch=256, what LR should you use at batch=2048?

learning rate
Show derivation
Batch increase factor = 2048 / 256 = 8
LR scale = √8 = 2.828
New LR = 3 × 10-4 × 2.828 = 8.49 × 10-4

Krizhevsky (2014) proposed square-root scaling, and Malladi et al. (2022) derive it for Adam. The linear rule (multiply LR by 8) is more aggressive; Goyal et al. (2017) used it with SGD and warmup. In practice, labs pick the peak LR with a sweep and use warmup plus a decay schedule.

Chinchilla vs inference-optimal. Chinchilla says D = 20N minimizes loss per training FLOP. Llama 3 70B used 6.35 × 1024 FLOPs. Spent the Chinchilla way (C = 120N2), that budget trains a ~230B model on ~4.6T tokens. Meta chose a 70B model on 15T tokens instead: a little worse per training FLOP, but about 3× cheaper per token to serve.
You have a fixed training compute budget of 1023 FLOPs. Under Chinchilla scaling (D = 20N, total FLOPs = 6ND), what model size maximizes quality?

Chapter 6: Inference Math

Inference has two distinct phases with completely different performance characteristics. Prefill processes the entire prompt in parallel — it is a batch matmul, compute-bound. Decode generates one token at a time — it is a matrix-vector product per layer, memory-bound.

Prefill vs decode intuition. Prefill is like reading a book all at once (batch processing). Decode is like writing a book one word at a time, looking up the entire story so far for each word (sequential, memory-bandwidth limited). This fundamental asymmetry drives the entire serving architecture.

Key Formulas

Prefill throughput (compute-bound):
tokens/s = n_GPUs × peak_FLOPs / (2N) // limited by compute

Decode throughput (memory-bound):
tokens/s/seq = mem_bandwidth / (N × bytes_per_param) // must read all weights per token
tokens/s total = tokens/s/seq × batch_size // batching amortizes weight reads

TTFT (time to first token) = prefill_tokens / prefill_throughput
ITL (inter-token latency) = 1 / decode_tokens_per_sec_per_seq

A 70B model in BF16 (141 GB) does not fit in one 80 GB H100. In this chapter, split its layers across two H100s that run one after the other. Each token still reads 141 GB at 3.35 TB/s, so the per-token times equal a single imaginary H100 that holds everything.

Exercise 6.1: Prefill Throughput

70B model (N = 70.6B), H100 at 990 TFLOP/s BF16, layers split as described above. Using 2N FLOPs per token for prefill, what is the theoretical prefill throughput?

tokens/s
Show derivation
Prefill tokens/s = Peak FLOPs / (2N) = 990 × 1012 / (2 × 70.6 × 109)
= 990 × 1012 / (141.2 × 109) ≈ 7,011 tokens/s

Real kernels reach only part of peak, and attention adds FLOPs. The next exercises assume a realistic 4,000 tokens/s.

Exercise 6.2: Time to First Token

You send a 4096-token prompt to the 70B model on the two-H100 setup. Assuming a realistic prefill throughput of 4000 tokens/s, what is the time to first token (TTFT)?

seconds
Show derivation
TTFT = prompt_length / prefill_throughput = 4096 / 4000 = 1.024 seconds

Over a second before the first token. Servers use chunked prefill (split the prompt so decode steps of other users run in between) and prefix caching (reuse the KV cache of a shared system prompt).

Exercise 6.3: Decode Throughput and Inter-Token Latency

70B model in BF16, H100 bandwidth 3.35 TB/s, layers split as described above. At batch_size=1, each decode step reads all model weights once. What is the decode speed (tokens/s) and inter-token latency? Ignore the KV cache reads.

tokens/s
ms (inter-token latency)
Show derivation
Model weights in BF16 = 70.6 × 109 × 2 bytes = 141.2 GB
Time per token = 141.2 GB / (3.35 TB/s) = 141.2 / 3350 = 0.04215 s = 42.2 ms
Tokens/s = 1 / 0.04215 = 23.7 tokens/s

At batch_size=1, decode is entirely memory-bandwidth-bound — the MXU/Tensor Cores sit mostly idle! This is why batching is critical: at batch_size=32, you read the weights once but generate 32 tokens, giving 32 × 23.7 = 758 tokens/s total throughput (though per-user latency stays ~42 ms until you become compute-bound).

Exercise 6.4: The Decode Crossover Batch Size

Prefill is compute-bound, decode is memory-bound. At what batch size does decode become compute-bound on an H100 with a 70B BF16 model? Ignore the KV cache reads.

Hint: decode becomes compute-bound when its arithmetic intensity, B × 2N FLOPs / 2N bytes, reaches the ridge point.

batch size (crossover)
Show derivation

The crossover point is the ridge point! At the crossover, FLOPs per byte equals the H100's ridge point.

Per-token decode: FLOPs = 2N, Bytes = 2N (read all weights)
With batch B: FLOPs = B × 2N, Bytes = 2N (weights read once, shared)
AI = B × 2N / (2N) = B FLOPs/byte
Crossover: B = ridge point ≈ 296

Below a batch of ~296, decode leaves the Tensor Cores partly idle. Prefill and decode want such different batch sizes that some systems run them on separate GPU pools (disaggregated serving). In practice, KV cache memory limits the batch long before 296, and the KV reads themselves lower the arithmetic intensity.

Exercise 6.5: Continuous Batching Throughput

With static batching (batch=32, all sequences decode together), if the shortest response is 50 tokens and the longest is 500, the GPU sits idle for finished sequences. Average output length is 200 tokens. What is the effective utilization compared to continuous batching (which immediately fills empty slots)?

Hint: In static batching, all sequences run for max_length steps. GPU utilization = average_length / max_length.

% effective utilization
Show derivation
Static batching utilization = avg_length / max_length = 200 / 500 = 40%

60% of the batch slots do no useful work. Continuous batching (iteration-level scheduling, from the Orca paper) removes a finished sequence at once and inserts a new one, so slots stay nearly full. Here that is up to 2.5× more throughput. With PagedAttention, which stops KV cache fragmentation, it is the core of servers like vLLM.

You are serving a 70B model. A user sends a 100-token prompt and expects a 500-token response. Which takes longer, prefill or decode?

Chapter 7: Serving Cost

Training is a one-time expense. Serving is forever. The cost to serve one million tokens determines whether your model is a viable product or a science experiment. Let's do the math.

The Serving Cost Formula

$/1M tokens = (GPU_cost_per_hour / tokens_per_hour) × 106

tokens_per_hour = tokens/s × 3600 × utilization

For decode-bound serving (most common):
tokens/s = batch × mem_bandwidth / (2 × N × bytes_per_param)
Exercise 7.1: Cost per Million Output Tokens

70B model in BF16 on 2× H100s, layers split across them (as in Chapter 6). Achievable decode throughput at batch=64: ~1500 tokens/s. GPU utilization: 70% (includes time waiting for requests). Cost: $4/hour for the 2-GPU setup. What is the cost per million output tokens?

$ per 1M tokens
Show derivation
Effective tokens/hour = 1500 × 3600 × 0.70 = 3,780,000
$/1M tokens = ($4 / 3,780,000) × 106 = $1.06

About $1 of hardware per million output tokens. An API price also has to cover idle capacity, engineering, margins and the training cost.

Exercise 7.2: Break-Even Batch Size

You need to serve at $0.50/1M tokens to be competitive. Same setup as 7.1 ($4/hour for 2× H100, 70% utilization). At batch=1, throughput is 24 tok/s. Throughput scales linearly with batch until the compute-bound crossover. What minimum batch size do you need?

sequences
Show derivation
Need: tokens_per_hour ≥ $4 / ($0.50 / 106) = 8,000,000
tokens/s = 8,000,000 / (3600 × 0.70) = 3,174
batch = 3,174 / 24 = 132.25 → batch ≥ 133

You need a batch of at least 133. But each sequence needs its own KV cache. At seq_len = 8192 with GQA, that is 2.68 GB per sequence (Exercise 3.5), so 133 × 2.68 ≈ 357 GB. Add 141 GB of weights and you need about 500 GB; two H100s have 160 GB. At this price you need shorter contexts, more GPUs, or a quantized KV cache.

Exercise 7.3: Speculative Decoding Break-Even

Speculative decoding uses a small "draft" model to propose K tokens, then the large model verifies all K in one forward pass. The draft model is 10× faster, and each drafted token is accepted with probability α. Use the simple model below. With K=5, what acceptance rate α do you need for a 2× overall speedup?

Simplified: expected accepted tokens per step = Kα. Cost per step = 1 (verify) + K/10 (draft). Speedup = Kα / (1 + K/10). Set this ≥ 2.

α (acceptance rate, 0-1)
Show derivation
Speedup = Kα / (1 + K/10) ≥ 2
5α / (1 + 0.5) ≥ 2
5α / 1.5 ≥ 2
α ≥ 3.0 / 5.0 = 0.60

The simple model says 60%. It is optimistic: a rejection discards every later draft token. Leviathan et al. (2023) give the expected tokens per step as (1 − αK+1) / (1 − α). At α = 0.6 and K = 5 that is 2.38 tokens, or a 2.38 / 1.5 ≈ 1.6× speedup. Acceptance depends on how well the draft matches the target on the text at hand.

Exercise 7.4: Input vs Output Token Cost

API providers charge differently for input (prefill) and output (decode) tokens. For a 70B model on H100:
Prefill throughput: ~4000 tok/s (compute-bound, high GPU utilization).
Decode throughput at batch=64: ~1500 tok/s (memory-bound).
Same $4/hour GPU cost, 70% utilization for both. What is the cost per 1M input tokens vs output tokens?

$/1M input tokens
$/1M output tokens
Show derivation
Input: tokens/hour = 4000 × 3600 × 0.70 = 10,080,000
$/1M input = ($4 / 10,080,000) × 106 = $0.397
Output: tokens/hour = 1500 × 3600 × 0.70 = 3,780,000
$/1M output = ($4 / 3,780,000) × 106 = $1.06

Output tokens cost ~2.7× more than input tokens here. Prefill is compute-bound and fast; decode is memory-bound and slower. This is one reason API providers charge more for output tokens.

Exercise 7.5: Quantization Impact on Serving Cost

You quantize the 70B model from BF16 to INT4 (4 bits per weight). This cuts the model to a quarter of its size (from 141 GB to ~35 GB), so it fits on a single H100. Decode throughput scales with bandwidth/model_size: about 3.35 TB/s / 35 GB ≈ 95 tokens/s at batch=1, or ~6000 tok/s at batch=64. GPU cost is now $2/hour (single GPU). What is the new $/1M output tokens?

$/1M output tokens
Show derivation
Tokens/hour = 6000 × 3600 × 0.70 = 15,120,000
$/1M output = ($2 / 15,120,000) × 106 = $0.132

INT4 cuts the cost ~8× (from $1.06 to $0.13 per 1M tokens). Two factors multiply: 4× fewer bytes to read per token, and one GPU instead of two. The catch is quality: check that the quantized model still passes your evals.

Why is INT8/INT4 quantization so impactful for serving but less critical for training?

Chapter 8: Profiling Exercises

A profile tells you where time goes. Without a profile, optimization is guessing. With one, you can calculate exactly how much speedup is possible and where to focus.

The first rule of optimization. Measure before you optimize. The second rule: if you can not explain WHY a kernel takes the time it does from first principles, you do not understand the system well enough to optimize it.
Exercise 8.1: Identify the Bottleneck

Here is a simplified profile of one LLaMA 3 70B layer's forward pass on one H100 of a TP=8 group (each GPU holds 1/8 of every weight). Batch=2, seq=2048, so M = 4096 tokens.

KernelTime (ms)Category
QKV projection (fused gemm)0.10Compute
FlashAttention fwd0.05Compute
Output projection0.08Compute
RMSNorm + residual add0.12Memory
MLP up + gate (fused gemm)0.51Compute
SiLU × gate0.08Memory
MLP down projection0.26Compute
RMSNorm + residual add0.12Memory
NCCL all-reduce × 2 (TP, not overlapped)0.52Communication

Total: 1.84 ms per layer. Which target is worth the most time?

Show reasoning

Each all-reduce moves [4096, 8192] BF16 = 67.1 MB, so 2 × 7/8 × 67.1 ≈ 117 MB per device. At 450 GB/s that is 0.26 ms, twice per layer: 0.52 ms, the largest non-matmul cost. Fusion (0.32 ms, 17%) is the second target; Exercise 8.4 measures it. FlashAttention is only 0.05 ms, so speeding it up barely matters. INT8 gemms would shrink compute, which is already near peak (Exercise 8.2).

Exercise 8.2: Expected vs Measured Kernel Time

The MLP up+gate projection is a [batch×seq, d] × [d, 2×d_ff] matmul with batch=2, seq=2048, d=8192, d_ff=28672. With TP=8, each GPU computes 1/8 of the output columns. BF16 on H100. What is the theoretical minimum time per GPU (compute-bound at 990 TFLOP/s)?

ms
Show derivation
M = 2 × 2048 = 4096, K = 8192, N = 2 × 28672 / 8 = 7168
FLOPs = 2 × 4096 × 8192 × 7168 = 4.81 × 1011
Time = 4.81 × 1011 / (990 × 1012) = 0.486 ms

The profile measured 0.51 ms, so the kernel runs at 0.486 / 0.51 ≈ 95% of peak: near-optimal. Rule of thumb: if measured time is more than 2× the minimum, there is headroom; within 1.2×, look elsewhere. The same check on the down projection gives 0.243 ms against 0.26 ms measured.

Exercise 8.3: SM Occupancy

An H100 has 132 SMs, each supporting up to 2048 threads (64 warps of 32 threads). Your kernel uses 128 threads per block and 48 KB of shared memory per block. Each SM has 228 KB of shared memory, and CUDA reserves 1 KB more per block. Ignore register limits. How many blocks can run per SM, and what is the occupancy?

blocks per SM
% occupancy
Show derivation

Shared memory limit: 228 KB / (48 + 1) KB = 4.65 → 4 blocks per SM

Thread limit: 2048 / 128 = 16 blocks per SM

Limiting factor: shared memory → 4 blocks per SM

Active threads = 4 × 128 = 512
Occupancy = 512 / 2048 = 25%

25% occupancy is low. To improve: reduce shared memory per block (use tiling to process in smaller chunks), increase threads per block (if the algorithm allows), or restructure the kernel to use less shared memory. Low occupancy is not always bad — if the kernel is compute-bound and achieves high IPC, occupancy matters less than if it is memory-bound.

Exercise 8.4: Amdahl's Law for Kernel Fusion

From the profile in Exercise 8.1, the total layer time is 1.84 ms. If you perfectly fuse all memory-bound ops (0.32 ms total) into adjacent compute kernels, eliminating them entirely, what is the speedup for the full layer?

× speedup
Show derivation
New layer time = 1.84 - 0.32 = 1.52 ms
Speedup = 1.84 / 1.52 = 1.21×

This is Amdahl's Law: removing 17.4% of the time gives 1/(1 − 0.174) = 1.21×. The other 82.6% is unchanged, so no fusion can beat 1.21× here. Over 80 layers, it saves 80 × 0.32 ms = 25.6 ms per forward pass.

Exercise 8.5: Communication Overlap

The two TP all-reduces in the profile take 0.52 ms. They cannot overlap the next layer's QKV, because the QKV needs their result. So you split the batch into two microbatches: one microbatch's all-reduces run while the other's matmuls run. Say this gives 0.42 ms of independent matmul time to overlap with. What fraction of the communication is hidden?

%
Show derivation
Communication time = 0.52 ms
Independent compute to overlap = 0.42 ms
Hidden = 0.42 / 0.52 ≈ 81%; exposed = 0.10 ms

Overlap can hide communication only up to the length of the independent work beside it. The layer drops from 1.84 to 1.42 ms. To hide the last 0.10 ms you need more independent work, or overlap inside the matmul itself (a collective matmul splits the communication into chunks; see the Scaling Book's sharded-matmul chapter).

You profile a CUDA kernel and find it achieves 40% of peak memory bandwidth but only 5% of peak compute. What does this tell you?

Chapter 9: JAX Programming

JAX is the framework of choice for scaling research at Google, DeepMind, and many frontier labs. It is not PyTorch with a different API. JAX is a functional transformation system: you write pure functions, and JAX transforms them (differentiation, compilation, parallelization, vectorization).

The JAX mental model. Think of JAX as a compiler, not a runtime. jax.jit traces your Python function into XLA HLO (an intermediate representation), which XLA compiles into optimized device code. jax.grad transforms the traced graph by adding backward nodes. jax.pmap runs one copy of the graph on each device. All transforms compose.

Guided: JAX Training Loop for Linear Regression

Here is the simplest possible JAX training loop. Study each line — the patterns here scale to billion-parameter models.

jax
import jax
import jax.numpy as jnp

# 1. Generate synthetic data: y = 3x + 1 + noise
key = jax.random.PRNGKey(42)
key_x, key_noise = jax.random.split(key)  # never reuse a key: same key = same random numbers
X = jax.random.normal(key_x, (100, 1))
y = 3.0 * X + 1.0 + 0.1 * jax.random.normal(key_noise, (100, 1))

# 2. Initialize parameters (JAX uses explicit state, no hidden .grad)
params = {'w': jnp.zeros((1, 1)), 'b': jnp.zeros((1,))}

# 3. Define loss as a PURE FUNCTION of params (no side effects!)
def loss_fn(params, X, y):
    pred = X @ params['w'] + params['b']
    return jnp.mean((pred - y) ** 2)

# 4. jax.grad returns a FUNCTION that computes gradients
grad_fn = jax.grad(loss_fn)  # differentiates w.r.t. first arg (params)

# 5. Training loop — functional update, no in-place mutation
lr = 0.1
for step in range(100):
    grads = grad_fn(params, X, y)
    params = jax.tree.map(lambda p, g: p - lr * g, params, grads)
    if step % 20 == 0:
        print(f"Step {step}: loss={loss_fn(params, X, y):.4f}")

print(f"Learned: w={params['w'][0,0]:.3f}, b={params['b'][0]:.3f}")
# Expected: w≈3.0, b≈1.0
Key JAX patterns. (1) jax.grad differentiates pure functions — no .backward() on tensors. (2) jax.tree.map applies a function across all leaves of a pytree (nested dict/list of arrays). (3) No in-place updates — every step creates new arrays. (4) jax.jit wraps the training step for compilation.

Guided: Data Parallel Training with pmap

jax
import functools
import jax
import jax.numpy as jnp

n_devices = jax.local_device_count()  # the batch (100) must divide by this

# Replicate params to all devices
params = jax.tree.map(lambda x: jnp.stack([x] * n_devices), params)

# Define one step: forward, backward, all-reduce, update
@functools.partial(jax.pmap, axis_name='batch')  # name the device axis for pmean
def train_step(params, X_shard, y_shard):
    grads = jax.grad(loss_fn)(params, X_shard, y_shard)
    # All-reduce: average gradients across devices
    grads = jax.lax.pmean(grads, axis_name='batch')
    return jax.tree.map(lambda p, g: p - 0.1 * g, params, grads)

# Split data across devices (axis 0 = device dimension)
X_sharded = X.reshape(n_devices, -1, 1)
y_sharded = y.reshape(n_devices, -1, 1)

# Train!
for step in range(100):
    params = train_step(params, X_sharded, y_sharded)

jax.pmap maps the function across devices. jax.lax.pmean inserts an all-reduce over the axis named 'batch'; without axis_name, JAX raises an unbound-axis error. This is plain data parallelism: every device holds a full copy of the parameters (FSDP would shard them). Newer JAX code does the same with jax.jit plus shardings or shard_map.

Guided: A Minimal Pallas Kernel

Pallas is JAX's kernel language. You write kernels in Python, and they compile to GPU and TPU kernels. Here is a vector addition kernel, the "hello world" of custom kernels. On a CPU, pass interpret=True to pallas_call to test it.

jax/pallas
import jax
import jax.numpy as jnp
from jax.experimental import pallas as pl

def add_kernel(x_ref, y_ref, o_ref):
    """Pallas kernel: reads from x_ref and y_ref, writes to o_ref.
    Refs are BlockSpec views — they point to a tile of the full array."""
    o_ref[...] = x_ref[...] + y_ref[...]

def add_vectors(x, y):
    return pl.pallas_call(
        add_kernel,
        out_shape=jax.ShapeDtypeStruct(x.shape, x.dtype),
        grid=(8,),  # launch 8 blocks
        in_specs=[
            pl.BlockSpec((128,), lambda i: (i,)),  # each block gets 128 elements
            pl.BlockSpec((128,), lambda i: (i,)),
        ],
        out_specs=pl.BlockSpec((128,), lambda i: (i,)),
    )(x, y)

# Test
x = jnp.arange(1024, dtype=jnp.float32)
y = jnp.ones(1024, dtype=jnp.float32)
result = add_vectors(x, y)
# result == jnp.arange(1024) + 1

Guided: Automatic Vectorization with vmap

jax.vmap automatically vectorizes a function that operates on a single example to work on a batch. This is how you write clean, single-example logic and get batched execution for free.

jax
import jax
import jax.numpy as jnp

# Function that works on ONE example (no batch dim)
def predict_single(params, x):
    """x: shape [D], returns scalar"""
    return jnp.dot(params['w'], x) + params['b']

# vmap it to handle batches: x now shape [B, D]
predict_batch = jax.vmap(predict_single, in_axes=(None, 0))
# in_axes=(None, 0): don't batch over params, batch over axis 0 of x

# This compiles to the SAME efficient batched matmul as manual batching
params = {'w': jnp.ones(768), 'b': jnp.zeros(())}
x_batch = jax.random.normal(jax.random.PRNGKey(0), (32, 768))
out = predict_batch(params, x_batch)  # shape [32]

vmap composes with jit and grad: jax.jit(jax.vmap(jax.grad(loss_fn), in_axes=(None, 0, 0))) gives one gradient per example, compiled into a single XLA graph. Feed it X and y with shape [B, 1, 1] so each example is a batch of one.

Why Pallas matters. XLA's compiler is good at fusing small ops, but it cannot invent new algorithms. When you need a tiled matmul with custom masking, a ragged-batch attention kernel, or an operation that does not exist in XLA, Pallas lets you write it in Python and compile it to efficient device code. Vlad Feinberg's post "How to Land a Job at a Frontier Lab" (2026) lists a Pallas kernel exercise; Chapter 10 returns to it.
In JAX, what does jax.jit(f) do?

Chapter 10: The Capstone — Addition Transformer

Vlad Feinberg's post "How to Land a Job at a Frontier Lab" (2026) suggests this exercise: code a ~10M parameter transformer with only JAX, Flax and Optax, and have it learn addition. It tests tokenization, model sizing, scaling laws, training dynamics and JAX fluency at once.

The meta-lesson. The addition transformer is a microcosm of frontier model development. Every decision you make here (tokenizer design, model sizing, learning rate schedule, data distribution) has a direct analog at the 70B scale. The person who has built a 10M model from scratch understands scaling in their bones, not just in their notes.

Step 1: Design the Tokenizer

The vocabulary is tiny: digits 0-9, plus sign, equals sign, and a padding token. That is 13 tokens total. (The post uses a space token where we use padding.)

Token vocabulary (13 tokens):
0-9: digit tokens (IDs 0-9)
+ : addition operator (ID 10)
= : equals sign (ID 11)
<pad>: padding (ID 12)

Input format: "123+456=579<pad><pad>..."
All examples padded to a fixed length (max_len = 12 for 3-digit addition; see Exercise 10.1)

The model sees the entire string but only predicts the answer portion (after the = sign). During training, you mask the loss on the prompt tokens.

Exercise 10.1: Maximum Sequence Length

For addition of up to 3-digit numbers, what is the maximum input sequence length? Consider: "999+999=1998". Count each character as one token.

tokens
Show derivation
"999+999=1998" = 3 + 1 + 3 + 1 + 4 = 12 characters

The maximum result of 3-digit addition is 999+999=1998, which is 4 digits. So max_len = 3 + 1 + 3 + 1 + 4 = 12. Pad all sequences to length 12. Shorter examples (like "1+2=3") get padded: "1+2=3<pad><pad><pad><pad><pad><pad><pad>".

Step 2: Architecture Sizing

You want ~10M parameters. For a decoder-only transformer with:

Params ≈ vocab × d + L × (4d2 + 2 × d × d_ff + norms) + d × vocab

With d_ff = 4d (standard) and small vocab (13):
Params ≈ 2 × 13d + L × (4d2 + 8d2) = 26d + L × 12d2
Params ≈ L × 12d2 (embedding terms are negligible for small vocab)
Exercise 10.2: Choose Architecture Parameters

Target 10M parameters. Try L=6 layers. What value of d (model dimension) gets you closest to 10M? Use the simplified formula: Params ≈ L × 12d2.

d (model dimension)
Show derivation
10 × 106 = 6 × 12 × d2 = 72d2
d2 = 10,000,000 / 72 = 138,889
d = √138,889 ≈ 373

Round to d=384 (divisible by common head counts). With 6 heads, d_head = 384/6 = 64. The formula gives 6 × 12 × 3842 = 10,616,832 ≈ 10.6M. The Flax model below, with biases, norms and embeddings, has 10,662,157.

A practical config: L=6, d=384, n_heads=6, d_head=64, d_ff=1536, vocab=13, max_seq=12.

Step 3: The Flax Implementation

Here is the core transformer in Flax (linen API). It runs as written; initialising it on [2, 12] tokens gives [2, 12, 13] logits.

python/flax
import jax
import jax.numpy as jnp
import flax.linen as nn
import optax

class CausalSelfAttention(nn.Module):
    n_heads: int
    d_model: int

    @nn.compact
    def __call__(self, x):
        B, T, C = x.shape
        d_head = C // self.n_heads
        # Fused QKV projection
        qkv = nn.Dense(3 * C)(x)  # [B, T, 3C]
        q, k, v = jnp.split(qkv, 3, axis=-1)
        # Reshape for multi-head: [B, T, C] -> [B, n_heads, T, d_head]
        q = q.reshape(B, T, self.n_heads, d_head).transpose(0,2,1,3)
        k = k.reshape(B, T, self.n_heads, d_head).transpose(0,2,1,3)
        v = v.reshape(B, T, self.n_heads, d_head).transpose(0,2,1,3)
        # Scaled dot-product attention with causal mask
        attn = (q @ k.transpose(0,1,3,2)) / jnp.sqrt(d_head)
        mask = jnp.tril(jnp.ones((T, T)))[None, None, :, :]
        attn = jnp.where(mask == 0, -1e9, attn)
        attn = jax.nn.softmax(attn, axis=-1)
        out = (attn @ v).transpose(0,2,1,3).reshape(B, T, C)
        return nn.Dense(C)(out)  # output projection

class TransformerBlock(nn.Module):
    n_heads: int
    d_model: int
    d_ff: int

    @nn.compact
    def __call__(self, x):
        # Pre-norm architecture (like GPT-2, LLaMA)
        x = x + CausalSelfAttention(self.n_heads, self.d_model)(nn.LayerNorm()(x))
        x = x + nn.Dense(self.d_model)(nn.gelu(nn.Dense(self.d_ff)(nn.LayerNorm()(x))))
        return x

class AdditionTransformer(nn.Module):
    vocab: int = 13
    d_model: int = 384
    n_heads: int = 6
    n_layers: int = 6
    d_ff: int = 1536
    max_len: int = 12

    @nn.compact
    def __call__(self, tokens):
        B, T = tokens.shape
        # Token + positional embeddings
        tok_emb = nn.Embed(self.vocab, self.d_model)(tokens)
        pos_emb = nn.Embed(self.max_len, self.d_model)(jnp.arange(T))
        x = tok_emb + pos_emb  # [B, T, d_model]
        # Transformer blocks
        for _ in range(self.n_layers):
            x = TransformerBlock(self.n_heads, self.d_model, self.d_ff)(x)
        x = nn.LayerNorm()(x)
        logits = nn.Dense(self.vocab)(x)  # [B, T, vocab]
        return logits
Exercise 10.3: Chinchilla-Optimal Training for 10M

Using D = 20N, how many tokens should you train your 10M model on? How many 3-digit addition examples is that (each example ~12 tokens)?

tokens
examples
Show derivation
D = 20 × 10 × 106 = 200,000,000 tokens
Examples = 200,000,000 / 12 ≈ 16,666,667 examples

With operands from 0 to 999 there are only 1,000 × 1,000 = 1,000,000 distinct problems, so the model sees each one ~17 times. Chinchilla's 20N rule was fit on web text, not on a small closed task, so treat 200M tokens as a starting budget and watch held-out accuracy.

Exercise 10.2b: Training Compute for the Addition Transformer

Your 10.6M parameter model will train on 200M tokens. What is the total training compute in PFLOP (1015 FLOPs)? How long will this take on a single T4 GPU (65 TFLOP/s FP16, assuming 30% MFU)?

PFLOP (total training FLOPs)
minutes
Show derivation
FLOPs = 6 × 10.6 × 106 × 200 × 106 = 1.272 × 1016 = 12.72 PFLOP
Effective throughput = 65 × 1012 × 0.30 = 19.5 × 1012 FLOP/s
Time = 1.272 × 1016 / (19.5 × 1012) = 652 seconds ≈ 10.9 minutes

Under 11 minutes on a Colab T4, so you can run many experiments in an afternoon. If training takes much longer, check your batch size and your data pipeline first.

Step 4: Dense vs MoE

The post also asks you to derive Chinchilla laws for this model and see how they differ for dense and MoE (Mixture of Experts) models. An MoE model has more total parameters (N_total) but uses only some of them per token (N_active). Training compute depends on the active count.

Dense: FLOPs/token = 2N, Training FLOPs = 6ND
MoE: FLOPs/token = 2N_active, Training FLOPs = 6 × N_active × D

If top-2 routing with E experts: N_active ≈ N_shared + 2/E × N_expert
First guess: D = 20 × N_active (an assumption; MoE scaling laws differ from dense ones, which is what the post asks you to find)
Exercise 10.4: MoE vs Dense Scaling

You have a compute budget of 6 × 1020 FLOPs. Compare two options:
(A) Dense 1B model, trained on D_A tokens.
(B) MoE with 8 experts, top-2 routing, N_active = 1B (but total params = ~3.5B). Trained on D_B tokens.
Using C = 6 × N_active × D, how many tokens can each train on?

B tokens (Dense)
B tokens (MoE)
Show derivation
Budget = 6 × N_active × D
D = Budget / (6 × N_active) = 6 × 1020 / (6 × 109) = 1011 = 100B tokens

Both get 100B tokens, because both have N_active = 1B. The MoE has 3.5B total parameters, so more capacity, for the same training compute. At inference it needs 3.5× the weight memory but the same FLOPs per token. (Router and load-balancing FLOPs are ignored here.)

Step 5: When Does Pallas Beat ragged_dot?

MoE routing creates "ragged" batches: each expert gets a different number of tokens. jax.lax.ragged_dot does one grouped matmul over such groups. The post asks you to "write a pallas kernel that beats ragged dot for F > D by fusing the up/down projections". It does not define F and D. We read D as the model width and F as each expert's hidden (FFN) width.

Exercise 10.5: The F > D Condition

Each expert computes down(act(up(x))): up maps [tokens, D] to [tokens, F], down maps back to [tokens, D]. With ragged_dot you call it twice: once for up, once for down. Take D = 1024 and F = 4096. Why can a fused Pallas kernel win when F > D?

Show reasoning

Per routed token in BF16: the input and output are D × 2 bytes each (2 KB each). The hidden activation is F × 2 = 8 KB, written once and read once: 16 KB of extra traffic, 4× the input plus output. Fusion removes it; the FLOPs stay the same.

The gain only shows when activations matter. Reading the expert weights (2 × D × F × 2 bytes = 16.8 MB per expert here) is a fixed cost. With few tokens per expert, weight reads dominate and fusion barely helps. The post asks you to find a setting with a measurable speedup and explain it; this traffic count is where to start.

You have trained your 10M addition transformer and it reaches 95% accuracy on 3-digit addition. You want 99.9%. What should you try first?

Chapter 11: Self-Assessment Checklist

Here is every skill this workbook covers. Be honest with yourself: can you do each of these from scratch, with paper and pencil, without looking up the formula? Check off each one as you master it.

The proof-of-work standard. Vlad Feinberg's post suggests you "record yourself doing the jax scaling book exercises above with paper and pencil". The same test works here: close this page, open a blank notebook, and redo each item below from memory. If you cannot reproduce a derivation, you have not learned it yet.

Roofline & Hardware

Sharding & Communication

Parameter Counting & FLOPs

Training Parallelism

Training Cost

Inference & Serving

Profiling & Kernels

The Capstone

Your Progress

Click checklist items above to track your progress.

0 / 41 mastered

Related Lessons

TopicLesson
Transformer internalsTransformer — From Absolute Zero
GPT architectureGPT — From Absolute Zero
Diffusion modelsDiffusion — From Absolute Zero
Reward & alignmentRLHF — From Absolute Zero
Inference engineeringML Inference Engineer — Day In The Life

The Study Protocol

Here is the protocol that maximizes retention from this workbook. It is based on spaced retrieval practice — the most evidence-backed learning technique in cognitive science.

Day 1: Work Through
Complete every exercise in this workbook with the hints and solutions available. Take notes on any formula you did not know. The goal is exposure, not mastery.
↓
Day 2: Blank Paper
Open a blank notebook. Without looking at this page, try to re-derive every key formula: ridge point, 6ND, Chinchilla D=20N, KV cache sizing, MFU, serving cost. Check your work afterward. Mark the ones you got wrong.
↓
Day 4: Teach It
Explain three concepts from this workbook to a friend, a rubber duck, or a voice recorder. Speaking forces deeper processing than passive review. Focus on the ones you missed on Day 2.
↓
Day 7: Full Test
Blank paper, no hints. Work through 10 randomly selected exercises. If you score 8/10, move on. If not, repeat the cycle for the missed topics.
↓
Day 14: Implement
Code the addition transformer in a Colab notebook. Run training. Measure actual throughput and compare to your theoretical prediction. This is your proof of work.
The standard. "What I cannot create, I do not understand." — Richard Feynman. If you can work through every exercise in this workbook from a blank sheet of paper, you understand scaling at the level that frontier labs expect. That is the proof of work.
What is the single most important formula to internalize from this workbook?