I’ve spent some time chasing a bug in my code. Apparently, if the computation graph has no tensors requiring grads, autograd raises an error. More specifically:
x = torch.ones((2, 2), requires_grad=False)
y = torch.ones((2, 2), requires_grad=True)
torch.sum(x * y).backward()  # ok
torch.sum(x * x).backward()  # crashes

The last line throws a cryptic RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn, and detect_anomaly is not helpful in localising the problem.
In my case, the graph is dynamic, and on rare occasions the latter case occurs, resulting in the error. Would it be better to stop gradients without crashing, i.e. do nothing in backward() if the graph is empty?