Note that

the error message comes from modifying something in-place that has been used in the forward and is needed for the backward. You might check the backtrace mentioned here: (Note that it typically is in C++ stderr and not shown in Jupyter unless you use nightlies when it is in Python stderr.)


Hint: the backtrace further above shows the operation that failed to compute its gradient.


If for the type of thing you are doing, I would probably not use backward for the intermediates but use torch.autograd.grad. Basically, backward is best thought of as existing for the very narrow case of “compute gradients then fed into the optimizer”.

Best regards
Thomas