It seems that loss is already detached when it’s returned from the model, which might happen, if you rewrap it into a tensor, use a non-differentiable operation, or use a 3rd party library (e.g. numpy) without writing a custom autograd.Function.
To debug it further, you can check the .grad_fn of the intermediate tensors and locate which tensor returns a None value.