|
| 1 | +""" |
| 2 | +Optional: Data Parallelism |
| 3 | +========================== |
| 4 | +**Authors**: `Sung Kim <https://github.com/hunkim>`_ and `Jenny Kang <https://github.com/jennykang>`_ |
| 5 | +
|
| 6 | +In this tutorial, we will learn how to use multiple GPUs using ``DataParallel``. |
| 7 | +
|
| 8 | +It's very easy to use GPUs with PyTorch. You can put the model on a GPU: |
| 9 | +
|
| 10 | +.. code:: python |
| 11 | +
|
| 12 | + model.gpu() |
| 13 | +
|
| 14 | +Then, you can copy all your tensors to the GPU: |
| 15 | +
|
| 16 | +.. code:: python |
| 17 | +
|
| 18 | + mytensor = my_tensor.gpu() |
| 19 | +
|
| 20 | +Please note that just calling ``mytensor.gpu()`` won't copy the tensor |
| 21 | +to the GPU. You need to assign it to a new tensor and use that tensor on the GPU. |
| 22 | +
|
| 23 | +It's natural to execute your forward, backward propagations on multiple GPUs. |
| 24 | +However, Pytorch will only use one GPU by default. You can easily run your |
| 25 | +operations on multiple GPUs by making your model run parallelly using |
| 26 | +``DataParallel``: |
| 27 | +
|
| 28 | +.. code:: python |
| 29 | +
|
| 30 | + model = nn.DataParallel(model) |
| 31 | +
|
| 32 | +That's the core behind this tutorial. We will explore it in more detail below. |
| 33 | +""" |
| 34 | + |
| 35 | + |
| 36 | +###################################################################### |
| 37 | +# Imports and parameters |
| 38 | +# ---------------------- |
| 39 | +# |
| 40 | +# Import PyTorch modules and define parameters. |
| 41 | +# |
| 42 | + |
| 43 | +import torch |
| 44 | +import torch.nn as nn |
| 45 | +from torch.autograd import Variable |
| 46 | +from torch.utils.data import Dataset, DataLoader |
| 47 | + |
| 48 | +# Parameters and DataLoaders |
| 49 | +input_size = 5 |
| 50 | +output_size = 2 |
| 51 | + |
| 52 | +batch_size = 30 |
| 53 | +data_size = 100 |
| 54 | + |
| 55 | + |
| 56 | +###################################################################### |
| 57 | +# Dummy DataSet |
| 58 | +# ------------- |
| 59 | +# |
| 60 | +# Make a dummy (random) dataset. You just need to implement the |
| 61 | +# getitem |
| 62 | +# |
| 63 | + |
| 64 | +class RandomDataset(Dataset): |
| 65 | + |
| 66 | + def __init__(self, size, length): |
| 67 | + self.len = length |
| 68 | + self.data = torch.randn(length, size) |
| 69 | + |
| 70 | + def __getitem__(self, index): |
| 71 | + return self.data[index] |
| 72 | + |
| 73 | + def __len__(self): |
| 74 | + return self.len |
| 75 | + |
| 76 | +rand_loader = DataLoader(dataset=RandomDataset(input_size, 100), |
| 77 | + batch_size=batch_size, shuffle=True) |
| 78 | + |
| 79 | + |
| 80 | +###################################################################### |
| 81 | +# Simple Model |
| 82 | +# ------------ |
| 83 | +# |
| 84 | +# For the demo, our model just gets an input, performs a linear operation, and |
| 85 | +# gives an output. However, you can use ``DataParallel`` on any model (CNN, RNN, |
| 86 | +# Capsule Net etc.) |
| 87 | +# |
| 88 | +# We've placed a print statement inside the model to monitor the size of input |
| 89 | +# and output tensors. |
| 90 | +# Please pay attention to what is printed at batch rank 0. |
| 91 | +# |
| 92 | + |
| 93 | +class Model(nn.Module): |
| 94 | + # Our model |
| 95 | + |
| 96 | + def __init__(self, input_size, output_size): |
| 97 | + super(Model, self).__init__() |
| 98 | + self.fc = nn.Linear(input_size, output_size) |
| 99 | + |
| 100 | + def forward(self, input): |
| 101 | + output = self.fc(input) |
| 102 | + print(" In Model: input size", input.size(), |
| 103 | + "output size", output.size()) |
| 104 | + |
| 105 | + return output |
| 106 | + |
| 107 | + |
| 108 | +###################################################################### |
| 109 | +# Create Model and DataParallel |
| 110 | +# ----------------------------- |
| 111 | +# |
| 112 | +# This is the core part of the tutorial. First, we need to make a model instance |
| 113 | +# and check if we have multiple GPUs. If we have multiple GPUs, we can wrap |
| 114 | +# our model using ``nn.DataParallel``. Then we can put our model on GPUs by |
| 115 | +# ``model.gpu()`` |
| 116 | +# |
| 117 | + |
| 118 | +model = Model(input_size, output_size) |
| 119 | +if torch.cuda.device_count() > 1: |
| 120 | + print("Let's use", torch.cuda.device_count(), "GPUs!") |
| 121 | + # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs |
| 122 | + model = nn.DataParallel(model) |
| 123 | + |
| 124 | +if torch.cuda.is_available(): |
| 125 | + model.cuda() |
| 126 | + |
| 127 | + |
| 128 | +###################################################################### |
| 129 | +# Run the Model |
| 130 | +# ------------- |
| 131 | +# |
| 132 | +# Now we can see the sizes of input and output tensors. |
| 133 | +# |
| 134 | + |
| 135 | +for data in rand_loader: |
| 136 | + if torch.cuda.is_available(): |
| 137 | + input_var = Variable(data.cuda()) |
| 138 | + else: |
| 139 | + input_var = Variable(data) |
| 140 | + |
| 141 | + output = model(input_var) |
| 142 | + print("Outside: input size", input_var.size(), |
| 143 | + "output_size", output.size()) |
| 144 | + |
| 145 | + |
| 146 | +###################################################################### |
| 147 | +# Results |
| 148 | +# ------- |
| 149 | +# |
| 150 | +# When we batch 30 inputs and 30 outputs, the model gets 30 and outputs 30 as |
| 151 | +# expected. But if you have GPUs, then you can get results like this. |
| 152 | +# |
| 153 | +# 2 GPUs |
| 154 | +# ~~~~~~ |
| 155 | +# |
| 156 | +# If you have 2, you will see: |
| 157 | +# |
| 158 | +# .. code:: bash |
| 159 | +# |
| 160 | +# # on 2 GPUs |
| 161 | +# Let's use 2 GPUs! |
| 162 | +# In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) |
| 163 | +# In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) |
| 164 | +# Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) |
| 165 | +# In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) |
| 166 | +# In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) |
| 167 | +# Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) |
| 168 | +# In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) |
| 169 | +# In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) |
| 170 | +# Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) |
| 171 | +# In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2]) |
| 172 | +# In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2]) |
| 173 | +# Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2]) |
| 174 | +# |
| 175 | +# 3 GPUs |
| 176 | +# ~~~~~~ |
| 177 | +# |
| 178 | +# If you have 3 GPUs, you will see: |
| 179 | +# |
| 180 | +# .. code:: bash |
| 181 | +# |
| 182 | +# Let's use 3 GPUs! |
| 183 | +# In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) |
| 184 | +# In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) |
| 185 | +# In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) |
| 186 | +# Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) |
| 187 | +# In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) |
| 188 | +# In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) |
| 189 | +# In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) |
| 190 | +# Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) |
| 191 | +# In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) |
| 192 | +# In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) |
| 193 | +# In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) |
| 194 | +# Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) |
| 195 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 196 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 197 | +# In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) |
| 198 | +# Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2]) |
| 199 | +# |
| 200 | +# 8 GPUs |
| 201 | +# ~~~~~~~~~~~~~~ |
| 202 | +# |
| 203 | +# If you have 8, you will see: |
| 204 | +# |
| 205 | +# .. code:: bash |
| 206 | +# |
| 207 | +# Let's use 8 GPUs! |
| 208 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 209 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 210 | +# In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) |
| 211 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 212 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 213 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 214 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 215 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 216 | +# Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) |
| 217 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 218 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 219 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 220 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 221 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 222 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 223 | +# In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) |
| 224 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 225 | +# Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) |
| 226 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 227 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 228 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 229 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 230 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 231 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 232 | +# In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) |
| 233 | +# In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) |
| 234 | +# Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) |
| 235 | +# In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) |
| 236 | +# In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) |
| 237 | +# In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) |
| 238 | +# In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) |
| 239 | +# In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) |
| 240 | +# Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2]) |
| 241 | +# |
| 242 | + |
| 243 | + |
| 244 | +###################################################################### |
| 245 | +# Summary |
| 246 | +# ------- |
| 247 | +# |
| 248 | +# DataParallel splits your data automatically and sends job orders to multiple |
| 249 | +# models on several GPUs. After each model finishes their job, DataParallel |
| 250 | +# collects and merges the results before returning it to you. |
| 251 | +# |
| 252 | +# For more information, please check out |
| 253 | +# http://pytorch.org/tutorials/beginner/former\_torchies/parallelism\_tutorial.html. |
| 254 | +# |
0 commit comments