Pytorch Pytork推断导致cpu内存泄漏
我使用pytorch模型推断,我得到了内存泄漏问题,我的代码如下:Pytorch Pytork推断导致cpu内存泄漏,pytorch,Pytorch,我使用pytorch模型推断,我得到了内存泄漏问题,我的代码如下: import torch import torch.nn as nn from memory_profiler import profile @profile(func=None, stream=open('./mem.log', 'w')) def run(net, x): with x: net(x) class ConvVerify(nn.Module): def init(se
import torch
import torch.nn as nn
from memory_profiler import profile
@profile(func=None, stream=open('./mem.log', 'w'))
def run(net, x):
with x:
net(x)
class ConvVerify(nn.Module):
def init(self):
for name, m in self.named_modules():
# print(name, m)
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.running_mean, 0)
nn.init.constant_(m.running_var, 1)
if m.weight is not None:
nn.init.constant_(m.weight, 1)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def __init__(self):
super(ConvVerify, self).__init__()
stride = 1
self.model = nn.Sequential(
nn.Conv2d(288, 64, (2, 2), stride),
nn.BatchNorm2d(64),
nn.ReLU(),
)
self.init()
def forward(self, x):
return self.model(x)
def begin():
conv_verify_inst = ConvVerify()
x = torch.autograd.Variable(torch.rand(1, 288, 7, 7))
for i in range(3000):
run(conv_verify_inst, x)
if __name__ == "__main__":
begin()
当运行某个迭代器时,内存将增加0.1M或0.2M。例如,基本内存是71.8mib,最后,内存增加到72mib我投票结束这个问题,因为它应该是pytorch的GitHub上的错误报告,提供的代码在执行时会产生错误;你能检查一下你是否错过了什么吗。?
run
函数中带有x的行似乎导致了错误,我也将在PyHub上报告此问题@Shai@Alka,您是对的,代码有错误。应该用x:net(x)
而不是“net(x)”退出