CUDA out of memory when calculating through the VGG layer

username: shirui-japina
cooked: <p>I’m studying the modification of SSD model and referring <a href="https://github.com/amdegroot/ssd.pytorch" rel="noopener nofollow ugc">here</a>.</p>
<p>What puzzles me is that there will be a problem in the calculation process of the VGG layer.</p>
<p><em>train.py</em></p>
<pre><code class="lang-auto">...
for iteration in range(args.start_iter, cfg['max_iter']):
    ...
    out = net(images)
    ...
</code></pre>
<p><em>ssd.py</em></p>
<pre><code class="lang-auto">def forward(self, x):
    for k in range(23):
        x = self.vgg[k](x).detach()
        print(torch.cuda.memory_allocated() / 1024**2)
</code></pre>
<ul>
<li>When batch_size is set to <strong>16</strong>, the process of training the model can run normally.<br>
Below is what it prints:</li>
</ul>
<blockquote>
<p>461.8271484375<br>
461.8271484375<br>
461.3896484375<br>
461.3896484375<br>
197.7177734375<br>
285.6083984375<br>
285.6083984375<br>
285.6083984375<br>
285.6083984375<br>
153.7724609375<br>
197.7177734375<br>
197.7177734375<br>
197.7177734375<br>
197.7177734375<br>
197.7177734375<br>
197.7177734375<br>
132.3896484375<br>
154.9521484375<br>
154.9521484375<br>
154.9521484375<br>
154.9521484375<br>
154.9521484375<br>
154.9521484375</p>
</blockquote>
<ul>
<li>When batch_size is set to <strong>32</strong>, it will <strong>CUDA out of memory</strong>.<br>
Below is what it prints:</li>
</ul>
<blockquote>
<p>830.306640625<br>
830.306640625<br>
Traceback (most recent call last):<br>
…<br>
RuntimeError: CUDA out of memory. Tried to allocate 3.09 GiB (GPU 0; 8.00 GiB total capacity; 1.50 GiB already allocated; 3.52 GiB free; 2.44 GiB reserved in total by PyTorch)</p>
</blockquote>
<ul>
<li>And even sometimes it will OOM when batch_size is set to <strong>16</strong>,<br>
Then it can run nomally after <strong>reboot</strong>.<br>
Below is what it prints when OOM:</li>
</ul>
<blockquote>
<p>461.8349609375<br>
461.8349609375<br>
461.3974609375<br>
461.3974609375<br>
197.7255859375<br>
285.6162109375<br>
285.6162109375<br>
285.6162109375<br>
285.6162109375<br>
153.7802734375<br>
197.7255859375<br>
197.7255859375<br>
197.7255859375<br>
197.7255859375<br>
197.7255859375<br>
197.7255859375<br>
132.3974609375<br>
Traceback (most recent call last):<br>
…<br>
RuntimeError: CUDA out of memory. Tried to allocate 4.26 GiB (GPU 0; 8.00 GiB total capacity; 177.52 MiB already allocated;<br>
5.51 GiB free; 474.00 MiB reserved in total by PyTorch)</p>
</blockquote>
<p>The questions I want to ask:</p>
<ol>
<li>
<p>Why does <em><strong>the calculation of the VGG layer</strong></em> cause CUDA OOM?<br>
This is <strong>NOT</strong> the process of inputting the image to the GPU.</p>
</li>
<li>
<p>Why it can loop for twice? (prints twice) when batch_size is set to <strong>32</strong>.</p>
</li>
<li>
<p>Why OOM happens a bit randomly? is something not cleaned up in the GPU during the last run? (something that can only be cleaned <em><strong>by reboot</strong></em>?)</p>
</li>
<li>
<p>How to solve this problem?</p>
</li>
</ol>
<hr>
<p>OS: Windows 10<br>
Python: 3.6.8<br>
Pytorch: 1.5.1<br>
nvcc: 10.2<br>
GPU: NVIDIA GeForce RTX 2070 SUPER (8192MB)</p>

username: Scott_Hoang
cooked: <p>CUDA OOM is simply when there isn’t enough RAM space on your video card to store all variables.<br>
The variables are your model’s parameters, your data’s physical size, model’s graphs, optimizers’s variables, and various inner parameters PyTorch keeps, plus any unfreed parameters from the previous run.<br>
So to answer your question in order:</p>
<ol>
<li>calculation of the VGG generates temp parameters to be store in Cuda which can cause OOM.</li>
<li>Unfreed parameters from the previous run added up to cause OOM.</li>
<li>OOM happens randomly due to various reasons, but mostly due to a combination of unfreed parameters, creation of temporary variables for calculation, …etc</li>
<li>
</li></ol>
<p>a)Reduce your physical batch size, and employ gradient accumulation in software to achieve higher virtual batch size.<br>
b)Be vigilant on how you send data to Cuda. Define a fixed size torch Variable in Cuda and clone your data in CPU to this allocated space to reduce overhead and save space (but might make your model run a teeny bit slower)<br>
c) Take advantage of rtx20xx tensor cores and use fp16 to double your batch size and halve your run time. Take a look at <a href="https://github.com/NVIDIA/apex" rel="nofollow noopener">https://github.com/NVIDIA/apex</a>.</p>