[DistributedDataParallel]分布式训练¶
概述¶
PyTorch
官网教程上提供了分布式训练文档,不过说的不是很清楚。在网上找了另外一个参考:Distributed data parallel training in Pytorch,里面并没有介绍具体的实现原理,不过给出了清晰的实例。还参考了以下资源:
- tczhangzhi/pytorch-distributed
- Getting Started with Distributed Data Parallel
- [pytorch]单多机下多GPU下分布式负载均衡训练
- pytorch 分布式计算 你们都遇到过哪些 坑/bug?
- Writing Distributed Applications with PyTorch
示例位于: zjykzj/pytorch-distributed ,完整实现可参考:ZJCV/TSN
原理¶
共有两种类型的分布式训练:
- 模型分布
- 数据分布
PyTorch
提供了两种类型的实现,不过当前使用的是数据分布实现,PyTorch
实现了两种方式:
DataParallel
DistributedDataParallel
前者使用单进程模式,虽然实现很简单,但是其将所有GPU
的运算结果汇集到主GPU
进行计算,会出现多GPU
负载不均衡问题;后者对每个GPU
执行单个进程,所有进程在各自GPU
内计算完成loss
后,将loss
汇集到主GPU
进行计算,大大减缓了负载不均衡的问题。当前使用的是DistributedDataParallel
简单的说,DistributedDataParallel
使用了All-Reduce
技术:
- 首先每个
GPU
各有一个进程,每个进程里面有自己的数据加载器、模型、优化器和学习率调度器 - 首先所有
GPU
各自加载同一个模型,所有模型对象进行相同的初始化 - 将数据集按
GPU
个数进行切分,每个GPU
载入不同的数据进行前向计算和梯度计算 - 通过ring allreduce方式,
GPU
之间相互通信以平均由不同GPU
计算的梯度,将平均梯度应用于各自GPU模型的反向传播以获得新权重
通过平均梯度方式,保证每个GPU
上的模型参数在整个训练过程中保持一致,分布式训练相当于大批量的单GPU
训练
复现¶
参考:
What is the differenc between cudnn.deterministic and .cudnn.benchmark?
为了保证复现性,在训练时可以设置
import numpy as np
import torch
torch.manual_seed(0)
np.random.seed(0)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
在测试时
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = True
额外阅读¶
- Is my DistributedDataParallel code correct? Why is DistributedDataParallel’s performance worse than nn.DataParallel?
- Setting visible devices with Distributed Data Parallel
- torch.cuda.empty_cache() write data to gpu0 #25752
- Distributed training creates multiple processes in GPU0
- Avoid keeping two copies of gradients (param.grad and buckets) in DDP
- Is it expected for DistributedDataParallel to use more memory on 1 GPU in a 1GPU:1process setup?
- Torch not able to utilize GPU ram properly
- DDP taking up too much memory on rank 0
- How to use SyncBatchNorm in nn.parallel.DistributedDataParallel with v1.1.0?
- Distributed: other ranks not waiting rank_0’s evaluation
- Distributed Data Parallel with Multiple Losses
- Average loss in DP and DDP
- DistributedDataParallel loss compute and backpropogation?
- What is the differenc between cudnn.deterministic and .cudnn.benchmark?