norabelrose:

The former happens when I call detach().requires_grad_() on the state tensor, and the latter happens if I don’t do that.


Your workaround if detaching the tensor and setting requires_grad_() on it might not work properly and the latter error seems to indicate that the issue might be indeed an unused parameter.
Could you post the shapes for each tensor to make your code snippet executable so that we could take a proper look at it?