I have checked and the last iteration before NAN occurs does not have 0 in the denominator of the line you just mentioned.
In fact, there are no NAN values in forward before NAN occurs. Therefore, I suspect it happens in backward when gradient is computed. Not sure how to debug this!