PyTorch 分布式训练和启动脚本torch.distributed.launch torchrun slurm

1、DataParallel

如果当前有4个GPU,batch_size=16,那么模型将被复制到每一个GPU上,在前向传播时,每一个gpu将分到4个batch,每个gpu独立计算依据分到的batch计算出结果的梯度,然后将梯度返回到第一个GPU上,第一个GPU再进行梯度融合、模型更新。在下一次前向传播的时候,将更新后的模型再复制给每一个GPU。

1、DP在每个训练批次(batch)中,因为模型的权重都是在 一个进程上先算出来 然后再把他们分发到每个GPU上,所以网络通信就成为了一个瓶颈,而GPU使用率也通常很低。

2、因为它在每一次的前向传播的时候把模型也复制了(即每次更新都复制一遍模型),并且单进程多线程会造成GIL contention(全局解释器锁争用) 这里进程计算权重使通信成为瓶颈造成了大量的时间浪费,因此引入了DDP。

2、DistributedDataParallel

DDP采用多进程控制多GPU,共同训练模型,一份代码会被pytorch自动分配到n个进程并在n个GPU上运行。 DDP运用Ring-Reduce通信算法在每个GPU间对梯度进行通讯,交换彼此的梯度,从而获得所有GPU的梯度。对比DP,不需要在进行模型本体的通信,因此可以加速训练。

参考https://zhuanlan.zhihu.com/p/489011749

在所有节点上运行命令来初始化上面创建的 DDP 作业:

torchrun --nnodes**=**2 --nproc_per_node**=**8 --rdzv_id**=**100 --rdzv_backend**=**c10d --rdzv_endpoint**=**$MASTER_ADDR:29400 elastic_ddp.py

这里torchrun将启动8个进程并调用elastic_ddp.py 其启动的节点上的每个进程,但用户还需要应用slurm等集群管理工具才能在2个节点上实际运行此命令。

srun --nodes=2 ./torchrun_script.sh

启动脚本

无论 DDP 应用程序如何启动,每个进程都需要一种机制来了解其rank等,使用torch提供的分布式脚本可以通过环境变量将世界大小、全局等级、主地址和主端口以及本地等级作为命令行参数传递给每个实例,初始化的时候选择环境变量初始化就很方便 (就不应该使用启动子进程torch.multiprocessing.spawn 了)。

torch.distributed.launch

python -m torch.distributed.launch --nproc_per_node 8 test.py

https://github.com/pytorch/pytorch/blob/main/torch/distributed/launch.py

import torch

import os
import time

print("|| MASTER_ADDR:",os.environ["MASTER_ADDR"],
     "|| MASTER_PORT:",os.environ["MASTER_PORT"],
     "|| LOCAL_RANK:",os.environ["LOCAL_RANK"],
     "|| RANK:",os.environ["RANK"], 
     "|| WORLD_SIZE:",os.environ["WORLD_SIZE"])

单机启动八个worker的运行结果

/home  $ python -m torch.distributed.launch   --nproc_per_node 8 test.py                                            2 ↵
*****************************************
Setting OMP_NUM_THREADS environment variable for each process to be 1 in default, to avoid your system being overloaded, please further tune the variable for optimal performance in your application as needed.
*****************************************
|| MASTER_ADDR: 127.0.0.1 || MASTER_PORT: 29500 || LOCAL_RANK: 0 || RANK: 0 || WORLD_SIZE: 8
|| MASTER_ADDR: 127.0.0.1 || MASTER_PORT: 29500 || LOCAL_RANK: 1 || RANK: 1 || WORLD_SIZE: 8
|| MASTER_ADDR: 127.0.0.1 || MASTER_PORT: 29500 || LOCAL_RANK: 5 || RANK: 5 || WORLD_SIZE: 8
|| MASTER_ADDR: 127.0.0.1 || MASTER_PORT: 29500 || LOCAL_RANK: 4 || RANK: 4 || WORLD_SIZE: 8
|| MASTER_ADDR: 127.0.0.1 || MASTER_PORT: 29500 || LOCAL_RANK: 3 || RANK: 3 || WORLD_SIZE: 8
|| MASTER_ADDR: 127.0.0.1 || MASTER_PORT: 29500 || LOCAL_RANK: 2 || RANK: 2 || WORLD_SIZE: 8
|| MASTER_ADDR: 127.0.0.1 || MASTER_PORT: 29500 || LOCAL_RANK: 6 || RANK: 6 || WORLD_SIZE: 8
|| MASTER_ADDR: 127.0.0.1 || MASTER_PORT: 29500 || LOCAL_RANK: 7 || RANK: 7 || WORLD_SIZE: 8

多机


/home# python -m torch.distributed.launch --nproc_per_node 8 --nnodes 2 --node_rank 0 --master_addr "替换成你的IP地址" --master_port 1234 test.py

|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 3 || RANK: 3 || WORLD_SIZE: 16
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 4 || RANK: 4 || WORLD_SIZE: 16
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 5 || RANK: 5 || WORLD_SIZE: 16
|| MASTER_ADDR:|| MASTER_ADDR: IP地址  IP地址|| MASTER_PORT:  || MASTER_PORT:1234  1234|| LOCAL_RANK:  || LOCAL_RANK:1  7|| RANK:  || RANK:1  7|| WORLD_SIZE:  || WORLD_SIZE:16
16
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 6 || RANK: 6 || WORLD_SIZE: 16
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 2 || RANK: 2 || WORLD_SIZE: 16
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 0 || RANK: 0 || WORLD_SIZE: 16

第二个机器运行同样的命令(除了noderank

❯ python -m torch.distributed.launch --nproc_per_node 8 --nnodes 2 --node_rank 1 --master_addr "IP地址" --master_port 1234 test.py

*****************************************
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 0 || RANK: 8 || WORLD_SIZE: 16
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 1 || RANK: 9 || WORLD_SIZE: 16
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 3 || RANK: 11 || WORLD_SIZE: 16
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 4 || RANK: 12 || WORLD_SIZE: 16
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 2 || RANK: 10 || WORLD_SIZE: 16
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 5 || RANK: 13 || WORLD_SIZE: 16
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 6 || RANK: 14 || WORLD_SIZE: 16
|| MASTER_ADDR: IP地址 || MASTER_PORT: 1234 || LOCAL_RANK: 7 || RANK: 15 || WORLD_SIZE: 16

torchrun

torchrun 提供了 torch.distributed.launch 功能的超集,并具有一些附加功能:

从 torch.distributed.launch 过渡到 torchrun

https://pytorch.org/docs/stable/elastic/run.html

代码中需要改成从环境变量中读取

slurm多机启动

上面的torch.distributed.launch或者torchrun启动如果多机的话要手动在多个机器执行,使用slurm提交一个命令脚本slurm会分发到多个node来执行。

srun --nodes=2 ./torchrun_script.sh

https://github.com/pytorch/tutorials/blob/main/intermediate_source/ddp_tutorial.rst

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: torch.distributed.launchPyTorch的一个工具,可以用来启动分布式训练任务。具体使用方法如下: 首先,在你的代码中使用torch.distributed模块来定义分布式训练的参数,如下所示: ``` import torch.distributed as dist dist.init_process_group(backend="nccl", init_method="env://") ``` 这个代码片段定义了使用NCCL作为分布式后端,以及使用环境变量作为初始化方法。 接下来,在命令行中使用torch.distributed.launch启动分布式训练任务,如下所示: ``` python -m torch.distributed.launch --nproc_per_node=NUM_GPUS YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3 and so on) ``` 其中,NUM_GPUS是每个节点上使用的GPU数量,YOUR_TRAINING_SCRIPT.py是你的训练脚本,(--arg1 --arg2 --arg3 and so on)是传递给训练脚本的参数。 torch.distributed.launch会自动为每个节点启动一个进程,并传递适当的环境变量和命令行参数。在训练过程中,你可以使用torch.distributed模块来进行分布式的操作,如在每个节点之间同步参数、收集梯度等。 希望这个回答对你有所帮助! ### 回答2: torch.distributed.launchPyTorch中用于多节点分布式训练的一个工具。它能够帮助我们简化在多个节点上启动分布式训练的过程,使得代码编写更加简单方便。 使用torch.distributed.launch,首先需要确保环境中已经安装了PyTorch库。然后,在命令行中执行以下命令: python -m torch.distributed.launch --nproc_per_node=<num_gpus> <your_script.py> (--arg1 --arg2 ...) 其中,"<num_gpus>"是每个节点上的GPU数量,"<your_script.py>"是要运行的脚本路径。"--arg1 --arg2 ..."是你的脚本所需的各种参数,与普通的命令行参数传递方式相同。 执行上述命令后,torch.distributed.launch将会自动在每个节点上启动训练进程,并负责进程间的通信和同步。每个进程将会自动获得一个本地的rank编号,从0开始递增,并且可以通过torch.distributed.get_rank()函数获得。 在你的训练脚本中,可以通过torch.distributed.get_world_size()获得总的节点数量,通过torch.distributed.get_rank()获得当前节点的rank编号。你可以根据这些信息来区分不同的节点,进行相应的分布式操作。 除了以上基本用法外,torch.distributed.launch还提供了其他的一些选项,如--use_env、--master_addr、--master_port等,可以根据需要进行使用。可以通过在命令行中执行python -m torch.distributed.launch --help来查看更多详细的帮助信息。 总之,使用torch.distributed.launch可以方便地实现多节点分布式训练,简化了代码编写和启动的过程,提高了训练效率和灵活性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值