摘要
可选的:数据并行性
= = = = = = = = = = = = = = = = = = = = = = = = = =
**作者**:' Sung Kim <https://github.com/hunkim>' 和' Jenny Kang <https://github.com/jennykang>'
在本教程中,我们将学习如何使用多个gpu使用“数据并行”。
使用PyTorch使用gpu非常容易。你可以把模型放在GPU上:
. .python代码:
device = torch.device("cuda:0")
model.to(device)
然后,你可以复制你所有的张量到GPU:
. .python代码:
mytensor = my_tensor.to(device)
请注意,只是调用' ' my_tensor.to(device) ' '返回一个新的副本my_tensor在GPU上而不是重写my_tensor。你需要把它分配给
一个新的张量,在GPU上使用这个张量。
在多个gpu上执行正向和反向传播是很自然的。然而,Pytorch默认情况下只使用一个GPU。通过让模型并行运行,可以很容易地在多个gpu上运行操作
' ' DataParallel ' ':
. .python代码:
model = nn.DataParallel(model)
这就是本教程的核心。我们将在下面更详细地探讨它。
# Imports and parameters
# ----------------------
#
# Import PyTorch modules and define parameters.
#
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
######################################################################
# Device
#
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
######################################################################
# Dummy DataSet
# -------------
#
# Make a dummy (random) dataset. You just need to implement the
# getitem
#
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)
######################################################################
# Simple Model
# ------------
#
# For the demo, our model just gets an input, performs a linear operation, and
# gives an output. However, you can use ``DataParallel`` on any model (CNN, RNN,
# Capsule Net etc.)
#
# We've placed a print statement inside the model to monitor the size of input
# and output tensors.
# Please pay attention to what is printed at batch rank 0.
#
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
######################################################################
# Create Model and DataParallel
# -----------------------------
#
# This is the core part of the tutorial. First, we need to make a model instance
# and check if we have multiple GPUs. If we have multiple GPUs, we can wrap
# our model using ``nn.DataParallel``. Then we can put our model on GPUs by
# ``model.to(device)``
#
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)
######################################################################
# Run the Model
# -------------
#
# Now we can see the sizes of input and output tensors.
#
for data in rand_loader:
input = data.to(device)
output = model(input)
print("Outside: input size", input.size(),
"output_size", output.size())