1 /** 2 * Copyright 2020 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_FRONTEND_OPTIMIZER_PASS_GROUP_H_ 17 #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_PASS_GROUP_H_ 18 19 #include <utility> 20 #include <vector> 21 #include <string> 22 #include <memory> 23 24 #include "frontend/optimizer/py_pass.h" 25 26 namespace mindspore { 27 namespace opt { 28 namespace python_pass { 29 class PassGroup { 30 public: 31 explicit PassGroup(const std::string &name = "pass_group", bool run_only_once = false) name_(name)32 : name_(name), passes_{}, run_only_once_(run_only_once) {} 33 virtual ~PassGroup() = default; 34 // Add graph pass, the pass object will be freed when pass manager freed. 35 void AddPass(const PythonPassPtr &pass); 36 // Delete graph pass before the pass manager is freed. 37 bool DeletePass(const std::string &pass_name); 38 // Run passes added in pass manager on the input graph 39 // @param [inout] graph The graph to be optimized 40 // @return true, graph changed 41 // @return false, graph not changed 42 bool Run(const FuncGraphPtr &func_graph) const; 43 // Run the given graph passes on the input graph 44 // @param [inout] func_graph The graph to be optimized 45 // @param [in] passes The given graph passes 46 // @param [inout] res MatchResult used to collect all matched patterns and nodes 47 // @return true, graph changed 48 // @return false, graph not changed 49 bool Run(const FuncGraphPtr &func_graph, const std::vector<PythonPassPtr> &passes, const MatchResultPtr &res) const; name()50 std::string name() const { return name_; } SetRunOnlyOnce(bool run_only_once)51 void SetRunOnlyOnce(bool run_only_once) { run_only_once_ = run_only_once; } size()52 size_t size() { return passes_.size(); } 53 54 private: 55 const std::string name_; 56 std::vector<PythonPassPtr> passes_; 57 bool run_only_once_; 58 }; 59 using PassGroupPtr = std::shared_ptr<PassGroup>; 60 } // namespace python_pass 61 } // namespace opt 62 } // namespace mindspore 63 64 #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_PASS_GROUP_H_ 65