【问题标题】:What does local rank mean in distributed deep learning?局部排名在分布式深度学习中意味着什么?
【发布时间】:2021-01-29 15:30:38
【问题描述】:
【问题讨论】:
标签:
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()