【问题标题】:How to deal with GPU memory leakage issues in Torch?如何处理 Torch 中的 GPU 内存泄漏问题?
【发布时间】:2023-04-06 13:55:02
【问题描述】:

我机器的 GPU 有 2 GB 的内存。当我第一次运行以下代码时,我没有收到任何错误。但是,第二次运行代码时出现内存错误。作为一种短期补救措施,我唯一能做的就是使用torch.Tensor.float() 将数据转换为float32。但是问题依旧存在,进程完成后没有释放占用的内存,或者进程在运行时被终止。机器 RAM 也是如此。 Torch应该如何防止内存泄漏或释放内存?

require 'nn'
require 'image'
require 'cunn'
require 'paths'



collectgarbage(); collectgarbage()
if (not paths.filep("cifar10torchsmall.zip")) then
    os.execute('wget -c https://s3.amazonaws.com/torch7/data/cifar10torchsmall.zip')
    os.execute('unzip cifar10torchsmall.zip')
end
trainset = torch.load('cifar10-train.t7')
testset = torch.load('cifar10-test.t7')
classes = {'airplane', 'automobile', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck'}

setmetatable(trainset, 
    {__index = function(t, i) 
                    return {t.data[i], t.label[i]} 
                end}
);
trainset.data = trainset.data:double() -- convert the data from a ByteTensor to a DoubleTensor.

function trainset:size() 
    return self.data:size(1) 
end

mean = {} -- store the mean, to normalize the test set in the future
stdv  = {} -- store the standard-deviation for the future
for i=1,3 do -- over each image channel
    mean[i] = trainset.data[{ {}, {i}, {}, {}  }]:mean() -- mean estimation
    print('Channel ' .. i .. ', Mean: ' .. mean[i])
    trainset.data[{ {}, {i}, {}, {}  }]:add(-mean[i]) -- mean subtraction

    stdv[i] = trainset.data[{ {}, {i}, {}, {}  }]:std() -- std estimation
    print('Channel ' .. i .. ', Standard Deviation: ' .. stdv[i])
    trainset.data[{ {}, {i}, {}, {}  }]:div(stdv[i]) -- std scaling
end


testset.data = testset.data:double()   -- convert from Byte tensor to Double tensor
for i=1,3 do -- over each image channel
    testset.data[{ {}, {i}, {}, {}  }]:add(-mean[i]) -- mean subtraction    
    testset.data[{ {}, {i}, {}, {}  }]:div(stdv[i]) -- std scaling
end

trainset.data = trainset.data:cuda()
testset.data = testset.data:cuda()

net = nn.Sequential()
net:add(nn.SpatialConvolution(3, 6, 5, 5)) -- 3 input image channels, 6 output channels, 5x5 convolution kernel
net:add(nn.ReLU())                       -- non-linearity 
net:add(nn.SpatialMaxPooling(2,2,2,2))     -- A max-pooling operation that looks at 2x2 windows and finds the max.
net:add(nn.SpatialConvolution(6, 16, 5, 5))
net:add(nn.ReLU())                       -- non-linearity 
net:add(nn.SpatialMaxPooling(2,2,2,2))
net:add(nn.View(16*5*5))                    -- reshapes from a 3D tensor of 16x5x5 into 1D tensor of 16*5*5
net:add(nn.Linear(16*5*5, 120))             -- fully connected layer (matrix multiplication between input and weights)
net:add(nn.ReLU())                       -- non-linearity 
net:add(nn.Linear(120, 84))
net:add(nn.ReLU())                       -- non-linearity 
net:add(nn.Linear(84, 10))                   -- 10 is the number of outputs of the network (in this case, 10 digits)
net:add(nn.LogSoftMax())  
net = net:cuda()

criterion = nn.ClassNLLCriterion()
criterion = criterion:cuda()



pred = net:forward(trainset.data)
outputEr = criterion:forward(pred, trainset.label:cuda())
net:zeroGradParameters()
outputGrad = criterion:backward(pred, trainset.label:cuda())
collectgarbage()
inputGrad = net:backward(trainset.data, outputGrad)

附带问题:为什么 Torch 将网络参数初始化为 double,尽管 GPU 在计算双精度运算方面非常慢,而且实际上几乎所有神经网络应用程序都不需要 64 位参数值?如何使用浮点(32 位)参数向量初始化模型?

我找到了附带问题的答案。您可以在代码开头使用以下内容轻松地将 Torch 的默认数据类型设置为浮点数:

torch.setdefaulttensortype('torch.FloatTensor')

【问题讨论】:

    标签: memory-management lua machine-learning gpgpu torch


    【解决方案1】:

    我可以通过在我进行上述实验的机器上从 CUDA 6.5 升级到 CUDA 7.5 来解决这个问题(几乎)。现在,在大多数情况下,当程序在运行时崩溃时,GPU 内存会被释放。但是,有时它仍然没有发生,我必须重新启动机器。

    另外,为了确保程序在程序成功运行时清除 GPU 内存,我会执行以下操作:

    net = nil
    trainset = nil
    testset = nil
    pred = nil
    inputGrad = nil
    criterion = nil
    
    collectgarbage()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-19
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 2011-10-08
      • 2021-04-26
      相关资源
      最近更新 更多