DDP Tutorial Questions

username: Jo3
cooked: <p>I am attempting to follow <a href="https://pytorch.org/tutorials/intermediate/ddp_tutorial.html" rel="noopener nofollow ugc">this tutorial</a> to train a neural network over multiple GPUs on the same windows machine (using gloo), and do not fully understand the reasons why the example code is structured in the way presented. Does anyone know any other good tutorials on how to do this? I’m trying to utilize all my GPUs on my computer and do not fully understand how to write a full training and testing loop using DPP.</p>
<p>I’ve read several tutorials, but still have a few questions:</p>
<p>Is it possible to use DPP in a jupyter notebook?</p>
<p>why do all the functions take “rank” as an argument, but never define this value anywhere?</p>
<p>If you have your own boilerplate method for training a neural network in a loop how do you incorporate these techniques? do you only parallelize the model and let the computer determine where to send it, or do you need to parallelize everything as outlined in <a href="https://pytorch.org/tutorials/intermediate/dist_tuto.html" rel="noopener nofollow ugc">this tutorial</a>?</p>
<p>How would you “setup” your environment to access GPUs on the same computer? How do you know what port to use? Also, how is the “init_method” file used in windows computers? How would you pick one?</p>
<p>Even though Pytorch does not recommend using data parallel over DPP, is there a way to use it with a gloo backend? It seems easier to implement than the DPP method, and for that reason it might be easier to use for newbies.</p>
<p>Thank you,<br>
Joe</p>

username: mrshenli
cooked: <aside class="quote no-group" data-username="Jo3" data-post="1" data-topic="150874">
<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/j/8baadc/48.png" class="avatar"> Jo3:</div>
<blockquote>
<p>I’m trying to utilize all my GPUs on my computer and do not fully understand how to write a full training and testing loop using DPP.</p>
</blockquote>
</aside>
<p>I assume you are referring to DDP (DistributedDataParallel) instead DPP (Data-Preprocessing Pipeline) in this post.</p>
<blockquote>
<p>Is it possible to use DPP in a jupyter notebook?</p>
</blockquote>
<p>I wouldn’t say it’s impossible, but could be tricky, as notebook is not very compatible with multi-processing.</p>
<blockquote>
<p>why do all the functions take “rank” as an argument, but never define this value anywhere?</p>
</blockquote>
<p>That’s <code>torch.multiprocessing.spawn</code>’s default behavior. It will pass the rank automatically to the target function as the first argument. See the API doc below.</p>
<p><a href="https://pytorch.org/docs/stable/multiprocessing.html#torch.multiprocessing.spawn" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/multiprocessing.html#torch.multiprocessing.spawn</a></p>
<blockquote>
<p>If you have your own boilerplate method for training a neural network in a loop how do you incorporate these techniques? do you only parallelize the model and let the computer determine where to send it, or do you need to parallelize everything as outlined in <a href="https://pytorch.org/tutorials/intermediate/dist_tuto.html" rel="noopener nofollow ugc">this tutorial</a>?</p>
</blockquote>
<p>DDP operates on the process level (see the minimum DDP example: <a href="https://pytorch.org/docs/stable/notes/ddp.html" class="inline-onebox" rel="noopener nofollow ugc">Distributed Data Parallel — PyTorch 2.1 documentation</a>). It does not care how you launch those processes, or where those processes locate. In your code, you just need to replace your local model with <code>DDP(model, ...)</code>, and then it will take care of gradient synchronization for you.</p>
<p>Regarding how to launch the processes, you can use any tools you prefer. For single-machine multi-GPU, <code>mp.spawn()</code> is sufficient. For multiple machines, many people directly use Slurm or use torchrun (<a href="https://pytorch.org/docs/stable/elastic/run.html" class="inline-onebox" rel="noopener nofollow ugc">torchrun (Elastic Launch) — PyTorch 2.1 documentation</a>).</p>
<blockquote>
<p>How would you “setup” your environment to access GPUs on the same computer? How do you know what port to use?</p>
</blockquote>
<p>You just need to specify the master address and master port. All ranks will then talk to that master during rendezvous to discover each other (i.e., IPs and ports). After that all processes can directly talk to each other.</p>
<blockquote>
<p>Also, how is the “init_method” file used in windows computers? How would you pick one?</p>
</blockquote>
<p>You can use a file on an NFS as the init method. As long as all processes can access that file, it should work. I believe the TCP init method also works on Windows today. Let us know if you hit issues there. cc <a class="mention" href="/u/h-huang">@H-Huang</a></p>
<blockquote>
<p>Even though Pytorch does not recommend using data parallel over DPP, is there a way to use it with a gloo backend?</p>
</blockquote>
<p>DataParallel (DP) is single-process multi-thread, and is slower than DDP. If you are using single-machine multi-GPU and are OK with DP’s perf, yep, DP is indeed an easier entry point. It does not need Gloo or NCCL ProcessGroup at all, as there is only one process and it can directly access all Tensors.</p>