PyTorch Data Parrallel数据并行

  • 可选择:数据并行处理
  • 本文将学习如何用 DataParallel 来使用多 GPU。 通过 PyTorch 使用多个 GPU 非常简单。可以将模型放在一个 GPU:
  • device = torch.device("cuda:0")
  • model.to(device)
  • 可以复制所有的张量到 GPU:
  • mytensor = my_tensor.to(device)
  • 调用 my_tensor.to(device) 返回一个 my_tensor, 新的复制在GPU上,而不是重写 my_tensor。需要分配一个新的张量并且在 GPU 上使用这个张量。
  • 在多 GPU 中执行前馈,后继操作是非常自然的。尽管如此,PyTorch 默认只会使用一个 GPU。通过使用 DataParallel 让模型并行运行,可以很容易的在多 GPU 上运行操作。
  • model = nn.DataParallel(model)
  • 这是整个教程的核心,接下来将会详细讲解。 引用和参数
  • 引入 PyTorch 模块和定义参数
  • import torch
  • import torch.nn as nn
  • from torch.utils.data import Dataset, DataLoader
  • 参数
  • input_size = 5
  • output_size = 2
  • batch_size = 30
  • data_size = 100
  • 设备
  • device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  • 实验(玩具)数据
  • 生成一个玩具数据。只需要实现 getitem.
  • classRandomDataset(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)
  • 简单模型
  • 做一个小 demo,模型只是获得一个输入,执行一个线性操作,然后给一个输出。可以使用 DataParallel   在任何模型(CNN, RNN, Capsule Net 等等.)
  • 放置了一个输出声明在模型中,检测输出和输入张量的大小。在 batch rank 0 中的输出。
  • classModel(nn.Module):
  • # Our model
  • def__init__(self, input_size, output_size):
  • super(Model, self).__init__()
  • self.fc = nn.Linear(input_size, output_size)
  • defforward(self, input):
  • output = self.fc(input)
  • print("\tIn Model: input size", input.size(),
  • "output size", output.size())
  • return output
  • 创建模型并且数据并行处理
  • 这是本文的核心。首先需要一个模型的实例,然后验证是否有多个 GPU。如果有多个 GPU,可以用 nn.DataParallel 来包裹模型。然后使用 model.to(device) 把模型放到多 GPU 中。
  • model = Model(input_size, output_size)
  • if torch.cuda.device_count() > 1:
  • print("Let's use", torch.cuda.device_count(), "GPUs!")
  • # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
  • model = nn.DataParallel(model)
  • model.to(device)
  • 输出:
  • Let's use 2 GPUs!
  • 运行模型: 现在可以看到输入和输出张量的大小了。
  • 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([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])
  • 结果:
  • 如果没有 GPU 或者只有一个 GPU,当获取 30 个输入和 30 个输出,模型将期望获得 30 个输入和 30 个输出。但是如果有多个 GPU ,会获得这样的结果。
  • 多 GPU
  • 如果有 2 个GPU,会看到:
  • # on 2 GPUs
  • Let's 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,会看到:
  • Let's 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])
  • 如果有 8个GPU,会看到:
  • Let's use 8 GPUs!
  • 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])
  • 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([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([4, 5]) output size torch.Size([4, 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([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([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])
  • In Model: input size torch.Size([4, 5]) output size torch.Size([4, 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([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([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([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([30, 5]) output_size torch.Size([30, 2])
  • In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
  • In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
  • In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
  • In Model: input size torch.Size([2, 5]) output size torch.Size([2, 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])
  • 总结
  • 数据并行自动拆分了数据并,将任务单发送到多个 GPU 上。当每一个模型都完成任务之后,DataParallel 收集并且合并这些结果,然后再返回。

PyTorch Data Parrallel数据并行的更多相关文章

  1. PyTorch Tutorials 5 数据并行(选读)

    %matplotlib inline 数据并行(选读) Authors: Sung Kim and Jenny Kang 在这个教程里,我们将学习如何使用 DataParallel 来使用多GPU. ...

  2. [源码解析] PyTorch分布式优化器(2)----数据并行优化器

    [源码解析] PyTorch分布式优化器(2)----数据并行优化器 目录 [源码解析] PyTorch分布式优化器(2)----数据并行优化器 0x00 摘要 0x01 前文回顾 0x02 DP 之 ...

  3. [源码解析] PyTorch 分布式(1) --- 数据加载之DistributedSampler

    [源码解析] PyTorch 分布式(1) --- 数据加载之DistributedSampler 目录 [源码解析] PyTorch 分布式(1) --- 数据加载之DistributedSampl ...

  4. [源码解析] PyTorch 分布式(2) --- 数据加载之DataLoader

    [源码解析] PyTorch 分布式(2) --- 数据加载之DataLoader 目录 [源码解析] PyTorch 分布式(2) --- 数据加载之DataLoader 0x00 摘要 0x01 ...

  5. Data Pump(数据抽取)介绍

    从10g开始,Oracle提供更高效的Data Pump(即expdp/impdp)来进行数据的导入和导出,老的exp/imp还可以用,但已经不建议使用.注意:expdp/impdp和exp/imp之 ...

  6. 深度神经网络DNN的多GPU数据并行框架 及其在语音识别的应用

    深度神经网络(Deep Neural Networks, 简称DNN)是近年来机器学习领域中的研究热点,产生了广泛的应用.DNN具有深层结构.数千万参数需要学习,导致训练非常耗时.GPU有强大的计算能 ...

  7. 【深度学习系列2】Mariana DNN多GPU数据并行框架

    [深度学习系列2]Mariana DNN多GPU数据并行框架  本文是腾讯深度学习系列文章的第二篇,聚焦于腾讯深度学习平台Mariana中深度神经网络DNN的多GPU数据并行框架.   深度神经网络( ...

  8. Oracle 11g R2 Backup Data Pump(数据泵)之expdp/impdp工具

    Oracle Data Pump(以下简称数据泵)是Oracle 10g开始提供的一种数据迁移工具,同时也被广大DBA用来作为数据库的逻辑备份工具和体量较小的数据迁移工具.与传统的数据导出/导入工具, ...

  9. .Net并行编程(一)-TPL之数据并行

    前言 许多个人计算机和工作站都有多个CPU核心,可以同时执行多个线程.利用硬件的特性,使用并行化代码以在多个处理器之间分配工作. 应用场景 文件批量上传 并行上传单个文件.也可以把一个文件拆成几段分开 ...

随机推荐

  1. Vulkan移植GpuImage(四)从D到O的滤镜

    现把D到O的大部分滤镜用vulkan的ComputeShader实现了,列举其中一些有点特殊的说明. GaussianBlurPosition 指定区域高斯模糊 没有按照GPUImage里的方式实现, ...

  2. 【Spring】Spring中Bean的生命周期

    Spring中Bean的生命周期依赖于Spring的容器,大致可分为以下4个阶段: 1.Bean的初始化阶段 2.Bean属性赋值的阶段,获取上下文关联 3.Bean初始化的阶段 4.Bean销毁的阶 ...

  3. hdu4126(MST + 树形dp

    题意:       这个题目和hdu4756差不多,是给你一个图,然后是q次改变边的权值,权值只增不减,最后问你每次改变之后的最小树的平均值是多少. 思路:(prim+树形dp)       先跑一边 ...

  4. hdu5105给你一个方程,让你求极值(直接暴力)

    题意:       给你一个方程f[x] = abss(a * x * x * x + b * x * x + c * x + d); 然后给你各个参数还有x(-100<x<100)的取值 ...

  5. 我为Dexposed续一秒——论ART上运行时 Method AOP实现

    转载于:http://weishu.me/2017/11/23/dexposed-on-art/ 两年前阿里开源了 Dexposed 项目,它能够在Dalvik上无侵入地实现运行时方法拦截,正如其介绍 ...

  6. hdu4888 最大流(构造矩阵)

    题意:       让你构造一个矩阵,满足每一行的和,和每一列的和都等于他给的,还要判断答案是否唯一,还有一点就是矩阵内所有的数字都是[0,k]范围的. 思路:       这个题目看完就让我想起了h ...

  7. 【报错】org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSocketHandlerMapping' defined in class path resource

    环境:maven+eclipse+jdk1.8 [tomcat使用的是maven自带的插件,实质原因就是出在tomcat版本问题] 背景:在进行SSM集成WebSocket的时候,项目启动报org.s ...

  8. XAML 属性元素,标记扩展和注释

    这节来讲一下XAML中的属性元素,标记扩展,和注释. 属性元素 一般的,我们想要对一个标签的属性赋值,可以直接在标签内部键入属性名给其赋值,如我们给button的Content属性赋值: <Bu ...

  9. Jenkins 基础篇 - 任务分类

    从前面的小节中我们看到在创建 Jenkins 任务的时候有好几种类型,如果你专门安装了 Maven 相关插件,可能还会有一个[构建一个 maven 项目]的任务类型,那这些任务类型究竟有何区别,以及我 ...

  10. 普里姆(Prim)算法

    概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图(即"带权图")里搜索最小生成树.即此算法搜索到的边(Edge)子集所构成的树中,不但包括了连通图里的所有顶点(V ...