码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • torch.nn.DataParallel类


    参考   torch.nn.DataParallel类 - 云+社区 - 腾讯云

    class torch.nn.DataParallel(module, device_ids=None, output_device=None, dim=0)[source]

    Implements data parallelism at the module level.

    This container parallelizes the application of the given module by splitting the input across the specified devices by chunking in the batch dimension (other objects will be copied once per device). In the forward pass, the module is replicated on each device, and each replica handles a portion of the input. During the backwards pass, gradients from each replica are summed into the original module.

    The batch size should be larger than the number of GPUs used.

    See also: Use nn.DataParallel instead of multiprocessing

    Arbitrary positional and keyword inputs are allowed to be passed into DataParallel but some types are specially handled. tensors will be scattered on dim specified (default 0). tuple, list and dict types will be shallow copied. The other types will be shared among different threads and can be corrupted if written to in the model’s forward pass.

    The parallelized module must have its parameters and buffers on device_ids[0] before running this DataParallel module.

    Warning

    In each forward, module is replicated on each device, so any updates to the running module in forward will be lost. For example, if module has a counter attribute that is incremented in each forward, it will always stay at the initial value because the update is done on the replicas which are destroyed after forward. However, DataParallel guarantees that the replica on device[0] will have its parameters and buffers sharing storage with the base parallelized module. So in-place updates to the parameters or buffers on device[0] will be recorded. E.g., BatchNorm2d and spectral_norm() rely on this behavior to update the buffers.

    Warning

    Forward and backward hooks defined on module and its submodules will be invoked len(device_ids) times, each with inputs located on a particular device. Particularly, the hooks are only guaranteed to be executed in correct order with respect to operations on corresponding devices. For example, it is not guaranteed that hooks set via register_forward_pre_hook() be executed before all len(device_ids) forward() calls, but that each such hook be executed before the corresponding forward() call of that device.

    Warning

    When module returns a scalar (i.e., 0-dimensional tensor) in forward(), this wrapper will return a vector of length equal to number of devices used in data parallelism, containing the result from each device.

    Note

    There is a subtlety in using the pack sequence -> recurrent network -> unpack sequence pattern in a Module wrapped in DataParallel. See My recurrent network doesn’t work with data parallelism section in FAQ for details.

    Parameters

    • module (Module) – module to be parallelized

    • device_ids (list of python:int or torch.device) – CUDA devices (default: all devices)

    • output_device (int or torch.device) – device location of output (default: device_ids[0])

    Variables

    ~DataParallel.module (Module) – the module to be parallelized

    Example:

    1. >>> net = torch.nn.DataParallel(model, device_ids=[0, 1, 2])
    2. >>> output = net(input_var) # input_var can be on any device, including CPU

  • 相关阅读:
    Mac 使用 vscode 写 latex
    常用 Shell 脚本
    【Sql】sql server还原数据库的时候,提示:因为数据库正在使用,所以无法获得对数据库的独占访问权。
    android pcm播放器:有进度条同步、快进、快退、倍速功能
    windows命令行安装工具/包/软件后,命令行命令找不到(npm示例)
    ACREL DC energy meter Application in Indonesia
    在线渲染3d怎么用?3d快速渲染步骤设置
    基于 Angular和Material autocomplete组件再封装的可双向绑定key-value的可输入下拉框
    氨基修饰海藻酸钠;Alginate-Amine;Amine-Alginate
    聊聊ChatGLM-6B源码分析(二)
  • 原文地址:https://blog.csdn.net/weixin_36670529/article/details/101776756
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号