1 /** 2 * Copyright 2022-2023 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_LINK_CUSTOM_OP_H 18 #define MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_LINK_CUSTOM_OP_H 19 20 #include <set> 21 #include <utility> 22 #include "ir/anf.h" 23 #include "include/backend/optimizer/optimizer.h" 24 25 namespace mindspore::opt::dynamic_shape { 26 using DependPair = std::pair<AnfNodePtr, AnfNodePtr>; 27 struct DependPairCmp { operatorDependPairCmp28 bool operator()(const DependPair &lhs, const DependPair &rhs) const { 29 if (lhs.first != rhs.first) { 30 return lhs.first > rhs.first; 31 } 32 return lhs.second > rhs.second; 33 } 34 }; 35 36 class LinkCustomOp : public Pass { 37 public: LinkCustomOp()38 LinkCustomOp() : Pass("link_custom_op") {} 39 ~LinkCustomOp() override = default; 40 bool Run(const FuncGraphPtr &func_graph) override; 41 42 private: 43 void InsertDepend(const FuncGraphPtr &g, const AnfNodePtr &prev, const AnfNodePtr &next, 44 AnfNodePtrList *depend_nodes); 45 bool LinkInternalOp(const FuncGraphPtr &g, const AnfNodePtr &node, AnfNodePtrList *depend_nodes); 46 bool LinkInputOp(const FuncGraphPtr &g, const CNodePtr &cnode, AnfNodePtrList *depend_nodes); 47 bool LinkDependSync(const FuncGraphPtr &g, const CNodePtr &cnode, AnfNodePtrList *depend_nodes); 48 void AttachDependNodes(const FuncGraphPtr &g, const AnfNodePtrList &depend_nodes) const; 49 50 std::set<DependPair, DependPairCmp> added_set_; 51 }; 52 } // namespace mindspore::opt::dynamic_shape 53 #endif // MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_LINK_CUSTOM_OP_H 54