fb956fedf52b4c0d3d73e18c87eaac196f84a63c
[platform/core/ml/nnfw.git] / runtime / onert / core / include / ir / Graph.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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 __ONERT_IR_GRAPH_H__
18 #define __ONERT_IR_GRAPH_H__
19
20 #include <functional>
21 #include <unordered_map>
22
23 #include "ir/Operands.h"
24 #include "ir/Operations.h"
25 #include "ir/OpSequence.h"
26 #include "ir/OpSequences.h"
27 #include "ir/Subgraphs.h"
28
29 namespace onert
30 {
31 namespace backend
32 {
33 namespace custom
34 {
35 class IKernelBuilder;
36 } // namespace custom
37 } // namespace backend
38 } // namespace onert
39
40 namespace onert
41 {
42 namespace ir
43 {
44
45 class Graph
46 {
47 private:
48   enum class Phase
49   {
50     BUILDING,
51     MODEL
52   };
53
54 public:
55   Graph(void);
56   ~Graph(void);
57
58   // Graph Building
59 public:
60   OperandIndex addOperand(const Shape &shape, const TypeInfo &type);
61   OperationIndex addOperation(std::unique_ptr<Operation> &&node);
62   void setOperandValue(const OperandIndex &ind, std::shared_ptr<Data> data);
63   void addInput(const OperandIndex &ind);
64   void addOutput(const OperandIndex &ind);
65   void finishBuilding(void);
66   void removeOperand(const OperandIndex &ind) { _operands.remove(ind); }
67   bool isBuildingPhase(void) const { return _phase == Phase::BUILDING; }
68   void setLayout(Layout layout) { _layout = layout; }
69   void setSubgraphs(const std::shared_ptr<Subgraphs> &subgs) { _subgraphs = subgs; }
70
71 private:
72   void initializeUseDef();
73   void sweepGarbageOperands();
74
75   // Custom operations support
76 public:
77   void
78   bindKernelBuilder(const std::shared_ptr<onert::backend::custom::IKernelBuilder> &kernel_builder)
79   {
80     _kernel_builder = kernel_builder;
81   }
82
83   const std::shared_ptr<backend::custom::IKernelBuilder> &getKernelBuilder() const
84   {
85     return _kernel_builder;
86   }
87
88 private:
89   std::shared_ptr<backend::custom::IKernelBuilder> _kernel_builder;
90
91   // Accessors
92 public:
93   const OperandIndexSequence &getInputs() const { return _inputs; }
94   OperandIndexSequence &getInputs() { return _inputs; }
95   const OperandIndexSequence &getOutputs() const { return _outputs; }
96   OperandIndexSequence &getOutputs() { return _outputs; }
97   const Operands &operands() const { return _operands; }
98   Operands &operands() { return _operands; } // TODO Remove this non-const accessor
99   const Operations &operations() const { return _operations; }
100   Operations &operations() { return _operations; }
101   const std::shared_ptr<Subgraphs> &subgraphs() const { return _subgraphs; }
102   std::shared_ptr<Subgraphs> &subgraphs() { return _subgraphs; }
103   Layout layout() const { return _layout; }
104
105 private:
106   Phase _phase{Phase::BUILDING};
107   Operations _operations;
108   Operands _operands;
109   OperandIndexSequence _inputs;
110   OperandIndexSequence _outputs;
111   // Child subgraphs
112   std::shared_ptr<Subgraphs> _subgraphs;
113   // TFLite and circle's default layout is NHWC;
114   Layout _layout{Layout::NHWC};
115 };
116
117 } // namespace ir
118 } // namespace onert
119
120 #endif // __ONERT_IR_GRAPH_H__