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 #include "luci_interpreter/Interpreter.h"
19 #include "loader/ModuleLoader.h"
21 namespace luci_interpreter
24 #ifdef USE_STATIC_ALLOC
25 // Construct static interpreter with configurations
26 Interpreter::Interpreter(const char *model_data_raw, const InterpreterConfigure &configuration)
28 _runtime_module = std::make_unique<RuntimeModule>();
30 _memory_manager = StaticMemoryManager(configuration._input_buf_size, configuration._temp_buf_size,
31 configuration._output_buf_size)
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())
38 _memory_manager = std::make_unique<StaticMemoryManager>(
39 configuration._input_buf_size, configuration._temp_buf_size, configuration._output_buf_size);
41 else { _memory_manager = std::make_unique<SimpleMemoryManager>(); }
43 _memory_manager->is_allocate_input(configuration.getAllocateInputValue());
45 ModuleLoader loader();
46 ModuleLoader::load(_runtime_module.get(), _memory_manager.get(),
47 /* is_static_allocations */ configuration.isStaticManager(), model_data_raw);
49 ModuleLoader loader(_runtime_module.get(), _memory_manager.get());
50 loader.load(configuration.isStaticManager(), model_data_raw);
54 // Construct default interpreter with dynamic allocations and with input allocations
55 Interpreter::Interpreter(const char *model_data_raw, bool dealloc_input)
57 ModuleLoader::load(&_runtime_module, &_memory_manager, model_data_raw, dealloc_input);
60 #endif // USE_STATIC_ALLOC
62 Interpreter::~Interpreter() = default;
64 void Interpreter::interpret() { _runtime_module.execute(); }
66 int32_t Interpreter::getInputDataSizeByIndex(int32_t input_tensor_index)
68 auto *runtime_graph = _runtime_module.getMainGraph();
70 return runtime_graph->getInputDataSizeByIndex(input_tensor_index);
73 int32_t Interpreter::getOutputDataSizeByIndex(int32_t output_tensor_index)
75 auto *runtime_graph = _runtime_module.getMainGraph();
77 return runtime_graph->getOutputDataSizeByIndex(output_tensor_index);
80 void Interpreter::allocateAndWriteInputTensor(int32_t input_tensor_index, const void *data,
83 assert(data_size > 0);
84 assert(data != nullptr);
85 assert(input_tensor_index >= 0);
86 auto *runtime_graph = _runtime_module.getMainGraph();
87 auto tensor_data = runtime_graph->configureGraphInput(input_tensor_index);
89 std::memcpy(tensor_data, data, data_size);
92 uint8_t *Interpreter::allocateInputTensor(int32_t input_tensor_index)
94 assert(input_tensor_index >= 0);
96 auto *runtime_graph = _runtime_module.getMainGraph();
98 return runtime_graph->configureGraphInput(input_tensor_index);
101 uint8_t *Interpreter::readOutputTensor(int32_t output_tensor_index)
103 auto *runtime_graph = _runtime_module.getMainGraph();
105 return runtime_graph->getOutputDataByIndex(output_tensor_index);
108 } // namespace luci_interpreter