• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // protected:
59   //  GraphNode *graphNode = nullptr;
60 };
61 
62 class Optimizer {
63  public:
64   Optimizer() = default;
65 
66   virtual ~Optimizer();
67 
68   void AddPass(GraphPass *graphPass);
69 
70   void AddPass(NodePass *nodePass);
71 
72   STATUS Run(schema::MetaGraphT *graphDefT);
73 
74  private:
75   std::vector<GraphPass *> graph_passes_;
76   std::vector<NodePass *> node_passes_;
77 };
78 }  // namespace lite
79 }  // namespace mindspore
80 
81 #endif
82