【问题标题】:What does local rank mean in distributed deep learning?局部排名在分布式深度学习中意味着什么?
【发布时间】:2021-01-29 15:30:38
【问题描述】:

https://github.com/huggingface/transformers/blob/master/examples/run_glue.py

我想调整这个脚本来对我的数据进行文本分类。用于此任务的计算机是一台带有两个图形卡的单机。因此,这涉及到在上面的脚本中使用术语 local_rank 进行“分布式”训练,尤其是当 local_rank 等于 0 或 -1 时,如第 83 行。

在阅读了分布式计算的一些资料后,我猜local_rank 就像一台机器的 ID。 0 可能意味着这台机器是计算中的“主要”或“头”。但是-1 是什么?

【问题讨论】:

    标签: deep-learning pytorch distributed-computing


    【解决方案1】:

    问:-1 是什么?

    通常,这用于禁用分布式设置。确实如你所见here:

    train_sampler = RandomSampler(train_dataset) if args.local_rank == -1 else DistributedSampler(train_dataset)
    

    和here:

    if args.local_rank != -1:
        model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank],
                                                          output_device=args.local_rank,
                                                          find_unused_parameters=True)
    

    将local_rank 设置为-1 具有此效果。

    【讨论】:

      【解决方案2】:

      我想为@Berriel 的答案添加更多内容。由于您有两个 GPU 而不是具有节点结构的分布式机器,因此您不需要像 DistributedSampler 这样的分布式方法。 Hugginface 使用 -1 来禁用训练机制中的分布式设置。

      查看来自 huggiface training_args.py 脚本的以下代码。如您所见,是否存在分布式训练机制 self.local_rank 进行更改。

      def _setup_devices(self) -> "torch.device":
      
            
              logger.info("PyTorch: setting up devices")
              if self.no_cuda:
                  device = torch.device("cpu")
                  self._n_gpu = 0
              elif is_torch_tpu_available():
                  device = xm.xla_device()
                  self._n_gpu = 0
              elif is_sagemaker_distributed_available():
                  import smdistributed.dataparallel.torch.distributed as dist
      
                  dist.init_process_group()
                  self.local_rank = dist.get_local_rank()
                  device = torch.device("cuda", self.local_rank)
                  self._n_gpu = 1
              elif self.local_rank == -1:
                  # if n_gpu is > 1 we'll use nn.DataParallel.
                  # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
                  # Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will
                  # trigger an error that a device index is missing. Index 0 takes into account the
                  # GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0`
                  # will use the first GPU in that env, i.e. GPU#1
                  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
                  # Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at
                  # the default value.
                  self._n_gpu = torch.cuda.device_count()
      

      【讨论】:

        猜你喜欢
        • 2012-12-16
        • 2016-06-10
        • 2011-08-28
        • 2018-11-05
        • 2018-02-21
        • 1970-01-01
        • 1970-01-01
        • 2020-04-22
        • 1970-01-01
        相关资源
        最近更新 更多