0
PyTorch NaNs Are Silent Killers — So I Built a 3ms Hook to Catch Them at the Exact Layer
https://towardsdatascience.com/pytorch-nans-are-silent-killers-i-built-a-3ms-hook-to-catch-them-at-the-exact-layer/(towardsdatascience.com)Debugging NaN errors in PyTorch models is challenging because they propagate silently and the standard `torch.autograd.set_detect_anomaly` tool is slow and often misleading. A more efficient solution uses PyTorch's `register_forward_hook` to inspect tensor outputs at every layer in real-time, identifying the exact layer and batch where a NaN or infinity first occurs. This hook-based approach has significantly lower overhead, especially on GPUs, compared to the standard anomaly detection. The system also includes a gradient norm guard to catch exploding gradients, which are often the root cause of NaNs, before they corrupt the model's activations. The provided code offers a production-ready, thread-safe implementation for precise debugging.
0 points•by ogg•3 hours ago