My first move is to add gradient norm logging immediately before the NaN appears, because NaN in the loss usually traces back to either an exploding gradient or a numerical issue in the forward pass, and gradient norms tell me which direction to look. If the gradient norm spikes to infinity in the backward pass, I know the issue is exploding gradients and I add gradient clipping as a short-term fix while investigating the root cause. If the loss is NaN from the first iteration, I look at the forward pass: check for log of zero in cross-entropy, division by zero in normalization layers, or large activations from poor initialization. I'll reduce batch size to 1 and inspect activations layer by layer to find where the NaN first appears. Common culprits I've seen in production: mixed-precision training with FP16 where activations overflow, a log function applied to a softmax that's exactly zero, or a learning rate that's two orders of magnitude too high. The systematic approach is to binary search: if batch 100 is NaN, check batch 50, then narrow down. I also check that the input data itself doesn't contain NaNs, which sounds obvious but has burned me before.
Insider read
Really testing: Whether the candidate has a systematic debugging methodology, not just a list of possible causes.
The tell: Juniors list possible causes; seniors describe a systematic localization process: gradient norm logging, layer-by-layer activation inspection, and binary search over iterations.
Follow-up: How does mixed-precision (FP16) training increase the risk of NaN gradients, and what is gradient scaling?
Say this"The fastest path to root cause is gradient norm logging: if norms spike before the NaN, it's a training dynamics issue; if activations are NaN from the first forward pass, it's a data or initialization issue."