[torch]memory leakage

https://github.com/torch/torch7/issues/229
https://groups.google.com/forum/#!topic/torch7/POColSrmlzk
https://discuss.pytorch.org/t/tracking-down-a-suspected-memory-leak/1130

在学校的超级计算机上训练模型,跑了2h15min左右之后说是memory超过了25g, 任务被终止了.
(我新写了个crossentropy的module. 跑hard target可以训练30个小时共100个epoch. 跑soft target就挂了..)

于是我在lua里写了print("memory:"..collectgarbage("count").."KB"), 重新跑程序.
明明只显示到memory:75136.659179688KB啊…. 为什么任务还是说超过了25g被终止了.

我尝试了

1.把for循环里的多次被重复赋值的local变量设为全局变量, 能比之前大概多跑一个epoch(之前大概跑到epoch 9就memory不够了).
2.每个batch跑完以后collectgarbage(), 仍旧跑到epoch 9就挂了, 时间还比之前多了快一个小时(用了3个小时).
3.每个epoch跑完以后collectgarbage(), 用了五个小时跑完10个epoch, 然后因为我只设定了五个小时所以任务停了.
4.每个batch跑完以后model:clearState(); collectgarbage(), 并把了一个local tensor 变量(5x2000x21)改写为(1x5)大小.

print("memory:"..collectgarbage("count").."KB")每个batch都打印结果:
4的基本在10M左右.
2的基本在40M左右.
1和3的从10M渐渐增加到80M.

看到有人说cudatensor没问题, doubletensor有memory leak.可是我的crossentropy是在lua上写的,没有cuda版…

接下来准备:
1.升级一下torch.
2.

require 'nn'
require 'rnn'
require 'os'
require 'cunn'
typee=5
if (typee==1) then
local a= torch.Tensor(10000):zero()
for i=1,1000000 do
        --print(i)
        a=torch.Tensor(10000):fill(0)
        print(collectgarbage("count"))
end

elseif (typee==2) then
for i=1,1000000 do
        --print(i)
        local a= torch.Tensor(10000):zero()
        print(collectgarbage("count"))
end


elseif (typee==3) then
local a= torch.Tensor(10000):zero()
for i=1,1000000 do
        --print(i)
        a=nil
        a=torch.Tensor(10000):fill(0)
        print(collectgarbage("count"))
end

elseif (typee==4) then
for i=1,1000000 do
        --print(i)
        collectgarbage(); collectgarbage()
        local a= torch.Tensor(10000):zero()
        print(collectgarbage("count"))
end


elseif (typee==5) then
local a= torch.Tensor(10000):zero()
for i=1,1000000 do
        collectgarbage()
        a=torch.Tensor(10000):fill(0)
        print(collectgarbage("count"))
end
end

最后的感觉是collectgarbage并没有什么用,
虽然用torch的collectgarbage("count")看起来好像是内存变小了,可是实际上内存还是没有得到释放.
我最后删除了一个中间变量(比较大),每次都在for循环里重新赋值.删掉它以后内存就好一些了.
可是!!!治标不治本啊!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值