Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/main/beginner_source/blitz/data_parallel_tutorial.py
Views: 713
"""1Optional: Data Parallelism2==========================3**Authors**: `Sung Kim <https://github.com/hunkim>`_ and `Jenny Kang <https://github.com/jennykang>`_45In this tutorial, we will learn how to use multiple GPUs using ``DataParallel``.67It's very easy to use GPUs with PyTorch. You can put the model on a GPU:89.. code:: python1011device = torch.device("cuda:0")12model.to(device)1314Then, you can copy all your tensors to the GPU:1516.. code:: python1718mytensor = my_tensor.to(device)1920Please note that just calling ``my_tensor.to(device)`` returns a new copy of21``my_tensor`` on GPU instead of rewriting ``my_tensor``. You need to assign it to22a new tensor and use that tensor on the GPU.2324It's natural to execute your forward, backward propagations on multiple GPUs.25However, Pytorch will only use one GPU by default. You can easily run your26operations on multiple GPUs by making your model run parallelly using27``DataParallel``:2829.. code:: python3031model = nn.DataParallel(model)3233That's the core behind this tutorial. We will explore it in more detail below.34"""353637######################################################################38# Imports and parameters39# ----------------------40#41# Import PyTorch modules and define parameters.42#4344import torch45import torch.nn as nn46from torch.utils.data import Dataset, DataLoader4748# Parameters and DataLoaders49input_size = 550output_size = 25152batch_size = 3053data_size = 100545556######################################################################57# Device58#59device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")6061######################################################################62# Dummy DataSet63# -------------64#65# Make a dummy (random) dataset. You just need to implement the66# getitem67#6869class RandomDataset(Dataset):7071def __init__(self, size, length):72self.len = length73self.data = torch.randn(length, size)7475def __getitem__(self, index):76return self.data[index]7778def __len__(self):79return self.len8081rand_loader = DataLoader(dataset=RandomDataset(input_size, data_size),82batch_size=batch_size, shuffle=True)838485######################################################################86# Simple Model87# ------------88#89# For the demo, our model just gets an input, performs a linear operation, and90# gives an output. However, you can use ``DataParallel`` on any model (CNN, RNN,91# Capsule Net etc.)92#93# We've placed a print statement inside the model to monitor the size of input94# and output tensors.95# Please pay attention to what is printed at batch rank 0.96#9798class Model(nn.Module):99# Our model100101def __init__(self, input_size, output_size):102super(Model, self).__init__()103self.fc = nn.Linear(input_size, output_size)104105def forward(self, input):106output = self.fc(input)107print("\tIn Model: input size", input.size(),108"output size", output.size())109110return output111112113######################################################################114# Create Model and DataParallel115# -----------------------------116#117# This is the core part of the tutorial. First, we need to make a model instance118# and check if we have multiple GPUs. If we have multiple GPUs, we can wrap119# our model using ``nn.DataParallel``. Then we can put our model on GPUs by120# ``model.to(device)``121#122123model = Model(input_size, output_size)124if torch.cuda.device_count() > 1:125print("Let's use", torch.cuda.device_count(), "GPUs!")126# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs127model = nn.DataParallel(model)128129model.to(device)130131132######################################################################133# Run the Model134# -------------135#136# Now we can see the sizes of input and output tensors.137#138139for data in rand_loader:140input = data.to(device)141output = model(input)142print("Outside: input size", input.size(),143"output_size", output.size())144145146######################################################################147# Results148# -------149#150# If you have no GPU or one GPU, when we batch 30 inputs and 30 outputs, the model gets 30 and outputs 30 as151# expected. But if you have multiple GPUs, then you can get results like this.152#153# 2 GPUs154# ~~~~~~155#156# If you have 2, you will see:157#158# .. code:: bash159#160# # on 2 GPUs161# 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 GPUs176# ~~~~~~177#178# If you have 3 GPUs, you will see:179#180# .. code:: bash181#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 GPUs201# ~~~~~~~~~~~~~~202#203# If you have 8, you will see:204#205# .. code:: bash206#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#242243244######################################################################245# Summary246# -------247#248# DataParallel splits your data automatically and sends job orders to multiple249# models on several GPUs. After each model finishes their job, DataParallel250# collects and merges the results before returning it to you.251#252# For more information, please check out253# https://pytorch.org/tutorials/beginner/former\_torchies/parallelism\_tutorial.html.254#255256257