关于使用torch.dataparallel时锁死的解决办法

1

大致情况是这样的,就是在训练中通过torch.dataparallel时进行了训练,这个时候会出现不报错,也不显示任何进展的问题。这种情况可能一开始训练就会出现,也有可能再重新训练时出现。当终止进程时会出现

Process finished with exit code 137 (interrupted by signal 9: SIGKILL

然后我去查看gpu的使用情况,主显卡(用于加载模型)显存已经占用了一部分,说明模型已经加载进去了。而并行gpu却基本没有显存占用,说明数据没有被加载进去,问题一般出现在了dataloader。
网上有很多已有的办法,其实都没啥用,这些能用得到的就看看吧[1]
然后看到一个github链接[2],在里面试了各种办法,找到了一个办法可以解决我这种问题。

I recently come across a situation where I need to load many small images. My work station has a CPU with 22 cores and four GPUs, so I run four experiments with different random seeds, and each experiment uses one seperate GPU. I find out that the run time of four processes is amost four times the run time of a single process (No parallel benefit).

The model I train is relatively small and the most time-consuming part acutally comes from data loading. I have tried many different approachs including:

pin_memory =False/True
num_workers = 0/1/8
Increase ulimit
staggering the start of each experiment
Thanks to the system level diagnosis by @vjorlikowski, we find out that if we set num_workers = 0/1/8, each process will try to use all CPU cores and viciously compete with each other for CPU cores.

Solution:
Use export OMP_NUM_THREADS=N, as described here
or use torch.set_num_threads(N), as described here
We set num_workers = 0 and N=5 in our case, as we have 22 cores. The estimated run time of my program is reduced from 12 days to 1.5 days.

我是通过这句解决了锁死的问题:

torch.set_num_threads(N)

refrences

[1]https://3water.com/article/8MTM21NDY22Ljg4
[2]https://github.com/pytorch/pytorch/issues/1355

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
torch.nn.parallel.DataParallelPyTorch中的一个模块,用于在单个节点上进行多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、付费专栏及课程。

余额充值