数据并行
可选:数据并行¶
作者:Sung Kim 与 Jenny Kang
在这个教程中,我们将学习如何通过DataParallel
使用多GPU。
在PyTorch中使用GPU非常简单。可以把模型放到GPU:
1 2 | device = torch.device("cuda:0") model.to(device) |
然后,复制张量到GPU:
1 | mytensor = my_tensor.to(device) |
注意:调用my_tensor.to(device)
返回了my_tensor
的新拷贝,而不是改写my_tensor
。
需要将它复制到新的张量对象,而后在GPU上使用这个新张量。
在多GPU中执行正向、反向传播很简单。不过,PyTorch默认只用一个GPU。
使用DataParallel
可以简单地让模型在多个GPU上进行运算:
1 | model = nn.DataParallel(model) |
这是本教程背后的核心。我们将在下面更详细地探讨它。
导入和参数¶
导入PyTorch模块并定义参数。
1 2 3 4 5 6 7 8 9 10 11 12 | from tqdm import tqdm import torch import torch.nn as nn from torch.utils.data import Dataset, DataLoader # Parameters and DataLoaders input_size = 5 output_size = 2 batch_size = 30 data_size = 100 |
设备
1 | device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") |
假数据集¶
制作一个假(随机)数据集。 只需要实现getitem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class RandomDataset(Dataset): def __init__(self, size, length): self.len = length self.data = torch.randn(length, size) def __getitem__(self, index): return self.data[index] def __len__(self): return self.len rand_loader = DataLoader(dataset=RandomDataset(input_size, data_size), batch_size=batch_size, shuffle=True) |
简单模型¶
在这个演示中,我们的模型只要输入,执行一次现行运算,并给出输出。
另外,DataParallel
在任何模型(CNN,RNN,Capsule Net 等)上都可用
我们在模型中放置了一个print语句来监视输入和输出张量的大小。 请注意批次0中打印的内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Model(nn.Module): # Our model def __init__(self, input_size, output_size): super(Model, self).__init__() self.fc = nn.Linear(input_size, output_size) def forward(self, input): output = self.fc(input) print("\tIn Model: input size", input.size(), "output size", output.size()) return output |
创建模型和数据并发¶
这是本教程的核心部分。
首先,我们需要创建一个模型实例并检查我们是否有多个GPU。如果我们有多个GPU,我们可以使用nn.DataParallel
包装我们的模型。
然后我们可以用model.to(device)
将我们的模型放在GPU上
1 2 3 4 5 6 7 | model = Model(input_size, output_size) if torch.cuda.device_count() > 1: print("Let's use", torch.cuda.device_count(), "GPUs!") # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs model = nn.DataParallel(model) model.to(device) |
1 2 3 4 5 6 7 8 9 10 11 | Let's use 3 GPUs! DataParallel( (module): Model( (fc): Linear(in_features=5, out_features=2, bias=True) ) ) |
运行模型¶
现在可以看看输入和输出张量的大小。
1 2 3 4 5 | for data in tqdm(rand_loader): input = data.to(device) output = model(input) print("Outside: input size", input.size(), "output_size", output.size()) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 100%|██████████| 4/4 [00:07<00:00, 1.93s/it] In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2]) |
结果¶
如果没有GPU或只有一个GPU,当我们批次执行30个输入和30个输出的时候,模型得到了30个输入并期待30个输出。 如果有多个GPU,就可以得到像这样的结果。
2 GPU¶
如果有2个GPU,就会看到:
1 2 3 4 5 6 7 8 9 10 11 12 13 | Let's use 2 GPUs! In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2]) In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2]) Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2]) |
3 GPU¶
如果有3个GPU,就会看到:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Let's use 3 GPUs! In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2]) |
8 GPU¶
如果有8个GPU,就会看到
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | Let's use 8 GPUs! In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2]) |
总结¶
DataParallel自动拆分数据并将作业发送到多个GPU上的多个模型。 在每个模型完成其工作后,DataParallel就会在函数返回之前收集并且合并结果。
有关详细信息,请查看 https://pytorch.org/tutorials/beginner/former_torchies/parallelism_tutorial.html。