ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
nnfw_api_wrapper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 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_api_wrapper.h"
18
19#include <iostream>
20
21namespace onert::api::python
22{
23
24namespace py = pybind11;
25
27{
28 switch (status)
29 {
31 break;
33 std::cout << "[ERROR]\tNNFW_STATUS_ERROR\n";
34 exit(1);
36 std::cout << "[ERROR]\tNNFW_STATUS_UNEXPECTED_NULL\n";
37 exit(1);
39 std::cout << "[ERROR]\tNNFW_STATUS_INVALID_STATE\n";
40 exit(1);
42 std::cout << "[ERROR]\tNNFW_STATUS_OUT_OF_MEMORY\n";
43 exit(1);
45 std::cout << "[ERROR]\tNNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE\n";
46 exit(1);
48 std::cout << "[ERROR]\tNNFW_STATUS_DEPRECATED_API\n";
49 exit(1);
50 }
51}
52
53NNFW_LAYOUT getLayout(const char *layout)
54{
55 if (!strcmp(layout, "NCHW"))
56 {
58 }
59 else if (!strcmp(layout, "NHWC"))
60 {
62 }
63 else if (!strcmp(layout, "NONE"))
64 {
66 }
67 else
68 {
69 std::cout << "[ERROR]\tLAYOUT_TYPE\n";
70 exit(1);
71 }
72}
73
74NNFW_TYPE getType(const char *type)
75{
76 if (!strcmp(type, "float32"))
77 {
79 }
80 else if (!strcmp(type, "int32"))
81 {
83 }
84 else if (!strcmp(type, "uint8"))
85 {
86 return NNFW_TYPE::NNFW_TYPE_TENSOR_UINT8;
87 // return NNFW_TYPE::NNFW_TYPE_TENSOR_QUANT8_ASYMM;
88 }
89 else if (!strcmp(type, "bool"))
90 {
91 return NNFW_TYPE::NNFW_TYPE_TENSOR_BOOL;
92 }
93 else if (!strcmp(type, "int64"))
94 {
95 return NNFW_TYPE::NNFW_TYPE_TENSOR_INT64;
96 }
97 else if (!strcmp(type, "int8"))
98 {
99 return NNFW_TYPE::NNFW_TYPE_TENSOR_QUANT8_ASYMM_SIGNED;
100 }
101 else if (!strcmp(type, "int16"))
102 {
103 return NNFW_TYPE::NNFW_TYPE_TENSOR_QUANT16_SYMM_SIGNED;
104 }
105 else
106 {
107 std::cout << "[ERROR] String to NNFW_TYPE Failure\n";
108 exit(1);
109 }
110}
111
112const char *getStringType(NNFW_TYPE type)
113{
114 switch (type)
115 {
117 return "float32";
119 return "int32";
120 case NNFW_TYPE::NNFW_TYPE_TENSOR_QUANT8_ASYMM:
121 case NNFW_TYPE::NNFW_TYPE_TENSOR_UINT8:
122 return "uint8";
123 case NNFW_TYPE::NNFW_TYPE_TENSOR_BOOL:
124 return "bool";
125 case NNFW_TYPE::NNFW_TYPE_TENSOR_INT64:
126 return "int64";
127 case NNFW_TYPE::NNFW_TYPE_TENSOR_QUANT8_ASYMM_SIGNED:
128 return "int8";
129 case NNFW_TYPE::NNFW_TYPE_TENSOR_QUANT16_SYMM_SIGNED:
130 return "int16";
131 default:
132 std::cout << "[ERROR] NNFW_TYPE to String Failure\n";
133 exit(1);
134 }
135}
136
137uint64_t num_elems(const nnfw_tensorinfo *tensor_info)
138{
139 uint64_t n = 1;
140 for (int32_t i = 0; i < tensor_info->rank; ++i)
141 {
142 n *= tensor_info->dims[i];
143 }
144 return n;
145}
146
147py::list get_dims(const tensorinfo &tensor_info)
148{
149 py::list dims_list;
150 for (int32_t i = 0; i < tensor_info.rank; ++i)
151 {
152 dims_list.append(tensor_info.dims[i]);
153 }
154 return dims_list;
155}
156
157void set_dims(tensorinfo &tensor_info, const py::list &array)
158{
159 tensor_info.rank = py::len(array);
160 for (int32_t i = 0; i < tensor_info.rank; ++i)
161 {
162 tensor_info.dims[i] = py::cast<int32_t>(array[i]);
163 }
164}
165
166NNFW_SESSION::NNFW_SESSION(const char *package_file_path, const char *backends)
167{
168 this->session = nullptr;
170 ensure_status(nnfw_load_model_from_file(this->session, package_file_path));
172}
174{
175 if (session)
176 {
178 }
179}
180
182{
184 this->session = nullptr;
185}
186void NNFW_SESSION::set_input_tensorinfo(uint32_t index, const tensorinfo *tensor_info)
187{
189 ti.dtype = getType(tensor_info->dtype);
190 ti.rank = tensor_info->rank;
191 for (int i = 0; i < NNFW_MAX_RANK; i++)
192 {
193 ti.dims[i] = tensor_info->dims[i];
194 }
196}
202{
203 uint32_t number;
204 NNFW_STATUS status = nnfw_input_size(session, &number);
205 ensure_status(status);
206 return number;
207}
209{
210 uint32_t number;
211 NNFW_STATUS status = nnfw_output_size(session, &number);
212 ensure_status(status);
213 return number;
214}
215void NNFW_SESSION::set_input_layout(uint32_t index, const char *layout)
216{
217 NNFW_LAYOUT nnfw_layout = getLayout(layout);
218 ensure_status(nnfw_set_input_layout(session, index, nnfw_layout));
219}
220void NNFW_SESSION::set_output_layout(uint32_t index, const char *layout)
221{
222 NNFW_LAYOUT nnfw_layout = getLayout(layout);
223 ensure_status(nnfw_set_output_layout(session, index, nnfw_layout));
224}
226{
227 nnfw_tensorinfo tensor_info = nnfw_tensorinfo();
228 ensure_status(nnfw_input_tensorinfo(session, index, &tensor_info));
229 tensorinfo ti;
230 ti.dtype = getStringType(tensor_info.dtype);
231 ti.rank = tensor_info.rank;
232 for (int i = 0; i < NNFW_MAX_RANK; i++)
233 {
234 ti.dims[i] = tensor_info.dims[i];
235 }
236 return ti;
237}
239{
240 nnfw_tensorinfo tensor_info = nnfw_tensorinfo();
241 ensure_status(nnfw_output_tensorinfo(session, index, &tensor_info));
242 tensorinfo ti;
243 ti.dtype = getStringType(tensor_info.dtype);
244 ti.rank = tensor_info.rank;
245 for (int i = 0; i < NNFW_MAX_RANK; i++)
246 {
247 ti.dims[i] = tensor_info.dims[i];
248 }
249 return ti;
250}
251
253// Experimental APIs for training
261
266
268
269void NNFW_SESSION::train(bool update_weights)
270{
271 ensure_status(nnfw_train(session, update_weights));
272}
273
274float NNFW_SESSION::train_get_loss(uint32_t index)
275{
276 float loss = 0.f;
278 return loss;
279}
280
281void NNFW_SESSION::train_export_circle(const py::str &path)
282{
283 const char *c_str_path = path.cast<std::string>().c_str();
285}
286
288{
289 const char *c_str_path = path.cast<std::string>().c_str();
291}
292
294{
295 const char *c_str_path = path.cast<std::string>().c_str();
297}
298
299} // namespace onert::api::python
void train_export_circle(const py::str &path)
tensorinfo input_tensorinfo(uint32_t index)
void train_import_checkpoint(const py::str &path)
void set_input_tensorinfo(uint32_t index, const tensorinfo *tensor_info)
void train(bool update_weights)
void set_input_layout(uint32_t index, const char *layout)
void train_set_traininfo(const nnfw_train_info *info)
void set_output_layout(uint32_t index, const char *layout)
tensorinfo output_tensorinfo(uint32_t index)
NNFW_SESSION(const char *package_file_path, const char *backends)
void train_export_checkpoint(const py::str &path)
volatile const char info[]
void set_dims(tensorinfo &tensor_info, const py::list &array)
Set nnfw_tensorinfo->dims.
const char * getStringType(NNFW_TYPE type)
NNFW_TYPE getType(const char *type="")
void ensure_status(NNFW_STATUS status)
Handle errors with NNFW_STATUS in API functions.
NNFW_LAYOUT getLayout(const char *layout="")
uint64_t num_elems(const nnfw_tensorinfo *tensor_info)
Get the total number of elements in nnfw_tensorinfo->dims.
py::list get_dims(const tensorinfo &tensor_info)
Get nnfw_tensorinfo->dims.
NNFW_STATUS nnfw_await(nnfw_session *session)
Wait for asynchronous run to finish.
Definition nnfw_api.cc:90
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_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_run(nnfw_session *session)
Run inference.
Definition nnfw_api.cc:78
NNFW_STATUS nnfw_prepare(nnfw_session *session)
Prepare session to be ready for inference.
Definition nnfw_api.cc:72
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_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_set_input_layout(nnfw_session *session, uint32_t index, NNFW_LAYOUT layout)
Set the layout of an input.
Definition nnfw_api.cc:122
NNFW_STATUS nnfw_train_get_traininfo(nnfw_session *session, nnfw_train_info *info)
Get training information.
Definition nnfw_api.cc:238
NNFW_STATUS nnfw_create_session(nnfw_session **session)
Create a new session instance.
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_DEPRECATED_API
@ NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE
@ NNFW_STATUS_ERROR
Definition onert-micro.h:93
@ NNFW_STATUS_OUT_OF_MEMORY
Definition onert-micro.h:99
NNFW_STATUS nnfw_close_session(nnfw_session *session)
Close a session instance.
Definition nnfw_api.cc:60
NNFW_STATUS nnfw_train_get_loss(nnfw_session *session, uint32_t index, float *loss)
Get loss value for expected output.
NNFW_STATUS nnfw_train_export_checkpoint(nnfw_session *session, const char *path)
NNFW_STATUS nnfw_load_model_from_file(nnfw_session *session, const char *package_file_path)
Load model from nnpackage file or directory.
NNFW_STATUS nnfw_train_prepare(nnfw_session *session)
Prepare session to be ready for training.
NNFW_STATUS nnfw_train(nnfw_session *session, bool update_weights)
Train the model.
NNFW_STATUS nnfw_train_set_traininfo(nnfw_session *session, const nnfw_train_info *info)
Set training information.
NNFW_STATUS nnfw_train_import_checkpoint(nnfw_session *session, const char *path)
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
#define NNFW_MAX_RANK
Maximum rank expressible with nnfw.
NNFW_STATUS nnfw_train_export_circle(nnfw_session *session, const char *path)
Export current training model into circle model.
tensor info describes the type and shape of tensors
NNFW_TYPE dtype
int32_t dims[NNFW_MAX_RANK]
Training information to prepare training.
tensor info describes the type and shape of tensors
int32_t dims[NNFW_MAX_RANK]