首页 > 解决方案 > Google Colab 上的 GPU 内存不足错误消息

问题描述

我在 Google Colab 上使用 GPU 来运行一些深度学习代码。

我已经完成了 70% 的培训,但现在我不断收到以下错误:

RuntimeError: CUDA out of memory. Tried to allocate 2.56 GiB (GPU 0; 15.90 GiB total capacity; 10.38 GiB already allocated; 1.83 GiB free; 2.99 GiB cached)

我试图理解这意味着什么。它是在谈论RAM内存吗?如果是这样,代码应该和一直在做的一样运行,不是吗?当我尝试重新启动它时,内存消息立即出现。为什么我今天启动它时会使用比我昨天或前一天启动它时更多的 RAM?

或者这是关于硬盘空间的消息?我可以理解,因为代码会在执行过程中保存内容,因此硬盘使用量是累积的。

任何帮助将非常感激。


因此,如果只是 GPU 内存不足 - 有人可以解释为什么错误消息说10.38 GiB already allocated- 当我开始运行某些东西时,怎么可能已经分配了内存。会不会被别人使用?我只需要等待并稍后再试吗?

这是我运行代码时 GPU 使用情况的屏幕截图,就在它耗尽内存之前:

在此处输入图像描述


我发现这篇文章中人们似乎遇到了类似的问题。当我运行该线程上建议的代码时,我看到:

Gen RAM Free: 12.6 GB  | Proc size: 188.8 MB
GPU RAM Free: 16280MB | Used: 0MB | Util   0% | Total 16280MB

这似乎表明有 16 GB 的 RAM 可用。

我很困惑。

标签: memorygpugoogle-colaboratory

解决方案


您的 GPU 内存不足。如果您正在运行 python 代码,请尝试在您的代码之前运行此代码。它将显示您拥有的内存量。请注意,如果您尝试加载大于总内存的图像,它将失败。

# memory footprint support libraries/code
!ln -sf /opt/bin/nvidia-smi /usr/bin/nvidia-smi
!pip install gputil
!pip install psutil
!pip install humanize

import psutil
import humanize
import os
import GPUtil as GPU

GPUs = GPU.getGPUs()
# XXX: only one GPU on Colab and isn’t guaranteed
gpu = GPUs[0]
def printm():
    process = psutil.Process(os.getpid())
    print("Gen RAM Free: " + humanize.naturalsize(psutil.virtual_memory().available), " |     Proc size: " + humanize.naturalsize(process.memory_info().rss))
    print("GPU RAM Free: {0:.0f}MB | Used: {1:.0f}MB | Util {2:3.0f}% | Total     {3:.0f}MB".format(gpu.memoryFree, gpu.memoryUsed, gpu.memoryUtil*100, gpu.memoryTotal))
printm()

推荐阅读