Here is my code with DataParallel
import time
import torchvision.models as models
import torch
import torch.nn as nn
model = models.resnet50(num_classes=1000).to('cuda:0')

model = nn.DataParallel(model, device_ids=[0,1], output_device=0)
#####
batch_size = 8
image_w = 128
image_h = 128

#####
#warm up GPU
input = torch.randn(batch_size,3,image_w, image_h).to('cuda:0')
model.eval()

listTime = []
for i in range(20):
    with torch.no_grad():
        startTime = time.time()
        input = torch.randn(batch_size,3,image_w, image_h).to('cuda:0')
        out = model(input)
        esl = time.time() - startTime
        listTime.append(esl)
        print("Total time of loop {} :: {}".format(i, esl))

meanTime = torch.mean(torch.tensor(listTime[9:]))
print(meanTime)

I test with resnet50(). The size of input is (8,3,128,128).
I run the forward() pass in 20 steps and choice the last 10 steps to find the mean of execution time.
Without Dataparallel, meanTime = 0.0064s (run with single GPU)
and with Dataparalle, meanTime = 0.0396s(run with 2 GPUs)
P/s: I have 2 GPUs as below image.
Do you have any solution for my problem ?
Thanks

nvida.png710×206 26.4 KB