提交 1cafe7bf 编写于 作者: T Tao Luo 提交者: GitHub

Merge pull request #4703 from Xreki/core_optimize_backward

Simplify backward when inserting a sum operator to accumulate all duplicated variables
...@@ -172,30 +172,14 @@ static std::unique_ptr<OperatorBase> BackwardRecursive( ...@@ -172,30 +172,14 @@ static std::unique_ptr<OperatorBase> BackwardRecursive(
std::to_string(i)); std::to_string(i));
net->ops_[op_offset]->Rename(name, dup_outputs.back()); net->ops_[op_offset]->Rename(name, dup_outputs.back());
} }
// collect all the offset to append `add` op for each alias // collect all the offset for each alias,
// // insert a sum operator to add all aliases to output
// one variable is shared between multiple operators. insert_position.push_back(
// insert add operator one by one, then add it to output {dup_op.back(), OpRegistry::CreateOp("sum", {{"X", dup_outputs}},
for (size_t output_idx = 0; output_idx < dup_outputs.size() - 1; {{"Out", {name}}}, {})});
++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}}}, {})});
}
} }
// make sure the inserted `add` ops follow the BFS order. // make sure the inserted `sum` ops follow the BFS order.
insert_position.sort( insert_position.sort(
[](const Pos& l, const Pos& r) { return l.first > r.first; }); [](const Pos& l, const Pos& r) { return l.first > r.first; });
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册