多个gpu训练方法以及为何运行程序只gpu0运行(CUDA out of memory. Tried to allocate) 您所在的位置:网站首页 电脑有两个gpu但独立显卡不工作怎么回事 多个gpu训练方法以及为何运行程序只gpu0运行(CUDA out of memory. Tried to allocate)

多个gpu训练方法以及为何运行程序只gpu0运行(CUDA out of memory. Tried to allocate)

2024-07-16 07:26| 来源: 网络整理| 查看: 265

一,多个gpu训练方法

如果gpu还不错,不要改batchsize 一,这种方法偶尔有用,但是如果在别的地方使用了别的方法指定gpu,还是会只使用gpu0

import os os.environ['CUDA_VISIBLE_DEVICES'] = '1,2'

二,这种效果最弱,最常用,方便

CUDA_VISIBLE_DEVICES=1,2 python train.py

三,最好用的、绝对不会出错的device,缺点是只能指定一个,或者说用这种方法我不会指定多个gpu 在这里插入图片描述 为什么各种方式都不行,一种可能的原因是这种模型参数过大,可以用以下函数输出模型参数个数。

def print_model(model, logger): print(model) nParams = 0 for w in model.parameters(): nParams += functools.reduce(operator.mul, w.size(), 1) if logger: logger.write('nParams=\t'+str(nParams)) 二,为什么设置了在多个gpu运行,但是只有gpu0有进程呢。

一,如何查看在哪个gpu运行。 当我print(torch.cuda.device_count())输出结果甚至是8(个gpu),但是仍然报错,最靠谱的还是看进程 在你的文件路径下,命令行输入 nvidia-smi 在这里插入图片描述 由上图可以看见python在gpu1和gpu7执行任务,其中PID是进程号。顺便提一句shasi进程的命令 kill 进程号 二,只在gpu0执行任务怎么办? 上面介绍的方法中,即使指定了多个gpu运行,实际上却仍然只在gpu0上面运行,就会报错(内存输出错误)cuda out of memory, 还有RuntimeError: CUDA out of memory. Tried to allocate 158.00 MiB (GPU 0; 11.17 GiB total capacity; 556.72 MiB already allocated; 26.06 MiB free; 17.28 MiB cached)

只用方法三可以,但是方法三只能指定在一个gpu上训练,当然其中我也试过用多个gpu训练

net = torch.nn.DataParallel(model, device_ids=[0, 1, 2])

但是会报错似乎是在多个gpu梯度无法求导的错误,请指教。 参考连接



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有