Cuda initialization error

amsword: <p>The problem crashed with the initialization error. From the call stack, it seems like it crashed when it tries to release some resource. The whole program is kinds of large. The basic logic is that, it first calls train() which creates the model, and run the training. Then, it calls the predict(), which also creates the model and run the prediction. The program is running distributed in 8 GPUs. Each GPU only predicts part of the test files and then rank=0 will concatenate those files.</p>
<p>The crash is at the prediction phase. Only one GPU crashes with this error and all other GPUs can finish the predictions well.</p>
<p>Any idea on the potential issues?</p>
<p>pytorch = 1.7.1<br>
cuda = 10.2</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/a/1afa596d5fad212d9c93b7682bc4f7e603154fcd.png" data-download-href="https://discuss.pytorch.org/uploads/default/1afa596d5fad212d9c93b7682bc4f7e603154fcd" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/a/1afa596d5fad212d9c93b7682bc4f7e603154fcd_2_690x148.png" alt="image" data-base62-sha1="3QEMGg4EeopvbfO9348nNAbJHLT" width="690" height="148" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/a/1afa596d5fad212d9c93b7682bc4f7e603154fcd_2_690x148.png, https://discuss.pytorch.org/uploads/default/optimized/3X/1/a/1afa596d5fad212d9c93b7682bc4f7e603154fcd_2_1035x222.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/a/1afa596d5fad212d9c93b7682bc4f7e603154fcd_2_1380x296.png 2x" data-dominant-color="14404B"><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">3568×768 337 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>

rvarm1: <p>To be clear, you’re not seeing any crash during training, however there is a crash at inference time? Do you have a script to reproduce this issue?</p>

amsword: <p>yes. no issue at training. it crashes only on inference. The code is kind of large and it is hard to extract. One strange thing is that if i skip the training and run the inference only, it runs well with loading previously trained model.</p>

amsword: <p>Thanks. Problem solved, but the root cause is unknown. Here is the story for reference. The program first calls train() to do model training, where the model will be built, optimizer will be constructed, and learning rate scheduler will also be initialized. The learning rate scheduler is based on a lambda expression. After train(), supposedly, the learning rate scheduler/model/optimizer should be collected by teh garbage collector, but not. The reason seems to be the lambda expression-based learning rate scheduler. In this case, the model/optimizer also are in the GPU memory without releasing resources. Then, in test(), the model will be re-built, which might have some issues, although the crash should be on data loader (if <span class="hashtag">#workers</span> is 0, also no issues). After refactoring the learning rate scheduler by removing the lambda expression, everything works fine.</p>