当前位置: 首页 > news >正文

Pytorch多GPU分布式训练代码编写

Pytorch多GPU分布式训练代码编写

一、数据并行

1.单机单卡

  • 模型拷贝

    • model.cuda() 原地操作
  • 数据拷贝(每步)

    • data=data.cuda() 非原地操作
  • 基于torch.cuda.is_available()来判断是否可用

  • 模型保存与加载

    • torch.save 来保存模型、优化器、其他变量
    • torch.load(file.pt,map_location=torch.device(“cuda”/“cuda:0”/“cpu”))

实际演示

  1. 环境检测

    • 代码中操作

      if __name__ =="__main__":if torch.cuda.is_available():logging.warning("Cuda is available!")os.environ["CUDA_VISIBLE_DEVICES"]="0" #指定使用第0号gpuelse:logging.warning("Cuda is not available! Exit!")return
      
    • 命令行操作

      CUDA_VISIBLE_DEVICE="0" python xxx.py
      
  2. 模型拷贝

    image-20240906215319656

  3. 数据拷贝

    image-20240906215835882

  4. 模型保存与加载

    image-20240906220229669

​ 以上便是单机单卡的模型的加载和训练

2.单机多卡

  • 检测GPU数目
    • torch.cuda.device_count()
    • 可用通过命令行CUDA_VISIBLE_DEVICES=”“ 来限制GPU卡的使用
  • torch.nn.DataParallel API(已经淘汰)
    • 简单一行代码,包裹model即可
      • model=DataParallel(model.cuda(),device_ids=[0,1,2,3])
      • data=data.cuda()
    • 模型保存与加载
      • torch.save 注意模型需要调用model.module.state_dict()
      • torch.load 需要注意map_location的使用
    • 缺点
      • 单进程,效率慢
      • 不支持多机情况
      • 不支持模型并行
    • 注意实现
      • 此处的【dataloder中】batch_size应该是每个GPU的batch_size的总和

​ 演示,我们将之前的单卡的程序改造成多卡:

  1. 检测GPU数量

image-20240906223326186

  1. 用DataParallel来包裹住模型

    image-20240907074343558

  2. save的地方需要修改

    image-20240907074525268

  3. load时的注意事项

    image-20240907075048175

  4. batch_size的设置

    image-20240907075303926

  • torch.nn.parallel.DistributedDataParallel(推荐)
    • 多进程执行多卡训练,效率高
    • 代码编写流程
      • torch.distributed.init_process_group(“nccl”,world_size=n_gpus,rank=args.local_rank)
      • torch.cuda.set_device(args.local_rank) 该语句作用相当于CUDA_VISIBLE_DEVICES环境变量
      • model=DistributedDataParallel(model.cuda(args.local_rank),device_ids=[args.local_rank])
      • train_sampler=DistributedSampler(train_dataset)源码位于torch/utils/data/distributed.py
      • train_dataloader=DataLoader(…,sampler=train_sampler)
      • data=data.cuda(args.local_rank)
    • 执行命令
      • python -m torch.distributed.launch - - nproc_per_node=n_gpus train.py
    • 模型保存与加载
      • torch.save 在local_rank=0 的位置进行保存,同样注意调用model.module.state_dict()
      • torch.load 注意 map_location
    • 注意事项
      • train.py 中要有接受local_rank的参数选项,launch会传入这个参数
      • 每个进程的batch_size应该是一个GPU所需要的batch_size大小
      • 在每个周期开始处,调用train_sampler.set_epech(epoch)可以使得数据充分打乱
      • 有了sampler,就不要在DataLoader中设置shuffle=True了

演示:

  1. 指定GPU数量

    image-20240907085058885

  2. 设置使用哪几张卡

    image-20240907085224555

  3. 模型拷贝

    image-20240907085440920

  4. 数据拷贝

    ​ 我们本来传入train函数中的是data_loader,但是train_sampler=DistributedSampler(train_dataset)需要的是dataset,所以我们在向train函数中传递参数的时候需要稍加改动:
    image-20240907092159614

    image-20240907092649161

    image-20240907092955004

  5. save时注意事项

    ​ 我们只在编号0的GPU上进行保存

    image-20240907093143875

  6. 每个epoch打乱数据顺序

    image-20240907093515875

  7. 命令行运行

    image-20240907093754264

3.多机多卡

  • torch.nn.parallel.DistributedDataParallel
    • 代码编写流程
      • 跟单机多卡一致
    • 执行命令(以两节点为例,每个节点处有n_gpus个GPU)
      • python -m torch.distributed.launch - -nproc_per_node=n_gpus - -nnodes=2 - -node_rank=0 - -master_addr=“主节点IP” - -master_port=主节点端口 train.py
      • python -m torch.distributed.launch - -nproc_per_node=n_gpus - -nnodes=2 - -node_rank=1 - -master_addr=“主节点IP” - -master_port=主节点端口 train.py
    • 模型保存与加载
      • 同单机多卡基本一致

二、模型并行

1.背景

  • 模型参数太大,单个GPU无法容纳,需要将模型的不同层拆分到多个GPU上

2.示例

  • 参考:http://pytorch.org/tutorials/intermediate/model_parallel_tutorial.html

3.模型保存与加载

  • 分多个module进行分别保存与加载(略)

image-20240907095750272


http://www.mrgr.cn/news/21345.html

相关文章:

  • box64 安装
  • 2024 年高教社杯全国大学生数学建模竞赛B题第三问详细解题思路(终版)
  • SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
  • PDF和CDF
  • Python教程(二十一) : 从零开始制作计算器应用【PyQt6】
  • 独立按键单击检测(延时消抖+定时器扫描)
  • unity导入半透明webm + AE合成半透明视频
  • 华为OD机试真题-高矮个子排队-2024年OD统一考试(E卷)
  • c++ 构造函数详解
  • 什么是内核空间
  • 学习记录之Java学习笔记3
  • Python教程(二十) : 十分钟入门【PyQt6】
  • Unity 资源 之 Super Confetti FX:点亮项目的璀璨粒子之光
  • 2024年9月6日嵌入式学习
  • FSMC
  • LeetCode 每日一题 2024/9/2-2024/9/8
  • Unity Adressables 使用说明(四)分发远程内容(Distribute Remote Content)
  • 人工智能在胃癌中的最新研究进展|顶刊速递·24-09-07
  • 【数学分析笔记】第3章第1节 函数极限(6)
  • 线性表的定义和基本操作