One budget of 5.9 × 1023 FLOPs can train a 280B model on 350B tokens or a 70B model on 1.4T tokens. The smaller model reaches the lower loss. A frontier lab engineer makes that call with arithmetic, before the GPUs start.
Pick a plan and watch where it lands on the loss curve. Drag the MFU slider to see how many days it takes. Then we build each skill, piece by piece: compute, scaling laws, memory, parallelism, rooflines, reliability, experts, quantization and serving.
You need some Python and a rough idea of what a neural network is (weights, a loss, gradient steps). We build the rest from zero.
A Day in the Life guide to AI research infrastructure: the systems below the model. Loss values use the fitted law from Hoffmann et al., Training Compute-Optimal Large Language Models (2022). Hardware numbers are from NVIDIA's H100 page; run numbers are from the Llama 3 paper (2024).
Compute C = 6ND = 5.88 × 1023 FLOPs for every plan (Chinchilla's 70B and 1.4T tokens). Loss is the paper's fit L = 1.69 + 406.4/N0.34 + 410.7/D0.28, in nats per token on its own data. Days assume 2,048 H100s at 989.5 dense BF16 TFLOPS each. The 32B plan is the fit's own minimum.
Chapter 0
Follow one big model from a compute budget to a served answer, and see where a cluster's hours go
Your lab has booked 2,048 GPUs for the next model. Before anyone presses start, someone must answer four questions:
A frontier lab engineer builds and runs the systems that train, tune, test and serve the largest models. Most of the job is accounting: FLOPs, bytes, hours and error bars. Each skill below is one number you can compute and one failure you can catch.
| Skill | The question it answers | Chapter |
|---|---|---|
| Compute budget | How many FLOPs, and how many days? | 1 |
| Scaling laws | Which model size and token count give the lowest loss? | 2 |
| Memory | Does the training state fit on the GPUs? | 3 |
| Parallelism | How do we split the work, and how much time sits idle? | 4 |
| Rooflines | Is this kernel limited by math or by memory? | 5 |
| Reliability | How often do we save, given how often hardware fails? | 6 |
| Mixture of experts | How do we grow parameters without growing FLOPs? | 7 |
| Quantization | How few bits can each weight use? | 8 |
| Serving | How many users fit on one server? | 9 |
One number sums up a training cluster: MFU (model FLOPs utilization). It is the useful model math done, as a share of what the GPUs could do at peak. The rest of the time leaks away in many small places.
The bar is 100% of GPU time. Teal is useful math at peak speed. Switch on each fix and watch teal grow.
Illustrative shares. The end state, 44%, sits in the range that the Llama 3 paper reports for its 405B run (38% to 43% BF16 MFU).
Failure: MFU falls from 41% to 30% overnight with no code change. Measure: profile one step on every node and compare step times. Fix: one slow GPU makes every other GPU wait at each gradient exchange. Drain that node and restart from the last checkpoint.
Chapter 1
Count the FLOPs of a training run from two numbers, then turn them into days on a real cluster
Meta trained Llama 3 405B on 15.6 trillion tokens. How much math is that, and how long did it take? You can answer both from the model size, the token count and the GPU spec.
A FLOP is one floating-point operation: one multiply or one add. In the forward pass, each weight does one multiply and one add for each token, so 2 FLOPs. The backward pass costs about twice the forward, so 4 more. That gives 6 FLOPs per parameter per token.
Call the number of parameters N. Call the number of training tokens D. Call the total training math C, for compute.
C equals six times N times D.
Multiply the parameters N by the tokens D. Multiply by 6. That is the compute C.
Llama 3 405B: C = 6 × 405 × 109 × 15.6 × 1012 = 3.79 × 1025 FLOPs. The paper reports 3.8 × 1025, so the rule holds.
Now the days. Divide the compute by the cluster's useful speed. That speed is the number of GPUs, times one GPU's peak FLOPs per second, times the MFU.
days = C / (GPUs × peak × MFU) / 86,400 = 3.8e25 / (16,384 × 989.5e12 × 0.41) / 86,400 = 66 days
NVIDIA lists the H100 at 1,979 BF16 TFLOPS "with sparsity". Dense training math gets half: 989.5 TFLOPS. Llama 3 reports 38% to 43% MFU on up to 16,384 H100s.
The faint bar is the time at 100% MFU. The solid bar is the real time. Move any slider, or load the Llama 3 preset.
Peak is 989.5 dense BF16 TFLOPS per H100. The days count only math; restarts after failures add more (Chapter 6).
Failure: a plan for 400B parameters on 8T tokens over 2,048 H100s claims 2.72 hours. Measure: carry the units. 1.92 × 1025 FLOPs / (2,048 × 989 × 1012 FLOPs per second) = 9.5 million seconds, which is 110 days at peak. Fix: divide by MFU too: at 45% it is 244 days.
Chapter 2
Predict the loss of a run you cannot afford to try, and pick the model size that makes it lowest
You have 5.88 × 1023 FLOPs. A 280B model gets 350B tokens from that budget. A 70B model gets 1.4T tokens. You cannot train both to find out. Which one?
A scaling law is a formula fitted to many small training runs that predicts the loss of a bigger one. Hoffmann et al. trained over 400 models and fitted one. Call the loss L, in nats per token. Call the best loss any model could reach on this data E. Two more terms shrink as the model size N and the tokens D grow.
L of N and D equals E, plus A over N to the alpha, plus B over D to the beta.
Start at the floor E. Add a penalty for a small model, A divided by N to the alpha. Add a penalty for little data, B divided by D to the beta. The sum is the loss L.
280B on 350B tokens: L = 1.69 + 0.052 + 0.241 = 1.983. 70B on 1.4T tokens: L = 1.69 + 0.083 + 0.163 = 1.937. The smaller model wins by 0.046 nats per token.
Fix the budget and the two terms trade. Every N you pick sets D = C / (6N). Plot the loss against N and you get an IsoFLOP curve, a valley with one lowest point.
Pink is your model size. Teal marks the bottom of the valley. Raise the budget and watch the bottom move right.
The curve is the paper's fitted law (its third method). Its own minimum sits at more tokens per parameter than the paper's headline of about 20; the paper's three methods do not agree exactly.
Chinchilla, the 70B model, got 1.4T / 70B = 20 tokens per parameter. Labs often train past that point on purpose. Serving cost grows with N, so a smaller model on more tokens is cheaper to run for years. Llama 3 405B got 38.5 tokens per parameter.
Failure: the big run lands 0.05 nats above the forecast. Measure: refit the law without your largest small run, then predict that run. Fix: if the miss is large, fit on a wider range of sizes and report the forecast with a band.
Chapter 3
Count the bytes that training keeps for every parameter, then shard them until each GPU's share fits
You load a 70B model on one H100. The GPU has 80 GB of memory. The weights alone, at 2 bytes each, are 140 GB. Training needs far more than the weights.
Mixed-precision training with Adam keeps five things per parameter. The table counts the bytes, following the ZeRO paper.
| What | Format | Bytes per parameter |
|---|---|---|
| Weights for the forward and backward pass | BF16 | 2 |
| Gradients | BF16 | 2 |
| Master copy of the weights | FP32 | 4 |
| Adam momentum | FP32 | 4 |
| Adam variance | FP32 | 4 |
That is 16 bytes per parameter. A 70B model needs 1,120 GB before any activations. So you shard: each GPU keeps one slice of the state. Full sharding is called ZeRO stage 3, or FSDP in PyTorch. Call the memory per GPU M and the number of GPUs that share the model G.
M equals sixteen times N, over G.
Multiply the parameters N by 16 bytes. Divide by the GPUs G that share them. Compare M with the GPU's 80 GB, and leave room for activations.
70B over G = 16: M = 16 × 70e9 / 16 = 70 GB. It fits, with 10 GB left for activations. Over G = 128: M = 8.75 GB.
Each bar is one GPU's memory. The red line is 80 GB. Pick what to shard and how many GPUs share it.
Stage costs per parameter follow the ZeRO paper: none 16 bytes, optimizer 4 + 12/G, gradients too 2 + 14/G, weights too 16/G. Activations are left out and depend on batch and sequence length.
Activations are the layer outputs saved for the backward pass. Activation checkpointing saves only some of them and recomputes the rest. Recomputing every layer adds one forward pass: 8 FLOPs per parameter per token instead of 6.
Failure: the run is out of memory at step 1, but only on the longest sequences. Measure: log peak memory per step against sequence length. Fix: turn on activation checkpointing or cut the micro-batch.
Chapter 4
Choose what each GPU holds, and count the time a pipeline leaves idle
Sharding fits the state. It does not make one step faster. To use 16,384 GPUs, you split the work itself, in several ways at once. Llama 3 405B combined four: tensor, context, pipeline and data parallelism.
| Kind | Each GPU holds | What crosses the network | Where it fits |
|---|---|---|---|
| Data parallel (with sharding) | A slice of the batch | Gradients each step; weights too if sharded | Across nodes |
| Tensor parallel | A slice of each weight matrix | Activations in every layer | Inside one node, on the fast link |
| Pipeline parallel | A block of whole layers | Activations at stage borders | Across nodes |
| Expert parallel | Some experts of a mixture of experts | Tokens sent to their experts | MoE models |
A pipeline has a catch. Stage 2 cannot start until stage 1 hands it work. So you cut the batch into microbatches and stream them through. At the start and the end some stages still wait. That idle time is the bubble. Call the number of pipeline stages p and the number of microbatches m.
The bubble equals p minus one, over m plus p minus one.
Each stage works for m slots in the forward pass, but the pass lasts m + p − 1 slots. The idle part is p − 1 of them. The backward pass repeats the pattern, so the share is the same.
p = 8 and m = 8: bubble = 7 / 15 = 47%. Raise m to 32: bubble = 7 / 39 = 18%.
Each row is one stage. Teal is a forward pass, purple a backward pass, red an idle slot. Add microbatches and watch red shrink.
The GPipe schedule: all forwards, then all backwards, one slot each (a backward pass takes about two forward slots in real life). The bubble share does not depend on that ratio.
Failure: step time grows as you add nodes, though each GPU does less math. Measure: a profiler trace shows the gradient exchange after the backward pass, with compute stopped. Fix: start exchanging each layer's gradients while earlier layers still compute.
Chapter 5
Divide an operation's math by the bytes it moves, and learn whether a faster chip would help
A teammate's kernel runs at 3 TFLOPS on an H100 that can do 989. Is the kernel broken? Not if it spends its time waiting for memory. A GPU has two limits: how fast it does math, and how fast it reads memory.
Arithmetic intensity is the FLOPs an operation does for each byte it moves to or from memory. Call it I. Call the peak math speed F and the memory bandwidth B. The best speed you can reach is P.
P equals the minimum of F, and I times B.
Multiply the intensity I by the bandwidth B. That is how fast memory can feed the math. If it is below the peak F, memory is the limit. If not, math is.
Send one token through an 8,192 × 8,192 weight matrix. It does 2 × 8,1922 FLOPs and reads 2 × 8,1922 bytes of weights, so I ≈ 1. Then P = min(989.5, 1 × 3.35) = 3.35 TFLOPS. The chip is 0.3% busy.
The two limits meet at the ridge point: F / B = 989.5 / 3.35 = 295 FLOPs per byte. Batch b tokens through the same matrix and the weights are read once for all of them, so I grows almost as fast as b.
The slanted line is the memory limit, the flat line the math limit. Drag the batch and watch the dot climb to the ridge.
An 8,192 × 8,192 BF16 weight matrix; bytes count the weights plus the input and output tokens. H100 numbers from NVIDIA's page (dense BF16 is half the sparse figure).
Attention has the same problem. A plain kernel writes the full score matrix, one number per pair of tokens, to memory and reads it back. FlashAttention computes it in tiles inside the chip's fast on-chip memory, so the matrix never goes to main memory.
Failure: a layer-norm kernel reaches 2% of peak FLOPS. Measure: its achieved bandwidth. If it moves 2.9 TB/s of a possible 3.35, it is near its true limit. Fix: fuse it with the next operation, so the data is read once for both.
Chapter 6
Turn a cluster's failure rate into the checkpoint interval that wastes the least time
In a 54-day stretch of Llama 3 pretraining, the job stopped 466 times. 419 stops came as a surprise. The team traced about 78% of those to hardware faults. GPUs were the largest single cause. That is 7.8 surprise stops a day, one every 3.1 hours.
When a GPU fails, the whole job stops and restarts from the last checkpoint, a saved copy of the full training state. Save often and you spend time writing. Save rarely and each failure throws away more work. Call the time to write one checkpoint δ and the mean time between failures of the whole job M. The best interval between saves is τ.
Tau equals the square root of two times delta times M.
Multiply the write time δ by the failure gap M, double it, and take the square root. Saving every τ minutes balances the two losses: δ / τ spent writing, and τ / (2M) redone after a crash.
M = 3.1 hours = 185.6 minutes, and a made-up δ = 1 minute: τ = √(2 × 1 × 185.6) = 19.3 minutes. Waste = 1 / 19.3 + 19.3 / 371 = 5.2% + 5.2% = 10.4%.
The curve is total waste. Yellow is time spent writing, red is work redone after crashes. Drag your interval toward the bottom of the curve.
Failure gap scales from the Llama 3 rate (419 unexpected stops in 54 days on 16,384 GPUs), assuming failures grow with GPU count. Write times are illustrative. Restart time after each crash is left out.
The paper reports more than 90% effective training time despite those stops. Fast, frequent saves and automatic restarts made that possible.
Failure: after a restart, the loss jumps and takes hours to recover. Measure: compare the first batches after the restore with the batches the run saw before the crash. Fix: save the data loader's position and the random seeds in the checkpoint, not only the weights.
Chapter 7
Store many experts but run only a few per token, and count the compute and memory each costs
DeepSeek-V3 has 671B parameters. A dense model that size, on its 14.8T tokens, would need 6 × 671e9 × 14.8e12 = 6.0 × 1025 FLOPs. DeepSeek-V3 needed about 18 times less. Each token used only 37B of its parameters.
A mixture of experts (MoE) replaces each feed-forward block with many smaller blocks, the experts. A small router scores the experts for each token and sends the token to the top few. Every token uses the rest of the model (attention, embeddings): the shared part.
Call the shared parameters Ns and all expert parameters together Ne. Call the number of experts E and the experts each token uses k. The parameters one token touches are Nact.
N-act equals N-s, plus k over E, times N-e.
Take the share of experts a token uses, k over E. Multiply it by the expert parameters N-e. Add the shared parameters N-s. That is N-act, which sets the FLOPs. The total, N-s + N-e, sets the memory.
Mixtral has E = 8, k = 2, 47B parameters in total and 13B active. Solve the two: Ne = (47 − 13) × 4 / 3 = 45.3B, Ns = 1.7B. Check: 1.7 + (2 / 8) × 45.3 = 13.0B.
Each column is an expert; its height is the tokens it got. Add experts and memory grows; compute per token stays. Switch off balancing and watch a few experts take the load.
Expert size from Mixtral (5.7B per expert slot, 1.7B shared), scaled to more experts for illustration. The page simulates the routing of 400 toy tokens; the unbalanced router favours a few experts.
The memory bill does not shrink. All 671B parameters of DeepSeek-V3 must sit in GPU memory, so experts are spread over GPUs: expert parallelism from Chapter 4. Each token then crosses the network to its experts and back.
Failure: MFU drops and a few GPUs run hot while others idle. Measure: count tokens per expert per step. Fix: the router has collapsed onto a few experts. Add a load-balancing loss, or a per-expert bias as DeepSeek-V3 does.
Chapter 8
Store each weight in fewer bits, and see why that makes one user's answer arrive faster
One user chats with a 70B model. Each new token reads every weight once, and Chapter 5 showed that this is memory-bound. So the speed limit is the bytes of weights divided by the memory bandwidth. Fewer bytes per weight means more tokens per second.
Quantization stores each weight with fewer bits: 8 or 4 instead of 16. Call the parameters N, the bits per weight b, and the memory bandwidth of the GPUs that hold the model B. The top speed for one user is t.
t is at most B, over N times b over eight.
Multiply the parameters N by the bytes per weight. That is the data each token must read. Divide the bandwidth B by it. You cannot make tokens faster than that.
70B in BF16 is 140 GB, so it needs two H100s: t ≤ 6.7e12 / 140e9 = 48 tokens per second. In 4 bits it is 35 GB on one H100: t ≤ 3.35e12 / 35e9 = 96 tokens per second, on half the GPUs.
The price is precision. A 4-bit number has 16 levels. Quantization maps each weight to the nearest level on a grid between a group's smallest and largest value. One outlier stretches the grid, and every other weight in the group rounds worse.
Dots are 64 weights of one group; ticks are the levels. Lower the bits and watch the error grow. Add an outlier and see the grid stretch.
Toy weights from a seeded bell curve; the grid spans the group's minimum to maximum. Memory and speed use 70B parameters and the H100's 3.35 TB/s, at batch 1.
So real methods give each small group of weights, often 128, its own grid. They tune it on calibration data: sample inputs that show which weights matter (see Sparsity and Quantization).
Failure: a 4-bit model matches the original on average, but its maths scores fall hard. Measure: compare scores per task, not one average. Fix: the calibration set had no maths prompts. Add them, or keep the most sensitive layers in 8 bits.
Chapter 9
Count the memory each user's conversation holds, and see how many users fit on one server
Chapter 5 showed that one token at a time leaves an H100 0.3% busy. The fix is a big batch: many users' tokens through the same weights at once. What stops the batch from growing is memory, and the culprit is the KV cache.
To write each new token, attention looks back at every earlier token's keys and values. The server keeps them in memory for the whole conversation instead of recomputing them. Call the layers L, the key-value heads hkv and the size of each head dh. Call the tokens per conversation s, the conversations in the batch b and the bytes per number e.
KV equals two, times L, times h-k-v, times d-h, times s, times b, times e.
For one token, multiply 2 by the layers L, the heads h-k-v, the head size d-h and the bytes e. Multiply by the tokens s and the conversations b.
Llama 3 70B: 2 × 80 × 8 × 128 × 2 = 327,680 bytes per token. An 8,192-token conversation holds 2.7 GB. At 131,072 tokens it holds 42.9 GB.
The bar is a server of four H100s, 320 GB. Pink is the weights, purple the KV cache. Grow the context and the batch until it overflows.
Llama 3 70B shape from the paper's Table 3 (80 layers, width 8,192, 64 heads, 8 KV heads). BF16 weights, 140 GB; framework overhead is left out.
Serving engines such as vLLM store the cache in small pages, so memory is not reserved for tokens a user never writes (serving engines). Shared key-value heads cut the cache 8 times against one head pair per query head.
Failure: throughput collapses when users paste long documents. Measure: log KV cache use and the running batch size per second. Fix: cap the context per user, page the cache, or store it in 8 bits.
Chapter 10
Keep the terms, the numbers, the checklist and the code in one place
A frontier lab engineer turns a compute budget into a trained, tested and served model. Each skill on this page is one formula and one failure to watch for.
| Term | What it is | Worked number here |
|---|---|---|
| Training compute | C = 6ND | Llama 3 405B: 3.8 × 1025 FLOPs |
| Days | C / (GPUs × peak × MFU) | 66 days on 16,384 H100s at 41% |
| Scaling law | E + A / Nα + B / Dβ | 70B on 1.4T: 1.937; 280B on 350B: 1.983 |
| Training memory | 16 bytes per parameter, divided by G when sharded | 70B: 1,120 GB; over 16 GPUs, 70 GB |
| Pipeline bubble | (p − 1) / (m + p − 1) | p 8, m 32: 18% |
| Ridge point | Peak FLOPS / bandwidth | H100: 295 FLOPs per byte |
| Checkpoint interval | √(2 δ M) | 19.3 min for δ 1 min, M 3.1 h |
| Active parameters | Ns + (k / E) Ne | Mixtral: 1.7 + 45.3 / 4 = 13B of 47B |
| Decode speed limit | B / (N × b / 8) | 70B in 4 bits on one H100: 96 tokens/s |
| KV cache | 2 L hkv dh s b e | Llama 3 70B: 327,680 bytes per token |
pythonfrom math import sqrt PEAK, BW = 989.5e12, 3.35e12 # H100 dense BF16 FLOP/s, HBM bytes/s def train_days(N, D, gpus, mfu): return 6 * N * D / (gpus * PEAK * mfu) / 86400 def chinchilla_loss(N, D, E=1.69, A=406.4, B=410.7, a=0.34, b=0.28): return E + A / N**a + B / D**b def bubble(p, m): return (p - 1) / (m + p - 1) def attainable(intensity): return min(PEAK, intensity * BW) def ckpt_interval(write_min, mtbf_min): return sqrt(2 * write_min * mtbf_min) def active_params(shared, experts_total, k, E): return shared + k / E * experts_total def decode_tok_s(N, bits, bw=BW): return bw / (N * bits / 8) def kv_bytes(layers, kv_heads, d_head, seq, batch, nbytes=2): return 2 * layers * kv_heads * d_head * seq * batch * nbytes print(train_days(405e9, 15.6e12, 16384, 0.41)) # 66.0 print(chinchilla_loss(70e9, 1.4e12)) # 1.937 print(ckpt_interval(1, 54 * 24 * 60 / 419)) # 19.3 minutes print(decode_tok_s(70e9, 4)) # 95.7 tokens/s print(kv_bytes(80, 8, 128, 8192, 1) / 1e9) # 2.68 GB
Now press Present or Teach and explain each formula from memory, from 6ND to the KV cache. Then go back to the hero and say why the 70B plan beats the 280B plan.