'MmBackward' returned nan values in its 0th output

JIALI_MA: <p>I am training a face recognition model with DDP on 8 GPUs, but I am getting loss Nan occasionally. The issue does not happen every time, but at a very high frequency, e.g. 8 out of 10 times, and the occurring time differ from each other (which makes it harder to debug).<br>
The pipeline is: images are first fed into a DDP model for feature extraction, then those features are gathered and input into different classifiers on different GPUs to get the results and finally compute losses.<br>
After debugging, I find that whenever the loss is nan for a certain GPU, the features extracted by this GPU is Nan. I checked the inputs, they are within normal ranges of [-1, +1]. And according to my understanding, the DDP models should be always the same among all the GPUs, it’s weird only one of the GPU outputs Nan while the rest are normal.<br>
What might be the issue? I’ve been checking it for several days but still cannot find the problem.</p>
<p>-----------------------update---------------------<br>
I debugged with torch.autograd.set_detect_anomaly, and the traceback shows ‘MmBackward’ returned nan values in its 0th output:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/0/004d76584eb56abec6d56a5f351850c400ab6c69.png" data-download-href="https://discuss.pytorch.org/uploads/default/004d76584eb56abec6d56a5f351850c400ab6c69" title="2020-11-02 23_14_04-10.80.42.177 (jiali)"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/0/004d76584eb56abec6d56a5f351850c400ab6c69.png" alt="2020-11-02 23_14_04-10.80.42.177 (jiali)" data-base62-sha1="2FXECsRnHVt2oGrgYfH3hOGRjb" width="690" height="178" data-dominant-color="2E2E2E"><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">2020-11-02 23_14_04-10.80.42.177 (jiali)</span><span class="informations">1870×483 33.1 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></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/2/02a7219a4539d7e6ae2d2e0300c6883e0fbb9aff.png" data-download-href="https://discuss.pytorch.org/uploads/default/02a7219a4539d7e6ae2d2e0300c6883e0fbb9aff" title="2020-11-02 23_14_49-10.80.42.177 (jiali)"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/2/02a7219a4539d7e6ae2d2e0300c6883e0fbb9aff.png" alt="2020-11-02 23_14_49-10.80.42.177 (jiali)" data-base62-sha1="nt1Z3X6vChta2VgqSZZEPlPJPp" width="690" height="136" data-dominant-color="2E2F2F"><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">2020-11-02 23_14_49-10.80.42.177 (jiali)</span><span class="informations">1636×323 20.3 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></p>

albanD: <p>Hi,</p>
<p>MMBackward’s input 0 being nan most likely means that your weights contain nan values?<br>
So maybe your optimizer step that makes weights become nan?</p>
<p>What is the loss value before you encounter the issue? Also do you use any function that might lead to nan for special inputs (sqrt at 0 or log at &lt;= 0 etc)?</p>

JIALI_MA: <p>Thanks for the reply!<br>
I checked the inputs for F.linear, weights is normal and there is no Nan values, but emb_all has Nan. Since emb_all is the concatenation of embs from all 8 GPUs, I am very confused why it happens. All the 8 GPUs should share exactly the same weights of model (under DDP mode), if the input images are normal, then the output embs for all GPUs should also be normal.<br>
The loss value is normal as well, e.g. 40 and I set a small lr, so it is not divergence. There is no 0 div, sqrt 0, or log 0 operations. I’ll double check this anyway.<br>
BTW, is there any other reasons for Mmbackward returning nan? I cannot find many info on this, but overflow is mentioned in this blog: <a href="https://github.com/NVIDIA/apex/issues/440" rel="noopener nofollow ugc">https://github.com/NVIDIA/apex/issues/440</a></p>

JIALI_MA: <p>I guess the issue might be related to fp16. I’m using torch.cuda.amp fp16 to accelerate the training process. After I switch to fp32, the training is going on smoothly.<br>
In addition, I’m using 1cycle lr scheduler and encounter the issue, but the code works well with default step-wise lr scheduler.<br>
But I still don’t understand, is fp16 incompatible with 1cycle lr scheduler?</p>

albanD: <blockquote>
<p>All the 8 GPUs should share exactly the same weights of model (under DDP mode)</p>
</blockquote>
<p>Yes they do but if you use these as embedding, each GPU use only a subset depending on the particular input it gets, so you might read some bad values on a GPU and not on another.</p>
<p>fp16 is not incompatible with lr scheduler. but it can lead to instability in quite a few use cases.<br>
That why we actually have the AMP package which is quite complex that is tuned to only use fp16  when it is stable enough.</p>