Multiprocessing vs. Nvidia MPS for parallel training on a single GPU

alexgo: <p>I’m interested in parallel training of multiple instances of a neural network model, on a single GPU. Of course this is only relevant for small models which on their own, don’t utilize the GPU well enough.<br>
According to <a href="https://pytorch.org/docs/master/notes/multiprocessing.html#sharing-cuda-tensors" rel="noopener nofollow ugc">this</a>, Pytorch’s multiprocessing package allows to parallelize CUDA code.</p>
<p>I think that with slight modification of <a href="https://github.com/pytorch/examples/tree/master/mnist_hogwild" rel="noopener nofollow ugc">this example code</a>, I managed to do what I wanted (train several models) instead of training a single model using the Hogwild algorithm, and it worked pretty well. I managed to train 10 models in less then 150% the time it took to train a single one.</p>
<p>Is this functionality related in any way to <a href="https://docs.nvidia.com/deploy/pdf/CUDA_Multi_Process_Service_Overview.pdf" rel="noopener nofollow ugc">Nvidia’s MPS</a> capability? I suspect not, because it doesn’t seems to involve anything related to starting an MPS server from the system, setting the GPU to work in EXCLUSIVE mode and so on.</p>
<p>I understand that MPS is the way in which Nvidia supports CUDA multithreading/multiprocessing. Otherwise, it’s not really possible to tell the GPU to do things in parallel.<br>
Is this true? if it is then what exactly multiprocessing does and how does it work so well on the GPU without using MPS? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>Can anyone point me to an example which does demonstrate the use of MPS with Pytorch? I couldn’t really find one. And strangely enough, the above 28-page Nvidia guide on MPS doesn’t include any example in Pytorch or any other leading framework.</p>
<p>Thanks!</p>

mrshenli: <p>cc <a class="mention" href="/u/ngimel">@ngimel</a> <a class="mention" href="/u/mcarilli">@mcarilli</a>  for NVidia MPS</p>

mcarilli: <blockquote>
<p>I understand that MPS is the way in which Nvidia supports CUDA multithreading/multiprocessing.</p>
</blockquote>
<p>hmm we need to be more specific.  Each process receives its own cuda context on each device used by the process.  Per-device contexts are shared by all CPU threads within the process.  Any CPU thread in the process may submit work to any cuda stream (the kernel launch and stream API are thread safe), and the work may run concurrently with work submitted from other CPU threads.  And of course, each kernel may use thousands of GPU threads.</p>
<p>By default (without MPS) each device runs kernels from only one context (process) at a time.  If several processes target the same device, their kernels can’t run concurrently and GPU context switches between processes will occur.  MPS multiplexes kernels from different processes so kernels from any thread of any process targeting that device CAN run concurrently (not sure how MPS works at a low level, but it works).</p>
<p>MPS is application-agnostic.  After starting the MPS daemon in your shell:<br>
<code>nvidia-cuda-mps-control –d</code><br>
all processes (Python or otherwise) that use the device have their cuda calls multiplexed so they can run concurrently.  You shouldn’t need to do anything pytorch-specific:  start the MPS daemon in the background, then launch your pytorch processes targeting the same device.</p>
<p>One thing I don’t know is whether nccl allreduces in Pytorch can handle if data from all processes is actually on one GPU.  I’ve never seen it tried.  Sounds like your case doesn’t need inter-process nccl comms though.</p>
<p>MPS has been around for years, and works on any recent generation.  It is NOT the same thing as “multi-instance GPU” or MIG, which is Ampere-specific.  (I think MIG sandboxes client processes more aggressively than MPS, providing better per-process fault isolation among other things.  MPS should be fine for your case.)</p>

alexgo: <p>Thanks for your reply!</p>
<aside class="quote no-group" data-username="mcarilli" data-post="3" data-topic="99877">
<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/mcarilli/48/28517_2.png" class="avatar"> mcarilli:</div>
<blockquote>
<p>Any CPU thread in the process may submit work to any cuda stream (the kernel launch and stream API are thread safe), and the work may run concurrently with work submitted from other CPU threads.</p>
</blockquote>
</aside>
<p>One thing I don’t understand then, is how come in practice <a href="https://github.com/pytorch/examples/tree/master/mnist_hogwild" rel="noopener nofollow ugc">this example</a> can parallelize training (on single GPU), without MPS, and yet it doesn’t even use different cuda streams?<br>
Does it mean it just happens on high level due to better utilization of “idle” times, where the GPU is not busy, and these times now can be filled by commands from other threads/processes? If that’s the case it’s surprising that the improvement is almost linear in the number of processes.</p>
<aside class="quote no-group" data-username="mcarilli" data-post="3" data-topic="99877">
<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/mcarilli/48/28517_2.png" class="avatar"> mcarilli:</div>
<blockquote>
<p>By default (without MPS) each device runs kernels from only one context (process) at a time. If several processes target the same device, their kernels can’t run concurrently and GPU context switches between processes will occur.</p>
</blockquote>
</aside>
<p>So it means that without MPS, work submitted from different CPU <strong>processes</strong> cannot run concurrently on the same GPU, but work submitted from different <strong>threads</strong> can (via streams)?<br>
If I understand correctly, the example code above uses processes?</p>

mcarilli: <aside class="quote no-group" data-username="alexgo" data-post="4" data-topic="99877">
<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/a/71e660/48.png" class="avatar"> alexgo:</div>
<blockquote>
<p>One thing I don’t understand then, is how come in practice <a href="https://github.com/pytorch/examples/tree/master/mnist_hogwild" rel="noopener nofollow ugc">this example</a> can parallelize training (on single GPU), without MPS, and yet it doesn’t even use different cuda streams?</p>
</blockquote>
</aside>
<p>Without MPS, I don’t see how this example could run work from different processes concurrently.  It’s possible the model is so small, and each process’s individual utilization is so low, that you observe a speedup with multiple processes even if they aren’t running concurrently.  Hard to tell without <a href="https://gist.github.com/mcarilli/376821aa1a7182dfcf59928a7cde3223" rel="noopener nofollow ugc">profiling</a>.</p>
<blockquote>
<p>work submitted from different CPU <strong>processes</strong> cannot run concurrently on the same GPU, but work submitted from different <strong>threads</strong> can (via streams)?</p>
</blockquote>
<p>Streams are free-floating from threads.  Any thread may submit work to any stream.</p>
<ol>
<li>If all threads submit work to the same stream, those kernels will be serialized, even if the threads are in the same process.  However, no context switches will be required.</li>
<li>If threads in the same process submit work to different streams, the kernels may run with true concurrency (overlapping).</li>
<li>If threads in different processes submit work to the same device, without MPS the kernels may not run concurrently regardless what streams are used, because they are in separate contexts.  MPS allows processes to submit work to the device without a context switch.  I believe (but i’m not sure) kernels may run with true concurrrency (ie, overlapping) even if processes each use their default stream.  But even if the kernels from different processes can’t truly overlap, MPS increasing the density of work submission by avoiding expensive context switches is beneficial.</li>
</ol>
<p>Pytorch does not compile to use per-thread default streams.  By default, all threads submit work to a shared default stream (called “legacy” default stream in some cuda docs).  Therefore, unless you manually set <a href="https://pytorch.org/docs/stable/cuda.html#torch.cuda.stream" rel="noopener nofollow ugc">stream contexts</a> in each thread, case 1 applies.</p>

alexgo: <aside class="quote no-group" data-username="mcarilli" data-post="5" data-topic="99877">
<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/mcarilli/48/28517_2.png" class="avatar"> mcarilli:</div>
<blockquote>
<p>Without MPS, I don’t see how this example could run work from different processes concurrently</p>
</blockquote>
</aside>
<p>Not only that it works concurrently (in the sense that running the example with 10 processes vs. 1 results in much less than 10x slowdown), but in this case, MPS almost doesn’t help further.<br>
I think I’ve convinced myself by now that I know how to start and stop an MPS server. I modified this code to use a simpler network (MLP) and not use a dataloader but instead just have all data in memory, and in this case MPS showed some improvement, but still far from linear. That’s how I knew that MPS works.</p>
<p>But okay, I guess this doesn’t explain much without profiling like you said. Maybe this example is too complex. Maybe there are idle GPU times implied in it which allow the speedup without MPS, and don’t allow MPS to help because the Cuda computations aren’t actually possible to parallelize due to low utilization.</p>
<p>That’s why it would be very helpful to see some very minimal Pytorch example that can demonstrate good MPS usage, i.e. if you set num_processes=1, then the run takes X seconds, and then you change it to num_processes=10, and the runtime stays about X seconds (plus a small overhead), but without MPS it would be 10X.<br>
Maybe you could provide a clue on how to write such minimal example? i.e, should I use torch.multiprocessing as in the example above? or something else for dispatching processes to the GPU?</p>

alexgo: <aside class="quote no-group" data-username="mcarilli" data-post="5" data-topic="99877">
<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/mcarilli/48/28517_2.png" class="avatar"> mcarilli:</div>
<blockquote>
<ul>
<li>If all threads submit work to the same stream, those kernels will be serialized, even if the threads are in the same process. However, no context switches will be required.</li>
<li>If threads in the same process submit work to different streams, the kernels may run with true concurrency (overlapping).</li>
<li>If threads in different processes submit work to the same device, without MPS the kernels may not run concurrently regardless what streams are used, because they are in separate contexts. MPS allows processes to submit work to the device without a context switch. I believe (but i’m not sure) kernels may run with true concurrrency (ie, overlapping) even if processes each use their default stream. But even if the kernels from different processes can’t truly overlap, MPS increasing the density of work submission by avoiding expensive context switches is beneficial.</li>
</ul>
</blockquote>
</aside>
<p>That’s interesting. So according to this (case 3) I understand that this example may not be optimal for execution with MPS. It might be beneficial to try and further modify this example, and make sure that each process does it’s Cuda work on a different stream? because I don’t think this is the case now. I will try it out. Thanks!</p>

zzsunshine: <p>I’m interested in the training of multiple neural network models on a single GPU as well. For example, running MobileNet and ResNet on a single GPU at the same time. Is there any example code how could I implement that on PyTorch?  with MPS and without MPS (naive to running).</p>
<p>Appreciate for any help <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=10" title=":smiley:" class="emoji" alt=":smiley:"></p>

Emile_Courthoud: <p>On Kubernetes, you can check out this open source project called nos <a href="https://github.com/nebuly-ai/nos" rel="noopener nofollow ugc">https://github.com/nebuly-ai/nos</a>. It allows you to use MIG and MPS partitioning automatically and dynamically, so that you just need to input the amount of resources your workload/pod needs (e.g. 20 GB of memory) and this library will create the GPU partition accordingly.</p>
<p>I was involved in the development, so ask me questions if you have any</p>