ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnfw_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 <thread>
37#include <vector>
38
39struct nnfw_session
40{
41private:
77 enum class State
78 {
79 INITIALIZED, //< Session is initialized and nothing has done to it
80 MODEL_LOADED, //< Model is loaded
81 PREPARED, //< Prepared(compiled) for execution
82 RUNNING, //< Execution is in progress (only for asynchronous execution)
83 FINISHED_RUN, //< Executed at least once
84 PREPARED_TRAINING, //< Prepared for training
85 FINISHED_TRAINING //< Trained at least once
86 };
87
88 enum class AutoCompilationState
89 {
90 INITIAL_STATE, //< Initial state
91 QUANTIZED_MODEL_LOADED, //< Qunatized model is loaded
92 COMPILED_MODEL_LOADED //< Compiled model is loaded
93 };
94
95public:
101 static NNFW_STATUS create(nnfw_session **session);
102
103private:
104 nnfw_session();
105
106public:
108 NNFW_STATUS load_model_from_path(const char *path);
111
114
115 NNFW_STATUS set_input(uint32_t index, NNFW_TYPE type, const void *buffer, size_t length);
116 NNFW_STATUS set_output(uint32_t index, NNFW_TYPE type, void *buffer, size_t length);
117
118 NNFW_STATUS input_size(uint32_t *number);
119 NNFW_STATUS output_size(uint32_t *number);
120
121 NNFW_STATUS set_input_layout(uint32_t index, NNFW_LAYOUT layout);
122 NNFW_STATUS set_output_layout(uint32_t index, NNFW_LAYOUT layout);
123
124 NNFW_STATUS set_input_tensorinfo(uint32_t index, const nnfw_tensorinfo *ti);
125
126 NNFW_STATUS input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti);
127 NNFW_STATUS output_tensorinfo(uint32_t index, nnfw_tensorinfo *ti);
128
130
131 NNFW_STATUS set_workspace(const char *dir);
132
133 static NNFW_STATUS deprecated(const char *msg);
134
135 //
136 // Internal-only API
137 //
138
139 NNFW_STATUS set_config(const char *key, const char *value);
140 NNFW_STATUS get_config(const char *key, char *value, size_t value_size);
141 NNFW_STATUS load_circle_from_buffer(uint8_t *buffer, size_t size);
142
143 //
144 // Experimental API
145 //
146 NNFW_STATUS register_custom_operation(const std::string &id, nnfw_custom_eval eval_func);
147 NNFW_STATUS input_tensorindex(const char *tensorname, uint32_t *index);
148 NNFW_STATUS output_tensorindex(const char *tensorname, uint32_t *index);
149
150 // Run inference with auto compilation
152 // Set odc parameter: minmax_records_count for quantization in auto compilation mode
153 NNFW_STATUS set_odc_param_minmax_records_count(int minmax_records_count);
154 // delete MinMax File of on-device compiler
156
161 NNFW_STATUS set_backends_per_operation(const char *backend_settings);
162
168 NNFW_STATUS train_set_input(uint32_t index, const void *input,
170 NNFW_STATUS train_set_expected(uint32_t index, const void *expected,
171 const nnfw_tensorinfo *expected_tensorinfo);
172 NNFW_STATUS train_set_output(uint32_t index, NNFW_TYPE type, void *buffer, size_t length);
173 NNFW_STATUS train_run(bool update_weights);
174 NNFW_STATUS train_get_loss(uint32_t index, float *loss);
176 NNFW_STATUS train_export_circleplus(const char *path);
179
181 NNFW_STATUS set_quantized_model_path(const char *path);
183
184 NNFW_STATUS set_codegen_model_path(const char *path);
185 NNFW_STATUS codegen(const char *target, NNFW_CODEGEN_PREF pref);
186
187 NNFW_STATUS set_prepare_config(const NNFW_PREPARE_CONFIG key, const char *value);
189 NNFW_STATUS set_execute_config(const NNFW_RUN_CONFIG key, const char *value);
191
192private:
193 const onert::ir::IGraph *primary_subgraph();
194 uint32_t getInputSize();
195 uint32_t getOutputSize();
196 NNFW_STATUS loadModelFile(const std::string &model_file_path, const std::string &model_type);
197
198 bool isStateInitialized();
199 bool isStateModelLoaded();
200 bool isStatePrepared();
201 bool isStateRunning();
202 bool isStateFinishedRun();
203 bool isStatePreparedOrFinishedRun();
204 bool isStatePreparedTraining();
205 bool isStateFinishedTraining();
206 bool isStatePreparedOrFinishedTraining();
207
208private:
209 State _state{State::INITIALIZED};
210 std::shared_ptr<onert::ir::NNPkg> _nnpkg;
211 std::unique_ptr<onert::compiler::CompilerOptions> _coptions;
212 std::shared_ptr<onert::compiler::CompilerArtifact> _compiler_artifact;
213 std::unique_ptr<onert::exec::Execution> _execution;
214 std::shared_ptr<onert::api::CustomKernelRegistry> _kernel_registry;
215 std::vector<std::thread> _threads;
216 std::unique_ptr<onert::ir::train::TrainingInfo> _train_info;
217 std::unique_ptr<onert::odc::QuantizeManager> _quant_manager;
218 std::unique_ptr<onert::odc::CodegenManager> _codegen_manager;
219 AutoCompilationState _autoCompilationState = AutoCompilationState::INITIAL_STATE;
220 // Remember path to loaded original model
221 // It may be used for on-device compiler / on-device training.
222 //
223 // If necessary, we may replace _model_path to _model_origin like:
224 //
225 // union _model_origin {
226 // const char *path;
227 // const uint8 *buf;
228 // }
229 std::filesystem::path _model_path;
230};
231
232#endif // __API_NNFW_SESSION_H__
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
NNFW_STATUS train_prepare()
NNFW_STATUS run()
NNFW_STATUS train_set_output(uint32_t index, NNFW_TYPE type, void *buffer, size_t length)
NNFW_STATUS train_get_traininfo(nnfw_train_info *info)
NNFW_STATUS set_config(const char *key, const char *value)
NNFW_STATUS train_set_expected(uint32_t index, void *expected)
NNFW_STATUS delete_odc_minmax_file()
NNFW_STATUS set_odc_param_minmax_records_count(int minmax_records_count)
NNFW_STATUS train_run(bool update_weights)
NNFW_STATUS input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti)
NNFW_STATUS output_tensorinfo(uint32_t index, nnfw_tensorinfo *ti)
NNFW_STATUS set_input_tensorinfo(uint32_t index, const nnfw_tensorinfo *ti)
NNFW_STATUS input_tensorindex(const char *tensorname, uint32_t *index)
NNFW_STATUS train_export_checkpoint(const char *path)
NNFW_STATUS set_output(uint32_t index, NNFW_TYPE type, void *buffer, size_t length)
NNFW_STATUS train_import_checkpoint(const char *path)
NNFW_STATUS set_workspace(const char *dir)
static NNFW_STATUS deprecated(const char *msg)
NNFW_STATUS train_expected_tensorinfo(uint32_t index, nnfw_tensorinfo *ti)
NNFW_STATUS reset_prepare_config()
NNFW_STATUS set_codegen_model_path(const char *path)
NNFW_STATUS train_set_input(uint32_t index, void *input)
NNFW_STATUS reset_execute_config()
NNFW_STATUS set_input(uint32_t index, NNFW_TYPE type, const void *buffer, size_t length)
NNFW_STATUS output_size(uint32_t *number)
NNFW_STATUS train_get_loss(uint32_t index, float *loss)
NNFW_STATUS prepare()
NNFW_STATUS load_model_from_path(const char *path)
NNFW_STATUS set_backends_per_operation(const char *backend_settings)
Set backends with string-encoded mapping from operation index to backend type (cpu,...
NNFW_STATUS set_quantization_type(NNFW_QUANTIZE_TYPE qtype)
NNFW_STATUS input_size(uint32_t *number)
NNFW_STATUS run_async()
NNFW_STATUS set_prepare_config(const NNFW_PREPARE_CONFIG key, const char *value)
NNFW_STATUS set_available_backends(const char *backends)
NNFW_STATUS await()
NNFW_STATUS get_config(const char *key, char *value, size_t value_size)
NNFW_STATUS codegen(const char *target, NNFW_CODEGEN_PREF pref)
NNFW_STATUS set_execute_config(const NNFW_RUN_CONFIG key, const char *value)
NNFW_STATUS run_with_auto_compilation(const char *target, NNFW_CODEGEN_PREF pref)
NNFW_STATUS set_output_layout(uint32_t index, NNFW_LAYOUT layout)
NNFW_STATUS train_input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti)
NNFW_STATUS register_custom_operation(const std::string &id, nnfw_custom_eval eval_func)
NNFW_STATUS train_export_circle(const char *path)
NNFW_STATUS output_tensorindex(const char *tensorname, uint32_t *index)
NNFW_STATUS set_quantized_model_path(const char *path)
NNFW_STATUS load_circle_from_buffer(uint8_t *buffer, size_t size)
NNFW_STATUS train_export_circleplus(const char *path)
NNFW_STATUS set_input_layout(uint32_t index, NNFW_LAYOUT layout)
static NNFW_STATUS create(nnfw_session **session)
Factory method. It creates and initialize nnfw_session.
NNFW_STATUS train_set_traininfo(const nnfw_train_info *info)
NNFW_STATUS quantize()
tensor info describes the type and shape of tensors
Training information to prepare training.