From f43b71b242467d665c134262c2b7167cef622757 Mon Sep 17 00:00:00 2001 From: whs Date: Mon, 7 May 2018 19:20:38 +0800 Subject: [PATCH] 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. --- python/paddle/fluid/framework.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index ce9b880aeb..d7eda619c3 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -1042,13 +1042,14 @@ class Program(object): Returns(Program): The cloned Program object. """ - p = Program() if for_test: - p.desc = core.inference_optimize(self.desc) + p = self.inference_optimize() else: + p = Program() p.desc = core.ProgramDesc(self.desc) - p.blocks = [Block(p, i) for i in xrange(self.desc.num_blocks())] - p.sync_with_cpp() + p.blocks = [Block(p, i) for i in xrange(self.desc.num_blocks())] + p.sync_with_cpp() + p.copy_param_info_from(self) return p @@ -1061,7 +1062,7 @@ class Program(object): if isinstance(t, Variable): # After transpiler processing, the op that output this # variable maybe has been changed, so t.op is not reliable - # and we need to find the current op that generate this + # and we need to find the current op that generate this # variable here. t.op = None global_block = self.global_block() @@ -1087,8 +1088,16 @@ class Program(object): return res def inference_optimize(self): + # this is an alternative implement before + # core.inference_optimize being fixed. 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.sync_with_cpp() return res -- GitLab