Pytorch 为什么不使用torch.CUDA.empty_cache()释放CUDA内存

Pytorch 为什么不使用torch.CUDA.empty_cache()释放CUDA内存,pytorch,Pytorch,在我的Windows10上,如果我直接创建一个GPU张量,我可以成功地释放它的内存 import torch a = torch.zeros(300000000, dtype=torch.int8, device='cuda') del a torch.cuda.empty_cache() import torch a = torch.zeros(300000000, dtype=torch.int8) a.cuda() del a torch.cuda.empty_cache() 但如果我

在我的Windows10上,如果我直接创建一个GPU张量,我可以成功地释放它的内存

import torch
a = torch.zeros(300000000, dtype=torch.int8, device='cuda')
del a
torch.cuda.empty_cache()
import torch
a = torch.zeros(300000000, dtype=torch.int8)
a.cuda()
del a
torch.cuda.empty_cache()
但如果我创建一个法线张量并将其转换为GPU张量,我就不能再释放它的内存了

import torch
a = torch.zeros(300000000, dtype=torch.int8, device='cuda')
del a
torch.cuda.empty_cache()
import torch
a = torch.zeros(300000000, dtype=torch.int8)
a.cuda()
del a
torch.cuda.empty_cache()

为什么会发生这种情况。

至少在Ubuntu中,您的脚本在交互式shell中运行时不会释放内存,而在作为脚本运行时会按预期工作。我认为现场电话中存在一些参考问题。以下内容将在交互式shell和脚本中使用

导入火炬
a=火炬.0(300000000,dtype=火炬.int8)
a=a.cuda()
德拉
torch.cuda.empty_cache()

是的,这也发生在具有以下配置的我的电脑上:

  • 20.04.1-Ubuntu
  • 1.7.1+cu110
根据fastai讨论的信息:

这与ipython环境中的python垃圾收集器有关

def pretty_大小(大小):
“”“Pretty打印火炬。大小对象”“”
断言(isinstance(大小、火炬大小))
返回“×”.join(映射(str,size))
def dump_张量(仅gpu_=真):
“”“打印垃圾收集器正在跟踪的张量列表。”“”
导入gc
总尺寸=0
对于gc.get_objects()中的obj:
尝试:
如果火炬是张量(obj):
如果不是仅gpu或obj.is\U cuda:
打印(“%s:%s%s%s%”(类型(obj)。\u\u名称\u,
“GPU”如果对象是“其他”,
“钉住”如果对象被钉住,则为“其他”,
漂亮的尺寸(obj.size())
总大小+=obj.numel()
elif hasattr(对象,“数据”)和torch.is_张量(对象数据):
如果不是仅gpu或obj.is\U cuda:
打印(“%s”)→ %s:%s%s%s%s%s“%(类型(obj)。\u名称\u,
类型(对象数据)。\uuuuu名称\uuuuuuuu,
“GPU”如果对象是“其他”,
如果obj.data.is_pinted else“”,则为“pinted”,
如果obj.REQUIRED_grad else”“,则为“grad”,
“volatile”如果为obj.volatile,则为“else”,
漂亮的大小(obj.data.size())
总大小+=obj.data.numel()
例外情况除外,如e:
通过
打印(“总尺寸:”,总尺寸)
如果我做了类似的事情

将火炬导入为th
a=th.randn(1010001000)
aa=a.cuda()
德拉
th.cuda.empty_cache()
您不会看到nvidia smi/nvtop的任何减少。 但您可以使用handy函数了解发生了什么

dump_张量()
您可以观察以下信息:

张量:GPU固定10×1000×1000
总规模:10000000
这意味着您的gc仍然持有资源

我们可以参考更多关于python gc机制的讨论