The two weather reports are packed without letting one predict the start of the other. That leaves eight targets. If we process each report separately to save memory, should each backward pass count for half an update? Not if we want each token to count equally.

First, understand one small step on one device

In one lineGet one update right on one device: two sequences give 1.25 by token mean and 1.5 by sequence mean. Fix target, mask and denominator before speed.

Take rain stops EOS and wind from the west at noon EOS. In each report the first word supplies context and EOS is the last target. Suppose the summed losses are 4 on the short report's two targets and 6 on the long report's six. Those are example losses, not values we can infer from the text.

The token-weighted mean is $(4+6)/(2+6)=1.25$. Averaging the two sequence means instead gives $(2+1)/2=1.5$. The data did not change, but each token's weight in the objective did. Gradient accumulation, distributed training, and padding all bring this small issue back.

Imagine putting one report in each microbatch. If the backward pass uses each report's mean loss, weight those means by 2/8 and 6/8 before accumulating the gradients. If it uses sums, divide both by the same total of eight targets. Either way, take one optimizer step after both backward passes. Equal weights of 1/2 would instead train for the average report. One training step shows where backward ends and the optimizer step begins.

Need to retrace a step? Next-token prediction shows where the labels come from; the packing example shows why we counted eight rather than nine. To write your first loop, start with the two-example PyTorch update.

For a different objective, multi-token prediction adds targets farther ahead. Check which heads would look past the end of a report and whether the inputs reveal their targets. For unstable gradients or poor validation results, see initialization, optimizers, or training diagnosis.

Then consider precision, memory, and multiple devices

In one lineBF16 changes number format, recomputation changes which activations are saved, and FSDP and TP split different parts of the workload.

These interventions act at different levels. BF16 changes number representation; recomputation changes stored intermediates; FSDP changes where state lives; TP partitions a matrix computation. All can affect memory, but they are not interchangeable switches.

Troubleshooting: NaNs, memory and multi-GPU runs
Symptom Check first Continue with
Inf / NaN loss Numeric range, scaling, and clipping order Precision and memory
Weights fit but backward runs out of memory Activations, optimizer state, temporary tensors, sequence length Precision and memory
Results change with device count Data partitioning, valid-token counts, synchronization, randomness Distributed training
High utilization but poor throughput Useful-token throughput, recomputation, communication, waiting Distributed training · How MFU is computed

On a first pass, the overview's distinction between DDP, FSDP, TP, and PP is enough. For the mechanics inside a layer, continue with two GPUs computing one FFN: follow the calculation from [2, −1] to [9, 6], then trace the input gradient to see where communication happens. Framework configuration names can wait.

Once data and model parallelism are clear, read DDP, FSDP2, and training stacks. Follow parameter gathering and gradient updates to see what sharding saves and why it can take more time. The note also explains the responsibilities of Trainer, Accelerate, NeMo RL, and generation engines.

Chapter contents

In one lineThree groups: objective, data pipeline, one training step and multi-token prediction; then precision, memory and many GPUs; then scaling laws.

01

Pretraining & training mechanics

19 notes

Connect the objective, data, and one batch to precision, memory, and distributed execution.

Training basics5

Start with a linear layer's gradients, then study generalization, activations, initialization, and optimizer updates.

  1. Matrix gradients & training systems
  2. Generalization & diagnosis
  3. Activations & initialization
  4. SGD to AdamW
  5. Muon: updating a matrix
Pretraining5

Establish the prediction target, then follow the data into a batch and a parameter update.

  1. Training route
  2. LM objective
  3. Pretraining pipeline
  4. One training step
  5. Multi-token prediction
Many GPUs4

After one training step, separate numeric formats, stored state, recomputation, and multi-device partitioning.

  1. Precision & memory
  2. Distributed training
  3. Tensor parallelism, worked through
  4. DDP, FSDP2 & training stacks
Scaling laws5
  1. Overview
  2. Compute-optimal size and data
  3. Training past Chinchilla
  4. LR, batch size & schedule
  5. Review

Put it to use

In one lineRun one update on a fixed tiny batch, change one condition at a time, and check outputs, gradients and the update agree before scaling up.

Run forward, backward, and one update on a fixed tiny batch and save the parameter changes. Change only one condition, such as accumulation steps or precision. Compare outputs, gradients, and the update within appropriate tolerances before scaling up.

Worked numbers test mechanisms; they are not hardware benchmarks. Report model version, sequence length, effective batch, dtype, hardware, and timing boundaries for actual memory or speed measurements.

Continue with SFT and post-training, or turn to generation and inference.