Using multiple backward()s and getting this error RuntimeError:one of the variables needed for gradient computation has been modified by an inplace operation:[torch.cuda.FloatTensor which is output0 of TBackward, is at version 3; expected version 2instead

username: Mohammed_Hassoubah
cooked: <p>Hello,</p>
<p>I created a model of 3 parts encoder, decoder and discriminator, and created a separate optimizer for each part this way</p>
<pre><code class="lang-auto">    optimizer_encoder       = optim.Adam(model.encode.parameters(), lr=args.lr)
    optimizer_decoder       = optim.Adam(model.decode.parameters(), lr=args.lr)
    optimizer_discriminator = optim.Adam(model.dis.parameters(), lr=args.lr)
</code></pre>
<p>in the training loop I try to backpropagate losses this way</p>
<pre><code class="lang-auto">            model.zero_grad()

            loss_encoder.backward(retain_graph=True)
            # # someone likes to clamp the grad here
            # #[p.grad.data.clamp_(-1,1) for p in model.encode.parameters()]
            # # update parameters
            optimizer_encoder.step()
            # clean others, so they are not afflicted by encoder loss
            model.zero_grad()
            #decoder
            # if train_dec:
            loss_decoder.backward(retain_graph=True)
            #[p.grad.data.clamp_(-1,1) for p in model.decode.parameters()]
            optimizer_decoder.step()
            #clean the discriminator
            model.dis.zero_grad()
            # # #discriminator
            # # # if train_dis:
            loss_discriminator.backward()
            # # #[p.grad.data.clamp_(-1,1) for p in model.dis.parameters()]
            optimizer_discriminator.step()
</code></pre>
<p>Here a simple schematic the model<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/d/0ddf7c7e5da4599ce77ae8da519ef6b560fc7761.png" data-download-href="https://discuss.pytorch.org/uploads/default/0ddf7c7e5da4599ce77ae8da519ef6b560fc7761" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/d/0ddf7c7e5da4599ce77ae8da519ef6b560fc7761.png" alt="image" data-base62-sha1="1YJ1vsgkvNDiWupXE1TlqwYbxXH" width="690" height="255" data-dominant-color="F6F6F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1414×523 7.31 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
I need the parameters of each part to be updated only through backpropagating it’s own loss, that’s why the above code was implemented this way</p>
<p>but I get this error<br>
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [256, 256]], which is output 0 of TBackward, 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!</p>
<p>I found that it seems my issue is related to this release not <a href="https://github.com/pytorch/pytorch/releases/tag/v1.5.0" rel="noopener nofollow ugc">here</a><br>
<strong>### <code>torch.optim</code>  optimizers changed to fix in-place checks for the changes made by the optimizer (<a href="https://github.com/pytorch/pytorch/pull/33640" rel="noopener nofollow ugc">#33640</a>, <a href="https://github.com/pytorch/pytorch/pull/34211" rel="noopener nofollow ugc">#34211</a>)</strong></p>
<p>any idea how I can solve this? in my case would it be ok to switch back to pytorch 1.4?</p>