如何在pytorch中使用多个GPU?

79

我使用这个命令来使用GPU。

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

但是,我想在 jupyter 中使用两个GPU,就像这样:

device = torch.device("cuda:0,1" if torch.cuda.is_available() else "cpu")
6个回答

83

假设您想要在可用的GPU之间分配数据(如果您有批量大小为16且有2个GPU,则可能希望将8个样本提供给每个GPU),而不是将模型的部分分散到不同的GPU上。可以按以下方式完成:

如果您想要使用所有可用的GPU:

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

model = CreateModel()

model= nn.DataParallel(model)
model.to(device)

如果你想使用特定的GPU: (例如,只使用4个GPU中的2个)

device = torch.device("cuda:1,3" if torch.cuda.is_available() else "cpu") ## specify the GPU id's, GPU id's start from 0.

model = CreateModel()

model= nn.DataParallel(model,device_ids = [1, 3])
model.to(device)

通过设置操作系统环境变量来使用特定的GPU:

在执行程序之前,按照以下方式设置CUDA_VISIBLE_DEVICES变量:

export CUDA_VISIBLE_DEVICES=1,3(假设您想选择第2个和第4个GPU)

然后,在程序内部,您可以像要使用所有GPU一样使用DataParallel()(类似于第1种情况)。这里程序可用的GPU受到操作系统环境变量的限制。

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

model = CreateModel()

model= nn.DataParallel(model)
model.to(device)

在所有这些情况下,数据必须映射到设备
如果Xy是数据:
X.to(device)
y.to(device)


1
如何访问GPU或CPU网络?假设我有两部手机,我想使用这些手机中的GPU来训练我的模型。您有任何想法从哪里开始以实现这一点吗? - Kay
2
抱歉Kay,我不知道这个特定的情况。 - Ashwin Geet D'Sa
在Torch 1.3.1中,torch.device("cuda:0,3")返回device(type='cuda', index=0)。如果您在nn.DataParallel(model,device_ids = [1, 3])中指定了ids,那么运行这行代码有意义吗? - Igor
1
设备 = torch.device(“cuda:1,3” if torch.cuda.is_available()else“cpu”)##指定GPU id,GPU id从0开始。给出字符串错误 - MAC
建议使用DistributedDataParallel来进行多GPU训练,即使只有一个节点也是如此。请参阅:使用nn.parallel.DistributedDataParallel而不是multiprocessing或nn.DataParallel和Distributed Data Parallel。详情请见:https://pytorch.org/docs/stable/generated/torch.nn.DataParallel.html#torch.nn.DataParallel - ch271828n
显示剩余3条评论

29

我从网站上了解到一些信息。也许对我来说,并没有更简单的方法来使用多个GPU。我会再试一次。谢谢。 - ML Xu
3
@MLXu,你在说什么?只需要在原始代码中添加1-2行代码就可以了! - Black Jack 21
也许我应该安装并行CUDA版本。到目前为止,我还没有尝试过同时使用多个GPU。因为每次我尝试使用多个GPU时,什么都没有发生,我的程序在几个小时后崩溃了。谢谢您的回复。 - ML Xu

5

另一个选择是使用PyTorch的一些辅助库:

PyTorch Ignite 库分布式GPU训练

这里有一个关于分布式配置的上下文管理器概念,可在以下情况下使用:

  • nccl - 多个GPU上的torch原生分布式配置
  • xla-tpu - TPU分布式配置

PyTorch Lightning多GPU训练

这是我认为可以在CPU/GPU/TPU上进行训练且不更改原始PyTorch代码的最佳选择。

值得检查使用Catalyst获取类似的分布式GPU选项。


PyTorch Lightning或Catalyst哪个更好?哪一个更容易使用? - Purushothaman Srikanth
1
不要重复我在答案中已经提到的内容,可以查看这个链接:https://discuss.pytorch.org/t/lightning-vs-ignite/84972 - prosti

3
2022年,PyTorch 表示:
建议使用 DistributedDataParallel 来进行多GPU训练,即使只有一个节点也是如此。请参见:使用 nn.parallel.DistributedDataParallel 而不是 multiprocessing 或 nn.DataParallel 和 Distributed Data Parallel。
因此,似乎我们应该使用 DistributedDataParallel,而不是 DataParallel。
链接:https://pytorch.org/docs/stable/generated/torch.nn.DataParallel.html#torch.nn.DataParallel

3
DistributedDataParallel 非常难以实现。 - Khawar Islam

2

如果你只想在特定的GPU上运行代码(例如,仅在GPU id 2和3上运行),则可以在从终端触发Python代码时使用CUDA_VISIBLE_DEVICES=2,3变量来指定。

CUDA_VISIBLE_DEVICES=2,3 python lstm_demo_example.py --epochs=30 --lr=0.001

在代码内部,保留如下:

device = torch.device("cuda" if torch.cuda.is_available() else 'cpu')
model = LSTMModel()
model = nn.DataParallel(model)
model = model.to(device)

来源: https://glassboxmedicine.com/2020/03/04/multi-gpu-training-in-pytorch-data-and-model-parallelism/

本文将介绍如何在PyTorch中使用多GPU进行数据和模型并行训练。对于大规模的深度学习任务,使用多个GPU可以显著提高训练速度。在本文中,我们将学习如何使用DataParallel和DistributedDataParallel两种方法实现多GPU训练。


1

当我运行naiveinception_googlenet时,上述方法对我无效。下面的方法解决了我的问题。

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0,3"  # specify which GPU(s) to be used

但是我怎么知道哪些GPU ID对于我的系统是正确的呢?你能帮忙吗? - LITDataScience
1
我不确定我真正理解你的问题。通常使用“nvidia-smi”可以找到可用的GPU,然后您可以在脚本中指定要使用的GPU(s)。 - ChengguiS.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接