Weight of linear updated using backprop, but pytorch still make calculation using old weight

barakb: <p>Hi! Im having a peculiar issue, I’m doing an inner optimization steps using this code:</p>
<blockquote>
<pre><code>        inner_model = NN(param.detach(),self.kwargs).to(self.device).train()
        inner_opt = torch.optim.SGD(inner_model.model.parameters(), lr=100, momentum=.7, weight_decay=1e-6)
       for opt_step in range(20):
           out_inner = inner_model(images)
           loss = torch.sum(mask * F.binary_cross_entropy_with_logits(out_inner.squeeze(),inner_cur_lbl[idx].float(),                                                          reduction='none'))
           loss.backward()
           inner_opt.step()
           inner_opt.zero_grad()
</code></pre>
</blockquote>
<p>There are few important things to point out:<br>
1.The grads are good.<br>
2. The weight are updating, I check them using:</p>
<blockquote>
<p>inner_model.model.regressor.weight</p>
</blockquote>
<ol start="3">
<li>The output a.k.a: out_inner is the same(!) as before the updates!!<br>
So I entered nn.Linear forward method:</li>
</ol>
<blockquote>
<p>def forward(self, input: Tensor) → Tensor:<br>
return F.linear(input, self.weight, self.bias)</p>
</blockquote>
<p>And checked the self.weight, they are exactly as expected (i.e they changed exactly as I saw using section 2), hence I tried to run:</p>
<blockquote>
<p>return F.linear(input, self.weight, self.bias)</p>
</blockquote>
<p>But I got the same unchanged results, thus I did another experiment I checked:</p>
<blockquote>
<p>torch.mm(input,self.weight.T) <span class="hashtag-raw">#I</span> have no bias</p>
</blockquote>
<p>And got different results!! results that shows that self.weight has changed!,i.e</p>
<blockquote>
<p>torch.mm(input,self.weight.T) !=F.linear(input, self.weight, self.bias)</p>
</blockquote>
<p>I think that something I did non properly made the F.linear use an old copy of the weights, once again , this is inspite self.weight has changed.<br>
The final experiment and maybe the most important one was running:</p>
<blockquote>
<p>F.linear(input, self.weight.clone(), self.bias)</p>
</blockquote>
<p>This line calculated the same result  as the matmul, but not as:</p>
<blockquote>
<p>F.linear(input, self.weight, self.bias)</p>
</blockquote>
<p>I know its not an easy post to follow, I tried to do my best to illustrate, does anyone has any idea what is going on?</p>
<p>Edit-Few more hints:<br>
1.Switching from DDP to 1 GPU didn’t help.<br>
2. This code is under training_step method under the pytorch lightning package<br>
3. When switching to CPU it works!!!<br>
This is even more puzzeling! what Im missing?</p>

ptrblck: <aside class="quote no-group" data-username="barakb" data-post="1" data-topic="184895">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/barakb/48/47322_2.png" class="avatar"> barakb:</div>
<blockquote>
<p>I think that something I did non properly made the F.linear use an old copy of the weights, once again , this is inspite self.weight has changed.</p>
</blockquote>
</aside>
<p>I doubt it and guess other parts of your models might “saturate” the outputs.</p>
<aside class="quote no-group" data-username="barakb" data-post="1" data-topic="184895">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/barakb/48/47322_2.png" class="avatar"> barakb:</div>
<blockquote>
<p>This line calculated the same result as the matmul, but not as:</p>
</blockquote>
</aside>
<p>Could you post a minimal and executable code snippet reproducing the issue?</p>

barakb: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="184895">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>ou</p>
</blockquote>
</aside>
<p>Ill try to work on Isolating the code for a running example.</p>
<p>About the “saturation”, it was the first thing I checked. But the experiments I did shows nothing like that, did you see them?  the linear not using the self.weight, when using the self.weight with matmul it return the correct result. when using inside Linear function in torch code with:</p>
<blockquote>
<p>F.linear(input, self.weight.clone(), self.bias)</p>
</blockquote>
<p>It returns the correct result, how is it possible that this is an output saturation issue?</p>