Multiple but different gpus

t-bliga1: <p>Hey guys ,</p>
<p>is it in general possible to use the data.parallel wrapper if you got two different gpus</p>
<p>thanks Tobi</p>

ptrblck: <p>Yes, that’s possible.<br>
However, you will get a warning, if there is an imbalance in the GPU memory (one has less memory than the other).<br>
Also, your performance should depend on the slowest GPU you are using, so it might not be recommended, if you are using GPUs with a very different performance profile.</p>

t-bliga1: <p>thanks for the reply,</p>
<p>I got another question it seems like that the two gpus get the same distributions of the data , if the dataset is to large then i got a cuda out off memory error, but for small dataset this is no problem . Do you have any idea how to fix this?</p>
<p>best, Tobi</p>

ptrblck: <p>Have a look at <a href="https://medium.com/huggingface/training-larger-batches-practical-tips-on-1-gpu-multi-gpu-distributed-setups-ec88c3e51255">this blog post</a> to see how <code>nn.DataParallel</code> works internally and how to counter some effects of an imbalanced memory usage.</p>
<p>PS: you could also try out <code>nn.DistributedDataParallel</code>, which shouldn’t introduce the imbalance.</p>

sevagh: <p>Apologies for the old thread resurrection here.</p>
<p>If I have a single machine with 2 mismatched GPUs (1 fast with big memory, 1 slow with small memory), DataParallel will only go as fast as the slow GPU with less memory, making it not worth using - as was covered above.</p>
<p>Does DistributedDataParallel on a single machine with 2 mismatched GPUs make sense? Or am I better off simply using the single fast GPU?</p>
<p>I’m trying to understand the usages of DistributedDataParallel (SPSD, SPMD, etc.)</p>
<p>I’ve been trying to read this and getting a bit confused: <a href="https://github.com/pytorch/pytorch/issues/47012" class="inline-onebox" rel="noopener nofollow ugc">[POLL][RFC] Can we retire Single-Process Multi-Device Mode from DistributedDataParallel? · Issue #47012 · pytorch/pytorch · GitHub</a></p>

ptrblck: <p>Both parallel approaches would suffer from the slow GPU, so you would have to check the actual performance using the fast GPU only vs. <code>DistributedDataParallel</code>.</p>

Mole_m7b5: <p>I’ve got a similar issue, but in my instance it’s not to much the speed I’m worried about.</p>
<p>I’ve got a machine with a single 3090, and ideally would purchase another but they seem hard to come by since the release of the 40xx series.</p>
<p>If I buy a 4090 and put it in the same machine am I right in that I’ll be limited by the speed of the 3090, but I will be able to take advantage of the extra memory capacity from having two gpus?</p>

ptrblck: <p>This could be the case as the slowest part of your pipeline would create the bottleneck.<br>
It’s unclear if this would be the 3090 (or e.g. the data loading) and depends on your actual use case.</p>

Mole_m7b5: <p>Sorry I should have been clearer. What I meant was, can I assume that if I’m using a 3090 and 4090 in the same machine, I won’t get the speed of the 4090 because I’m using it in conjunction with a slower GPU, but I will be able to take advantage of having the additional GPU memory. So I can use larger models, batch sizes, etc.</p>

ptrblck: <p>Yes, as mentioned before you might not be able to get the full performance of the 4090 as it would have to wait for the slowest part of your entire training pipeline. It could be the 3090, it could also be any other part such as the data loading and even your 3090 could already be running into the bottleneck, so I would recommend to profile the workload to see how the overall training behaves.</p>

Ssslakter: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Would it make sense to distribute batches in proportions corresponding to the performance of each GPU? For example, we could run 24 samples on the 4090 and 8 on the 3090, or find another optimum distribution that would result in a speedup.</p>

ptrblck: <p>Yes, this could be a valid idea you could experiment with. However, note that <code>DDP</code> divides the gradients by the <code>world_size</code> as seen <a href="https://github.com/pytorch/pytorch/blob/f326045b3757236aabe367dfca1894be14ce31ef/torch/csrc/distributed/c10d/reducer.cpp#L360">here</a> so you might need to scale the gradients manually since your batch size differs on each rank.<br>
Here is a simple example showing the different outputs using balanced vs. imbalanced batches:</p>
<pre data-code-wrap="python"><code class="lang-python"># balanced DDP workload
world_size = 2
batch_size = 16
global_batch_size = world_size * batch_size

g1 = torch.arange(batch_size).float()
g2 = torch.arange(batch_size, batch_size*2)

g_allreduced = (g1.sum() / batch_size + g2.sum() / batch_size) / world_size
print(g_allreduced)
# tensor(15.5000)
print(g_allreduced == torch.cat((g1, g2)).sum() / global_batch_size)
# tensor(True)

# imbalanced DDP workload
batch_size1 = 24
batch_size2 = 8

g1 = torch.arange(batch_size1).float()
g2 = torch.arange(batch_size1, batch_size1+batch_size2).float()

g_allreduced = (g1.sum() / batch_size1 + g2.sum() / batch_size2) / world_size
print(g_allreduced)
# tensor(19.5000)

# scaling the imbalanced DDP workload
g_allreduced = (g1.sum() / batch_size1 * batch_size1/batch_size + g2.sum() / batch_size2 * batch_size2/batch_size) / world_size
print(g_allreduced)
# tensor(15.5000)
</code></pre>
<p>You could also use other approaches to create the right average gradients e.g. by using <code>reduction="sum"</code> in the criterion and then normalizing afterwards.</p>

vinayak19th: <p>I have a specific research case where I’m trying manually delegate tasks to an intel A770 and a RTX 3090. I can’t seem to create an environment with XPU and Cuda support using pip. Is there a pathway to this if I build from source</p>

ptrblck: <p>I haven’t heard of any builds supporting multiple different accelerators and thus don’t believe it’s possible at the moment.</p>