Errors when using num_workers>0 in DataLoader

simoloca: <p>Hi all, I’m facing a problem when setting the num_workers value in the DataLoader bigger than 0.<br>
In particular I’m trying to train a custom model on a custom dataset.<br>
Thus, if I hold the num_workers=0, everything it’s fine and the whole process is successful.<br>
But for every other configuration on num_workers, the problem persist for every setting I try with the batch_size used, number of epochs for traning, ecc.<br>
And it seems that when num_workers is greater than 0, x for istance, the script try to run x times and then the error came out.</p>
<p>The strange issue comes when I try to run the script with the bottleneck utility with the num_workers settings bigger then 0, and in this way it works correctly.<br>
So my question is if the bottleneck utility applies some type of optimization that I don’t do, or when we want to set the num_workers bigger than 0 we need to so something in particular.</p>
<p>P.S.<br>
I want to use the num_worker&gt;0 in order to push my GPU to the max usage.</p>
<p>Thanks.</p>

BramVanroy: <p>Num_workers sets the number of CPU workers in the data loader only. This has nothing to do with GPU utilization - although faster batch preprocessing will lead to batches being loaded faster and thus more streamlined GPU usage. On Windows, due to multiprocessing restrictions, setting num_workers to &gt; 0. This is expected, so don’t worry about it. You can set it to 0.</p>

simoloca: <p>Thanks for the reply!<br>
But I saw that when I run the program with the bottleneck tool, where num_workers is &gt; 0, the GPU “cuda usage” is much more efficient and stable, in the sense that it is always at about 95-100%.<br>
Instead, when I had to run the script normally without the bottleneck tool and num_worker is 0, the GPU cuda usage is more discontinuous (it goes to 50%, to 90% and it turns to 25 and so on), unstable.<br>
So, this behaviour make me think that it has something to do with the gpu usage.</p>

simoloca: <p>Ok, it seems that I have solved the problem, and now I can set the num_workers&gt;0.<br>
What I did was just to insert all the code that train the network in a function: train(), summaring:</p>
<pre><code class="lang-auto">def train():
    # Here was inserted the whole code that train the network ...
if __name__ == '__main__':
    train()
</code></pre>
<p>Could anyone explain how and why this works?</p>

BramVanroy: <p>You can have a look here: <a href="https://docs.python.org/3.7/library/multiprocessing.html#multiprocessing-programming" rel="noopener nofollow ugc">https://docs.python.org/3.7/library/multiprocessing.html#multiprocessing-programming</a></p>
<p>Note that Windows can still cause issues here, be it pickling errors or slow performance. See <a href="https://github.com/pytorch/pytorch/issues/12831" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/issues/12831</a>. The safest approach is using 0 on Windows.</p>

simoloca: <p>Ok, thanks you anyway!</p>

kartikpodugu: <p>Thanks for the solution. It worked for me.<br>
But, same question. How did it solve the problem?</p>

algeriapy: <p>Great !! <img src="https://discuss.pytorch.org/images/emoji/apple/partying_face.png?v=12" title=":partying_face:" class="emoji" alt=":partying_face:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/partying_face.png?v=12" title=":partying_face:" class="emoji" alt=":partying_face:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/partying_face.png?v=12" title=":partying_face:" class="emoji" alt=":partying_face:" loading="lazy" width="20" height="20"><br>
if works, but the question how it works <img src="https://discuss.pytorch.org/images/emoji/apple/face_with_spiral_eyes.png?v=12" title=":face_with_spiral_eyes:" class="emoji" alt=":face_with_spiral_eyes:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/face_with_spiral_eyes.png?v=12" title=":face_with_spiral_eyes:" class="emoji" alt=":face_with_spiral_eyes:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/face_with_spiral_eyes.png?v=12" title=":face_with_spiral_eyes:" class="emoji" alt=":face_with_spiral_eyes:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/face_with_spiral_eyes.png?v=12" title=":face_with_spiral_eyes:" class="emoji" alt=":face_with_spiral_eyes:" loading="lazy" width="20" height="20"></p>
<p>Thanks for sharing it !!!</p>