[W CudaIPCTypes.cpp:22] Producer process has been terminated before all shared CUDA tensors released. See Note [Sharing CUDA tensors]

B.W_Zhang: <p>I got this warning at the end of each epoch when using multiple GPUs:<br>
<code>[W CudaIPCTypes.cpp:22] Producer process has been terminated before all shared CUDA tensors released. See Note [Sharing CUDA tensors]</code><br>
But it doesn’t seem to affect the training since the result is as good as it is.<br>
But I would still like to know what the probable cause is and how to solve it  <img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=9" title=":grinning:" class="emoji" alt=":grinning:"></p>
<p>I simply use: <code>model = torch.nn.DataParallel(model)</code> to enable multi-GPUs.<br>
Besides, I also added <code>torch.multiprocessing.set_start_method('spawn', force=True)</code> in my code. Don’t know whether it has any effect on this.</p>
<p>Thank you for the answer in advance.</p>

ptrblck: <p>Are you seeing the same message, if you remove the multiprocessing code (I assume this is triggering it)?</p>

B.W_Zhang: <p>Sorry for my late response and thank you for the reply!<br>
I pinned the trigger of this warning. It happens if I set num_workers &gt; 0 in torch.utils.data.DataLoader, instead of being caused by nn.DataParallel().<br>
Any idea how to fix it? Is it probably due to that I set <code>torch.multiprocessing.set_start_method('spawn', force=True)</code>?</p>

Ilya_Kotlov: <p>I have issued the same problem when setting num of workers to more then 0. <a class="mention" href="/u/ptrblck">@ptrblck</a>, any solution ?</p>

ptrblck: <p>Unfortunately, I don’t have suggestions, as I don’t fully understand the use case of using <code>torch.multiprocessing</code> as well as multiple workers (which itself will use multiple processes), so could you explain the use case a bit more?</p>

visionscaper: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>, I came across the same issue and was intrigued by your remark:</p>
<aside class="quote no-group" data-username="ptrblck" data-post="5" data-topic="124445">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>I don’t fully understand the use case of using <code>torch.multiprocessing</code> as well as multiple workers</p>
</blockquote>
</aside>
<p>For one, isn’t it good to be able to process/load the data using a separate process, apart from the training process(es), in order to not tax them with extra processing load? Further, although it’s not my use case, loading the data might involve some heavy preprocessing, warranting even more than one data loader workers per training process; e.g. in the case in of some vision applications.</p>
<p>Maybe you have a completely different idea about this and give us your insight in to this topic.</p>
<p>Thanks in advance.</p>

ptrblck: <p>I totally agree with you and also think that multiple processes by themselves are useful, should be used for the data loading and process, and would yield a speedup in the overall training pipeline.<br>
This can be easily achieved with the <code>num_workers</code> argument of the <code>DataLoader</code>.<br>
My comment might not have been clear enough, but I was wondering about the use case to use the <code>multiprocessing</code> package manually in the training script as well as multiple workers (which will again spawn multiple processes, so you would end up with a “nested” multiprocessing workload), as I would imagine that using the simpler approach of setting <code>num_workers&gt;=1</code> should already do the job.</p>

visionscaper: <p>Ah, ok, sorry, I didn’t notice the part about using the <code>multiprocessing</code> package separately. I think this is because I had the same issue just using DDP and Dataloaders with num_workers &gt; 0. I get these errors just before my script exits; I already am a good citizen by using <code>dist.barrier()</code> to wait for all training processes to complete before exiting.</p>
<p>When I set the <code>num_workers</code> to zero the script exits smoothly without any errors. In the past I have often used multiple DataLoader workers in a DDP context and never had this issue. I’m wondering if this is something that sneaked in to a recent Pytorch release or is related to recent a CUDA version?</p>
<p>My setup:</p>
<pre><code class="lang-auto">| NVIDIA-SMI 470.86 Driver Version: 470.86 CUDA Version: 11.4     |
</code></pre>
<pre><code class="lang-auto">$ pip show torch
Name: torch
Version: 1.10.0
</code></pre>
<p>This is on a 4x V100 machine with Ubuntu.</p>
<p>It might also be in my script of course.</p>

visionscaper: <p>Ok, I dove a bit more in to this, and found the issue. In this particular instance I am using a model library where the author unfortunately had added the batch collate function as a method to the model class. Because of this the model <code>self</code> is also transported to the DataLoader workers.</p>
<p>When the training scripts exits the DataLoader worker processes are killed too, without properly dealing with the Tensors that are copied to the worker.</p>
<p>I refactored all the batch collating code out of the model class and thus made it independent of the <code>self</code> of the model instance. This resolved the issue, exiting the script is very smooth now!</p>
<p>So, if anyone gets this error, it could be because your are copying the model over to the DataLoader workers without knowing it.</p>

sjtuhl: <p>Hi <a class="mention" href="/u/visionscaper">@visionscaper</a>, I’m encountering the same issue. I’m currently using pytorch lightning to write my model. When I set the num_worker &gt; 0 in the dataloader, the warning will occur. I’m a beginner in pytorch. Could you give me some example that how did you refactor your batch collating code out of model class? I don’t know how to do it.</p>

visionscaper: <p>Hi <a class="mention" href="/u/sjtuhl">@sjtuhl</a>, if you have a similar issue to mine, somehow your model is linked to your <code>dataset</code>, <code>collate_fn</code> or other object given to a <code>DataLoader</code>. Because of this, when you set <code>num_worker &gt; 0</code>, the model is copied to the <code>DataLoader</code> workers. For instance, if your <code>dataset</code> is a property of your model class instance (which is just bad design), not only your dataset is copied to the workers, but everything it is “attached” to as well.</p>
<p>I hope this makes sense to you. I can’t really help you without inspecting your code.</p>

Eveefun: <p>Hi <a class="mention" href="/u/visionscaper">@visionscaper</a>, I have the same issue as you. My data collate fn is written inside the dataset model. There are some tensors my collate_fn need, stored in the dataset model. I wonder if there is a way to avoid moving the collate_fn out and escape the error?<br>
<img src="https://discuss.pytorch.org/images/emoji/apple/hugs.png?v=12" title=":hugs:" class="emoji only-emoji" alt=":hugs:" loading="lazy" width="20" height="20"></p>

Garret_Kaye: <p>This warning is caused by tensors (or other objects) that are not being properly cleaned up (released) on the CUDA device before the process they belonged to was terminated.</p>
<p>As it was for me in my case, and seems to be the case for mostly everyone else, the culprit behind this warning has to do with moving objects to, or owning objects on, the CUDA device from inside of one of the <code>DataLoader</code> worker processes, and the worker process being terminated before the  objects are released from CUDA.</p>
<p>Moving tensors to CUDA inside a <code>collate_fn</code> that is passed to a <code>DataLoader</code> where <code>num_workers &gt; 0</code> - which just means that a child process will be created for each worker - is an example of this, and is exactly what I was doing. In my training loop then, after all the training samples were iterated through, my <code>DataLoader</code> worker processes would be terminated and I would get this warning.</p>
<p>As tempting as it is to move batches to CUDA inside the <code>collate_fn</code>, and though it may even provide some performance gains, the potential for a memory leak or some other resource management issue along with the annoying warning made me rethink my strategy. I ended up just relocating this logic to the first line in my training loop (on CPU - main process) where I move each batch to CUDA before my forward pass which I think is generally recommended anyway - never looked back since.</p>
<p>Alternatively, if you really wanted to, you could keep whatever objects you want on the CUDA device inside your <code>DataLoader</code> worker processes if you set <code>persistent_workers=True</code> in your <code>DataLoader</code> constructor and not encounter this warning until your <code>DataLoader</code> is entirely deallocated. You may encounter other issues though like running out of memory on the GPU, but I think for some use cases, maybe doing this could be beneficial.</p>
<p>Hope this helps.</p>

zsxlkn: <p>Very helpful,Thank you.The issue has been solved.</p>