栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Pytorch 多GPU数据并行(DataParallel)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pytorch 多GPU数据并行(DataParallel)

利用多个空闲GPU,提高模型训练推理速度

import torch 
from gpuinfo import GPUInfo

# 查询空闲GPU
gpu_ids = GPUInfo.check_empty()
# 设置模型初始化设备
device = torch.device(f"cuda:{str(gpu_ids[0])}" if gpu_ids else "cpu")
# 数据并行  model = xxxxxx
model = torch.nn.DataParallel(model, device_ids=gpu_ids)
# 模型加载到初始化设备中
model = model.to(device)
# 模型计算数据并行
for line in dataloader:
    # 数据不需要加载到相同的设备中
    moded(line)
# ......

DistributedDataParallel 相对于 DataParallel的优势主要在模型训练阶段,模型推理时由于不涉及参数更新不需要进程间通信,所以推理速度应该相差不大

# The difference between DistributedDataParallel and DataParallel is: 
# DistributedDataParallel uses multiprocessing where a process is created for each GPU, 
# while DataParallel uses multithreading. By using multiprocessing, each GPU has its dedicated process, 
# this avoids the performance overhead caused by GIL of Python interpreter.

模型训练时分布式多进程数据并行可结合这两个类改写
torch.nn.parallel.DistributedDataParallel
torch.utils.data.distributed.DistributedSampler
Distributed Data Parallel
(有时间再写一版)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/461663.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号