分布式训练ddp和slurm

使用ddp:https://blog.51cto.com/u_16213675/9632480

使用slurm:https://blog.csdn.net/LittleNyima/article/details/136813418

使用 Slurm 管理多机多卡训练

对于一般的用户来说,ddp多机多卡训练方式已经基本上够用了。然而对于需要进行更大规模训练的人来说,在每个节点上依次运行命令比较繁琐并且容易出错。同时,大规模 GPU 集群需要有效的管理方式,来提高资源利用率。为了做到这一点,Slurm 是一个比较好的选择。Slurm 主要的作用在于任务调度,其可以为用户分配计算机节点来执行任务,并且支持任务队列,可以比较高效地分配资源。

在编写训练脚本时,无论启动方式如何,我们关心的都是 master 节点地址、local rank、进程总数等信息,我们可以参考 mmcv 的方式对这些内容进行初始化:

python 复制代码
def _init_dist_slurm(backend: str, port: Optional[int] = None) -> None:
    proc_id = int(os.environ['SLURM_PROCID'])
    ntasks = int(os.environ['SLURM_NTASKS'])
    node_list = os.environ['SLURM_NODELIST']
    num_gpus = torch.cuda.device_count()
    torch.cuda.set_device(proc_id % num_gpus)
    addr = subprocess.getoutput(
        f'scontrol show hostname {node_list} | head -n1')
    # specify master port
    if port is not None:
        os.environ['MASTER_PORT'] = str(port)
    elif 'MASTER_PORT' in os.environ:
        pass  # use MASTER_PORT in the environment variable
    else:
        # if torch.distributed default port(29500) is available
        # then use it, else find a free port
        if _is_free_port(29500):
            os.environ['MASTER_PORT'] = '29500'
        else:
            os.environ['MASTER_PORT'] = str(_find_free_port())
    # use MASTER_ADDR in the environment variable if it already exists
    if 'MASTER_ADDR' not in os.environ:
        os.environ['MASTER_ADDR'] = addr
    os.environ['WORLD_SIZE'] = str(ntasks)
    os.environ['LOCAL_RANK'] = str(proc_id % num_gpus)
    os.environ['RANK'] = str(proc_id)
    dist.init_process_group(backend=backend)

在任务启动时,使用 Slurm 提供的工具:

python 复制代码
srun \
    -p ${PARTITION} \
    --job-name=${JOB_NAME} \
    --gres=${GPUS_PER_NODE} \
    --ntasks=${GPUS} \
    --ntasks-per-node=${GPUS_PER_NODE} \
    --cpus-per-task=${CPUS_PER_TASK} \
    --kill-on-bad-exit=1 \
    python train.py
相关推荐
xianyuCcCcCCCcc14 小时前
Ceph 分布式存储:核心架构与集群部署
分布式·ceph·架构
真上帝的左手14 小时前
10. 软件设计&架构-分布式文件存储系统
分布式·架构·应用架构
Thomas.Sir19 小时前
第30课:分布式事务解决方案 & Seata最终一致性实战
分布式·spring cloud·seata
donk131420 小时前
React Native for OpenHarmony 实战:三方库 react-native-circular-action-menu 的鸿蒙化适配指南
javascript·分布式·react native·react.js·wpf·harmonyos
蓝胖的四次元口袋21 小时前
RabbitMQ知识梳理(1)
分布式·rabbitmq
霸道流氓气质1 天前
Redisson BloomFilter:分布式位图实现、误判率调优与缓存穿透三级防护的 Spring Boot 生产级实战
spring boot·分布式·缓存
流烟默1 天前
ClickHouse 集群建表实战:从引擎选择到本地表 / 分布式表落地
分布式·clickhouse
donk13142 天前
React Native for OpenHarmony 实战:三方库 react-native-step-indicator 的鸿蒙化适配指南
分布式·react native·react.js·wpf·harmonyos
梅菲塔亚2 天前
Ceph分布式存储
分布式·ceph
acrel72 天前
Acrel-1000系列分布式光伏监控系统在湖北荆门一马光彩大市场屋顶光伏发电项目中应用
分布式·光伏并网