DistributedDataParallel是Pytorch中用于支持分布式训练的模块,允许在多个GPU和多台机器上训练深度学习模型。本文介绍DistributedDataParallel的简单使用流程。
1. 导入库
import torch
import torch.nn as nn
import torch.optim as optim
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel
2. 初始化分布式环境
def main():
# 初始化分布式环境
dist.init_process_group(backend='nccl', init_method='env://')
# 设置当前进程的本地 GPU 设备
local_rank = int(os.environ['LOCAL_RANK'])
torch.cuda.set_device(local_rank)
# 其他初始化代码...
上述代码中,backend=‘nccl’ 表示使用 NCCL 通信后端,init_method='env://' 表示使用环境变量来初始化进程组。
3. 定义模型
这里随意定义了一个深度学习模型,将其包装在DistributedDataParallel中:
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 64, 5)
self.fc1 = nn.Linear(64, 10)
def forward(self, x):
x = self.conv1(x)
x = x.view(-1, 64)
x = self.fc1(x)
return x
model = Net().to('cuda')
model = DistributedDataParallel(model)
4.定义优化器
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
5. 训练模型
训练循环与单GPU训练类似,但在使用DistributedDataParallel时,数据加载和模型训练会自动分布到多个GPU上。示例如下:
for epoch in range(num_epochs):
for inputs, labels in dataloader:
inputs, labels = inputs.to('cuda'), labels.to('cuda')
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
这个训练循环会在多个GPU上并行运行,将数据和模型拆分到多个GPU上,自动进行梯度同步。(与DataParallel相比这里反而更简单~)
6.启动多个进程
在分布式训练中,通常会在多台机器上启动多个进程。可以使用torch.distributed.launch辅助函数来简化这个过程:
python -m torch.distributed.launch --nproc_per_node=NUM_GPUS my_test.py
其中,NUM_GPUs是每台机器上使用的GPU数量, my_test是包含上述代码的Python脚本。
这个脚本会启动多个进程,每个进程在不同的GPU上运行,同时使用分布式训练框架来协调训练过程。
以上就是利用DistributedDataParallel进行多GPU训练模型的完整操作步骤了。