未验证 提交 f43b71b2 编写于 作者: W whs 提交者: GitHub

Fix clone function of Program to avoid memory leak. (#10358)

* Fix clone function of Program to avoid memory leak.

* Fix inference_optimize function of framework.py.

* Reuse inference_optimize in framework.py.

* Add comments.
上级 5b069448
...@@ -1042,13 +1042,14 @@ class Program(object): ...@@ -1042,13 +1042,14 @@ class Program(object):
Returns(Program): Returns(Program):
The cloned Program object. The cloned Program object.
""" """
p = Program()
if for_test: if for_test:
p.desc = core.inference_optimize(self.desc) p = self.inference_optimize()
else: else:
p = Program()
p.desc = core.ProgramDesc(self.desc) p.desc = core.ProgramDesc(self.desc)
p.blocks = [Block(p, i) for i in xrange(self.desc.num_blocks())] p.blocks = [Block(p, i) for i in xrange(self.desc.num_blocks())]
p.sync_with_cpp() p.sync_with_cpp()
p.copy_param_info_from(self) p.copy_param_info_from(self)
return p return p
...@@ -1087,8 +1088,16 @@ class Program(object): ...@@ -1087,8 +1088,16 @@ class Program(object):
return res return res
def inference_optimize(self): def inference_optimize(self):
# this is an alternative implement before
# core.inference_optimize being fixed.
res = Program() res = Program()
res.desc = core.inference_optimize(self.desc) res.desc = core.ProgramDesc(self.desc)
for i in xrange(res.desc.num_blocks()):
block = res.desc.block(i)
for j in xrange(block.op_size()):
op = block.op(j)
if op.has_attr('is_test'):
op.set_attr('is_test', True)
res.blocks = [Block(res, i) for i in xrange(res.desc.num_blocks())] res.blocks = [Block(res, i) for i in xrange(res.desc.num_blocks())]
res.sync_with_cpp() res.sync_with_cpp()
return res return res
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册