From a2c4c86b1c139ca8242355d673b78e746d189f54 Mon Sep 17 00:00:00 2001 From: Ruibiao Chen Date: Thu, 14 Jul 2022 15:45:01 +0800 Subject: [PATCH] Fix var duplication bug for graph_to_program_pass (#44278) --- paddle/fluid/framework/ir/graph_helper.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/framework/ir/graph_helper.cc b/paddle/fluid/framework/ir/graph_helper.cc index 97f486065ac62..b0a2b6754cb2a 100644 --- a/paddle/fluid/framework/ir/graph_helper.cc +++ b/paddle/fluid/framework/ir/graph_helper.cc @@ -579,6 +579,12 @@ void GraphToProgram(const Graph &graph, VLOG(3) << "Graph to program need convert " << graph.SubGraphsSize() << " sub graph"; + + std::unordered_set vars_in_root_block; + for (const proto::VarDesc &var : block->vars()) { + vars_in_root_block.insert(var.name()); + } + for (size_t idx = 0; idx < graph.SubGraphsSize(); ++idx) { // avoid kRootBlockIndex not 0 if (idx == kRootBlockIndex) continue; @@ -586,7 +592,14 @@ void GraphToProgram(const Graph &graph, block = program_pb.add_blocks(); block->set_idx(idx); block->set_parent_idx(kRootBlockIndex); - GraphToBlock(*graph.GetSubGraph(idx), block, sort_kind); + + Graph *subgraph = graph.GetSubGraph(idx); + subgraph->SetNotOwned>( + kGraphToProgramVarsToRemove, &vars_in_root_block); + + GraphToBlock(*subgraph, block, sort_kind); + + subgraph->Erase(kGraphToProgramVarsToRemove); } } else { GraphToBlock(graph, block, sort_kind);