ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnfw_api.cc
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#include "nnfw.h"
18#include "nnfw_experimental.h"
19#include "nnfw_version.h"
20
21#include "nnfw_session.h"
22
23// Double-check enum value changes
24
25#define STATIC_ASSERT_ENUM_CHECK(ENUM, VAL) static_assert((ENUM) == (VAL), #ENUM " has changed")
26
35
42
46
48
49#undef STATIC_ASSERT_ENUM_CHECK
50
51#define NNFW_RETURN_ERROR_IF_NULL(p) \
52 do \
53 { \
54 if ((p) == NULL) \
55 return NNFW_STATUS_UNEXPECTED_NULL; \
56 } while (0)
57
59
61{
62 delete session;
64}
65
67{
69 return session->load_model_from_path(path);
70}
71
73{
75 return session->prepare();
76}
77
79{
81 return session->run();
82}
83
85{
87 return session->run_async();
88}
89
91{
93 return session->await();
94}
95
96NNFW_STATUS nnfw_set_input(nnfw_session *session, uint32_t index, NNFW_TYPE type,
97 const void *buffer, size_t length)
98{
100 return session->set_input(index, type, buffer, length);
101}
102
103NNFW_STATUS nnfw_set_output(nnfw_session *session, uint32_t index, NNFW_TYPE type, void *buffer,
104 size_t length)
105{
107 return session->set_output(index, type, buffer, length);
108}
109
110NNFW_STATUS nnfw_input_size(nnfw_session *session, uint32_t *number)
111{
113 return session->input_size(number);
114}
115
116NNFW_STATUS nnfw_output_size(nnfw_session *session, uint32_t *number)
117{
119 return session->output_size(number);
120}
121
123{
125 return session->set_input_layout(index, layout);
126}
127
129{
131 return session->set_output_layout(index, layout);
132}
133
135 nnfw_tensorinfo *tensor_info)
136{
138 return session->input_tensorinfo(index, tensor_info);
139}
140
142 nnfw_tensorinfo *tensor_info)
143{
145 return session->output_tensorinfo(index, tensor_info);
146}
147
150{
152 return session->register_custom_operation(id, info->eval_function);
153}
154
156{
157 return nnfw_session::deprecated("nnfw_apply_tensorinfo: Deprecated");
158}
159
161 const nnfw_tensorinfo *tensor_info)
162{
164 return session->set_input_tensorinfo(index, tensor_info);
165}
166
168{
170 return session->set_available_backends(backends);
171}
172
173NNFW_STATUS nnfw_set_op_backend(nnfw_session *, const char *, const char *)
174{
175 return nnfw_session::deprecated("nnfw_set_op_backend: Deprecated");
176}
177
179{
180 (void)session;
181 switch (id)
182 {
184 if (val)
185 {
186 *val = NNFW_VERSION;
188 }
189 break;
190 default:
191 return NNFW_STATUS_ERROR;
192 }
193 // It should not be reached.
194 return NNFW_STATUS_ERROR;
195}
196
197NNFW_STATUS nnfw_input_tensorindex(nnfw_session *session, const char *tensorname, uint32_t *index)
198{
200 return session->input_tensorindex(tensorname, index);
201}
202
203NNFW_STATUS nnfw_output_tensorindex(nnfw_session *session, const char *tensorname, uint32_t *index)
204{
206 return session->output_tensorindex(tensorname, index);
207}
208
209NNFW_STATUS nnfw_set_backends_per_operation(nnfw_session *session, const char *backend_settings)
210{
212 return session->set_backends_per_operation(backend_settings);
213}
214
216{
217 return nnfw_session::deprecated("nnfw_prepare_pipeline: Deprecated");
218}
219
221{
222 return nnfw_session::deprecated("nnfw_push_pipeline_input: Deprecated");
223}
224
226{
227 return nnfw_session::deprecated("nnfw_pop_pipeline_output: Deprecated");
228}
229
231{
233 return session->set_workspace(dir);
234}
235
236// Training
237
239{
241 return session->train_get_traininfo(info);
242}
243
245{
247 return session->train_set_traininfo(info);
248}
249
251{
253 return session->train_prepare();
254}
255
258{
260 return session->train_input_tensorinfo(index, info);
261}
262
265{
267 return session->train_expected_tensorinfo(index, info);
268}
269
270NNFW_STATUS nnfw_train_set_input(nnfw_session *session, uint32_t index, const void *input,
271 const nnfw_tensorinfo *input_info)
272{
274 return session->train_set_input(index, input, input_info);
275}
276
277NNFW_STATUS nnfw_train_set_expected(nnfw_session *session, uint32_t index, const void *expected,
278 const nnfw_tensorinfo *expected_info)
279{
281 return session->train_set_expected(index, expected, expected_info);
282}
283
285 void *buffer, size_t length)
286{
288 return session->train_set_output(index, type, buffer, length);
289}
290
291NNFW_STATUS nnfw_train(nnfw_session *session, bool update_weights)
292{
294 return session->train_run(update_weights);
295}
296
297NNFW_STATUS nnfw_train_get_loss(nnfw_session *session, uint32_t index, float *loss)
298{
300 return session->train_get_loss(index, loss);
301}
302
304{
306 return session->train_export_circle(path);
307}
308
310{
312 return session->train_import_checkpoint(path);
313}
314
316{
318 return session->train_export_checkpoint(path);
319}
320
321// Quantization
322
324{
326 return session->set_quantization_type(qtype);
327}
328
330{
332 return session->set_quantized_model_path(path);
333}
334
336{
338 return session->quantize();
339}
340
342{
344 return session->set_codegen_model_path(path);
345}
346
347NNFW_STATUS nnfw_codegen(nnfw_session *session, const char *target, NNFW_CODEGEN_PREF pref)
348{
350 return session->codegen(target, pref);
351}
352
354{
356 return session->set_odc_param_minmax_records_count(minmax_records_count);
357}
358
360{
362 return session->delete_odc_minmax_file();
363}
364
367{
369 return session->run_with_auto_compilation(target, pref);
370}
371
372// Configuration
373
375 const char *value)
376{
378 return session->set_prepare_config(key, value);
379}
380
382{
384 return session->reset_prepare_config();
385}
386
388 const char *value)
389{
391 return session->set_execute_config(key, value);
392}
393
395{
397 return session->reset_execute_config();
398}
volatile const char info[]
This file describes runtime API.
NNFW_INFO_ID
Information ID for retrieving information on nnfw (e.g. version)
Definition nnfw.h:153
@ NNFW_INFO_ID_VERSION
Definition nnfw.h:157
@ NNFW_TYPE_TENSOR_INT64
Definition nnfw.h:86
@ NNFW_TYPE_TENSOR_QUANT16_SYMM_SIGNED
Definition nnfw.h:102
@ NNFW_TYPE_TENSOR_QUANT8_ASYMM_SIGNED
Definition nnfw.h:93
@ NNFW_TYPE_TENSOR_BOOL
Definition nnfw.h:80
@ NNFW_TYPE_TENSOR_QUANT8_ASYMM
Definition nnfw.h:78
@ NNFW_TYPE_TENSOR_UINT8
Definition nnfw.h:83
NNFW_LAYOUT
Data format of a tensor.
Definition nnfw.h:134
@ NNFW_LAYOUT_CHANNELS_LAST
Definition nnfw.h:141
@ NNFW_LAYOUT_CHANNELS_FIRST
Definition nnfw.h:146
@ NNFW_LAYOUT_NONE
Definition nnfw.h:136
NNFW_STATUS nnfw_create_session(nnfw_session **session)
Create a new session instance.
Definition nnfw_api.cc:58
NNFW_STATUS nnfw_load_model_from_file(nnfw_session *session, const char *path)
Load model from nnpackage file or directory.
Definition nnfw_api.cc:66
NNFW_STATUS nnfw_set_input(nnfw_session *session, uint32_t index, NNFW_TYPE type, const void *buffer, size_t length)
Set input buffer.
Definition nnfw_api.cc:96
NNFW_STATUS nnfw_await(nnfw_session *session)
Wait for asynchronous run to finish.
Definition nnfw_api.cc:90
NNFW_STATUS nnfw_close_session(nnfw_session *session)
Close a session instance.
Definition nnfw_api.cc:60
#define STATIC_ASSERT_ENUM_CHECK(ENUM, VAL)
Definition nnfw_api.cc:25
NNFW_STATUS nnfw_train_get_loss(nnfw_session *session, uint32_t index, float *loss)
Get loss value for expected output.
Definition nnfw_api.cc:297
NNFW_STATUS nnfw_register_custom_op_info(nnfw_session *session, const char *id, custom_kernel_registration_info *info)
Definition nnfw_api.cc:148
NNFW_STATUS nnfw_query_info_u32(nnfw_session *session, NNFW_INFO_ID id, uint32_t *val)
Retrieve uint32 type of nnfw information for given information ID.
Definition nnfw_api.cc:178
NNFW_STATUS nnfw_pop_pipeline_output(nnfw_session *, void *)
Get last outputs of partitioned model in session.
Definition nnfw_api.cc:225
NNFW_STATUS nnfw_run_with_auto_compilation(nnfw_session *session, const char *target, NNFW_CODEGEN_PREF pref)
Run inference with auto compilation.
Definition nnfw_api.cc:365
#define NNFW_RETURN_ERROR_IF_NULL(p)
Definition nnfw_api.cc:51
NNFW_STATUS nnfw_train_export_checkpoint(nnfw_session *session, const char *path)
Export circle checkpoint.
Definition nnfw_api.cc:315
NNFW_STATUS nnfw_train_set_expected(nnfw_session *session, uint32_t index, const void *expected, const nnfw_tensorinfo *expected_info)
Set training expected output.
Definition nnfw_api.cc:277
NNFW_STATUS nnfw_prepare_pipeline(nnfw_session *, const char *)
Prepare session to be ready for inference.
Definition nnfw_api.cc:215
NNFW_STATUS nnfw_run_async(nnfw_session *session)
Run inference asynchronously.
Definition nnfw_api.cc:84
NNFW_STATUS nnfw_output_tensorinfo(nnfw_session *session, uint32_t index, nnfw_tensorinfo *tensor_info)
Get i-th output tensor info.
Definition nnfw_api.cc:141
NNFW_STATUS nnfw_set_available_backends(nnfw_session *session, const char *backends)
Set available backends.
Definition nnfw_api.cc:167
NNFW_STATUS nnfw_input_tensorinfo(nnfw_session *session, uint32_t index, nnfw_tensorinfo *tensor_info)
Get i-th input tensor info.
Definition nnfw_api.cc:134
NNFW_STATUS nnfw_output_size(nnfw_session *session, uint32_t *number)
Get the number of outputs.
Definition nnfw_api.cc:116
NNFW_STATUS nnfw_push_pipeline_input(nnfw_session *, void *, void *)
Set input buffer.
Definition nnfw_api.cc:220
NNFW_STATUS nnfw_output_tensorindex(nnfw_session *session, const char *tensorname, uint32_t *index)
Get the input tensor index by name.
Definition nnfw_api.cc:203
NNFW_STATUS nnfw_set_workspace(nnfw_session *session, const char *dir)
Set runtime's workspace directory.
Definition nnfw_api.cc:230
NNFW_STATUS nnfw_input_size(nnfw_session *session, uint32_t *number)
Get the number of inputs.
Definition nnfw_api.cc:110
NNFW_STATUS nnfw_set_input_tensorinfo(nnfw_session *session, uint32_t index, const nnfw_tensorinfo *tensor_info)
Set input model's tensor info for resizing.
Definition nnfw_api.cc:160
NNFW_STATUS nnfw_train_prepare(nnfw_session *session)
Prepare session to be ready for training.
Definition nnfw_api.cc:250
NNFW_STATUS nnfw_apply_tensorinfo(nnfw_session *, uint32_t, nnfw_tensorinfo)
Apply i-th input's tensor info to resize input tensor.
Definition nnfw_api.cc:155
NNFW_STATUS nnfw_train(nnfw_session *session, bool update_weights)
Train the model.
Definition nnfw_api.cc:291
NNFW_STATUS nnfw_set_quantization_type(nnfw_session *session, NNFW_QUANTIZE_TYPE qtype)
Set quantization type.
Definition nnfw_api.cc:323
NNFW_STATUS nnfw_train_set_traininfo(nnfw_session *session, const nnfw_train_info *info)
Set training information.
Definition nnfw_api.cc:244
NNFW_STATUS nnfw_train_get_traininfo(nnfw_session *session, nnfw_train_info *info)
Get training information.
Definition nnfw_api.cc:238
NNFW_STATUS nnfw_input_tensorindex(nnfw_session *session, const char *tensorname, uint32_t *index)
Get the input tensor index by name.
Definition nnfw_api.cc:197
NNFW_STATUS nnfw_run(nnfw_session *session)
Run inference.
Definition nnfw_api.cc:78
NNFW_STATUS nnfw_set_odc_param_minmax_records_count(nnfw_session *session, int minmax_records_count)
Set MinMax records count in auto compilation mode with on-device compiler.
Definition nnfw_api.cc:353
NNFW_STATUS nnfw_prepare(nnfw_session *session)
Prepare session to be ready for inference.
Definition nnfw_api.cc:72
NNFW_STATUS nnfw_codegen(nnfw_session *session, const char *target, NNFW_CODEGEN_PREF pref)
Generate target-dependent code.
Definition nnfw_api.cc:347
NNFW_STATUS nnfw_quantize(nnfw_session *session)
Quantize circle model.
Definition nnfw_api.cc:335
NNFW_STATUS nnfw_set_op_backend(nnfw_session *, const char *, const char *)
Set the operation's backend.
Definition nnfw_api.cc:173
NNFW_STATUS nnfw_train_set_input(nnfw_session *session, uint32_t index, const void *input, const nnfw_tensorinfo *input_info)
Set training input.
Definition nnfw_api.cc:270
NNFW_STATUS nnfw_reset_prepare_config(nnfw_session *session)
Reset prepare configurations.
Definition nnfw_api.cc:381
NNFW_STATUS nnfw_reset_execute_config(nnfw_session *session)
Reset execution (run or train) configurations.
Definition nnfw_api.cc:394
NNFW_STATUS nnfw_train_set_output(nnfw_session *session, uint32_t index, NNFW_TYPE type, void *buffer, size_t length)
Set training output buffer.
Definition nnfw_api.cc:284
NNFW_STATUS nnfw_set_execute_config(nnfw_session *session, const NNFW_RUN_CONFIG key, const char *value)
Set execution (run or train) configuration.
Definition nnfw_api.cc:387
NNFW_STATUS nnfw_set_output(nnfw_session *session, uint32_t index, NNFW_TYPE type, void *buffer, size_t length)
Set output buffer.
Definition nnfw_api.cc:103
NNFW_STATUS nnfw_train_import_checkpoint(nnfw_session *session, const char *path)
Import circle checkpoint.
Definition nnfw_api.cc:309
NNFW_STATUS nnfw_set_backends_per_operation(nnfw_session *session, const char *backend_settings)
Set the backend for each operation in the session.
Definition nnfw_api.cc:209
NNFW_STATUS nnfw_set_prepare_config(nnfw_session *session, const NNFW_PREPARE_CONFIG key, const char *value)
Set prepare configuration.
Definition nnfw_api.cc:374
NNFW_STATUS nnfw_odc_delete_minmax_file(nnfw_session *session)
Delete MinMax file for on-device compiler.
Definition nnfw_api.cc:359
NNFW_STATUS nnfw_set_codegen_model_path(nnfw_session *session, const char *path)
Set exported codegen model path.
Definition nnfw_api.cc:341
NNFW_STATUS nnfw_set_quantized_model_path(nnfw_session *session, const char *path)
Set exported quantized model path.
Definition nnfw_api.cc:329
NNFW_STATUS nnfw_train_input_tensorinfo(nnfw_session *session, uint32_t index, nnfw_tensorinfo *info)
Get the training model input information.
Definition nnfw_api.cc:256
NNFW_STATUS nnfw_train_expected_tensorinfo(nnfw_session *session, uint32_t index, nnfw_tensorinfo *info)
Get the training model expected output information.
Definition nnfw_api.cc:263
NNFW_STATUS nnfw_set_output_layout(nnfw_session *session, uint32_t index, NNFW_LAYOUT layout)
Set the layout of an output.
Definition nnfw_api.cc:128
NNFW_STATUS nnfw_train_export_circle(nnfw_session *session, const char *path)
Export current training model into circle model.
Definition nnfw_api.cc:303
NNFW_STATUS nnfw_set_input_layout(nnfw_session *session, uint32_t index, NNFW_LAYOUT layout)
Set the layout of an input.
Definition nnfw_api.cc:122
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)
#define NNFW_VERSION
NNFW_STATUS
Result values returned from a call to an API function.
Definition onert-micro.h:86
@ NNFW_STATUS_INVALID_STATE
Definition onert-micro.h:97
@ NNFW_STATUS_UNEXPECTED_NULL
Definition onert-micro.h:95
@ NNFW_STATUS_NO_ERROR
Definition onert-micro.h:88
@ NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE
@ NNFW_STATUS_ERROR
Definition onert-micro.h:93
@ NNFW_STATUS_OUT_OF_MEMORY
Definition onert-micro.h:99
NNFW_TYPE
Definition onert-micro.h:75
@ NNFW_TYPE_TENSOR_INT32
Definition onert-micro.h:79
@ NNFW_TYPE_TENSOR_FLOAT32
Definition onert-micro.h:77
static NNFW_STATUS deprecated(const char *msg)
static NNFW_STATUS create(nnfw_session **session)
Factory method. It creates and initialize nnfw_session.
tensor info describes the type and shape of tensors
Training information to prepare training.