解决方法:RuntimeError: CUDA out of memory. Tried to allocate ... MiB

解决方法:

1.直接上大显存显卡,当然能上就不在这了,哈哈,看下面的方法;

 2.调小batch_size,1都不行,再看看下面;

 3.在验证模型代码前面,插入with torch.no_grad():

因为在验证和测试的时候,是不需要计算梯度的。

 4.pin_memory 由true改成false,有时候很有效!亲测

pin_memory当为True,会加快训练速度;

pin_memory是锁页内存,创建DataLoader时,设置pin_memory=True,则意味着生成的Tensor数据最开始是属于内存中的锁页内存,这样将内存的Tensor转义到GPU的显存就会更快一些。

5.使用代码清理内存

import torch
torch.cuda.empty_cache()
!pip install GPUtil

import torch
from GPUtil import showUtilization as gpu_usage
from numba import cuda

def free_gpu_cache():
    print("Initial GPU Usage")
    gpu_usage()                             

    torch.cuda.empty_cache()

    cuda.select_device(0)
    cuda.close()
    cuda.select_device(0)

    print("GPU Usage after emptying the cache")
    gpu_usage()

free_gpu_cache()  

参考: 

 Solving "CUDA out of memory" Error | Data Science and Machine Learning | Kaggle

pytorch: 四种方法解决RuntimeError: CUDA out of memory. Tried to allocate ... MiB_xiyou__的博客-CSDN博客

  • 6
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是由于CUDA内存不足导致的。根据引用\[1\]和引用\[2\]的信息,你的GPU总容量为4.00 GiB或10.76 GiB,但已经分配了2.34 GiB或1.82 GiB的内存,剩余的内存不足以分配14.00 MiB的内存。这可能是由于你的模型或数据的规模过大,导致内存不足。你可以尝试减小batch size或者使用更小的模型来减少内存的使用。另外,你还可以尝试设置max_split_size_mb参数来避免内存碎片化。关于内存管理和PYTORCH_CUDA_ALLOC_CONF的更多信息,请参考PyTorch的文档。 此外,根据引用\[3\]的信息,你还可以通过手动杀死占用GPU内存的进程来释放内存。你可以使用kill命令加上进程的PID来终止该进程,例如kill -9 31272。 综上所述,你可以通过减小batch size、使用更小的模型、设置max_split_size_mb参数或手动杀死占用内存的进程来解决CUDA内存不足的问题。 #### 引用[.reference_title] - *1* [已解决yolov5报错RuntimeError: CUDA out of memory. Tried to allocate 14.00 MiB](https://blog.csdn.net/Code_and516/article/details/129798540)[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^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [解决RuntimeError: CUDA out of memory. Tried to allocate 14.00 MiB](https://blog.csdn.net/qq_43733107/article/details/126876755)[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^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值