Skip to content

17.5 Training failures, profiling, and recovery

A training run is a stateful computation over a data stream. Diagnose failures by locating which state or assumption changed: data, arithmetic, optimization, distribution, or infrastructure. Record the first abnormal step rather than only the final failed checkpoint.

Concepts from other sections (9)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Learning objectives

  • Connect training symptoms to discriminating checks.
  • Save enough state to resume the intended optimization.
  • Separate useful throughput from apparent utilization.
SymptomCandidate mechanismFirst discriminating check
Nonfinite loss/gradientsFP16 overflow, invalid log/division, bad data, extreme updateInspect inputs and activations at the first nonfinite operation; compare higher precision on the same batch
Loss spikes at regular intervalsData boundary, schedule reset, resumed optimizer mismatchAlign spikes with shard IDs, LR, gradient norm, and checkpoint events
Training improves, validation worsensOverfitting, leakage in validation setup, shift, forgettingEvaluate frozen task slices and the previous checkpoint on identical examples
No learningMasked-out targets, frozen intended parameters, zero LR, detached graphCheck valid-token count, nonzero gradients, and parameter deltas on one batch
OOM in backwardSaved activations, long outlier, logits, gathered parametersProfile memory by phase and sequence length; test recomputation or smaller token micro-batches
OOM at optimizer stepMoment/state allocation or optimizer temporariesObserve the first optimizer step separately from forward/backward
Low device utilizationInput stalls, tiny kernels, synchronization, rank imbalanceTrace data wait, host launches, collectives, and per-rank step time
Distributed hangInconsistent collective order, rank failure, uneven data iterationCompare the last completed operation and input count on every rank
Reward rises, task success fallsReward exploitation or changing denominatorRe-run independent checks and inspect excluded/truncated samples

Batch-specific reproduction is valuable: preserve example IDs, token IDs, masks, length, precision, and the preceding optimizer state. Replaying only model weights may miss optimizer-driven instability. Avoid treating every spike as a hardware failure or every NaN as a reason to lower learning rate.

Learning rate, warmup, decay schedule, weight decay, gradient clipping, optimizer moments, effective batch, precision, loss scale, recomputation, and data mixture are separate controls. Change one mechanism at a time on a short reproducible run. BF16’s exponent range reduces one overflow risk but does not guarantee stable losses. FP16 loss scaling protects small gradients; gradients should be unscaled before norm clipping and overflowed updates should follow the recipe’s skip/schedule convention.

Catastrophic forgetting is a regression on previously learned tasks after domain adaptation. Compare a domain suite and a retained general suite. Mixing replay data, reducing update magnitude, and parameter-efficient adaptation are candidate interventions; none guarantees preservation. Distribution shift is broader: even a stable training run can fail when production inputs or tool behavior change.

For data issues, check train/test contamination, near duplicates, bad special tokens, repeated documents, mislabeled answers, and unintended mixture weights. Packed examples need correct boundaries and a declared attention policy. Logs should distinguish observed tokens, valid loss tokens, and examples dropped for length or quality.

StateWhy weights alone are insufficient
Model and any adaptersDefines the predictor and trainable parameterization
Optimizer moments and master parametersDetermines the next update, especially for Adam and mixed precision
LR schedule, update/token counters, loss scalerPrevents accidental schedule restart or scale reset
Data sampler/cursor and mixture statePrevents silent replay or omission of examples
Random-generator state per relevant process/deviceReproduces sampling, dropout, and data ordering within the supported environment
Configuration, tokenizer, template, dataset and code revisionsEstablishes what the saved state means
Distributed shard metadataAllows the loader to reconstruct state on a supported layout

Publish a checkpoint as complete only after every required shard is durable and metadata identifies the completed set. A partially written latest directory is not a valid recovery point. Keep a previous known-complete checkpoint until the new one is verified. Asynchronous save requires a consistent snapshot of the intended step, not tensors that continue mutating during serialization.

PyTorch distributed checkpoint documentation describes one sharded-save implementation and its constraints. Resharding support does not automatically guarantee bitwise continuation after changing world size, data order, kernels, or batch grouping.

Run 20 updates with a fixed environment and record per-step losses and final state. Repeat from the same initial state, save after update 10, stop, restore, and run updates 11–20. Compare consumed example IDs, schedule values, optimizer state, losses, and parameters under the stated equality criterion. Repeat with a save interrupted before completion: recovery should select the previous complete checkpoint rather than accepting missing shards.

Changing only the topology is a separate experiment. Recompute global batch and token weighting, confirm the loader’s supported resharding, and define a numerical rather than bitwise criterion if reduction order changes. A successful inference from the checkpoint proves neither optimizer restoration nor training continuation.

Include data loading, evaluation, checkpointing, compilation, and restarts in end-to-end wall time. Report cold-start cost separately from steady state. If a kernel is faster but the input pipeline dominates, end-to-end throughput may barely change. At scale, the slowest rank determines a synchronized step, so collect per-rank distributions rather than one average utilization value.

  • Recovery point: a complete, durable state from which the declared training process can resume.
  • Gradient norm: a measure of update magnitude before optimizer-specific scaling.
  • Forgetting: loss of previously measured capabilities during later adaptation.

Reproduce the first failure, inspect the relevant state, and verify continuation with a deliberate restart. Quality, stability, and useful wall-clock throughput are separate measurements.

  1. Why might restored weights give the same first loss but different later losses?

    AnswerThe optimizer, LR schedule, data cursor, loss scale, or RNG state may not have been restored. Those affect subsequent updates.
  2. Is a lower peak allocated-memory measurement enough to promise a larger production batch?

    AnswerNo. Include reserved/temporary memory, the longest intended sequences, evaluation and checkpoint phases, and the actual distributed layout.