Skip to content

Commit ec4c6e1

Browse files
authored
Merge pull request #12384 from JiayiFeng/dev_update_save_inference_model
update inference_optimize() to support program with readers
2 parents 4f10039 + 604bd85 commit ec4c6e1

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

paddle/fluid/framework/block_desc.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ class BlockDesc {
8888
OpDesc *InsertOp(size_t index);
8989

9090
/*
91-
* Remove Op and its input/output variables.
92-
* Note that for either input or output variable, if it is also an input or
93-
* output variable of other ops, we should remain it.
91+
* Only remove op itself,
92+
* do nothing to its input and output variables
9493
*/
9594
void RemoveOp(size_t s, size_t e);
9695

python/paddle/fluid/framework.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,12 @@ def prune(self, targets):
15401540

15411541
def inference_optimize(self):
15421542
"""
1543-
This method will create a new program and change the :code:`is_test`
1543+
This method will create a new program and do following adjustments on it:
1544+
1. Remove all reader variables and their creator ops if exist.
1545+
1546+
2. Remove the :code:`read_op` if exists.
1547+
1548+
3. change the :code:`is_test`
15441549
attribute of operators to :code:`True`. All the :code:`Parameter`
15451550
information will be lost.
15461551
@@ -1554,6 +1559,22 @@ def inference_optimize(self):
15541559
# core.inference_optimize being fixed.
15551560
res = Program()
15561561
res.desc = core.ProgramDesc(self.desc)
1562+
1563+
# remove all readers and the read_op if exist
1564+
read_op_idx = 0
1565+
root_block = res.desc.block(0)
1566+
while True:
1567+
if read_op_idx >= root_block.op_size() or root_block.op(
1568+
read_op_idx).type() == 'read':
1569+
break
1570+
read_op_idx += 1
1571+
if read_op_idx < root_block.op_size():
1572+
root_block._remove_op(0, read_op_idx + 1)
1573+
for var in root_block.all_vars():
1574+
if var.type() == core.VarDesc.VarType.READER:
1575+
root_block._remove_var(var.name())
1576+
1577+
# change all `is_test` attributes to True
15571578
for i in xrange(res.desc.num_blocks()):
15581579
block = res.desc.block(i)
15591580
for j in xrange(block.op_size()):

python/paddle/fluid/layers/io.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,6 @@ def random_data_generator(low, high, shapes, lod_levels, for_parallel=True):
443443
main_prog_var = _copy_reader_var_(default_main_program().current_block(),
444444
startup_var)
445445

446-
if for_parallel:
447-
main_prog_var = parallel(reader=main_prog_var)
448-
449446
return monkey_patch_reader_methods(main_prog_var)
450447

451448

0 commit comments

Comments
 (0)