Hi,
I am trying to train a model with the loss on gradients.

torch.autograd.grad(outputs=loss, inputs=(model.parameters()), create_graph=True, retain_graph=True, allow_unused=True)

works just fine.
But calling

loss.backward()

throws the following error :

RuntimeError: trying to differentiate twice a function that was markedwith @once_differentiable

I am not sure why one works and other (backward) doesn’t. From the error it is not clear which of the function is only differentiable once.
The reason I am cannot use torch.autograd.grad() for my problem is because I need to accumulate gradients for updating my model parameters, and this is not possible with grad() as only_inputs flag is deprecated in v0.4.
Any ideas on how I can get backward() to work?