美文网首页
pytorch多卡训练

pytorch多卡训练

作者: 顾北向南 | 来源:发表于2021-07-10 22:59 被阅读0次

参考链接:https://fyubang.com/2019/07/23/distributed-training3/

pytorch多gpu并行训练 - 知乎 (zhihu.com)

PyTorch DistributedDataParallel使用小结_yuanye_yuanye的博客-CSDN博客

GitHub - tczhangzhi/pytorch-distributed: A quickstart and benchmark for pytorch distributed training.

1. 使用DistributedDataParallel并行训练

  • 运行命令:CUDA_VISIBLE_DEVICES=0,1 python -m torch.distributed.launch --nproc_per_node=2 torch_ddp.py
  • DistributedDataParallel比DataParallel运行的更快, 然后显存分配的更加均衡。
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
import os
from torch.utils.data.distributed import DistributedSampler
# 1) 初始化
torch.distributed.init_process_group(backend="nccl")

input_size = 5
output_size = 2
batch_size = 30
data_size = 90

# 2) 配置每个进程的gpu
local_rank = torch.distributed.get_rank()
torch.cuda.set_device(local_rank)
device = torch.device("cuda", local_rank)

class RandomDataset(Dataset):
    def __init__(self, size, length):
        self.len = length
        self.data = torch.randn(length, size).to('cuda')

    def __getitem__(self, index):
        return self.data[index]

    def __len__(self):
        return self.len

dataset = RandomDataset(input_size, data_size)
# 3)使用DistributedSampler
rand_loader = DataLoader(dataset=dataset,
                         batch_size=batch_size,
                         sampler=DistributedSampler(dataset))

class Model(nn.Module):
    def __init__(self, input_size, output_size):
        super(Model, self).__init__()
        self.fc = nn.Linear(input_size, output_size)

    def forward(self, input):
        output = self.fc(input)
        print("  In Model: input size", input.size(),
              "output size", output.size())
        return output
    
model = Model(input_size, output_size)

# 4) 封装之前要把模型移到对应的gpu
model.to(device)
    
if torch.cuda.device_count() > 1:
    print("Let's use", torch.cuda.device_count(), "GPUs!")
    # 5) 封装
    model = torch.nn.parallel.DistributedDataParallel(model,
                                                      device_ids=[local_rank],
                                                      output_device=local_rank)
   
for data in rand_loader:
    if torch.cuda.is_available():
        input_var = data
    else:
        input_var = data

    output = model(input_var)
    print("Outside: input size", input_var.size(), "output_size", output.size())
  • 使用DistributedDataParallel分布式训练,模型中含有BatchNormalization层会出现如下问题:
  • 解决方法:1)删除BN层;2)使用SyncBatchNorm层代替

相关文章

  • Pytorch单机多卡分布式训练 数据并行

    Pytorch单机多卡训练(数据并行训练) Pytorch的数据并行训练,已经被封装的十分完善。全程只需两步: 1...

  • pytorch多卡训练

    参考链接:https://fyubang.com/2019/07/23/distributed-training3...

  • PyTorch多卡训练

    https://blog.csdn.net/kejizuiqianfang/article/details/102...

  • PyTorch DDP模式单机多卡训练

    一、启动训练的命令 其中torch.distributed.launch表示以分布式的方式启动训练,--nproc...

  • pytorch: 一机多卡训练的尝试

    作 者: 月牙眼的楼下小黑联 系: zhanglf_tmac (Wechat)声 明: 欢迎转载本文中的图片或文字...

  • PyTorch 训练

     PyTorch 训练与加速神经网络训练. 更多可以查看官网 :* PyTorch 官网 批训练 Torch 中提...

  • 踩过的pytorch坑

    1. 多卡训练模型 如果使用torch.nn.DataParallel(model)多卡并行训练模型的话需要注意:...

  • pytorch 多卡负载不均衡

    使用3个1080进行模型训练,发现设置batch_size=16把第一张卡给炸掉,但是其他的卡只用了一半不到,严重...

  • 基于Pytorch的MLP实现

    基于Pytorch的MLP实现 目标 使用pytorch构建MLP网络 训练集使用MNIST数据集 使用GPU加速...

  • Pytorch中多GPU训练指北

    前言 在数据越来越多的时代,随着模型规模参数的增多,以及数据量的不断提升,使用多GPU去训练是不可避免的事情。Py...

网友评论

      本文标题:pytorch多卡训练

      本文链接:https://www.haomeiwen.com/subject/miuzdltx.html