Skip to content

Commit f43b71b

Browse files
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.
1 parent 5b06944 commit f43b71b

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

python/paddle/fluid/framework.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,13 +1042,14 @@ def clone(self, for_test=False):
10421042
Returns(Program):
10431043
The cloned Program object.
10441044
"""
1045-
p = Program()
10461045
if for_test:
1047-
p.desc = core.inference_optimize(self.desc)
1046+
p = self.inference_optimize()
10481047
else:
1048+
p = Program()
10491049
p.desc = core.ProgramDesc(self.desc)
1050-
p.blocks = [Block(p, i) for i in xrange(self.desc.num_blocks())]
1051-
p.sync_with_cpp()
1050+
p.blocks = [Block(p, i) for i in xrange(self.desc.num_blocks())]
1051+
p.sync_with_cpp()
1052+
10521053
p.copy_param_info_from(self)
10531054
return p
10541055

@@ -1061,7 +1062,7 @@ def prune(self, targets):
10611062
if isinstance(t, Variable):
10621063
# After transpiler processing, the op that output this
10631064
# variable maybe has been changed, so t.op is not reliable
1064-
# and we need to find the current op that generate this
1065+
# and we need to find the current op that generate this
10651066
# variable here.
10661067
t.op = None
10671068
global_block = self.global_block()
@@ -1087,8 +1088,16 @@ def prune(self, targets):
10871088
return res
10881089

10891090
def inference_optimize(self):
1091+
# this is an alternative implement before
1092+
# core.inference_optimize being fixed.
10901093
res = Program()
1091-
res.desc = core.inference_optimize(self.desc)
1094+
res.desc = core.ProgramDesc(self.desc)
1095+
for i in xrange(res.desc.num_blocks()):
1096+
block = res.desc.block(i)
1097+
for j in xrange(block.op_size()):
1098+
op = block.op(j)
1099+
if op.has_attr('is_test'):
1100+
op.set_attr('is_test', True)
10921101
res.blocks = [Block(res, i) for i in xrange(res.desc.num_blocks())]
10931102
res.sync_with_cpp()
10941103
return res

0 commit comments

Comments
 (0)