MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / apply_action

Method apply_action

src/core/impl/graph/seq_sublinear_memory.cpp:975–1027  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

973}
974
975void SeqModifierForSublinearMemory::apply_action(
976 SeqModifyAction& action, const OprNodeArray& oprseq) {
977 auto cur_priority = std::numeric_limits<
978 decltype(OperatorNodeBase::NodeProp::Attribute::priority)>::min();
979
980 ThinHashSet<OperatorNodeBase*> modified_opr;
981
982 // each operator should be set no more than once
983 auto set_priority = [&](OperatorNodeBase* opr) {
984 mgb_assert(modified_opr.insert(opr).second);
985 mem_opt().set_priority(opr, cur_priority++);
986 };
987
988 auto on_opr_visited = [&](OperatorNodeBase* opr) {
989 if (replace_vars(opr->input())) {
990 auto&& repl_info = m_opr2replace_info[opr];
991 mgb_assert(
992 !repl_info.recomp, "input of operator %s{%s} already replaced",
993 opr->cname(), opr->dyn_typeinfo()->name);
994 opr = copy_opr_from_new_inputs(opr, true);
995 repl_info.recomp = opr;
996 }
997 set_priority(opr);
998 };
999
1000 // use a DepOprIter rather than directly iterate on oprseq because shape-dep
1001 // oprs would be omitted in the opr_seq generated by topo sorter; but they
1002 // should be replaced too
1003 DepOprIter dep_iter{on_opr_visited};
1004
1005 // setup m_var_map and priority
1006 for (auto opr : oprseq) {
1007 auto iter = action.find(opr);
1008
1009 if (iter != action.end()) {
1010 // insert duplicated oprs
1011 for (auto i : iter->second) {
1012 replace_vars(i->input());
1013 auto&& repl_info = m_opr2replace_info[i];
1014 mgb_assert(
1015 !repl_info.dup, "operator %s{%s} already duplicated",
1016 i->cname(), i->dyn_typeinfo()->name);
1017 auto opr_new = copy_opr_from_new_inputs(i, false);
1018 repl_info.dup = opr_new;
1019 set_priority(opr_new);
1020 }
1021 action.erase(iter);
1022 }
1023
1024 dep_iter.add(opr);
1025 }
1026 mgb_assert(action.empty());
1027}
1028
1029void SeqModifierForSublinearMemory::modify_endpoint_vars(VarNodeArray& endpoints) {
1030 auto comp_seq = MemoryOptimizerHelper::CompSeq(owner_graph(), endpoints);

Callers

nothing calls this directly

Calls 12

replace_varsFunction · 0.85
set_priorityFunction · 0.85
set_priorityMethod · 0.80
minFunction · 0.50
insertMethod · 0.45
inputMethod · 0.45
cnameMethod · 0.45
findMethod · 0.45
endMethod · 0.45
eraseMethod · 0.45
addMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected