作者:Soumith Chintala

原文翻译自:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html

中文翻译、注释制作:黄海广

github:https://github.com/fengdu78

代码全部测试通过。

配置环境:PyTorch 1.0,python 3.6,

主机:显卡:一块1080ti;内存:32g(注:绝大部分代码不需要GPU)

目录

五、数据并行(选读)

作者:Sung Kim和Jenny Kang

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

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

device = torch.device("cuda:0")

model.to(device)

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

mytensor = my_tensor.to(device)

请注意,只调用mytensor.gpu()并没有复制张量到GPU上。你需要把它赋值给一个新的张量并在GPU上使用这个张量。

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

model = nn.DataParallel(model)

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

导入和参数

导入PyTorch模块和定义参数。

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

设备:

  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. Model(
  2. (fc): Linear(in_features=5, out_features=2, bias=True)
  3. )

运行模型

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

  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([30, 5]) output size torch.Size([30, 2])
  2. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  3. In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
  4. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  5. In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
  6. Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  7. In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
  8. Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

结果

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

2个GPU

如果你有2个GPU,你将看到:

on 2 GPUs

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,你将看到:
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,你将看到:
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收集并合并结果返回给你。

更多信息请看这里:

http://pytorch.org/tutorials/beginner/former_torchies/parallelism_tutorial.html

本章的官方代码: