Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / core / RuntimeGraph.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 LUCI_INTERPRETER_CORE_RUNTIMEGRAPH_H
18 #define LUCI_INTERPRETER_CORE_RUNTIMEGRAPH_H
19
20 #include "luci_interpreter/core/Tensor.h"
21 #ifdef USE_STATIC_ALLOC
22 #include "memory_managers/StaticMemoryManager.h"
23 #else
24 #include "memory_managers/SimpleMemoryManager.h"
25 #endif // USE_STATIC_ALLOC
26
27 #include "luci_interpreter/core/reader/CircleMicroReader.h"
28
29 #include <memory>
30 #include <vector>
31 #include <unordered_map>
32 #include <unordered_set>
33
34 namespace luci_interpreter
35 {
36
37 class RuntimeModule;
38
39 #ifdef USE_STATIC_ALLOC
40 // TODO: Enable it
41 #if 0
42 class StaticRuntimeGraph final : public IBaseRuntimeGraph
43 {
44 public:
45   explicit StaticRuntimeGraph(IMemoryManager *memory_manager, CircleReader *circle_reader);
46   ~StaticRuntimeGraph() final;
47
48   void configureGraphInputs() final;
49   void execute() final;
50   void configure() final;
51
52   void configure_kernels() final;
53 };
54 #endif
55 #else
56
57 class RuntimeGraph
58 {
59 public:
60   RuntimeGraph() = delete;
61
62   explicit RuntimeGraph(SimpleMemoryManager *memory_manager, CircleReader *circle_reader,
63                         RuntimeModule *runtime_module, uint32_t subgraph_index);
64   ~RuntimeGraph();
65
66   Tensor *addTensor(const circle::Tensor *raw_tensor, std::unique_ptr<Tensor> &&tensor);
67
68   const circle::Tensor *getCircleTensorByIndex(int32_t index);
69
70   void makeInplaceOperation(const circle::Tensor *src_tensor, const circle::Tensor *dst_tensor);
71
72   uint8_t *getDataByTensor(const circle::Tensor *raw_tensor);
73   uint8_t *getConstDataByTensor(const circle::Tensor *raw_tensor);
74
75   uint8_t *configureGraphInput(int32_t input_index);
76   void configureGraphInput(int32_t input_index, uint8_t *data);
77
78   int32_t getInputDataSizeByIndex(int32_t input_index);
79   int32_t getOutputDataSizeByIndex(int32_t output_index);
80
81   int32_t getNumOfInputTensors();
82   int32_t getNumOfOutputTensors();
83
84   const circle::Tensor *getInputTensorByIndex(int32_t input_index);
85   const circle::Tensor *getOutputTensorByIndex(int32_t input_index);
86
87   uint8_t *getOutputDataByIndex(int32_t output_index);
88
89   void addInplaceOpIndex(const circle::Operator *op) { _inplace_op_indexes.insert(op); }
90
91   void execute();
92   void configure(bool dealloc_input);
93
94   void invalidate() { _is_valid = false; }
95   bool isValid() const { return _is_valid; }
96
97   void selectOwnSubgraph() { _reader->select_subgraph(_subgraph_index); };
98   void resetOutputTensorsData();
99
100   void clearTensors();
101
102   void setDataToTensor(const circle::Tensor *tensor, uint8_t *data);
103
104   void resetTensorData(uint8_t *new_data, const circle::Tensor *tensor);
105
106   RuntimeModule *getRuntimeModule() { return _runtime_module; };
107
108   bool is_inplace_op(const circle::Operator *op)
109   {
110     return _inplace_op_indexes.find(op) != _inplace_op_indexes.end();
111   }
112
113 #ifndef DIS_DYN_SHAPES
114   void addDynamicShapeTensor(const circle::Tensor *tensor, luci_interpreter::RuntimeShape &&shapes);
115
116   luci_interpreter::RuntimeShape *getDynamicShapeTensor(const circle::Tensor *tensor);
117
118   void removeDynamicShapeTensor(const circle::Tensor *tensor);
119 #endif // DIS_DYN_SHAPES
120
121 private:
122   void buildAllocDeallocPlan(bool dealloc_input);
123   void allocate(size_t kernel_index);
124   void deallocate(size_t kernel_index);
125
126 private:
127   SimpleMemoryManager *_memory_manager;
128   CircleReader *_reader;
129   RuntimeModule *_runtime_module;
130
131   std::unordered_map<const circle::Tensor *, uint8_t *> _tensor_to_data;
132   std::unordered_set<const circle::Operator *> _inplace_op_indexes;
133
134   bool _is_valid = false;
135
136   // Tensors that are not used anymore after given op
137   std::vector<std::vector<const circle::Tensor *>> _alloc_plan;
138   std::vector<std::vector<const circle::Tensor *>> _dealloc_plan;
139
140   uint32_t _subgraph_index;
141
142 #ifndef DIS_DYN_SHAPES
143   std::unordered_map<const circle::Tensor *, luci_interpreter::RuntimeShape> _dynamic_tensor_shapes;
144 #endif // DIS_DYN_SHAPES
145 };
146
147 #endif // USE_STATIC_ALLOC
148
149 } // namespace luci_interpreter
150
151 #endif // LUCI_INTERPRETER_CORE_RUNTIMEGRAPH_H