Training Stability › Loss Spike Detection and Recovery

Michael BrenndoerferJanuary 28, 20265 min read
Language AI HandbookAll chapters
Part 32 of 67Training Optimization
Chapter 237Training Stability › Loss Spike Detection and Recovery
Reading section 5 of 15 · Loss Spike Detection and Recovery · 5 minSection 5 of 15 · 5 min

Book contents

Section 5 of 15 · 5 min read

Training Stability › Loss Spike Detection and RecoveryOne section · Sections in this chapter5 / 15 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Gradient norm monitoring helps you anticipate problems, but loss spikes still occur in practice. Knowing how to detect them reliably and recover efficiently is essential for long training runs.

Detecting Loss Spikes

A loss spike is a sudden increase in training loss that is substantially larger than normal batch-to-batch variance. The challenge is distinguishing a damaging spike from ordinary noise. A reliable detection approach uses a rolling baseline:

spike detected if: Lt>μrecent+k⋅σrecent\text{spike detected if: } \mathcal{L}_t > \mu_{\text{recent}} + k \cdot \sigma_{\text{recent}}

where μrecent\mu_{\text{recent}} and σrecent\sigma_{\text{recent}} are the mean and standard deviation of loss over the last WW steps, and kk is a sensitivity threshold (typically 3-5).

This formulation is essentially a statistical process control chart adapted for training monitoring. The window WW should be long enough to capture a stable baseline (50-200 steps is typical) but short enough to adapt to the gradual downward trend in loss as training progresses. If WW is too short, the baseline is noisy and you get false positives. If WW is too long, the baseline does not adapt and you miss spikes during the early high-loss phase.

The threshold kk balances sensitivity against false alarms. Using k=3k=3 gives a threshold at 3 standard deviations, which under a normal distribution would trigger on about 0.1% of steps by chance. For a training run of 100,000 steps, that is still 100 false alarms. In practice, k=4k=4 or k=5k=5 works better for long runs.

In[12]:
Code
def detect_loss_spikes(loss_history, window=50, threshold_k=3.5):
    """Detect loss spikes using rolling statistics.

    Returns a list of (step, loss_value) for detected spikes.
    """
    spikes = []
    for i in range(window, len(loss_history)):
        recent = loss_history[i - window : i]
        mu = np.mean(recent)
        sigma = np.std(recent)
        if sigma > 0 and loss_history[i] > mu + threshold_k * sigma:
            spikes.append((i, loss_history[i]))
    return spikes


# Simulate training loss with injected spikes
np.random.seed(42)
n_steps = 300
base_loss = 2.0 * np.exp(-np.linspace(0, 3, n_steps)) + 0.3
noise = np.random.normal(0, 0.05, n_steps)
simulated_loss = base_loss + noise

# Inject realistic loss spikes
spike_configs = [(100, 3.5), (180, 2.8), (240, 4.1)]
for step_idx, magnitude in spike_configs:
    simulated_loss[step_idx] *= magnitude
    simulated_loss[step_idx + 1] *= 1.8  # Partial recovery lag

detected_spikes = detect_loss_spikes(simulated_loss.tolist())
Out[13]:
Console
Total training steps: 300
Detected spike events: 3
  Step 100: loss=3.369 (baseline=1.258, ratio=2.7x)
  Step 180: loss=1.848 (baseline=0.734, ratio=2.5x)
  Step 240: loss=1.805 (baseline=0.541, ratio=3.3x)
Out[14]:
Visualization
Line plot of training loss over 300 steps with red triangle markers indicating detected spike events at steps 100, 180, and 240.
Training loss trajectory with automated spike detection using a rolling 50-step window and 3.5-sigma threshold. Detected spikes (marked with red triangles) correspond to sudden loss increases that exceed the expected variance band (orange shaded region), cleanly distinguishing genuine instabilities from normal batch-to-batch fluctuations. The rolling mean (orange line) adapts to the downward trend in loss without being distorted by the spikes themselves.

Recovery Strategies

When a loss spike occurs, the default behavior of most training scripts is to continue training, hoping the optimizer self-corrects. For small spikes this often works. For large spikes (loss returning to near-initialization levels), it usually does not, because the optimizer's momentum state has been corrupted.

The most reliable recovery approach is checkpoint rollback with optimizer state reset:

  1. Maintain checkpoints at regular intervals (every 500-1000 steps is typical for large models).
  2. When a spike is detected, roll back to the most recent checkpoint before the spike.
  3. Optionally skip or re-weight the batch that triggered the spike.
  4. Consider reducing the learning rate slightly (10-20%) before resuming, to allow gradual re-entry into the training trajectory.

The reason optimizer state matters here is subtle but important. Rolling back model parameters without also rolling back optimizer state (momentum buffers) means the optimizer has incorrect expectations about the loss landscape geometry. The momentum carries the "memory" of a corrupted gradient signal, and this can cause additional instability immediately after recovery. Always checkpoint and restore both model parameters and optimizer state together.

A more sophisticated recovery strategy used in some large-scale training runs involves partial rollback: instead of reverting to the most recent checkpoint, you revert only the parameters of the layers that showed the largest gradient norms during the spike. This preserves progress made in stable parts of the network while resetting the layers that were most affected by the instability. This approach requires per-layer gradient monitoring infrastructure but can save significant compute when spikes happen late in training.