For example in the code below, you can see the warning that points to the faulty forward part:
import torch
from torch import autograd
autograd.set_detect_anomaly(True)

return_nan = True

class MyBadFn(autograd.Function):
    @staticmethod
    def forward(ctx, inp):
        ctx.save_for_backward(inp)
        return inp ** 2

    @staticmethod
    def backward(ctx, grad_out):
        inp, = ctx.saved_tensors
        res = 2 * grad_out * inp
        if return_nan:
            res = grad_out / 0 * 0
        return res


inp = torch.rand(10, requires_grad=True)

out = MyBadFn.apply(inp)

out.sum().backward()

prints
[W python_anomaly_mode.cpp:60] Warning: Error detected in MyBadFnBackward. Traceback of forward call that caused the error:
  File "foo.py", line 24, in <module>
    out = MyBadFn.apply(inp)
 (function print_stack)
Traceback (most recent call last):
  File "foo.py", line 26, in <module>
    out.sum().backward()
  File "/Users/albandes/workspace/pytorch_dev/torch/tensor.py", line 184, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/Users/albandes/workspace/pytorch_dev/torch/autograd/__init__.py", line 107, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: Function 'MyBadFnBackward' returned nan values in its 0th output.