Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / Interpreter.cpp
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
21 namespace luci_interpreter
22 {
23
24 #ifdef USE_STATIC_ALLOC
25 // Construct static interpreter with configurations
26 Interpreter::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 { _memory_manager = std::make_unique<SimpleMemoryManager>(); }
42
43   _memory_manager->is_allocate_input(configuration.getAllocateInputValue());
44
45   ModuleLoader loader();
46   ModuleLoader::load(_runtime_module.get(), _memory_manager.get(),
47                      /* is_static_allocations */ configuration.isStaticManager(), model_data_raw);
48
49   ModuleLoader loader(_runtime_module.get(), _memory_manager.get());
50   loader.load(configuration.isStaticManager(), model_data_raw);
51 }
52 #else
53
54 // Construct default interpreter with dynamic allocations and with input allocations
55 Interpreter::Interpreter(const char *model_data_raw, bool dealloc_input)
56 {
57   ModuleLoader::load(&_runtime_module, &_memory_manager, model_data_raw, dealloc_input);
58 }
59
60 #endif // USE_STATIC_ALLOC
61
62 Interpreter::~Interpreter() = default;
63
64 void Interpreter::interpret() { _runtime_module.execute(); }
65
66 int32_t Interpreter::getInputDataSizeByIndex(int32_t input_tensor_index)
67 {
68   auto *runtime_graph = _runtime_module.getMainGraph();
69
70   return runtime_graph->getInputDataSizeByIndex(input_tensor_index);
71 }
72
73 int32_t Interpreter::getOutputDataSizeByIndex(int32_t output_tensor_index)
74 {
75   auto *runtime_graph = _runtime_module.getMainGraph();
76
77   return runtime_graph->getOutputDataSizeByIndex(output_tensor_index);
78 }
79
80 void Interpreter::allocateAndWriteInputTensor(int32_t input_tensor_index, const void *data,
81                                               size_t data_size)
82 {
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);
88
89   std::memcpy(tensor_data, data, data_size);
90 }
91
92 uint8_t *Interpreter::allocateInputTensor(int32_t input_tensor_index)
93 {
94   assert(input_tensor_index >= 0);
95
96   auto *runtime_graph = _runtime_module.getMainGraph();
97
98   return runtime_graph->configureGraphInput(input_tensor_index);
99 }
100
101 uint8_t *Interpreter::readOutputTensor(int32_t output_tensor_index)
102 {
103   auto *runtime_graph = _runtime_module.getMainGraph();
104
105   return runtime_graph->getOutputDataByIndex(output_tensor_index);
106 }
107
108 } // namespace luci_interpreter