d2eb83159778708c0ed380e5e9cf33fb126022aa
[platform/core/ml/nnfw.git] / runtime / onert / core / include / exec / DynamicShapeInferer.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_INFERER_H__
18 #define __ONERT_EXEC_DYNAMIC_SHAPE_INFERER_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,
42                       const std::shared_ptr<backend::ITensorRegistry> &tensor_registry)
43       : _operands(operands), _tensor_registry(tensor_registry)
44   {
45     UNUSED_RELEASE(_operands);
46     UNUSED_RELEASE(_tensor_registry);
47   }
48
49 public:
50   // TODO Define visitors for operations. List them in alphabetic order.
51   // Remove TODO when any op starting from the alphabet is added
52   void visit(const ir::operation::ArgMax &op) override;
53   void visit(const ir::operation::BatchMatMul &op) override;
54   void visit(const ir::operation::BCQFullyConnected &op) override;
55   void visit(const ir::operation::BCQGather &op) override;
56   void visit(const ir::operation::BinaryArithmetic &op) override;
57   void visit(const ir::operation::BroadcastTo &op) override;
58   void visit(const ir::operation::Comparison &op) override;
59   void visit(const ir::operation::Concat &op) override;
60   void visit(const ir::operation::Conv2D &op) override;
61   void visit(const ir::operation::ElementwiseActivation &op) override;
62   void visit(const ir::operation::ElementwiseBinary &op) override;
63   void visit(const ir::operation::ElementwiseUnary &op) override;
64   void visit(const ir::operation::ExpandDims &op) override;
65   void visit(const ir::operation::Fill &op) override;
66   void visit(const ir::operation::FullyConnected &op) override;
67   void visit(const ir::operation::FusedBatchNorm &op) override;
68   void visit(const ir::operation::Gather &op) override;
69   void visit(const ir::operation::L2Normalization &op) override;
70   void visit(const ir::operation::LSTM &op) override;
71   void visit(const ir::operation::MatrixBandPart &op) override;
72   void visit(const ir::operation::OneHot &op) override;
73   void visit(const ir::operation::Pack &op) override;
74   void visit(const ir::operation::Pad &op) override;
75   void visit(const ir::operation::Permute &op) override;
76   void visit(const ir::operation::Pow &op) override;
77   // TODO write op starting from Q
78   void visit(const ir::operation::Range &op) override;
79   void visit(const ir::operation::Reduce &op) override;
80   void visit(const ir::operation::Reshape &op) override;
81   void visit(const ir::operation::ResizeBilinear &op) override;
82   void visit(const ir::operation::Reverse &op) override;
83   void visit(const ir::operation::Select &op) override;
84   void visit(const ir::operation::Shape &op) override;
85   void visit(const ir::operation::Slice &op) override;
86   void visit(const ir::operation::Softmax &op) override;
87   void visit(const ir::operation::SpaceToBatchND &op) override;
88   void visit(const ir::operation::Split &op) override;
89   void visit(const ir::operation::Squeeze &op) override;
90   void visit(const ir::operation::StridedSlice &op) override;
91   void visit(const ir::operation::SquaredDifference &op) override;
92   void visit(const ir::operation::Tile &op) override;
93   void visit(const ir::operation::Transpose &op) override;
94   void visit(const ir::operation::Unpack &op) override;
95   // TODO write op starting from V
96
97 private:
98   /**
99    * @brief Performs shape inference and memory allocation for arithmetic operation
100    */
101   void handleBinaryArithmeticOp(const ir::Operation &op, const ir::OperandIndex lhs_idx,
102                                 const ir::OperandIndex rhs_idx);
103   /**
104    * @brief Performs shape inference and memory allocation for unary op whose output shape is
105    *        always same with input shape
106    */
107   void handleSimpleUnaryOp(const ir::Operation &op, const ir::OperandIndex input_idx);
108
109 private:
110   /**
111    * @brief To get operand-level info, e.g., ir::Operand::isConstant()
112    */
113   const ir::Operands &_operands;
114   /**
115    * @brief To get tensor object and access tensor-level info, e.g., ITensor::buffer()
116    */
117   std::shared_ptr<backend::ITensorRegistry> _tensor_registry;
118 };
119
120 } // namespace exec
121 } // namespace onert
122
123 #endif // __ONERT_EXEC_DYNAMIC_SHAPE_INFERER_H__