diff --git a/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.cc b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.cc index 2951294528..494ccc31f4 100644 --- a/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.cc +++ b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.cc @@ -18,8 +18,6 @@ #include #include -#include -#include #include "utils/anf_utils.h" #include "backend/common/session/anf_runtime_algorithm.h" #include "include/common/utils/anfalgo.h" @@ -28,27 +26,18 @@ namespace mindspore { namespace opt::dynamic_shape { -namespace { constexpr size_t kTupleFirstItemIndex = 0; constexpr size_t kFirstDataInputIndex = 1; -using DependPair = std::pair; -struct DependPairCmp { - bool operator()(const DependPair &lhs, const DependPair &rhs) const { - if (lhs.first != rhs.first) { - return lhs.first > rhs.first; - } - return lhs.second > rhs.second; - } -}; -void InsertDepend(const FuncGraphPtr &g, const AnfNodePtr &prev, const AnfNodePtr &next, AnfNodePtrList *depend_nodes) { + +void LinkCustomOp::InsertDepend(const FuncGraphPtr &g, const AnfNodePtr &prev, const AnfNodePtr &next, + AnfNodePtrList *depend_nodes) { MS_EXCEPTION_IF_NULL(g); MS_EXCEPTION_IF_NULL(prev); MS_EXCEPTION_IF_NULL(next); MS_EXCEPTION_IF_NULL(depend_nodes); - static std::set added_set = std::set(); DependPair cur_pair = std::make_pair(prev, next); - if (added_set.count(cur_pair) > 0) { + if (added_set_.count(cur_pair) > 0) { return; } @@ -57,10 +46,10 @@ void InsertDepend(const FuncGraphPtr &g, const AnfNodePtr &prev, const AnfNodePt std::vector{NewValueNode(std::make_shared(prim::kPrimDepend->name())), next, prev}); MS_EXCEPTION_IF_NULL(depend_node); depend_nodes->push_back(depend_node); - (void)added_set.insert(cur_pair); + (void)added_set_.insert(cur_pair); } -bool LinkInternalOp(const FuncGraphPtr &g, const AnfNodePtr &node, AnfNodePtrList *depend_nodes) { +bool LinkCustomOp::LinkInternalOp(const FuncGraphPtr &g, const AnfNodePtr &node, AnfNodePtrList *depend_nodes) { MS_EXCEPTION_IF_NULL(g); MS_EXCEPTION_IF_NULL(node); MS_EXCEPTION_IF_NULL(depend_nodes); @@ -82,7 +71,7 @@ bool LinkInternalOp(const FuncGraphPtr &g, const AnfNodePtr &node, AnfNodePtrLis return changed; } -bool LinkInputOp(const FuncGraphPtr &g, const CNodePtr &cnode, AnfNodePtrList *depend_nodes) { +bool LinkCustomOp::LinkInputOp(const FuncGraphPtr &g, const CNodePtr &cnode, AnfNodePtrList *depend_nodes) { MS_EXCEPTION_IF_NULL(g); MS_EXCEPTION_IF_NULL(cnode); MS_EXCEPTION_IF_NULL(depend_nodes); @@ -134,7 +123,7 @@ bool LinkInputOp(const FuncGraphPtr &g, const CNodePtr &cnode, AnfNodePtrList *d return changed; } -bool LinkDependSync(const FuncGraphPtr &g, const CNodePtr &cnode, AnfNodePtrList *depend_nodes) { +bool LinkCustomOp::LinkDependSync(const FuncGraphPtr &g, const CNodePtr &cnode, AnfNodePtrList *depend_nodes) { MS_EXCEPTION_IF_NULL(g); MS_EXCEPTION_IF_NULL(cnode); MS_EXCEPTION_IF_NULL(depend_nodes); @@ -193,7 +182,7 @@ bool LinkDependSync(const FuncGraphPtr &g, const CNodePtr &cnode, AnfNodePtrList * @param g Graph. * @param depend_nodes Custom's Depend nodes. */ -void AttachDependNodes(const FuncGraphPtr &g, const AnfNodePtrList &depend_nodes) { +void LinkCustomOp::AttachDependNodes(const FuncGraphPtr &g, const AnfNodePtrList &depend_nodes) { if (depend_nodes.empty()) { return; } @@ -215,13 +204,13 @@ void AttachDependNodes(const FuncGraphPtr &g, const AnfNodePtrList &depend_nodes // Attach back. return_node->set_input(kFirstDataInputIndex, get_1st_item); } -} // namespace bool LinkCustomOp::Run(const FuncGraphPtr &func_graph) { MS_EXCEPTION_IF_NULL(func_graph); bool changed = false; AnfNodePtrList depend_nodes; auto node_list = TopoSort(func_graph->get_return()); + added_set_.clear(); for (const auto &node : node_list) { CNodePtr cnode = node->cast(); if (cnode == nullptr || !CustomActorNodeManager::Instance().IsRegistered(cnode)) { diff --git a/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.h b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.h index 07240a6bc0..82c5824bff 100644 --- a/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.h +++ b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.h @@ -17,15 +17,37 @@ #ifndef MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_LINK_CUSTOM_OP_H #define MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_LINK_CUSTOM_OP_H +#include +#include #include "ir/anf.h" #include "backend/common/optimizer/optimizer.h" namespace mindspore::opt::dynamic_shape { +using DependPair = std::pair; +struct DependPairCmp { + bool operator()(const DependPair &lhs, const DependPair &rhs) const { + if (lhs.first != rhs.first) { + return lhs.first > rhs.first; + } + return lhs.second > rhs.second; + } +}; + class LinkCustomOp : public Pass { public: LinkCustomOp() : Pass("link_custom_op") {} ~LinkCustomOp() override = default; bool Run(const FuncGraphPtr &func_graph) override; + + private: + void InsertDepend(const FuncGraphPtr &g, const AnfNodePtr &prev, const AnfNodePtr &next, + AnfNodePtrList *depend_nodes); + bool LinkInternalOp(const FuncGraphPtr &g, const AnfNodePtr &node, AnfNodePtrList *depend_nodes); + bool LinkInputOp(const FuncGraphPtr &g, const CNodePtr &cnode, AnfNodePtrList *depend_nodes); + bool LinkDependSync(const FuncGraphPtr &g, const CNodePtr &cnode, AnfNodePtrList *depend_nodes); + void AttachDependNodes(const FuncGraphPtr &g, const AnfNodePtrList &depend_nodes); + + std::set added_set_; }; } // namespace mindspore::opt::dynamic_shape #endif // MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_LINK_CUSTOM_OP_H