A DPO run spikes to NaN around step 400 after a clean start: explain the most likely cause
A DPO run trains cleanly for the first few hundred steps, then training loss spikes to NaN around step 400 and never recovers. Diagnose the most likely cause and prescribe three concrete fixes a practitioner would try, in order.
The DPO logit term diverges on a pathological pair, beta amplifies it, fp16 overflows. Fix order: lower beta, add gradient clipping, switch to bf16; then audit the dataset.
Picture two students taking a quiz where they compare answers. Usually the teacher gently nudges them when one answer is clearly better. But on a quiz where the two answers are nearly identical (or the label is just wrong about which is better), the teacher's nudge becomes a violent shove because the rule says the more confident you are about a comparison, the harder you push. If the teacher is also stingy with precision (can only express scores between very narrow bounds), one bad shove can break the entire scorekeeping system and the whole class falls apart. The first fix is to make the teacher less aggressive, the second is to cap how hard any single shove can be, and the third is to give the teacher a more flexible scorekeeping format.
Detailed answer & concept explanation~8 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 min: the DPO loss term that diverges, the beta amplifier, why fp16 overflows while bf16 does not, the three ordered fixes, NaN propagation through AdamW state, and the deeper preference-dataset audit.
| Symptom timing | Likely cause | First fix |
|---|---|---|
| Flat from step 0 | Structural break (LR, wrapping, labels) | Trace optimization path |
| Clean start, NaN mid-run (this case) | DPO logit divergence + fp16 | Lower beta, clip, bf16 |
| Loss drops then plateaus | Capacity or convergence | Check eval; bump rank if eval flat |
| Train down, eval up | Overfitting | Early stopping; reduce capacity |
| Oscillating loss | LR too high or small batch | Lower LR, larger batch, more warmup |
Real products, models, and research that use this idea.
- TRL DPOTrainer in 2026 defaults to bf16 on Hopper hardware specifically to avoid the fp16 NaN failure mode this question describes.
- Llama 4 Maverick preference-tuning recipes ship with beta=0.05 by default after community reports of NaN spikes at beta=0.1 on noisy crowd-sourced pairs.
- Axolotl YAML configs for DPO runs set max_grad_norm=1.0 as the default since DPO is more prone to gradient spikes than SFT.
- Hugging Face's TRL repo issue history is full of DPO NaN bug reports traced to beta too high, fp16, or contradictory preference pairs.
- Mistral Large 3 community DPO fine-tunes in 2026 routinely include an embedding-similarity preprocessing step on the preference dataset to drop near-duplicate chosen/rejected pairs.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy does NaN propagate forward indefinitely once it appears in DPO?
QHow do you choose between dropping beta to 0.05 versus 0.03?
QWhat is the embedding-similarity threshold to use when filtering near-duplicate preference pairs?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Blaming the learning rate when DPO spikes to NaN. The DPO loss has its own amplifier (beta) on a term that can diverge independently of LR, and the right diagnostic is the logit blow-up, not a generic optimization issue.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.