As you said, you could register backward hooks to a functions which prints, if a nan was detected.
The hook body could look like this:
grad_output = torch.tensor([1.0, np.nan, 2.0])
...
def printgradnan(self, grad_input, grad_output):
    if (grad_output!=grad_output).any():
        print('NaN detected!')