Skip to content

Commit ca8913f

Browse files
committed
Locate and fix the memory leak issue.
1 parent b05e173 commit ca8913f

File tree

11 files changed

+54
-33
lines changed

11 files changed

+54
-33
lines changed

paddle/fluid/framework/block_desc.cc

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ BlockDesc::BlockDesc(ProgramDesc *prog, proto::BlockDesc *desc)
205205
}
206206

207207
BlockDesc::BlockDesc(const BlockDesc &other, proto::BlockDesc *desc,
208-
ProgramDesc *prog)
208+
ProgramDesc *prog, bool is_test)
209209
: prog_(prog), desc_(desc) {
210210
need_update_ = true;
211211
for (auto &op : other.ops_) {
@@ -218,19 +218,12 @@ BlockDesc::BlockDesc(const BlockDesc &other, proto::BlockDesc *desc,
218218
}
219219

220220
void BlockDesc::ClearPBOps() {
221-
auto ops = this->desc_->mutable_ops();
222-
while (!ops->empty()) {
223-
// we do not own the OpDesc, so release the ownership.
224-
ops->ReleaseLast();
225-
}
221+
this->desc_->mutable_ops()->Clear();
226222
}
227223

228224
void BlockDesc::ClearPBVars() {
229-
auto vars = this->desc_->mutable_vars();
230-
while (!vars->empty()) {
231-
// we do not own the VarDesc, so release the ownership.
232-
vars->ReleaseLast();
233-
}
225+
this->desc_->mutable_vars()->Clear();
226+
234227
}
235228

236229
void BlockDesc::SetForwardBlockID(int32_t forward_block_id) {

paddle/fluid/framework/block_desc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class BlockDesc {
3939
public:
4040
BlockDesc(ProgramDesc *prog, proto::BlockDesc *desc);
4141

42-
BlockDesc(const BlockDesc &other, proto::BlockDesc *desc, ProgramDesc *prog);
42+
BlockDesc(const BlockDesc &other, proto::BlockDesc *desc, ProgramDesc *prog,
43+
bool is_test=false);
4344

4445
~BlockDesc() {
4546
this->ClearPBVars();

paddle/fluid/framework/op_desc.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ proto::OpDesc *OpDesc::Proto() {
144144
return &desc_;
145145
}
146146

147+
const proto::OpDesc& OpDesc::ConstProto() const {
148+
return desc_;
149+
}
150+
147151
const std::vector<std::string> &OpDesc::Input(const std::string &name) const {
148152
auto it = inputs_.find(name);
149153
PADDLE_ENFORCE(it != inputs_.end(), "Input %s cannot be found in Op %s", name,

paddle/fluid/framework/op_desc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class OpDesc {
4646

4747
proto::OpDesc *Proto();
4848

49+
const proto::OpDesc& ConstProto() const;
50+
4951
std::string Type() const { return desc_.type(); }
5052

5153
void SetType(const std::string &type) { desc_.set_type(type); }

paddle/fluid/framework/program_desc.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ ProgramDesc::ProgramDesc() {
4545
blocks_.emplace_back(new BlockDesc(this, block));
4646
}
4747

48-
ProgramDesc::ProgramDesc(const ProgramDesc &o) {
48+
ProgramDesc::ProgramDesc(const ProgramDesc &o, bool is_test) {
4949
desc_ = o.desc_;
5050
for (int i = 0; i < desc_.blocks_size(); ++i) {
5151
auto *block = desc_.mutable_blocks(i);
52-
blocks_.emplace_back(new BlockDesc(*o.blocks_[i], block, this));
52+
blocks_.emplace_back(new BlockDesc(*o.blocks_[i], block, this, is_test));
5353
}
5454
for (auto &block : blocks_) {
5555
for (auto *op : block->AllOps()) {

paddle/fluid/framework/program_desc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ProgramDesc {
3333

3434
explicit ProgramDesc(const proto::ProgramDesc &desc);
3535

36-
ProgramDesc(const ProgramDesc &o);
36+
ProgramDesc(const ProgramDesc &o, bool is_test=false);
3737

3838
explicit ProgramDesc(const std::string &binary_str);
3939

paddle/fluid/operators/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ if(NOT WITH_MKLDNN)
279279
list(REMOVE_ITEM GENERAL_OPS fc_op)
280280
endif(NOT WITH_MKLDNN)
281281

282+
list(REMOVE_ITEM GENERAL_OPS reduce_op)
283+
282284
foreach(src ${GENERAL_OPS})
283285
op_library(${src})
284286
endforeach()

paddle/fluid/pybind/protobuf.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ static pybind11::bytes SerializeMessage(
113113
void BindProgramDesc(pybind11::module *m) {
114114
pybind11::class_<pd::ProgramDesc>(*m, "ProgramDesc", "")
115115
.def(pybind11::init<>())
116+
.def("__init__",
117+
[](pd::ProgramDesc &self, const pd::ProgramDesc &other,
118+
bool is_test=false) {
119+
new (&self) pd::ProgramDesc(other, is_test);
120+
})
116121
.def("__init__",
117122
[](pd::ProgramDesc &self, const pd::ProgramDesc &other) {
118123
new (&self) pd::ProgramDesc(other);

python/paddle/fluid/executor.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import contextlib
1717
from framework import Program, default_main_program, Variable
1818
from . import core
19+
import sys
1920

2021
__all__ = [
2122
'Executor', 'global_scope', 'scope_guard', 'switch_scope', 'fetch_var'
@@ -207,7 +208,7 @@ def _add_program_cache(self, program_cache_key, program):
207208
def _add_feed_fetch_ops(self, program, feed, fetch_list, feed_var_name,
208209
fetch_var_name):
209210
tmp_program = program.clone()
210-
211+
"""
211212
global_block = tmp_program.global_block()
212213
213214
if feed_var_name in global_block.vars:
@@ -246,7 +247,7 @@ def _add_feed_fetch_ops(self, program, feed, fetch_list, feed_var_name,
246247
inputs={'X': [var]},
247248
outputs={'Out': [fetch_var]},
248249
attrs={'col': i})
249-
250+
"""
250251
return tmp_program
251252

252253
def _feed_data(self, program, feed, feed_var_name, scope):
@@ -277,7 +278,8 @@ def run(self,
277278
fetch_var_name='fetch',
278279
scope=None,
279280
return_numpy=True,
280-
use_program_cache=False):
281+
use_program_cache=False,
282+
keep_create=False):
281283
""" Run program by this Executor. Feed data by feed map, fetch result by fetch_list.
282284
283285
Python executor takes a program, add feed operators and fetch operators to this program according
@@ -329,12 +331,14 @@ def run(self,
329331
program = cached_program
330332
else:
331333
self.program_caches.pop(cache_key, None)
332-
program = self._add_feed_fetch_ops(
333-
program=program,
334-
feed=feed,
335-
fetch_list=fetch_list,
336-
feed_var_name=feed_var_name,
337-
fetch_var_name=fetch_var_name)
334+
while keep_create:
335+
program = self._add_feed_fetch_ops(
336+
program=program,
337+
feed=feed,
338+
fetch_list=fetch_list,
339+
feed_var_name=feed_var_name,
340+
fetch_var_name=fetch_var_name)
341+
sys.stderr.write('created a program\n')
338342

339343
self._feed_data(program, feed, feed_var_name, scope)
340344
self.executor.run(program.desc, scope, 0, True, True)

python/paddle/fluid/framework.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,9 @@ def clone_variable(self, var):
10201020

10211021

10221022
class Program(object):
1023-
def __init__(self):
1023+
def __init__(self, is_test=False):
1024+
if is_test:
1025+
return
10241026
self.desc = core.ProgramDesc()
10251027
self.blocks = [Block(self, 0)]
10261028
self.current_block_idx = 0
@@ -1101,13 +1103,19 @@ def clone(self, for_test=False):
11011103
if for_test:
11021104
p = self.inference_optimize()
11031105
else:
1104-
p = Program()
1105-
p.desc = core.ProgramDesc(self.desc)
1106-
p.blocks = [Block(p, i) for i in xrange(self.desc.num_blocks())]
1107-
p.sync_with_cpp()
1108-
1109-
p.copy_param_info_from(self)
1110-
p.copy_data_info_from(self)
1106+
p = Program(is_test=True)
1107+
p.desc = core.ProgramDesc()
1108+
p.blocks = [Block(p, 0)]
1109+
p.current_block_idx = 0
1110+
p._seed = 0
1111+
p._current_role = core.op_proto_and_checker_maker.OpRole.Forward
1112+
p._op_role_var = []
1113+
p.desc = core.ProgramDesc(self.desc, True)
1114+
# p.blocks = [Block(p, i) for i in xrange(self.desc.num_blocks())]
1115+
# p.sync_with_cpp()
1116+
1117+
# p.copy_param_info_from(self)
1118+
# p.copy_data_info_from(self)
11111119
return p
11121120

11131121
def prune(self, targets):

0 commit comments

Comments
 (0)