Apex Loss Scale not stopping

Bika: <p>I am training a model on top of ALBERT using mixed precision with apex. And the loss keeps decreasing like this <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/8/78804ea30a82b3bef722012ccd9ef47ff77c7673.png" data-download-href="https://discuss.pytorch.org/uploads/default/78804ea30a82b3bef722012ccd9ef47ff77c7673" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/8/78804ea30a82b3bef722012ccd9ef47ff77c7673.png" alt="image" data-base62-sha1="hc0bZ4ZkiCWbfXdAiCSQ7zHzsVZ" width="690" height="248" data-dominant-color="E2E2E2"><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">735×265 49.6 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>. How can i track the problem? I found there was “nans” in the matrix and i fixed it with LayerNorm as they tend to be very large.</p>

ptrblck: <p>If you see this message every couple of iterations, you can just ignore it.<br>
However, if you encounter any NaN values in your input, this could also create NaNs in your parameters, thus output and you will end up decreasing the loss scaling value until you underflow and divide by zero.</p>

Bika: <p>Can the parameters in the model after certain operation cause the gradient to explode that cause overflow ? Or i mainly should debug the input ?<br>
Now there is no NaNs in the output of each layer , however still the loss scaling is decreasing.</p>

ptrblck: <p>If the gradients overflow due to loss scaling, the scaling value will be lowered and you will see this message.<br>
If you encounter a NaN output, something in your model went wrong or the input contained invalid values.</p>
<p>If you see the loss scaling information not in every iteration, you can still ignore it, or are you seeing it in every step?</p>

Bika: <p>I see the loss scaling every iteration. And the i print all the matrices , there is not NaNs</p>

ptrblck: <p>Could you check all parameters for NaN or Infs?<br>
Also, how large is your loss?</p>

Bika: <p>Loss is extremely large more than 60k and next step 120k and third step 220k. This is kind of weird.</p>

Bika: <blockquote>
<p>tensor(42380.2852, device=‘cuda:0’, grad_fn=)<br>
tensor(124045.8906, device=‘cuda:0’, grad_fn=)<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 32768.0<br>
tensor(917312., device=‘cuda:0’, grad_fn=)<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 16384.0<br>
tensor(422464., device=‘cuda:0’, grad_fn=)<br>
tensor(129528., device=‘cuda:0’, grad_fn=)<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 8192.0<br>
tensor(nan, device=‘cuda:0’, grad_fn=)<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 4096.0<br>
tensor(nan, device=‘cuda:0’, grad_fn=)</p>
</blockquote>
<p>These are the loss values for the first couple of steps</p>

Bika: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> There is something also i found out , i am building a 1D conv over the top of ALBERT model. I tried different combinations while freezing ALBERT. I tried :<br>
1-<br>
There is something also i found out , i am build a 1D conv over the top of ALBERT model. I tried different combinations while freezing ALBERT. I tried :<br>
1-</p>
<blockquote>
<p>class Grammer_Classification(nn.Module):<br>
def <strong>init</strong>(self,Albert):<br>
super(Grammer_Classification,self).<strong>init</strong>()<br>
self.attn_1 = Self_Attention(2048,2048)<br>
self.conv_1 = Conv1D_Layer(2048,4,1024)<br>
self.attn_2 = Self_Attention(1024,1024)<br>
self.conv_2 = Conv1D_Layer(1024,2,512)<br>
self.FFN1 = nn.Linear(2560,512)<br>
self.Layer_norm = nn.LayerNorm(512,eps=1e-12)<br>
self.FFN2 = nn.Linear(512,2)<br>
def forward(self,X,mask):<br>
X= Albert(X)<br>
<span class="hashtag-raw">#X</span> = X[0].view(X[0].shape[0],-1)<br>
X = self.attn_1(X[0])<br>
X = self.conv_1(X)<br>
X = self.attn_2(X)<br>
X = self.conv_2(X)<br>
X = X.reshape(X.shape[0],X.shape[1]*X.shape[2])<br>
X = F.leaky_relu(self.FFN1(X))<br>
X = self.Layer_norm(X)<br>
X = self.FFN2(X)<br>
return X</p>
</blockquote>
<p>2-</p>
<blockquote>
<p>class Grammer_Classification(nn.Module):<br>
def <strong>init</strong>(self,Albert):<br>
super(Grammer_Classification,self).<strong>init</strong>()<br>
self.attn_1 = Self_Attention(1024,1024)<br>
self.conv_1 = Conv1D_Layer(2048,16,1024)<br>
self.attn_2 = Self_Attention(512,512)<br>
self.conv_2 = Conv1D_Layer(1024,4,512)<br>
self.FFN1 = nn.Linear(94208,1024)<br>
self.Layer_norm = nn.LayerNorm(1024,eps=1e-12)<br>
self.FFN2 = nn.Linear(1024,2)<br>
def forward(self,X,mask):<br>
X= Albert(X)<br>
X = X[0].view(X[0].shape[0],-1)<br>
X = F.leaky_relu(self.FFN1(X))<br>
X = self.Layer_norm(X)<br>
X = self.FFN2(X)<br>
return X</p>
</blockquote>
<p>and only conv1D but i deleted the code.<br>
And the accuracy never changes while the loss scaling. Any idea how can i debug this further more ? I printed all the matrices and there are no NaNs except after 30-40 steps</p>

ptrblck: <p>It seems the loss is exploding.<br>
What range does the loss have without using <code>apex</code> or with <code>opt_level='O0'</code>?</p>

Bika: <p>Loss start from 6 and at epoch 3 its 13.6. And its increasing and accuracy decreasing. This is without using apex.</p>

ptrblck: <p>Thanks for the update.<br>
Could you try to make sure the FP32 run is working correctly, i.e. the loss is decreasing?<br>
Sometimes you will see an increasing loss if you forgot to zero out the gradients.</p>

Bika: <p>I kept changing in the model architecture till this happen.</p>
<blockquote>
<p>Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 32768.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 16384.0<br>
Epoch 1 loss is 14325.70703125 and accuracy is 0.7753031716417911<br>
Epoch 2 loss is 10062.3505859375 and accuracy is 0.8111007462686567<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 8192.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 4096.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 2048.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 1024.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 512.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 256.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 128.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 64.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 32.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 16.0<br>
Epoch 3 loss is 114.29520416259766 and accuracy is 0.8091962064676618<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 8.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 4.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 2.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 1.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 0.5<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 0.25</p>
</blockquote>
<p>Is this now normal ?</p>

ptrblck: <p>Is your model training correctly using FP32?<br>
The loss scaler should reduce the loss scaling at the beginning, but should eventually stop decreasing the loss continuously.<br>
I would still recommend to make sure the model trains in FP32 and the loss looks reasonable.</p>

Bika: <p>The loss looks reasonable in FP32 , however its fluctuating. And the loss scale decrease after 4 epochs.</p>
<blockquote>
<p>Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 32768.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 16384.0<br>
Epoch 1 loss is 14325.70703125 and accuracy is 0.7753031716417911<br>
Epoch 2 loss is 10062.3505859375 and accuracy is 0.8111007462686567<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 8192.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 4096.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 2048.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 1024.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 512.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 256.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 128.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 64.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 32.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 16.0<br>
Epoch 3 loss is 114.29520416259766 and accuracy is 0.8091962064676618<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 8.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 4.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 2.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 1.0<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 0.5<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 0.25<br>
Epoch 4 loss is 4.909278869628906 and accuracy is 0.7796175373134329<br>
Epoch 5 loss is 0.1522625833749771 and accuracy is 0.8130830223880597<br>
Epoch 6 loss is 0.15188875794410706 and accuracy is 0.8128498134328358<br>
Gradient overflow.  Skipping step, loss scaler 0 reducing loss scale to 0.125<br>
Epoch 7 loss is 0.1318310648202896 and accuracy is 0.812616604477612</p>
</blockquote>
<p>I am training as we speak. However i think i should change the architecture cause its kind of slow training. I am sure i am using everything multiple of 8</p>

ptrblck: <p>Since the loss downscaling stopped, it could work.</p>

Bika: <p>The output of the model during prediction is always the same. Does this has anything to do with loss issue ?</p>

ptrblck: <p>It could mean that your model is overfitting to a specific class.</p>