Weird CUDA out of memory error (OOM) at epoch end

gebbissimo: <p>I am facing a very weird OOM error with my current training. The OOM error happens systematically during the forward pass of the last (or second last, not sure) batch of an epoch. It happens independent of training size. It happens before validation.</p>
<p>I am logging the GPU memory consumption via nvidia-smi during training. During the training epoch the memory consumption stays constant, so I doubt it’s a typical memory leak (caused e.g. by a missing .detach() call). However, when running the last batch, the memory consumption suddenly starts to increase in the forward pass. This can be seen by printing the GPU memory consumption during different steps in the forward pass.</p>
<p>Is there something special happening at epoch end, when the queue size of the dataloader approaches zero? Did anybody else face this phenomenon?</p>
<p>P.s.: While I said the error happens systematically, the training did work for at one time for a couple of epochs, so there does seem some randomness to it. However, it raised the OOM error at epoch end approximately a dozen times now.<br>
P.P.s.: Training pipeline is a modified monodepth2 and I’m getting the error at this point:  <a href="https://github.com/nianticlabs/monodepth2/blob/master/trainer.py#L285" rel="noopener nofollow ugc">https://github.com/nianticlabs/monodepth2/blob/master/trainer.py#L285</a> I am using only 1 GPU to train.</p>

gebbissimo: <p>P.P.P.s.: Only occurs for num_workers&gt;=2. Also now happened a few times in the middle of the first epoch, not the end…</p>

ptrblck: <p>Are you using static input shapes or are they dynamically changed?</p>
<aside class="quote no-group" data-username="gebbissimo" data-post="2" data-topic="102665">
<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/g/e0b2c6/48.png" class="avatar"> gebbissimo:</div>
<blockquote>
<p>Only occurs for num_workers&gt;=2.</p>
</blockquote>
</aside>
<p>Are you pushing the data to the GPU inside the <code>Dataset</code>?</p>

gebbissimo: <p>Thank you very much for your reply!</p>
<ul>
<li>The input shapes are static</li>
<li>Data is pushed to the GPU not inside the Dataset, but just before the forward pass</li>
</ul>
<p>By now I also ran a memory trace suggested in this thread: <a href="https://discuss.pytorch.org/t/how-to-debug-causes-of-gpu-memory-leaks/6741/18" class="inline-onebox">How to debug causes of GPU memory leaks?</a> . However, somehow the training runs smoothly (and extremely slowly) for several epochs in this case… Could it be that at some point GPU memory is not deallocated quickly enough and that some wait statements would solve it?</p>
<p>Also forgot to mention: I am using an older version of monodepth, namely 0.4.1</p>

ptrblck: <aside class="quote no-group" data-username="gebbissimo" data-post="4" data-topic="102665">
<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/g/e0b2c6/48.png" class="avatar"> gebbissimo:</div>
<blockquote>
<p>Could it be that at some point GPU memory is not deallocated quickly enough and that some wait statements would solve it?</p>
</blockquote>
</aside>
<p>This shouldn’t be the case, as PyTorch tries to recover from an OOM by clearing the cache for you.</p>
<p>Are you seeing these issues only in this old <code>monodepth2</code> repository or also using plain PyTorch code?</p>

gebbissimo: <p>Ok, thanks for the clarification.</p>
<p>What exactly do you mean with plain Pytorch code? What I can say is that the monodepth2 training runs with different parameter settings, e.g. different input size, batch size or network architectures. The current configuration is already pretty close to the GPU memory limit. But I will test today whether this memory uage spike in the last batch also happens for other configurations.</p>