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 17 #ifndef MINDSPORE_LITE_TOOLS_CONVERTER_OPTIMIZER_H_ 18 #define MINDSPORE_LITE_TOOLS_CONVERTER_OPTIMIZER_H_ 19 #include <vector> 20 #include "schema/inner/model_generated.h" 21 #include "include/errorcode.h" 22 23 namespace mindspore { 24 namespace lite { 25 using namespace schema; 26 template <typename T> 27 class Pass { 28 public: 29 Pass() = default; 30 virtual ~Pass() = default; 31 virtual STATUS Run(T *t) = 0; 32 }; 33 34 class GraphPass : public Pass<schema::MetaGraphT> { 35 public: 36 GraphPass() = default; 37 38 ~GraphPass() override = default; 39 40 STATUS Run(schema::MetaGraphT *graph) override = 0; 41 }; 42 43 struct GraphNode { GraphNodeGraphNode44 GraphNode(schema::MetaGraphT *subGraph, schema::CNodeT *opDefT) : sub_graph_(subGraph), op_def_(opDefT) {} 45 ~GraphNode() = default; 46 schema::MetaGraphT *sub_graph_ = nullptr; 47 schema::CNodeT *op_def_ = nullptr; 48 }; 49 50 class NodePass : public Pass<GraphNode> { 51 public: 52 NodePass() = default; 53 54 ~NodePass() override = default; 55 56 STATUS Run(GraphNode *graphNode) override = 0; 57 }; 58 59 class Optimizer { 60 public: 61 Optimizer() = default; 62 63 virtual ~Optimizer(); 64 65 void AddPass(GraphPass *graph_pass); 66 67 void AddPass(NodePass *node_pass); 68 69 STATUS Run(schema::MetaGraphT *graph_defT); 70 71 private: 72 std::vector<GraphPass *> graph_passes_; 73 std::vector<NodePass *> node_passes_; 74 }; 75 } // namespace lite 76 } // namespace mindspore 77 78 #endif 79