torch.nn.DataParallel使用细节

import torch
from torch import nn
#使用机器有两个GPU

a=torch.rand([5,4])
model=nn.Sequential(
      nn.Linear(4,2)
)

model=nn.DataParallel(model)
print(model.device_ids)
b=model(a)
print(b)

#报错,RuntimeError: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cpu
#解决方案:model=nn.DataParallel(model).to(torch.device('cuda'))
#或者:
#model.to(torch.device('cuda'))
#model=nn.DataParallel(model)

#The parallelized module must have its parameters and buffers on device_ids[0] before running this DataParallel module.
#在运行此DataParallel模块之前,并行化模块的参数和缓冲区必须位于device_ids[0]上。
#也就是说model=nn.DataParallel(model).to(torch.device('cuda:1'))
#会报错RuntimeError: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cuda:1

"""
解决报错后运行结果:
[0, 1]
tensor([[-0.7412, -0.2679],
        [-0.4051, -0.2946],
        [-0.5498, -0.2944],
        [-0.3615, -0.2638],
        [-0.2613, -0.2737]], device='cuda:0', grad_fn=<GatherBackward>)
"""

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

#torch.device('cuda')默认为torch.device('cuda:0')

当cuda:0显存不够时,可以使用例如:os.environ["CUDA_VISIBLE_DEVICES"] = "3,2,0,1"

这样的命令来把cuda:3修改为cuda:0。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
torch.nn.parallel.DataParallel是PyTorch中的一个模块,用于在单个节点上进行多GPU数据并行训练。它能够自动将输入数据划分为多个小批次,并将这些小批次分发到不同的GPU上进行计算。每个GPU上都有一个模型副本,每个副本都独立地计算损失和梯度,并将梯度聚合后进行参数更新。 然而,torch.nn.parallel.DataParallel有一些限制,例如需要在每个GPU上有足够的显存来存储模型和梯度,因为它会复制模型到每个GPU上。此外,它还要求输入数据能够被划分为多个小批次,并且每个小批次的大小是相同的。 相比之下,torch.nn.parallel.DistributedDataParallel是一个更高级的模块,可以在单节点或多节点上进行多进程的分布式训练。每个模型副本由独立的进程控制,可以在不同的机器上运行。它不需要将模型复制到每个GPU上,而是通过进程间通信来在各个进程之间共享模型参数和梯度。这样可以更好地利用多个GPU和多个机器的计算资源,提高训练速度。 值得注意的是,torch.nn.parallel.DistributedDataParallel的初始化和使用方法与torch.nn.parallel.DataParallel略有不同,需要进行一些额外的设置和配置。但是,对于单节点的多GPU数据并行训练,torch.nn.parallel.DistributedDataParallel已被证明比torch.nn.parallel.DataParallel更快。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Pytorchtorch.nn.parallel.DistributedDataParallel](https://blog.csdn.net/baidu_35120637/article/details/110816619)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [torch.nn.parallel.DistributedDataParallel](https://blog.csdn.net/weixin_45216013/article/details/125472676)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值