No gradient received in mixed precision training

CDhere: <p>Hi! I’m running into a very strange problem: My model trains fine without mixed precision mode, however, as soon as I turn on the fp16 mixed precision mode the DDP training will break due to all of the model’s weights not receiving gradients, except for norm layers and manually registered <code>nn.Parameters</code>.</p>
<p>The errors I got:</p>
<pre><code class="lang-auto">RuntimeError: Expected to have finished reduction in the prior iteration before starting a new o
ne. This error indicates that your module has parameters that were not used in producing loss. Y
ou can enable unused parameter detection by passing the keyword argument `find_unused_parameters
=True` to `torch.nn.parallel.DistributedDataParallel`, and by 
making sure all `forward` function outputs participate in calculating loss. 
If you already have done the above, then the distributed data parallel module wasn't able to loc
ate the output tensors in the return value of your module's `forward` function. Please include t
he loss function and the structure of the return value of `forward` of your module when reportin
g this issue (e.g. list, dict, iterable).
Parameters which did not receive grad for rank 1: model.blocks.5.patches_cross_attn_ff.net.3.bia
s, model.blocks.5.patches_cross_attn_ff.net.3.weight,

... omit for space
</code></pre>
<p>I also tried the same training script with single-GPU, i.e. no DDP, and have the same no gradient problem (the training doesn’t crash as there’s no DDP, but the loss certainly reflects that only partial model is updated).</p>
<p>Why would mixed precision training cause this? Any suggestions?</p>
<p>Thanks!</p>
<p>UPDATE: it seems there’s something with <code>torch.no_grad()</code> - I have a <code>torch.no_grad()</code> context in each iteration where the model calculates some target which will be fed into the model outside the context, if I remove this block of code and feed some dummy values as the target instead, the model can receive gradients properly under mixed precision training. But I don’t know how to make of this, any suggestions?</p>

ptrblck: <aside class="quote no-group" data-username="CDhere" data-post="1" data-topic="200696">
<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/cdhere/48/29866_2.png" class="avatar"> CDhere:</div>
<blockquote>
<p>UPDATE: it seems there’s something with <code>torch.no_grad()</code> - I have a <code>torch.no_grad()</code> context in each iteration where the model calculates some target which will be fed into the model outside the context, if I remove this block of code and feed some dummy values as the target instead, the model can receive gradients properly under mixed precision training. But I don’t know how to make of this, any suggestions?</p>
</blockquote>
</aside>
<p>Could you post the corresponding code causing the issue?</p>

CDhere: <p>After some digging I found out this thread:</p>
<aside class="quote quote-modified" data-post="2" data-topic="93475">
  <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">
    <a href="https://discuss.pytorch.org/t/autocast-and-torch-no-grad-unexpected-behaviour/93475/2">Autocast and torch.no_grad() unexpected behaviour</a> <a class="badge-category__wrapper " href="/c/autograd/7"><span data-category-id="7" style="--category-badge-color: #AB9364; --category-badge-text-color: #FFFFFF;" data-drop-close="true" class="badge-category " title="A category of posts relating to the autograd engine itself."><span class="badge-category__name">autograd</span></span></a>
  </div>
  <blockquote>
    Thanks for reporting this issue. I can reproduce it and it looks like a bug, as autograd seems to keep the gradients disabled even after leaving the no_grad() block: 
with autocast(enabled=True):
    with torch.no_grad():
        y = net(input)

    z = net(input)
    print('z {}'.format(z.requires_grad))
&gt; False

with autocast(enabled=False):
    with torch.no_grad():
        y = net(input)

    z = net(input)
    print('z {}'.format(z.requires_grad))
&gt; True

<a class="mention" href="/u/mcarilli">@mcarilli</a> Have you seen this behavi…
  </blockquote>
</aside>

<p>which describes exactly the same issue. I used the solution in my code and the training works properly now! Although I kinda agree that this is more like bug rather than missed practice, as <code>torch.no_grad()</code> can happen anywhere inside the model while we typically wrap the entire forward pass at the most outter layer. Also, I was using Huggingface’s accelerate launch tool which achieves the mixed precision training by “preparing” the entire model too so it took me a long time to ping point the <code>torch.no_grad()</code> part that happened somewhere inside my model.</p>