Inplace error, in autograd function -pytorch

el_youssfi_azeddine: <p>Hello,<br>
I try to evaluate mu modele on gpu, but when I send data to gpu and the modele I get this error.<br>
I searched for inplace errors but in this function (autograd) I did’nt find any.<br>
Someone of you have an idea?<br>
this is the error!<br>
[<br>
49 def Divergence(var:Tensor,Field:Tensor) → Tensor:<br>
50     ones = torch.ones_like(Field)<br>
—&gt; 51     dFdvar = torch.autograd.grad(Field, var,<br>
52                                 grad_outputs=ones,<br>
53                                 create_graph=True, retain_graph=True)[0]</p>
<p>~\Anaconda3\lib\site-packages\torch\autograd_<em>init</em>_.py in grad(outputs, inputs, grad_outputs, retain_graph, create_graph, only_inputs, allow_unused)<br>
200         retain_graph = create_graph<br>
201<br>
 → 202     return Variable.<em>execution_engine.run_backward(<br>
203         outputs, grad_outputs</em>, retain_graph, create_graph,<br>
204         inputs, allow_unused)</p>
<p>RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [51126]], which is output 2 of struct torch::jit::`anonymous namespace’::DifferentiableGraphBackward, is at version 3; expected version 2 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!<br>
]<br>
thanks !</p>

ptrblck: <p>If you cannot find the inplace operation, you might want to add <code>.clone()</code> operations to the activations and check, which line of code causes the issue. This is a bit cumbersome, so you could also check again for all inplace ops. They should either use the <code>inplace=True</code> argument or are used via an inplace op directly, e.g. <code>a += b</code>.</p>

el_youssfi_azeddine: <p>Hello, thanks for reply.<br>
I directly replace all a+=b by a=a+b, now there is no such operation in my code.<br>
I read somewhere to use “with torch.no_grad():” function I tried it but I get an other error " element 0 of tensors  does not require grad and does not have a grad_fn"</p>

ptrblck: <p>No, you shouldn’t use the <code>no_grad()</code> guard, if you need to compute gradients. This wrapper is used during validation or testing in order to save memory, as intermediate tensors are not stored, which would be needed to compute the gradients.</p>

el_youssfi_azeddine: <p>thanks for your quick answer, this one of the reasons I like this forum <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
Actually I’m in validation phase, I called the trained model and stroed it in GPU, I send new data to GPU that I will give to model to predict.</p>

ptrblck: <p>During the validation phase you shouldn’t get the initial error message, as it would be caused by a <code>backward()</code> call, which should probably not used during validation.</p>

el_youssfi_azeddine: <p>You’re right,I got  the initial error before using torch.no_grad(), I’m trying this in order to avoid the gradient error.</p>

el_youssfi_azeddine: <p>There is something I don’t understand, why the model is needing to review gradient calculation during validation phase… if this error occurs because of gradient, why not just disable grad computation?</p>

ptrblck: <p>This is an issue, which I also don’t understand in your use case.<br>
The error is raised by Autograd, as it’s detecting an inplace operation.<br>
However, you explained that you are seeing it during the validation step, which wouldn’t calculate the gradients in the common use case, so you would have to check why you are trying to calculate gradients during the validation step.</p>

el_youssfi_azeddine: <p>thanks for reply.<br>
Infact, I’m working with PINNs (physics informed neural network) I’m trying to solve PDE with neural network, In my loss function definition, we use gradient and divergence of the temperature to express the physic loss.<br>
so those gradient above I’m talking about are  calculated in forward method of the method to retrun all physics items that the model calculate and used to define physic loss.<br>
thanks</p>