I’m afraid the graph that we create to compute gradients does not store all the intermediary results (only the ones that are required) and they are not easily accessible to the user in general.
What I mean by binary search is:

Take the middle of your code
Is the nan already there?
If so, do the same thing on the first half
else do the same thing on the other half

This will give you the line where it appears (even if you have 1k+ line, in 7 iterations, this will give you the line where the nan appears).