Every Layer← All lessons

Models

A training run at scale

Compute, memory, and the work split across thousands of GPUs

The lessons How wrong, and which way and Every weight moves a little took one training step apart. This lesson multiplies it. A model of this site's size, 32.8 billion weights, trained on Qwen3's 36 trillion tokens needs about 7.1 × 10²⁴ arithmetic operations and holds 524 GB of training state before a single activation is stored. Neither fits one GPU, in time or in memory. The lesson covers how a run is forecast before it is paid for, how compute is counted, how much text a model should see, why 16 bytes are held for every weight, and the four ways the work is split across GPUs: data, sharding, tensor and pipeline parallelism. It ends with what Llama 3's run on 16,384 GPUs looked like, failures included.

Chapter 1 of 12

A forecast before the money is spent

Small runs, a straight line, and a prediction for a large one

A large run costs millions in GPU time, so nobody starts one to see what happens. In 2020 a team at OpenAI, Kaplan and colleagues, trained many language models of different sizes on different amounts of data with different amounts of compute, and found that the final loss followed smooth power laws in each of the three, with some trends holding over more than seven orders of magnitude. A power law is a straight line when both axes are drawn on a logarithmic scale. That is what makes it useful: train a set of small, cheap models, plot their losses, extend the line, and read off what a much larger model should reach.

The paper gives the law for model size as the loss equals a constant divided by the number of weights, all raised to the power 0.076. The exponent is small, which is why models must grow so much to improve. Doubling the weights multiplies the loss by 2 to the power minus 0.076, which is 0.949, about 5 percent lower. Ten times the weights gives 0.839, about 16 percent lower.

The Llama 3 team used the same method, with its own law, to size its largest model. It trained models with compute budgets between 6 × 10¹⁸ and 10²² operations, found at each budget the token count that gave the lowest loss, fitted a power law of that best token count against compute, and extended it to its budget of 3.8 × 10²⁵, almost 4,000 times the largest experiment. The forecast was a model of 402 billion weights trained on 16.55 trillion tokens. The model it trained has 405 billion weights and saw 15.6 trillion tokens.

Chapter 2 of 12

Counting the arithmetic

Six operations per weight per token, and where the six come from

Compute is counted in floating-point operations, FLOPs, where one FLOP is one multiplication or one addition. The rule of thumb that runs through every training budget is that training costs about 6 FLOPs per weight per token. Here is where the 6 comes from.

In the forward pass each weight takes part in one multiply and one add for every token: the input number is multiplied by the weight and the product is added to a running sum. That is 2 FLOPs per weight per token. For this model one token's forward pass costs about 2 × 32.76 billion, which is 65.5 billion FLOPs. The backward pass does about twice that work, because each layer has to produce two things: the slopes for its own weights and the slopes to hand back to the layer below. Each of those is a matrix multiply as large as the forward one, so the backward pass is about 4 FLOPs per weight per token. Forward plus backward is 6.

Multiply by the tokens and the rule becomes compute equals 6 × weights × tokens. GPT-3 had 175 billion weights and saw 300 billion tokens: 6 × 175 × 10⁹ × 300 × 10⁹ is 3.15 × 10²³ FLOPs, roughly a 3 followed by 23 zeros. For this site's model on Qwen3's 36 trillion tokens it is 7.1 × 10²⁴.

The rule leaves out attention's comparisons between tokens, which grow with the window. Kaplan's paper gives that extra forward cost per token as 2 × layers × window × attention width. For this model at 4,096 tokens that is 4.3 billion FLOPs, about 6.6 percent on top of 65.5 billion.

Chapter 3 of 12

A big model, or more text

The same compute buys either, and for years the wrong one was bought

With a fixed budget of compute, the formula 6 × weights × tokens leaves one choice open: more weights and fewer tokens, or fewer weights and more tokens. The early scaling work read its results as saying that size mattered most, and for a while the industry made models bigger and bigger. GPT-3 had 175 billion weights and was trained on 300 billion tokens, fewer than 2 tokens for every weight.

In 2022 a team at DeepMind, Hoffmann and colleagues, went back to the question. They trained over 400 language models, from 70 million to over 16 billion weights, on 5 to 500 billion tokens, and found that for compute-optimal training the model size and the number of tokens should grow together: every doubling of model size should come with a doubling of the tokens. Measured that way, most large models of the time were under-trained. They were too big for the text they had seen.

To test it they trained a model called Chinchilla with 70 billion weights on 1.4 trillion tokens, about the same compute as their earlier model Gopher, which had 280 billion weights trained on 300 billion tokens. The arithmetic is close: 6 × 70 billion × 1.4 trillion is 5.9 × 10²³, and 6 × 280 billion × 300 billion is 5.0 × 10²³. Chinchilla was four times smaller and outperformed Gopher across a wide range of tasks. A smaller model is also cheaper every time it runs afterwards, which for a model that serves many requests can matter more than the training.

Chapter 4 of 12

Twenty tokens per weight, and going far past it

A rule for the cheapest training, and why models are trained longer anyway

The rule of thumb that came out of Chinchilla is about 20 tokens of text for every weight, if the goal is the best model for a fixed training budget. Chinchilla itself, 70 billion weights on 1.4 trillion tokens, is exactly that ratio. For this site's model the rule would ask for 20 × 32.76 billion, about 655 billion tokens.

Qwen3 was trained on 36 trillion tokens, about 1,100 tokens per weight for a model of this size, more than 50 times the rule. Llama 3's model with 8 billion weights was trained on over 15 trillion tokens, close to 1,900 per weight and almost 100 times the rule. This is sometimes called over-training, and it is done on purpose.

The reason is that the Chinchilla rule minimises the cost of training and ignores everything after it. A model that is trained once is then served for months to millions of requests, and the cost of serving grows with its size: each generated token costs about 2 FLOPs per weight, and the weights must be read from GPU memory for every pass. The loss keeps improving with more text, only more slowly. So a team can spend extra compute once, in training, to get a smaller model that does as well as a larger one, and then save on every request it ever serves.

This site's factory serves the model from its official FP8 build, Qwen3-32B-FP8, which is 34.3 GB of files. Most weights take 1 byte each there, and some layers stay at higher precision. Chinchilla's own fitted loss formula, extended well past the models it was fitted on, gives the size a model trained by the Chinchilla rule would need to do as well: about 225 billion weights on 4.5 trillion tokens, about 7 times the size. A model 4 times the size trained by the rule would still come out worse. The 7-times model would need about 7 times the memory and about 7 times the compute for every token served.

Chapter 5 of 12

Time on one GPU, and on thousands

Centuries become weeks only by splitting the work

Memory is one wall a run hits. Time is the other. A GPU's arithmetic rate is quoted as a peak, and real training reaches a fraction of it. The share actually achieved is called the model FLOPs utilisation, MFU: the FLOPs the training formula says were needed, divided by the time taken and the GPU's peak. Llama 3 reports 380 to 430 trillion FLOPs per second per H100 in bfloat16, an MFU of 38 to 43 percent.

Take 400 trillion FLOPs per second as a sustained rate for this site's model. Its 7.1 × 10²⁴ FLOPs would take 1.77 × 10¹⁰ seconds on one GPU, which is about 561 years. On the 8 GPUs of the northlight-eu cluster it would take about 70 years. On 4,096 GPUs it would take about 50 days. Llama 3 405B, at 3.8 × 10²⁵ FLOPs, would take about 3,000 years on one GPU at the same rate, and about 67 days on 16,384.

These figures assume that every GPU computes all the time, and that is the hard part. Dividing a training step across thousands of GPUs means they must exchange slopes, weights and activations on every step. Any time spent waiting for those transfers is time the GPUs are paid for and do not compute. That is why training clusters are built with networks of the highest bandwidth available, and why the lesson The network is part of the computer treats the network as part of the machine. The rest of this lesson is about the ways the work is divided, and what each one has to send.

Chapter 6 of 12

Sixteen bytes for every weight

Mixed precision, and why a 32-bit copy is still kept

Serving a model needs its weights and little else. At 16 bits, 2 bytes, a model of 7 billion weights takes 14 GB, and this site's model takes 65.5 GB in its BF16 build. The FP8 build the factory serves is 34.3 GB. Training holds much more. Most of its arithmetic runs in a 16-bit format called bfloat16, which keeps the 8 exponent bits of a 32-bit float and only 7 bits of fraction. GPUs process 16-bit numbers much faster and they take half the memory, so the forward and backward passes use a 2-byte copy of every weight.

The problem is precision. Near 1.0 the next number bfloat16 can represent is 1.0078125, so the gap is about 0.0078. Add an update of 0.001 to a weight of 1.0 and the result rounds back to exactly 1.0. The update is lost. What decides it is the update's size relative to the weight: in bfloat16 an update 256 or more times smaller than its weight can be lost, and one more than 512 times smaller always is. An optimizer step is usually about one learning rate in size, often 0.0001 or less, so in bfloat16 it rounds away on every weight of about 0.03 or larger, and on smaller weights it is rounded coarsely. The mixed precision paper by Micikevicius and colleagues showed the same effect in the older FP16 format: an update 2,048 or more times smaller than its weight can become zero, and one more than 4,096 times smaller always does. The fix is to keep a 32-bit master copy of every weight, which takes the updates, and to make a fresh 16-bit copy from it for each step.

Now add it up. The 16-bit working copy is 2 bytes. The 32-bit master copy is 4. The slope is 2. AdamW's two averages are 4 bytes each, 8 together. That is 16 bytes per weight. For 7 billion weights it is 112 GB, for this model 524 GB, for 70 billion 1,120 GB. One H100 has 80 GB.

Chapter 7 of 12

The results kept for the backward pass

Activations, and trading compute for memory

The 16 bytes per weight do not depend on how much text is in flight. The activations do. As the lesson How wrong, and which way showed, the backward pass needs the intermediate results of the forward pass, so every block keeps its results until the backward pass has used them. For one window of 4,096 tokens, one block's output is 4,096 × 5,120 numbers, 41.9 MB at 2 bytes. Inside the feed-forward layer the intermediate result is 4,096 × 25,600 numbers, 209.7 MB. A block keeps several results of each size, attention keeps more, and there are 64 blocks. For a model of this size the activations of a handful of windows can exceed the weights themselves.

The common answer is activation checkpointing, also called gradient checkpointing. Only some results are kept, typically the input of each block, and everything inside a block is computed again during the backward pass, just before it is needed. Keeping one input of 41.9 MB per block for 64 blocks takes 2.68 GB per window. The price is a second forward pass through each block during the backward pass: the step costs about 4 forward passes of work where it cost 3, a third more compute.

The trade is usually worth it, because memory decides how many windows fit on a GPU at once, and more windows in flight keep the GPU busier. It is common enough that the fine-tuning library TRL turns it on by default: its SFTConfig sets gradient_checkpointing to True. When a training job runs out of GPU memory, activations are the first thing to look at, because they grow with the window length and the number of windows.

Chapter 8 of 12

Data parallel: one model, many slices of the batch

Every GPU holds a full copy, and the slopes are averaged

The simplest way to use many GPUs is data parallelism. Every GPU holds a full copy of the model. The batch is cut into slices, one per GPU, and each GPU runs the forward and backward pass on its own slice and gets its own slopes. Then all the GPUs exchange their slopes and average them, an operation called an all-reduce. After it every GPU holds the same averaged slopes, so every GPU makes the same update and the copies stay identical. Averaging slices gives the same result as one large batch, for the same reason that gradient accumulation does.

The all-reduce moves a lot of data. The usual ring algorithm has each GPU send and receive about twice the size of the slopes. For this model the slopes are 32.8 billion numbers at 2 bytes, 65.5 GB, so each GPU sends about 131 GB on every step. Over a link of 400 gigabits per second, which is 50 GB per second, that is 2.6 seconds per step if nothing overlaps it. PyTorch's DistributedDataParallel reduces the wait by grouping slopes into buckets, 25 MiB by default, and starting the all-reduce of a bucket while the backward pass is still computing the next.

Data parallelism is good for speed. It does nothing for memory: every GPU still holds all 16 bytes of every weight, 524 GB for this model. On its own it can't train this model on any GPU that exists. It needs the next idea.

Chapter 9 of 12

Sharding: ZeRO and FSDP

Stop every GPU from holding the same 16 bytes

Data parallelism wastes memory: with 8 GPUs, 8 identical copies of the optimizer state, the slopes and the weights sit in 8 places. The ZeRO paper by Rajbhandari and colleagues at Microsoft removes that redundancy in three stages. Write Ψ for the number of weights and Nd for the number of GPUs. Plain data parallelism holds 16Ψ bytes on every GPU: 2Ψ of 16-bit weights, 2Ψ of 16-bit slopes and 12Ψ of optimizer state, which ZeRO counts as the 32-bit master copy and Adam's two averages.

Stage 1 splits the optimizer state, so each GPU holds 12Ψ ÷ Nd of it and updates only its share of the weights. Stage 2 splits the slopes as well: each GPU only needs the averaged slopes for its own share, so the all-reduce becomes a reduce-scatter. Stage 3 splits the 16-bit weights too. Each GPU stores a slice, and before a layer runs its full weights are gathered from all GPUs just in time, then dropped again after use. Stages 1 and 2 send the same volume as plain data parallelism, 2Ψ. Stage 3 sends 3Ψ, half as much again: the weights are gathered for the forward pass and again for the backward pass, Ψ each, and the slopes are then reduce-scattered, one more Ψ.

For this model on 8 GPUs, stage 1 needs 180 GB per GPU, stage 2 needs 123 GB, and stage 3 needs 65.5 GB, which fits an 80 GB H100 with roughly 15 to 20 GB to spare. The range is there because this lesson counts decimal gigabytes while the H100's 80 are binary ones, about 85.5 billion usable bytes. That spare room must hold the activations, the full layers gathered during each step and the communication buffers. On 64 GPUs stage 3 needs 8.2 GB each. PyTorch implements the same idea as Fully Sharded Data Parallel, FSDP. Its FULL_SHARD strategy is stage 3, and SHARD_GRAD_OP corresponds to stage 2.

Chapter 10 of 12

Splitting the layers: tensor and pipeline parallelism

One matrix across GPUs, or groups of layers in a line

Sharding stores less on each GPU, and each GPU still computes whole layers. For the largest models the layers themselves are split. Tensor parallelism splits one matrix multiplication across GPUs. Take this model's attention with 4 GPUs in one server. It has 64 query heads and 8 key-value heads, so each GPU takes 16 query heads and 2 key-value heads and computes them on its own. The feed-forward layer's inner width of 25,600 is cut into 4 slices of 6,400, and each GPU multiplies by its slice. The partial results are then added across the 4 GPUs, an all-reduce that happens inside every block, twice per block in the forward pass. That is far too much traffic for the network between servers, so tensor parallelism is kept inside a server, where the GPUs are joined by NVLink.

Pipeline parallelism splits the model by depth. With 4 stages the 64 blocks become 4 groups of 16, each on its own GPU, and the output of one group is passed to the next like work on an assembly line. Only the activations at the boundaries cross the network, so pipeline stages can sit in different servers.

An assembly line has a startup cost. The first stage starts alone and the last stage waits until the first micro-batch reaches it. With p stages and m micro-batches the line takes m + p - 1 time slots and each stage is busy for m of them, so the idle share, called the bubble, is (p - 1) ÷ (m + p - 1). With 4 stages and 8 micro-batches that is 3 ÷ 11, 27 percent. With 32 micro-batches it falls to 3 ÷ 35, under 9 percent.

Chapter 11 of 12

All four at once: Llama 3 405B

Every GPU has a position in a four-dimensional grid

Real runs combine the methods, and the Llama 3 report gives a complete example. It calls its layout 4D parallelism: tensor parallelism, TP, context parallelism, CP, pipeline parallelism, PP, and data parallelism, DP, implemented with FSDP. Context parallelism is the one this lesson has not met yet. It divides a very long input window into segments on different GPUs, which reduces the memory needed for very long sequences.

The GPUs are arranged in the order TP, CP, PP, DP, and the order follows the network. Tensor parallelism has the most traffic and the strictest timing, so its 8 GPUs are the 8 GPUs of one server, joined by NVLink. Pipeline stages pass only boundary activations and can span servers. Data parallelism exchanges its slopes once per step and can overlap them with computation, so it spans the most distant GPUs.

On 16,384 GPUs with windows of 8,192 tokens the report gives TP 8, CP 1, PP 16 and DP 128, and 8 × 1 × 16 × 128 is 16,384. Each data-parallel group handles 16 windows per step, so a batch is 128 × 16 = 2,048 windows of 8,192 tokens, 16.8 million tokens. In the long-context phase, with windows of 131,072 tokens, CP becomes 16 and DP drops to 8. The FSDP in this run shards optimizer state and slopes, and keeps the model's weights unsharded after the forward pass to avoid gathering them again for the backward pass. Throughput was 380 to 430 trillion FLOPs per second per GPU. Every one of these choices is a trade between memory, arithmetic and what the network can carry.

Chapter 12 of 12

When hardware fails

At this scale something breaks every few hours

Thousands of GPUs running for weeks means failures are routine. The Llama 3 report gives the record of a 54-day stretch of pre-training. There were 466 job interruptions. 47 were planned, such as firmware upgrades or dataset updates. The other 419 were unexpected, about 7.8 a day, one every three hours or so. About 78 percent of the unexpected ones were confirmed or suspected hardware problems, and GPU issues alone were 58.7 percent of all unexpected interruptions. Despite that, significant manual intervention was needed only three times, and the team kept effective training time above 90 percent.

When one GPU fails, the whole synchronous job stops, because every step waits for every GPU. The job restarts from the latest checkpoint on healthy hardware. So checkpoints decide how much work a failure costs. A checkpoint for this model holds the 32-bit weights and AdamW's two averages: 12 bytes per weight, 393 GB. With 64-way sharding each GPU writes only its own slice, 6.1 GB, in parallel with the others, which is what makes frequent checkpoints affordable.

The interval is a trade. Saving takes time during which no training happens. Not saving means that a failure throws away everything since the last save. With a failure every three hours and a checkpoint every 30 minutes, a failure loses 15 minutes of work on average, plus the time to restart. That is why the Llama 3 team also worked on shortening job startup and checkpoint time, and used PyTorch's NCCL flight recorder to find which GPU or collective had stalled.

What to keep

Compute, memory and the network

A run is sized before it starts, from scaling laws fitted to small runs. Its compute is about 6 FLOPs per weight per token, which for this site's model on 36 trillion tokens is 7.1 × 10²⁴, centuries on one GPU and weeks on thousands. Chinchilla showed that weights and tokens should grow together, about 20 tokens per weight for the cheapest training, and models are now trained far past that because serving a smaller model saves money on every request. Training holds 16 bytes per weight in mixed precision, plus activations that checkpointing can trade for recomputation. Data parallelism adds speed, ZeRO and FSDP remove the copies, and tensor, pipeline and context parallelism split the model itself. On thousands of GPUs, failures are routine and checkpoints decide what they cost.