From 7454ec0400429676edaa46578b024ff4bd4c028e Mon Sep 17 00:00:00 2001 From: Liu Yiqun Date: Wed, 11 Oct 2017 03:48:43 +0000 Subject: [PATCH] Simplify backward when inserting a sum operator to accumulate all duplicated variables. --- paddle/framework/backward.cc | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/paddle/framework/backward.cc b/paddle/framework/backward.cc index 0a4688db9c9..063b108500d 100644 --- a/paddle/framework/backward.cc +++ b/paddle/framework/backward.cc @@ -172,30 +172,14 @@ static std::unique_ptr BackwardRecursive( std::to_string(i)); net->ops_[op_offset]->Rename(name, dup_outputs.back()); } - // collect all the offset to append `add` op for each alias - // - // one variable is shared between multiple operators. - // insert add operator one by one, then add it to output - for (size_t output_idx = 0; output_idx < dup_outputs.size() - 1; - ++output_idx) { - auto insert_add_x = dup_outputs[output_idx]; - auto insert_add_y = dup_outputs[output_idx + 1]; - auto insert_add_out = name + "@SHARED@" + std::to_string(output_idx); - // first add op inserted - if (output_idx == dup_outputs.size() - 2) { - insert_add_out = name; - } - if (output_idx != 0) { - insert_add_y = name + "@SHARED@" + std::to_string(output_idx - 1); - } - insert_position.push_back( - {dup_op.back(), - OpRegistry::CreateOp("sum", {{"X", {insert_add_x, insert_add_y}}}, - {{"Out", {insert_add_out}}}, {})}); - } + // collect all the offset for each alias, + // insert a sum operator to add all aliases to output + insert_position.push_back( + {dup_op.back(), OpRegistry::CreateOp("sum", {{"X", dup_outputs}}, + {{"Out", {name}}}, {})}); } - // make sure the inserted `add` ops follow the BFS order. + // make sure the inserted `sum` ops follow the BFS order. insert_position.sort( [](const Pos& l, const Pos& r) { return l.first > r.first; }); -- GitLab