【问题标题】:(Distributed) Why all gpus are giving the same output?(分布式)为什么所有 gpus 都给出相同的输出?
【发布时间】:2019-12-17 07:15:50
【问题描述】:

我是 PyTorch 的新手,在运行 torch.distributed 的官方示例时我很困惑 在PyTorch ImageNet main.py L304

我对源代码的评估部分做了一些小的修改,如下所示:

model.eval()
    with torch.no_grad():
        end = time.time()
        for i, (images, target, image_ids) in enumerate(val_loader):
            if args.gpu is not None:
                images = images.cuda(args.gpu, non_blocking=True)

            target = target.cuda(args.gpu, non_blocking=True)
            image_ids = image_ids.data.cpu().numpy()
            output = model(images)
            loss = criterion(output, target)

            # Get acc1, acc5 and update
            acc1, acc5 = accuracy(output, target, topk=(1, 5))
            losses.update(loss.item(), images.size(0))
            top1.update(acc1[0], images.size(0))
            top1.update(acc1[0], images.size(0))
            top5.update(acc5[0], images.size(0))

            # print at i-th batch of images only
            dist.barrier()
            if i==0:
                if args.gpu==0:
                    print("gpu 0",acc1,output.shape)
                if args.gpu==1:
                    print("gpu 1",acc1,output.shape)
                if args.gpu==2:
                    print("gpu 2",acc1,output.shape)
                if args.gpu==3:
                    print("gpu 3",acc1,output.shape)

上面的代码给出了以下输出:

Use GPU: 0 for training
Use GPU: 1 for training
Use GPU: 3 for training
Use GPU: 2 for training
=> loading checkpoint model_best.pth.tar'
...
gpu 3 tensor([75.], device='cuda:3') torch.Size([32, 200])
gpu 2 tensor([75.], device='cuda:2') torch.Size([32, 200])
gpu 1 tensor([75.], device='cuda:1') torch.Size([32, 200])
gpu 0 tensor([75.], device='cuda:0') torch.Size([32, 200])

由于我使用的是 4 GPU,批量大小为 128,我认为 128 幅图像已被划分并分别输入 4 个 GPU。所以四个 GPU 都有output.shape[0]=32(其中 200 是 num_classes)。

但真正让我感到困惑的是,所有 4 个 GPU 都显示相同的acc1。据我了解,由于 4 个 GPU 分别采用不同的输入部分(分别为 32 张图像),因此它们也应该分别给出与其输入相对应的不同输出和精度。但是,在我的打印测试中,这些 GPU 显示出相同的输出和准确性。而且我不知道为什么,他们不应该是不同的吗?

寻求帮助。提前谢谢你!

【问题讨论】:

  • 分布式处理是当你有多个节点时。当您有一台带有多个 GPU 的机器时,您应该只使用 torch.nn.DataParallel
  • @jodag 感谢您的回复。但是torch.distributed的文档表明这个模块也提供了对单节点多GPU的支持。而从相关的issues 中,我刚刚发现对分布式评估的支持可能仍需在此代码中完成。

标签: python pytorch torch


【解决方案1】:

好的,我想可以在 Github issuesenter link description here 找到这个 PyTorch 官方 ImageNet 示例代码的解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-05
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2020-06-12
    • 2018-01-19
    • 1970-01-01
    相关资源
    最近更新 更多