The GradScaler implosion: how filtered-out NaN gradients became NaN weights

· · Dec. 4, 2026, 6:54 p.m.
Summary
The author investigates a perplexing issue with the GradScaler in PyTorch, which led to model weights becoming NaN during training, despite using Automatic Mixed Precision (AMP). The post explains the detailed mechanics of GradScaler and explores how it handles finite and non-finite gradients. It also recounts the author's personal experience of diagnosing the problem and finding that a chain of NaN losses resulted in the scaler underflowing to zero, which ultimately corrupted the model weights. The author concludes that while the GradScaler's behavior is insightful, the root issue lies in the model's hyperparameters and training setup, which must be addressed to prevent divergence.
AUTHOR
BLOG POST FEATURED ON

Add this plugin to your blog