Why the scale became zero when using torch.cuda.amp.GradScaler?

Cuistiano: <p>I print the scale after each step’s update as follows:</p>
<pre><code class="lang-auto">print(scaler.get_scale())
</code></pre>
<pre><code class="lang-auto">...
65536.0
32768.0
32768.0
16384.0
8192.0
4096.0
...
1e-xxx
...
0.0
0.0
0.0
</code></pre>
<p>And all the grads and loss after this step became  <code>Nan</code>  (the scale still is 0).<br>
What’s wrong with my model, loss function or training data? How to fix it?</p>

ptrblck: <p>Is your model generally working fine without using amp?<br>
The loss scaler might run into this “death spiral” of decreasing the scale value, if the model output or loss contains NaN values.<br>
These NaN values in the loss would thus create NaN gradients and the loss scaler will decrease the scale factor as it thinks the gradients are overflowing.</p>
<p>However, in fact the gradients are not overflowing, but your model yields invalid outputs.<br>
Could you check the output and loss for NaNs and check, if they are also created without amp?</p>

victorc25: <p>Hello! I’m facing the exact same situation as the OP, the scale just halves after each iteration until the loss becomes zero.</p>
<p>In my case not only it works fine without AMP, but only one out of multiple losses is having this issue. The others work fine with AMP.</p>
<p>I’m also checking both the loss and the output with:</p>
<pre><code class="lang-auto">torch.isnan(l_g_total).any() or torch.isinf(l_g_total).any()
</code></pre>
<p>And there are no NaN or infs.</p>
<p>What could be causing this behavior?</p>

ptrblck: <p>If the model output or losses do not contain NaN values and the scaler is constantly decreasing the scale value, some operation might cause large gradients, which are still overflowing. Could you post an executable code snippet, which reproduces this error?</p>

victorc25: <p>Hello again <a class="mention" href="/u/ptrblck">@ptrblck</a>, thanks for your reply! <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>
<p>A working snippet will be difficult, because the training is done using multiple files and using a custom loss (<a href="https://github.com/victorca25/BasicSR/blob/3fb5851af3a411df153f0b0e5873f3b2de324a3d/codes/models/modules/loss.py#L767" rel="nofollow noopener">https://github.com/victorca25/BasicSR/blob/3fb5851af3a411df153f0b0e5873f3b2de324a3d/codes/models/modules/loss.py#L767</a>), but I think there’s nothing out of the ordinary, this is how the code looks like:</p>
<pre><code class="lang-auto">#In the init:
self.amp_scaler =  GradScaler()
</code></pre>
<pre><code class="lang-auto">#in the training loop:
        ### Network forward, generate SR
        with autocast():
            self.fake_H = self.netG(self.var_L)
        l_g_total = 0
        with autocast(): #this is separated, because there are some conditionals in the middle
            loss_results = self.generatorlosses(self.fake_H, self.var_H, self.log_dict, self.f_low) # multiple losses can be used here, but I'm testing with just the one loss in the link above
            l_g_total += sum(loss_results)/self.accumulations
        self.amp_scaler.scale(l_g_total).backward()
        if (step + 1) % self.accumulations == 0:
            self.amp_scaler.step(self.optimizer_G)
            self.amp_scaler.update() 
            self.optimizer_G.zero_grad()
</code></pre>
<p>I’m checking the isnan() and isinf() for the loss and output right after the loss. This might be difficult to try to debug as-is, but any pointers at what I should look at or how to catch the potential overflow would be awesome. It’s the only loss with this behaviour ATM.</p>

ptrblck: <p>The code looks alright.</p>
<aside class="quote no-group" data-username="victorc25" data-post="5" data-topic="90779">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/13edae/48.png" class="avatar"> victorc25:</div>
<blockquote>
<p>I’m checking the isnan() and isinf() for the loss and output right after the loss.</p>
</blockquote>
</aside>
<p>Note that a NaN output or loss would be a different issue than a valid output or loss and a constantly reduced scaling factor (and thus also the debugging steps). Which case are you dealing with?</p>

victorc25: <aside class="quote no-group" data-username="ptrblck" data-post="6" data-topic="90779">
<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>valid output or loss and a constantly reduced scaling factor</p>
</blockquote>
</aside>
<p>This, same as OP, my scaler’s scale is halving each iteration until it becomes of magnitude 1e-45 and after that zeroes out, so all of the model’s output become black. There are no NaN or Inf happening in the loss or output.</p>

ptrblck: <p>Thanks for the follow-up.<br>
How could we reproduce this issue, i.e. which input shapes and values are you using for which model and loss function?</p>

victorc25: <p>I don’t currently have the AMP code in the repo (because of this bug), but I’m using the ESRGAN model for 4x SISR (RRDBNet: <a href="https://github.com/victorca25/BasicSR/blob/3fb5851af3a411df153f0b0e5873f3b2de324a3d/codes/models/modules/architectures/RRDBNet_arch.py" rel="nofollow noopener">https://github.com/victorca25/BasicSR/blob/3fb5851af3a411df153f0b0e5873f3b2de324a3d/codes/models/modules/architectures/RRDBNet_arch.py</a>), the inputs are images of size 32x32 and the target patch size is 128x128. The loss function is the contextual loss (<a href="https://github.com/victorca25/BasicSR/blob/3fb5851af3a411df153f0b0e5873f3b2de324a3d/codes/models/modules/loss.py#L767" rel="nofollow noopener">https://github.com/victorca25/BasicSR/blob/3fb5851af3a411df153f0b0e5873f3b2de324a3d/codes/models/modules/loss.py#L767</a>). I’m not using other losses or a discriminator, only a generator. The model “trainer” is here: <a href="https://github.com/victorca25/BasicSR/blob/3fb5851af3a411df153f0b0e5873f3b2de324a3d/codes/models/SRRaGAN_model.py" rel="nofollow noopener">https://github.com/victorca25/BasicSR/blob/3fb5851af3a411df153f0b0e5873f3b2de324a3d/codes/models/SRRaGAN_model.py</a> (minus the AMP parts), but its really no different from the official documentation.</p>
<p>I know it may not be easy to reproduce like this, but maybe I could help with some guidance? I already tried changing the default GradScaler() parameter initialization, increasing the backoff_factor only delays the inevitable and the others can’t really change anything.</p>

victorc25: <p>Hello again!</p>
<p>After more testing I found out that the problem is in the autoscaling of the losses. If I only autoscale the forward pass, everything works fine (of course, minus the proper loss handling). After diagnosing the losses, I also found that SSIM caused a similar behavior, but I think that the important thing is that while the model output is converted into a HalfTensor, the target (the ground truth image) is not, it stays as a FloatTensor.</p>
<p>Update: I fiddled around with the targets and that’s not the issue. They types are inconsistent, but it doesn’t change anything if they are the same type.</p>

ptrblck: <p>Which <code>self.distanceType</code> are you using?<br>
I see that <code>meshgrid</code> is used, which could be numerically unstable (but we haven’t seen a failed use case so far).<br>
Could you disable <code>autocast</code> for <code>mesgrid</code>, while enabling it for the general loss calculation and rerun the script?</p>

victorc25: <p>Hello!</p>
<p>I’m using the default distanceType (‘cosine’) and the regular CX loss (‘calculate_CX_Loss()’), so the meshgrid computation is not being used. _random_sampling(), _random_pooling() and _crop_quarters() are also not being used, only _create_using_dotP (cosine distance) and calculate_CX_Loss, in principle.</p>
<p>So far only by taking the whole loss calculation out of the autocasting allows for the scale to remain stable. If I only autocast parts of the loss, wouldn’t calculations become inacurate?</p>
<p>In the case of SSIM, the scale also drops heavily, but stabilizes at a scale of 2. The only similarity I find between the two loss functions is that both are in principle distance metrics, but no idea if that is related.</p>

ptrblck: <aside class="quote no-group" data-username="victorc25" data-post="12" data-topic="90779">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/13edae/48.png" class="avatar"> victorc25:</div>
<blockquote>
<p>If I only autocast parts of the loss, wouldn’t calculations become inacurate?</p>
</blockquote>
</aside>
<p>That shouldn’t be the case, since <code>autocast</code> should take care of using FP16, where it’s safe to do so, and fallback to FP32 if necessary.<br>
Could you post the setup for the loss function, so that I could run it in isolation?</p>
<aside class="quote no-group" data-username="victorc25" data-post="12" data-topic="90779">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/13edae/48.png" class="avatar"> victorc25:</div>
<blockquote>
<p>In the case of SSIM, the scale also drops heavily, but stabilizes at a scale of 2.</p>
</blockquote>
</aside>
<p>The initial loss scale might drop and stabilize, which is the expected behavior.<br>
However, the outputs (and thus loss) should never contain any NaN values.</p>

victorc25: <aside class="quote no-group" data-username="ptrblck" data-post="13" data-topic="90779">
<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>That shouldn’t be the case, since <code>autocast</code> should take care of using FP16, where it’s safe to do so, and fallback to FP32 if necessary.</p>
</blockquote>
</aside>
<p>Awesome! I’ll be testing that today then.</p>
<aside class="quote no-group" data-username="ptrblck" data-post="13" data-topic="90779">
<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>Could you post the setup for the loss function, so that I could run it in isolation?</p>
</blockquote>
</aside>
<p>Yes, for normal use it should be like :</p>
<pre><code class="lang-auto">layers = {"conv_3_2": 1.0, "conv_4_2": 1.0}
Contextual_Loss(layers, crop_quarter=False, max_1d_size=64, distance_type = 'cosine', b=1.0, band_width=0.5, use_vgg = True, net = 'vgg19', calc_type = 'regular')
</code></pre>
<aside class="quote no-group" data-username="ptrblck" data-post="13" data-topic="90779">
<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>The initial loss scale might drop and stabilize, which is the expected behavior.</p>
</blockquote>
</aside>
<p>Perfect! That makes sense.</p>
<p>Thanks a lot for your help!</p>

victorc25: <p>Hello again!</p>
<p>Thanks to your suggestion of autocasting only parts of the loss, I found the exact line causing the scale to drop, its this one: <a href="https://github.com/victorca25/BasicSR/blob/3fb5851af3a411df153f0b0e5873f3b2de324a3d/codes/models/modules/loss.py#L704" rel="nofollow noopener">https://github.com/victorca25/BasicSR/blob/3fb5851af3a411df153f0b0e5873f3b2de324a3d/codes/models/modules/loss.py#L704</a></p>
<p><code>dist = F.conv2d(I_features_i, T_features_i).permute(0, 2, 3, 1).contiguous()</code></p>
<p>If just this line is put inside an autocast context, the scale is halved every iteration.</p>
<p>Any suggestions on what could be the reason? It doesn’t appear to be anything special there.</p>

ptrblck: <p>It is indeed a common operation.<br>
Could you check the stats (min, max, mean, median, shape) or <code>I_features_i</code> and <code>T_features_i</code>, which cause the NaNs?<br>
If possible, could you upload them so that we could use them to debug?</p>

victorc25: <aside class="quote no-group" data-username="ptrblck" data-post="16" data-topic="90779">
<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>Could you check the stats (min, max, mean, median, shape) or <code>I_features_i</code> and <code>T_features_i</code> , which cause the NaNs?<br>
If possible, could you upload them so that we could use them to debug?</p>
</blockquote>
</aside>
<p>I don’t know if this works, but here is the output from the first few iterations: <a href="https://pastebin.com/YgH70rth" class="inline-onebox" rel="noopener nofollow ugc">20-08-28 10:08:46.412 - INFO: Model [SRRaGANModel] is created.20-08-28 10:08:4 - Pastebin.com</a></p>
<p>I also included the gradscaler statedict output, in case it is useful. I wouldn’t know what to say about the stats, but there are no NaNs. The values are small, but I don’t know if that could cause problems? Its at that convolution where the problem is happening.</p>

victorc25: <p>I’ve now committed the code with AMP to the branch (<a href="https://github.com/victorca25/BasicSR/tree/dev2/codes" rel="nofollow noopener">https://github.com/victorca25/BasicSR/tree/dev2/codes</a>).</p>
<p>I’m giving up on making AMP work properly after that convolution in the CX loss, I can’t find the cause of the problem, so I’m forcing the tensors to be converted to FP32 right after that convolution as a workaround for now (<a href="https://github.com/victorca25/BasicSR/blob/57577667067c99194098ce034a97d0bef028620d/codes/models/modules/loss.py#L707" rel="nofollow noopener">https://github.com/victorca25/BasicSR/blob/57577667067c99194098ce034a97d0bef028620d/codes/models/modules/loss.py#L707</a>).</p>
<p>It runs and the other losses work fine. The forward pass also works fine in the autocast context, so at least its better than nothing.</p>