Skip to content Skip to sidebar Skip to footer

Problems About Torch.nn.dataparallel

I am new in deep learning area. Now I am reproducing a paper’s codes. since they use several GPUs, there is a command torch.nn.DataParallel(model, device_ids= args.gpus).cuda() i

Solution 1:

DataParallel should work on a single GPU as well, but you should check if args.gpus only contains the id of the device that is to be used (should be 0) or None. Choosing None will make the module use all available devices.

Also you could remove DataParallel as you do not need it and move the model to GPU only by calling model.cuda() or, as I prefer, model.to(device) where device is the device's name.

Example:

This example shows how to use a model on a single GPU, setting the device using .to() instead of .cuda().

from torch import nn
import torch

# Set device to cuda if cuda is available
device = torch.device("cuda"if torch.cuda.is_available() else"cpu")

# Create model
model = nn.Sequential(
  nn.Conv2d(1,20,5),
  nn.ReLU(),
  nn.Conv2d(20,64,5),
  nn.ReLU()
)

# moving model to GPU
model.to(device)

If you want to use DataParallel you could do it like this

# Optional DataParallel, not needed for single GPU usage
model1 = torch.nn.DataParallel(model, device_ids=[0]).to(device)
# Or, using default 'device_ids=None'
model1 = torch.nn.DataParallel(model).to(device)

Post a Comment for "Problems About Torch.nn.dataparallel"