I am working on parallelizing my training using DP, however within a single instance (with 4 GPUs) it it not working when selecting more than 1 GPUs. Following is my code snippet:
def main():
# Set CUDA_VISIBLE_DEVICES to limit the GPUs used
if args.num_of_gpu > 0:
os.environ["CUDA_VISIBLE_DEVICES"] = ",".join(str(i) for i in range(args.num_of_gpu))
# Check available GPUs
device = "cuda" if torch.cuda.is_available() else "cpu"
num_available_gpus = torch.cuda.device_count()
print(f"Running on {num_available_gpus} GPU(s): {torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'No GPU'}")
# Check if the GPU is available
# device = torch.device(args.device) if torch.cuda.is_available() else torch.device("cpu")
# print(f'Main Selected device: {device}')
# To store the five fold losses
cv_valid_losses = []
cv_valid_mse_losses = []
cv_test_losses = []
for i in range (5):
# Specify the Model
if model_name=="x":
model = DNN() ## note this DNN is imported
elif model_name=="enc":
model = ED(64)
elif model_name=="dncnn":
model = DnCNN(2,5)
elif model_name=="dncnn_mod":
model = DnCNNmod(2,5)
elif model_name =="unet": ## this is imported as a model
model = UNet(n_channels=2,n_classes=1)
if num_available_gpus > 1:
model = nn.DataParallel(model)
model.to(device)
here is the error I am getting
RuntimeError: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cuda:1```
Please note, I am calling other models in this main.py. In only main.py I am using the DP. I am not sure whats is the problem or how should I solve. Can anyone please help?