Inspecting memory usage with DDP and workers

Seo: <p>Hello everyone,<br>
I am facing some memory issues running my model on multiple GPUs with DDP. I want to report to you the experiments I made to understand the memory utilization of the combination of workers + DDP. In all the experiments I will report in this post I will use always the same model, so the size of the model is always the same.</p>
<p>I have 4 GPUs with 16GB each. First of all, I used only one to understand the highest batch that fits on it. The maximum batch size is 64 (I started to have CUDA OOM with 128). Then, in order to increase the batch size, I started to use DDP with batches of 64 for each process (1 process per GPU). Using 2 GPUs with a total batch of 128 caused CUDA OOM. I have read online that the DDP uses part of the GPU memory to store the communication bucket. So I assume that this communication bucket does not fit into memory. I started to decrease the per-GPU batch size to 60, then 55, and finally to 50 (resulting in a total batch size of 100 on 2 GPUs). Unfortunately, none of these batch sizes worked, For curiosity, I started to analyze the memory usage of 1 single GPUs with a batch of 1. The total memory usage is 3.4G. I then tried to use DDP with a batch of 2 (1 per-GPU) resulting in 3.9G. This experiment demonstrates the overhead made by DDP. Finally, I tried to increase the number of workers and I have found that the final memory usage on GPU increases. This last founding shocked me because I thought that the workers simply increase the RAM usage, not GPU.</p>
<p>More specifically, whenever I use DDP I have several processes per GPU. The first process I see is the one performing the training (3.9G), then there are N_GPUS-1 processes that I suppose are in charge of keeping the communication with the other processes opened (0G each), and finally, the other ones should be the workers (0.6G each). Thus the memory usage on each process is 3.9G + num_workers*0.6G.</p>
<p>It’s not clear to me why the workers have a memory footprint on the GPU. Can someone help me to understand?</p>

ptrblck: <aside class="quote no-group" data-username="Seo" data-post="1" data-topic="114478">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/seo/48/33346_2.png" class="avatar"> Seo:</div>
<blockquote>
<p>Finally, I tried to increase the number of workers and I have found that the final memory usage on GPU increases. This last founding shocked me because I thought that the workers simply increase the RAM usage, not GPU.</p>
</blockquote>
</aside>
<p>This shouldn’t be the case. Could you share an executable code snippet, which shows an increase in GPU memory using <code>CPUTensors</code> in the <code>Dataset</code> after increasing the workers in the <code>DataLoader</code>?</p>

Seo: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a> , the code I am using is spread among different files so as soon as I can I will create a code snippet and try to reproduce this issue.<br>
In the meantime I want to share with you the findings:</p>
<p>1 GPU, batch=64, 0 workers:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/2/7/270fdd39b7ab747b68615e02cadfdf897c214a9a.png" alt="1g_0w" data-base62-sha1="5zyBPrFG0a2PDauqF0rAqIeuHRE" width="678" height="318"></p>
<p>1 GPU, batch=64, 7 workers:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/4/a/4a34c187e0d244bcfe7f44673196171b6ba2a870.png" alt="1gpu_7w" data-base62-sha1="aAsmrrxECQIOuOBpmqE68k1k3uM" width="678" height="308"></p>
<p>2 GPUs, batch=128, 0 workers:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/1/b/1b13f915eb08c94e5440cb40877d424ab14a1ced.png" alt="2g_0w" data-base62-sha1="3RxGnTUUu17Z5HxPp6bzlUCSXPT" width="650" height="386"></p>
<p>2GPUs, batch=128, 1 worker:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/d/6/d6c655923c414677720800190ad06ea7ad84c113.png" alt="2g_1w" data-base62-sha1="uDZ6GFoJQx7Qv2PeEnkkdAy9GbF" width="647" height="431"></p>
<p>As you can see the last one has different processes spawned per GPU and is the only one causing OOM (batch=128 with 0 workers instead is fine). The screenshot was taken just before the OOM.<br>
My DataLoader uses <code>pin_memory=True</code>, so I exclude to have <code>CUDATensor</code> inside the <code>__getitem__</code>, otherwise an exception occurs. I also used <code>.to(device, non_blocking=True)</code>.</p>

ptrblck: <p>It seems that an additional CUDA context is created, which is wrong.<br>
A code snippet would be helpful and needed to isolate this issue.</p>

Seo: <p>I created a simpler version of my code. If you run this code you will find the unusual worker processes to be spawned on GPUs. I have found out that these processes are spawned only if I set a custom  <code>collate_fn</code> into my <code>DataLoader</code>. If you use the default <code>collate_fn</code> the strange processes do not seem to spawn. It is not yet clear to me the why.</p>
<p>code: <a href="https://pastebin.com/cbvYsYce" class="inline-onebox" rel="noopener nofollow ugc">PyTorch DDP memory inspection - Pastebin.com</a></p>

ptrblck: <p>Could you upload the script to e.g. GitHub as a Gist, as I cannot access pastebin?</p>

Seo: <p>here: <a href="https://gist.github.com/seo-95/f3f95553dfe81f7e780be304449f7879" class="inline-onebox" rel="noopener nofollow ugc">DDP Memory inspection · GitHub</a></p>

Seo: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> I tried again to debug the code. The problem is there every time I use a <code>collate_fn</code> that is a method of a class that contains the <code>model</code> as one of its attributes (<code>self.model</code>). Each time this happens, there is a new cuda context created. I don’t know if this can be considered a bug or it is a normal behavior known by the developers</p>

Seo: <p>Another thing that I have noticed: even if I use the default <code>collate_fn</code> (without specifying a custom one), additional processes are created on GPUs with 0MiB each (even with 0 workers). The number of these processes are equal to N_GPUS-1, so I suppose they are in charge of keeping the connection opened with the other GPUs. Am I right?</p>

ptrblck: <p>Using the model or any CUDA operations in the <code>collate_fn</code> is uncommon, I think (at least I haven’t seen a use case so far) and I guess not well tested.<br>
The additional CUDA context creation might come from the usage of multiprocessing.<br>
I also don’t know what kind of 0MB process is created as I haven’t seen it before.</p>
<p>In your code snippet you are also using multiprocessing with DDP. What’s the reason for not using DDP directly?</p>

Seo: <p>I implemented again everything with DDP without multiprocessing. Now it works, thanks!</p>