ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Session.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 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 __API_NNFW_SESSION_H__
18#define __API_NNFW_SESSION_H__
19
20#include "nnfw.h"
21
24#include "compiler/ICompiler.h"
25#include "exec/Execution.h"
26#include "ir/NNPkg.h"
28#include "odc/CodegenManager.h"
29#include "odc/QuantizeManager.h"
30
31#include <util/TracingCtx.h>
32
33#include <filesystem>
34#include <memory>
35#include <string>
36#include <vector>
37
38namespace onert::api
39{
40
41struct Session
42{
43private:
79 enum class State
80 {
81 INITIALIZED, //< Session is initialized and nothing has done to it
82 MODEL_LOADED, //< Model is loaded
83 PREPARED, //< Prepared(compiled) for execution
84 RUNNING, //< Execution is in progress (only for asynchronous execution)
85 FINISHED_RUN, //< Executed at least once
86 PREPARED_TRAINING, //< Prepared for training
87 FINISHED_TRAINING //< Trained at least once
88 };
89
90 enum class AutoCompilationState
91 {
92 INITIAL_STATE, //< Initial state
93 QUANTIZED_MODEL_LOADED, //< Qunatized model is loaded
94 COMPILED_MODEL_LOADED //< Compiled model is loaded
95 };
96
97public:
103 static NNFW_STATUS create(Session **session);
104
105private:
106 Session();
107
108public:
110 NNFW_STATUS load_model_from_path(const char *path);
113
116
117 NNFW_STATUS set_input(uint32_t index, NNFW_TYPE type, const void *buffer, size_t length);
118 NNFW_STATUS set_output(uint32_t index, NNFW_TYPE type, void *buffer, size_t length);
119
120 NNFW_STATUS input_size(uint32_t *number);
121 NNFW_STATUS output_size(uint32_t *number);
122
123 NNFW_STATUS set_input_layout(uint32_t index, NNFW_LAYOUT layout);
124 NNFW_STATUS set_output_layout(uint32_t index, NNFW_LAYOUT layout);
125 NNFW_STATUS set_input_type(uint32_t index, NNFW_TYPE type);
126 NNFW_STATUS set_output_type(uint32_t index, NNFW_TYPE type);
127
128 NNFW_STATUS set_input_tensorinfo(uint32_t index, const nnfw_tensorinfo *ti);
129
130 NNFW_STATUS input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti);
131 NNFW_STATUS output_tensorinfo(uint32_t index, nnfw_tensorinfo *ti);
132
134
135 NNFW_STATUS set_workspace(const char *dir);
136
137 NNFW_STATUS configure_signature(const char *signature);
138 NNFW_STATUS set_signature_run(const char *signature);
139
140 NNFW_STATUS get_last_error_message(char *buffer, size_t length) const;
141
142 NNFW_STATUS deprecated(const char *msg);
143
144 //
145 // Internal-only API
146 //
147
148 NNFW_STATUS set_config(const char *key, const char *value);
149 NNFW_STATUS get_config(const char *key, char *value, size_t value_size);
150 NNFW_STATUS load_circle_from_buffer(uint8_t *buffer, size_t size);
151 NNFW_STATUS get_output(uint32_t index, nnfw_tensorinfo *out_info, const void **out_buffer);
152
153 //
154 // Experimental API
155 //
156 NNFW_STATUS register_custom_operation(const std::string &id, nnfw_custom_eval eval_func);
157 NNFW_STATUS input_tensorindex(const char *tensorname, uint32_t *index);
158 NNFW_STATUS output_tensorindex(const char *tensorname, uint32_t *index);
159
160 // Run inference with auto compilation
162 // Set odc parameter: minmax_records_count for quantization in auto compilation mode
163 NNFW_STATUS set_odc_param_minmax_records_count(int minmax_records_count);
164 // delete MinMax File of on-device compiler
166
171 NNFW_STATUS set_backends_per_operation(const char *backend_settings);
172
178 NNFW_STATUS train_set_input(uint32_t index, const void *input,
180 NNFW_STATUS train_set_expected(uint32_t index, const void *expected,
181 const nnfw_tensorinfo *expected_tensorinfo);
182 NNFW_STATUS train_set_output(uint32_t index, NNFW_TYPE type, void *buffer, size_t length);
183 NNFW_STATUS train_run(bool update_weights);
184 NNFW_STATUS train_get_loss(uint32_t index, float *loss);
185 NNFW_STATUS train_export_circle(const char *path);
186 NNFW_STATUS train_export_circleplus(const char *path);
187 NNFW_STATUS train_import_checkpoint(const char *path);
188 NNFW_STATUS train_export_checkpoint(const char *path);
189
191 NNFW_STATUS set_quantized_model_path(const char *path);
193
194 NNFW_STATUS set_codegen_model_path(const char *path);
195 NNFW_STATUS codegen(const char *target, NNFW_CODEGEN_PREF pref);
196
197 NNFW_STATUS set_prepare_config(const NNFW_PREPARE_CONFIG key, const char *value);
199 NNFW_STATUS set_execute_config(const NNFW_RUN_CONFIG key, const char *value);
201
202private:
203 const onert::ir::IGraph *primary_subgraph();
204 uint32_t getInputSize();
205 uint32_t getOutputSize();
206 NNFW_STATUS loadModelFile(const std::string &model_file_path, const std::string &model_type);
207 NNFW_STATUS getTensorIndexImpl(const onert::ir::IGraph &graph, const char *tensorname,
208 uint32_t *index, bool is_input);
209 void setLastErrorMessage(std::string message);
210
211 bool isStateInitialized();
212 bool isStateModelLoaded();
213 bool isStatePrepared();
214 bool isStateRunning();
215 bool isStateFinishedRun();
216 bool isStatePreparedOrFinishedRun();
217 bool isStatePreparedTraining();
218 bool isStateFinishedTraining();
219 bool isStatePreparedOrFinishedTraining();
220
221private:
222 State _state{State::INITIALIZED};
223 std::unique_ptr<onert::ir::NNPkg> _nnpkg;
224 std::unique_ptr<onert::compiler::CompilerOptions> _coptions;
225 std::unique_ptr<onert::compiler::CompilerArtifact> _compiler_artifact;
226 std::unique_ptr<onert::exec::Execution> _execution;
227 std::shared_ptr<onert::api::CustomKernelRegistry> _kernel_registry;
228 std::unique_ptr<onert::ir::train::TrainingInfo> _train_info;
229 std::unique_ptr<onert::odc::QuantizeManager> _quant_manager;
230 std::unique_ptr<onert::odc::CodegenManager> _codegen_manager;
231 AutoCompilationState _autoCompilationState = AutoCompilationState::INITIAL_STATE;
232 // Remember path to loaded original model
233 // It may be used for on-device compiler / on-device training.
234 //
235 // If necessary, we may replace _model_path to _model_origin like:
236 //
237 // union _model_origin {
238 // const char *path;
239 // const uint8 *buf;
240 // }
241 std::filesystem::path _model_path;
242 std::unordered_map<onert::ir::SubgraphIndex, std::string> _signature_map;
243 onert::ir::SubgraphIndex _selected_signature;
244 std::string _last_error_message;
245};
246
247} // namespace onert::api
248
249#endif // __API_NNFW_SESSION_H__
int32_t type
This file defines execution.
This file contains ICompiler class to define and run compilation phase.
volatile const char info[]
This file describes runtime API.
NNFW_LAYOUT
Data format of a tensor.
Definition nnfw.h:134
void(* nnfw_custom_eval)(nnfw_custom_kernel_params *params, char *userdata, size_t userdata_size)
NNFW_CODEGEN_PREF
Preference for target-dependent code generation.
NNFW_QUANTIZE_TYPE
Convert between training mode and inference mode.
NNFW_RUN_CONFIG
Configuration key for execution.
NNFW_PREPARE_CONFIG
Configuration key for prepare (compile and schedule)
int32_t size[5]
Definition Slice.cpp:35
NNFW_STATUS
Result values returned from a call to an API function.
Definition onert-micro.h:86
NNFW_TYPE
Definition onert-micro.h:75
tensor info describes the type and shape of tensors
Training information to prepare training.
NNFW_STATUS reset_execute_config()
Definition Session.cc:2367
NNFW_STATUS get_last_error_message(char *buffer, size_t length) const
Definition Session.cc:1131
NNFW_STATUS set_codegen_model_path(const char *path)
Definition Session.cc:2213
NNFW_STATUS run_async()
Definition Session.cc:507
NNFW_STATUS train_prepare()
Definition Session.cc:1719
NNFW_STATUS set_output_type(uint32_t index, NNFW_TYPE type)
Definition Session.cc:773
NNFW_STATUS set_signature_run(const char *signature)
Definition Session.cc:1102
NNFW_STATUS train_set_input(uint32_t index, const void *input, const nnfw_tensorinfo *input_tensorinfo)
Definition Session.cc:1791
NNFW_STATUS train_get_loss(uint32_t index, float *loss)
Definition Session.cc:1945
NNFW_STATUS await()
Definition Session.cc:522
NNFW_STATUS train_export_checkpoint(const char *path)
Definition Session.cc:2064
NNFW_STATUS load_model_from_path(const char *path)
Definition Session.cc:296
NNFW_STATUS reset_prepare_config()
Definition Session.cc:2323
NNFW_STATUS set_input_type(uint32_t index, NNFW_TYPE type)
Definition Session.cc:729
NNFW_STATUS train_set_output(uint32_t index, NNFW_TYPE type, void *buffer, size_t length)
Definition Session.cc:1883
NNFW_STATUS set_backends_per_operation(const char *backend_settings)
Set backends with string-encoded mapping from operation index to backend type (cpu,...
Definition Session.cc:1457
NNFW_STATUS prepare()
Definition Session.cc:446
NNFW_STATUS set_input_layout(uint32_t index, NNFW_LAYOUT layout)
Definition Session.cc:642
NNFW_STATUS train_get_traininfo(nnfw_train_info *info)
Definition Session.cc:1497
NNFW_STATUS set_workspace(const char *dir)
Definition Session.cc:1052
NNFW_STATUS load_circle_from_buffer(uint8_t *buffer, size_t size)
Definition Session.cc:260
NNFW_STATUS deprecated(const char *msg)
Definition Session.cc:1145
NNFW_STATUS input_tensorindex(const char *tensorname, uint32_t *index)
Definition Session.cc:1447
NNFW_STATUS set_available_backends(const char *backends)
Definition Session.cc:1018
NNFW_STATUS output_tensorindex(const char *tensorname, uint32_t *index)
Definition Session.cc:1452
NNFW_STATUS set_prepare_config(const NNFW_PREPARE_CONFIG key, const char *value)
Definition Session.cc:2299
NNFW_STATUS set_output(uint32_t index, NNFW_TYPE type, void *buffer, size_t length)
Definition Session.cc:563
NNFW_STATUS set_input(uint32_t index, NNFW_TYPE type, const void *buffer, size_t length)
Definition Session.cc:537
NNFW_STATUS input_size(uint32_t *number)
Definition Session.cc:590
NNFW_STATUS set_config(const char *key, const char *value)
Definition Session.cc:1151
NNFW_STATUS train_export_circleplus(const char *path)
Definition Session.cc:2008
NNFW_STATUS train_input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti)
Definition Session.cc:1757
NNFW_STATUS get_output(uint32_t index, nnfw_tensorinfo *out_info, const void **out_buffer)
Definition Session.cc:963
NNFW_STATUS train_set_traininfo(const nnfw_train_info *info)
Definition Session.cc:1615
NNFW_STATUS train_run(bool update_weights)
Definition Session.cc:1911
NNFW_STATUS set_quantized_model_path(const char *path)
Definition Session.cc:2163
NNFW_STATUS set_quantization_type(NNFW_QUANTIZE_TYPE qtype)
Definition Session.cc:2121
NNFW_STATUS output_size(uint32_t *number)
Definition Session.cc:616
NNFW_STATUS run_with_auto_compilation(const char *target, NNFW_CODEGEN_PREF pref)
Definition Session.cc:2414
NNFW_STATUS get_config(const char *key, char *value, size_t value_size)
Definition Session.cc:1281
NNFW_STATUS run()
Definition Session.cc:479
NNFW_STATUS register_custom_operation(const std::string &id, nnfw_custom_eval eval_func)
Definition Session.cc:957
NNFW_STATUS output_tensorinfo(uint32_t index, nnfw_tensorinfo *ti)
Definition Session.cc:912
NNFW_STATUS delete_odc_minmax_file()
Definition Session.cc:2398
NNFW_STATUS set_output_layout(uint32_t index, NNFW_LAYOUT layout)
Definition Session.cc:685
NNFW_STATUS train_export_circle(const char *path)
Definition Session.cc:1979
NNFW_STATUS quantize()
Definition Session.cc:2185
NNFW_STATUS train_set_expected(uint32_t index, const void *expected, const nnfw_tensorinfo *expected_tensorinfo)
Definition Session.cc:1834
NNFW_STATUS train_import_checkpoint(const char *path)
Definition Session.cc:2037
NNFW_STATUS set_odc_param_minmax_records_count(int minmax_records_count)
Definition Session.cc:2382
static NNFW_STATUS create(Session **session)
Factory method. It creates and initialize Session.
Definition Session.cc:231
NNFW_STATUS input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti)
Definition Session.cc:869
NNFW_STATUS set_execute_config(const NNFW_RUN_CONFIG key, const char *value)
Definition Session.cc:2336
NNFW_STATUS configure_signature(const char *signature)
Definition Session.cc:1073
NNFW_STATUS train_expected_tensorinfo(uint32_t index, nnfw_tensorinfo *ti)
Definition Session.cc:1774
NNFW_STATUS set_input_tensorinfo(uint32_t index, const nnfw_tensorinfo *ti)
Definition Session.cc:817