[torch]memory leakage

来源:互联网 发布:pro tools 12.8.2 mac 编辑:程序博客网 时间:2024/06/05 23:50

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=5if (typee==1) thenlocal a= torch.Tensor(10000):zero()for i=1,1000000 do        --print(i)        a=torch.Tensor(10000):fill(0)        print(collectgarbage("count"))endelseif (typee==2) thenfor i=1,1000000 do        --print(i)        local a= torch.Tensor(10000):zero()        print(collectgarbage("count"))endelseif (typee==3) thenlocal a= torch.Tensor(10000):zero()for i=1,1000000 do        --print(i)        a=nil        a=torch.Tensor(10000):fill(0)        print(collectgarbage("count"))endelseif (typee==4) thenfor i=1,1000000 do        --print(i)        collectgarbage(); collectgarbage()        local a= torch.Tensor(10000):zero()        print(collectgarbage("count"))endelseif (typee==5) thenlocal a= torch.Tensor(10000):zero()for i=1,1000000 do        collectgarbage()        a=torch.Tensor(10000):fill(0)        print(collectgarbage("count"))endend

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

原创粉丝点击