Open main menu

CDOT Wiki β

Changes

DPS921/PyTorch: Convolutional Neural Networks

2,913 bytes added, 14:05, 30 November 2020
Data Parallelism
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
==== Dummy DataSet ====
You can make a random dummy dataset:
 
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 ====
 
Here is a simple linear model definition, but <code>DataParallel</code> can be used any model (CNN, RNN, etc).
 
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 ====
 
Now that everything is defined, we need create an instance of the model and check if we have multiple GPUs.
 
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)
# wrap model using nn.DataParallel
 
model.to(device)
 
==== Run the Model ====
 
The print statement will let us see the input and output sizes.
 
for data in rand_loader:
input = data.to(device)
output = model(input)
print("Outside: input size", input.size(),
"output_size", output.size())
 
==== Results ====
 
If you have no GPU or only one GPU, the input and output size will match the batch size, so no parallelization. But if you have 2 GPUs, you'll get these results:
 
# 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])
 
With 2 GPUs, the input and output sizes are half of 30, 15.
=== Single-Machine Model ===
56
edits