PyTorch 1.5.0, RuntimeError when loading state dict

username: jplasser
cooked: <p>Hi!</p>
<p>I have something like the following in a loss function (this is code from an exercise, so there’s no direct practical value):</p>
<pre><code class="lang-auto">old_state = copy.deepcopy(model.state_dict())
logits = model(x)
loss = objective(logits, y)
model.load_state_dict(old_state)
return loss
</code></pre>
<p>This leads to an error in the backward step of the loss:</p>
<blockquote>
<p>RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [300, 10]], which is output 0 of TBackward, is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).</p>
</blockquote>
<p>The anomaly detection isn’t helpful in this case. Commenting out the model.load_state_dict statement removes the error message.</p>
<p>Any ideas on how I could save and load the state_dict without running into an error?</p>

username: albanD
cooked: <p>Hi,</p>
<p>The loading of the state dict is not tracked as a differentiable op by the autograd.<br>
But it does modify the existing parameters inplace with the new value. But these values are required to compute the backward pass (as they were used during the forward) and thus the error you see.</p>
<p>Why do you need to do this? Can you give a bit more context on what you’re trying to achieve here?</p>

username: jplasser
cooked: <p>As I wrote it‘s an exercise and we have to do some second order optimization of the learning rate by doing a backtracking using the loss function (Armijo condition, line search). While backtracking the loss has to be computed from different weights, so the network itself changes, therefore the state_dict is saved and the original weights are reloaded again (that‘s my interpretation, I‘m not sure as I‘m not the author of the code).</p>
<p>Is it in some way possible to disable PyTorch‘s checks for integrity? I know this doesn‘t make sense usually, but in this case it would be easier, I mean.</p>
<p>Or is there a PyTorch version that does no checks? I think there must be one, other than that this exercise would never be executable? Or I misunderstood something in the code, which might even be possible to some degree.</p>
<p>I can‘t provide more code as there‘s a strict copyright on it, and I do not want to run into legal issues.</p>

username: albanD
cooked: <blockquote>
<p>I  know this doesn‘t make sense usually, but in this case it would be easier, I mean.</p>
</blockquote>
<p>Well if you disable these checks, all the gradients the gradient computes in this case will be wrong. So that won’t really help you.</p>
<p>You might want to modify the code to move the <code>load_state_dict()</code> after the backward to make sure that you don’t override values that are required.<br>
Otherwise, you can change your model’s forward to <code>.clone()</code> the parameters before using them so that the value modified inplace by the <code>load_state_dict()</code> is not the one that is needed to compute the backward.</p>

username: jplasser
cooked: <p>Hi <a class="mention" href="/u/alband">@albanD</a>,</p>
<p>a solution to the problem would simply be to make a copy of the whole model, not only of the state_dict and operate on the copied model:</p>
<pre><code class="lang-auto">model_copy = copy.deepcopy(model)

        logits = model_copy(x)
        return objective(logits, y)
</code></pre>
<p>To be honest, this was not my idea.<br>
Anyway, thanks for your help!</p>
<p>Jürgen</p>