ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Interpreter.cpp
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#include "luci_interpreter/Interpreter.h"
18
19#include "loader/ModuleLoader.h"
20
21namespace luci_interpreter
22{
23
24#ifdef USE_STATIC_ALLOC
25// Construct static interpreter with configurations
26Interpreter::Interpreter(const char *model_data_raw, const InterpreterConfigure &configuration)
27{
28 _runtime_module = std::make_unique<RuntimeModule>();
29
30 _memory_manager = StaticMemoryManager(configuration._input_buf_size, configuration._temp_buf_size,
31 configuration._output_buf_size)
32
33 // Note:
34 // configuration._input_buf_size, configuration._temp_buf_size, configuration._output_buf_size
35 // will be removed and will be read from circle file
36 if (configuration.isStaticManager())
37 {
38 _memory_manager = std::make_unique<StaticMemoryManager>(
39 configuration._input_buf_size, configuration._temp_buf_size, configuration._output_buf_size);
40 }
41 else
42 {
43 _memory_manager = std::make_unique<SimpleMemoryManager>();
44 }
45
46 _memory_manager->is_allocate_input(configuration.getAllocateInputValue());
47
48 ModuleLoader loader();
49 ModuleLoader::load(_runtime_module.get(), _memory_manager.get(),
50 /* is_static_allocations */ configuration.isStaticManager(), model_data_raw);
51
52 ModuleLoader loader(_runtime_module.get(), _memory_manager.get());
53 loader.load(configuration.isStaticManager(), model_data_raw);
54}
55#else
56
57// Construct default interpreter with dynamic allocations and with input allocations
58Interpreter::Interpreter(const char *model_data_raw, bool dealloc_input)
59{
60 ModuleLoader::load(&_runtime_module, &_memory_manager, model_data_raw, dealloc_input);
61}
62
63#endif // USE_STATIC_ALLOC
64
66
67void Interpreter::interpret() { _runtime_module.execute(); }
68
69int32_t Interpreter::getInputDataSizeByIndex(int32_t input_tensor_index)
70{
71 auto *runtime_graph = _runtime_module.getMainGraph();
72
73 return runtime_graph->getInputDataSizeByIndex(input_tensor_index);
74}
75
76int32_t Interpreter::getOutputDataSizeByIndex(int32_t output_tensor_index)
77{
78 auto *runtime_graph = _runtime_module.getMainGraph();
79
80 return runtime_graph->getOutputDataSizeByIndex(output_tensor_index);
81}
82
83void Interpreter::allocateAndWriteInputTensor(int32_t input_tensor_index, const uint8_t *data,
84 size_t data_size)
85{
86 assert(data_size > 0);
87 assert(data != nullptr);
88 assert(input_tensor_index >= 0);
89 auto *runtime_graph = _runtime_module.getMainGraph();
90 auto tensor_data = runtime_graph->configureGraphInput(input_tensor_index);
91
92 std::memcpy(tensor_data, data, data_size);
93}
94
95uint8_t *Interpreter::allocateInputTensor(int32_t input_tensor_index)
96{
97 assert(input_tensor_index >= 0);
98
99 auto *runtime_graph = _runtime_module.getMainGraph();
100
101 return runtime_graph->configureGraphInput(input_tensor_index);
102}
103
104uint8_t *Interpreter::readOutputTensor(int32_t output_tensor_index)
105{
106 auto *runtime_graph = _runtime_module.getMainGraph();
107
108 return runtime_graph->getOutputDataByIndex(output_tensor_index);
109}
110
111} // namespace luci_interpreter
void allocateAndWriteInputTensor(int32_t input_tensor_index, const uint8_t *data, size_t data_size)
int32_t getInputDataSizeByIndex(int32_t input_tensor_index)
uint8_t * allocateInputTensor(int32_t input_tensor_index)
Interpreter(const luci::Module *module)
void readOutputTensor(const luci::CircleOutput *output_node, void *data, size_t data_size)
int32_t getOutputDataSizeByIndex(int32_t output_tensor_index)
const T * data(const std::vector< T, Alloc > &v)