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 LUCI_INTERPRETER_CORE_RUNTIMEGRAPH_H
18 #define LUCI_INTERPRETER_CORE_RUNTIMEGRAPH_H
20 #include "luci_interpreter/core/Tensor.h"
21 #ifdef USE_STATIC_ALLOC
22 #include "memory_managers/StaticMemoryManager.h"
24 #include "memory_managers/SimpleMemoryManager.h"
25 #endif // USE_STATIC_ALLOC
27 #include "luci_interpreter/core/reader/CircleMicroReader.h"
31 #include <unordered_map>
32 #include <unordered_set>
34 namespace luci_interpreter
39 #ifdef USE_STATIC_ALLOC
42 class StaticRuntimeGraph final : public IBaseRuntimeGraph
45 explicit StaticRuntimeGraph(IMemoryManager *memory_manager, CircleReader *circle_reader);
46 ~StaticRuntimeGraph() final;
48 void configureGraphInputs() final;
50 void configure() final;
52 void configure_kernels() final;
60 RuntimeGraph() = delete;
62 explicit RuntimeGraph(SimpleMemoryManager *memory_manager, CircleReader *circle_reader,
63 RuntimeModule *runtime_module, uint32_t subgraph_index);
66 Tensor *addTensor(const circle::Tensor *raw_tensor, std::unique_ptr<Tensor> &&tensor);
68 const circle::Tensor *getCircleTensorByIndex(int32_t index);
70 void makeInplaceOperation(const circle::Tensor *src_tensor, const circle::Tensor *dst_tensor);
72 uint8_t *getDataByTensor(const circle::Tensor *raw_tensor);
73 uint8_t *getConstDataByTensor(const circle::Tensor *raw_tensor);
75 uint8_t *configureGraphInput(int32_t input_index);
76 void configureGraphInput(int32_t input_index, uint8_t *data);
78 int32_t getInputDataSizeByIndex(int32_t input_index);
79 int32_t getOutputDataSizeByIndex(int32_t output_index);
81 int32_t getNumOfInputTensors();
82 int32_t getNumOfOutputTensors();
84 const circle::Tensor *getInputTensorByIndex(int32_t input_index);
85 const circle::Tensor *getOutputTensorByIndex(int32_t input_index);
87 uint8_t *getOutputDataByIndex(int32_t output_index);
89 void addInplaceOpIndex(const circle::Operator *op) { _inplace_op_indexes.insert(op); }
92 void configure(bool dealloc_input);
94 void invalidate() { _is_valid = false; }
95 bool isValid() const { return _is_valid; }
97 void selectOwnSubgraph() { _reader->select_subgraph(_subgraph_index); };
98 void resetOutputTensorsData();
102 void setDataToTensor(const circle::Tensor *tensor, uint8_t *data);
104 void resetTensorData(uint8_t *new_data, const circle::Tensor *tensor);
106 RuntimeModule *getRuntimeModule() { return _runtime_module; };
108 bool is_inplace_op(const circle::Operator *op)
110 return _inplace_op_indexes.find(op) != _inplace_op_indexes.end();
113 #ifndef DIS_DYN_SHAPES
114 void addDynamicShapeTensor(const circle::Tensor *tensor, luci_interpreter::RuntimeShape &&shapes);
116 luci_interpreter::RuntimeShape *getDynamicShapeTensor(const circle::Tensor *tensor);
118 void removeDynamicShapeTensor(const circle::Tensor *tensor);
119 #endif // DIS_DYN_SHAPES
122 void buildAllocDeallocPlan(bool dealloc_input);
123 void allocate(size_t kernel_index);
124 void deallocate(size_t kernel_index);
127 SimpleMemoryManager *_memory_manager;
128 CircleReader *_reader;
129 RuntimeModule *_runtime_module;
131 std::unordered_map<const circle::Tensor *, uint8_t *> _tensor_to_data;
132 std::unordered_set<const circle::Operator *> _inplace_op_indexes;
134 bool _is_valid = false;
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;
140 uint32_t _subgraph_index;
142 #ifndef DIS_DYN_SHAPES
143 std::unordered_map<const circle::Tensor *, luci_interpreter::RuntimeShape> _dynamic_tensor_shapes;
144 #endif // DIS_DYN_SHAPES
147 #endif // USE_STATIC_ALLOC
149 } // namespace luci_interpreter
151 #endif // LUCI_INTERPRETER_CORE_RUNTIMEGRAPH_H