1 /** 2 * Copyright 2019 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 #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_NODE_PASS_H_ 17 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_NODE_PASS_H_ 18 #include <string> 19 #include <memory> 20 #include <vector> 21 #include <set> 22 23 #include "include/backend/optimizer/pass.h" 24 #include "include/backend/visible.h" 25 26 namespace mindspore { 27 namespace opt { 28 // @brief ANF Node level optimization base pass 29 class BACKEND_EXPORT NodePass : public Pass { 30 public: NodePass(const std::string & name)31 explicit NodePass(const std::string &name) : Pass(name) {} 32 ~NodePass() override = default; 33 bool Run(const FuncGraphPtr &func_graph) override; IsFastPass()34 virtual bool IsFastPass() { return false; } AfterProcess(const AnfNodePtr &,const AnfNodePtr &,const FuncGraphPtr &,const FuncGraphIndexPtr &)35 virtual void AfterProcess(const AnfNodePtr &, const AnfNodePtr &, const FuncGraphPtr &, const FuncGraphIndexPtr &) {} GetPatternRootPrimitiveName()36 virtual std::string GetPatternRootPrimitiveName() { return ""; } 37 virtual AnfNodePtr Run(const FuncGraphPtr &func_graph, const AnfNodePtr &node) = 0; MustExistPrimitiveName()38 virtual std::vector<std::string> MustExistPrimitiveName() const { return {}; } 39 40 protected: 41 bool is_add_ = true; 42 43 private: 44 bool ProcessFastPassNode(const AnfNodePtr &node, const FuncGraphPtr &func_graph, 45 const FuncGraphIndexPtr &func_graph_index, const FuncGraphManagerPtr &manager); 46 bool ProcessFastPass(const FuncGraphPtr &func_graph, const FuncGraphIndexPtr &func_graph_index); 47 bool ProcessPass(const FuncGraphPtr &func_graph, const FuncGraphManagerPtr &manager); 48 }; 49 void GenIndex(const FuncGraphPtr &func_graph, const FuncGraphIndexPtr &func_graph_index); 50 void ModifyOutputAndCallerToMap(const CNodePtr &cnode, const FuncGraphPtr &fg, 51 mindspore::HashMap<AnfNodePtr, std::set<AnfNodePtr>> *out_caller_map, 52 bool is_add = true); 53 std::string GetCNodeKey(const AnfNodePtr &node); 54 } // namespace opt 55 } // namespace mindspore 56 #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_NODE_PASS_H_ 57