Catching PyTorch NaNs at the exact layer with forward hooks
set_detect_anomaly points at the wrong layer and costs you 10x on CPU. A forward hook adds roughly 3ms per pass, names the layer that produced the NaN, and pairs with a gradient norm check that fires a step earlier.