bca80db09d3063c00792504d1b302b6b2cd6f58f
[platform/core/ml/nnfw.git] / runtime / onert / core / include / exec / DynamicShapeInference.h
1 /*
2  * Copyright (c) 2020 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_EXEC_DYNAMIC_SHAPE_INFERENCE_H__
18 #define __ONERT_EXEC_DYNAMIC_SHAPE_INFERENCE_H__
19
20 #include "ir/Operands.h"
21 #include "ir/OperationVisitor.h"
22 #include "ir/Index.h"
23 #include "backend/IDynamicTensorManager.h"
24 #include "backend/ITensorManager.h"
25 #include "backend/ITensorRegistry.h"
26
27 #include <map>
28
29 namespace onert
30 {
31 namespace exec
32 {
33
34 /**
35  * @brief Class to infer shape of output tensor at execution time and
36  *        allocate memory fo output tensor if needed
37  */
38 class DynamicShapeInferer : public ir::OperationVisitor
39 {
40 public:
41   DynamicShapeInferer(const ir::Operands &operands, backend::IDynamicTensorManager *tensor_manager,
42                       const std::shared_ptr<backend::ITensorRegistry> &tensor_registry)
43       : _operands(operands), _dynamic_tensor_manager(tensor_manager),
44         _tensor_registry(tensor_registry)
45   {
46     UNUSED_RELEASE(_operands);
47     UNUSED_RELEASE(_dynamic_tensor_manager);
48     UNUSED_RELEASE(_tensor_registry);
49   }
50
51 public:
52   // TODO Define visitors for operations. List them in alphabetic order.
53   // Remove TODO when any op starting from the alphabet is added
54   void visit(const ir::operation::Abs &op) override;
55   void visit(const ir::operation::Add &op) override;
56   void visit(const ir::operation::ArgMax &op) override;
57   void visit(const ir::operation::BatchMatMul &op) override;
58   void visit(const ir::operation::BroadcastTo &op) override;
59   void visit(const ir::operation::Cast &op) override;
60   void visit(const ir::operation::Comparison &op) override;
61   void visit(const ir::operation::Concat &op) override;
62   void visit(const ir::operation::Conv2D &op) override;
63   void visit(const ir::operation::Cos &op) override;
64   void visit(const ir::operation::Div &op) override;
65   void visit(const ir::operation::Exp &op) override;
66   void visit(const ir::operation::ExpandDims &op) override;
67   void visit(const ir::operation::Fill &op) override;
68   void visit(const ir::operation::FullyConnected &op) override;
69   void visit(const ir::operation::FusedBatchNorm &op) override;
70   void visit(const ir::operation::Gather &op) override;
71   void visit(const ir::operation::Log &op) override;
72   void visit(const ir::operation::LogicalNot &op) override;
73   void visit(const ir::operation::LogicalOr &op) override;
74   void visit(const ir::operation::Logistic &op) override;
75   void visit(const ir::operation::L2Normalization &op) override;
76   void visit(const ir::operation::MatrixBandPart &op) override;
77   void visit(const ir::operation::Max &op) override;
78   void visit(const ir::operation::Min &op) override;
79   void visit(const ir::operation::Mul &op) override;
80   void visit(const ir::operation::Neg &op) override;
81   void visit(const ir::operation::OneHot &op) override;
82   void visit(const ir::operation::Pack &op) override;
83   void visit(const ir::operation::Pad &op) override;
84   void visit(const ir::operation::Permute &op) override;
85   void visit(const ir::operation::Pow &op) override;
86   // TODO write op starting from Q
87   void visit(const ir::operation::Range &op) override;
88   void visit(const ir::operation::Reduce &op) override;
89   void visit(const ir::operation::Reshape &op) override;
90   void visit(const ir::operation::Round &op) override;
91   void visit(const ir::operation::RSQRT &op) override;
92   void visit(const ir::operation::ResizeBilinear &op) override;
93   void visit(const ir::operation::Reverse &op) override;
94   void visit(const ir::operation::Select &op) override;
95   void visit(const ir::operation::Shape &op) override;
96   void visit(const ir::operation::Sin &op) override;
97   void visit(const ir::operation::Slice &op) override;
98   void visit(const ir::operation::Softmax &op) override;
99   void visit(const ir::operation::SpaceToBatchND &op) override;
100   void visit(const ir::operation::Split &op) override;
101   void visit(const ir::operation::Squeeze &op) override;
102   void visit(const ir::operation::StridedSlice &op) override;
103   void visit(const ir::operation::Sub &op) override;
104   void visit(const ir::operation::SquaredDifference &op) override;
105   void visit(const ir::operation::Tanh &op) override;
106   void visit(const ir::operation::Tile &op) override;
107   void visit(const ir::operation::Transpose &op) override;
108   void visit(const ir::operation::Unpack &op) override;
109   // TODO write op starting from V
110   void visit(const ir::operation::ZerosLike &op) override;
111
112 private:
113   /**
114    * @brief Performs shape inference and memory allocation for arithmetic operation
115    */
116   void handleBinaryArithmeticOp(const ir::Operation &op, const ir::OperandIndex lhs_idx,
117                                 const ir::OperandIndex rhs_idx);
118   /**
119    * @brief Performs shape inference and memory allocation for unary op whose output shape is
120    *        always same with input shape
121    */
122   void handleSimpleUnaryOp(const ir::Operation &op, const ir::OperandIndex input_idx);
123
124 private:
125   /**
126    * @brief To get operand-level info, e.g., ir::Operand::isConstant()
127    */
128   const ir::Operands &_operands;
129   /**
130    * @brief To allocate memory for output tensor if needed
131    */
132   // TODO Remove this, as it is no longer used
133   backend::IDynamicTensorManager *_dynamic_tensor_manager;
134   /**
135    * @brief To get tensor object and access tensor-level info, e.g., ITensor::buffer()
136    */
137   std::shared_ptr<backend::ITensorRegistry> _tensor_registry;
138 };
139
140 } // namespace exec
141 } // namespace onert
142
143 #endif // __ONERT_EXEC_DYNAMIC_SHAPE_INFERENCE_H__