Pytorch:DataParallel无法减少相同总批次大小的模型推断时间

时间:2019-08-21 02:43:30

标签: pytorch

当我使用torch.nn.DataParallel()实现数据并行计算时,我发现相同总量批次大小的并行模型推理时间与串行模型相比并没有明显减少。

如下所示。

model = SNModel()
criterion = nn.CrossEntropyLoss()
if parallel_enable:
    model = nn.DataParallel(model, device_ids=gpu_ids) # gpu_ids = [0,1,2,3] total 4 gpus
model.to(args.device)
criterion.to(args.device)

计算100个迭代模型推断时间:

torch.cuda.synchronize()
st = time.time()
outputs, loss = model(images, path, labels, criterion, gpu_nums)
torch.cuda.synchronize()
total_tm += time.time() - st

使用单个GPU:

100 iter model time  23s (around)

具有4 gpus:

100 iter model time  103s (around)

仅减少103 - 23*4 = 11s

有什么问题吗?希望你能帮我!我为此困扰了很长时间。我知道我没有发现错误。这种现象已经出现在其他模型中,因此我一直放弃使用并行性。

0 个答案:

没有答案