Custom forward using multiple GPUs

snowe: <p>Hi everyone <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>I am trying to run my code on multiple GPUs. According to <a href="https://pytorch.org/tutorials/beginner/blitz/data_parallel_tutorial.html" rel="noopener nofollow ugc">this tutorial</a>, this is as easy as passing my model into a function with the corresponding GPU IDs I would like to use.</p>
<p>However, if I have a model that uses a custom forward method with a for loop, will that be handled correctly by the multiple GPUs?</p>
<p>Also: where do I send the images and labels from the batches to? When using a single GPU I do something like:</p>
<pre><code class="lang-auto">for batch in loader:
   # unpack a batch and throw the images and labels on a specific device
   images = batch[0].to(device)
   labels = batch[1].to(device)
</code></pre>
<p>How does that look like for multiple GPUs?</p>
<p>Any help is very much appreciated!</p>
<p>All the best<br>
snowe</p>

JuanFMontesinos: <p>You can pass anything as you want. The only constrain is that any operation carried out must happen in the same device. You can have hybrid forward and mix everything as you want.</p>

snowe: <p>Thank you <a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a>!</p>
<p>But how do I specify certain (multiple) GPUs as my device? Say I have access to 8 GPUs but can only run my code on GPU 7 and 8 because the other ones are occupied. How do I tell PyTorch to just use GPU 7 and 8 and then also grab the images and labels from the corresponding GPU?</p>

JuanFMontesinos: <p>So,<br>
There are two options:</p>
<ol>
<li>You want to run independent process en each GPU (like two training pipes in parallel or so).</li>
<li>You want to use several gpus to train a single experiment.</li>
</ol>
<p>I think you are asking for the case 2.<br>
Soo there are two options in here. The easiest one is using a cuda environment variable.<br>
<code>CUDA_VISIBLE_DEVICES</code><br>
This enviroment variable can be used for any process in your OS. What it does is masking the devices such that the called process can only see the gpus that you allow.<br>
Example:<br>
<code>CUDA_VISIBLE_DEVICES=7,8 python3 run_exp.py</code><br>
Another option is letting the process to see the 8 gpus and choose which ones you want to parallelize over.<br>
The syntax of dataparallel is:</p>
<pre><code class="lang-auto">torch.nn.DataParallel(module, device_ids=None, output_device=None, dim=0)
</code></pre>
<p>You can pass device_ids=[7,8]<br>
The former case is preferred since there is less chance you mess it up.<br>
Note that when you call cuda_visible_devices=7,8, pytorch will only see two gpus.<br>
Thus the indices for those will be (inside python) 0,1 instead of 7,8</p>
<p>In short this module automatically allocates your inputs to the model splitting the batch in as many chunks as gpus you have chosen.<br>
So you don’t really need to take care of allocating them. You just need to ensure that everything inside your nn.Module has been written soft-coding devices. This is, if you need to create a tensor of zeros,<br>
you do something like<br>
<code>var=torch.zeros(10).to(input.device)</code><br>
instead of<br>
<code>vcar=torch.zeros(10).cuda()</code></p>

snowe: <p>Yes you are correct, I am asking for the case 2. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>(1) So, I am running a jupyter notebook on a remote server, is it possible to then say something like?</p>
<pre><code class="lang-auto">CUDA_VISIBLE_DEVICES=7,8 jupyter notebook --no-browser
</code></pre>
<p>(2) Also, do I understand it correctly that the above command will make sure that my notebook accesses the GPUs that have index 7 and 8 <strong>on the server</strong> (let’s say I have 9 GPUs then, otherwise there is no index 8 <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"> ), which I can check with e.g. <code>nvidia-smi</code> but inside my notebook they will have index 0 and 1?</p>
<p>(3) And then I use the command:</p>
<pre><code class="lang-auto">torch.nn.DataParallel(model, device_ids=0,1, output_device=None, dim=0)
</code></pre>
<p>to access the previously defined GPUs?</p>
<p>(4) And how do I need to define the <code>device</code> inside my notebook? When using a single GPU I can say:<br>
<code>device = torch.device('cuda')</code> and then send everything to it with <code>module.to(device)</code><br>
What do I have to put in the brackets instead of ‘cuda’?</p>
<p>Sorry for all the questions, I am relatively new to using multiple GPUs on a remote server <img src="https://discuss.pytorch.org/images/emoji/apple/see_no_evil.png?v=9" title=":see_no_evil:" class="emoji" alt=":see_no_evil:"></p>