Quick update: In the meantime, I have noticed that my code contained a bug. I tried to apply a loss to a calculation after some parameters used in it had already been modified by another calculation. This is what caused the error message. The question I asked is still valid, and the answers are still useful. But just in case somebody wonders, the reason for the error message was not the in-place modification of gradients.