Runtime Error CUDA error out of memory

Runtime Error CUDA error out of memory

问题描述

出错描述:在训练模型python train.py 的时候,出现 RuntimeErrorCUDA error out of memory 错误

出错原因:字面意思,out of memory

问题处理(三种常见的解决方式)

1、查看GPU的使用情况,切换(多GPU的情况)

在服务器上输入 :

nvidia-smi

查看GPU的使用情况:
在这里插入图片描述

可以看到第0个GPU 使用占了,可以用空闲的第1GPU来跑你的程序,修改CUDA_VISIBLE_DEVICES 的值,默认是 0

import os
os.environ["CUDA_VISIBLE_DEVICES"] = '1'

2、释放缓存

在报错的代码中加上异常捕捉的代码,清除空的缓存

try:
    trainer.train(30)
except RuntimeError as exception:
    if "out of memory" in str(exception):
        print("WARNING: out of memory")
        if hasattr(torch.cuda, 'empty_cache'):
            torch.cuda.empty_cache()
    else:
        raise exception

3、torch.no_grad()

对于tensor的计算操作,默认是要进行计算图的构建的,我们可以使用 with torch.no_grad():

让其不进行计算图构建,这样可以缓解GPU计算压力

with torch.no_grad():
    trainer.train(30)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值