How to specify GPU usage?

zhoubinxyz: <p>I am training different models on different GPUs.</p>
<p>I have 4 GPUs indexed as 0,1,2,3</p>
<p>I try this way:</p>
<pre data-code-wrap="python"><code class="lang-python">model = torch.nn.DataParallel(model, device_ids=[0,1]).cuda()
</code></pre>
<p>But actual process use GPU index 2,3 instead.</p>
<p>and if I use:</p>
<pre data-code-wrap="python"><code class="lang-python">model = torch.nn.DataParallel(model, device_ids=[1]).cuda()
</code></pre>
<p>I will get the error:</p>
<blockquote>
<p>RuntimeError: Assertion `THCTensor_(checkGPU)(state, 4, r_, t, m1, m2)’ failed.  at /data/users/soumith/miniconda2/conda-bld/pytorch-cuda80-0.1.8_1486039719409/work/torch/lib/THC/generic/THCTensorMathBlas.cu:230</p>
</blockquote>
<p>How to specify the GPU usage with index?</p>

Seungyoung_Park: <p>I am using Ubuntu 16.04. The GPU indexing are the same as you have.</p>
<p>If you want to execute <a href="http://xxx.py" rel="nofollow noopener">xxx.py</a> using only GPUs 0,1 in Ubuntu 16.04, use the following command as</p>
<pre><code>CUDA_VISIBLE_DEVICES=2,3 python xxx.py
</code></pre>
<p>with nn.DadaParallel in <a href="http://xxx.py" rel="nofollow noopener">xxx.py</a>.</p>
<p>In addition, I don’t think that dataparallel accepts only one gpu.</p>

zhoubinxyz: <p>Thanks a lot, it works <img src="//discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
Hope pytorch can integrate with this argument to specify gpu usage.</p>

apaszke: <p>What’s your PyTorch version? It should accept a single GPU. How is that even possible that it uses last two GPUs if you specify <code>device_ids=[0,1]</code>?</p>
<p>If you run your script with <code>CUDA_VISIBLE_DEVICES=2,3</code> it will <strong>always</strong> execute on the last two GPUs, not on the first ones. I can’t see how that helps in this case. <code>CUDA_VISIBLE_DEVICES=0,1</code> would make more sense.</p>

Seungyoung_Park: <p>I am using pytorch 0.1.9 and Ubuntu 16.04.</p>
<p>When I use CUDA_VISIBLE_DEVICES=2,3 (0,1), ‘nvidia-smi’ tells me that gpus 0,1 (2,3) are used.</p>
<p>I do not know the reason, but the gpu id used in nvidia-smi and the gpu id used in pytorch are reversed.</p>
<p>You can check it if you use Ubuntu 16.04.</p>

shicai: <p>I think it is more likely a cuda/nvidia problem.<br>
I have met this problem before when using Caffe with Tesla K10/K80 GPUs.</p>

albanD: <p><a class="mention" href="/u/seungyoung_park">@Seungyoung_Park</a> from my experience, it’s usually <code>nvidia-smi</code> that is reversed with everything else.<br>
For example, on my machine, the numbering from pytorch agrees with the numbering of the <code>deviceQuery</code> nvidia sample (and any cuda program for that matter) while <code>nvidia-smi</code> is the only one giving a different numbering.</p>

zhoubinxyz: <p>Thanks a lot for your answering.<br>
My pytorch version is 0.1.8.<br>
There may be a numbering problem of GPU device, but it does not affect our usages.<br>
My problem is about how to allocate GPU usages, now everything is fine <img src="//discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

jramapuram: <p>I’m curious about this as well. Can you currently use fractional GPU usage as in tensorflow? The tf equivalent is something like this:</p>
<pre><code class="lang-python">    with tf.device(FLAGS.device):
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=FLAGS.device_percentage)
        sess_cfg = tf.ConfigProto(allow_soft_placement=FLAGS.allow_soft_placement,
                                  gpu_options=gpu_options)
</code></pre>

Brando_Miranda: <p>How does one use GPUs if one has a custom NN class (that inherits from <code>torch.nn.Module</code>)?</p>
<p>For example, I know that using the easy example from (<a href="http://pytorch.org/tutorials/beginner/pytorch_with_examples.html" rel="nofollow noopener">http://pytorch.org/tutorials/beginner/pytorch_with_examples.html</a>) one can just change the type of the tensors being created:</p>
<pre><code>dtype = torch.FloatTensor
# dtype = torch.cuda.FloatTensor # Uncomment this to run on GPU
</code></pre>
<p>however, when using things like <code>torch.nn.Linear</code> and also <code>Variable</code>, how does one make sure to use GPUs?</p>
<p>Also, do I really have to track how GPUs are assigned, I am fine with torch just doing its stuff automagically.</p>
<p>In particular I would love to see how:</p>
<p><a href="http://pytorch.org/tutorials/beginner/examples_nn/two_layer_net_module.html#sphx-glr-beginner-examples-nn-two-layer-net-module-py" rel="nofollow noopener">http://pytorch.org/tutorials/beginner/examples_nn/two_layer_net_module.html#sphx-glr-beginner-examples-nn-two-layer-net-module-py</a></p>
<p>is made into a GPU version of it.</p>
<p>Related SO question: <a href="https://stackoverflow.com/questions/45553613/how-does-one-make-sure-that-everything-is-running-on-gpu-automatically-in-pytorc" rel="nofollow noopener">https://stackoverflow.com/questions/45553613/how-does-one-make-sure-that-everything-is-running-on-gpu-automatically-in-pytorc</a></p>

saurabhjha21: <p>Dear All,</p>
<p>I have installed Nvidia Cuda 9.0 toolkit with Cudnn to my ubuntu machine.<br>
I have installed pytorch when i am trying to check for gpu usage by running the below code -</p>
<h2>Code-</h2>
<h2>import torch<br>
print(torch.rand(2,3).cuda())</h2>
<p>I am getting the below error:</p>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
----&gt; 1 print(torch.rand(2,3).cuda())</p>
<p>~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/_utils.py in <em>cuda(self, device, async)<br>
67         else:<br>
68             new_type = getattr(torch.cuda, self.<strong>class</strong>.<strong>name</strong>)<br>
—&gt; 69             return new_type(self.size()).copy</em>(self, async)<br>
70<br>
71</p>
<p>~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/cuda/<strong>init</strong>.py in _lazy_new(cls, *args, **kwargs)<br>
385     # We need this method only for lazy init, so we can remove it<br>
386     del _CudaBase.<strong>new</strong><br>
–&gt; 387     return super(_CudaBase, cls).<strong>new</strong>(cls, *args, **kwargs)<br>
388<br>
389</p>
<p>RuntimeError: cuda runtime error (2) : out of memory at /opt/conda/conda-bld/pytorch_1518244421288/work/torch/lib/THC/generic/THCStorage.cu:58</p>
<p>I think pytorch is not communicating with the Nvidia GPU, please advise.</p>
<p>Regards<br>
Saurabh Jha</p>

ptrblck: <p>This error might occur after you installed CUDA etc. without restarting your machine.<br>
Have you rebooted after the driver installation?</p>

saurabhjha21: <p>Yes you are correct, it was fine after i restart the machine</p>

Brando_Miranda: <p>for a Unix command soln you can also do:</p>
<pre><code>export CUDA_VISIBLE_DEVICES=$i
</code></pre>
<p>though of course that only works if the scripts are independent and stuff like that…otherwise the other solutions here are probably better…</p>

JannaShen: <p>CUDA_VISIBLE_DEVICES=$i python main.py</p>

labixiaoK: <p>hi， do you have the answer？</p>

DonghunP: <p>Is there anyone who knows that…</p>
<p>When I attach below code in python file(in main.py),</p>
<pre><code class="lang-auto">import os
os.environment["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environment["CUDA_VISIBLE_DEVICES"] = "0"
</code></pre>
<p>it does’t work the same as <code>CUDA_VISIBLE_DEVICES=0 python main.py</code> do.</p>
<p>The former one doesn’t specify(divide) GPU but, the latter one works well.</p>
<p>It seems strange to me.</p>
<p>Thanks ahead.</p>

ptrblck: <p>I wouldn’t recommend the first approach, since you would have to make sure these lines of code are imported before any other library, which might take the GPU. If some script imports PyTorch and these lines are executed afterwards, they won’t have any effect anymore.</p>
<p>The second approach makes sure to mask the devices before running the Python script.</p>

DonghunP: <p>Totally understand thanks!!</p>

LucasX: <ol>
<li>try <code>CUDA_VISIBLE_DEVICES=0,1,2,3 xxx.py</code> to specify GPU</li>
<li>add <code>os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3"</code> in you python code</li>
</ol>