forked from Samsung/ONE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicShapeInferer.h
More file actions
126 lines (114 loc) · 5.44 KB
/
DynamicShapeInferer.h
File metadata and controls
126 lines (114 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __ONERT_EXEC_DYNAMIC_SHAPE_INFERER_H__
#define __ONERT_EXEC_DYNAMIC_SHAPE_INFERER_H__
#include "ir/Operands.h"
#include "ir/OperationVisitor.h"
#include "ir/Index.h"
#include "backend/ITensorRegistry.h"
#include <map>
namespace onert::exec
{
/**
* @brief Class to infer shape of output tensor at execution time and
* allocate memory fo output tensor if needed
*/
class DynamicShapeInferer : public ir::OperationVisitor
{
public:
DynamicShapeInferer(const std::shared_ptr<backend::ITensorRegistry> &tensor_registry)
: _tensor_registry(tensor_registry)
{
// DO NOTHING
}
public:
// TODO Define visitors for operations. List them in alphabetic order.
// Remove TODO when any op starting from the alphabet is added
void visit(const ir::operation::ArgMinMax &op) override;
void visit(const ir::operation::BatchMatMul &op) override;
void visit(const ir::operation::BCQFullyConnected &op) override;
void visit(const ir::operation::BCQGather &op) override;
void visit(const ir::operation::BinaryArithmetic &op) override;
void visit(const ir::operation::BroadcastTo &op) override;
void visit(const ir::operation::Comparison &op) override;
void visit(const ir::operation::Concat &op) override;
void visit(const ir::operation::Conv2D &op) override;
void visit(const ir::operation::ElementwiseActivation &op) override;
void visit(const ir::operation::ElementwiseBinary &op) override;
void visit(const ir::operation::ElementwiseUnary &op) override;
void visit(const ir::operation::ExpandDims &op) override;
void visit(const ir::operation::Fill &op) override;
void visit(const ir::operation::FullyConnected &op) override;
void visit(const ir::operation::FusedBatchNorm &op) override;
void visit(const ir::operation::Gather &op) override;
void visit(const ir::operation::L2Normalization &op) override;
void visit(const ir::operation::LSTM &op) override;
void visit(const ir::operation::DetectionPostProcess &op) override;
void visit(const ir::operation::OneHot &op) override;
void visit(const ir::operation::Pack &op) override;
void visit(const ir::operation::Pad &op) override;
void visit(const ir::operation::Permute &op) override;
void visit(const ir::operation::Pool2D &op) override;
void visit(const ir::operation::Pow &op) override;
// TODO write op starting from Q
void visit(const ir::operation::Range &op) override;
void visit(const ir::operation::Reduce &op) override;
void visit(const ir::operation::Reshape &op) override;
void visit(const ir::operation::ResizeBilinear &op) override;
void visit(const ir::operation::Reverse &op) override;
void visit(const ir::operation::Select &op) override;
void visit(const ir::operation::Shape &op) override;
void visit(const ir::operation::Slice &op) override;
void visit(const ir::operation::Softmax &op) override;
void visit(const ir::operation::SpaceToBatchND &op) override;
void visit(const ir::operation::Split &op) override;
void visit(const ir::operation::Squeeze &op) override;
void visit(const ir::operation::StridedSlice &op) override;
void visit(const ir::operation::SquaredDifference &op) override;
void visit(const ir::operation::Tile &op) override;
void visit(const ir::operation::Transpose &op) override;
void visit(const ir::operation::Unpack &op) override;
// TODO write op starting from V
private:
/**
* @brief Performs shape inference and memory allocation for arithmetic operation
*/
void handleBinaryArithmeticOp(const ir::Operation &op, const ir::OperandIndex lhs_idx,
const ir::OperandIndex rhs_idx);
/**
* @brief Performs shape inference and memory allocation for unary op whose output shape is
* always same with input shape
*/
void handleSimpleUnaryOp(const ir::Operation &op, const ir::OperandIndex input_idx);
// in case of output tensor of an op, it is possible that
// the output became dynamic although it had been static before.
// Once a tensor becomes dynamic, it will lost memory allocated for static.
// Therefore once output is dynamic, it should be treated as dynamic tensor. (memory should be
// allocated at runtime) `previously` means `dynamic` or `static` has been set in previous loop in
// WHILE of previous call of `nnfw_run()`
bool previously_static(backend::ITensor *op_output) { return !op_output->is_dynamic(); }
// helper function that check if op's input is static
// Note that input of n'th op has been set to static or dynamic by (n-1)th op.
// That's why it is called `currently_static`
bool currently_static(backend::ITensor *op_input) { return !op_input->is_dynamic(); }
private:
/**
* @brief To get tensor object and access tensor-level info, e.g., ITensor::buffer()
*/
std::shared_ptr<backend::ITensorRegistry> _tensor_registry;
};
} // namespace onert::exec
#endif // __ONERT_EXEC_DYNAMIC_SHAPE_INFERER_H__