ONE - On-device Neural Engine
Loading...
Searching...
No Matches
RuntimeGraph.h
Go to the documentation of this file.
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
22#ifdef ENABLE_TRAINING
23#include "TrainingGraph.h"
24#endif // ENABLE_TRAINING
25
26#ifdef USE_STATIC_ALLOC
28#else
30#endif // USE_STATIC_ALLOC
31
33
34#include <memory>
35#include <vector>
36#include <unordered_map>
37#include <unordered_set>
38
39namespace luci_interpreter
40{
41
42class RuntimeModule;
43
44#ifdef USE_STATIC_ALLOC
45// TODO: Enable it
46#if 0
47class StaticRuntimeGraph final : public IBaseRuntimeGraph
48{
49public:
50 explicit StaticRuntimeGraph(IMemoryManager *memory_manager, CircleReader *circle_reader);
51 ~StaticRuntimeGraph() final;
52
53 void configureGraphInputs() final;
54 void execute() final;
55 void configure() final;
56
57 void configure_kernels() final;
58};
59#endif
60#else
61
62class RuntimeGraph
63{
64public:
65 RuntimeGraph() = delete;
66
67 explicit RuntimeGraph(SimpleMemoryManager *memory_manager, CircleReader *circle_reader,
68 RuntimeModule *runtime_module, uint32_t subgraph_index);
70
71 Tensor *addTensor(const circle::Tensor *raw_tensor, std::unique_ptr<Tensor> &&tensor);
72
73 const circle::Tensor *getCircleTensorByIndex(int32_t index);
74
75 void makeInplaceOperation(const circle::Tensor *src_tensor, const circle::Tensor *dst_tensor);
76
77 uint8_t *getDataByTensor(const circle::Tensor *raw_tensor);
78 uint8_t *getConstDataByTensor(const circle::Tensor *raw_tensor);
79
80 uint8_t *configureGraphInput(int32_t input_index);
81 void configureGraphInput(int32_t input_index, uint8_t *data);
82
83 int32_t getInputDataSizeByIndex(int32_t input_index);
84 int32_t getOutputDataSizeByIndex(int32_t output_index);
85
86 int32_t getNumOfInputTensors();
87 int32_t getNumOfOutputTensors();
88
89 const circle::Tensor *getInputTensorByIndex(int32_t input_index);
90 const circle::Tensor *getOutputTensorByIndex(int32_t input_index);
91
92 uint8_t *getOutputDataByIndex(int32_t output_index);
93
94 void addInplaceOpIndex(const circle::Operator *op) { _inplace_op_indexes.insert(op); }
95
96 void execute();
97 void configure(bool dealloc_input);
98
99 void invalidate() { _is_valid = false; }
100 bool isValid() const { return _is_valid; }
101
102 void selectOwnSubgraph() { _reader->select_subgraph(_subgraph_index); };
103 void resetOutputTensorsData();
104
105 void clearTensors();
106
107 void setDataToTensor(const circle::Tensor *tensor, uint8_t *data);
108
109 void resetTensorData(uint8_t *new_data, const circle::Tensor *tensor);
110
111 RuntimeModule *getRuntimeModule() { return _runtime_module; };
112
113 bool is_inplace_op(const circle::Operator *op)
114 {
115 return _inplace_op_indexes.find(op) != _inplace_op_indexes.end();
116 }
117
118#ifdef ENABLE_TRAINING
119 void setLastTrainingLayersNumber(uint32_t training_number)
120 {
121 _number_of_last_trainable_layers = training_number;
122 }
123 void setGradientCalculationStorage(training::GradientCalculationStorage *gradient_calc_storage)
124 {
125 _gradient_calc_storage = gradient_calc_storage;
126 }
127 void setTrainingWeightStorage(training::TrainableWeightStorage *storage) { _storage = storage; }
128#endif // ENABLE_TRAINING
129
130#ifndef DIS_DYN_SHAPES
131 void addDynamicShapeTensor(const circle::Tensor *tensor, luci_interpreter::RuntimeShape &&shapes);
132
133 luci_interpreter::RuntimeShape *getDynamicShapeTensor(const circle::Tensor *tensor);
134
135 void removeDynamicShapeTensor(const circle::Tensor *tensor);
136#endif // DIS_DYN_SHAPES
137
138private:
139 void buildAllocDeallocPlan(bool dealloc_input);
140 void allocate(size_t kernel_index);
141 void deallocate(size_t kernel_index);
142
143private:
144 SimpleMemoryManager *_memory_manager;
145 CircleReader *_reader;
146 RuntimeModule *_runtime_module;
147
148 std::unordered_map<const circle::Tensor *, uint8_t *> _tensor_to_data;
149 std::unordered_set<const circle::Operator *> _inplace_op_indexes;
150
151 bool _is_valid = false;
152
153#ifdef ENABLE_TRAINING
154 uint32_t _number_of_last_trainable_layers = 0; // 0 means there is no training
155 training::GradientCalculationStorage *_gradient_calc_storage = nullptr;
156 training::TrainableWeightStorage *_storage = nullptr;
157#endif // ENABLE_TRAINING
158
159 // Tensors that are not used anymore after given op
160 std::vector<std::vector<const circle::Tensor *>> _alloc_plan;
161 std::vector<std::vector<const circle::Tensor *>> _dealloc_plan;
162
163 uint32_t _subgraph_index;
164
165#ifndef DIS_DYN_SHAPES
166 std::unordered_map<const circle::Tensor *, luci_interpreter::RuntimeShape> _dynamic_tensor_shapes;
167#endif // DIS_DYN_SHAPES
168};
169
170#endif // USE_STATIC_ALLOC
171
172} // namespace luci_interpreter
173
174#endif // LUCI_INTERPRETER_CORE_RUNTIMEGRAPH_H
Loads Circle file and provides helpers to access attributes.
void addInplaceOpIndex(const circle::Operator *op)
bool is_inplace_op(const circle::Operator *op)
Tensor * addTensor(const circle::Tensor *raw_tensor, std::unique_ptr< Tensor > &&tensor)
RuntimeModule * getRuntimeModule()