2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __ONERT_EXEC_DYNAMIC_SHAPE_INFERENCE_H__
18 #define __ONERT_EXEC_DYNAMIC_SHAPE_INFERENCE_H__
20 #include "ir/Operands.h"
21 #include "ir/OperationVisitor.h"
23 #include "backend/IDynamicTensorManager.h"
24 #include "backend/ITensorManager.h"
25 #include "backend/ITensorRegistry.h"
35 * @brief Class to infer shape of output tensor at execution time and
36 * allocate memory fo output tensor if needed
38 class DynamicShapeInferer : public ir::OperationVisitor
41 DynamicShapeInferer(const ir::Operands &operands,
42 const std::shared_ptr<backend::ITensorRegistry> &tensor_registry)
43 : _operands(operands), _tensor_registry(tensor_registry)
45 UNUSED_RELEASE(_operands);
46 UNUSED_RELEASE(_tensor_registry);
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::BinaryArithmetic &op) override;
55 void visit(const ir::operation::BroadcastTo &op) override;
56 void visit(const ir::operation::Comparison &op) override;
57 void visit(const ir::operation::Concat &op) override;
58 void visit(const ir::operation::Conv2D &op) override;
59 void visit(const ir::operation::ElementwiseActivation &op) override;
60 void visit(const ir::operation::ElementwiseBinary &op) override;
61 void visit(const ir::operation::ElementwiseUnary &op) override;
62 void visit(const ir::operation::ExpandDims &op) override;
63 void visit(const ir::operation::Fill &op) override;
64 void visit(const ir::operation::FullyConnected &op) override;
65 void visit(const ir::operation::FusedBatchNorm &op) override;
66 void visit(const ir::operation::Gather &op) override;
67 void visit(const ir::operation::L2Normalization &op) override;
68 void visit(const ir::operation::MatrixBandPart &op) override;
69 void visit(const ir::operation::OneHot &op) override;
70 void visit(const ir::operation::Pack &op) override;
71 void visit(const ir::operation::Pad &op) override;
72 void visit(const ir::operation::Permute &op) override;
73 void visit(const ir::operation::Pow &op) override;
74 // TODO write op starting from Q
75 void visit(const ir::operation::Range &op) override;
76 void visit(const ir::operation::Reduce &op) override;
77 void visit(const ir::operation::Reshape &op) override;
78 void visit(const ir::operation::ResizeBilinear &op) override;
79 void visit(const ir::operation::Reverse &op) override;
80 void visit(const ir::operation::Select &op) override;
81 void visit(const ir::operation::Shape &op) override;
82 void visit(const ir::operation::Slice &op) override;
83 void visit(const ir::operation::Softmax &op) override;
84 void visit(const ir::operation::SpaceToBatchND &op) override;
85 void visit(const ir::operation::Split &op) override;
86 void visit(const ir::operation::Squeeze &op) override;
87 void visit(const ir::operation::StridedSlice &op) override;
88 void visit(const ir::operation::SquaredDifference &op) override;
89 void visit(const ir::operation::Tile &op) override;
90 void visit(const ir::operation::Transpose &op) override;
91 void visit(const ir::operation::Unpack &op) override;
92 // TODO write op starting from V
96 * @brief Performs shape inference and memory allocation for arithmetic operation
98 void handleBinaryArithmeticOp(const ir::Operation &op, const ir::OperandIndex lhs_idx,
99 const ir::OperandIndex rhs_idx);
101 * @brief Performs shape inference and memory allocation for unary op whose output shape is
102 * always same with input shape
104 void handleSimpleUnaryOp(const ir::Operation &op, const ir::OperandIndex input_idx);
108 * @brief To get operand-level info, e.g., ir::Operand::isConstant()
110 const ir::Operands &_operands;
112 * @brief To get tensor object and access tensor-level info, e.g., ITensor::buffer()
114 std::shared_ptr<backend::ITensorRegistry> _tensor_registry;
120 #endif // __ONERT_EXEC_DYNAMIC_SHAPE_INFERENCE_H__