可选: 数据并行处理

作者: Sung Kim Jenny Kang

译者: bat67

校验者: FontTian 片刻

在这个教程里,我们将学习如何使用数据并行(DataParallel)来使用多GPU。

PyTorch非常容易的就可以使用GPU,可以用如下方式把一个模型放到GPU上:

  1. device = torch.device("cuda: 0")
  2. model.to(device)

然后可以复制所有的张量到GPU上:

  1. mytensor = my_tensor.to(device)

请注意,调用my_tensor.to(device)返回一个GPU上的my_tensor副本,而不是重写my_tensor。我们需要把它赋值给一个新的张量并在GPU上使用这个张量。

在多GPU上执行前向和反向传播是自然而然的事。然而,PyTorch默认将只是用一个GPU。你可以使用DataParallel让模型并行运行来轻易的让你的操作在多个GPU上运行。

  1. model = nn.DataParallel(model)

这是这篇教程背后的核心,我们接下来将更详细的介绍它。

导入和参数

导入PyTorch模块和定义参数。

  1. import torch
  2. import torch.nn as nn
  3. from torch.utils.data import Dataset, DataLoader
  4. # Parameters 和 DataLoaders
  5. input_size = 5
  6. output_size = 2
  7. batch_size = 30
  8. data_size = 100

设备(Device):

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

虚拟数据集

要制作一个虚拟(随机)数据集,只需实现__getitem__

  1. class RandomDataset(Dataset):
  2. def __init__(self, size, length):
  3. self.len = length
  4. self.data = torch.randn(length, size)
  5. def __getitem__(self, index):
  6. return self.data[index]
  7. def __len__(self):
  8. return self.len
  9. rand_loader = DataLoader(dataset=RandomDataset(input_size, data_size),
  10. batch_size=batch_size, shuffle=True)

简单模型

作为演示,我们的模型只接受一个输入,执行一个线性操作,然后得到结果。然而,你能在任何模型(CNN,RNN,Capsule Net等)上使用DataParallel

我们在模型内部放置了一条打印语句来检测输入和输出向量的大小。请注意批等级为0时打印的内容。

  1. class Model(nn.Module):
  2. # Our model
  3. def __init__(self, input_size, output_size):
  4. super(Model, self).__init__()
  5. self.fc = nn.Linear(input_size, output_size)
  6. def forward(self, input):
  7. output = self.fc(input)
  8. print("\tIn Model: input size", input.size(),
  9. "output size", output.size())
  10. return output

创建一个模型和数据并行

这是本教程的核心部分。首先,我们需要创建一个模型实例和检测我们是否有多个GPU。如果我们有多个GPU,我们使用nn.DataParallel来包装我们的模型。然后通过model.to(device)把模型放到GPU上。

  1. model = Model(input_size, output_size)
  2. if torch.cuda.device_count() > 1:
  3. print("Let's use", torch.cuda.device_count(), "GPUs!")
  4. # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
  5. model = nn.DataParallel(model)
  6. model.to(device)

输出:

  1. Let's use 2 GPUs!

运行模型

现在我们可以看输入和输出张量的大小。

  1. for data in rand_loader:
  2. input = data.to(device)
  3. output = model(input)
  4. print("Outside: input size", input.size(),
  5. "output_size", output.size())

输出:

  1. In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
  2. In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
  3. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  4. In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
  5. In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
  6. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  7. In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
  8. In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
  9. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  10. In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
  11. In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
  12. Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

结果

当我们对30个输入和输出进行批处理时,我们和期望的一样得到30个输入和30个输出,但是若有多个GPU,会得到如下的结果。

2个GPU

若有2个GPU,将看到:

  1. Let's use 2 GPUs!
  2. In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
  3. In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
  4. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  5. In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
  6. In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
  7. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  8. In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
  9. In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
  10. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  11. In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
  12. In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
  13. Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

3个GPU

若有3个GPU,将看到:

  1. Let's use 3 GPUs!
  2. In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
  3. In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
  4. In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
  5. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  6. In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
  7. In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
  8. In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
  9. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  10. In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
  11. In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
  12. In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
  13. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  14. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  15. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  16. In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
  17. Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

8个GPU

若有8个GPU,将看到:

  1. Let's use 8 GPUs!
  2. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  3. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  4. In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
  5. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  6. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  7. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  8. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  9. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  10. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  11. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  12. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  13. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  14. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  15. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  16. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  17. In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
  18. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  19. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  20. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  21. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  22. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  23. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  24. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  25. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  26. In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
  27. In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
  28. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  29. In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
  30. In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
  31. In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
  32. In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
  33. In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
  34. 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