pytorch利用多个GPU并行计算多gpu
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Answer3664/article/details/98992409
参考:
https://pytorch.org/docs/stable/nn.html
https://github.com/apachecn/pytorch-doc-zh/blob/master/docs/1.0/blitz_data_parallel_tutorial.md
一、 torch.nn.DataParallel
torch.nn.DataParallel(module, device_ids=None, output_device=None, dim=0)
在正向传递中,模块在每个设备上复制,每个副本处理一部分输入。在向后传递期间,来自每个副本的渐变被加到原始模块中。
module:需要并行处理的模型
device_ids:并行处理的设备,默认使用所有的cuda
output_device:输出的位置,默认输出到cuda:0
例子:
>>> net = torch.nn.DataParallel(model, device_ids=[0, 1, 2])
>>> output = net(input_var) # input_var can be on any device, including CPU
torch.nn.DataParallel()返回一个新的模型,能够将输入数据自动分配到所使用的GPU上。所以输入数据的数量应该大于所使用的设备的数量。
二、一个完整例子
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
# parameters and DataLoaders
input_size = 5
output_size = 2
batch_size = 30
data_size = 100
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# 随机数据集
class RandomDataset(Dataset):
def __init__(self, size, length):
self.len = length
self.data = torch.randn(length, size)
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return self.len
rand_loader = DataLoader(dataset=RandomDataset(input_size, data_size),
batch_size=batch_size, shuffle=True)
# 以简单模型为例,同样可以用于CNN, RNN 等复杂模型
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)
if torch.cuda.device_count() > 1:
print("Use", torch.cuda.device_count(), 'gpus')
model = nn.DataParallel(model)
model.to(device)
for data in rand_loader:
input = data.to(device)
output = model(input)
print('Outside: input size ', input.size(), 'output size: ', output.size())
输出:
In model: input size torch.Size([30, 5]) output size: torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output size: torch.Size([30, 2])
In model: input size torch.Size([30, 5]) output size: torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output size: torch.Size([30, 2])
In model: input size torch.Size([30, 5]) output size: torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output size: torch.Size([30, 2])
In model: input size torch.Size([10, 5]) output size: torch.Size([10, 2])
Outside: input size torch.Size([10, 5]) output size: torch.Size([10, 2])
若有2个GPU
Use 2 GPUs!
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])
若有3个GPU
Use 3 GPUs!
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])
总结:
DataParallel自动的划分数据,并将作业发送到多个GPU上的多个模型。DataParallel会在每个模型完成作业后,收集与合并结果然后返回给你。
————————————————
版权声明:本文为CSDN博主「Answerlzd」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Answer3664/article/details/98992409

pytorch利用多个GPU并行计算多gpu的更多相关文章
- 浅说CPU并行计算与GPU并行计算
最近在学一门课,叫做“C++与并行计算”.要用到多CPU(进程)并行的原理,实现语言是C++的MPI接口.联想到上学期用到CUDA C/C++来做并行计算,就对这两门语言做一个总结,分享下自己关于并行 ...
- 国内云计算的缺失环节: GPU并行计算(转)
[IT时代周刊编者按]云计算特有的优点和巨大的商业前景,让其成为了近年来的IT界最热门词汇之一.当然,这也与中国移动互联网的繁荣紧密相关,它们需要有相应的云计算服务作为支撑.但本文作者祁海江结合自身的 ...
- 科学计算 | Matlab 使用 GPU 并行计算
科学计算 | Matlab 使用 GPU 并行计算 本文转载自: https://sanwen8.cn/p/14bJc10.html Matlab下直接使用GPU并行计算(预告)< ...
- [信安Presentation]一种基于GPU并行计算的MD5密码解密方法
-------------------paper--------------------- 一种基于GPU并行计算的MD5密码解密方法 0.abstract1.md5算法概述2.md5安全性分析3.基 ...
- [源码解析] PyTorch 流水线并行实现 (6)--并行计算
[源码解析] PyTorch 流水线并行实现 (6)--并行计算 目录 [源码解析] PyTorch 流水线并行实现 (6)--并行计算 0x00 摘要 0x01 总体架构 1.1 使用 1.2 前向 ...
- 【视频开发】GPU编解码:GPU硬解码---DXVA
GPU编解码:GPU硬解码---DXVA 一.DXVA介绍 DXVA是微软公司专门定制的视频加速规范,是一种接口规范.DXVA规范制定硬件加速解码可分四级:VLD,控制BitStream;IDCT,反 ...
- ARM:移动GPU往PC GPU效能迈进
行动装置的热潮持续不退,各大手机制造商除了想尽办法推出外型酷炫的行动装置设备来吸引消费者的目光之外,更在行动应用处理器玩起多核心的「核」战争,无非是希望能够带给消费者更优异的效能新体验.然而,随着消费 ...
- TensorFlow指定使用GPU 多块gpu
持续监控GPU使用情况命令: $ watch -n 10 nvidia-smi1一.指定使用某个显卡如果机器中有多块GPU,tensorflow会默认吃掉所有能用的显存, 如果实验室多人公用一台服务器 ...
- 【并行计算-CUDA开发】浅谈GPU并行计算新趋势
随着GPU的可编程性不断增强,GPU的应用能力已经远远超出了图形渲染任务,利用GPU完成通用计算的研究逐渐活跃起来,将GPU用于图形渲染以外领域的计算成为GPGPU(General Purpose c ...
随机推荐
- 在windows系统和linux系统中查询IP地址命令的不同
在linux和windows系统上查询IP地址的命令是不一样的. 在linux中的命令行模式下,输入ifconfig即可查询到IP.而在windows系统下要查询IP地址需要先打开do ...
- Django项目:CRM(客户关系管理系统)--22--14PerfectCRM实现King_admin分页的省略显示
{#table_data_list.html#} {## ————————08PerfectCRM实现King_admin显示注册表的字段表头————————#} {% extends 'king_m ...
- Sublime setting 个性修改
{ // 编辑器字体大小 "font_size": 10, // 设置行间距 "line_padding_bottom":5, "line_paddi ...
- 珂朵莉树(ODT)笔记
珂朵莉树,又叫老司机树($Old\, Driver \, Tree$) 是一种暴力出奇迹,就怕数据不随机的数据结构. 适用 需要用线段树维护一些区间修改的信息…… 像是区间赋值(主要),区间加…… 原 ...
- 「LOJ10104」「一本通 3.6 练习 5」Blockade-Tarjan
首先%%%lydrainbowcat 这个题唔,真是Tarjan好题(?) 不知道为啥也叫BLO,首字母? <题面> 搜索树真真的棒! 仔细看看,知道是Tarjan了,切掉一个点有不通的对 ...
- poj 1269 Intersecting Lines(判断两直线关系,并求交点坐标)
Intersecting Lines Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12421 Accepted: 55 ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- “Bootstrap做的响应式菜单在iPhone上点击不了二级菜单“的解决办法!
只需把把点击的a(被点击的)变成button即可.
- jframe 设置左上角和任务栏的图标
默认就是 改成有意义的,一眼就能看出来功能的,比如一个小蜘蛛 第一个最简单的做法,把图片扔到工程的根目录,但是这样会相当乱,不便于文件管理 ImageIcon icon = new ImageIcon ...
- BZOJ1452 [JSOI2009]Count [2017年4月计划 树状数组02]
1452: [JSOI2009]Count Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2419 Solved: 1403[Submit][Stat ...