【问题标题】:Torch: Unable to free gpu memory after moving tensors to cpuTorch:将张量移动到 cpu 后无法释放 gpu 内存
【发布时间】:2018-09-04 03:56:55
【问题描述】:

遵循 jcjohnson 提出的一个想法,为了在受限的内存环境中生成神经风格的大图像,我试图在 updateOutput 和 updateGradInput 中将层激活从 gpu 来回移动到 cpu。例如

 if (torch.type(previousModule) == 'cudnn.ReLU' or torch.type(previousModule) == 'cudnn.SpatialConvolution') then
    previousModule.output = previousModule.output:cuda()
    collectgarbage('collect')
 end
....
 if (torch.type(currentModule) == 'cudnn.ReLU' or torch.type(currentModule) == 'cudnn.SpatialConvolution') then
    currentModule.output = currentModule.output:float()
    collectgarbage('collect')
 end

从功能上讲,它适用于较小的图像,生成图像,尽管速度很慢。然而,即使在将输出张量从 cuda 转换为浮点数之后,尽管显式调用了 collectgarbage() 例程,它仍然不会释放 gpu 中的内存,并且我无法生成比最初受 gpu 内存限制的图像更大的图像。

有什么想法我在这里出错了吗?

【问题讨论】:

    标签: lua garbage-collection torch


    【解决方案1】:

    我不确定它应该以这种方式工作。网络在运行时应保持不变。

    cudnnthis 函数表示它执行转换,当模块从 gpu 移动时内存会发生什么没有明确的答案。 (CPU 运行函数可以处理 GPU 存储的数据似乎很奇怪)

    标准cunn 模块根本没有网络的反向转换。

    您的代码执行以下操作:

    if currentModule is cuda, then convert its output to float.
    previousModule = currentModule
    if previousModule is cuda then convert its output to cuda
    

    最后,如果你的 gpu 代码很慢,而且你不断地来回移动数据,你确定只使用 cpu 不会更容易吗?

    【讨论】:

      猜你喜欢
      • 2016-10-18
      • 2020-03-14
      • 2018-09-07
      • 2016-07-09
      • 2020-07-20
      • 1970-01-01
      • 2020-11-10
      • 2021-07-30
      • 2016-01-01
      相关资源
      最近更新 更多