Pytorch分布式训练指南(详细)

pytorch分布式训练指北

第一章节的部分是简单的科普,想看如何在本地及docker内跑pytorch分布式的直接看第二章。

1、pytorch分布式代码基础

1.1、如何写pytorch的分布式代码

这个部分大概讲一下如何写分布式的Pytorch代码,首先,官方pytorch(v1.0.10)在分布式上给出的api有这么两个非常重要的
,需要使用的:

torch.nn.parallel.DistributedDataParallel

这个api和DataParallel相类似,也是一个模型wrapper。这个api可以帮助我们在不同机器的多个模型拷贝之间平均梯度。

torch.utils.data.distributed.DistributedSampler

在多机多卡情况下分布式训练数据的读取也是一个问题,不同的卡读取到的数据应该是不同的。dataparallel的做法是直接
将batch切分到不同的卡,这种方法对于多机来说不可取,因为多机之间直接进行数据传输会严重影响效率。于是有了利用sampler
确保dataloader只会load到整个数据集的一个特定子集的做法。DistributedSampler就是做这件事的。它为每一个子进程划分
出一部分数据集,以避免不同进程之间数据重复。

到这里要是还没看明白,那就建议看看谷歌。下面给出Pytorch的代码如何改成分布式代码:

from torch.utils.data import Dataset, DataLoader
from torch.utils.data.distributed import DistributedSampler
from torch.nn.parallel import DistributedDataParallel

dataset = your_dataset()
datasampler = DistributedSampler(dataset, num_replicas=world_size, rank=rank)
dataloader = DataLoader(dataset, batch_size=batch_size_per_gpu, sampler=datasampler)
model = your_model()

##这个部分东西比较多,文章的后面我稍微做一些补充,细节可以去谷歌
model = DistributedDataPrallel(model, device_ids=[local_rank], output_device=local_rank)

1.2、各种参数介绍

想要使用DistributedDataParallel,想要用pytorch的分布式,那么首先需要完成多进程的初始化即:

torch.distributed.init_process_group(backend, init_method='env://', timeout=datetime.timedelta(0, 1800), **kwargs)

官方文档的介绍如下,想看我的介绍的直接跳过下面这段即可。

# Initializes the default distributed process group, and this will also initialize the distributed package

# Parameters: 
# backend (str or Backend) – The backend to use. Depending on build-time configurations, valid values include mpi, gloo, and nccl. This field should be given as a lowercase string (e.g., "gloo"), which can also be accessed via Backend attributes (e.g., Backend.GLOO).
# init_method (str, optional) – URL specifying how to initialize the process group.
# world_size (int, optional) – Number of processes participating in the job.
# rank (int, optional) – Rank of the current process.
# timeout (timedelta, optional) – Timeout for operations executed against the process group. Default value equals 30 minutes. This is only applicable for the gloo backend.
# group_name (str, optional, deprecated) – Group name.  请注意,最新的分布式软件包中不再支持多播地址。group_name也被弃用了。
# To enable backend == Backend.MPI, PyTorch needs to built from source on a system that supports MPI. The same applies to NCCL as well.

这里面涉及到一些不同机器通信的东西,我就不讲了,总而言之言而总之,我们使用的backend nccl即可,为什么?自己谷歌

1.3、nccl三种通信方式

1.3.1、TCP初始化

以TCP协议的方式进行不同分布式进程之间的数据交流,需要设置一个端口,不同进程之间公用这一个端口。
Note:如果使用docker的话需要在创建容器时加上 --net=host。例如:

sudo nvidia-docker run -dit --name gaobin_pytorch_dist -v /home/gaobin/docker:/notebooks -w /notebooks --net=host pytorch/pytorch:latest
1.3.2、文件共享系统初始化

这个地方不多说,需要自己的服务器有共享文件系统

1.3.3、环境变量初始化

这个和TCP一样也是非常好用的,我在这里贴一个官方的说明,有问题私下问我。

Node 1: (IP: 192.168.1.1, and has a free port: 1234)

>>> python -m torch.distributed.launch --nproc_per_node=NUM_GPUS_YOU_HAVE
           --nnodes=2 --node_rank=0 --master_addr="192.168.1.1"
           --master_port=1234 YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3
           and all other arguments of your training script)
Node 2:

>>> python -m torch.distributed.launch --nproc_per_node=NUM_GPUS_YOU_HAVE
           --nnodes=2 --node_rank=1 --master_addr="192.168.1.1"
           --master_port=1234 YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3
           and all other arguments of your training script)

2、本地跑Pytorch分布式代码

2.1、不使用docker本地跑

2.1.1、准备工作

这里我用我们的三台机器距离,M1: 10.90.25.19, M2: 10.90.25.18, M3: 10.90.25.14

多台机器需要有共同的训练环境,训练代码,训练数据,而且两台机器的代码、数据存储路径地址必须相同。例如:
代码和数据的路径位置在M1、M2、M3必须一致,比如代码存在M1的/home/gaobin/docker/dist_test文件夹下,那么在
M2和M3机器下也必须存在这个路径下。

2.1.2、TCP和环境变量方法运行示例:

1、TCP方式
以TCP协议的方式进行不同分布式进程之间的数据交流,需要设置一个端口,不同进程之间公用这一个端口,并且设置host的级别和host的数量。
设计两个参数rank和world_size。
其中rank为host的编号,默认0为主机,端口应该位于该主机上。world_size为分布式主机的个数。
执行方式命令例子:

## 第一台机器
## M1: 10.90.25.19:
python train.py --backend nccl --init-method tcp://10.90.25.19:12345 --rank 0 --world-size 3 --其他参数

## 第二台机器
## M2: 10.90.25.18:
python train.py --backend nccl --init-method tcp://10.90.25.19:12345 --rank 1 --world-size 3 --其他参数

## 第三台机器
## M3: 10.90.25.14:
python train.py --backend nccl --init-method tcp://10.90.25.19:12345 --rank 2 --world-size 3 --其他参数

上面这三句指令的先后顺序没有要求,只有三条指令全部输入,程序才会运行起来。

2、环境变量方式
需要额外提供如下参数:

MASTER_PORT - required; has to be a free port on machine with rank 0
MASTER_ADDR - required (except for rank 0); address of rank 0 node
WORLD_SIZE - required; can be set either here, or in a call to init function
RANK - required; can be set either here, or in a call to init function

这个方式的运行和上面tcp的稍微有点不同,可以看到相当于将distributed的初始化放到了代码外面。
执行方式命令例子:

## 第一台机器
## M1: 10.90.25.19:
python -m torch.distributed.launch --nproc_per_node=NUM_GPUS_YOU_HAVE
       --nnodes=3 --node_rank=0 --master_addr="10.90.25.19"
       --master_port=12345 YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3
       and all other arguments of your training script)

## 第二台机器
## M1: 10.90.25.18:
python -m torch.distributed.launch --nproc_per_node=NUM_GPUS_YOU_HAVE
       --nnodes=3 --node_rank=1 --master_addr="10.90.25.19"
       --master_port=12345 YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3
       and all other arguments of your training script)
       
## 第三台机器
## M1: 10.90.25.14:
python -m torch.distributed.launch --nproc_per_node=NUM_GPUS_YOU_HAVE
       --nnodes=3 --node_rank=2 --master_addr="10.90.25.19"
       --master_port=12345 YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3
       and all other arguments of your training script)

其实个人觉得这个把distributed的初始化放在外面的方式也是非常清晰的。

2.2、使用docker本地跑

2.2.1、准备工作

和上面一样只是在创建容器的时候需要做端口映射,或者使用–net=host。我在这里给个例子:

所有的机器上均需要执行下面的命令来创建容器
sudo nvidia-docker run -dit --name gaobin_pytorch_dist -v /home/gaobin/docker:/notebooks -w /notebooks --net=host pytorch/pytorch:latest

说明:–net=host就是把容器的端口和主机的端口一一映射,这个文件挂载映射一定要保证映射到容器里的路径
在三台机器上一样,即:-v host_dir:container_dir,那么一定要保证container_dir在三台机器上用一样的名称,
host_dir可以不同。

其他的部分和上面2.1章节没有任何差别,在这里不再叙述。

2.3、使用共享文件系统本地跑

这个部分不做过多介绍,非常简单。

最后,祝大家不烧香拜佛也能炼出仙丹。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值