This error is raised, if you are trying to call backward() on a tensor, which isn’t attached to the computation graph and you can verify it by checking its .grad_fn attribute which would return None.
This might happen, if you’ve detached the tensors manually (e.g. via calling detach() directly, using a 3rd party library such as numpy etc.) or if you’ve used a non-differentiable operation.
In both cases you could check the .grad_fn attribute of the intermediate tensors and narrow down where the tensor is being detached.