Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / api / src / nnfw_api.cc
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_api_internal.h"
18 #include "nnfw_version.h"
19
20 // Double-check enum value changes
21
22 #define STATIC_ASSERT_ENUM_CHECK(ENUM, VAL) static_assert((ENUM) == (VAL), #ENUM " has changed")
23
24 STATIC_ASSERT_ENUM_CHECK(NNFW_TYPE_TENSOR_FLOAT32, 0);
25 STATIC_ASSERT_ENUM_CHECK(NNFW_TYPE_TENSOR_INT32, 1);
26 STATIC_ASSERT_ENUM_CHECK(NNFW_TYPE_TENSOR_QUANT8_ASYMM, 2);
27 STATIC_ASSERT_ENUM_CHECK(NNFW_TYPE_TENSOR_BOOL, 3);
28 STATIC_ASSERT_ENUM_CHECK(NNFW_TYPE_TENSOR_UINT8, 4);
29 STATIC_ASSERT_ENUM_CHECK(NNFW_TYPE_TENSOR_INT64, 5);
30 STATIC_ASSERT_ENUM_CHECK(NNFW_TYPE_TENSOR_QUANT8_ASYMM_SIGNED, 6);
31
32 STATIC_ASSERT_ENUM_CHECK(NNFW_STATUS_NO_ERROR, 0);
33 STATIC_ASSERT_ENUM_CHECK(NNFW_STATUS_ERROR, 1);
34 STATIC_ASSERT_ENUM_CHECK(NNFW_STATUS_UNEXPECTED_NULL, 2);
35 STATIC_ASSERT_ENUM_CHECK(NNFW_STATUS_INVALID_STATE, 3);
36 STATIC_ASSERT_ENUM_CHECK(NNFW_STATUS_OUT_OF_MEMORY, 4);
37 STATIC_ASSERT_ENUM_CHECK(NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE, 5);
38
39 STATIC_ASSERT_ENUM_CHECK(NNFW_LAYOUT_NONE, 0);
40 STATIC_ASSERT_ENUM_CHECK(NNFW_LAYOUT_CHANNELS_LAST, 1);
41 STATIC_ASSERT_ENUM_CHECK(NNFW_LAYOUT_CHANNELS_FIRST, 2);
42
43 STATIC_ASSERT_ENUM_CHECK(NNFW_INFO_ID_VERSION, 0);
44
45 #undef STATIC_ASSERT_ENUM_CHECK
46
47 #define NNFW_RETURN_ERROR_IF_NULL(p)      \
48   do                                      \
49   {                                       \
50     if ((p) == NULL)                      \
51       return NNFW_STATUS_UNEXPECTED_NULL; \
52   } while (0)
53
54 /*
55  * Create a new session instance
56  *
57  * @param session the session to be created
58  * @return NNFW_STATUS_NO_ERROR if successful
59  */
60 NNFW_STATUS nnfw_create_session(nnfw_session **session)
61 {
62   NNFW_RETURN_ERROR_IF_NULL(session);
63
64   *session = new (std::nothrow) nnfw_session();
65   if (*session == nullptr)
66     return NNFW_STATUS_OUT_OF_MEMORY;
67   return NNFW_STATUS_NO_ERROR;
68 }
69
70 /*
71  * Close a session instance
72  *
73  * @param session the session to be closed
74  * @return NNFW_STATUS_NO_ERROR if successful
75  */
76 NNFW_STATUS nnfw_close_session(nnfw_session *session)
77 {
78   delete session;
79   return NNFW_STATUS_NO_ERROR;
80 }
81
82 /*
83  * Load model from nnpackage file or directory
84  *
85  * @param session nnfw_session loading the given nnpackage file/dir
86  * @param package_file_path path to the nnpackage file or unzipped directory to be loaded
87  *
88  * @return NNFW_STATUS_NO_ERROR if successful
89  */
90 NNFW_STATUS nnfw_load_model_from_file(nnfw_session *session, const char *pacakge_file_path)
91 {
92   NNFW_RETURN_ERROR_IF_NULL(session);
93   return session->load_model_from_nnpackage(pacakge_file_path);
94 }
95
96 /*
97  * Prepare session to be ready for inference
98  * This phase may finalize model compilation, scheduling, and additional settings.
99  *
100  * @param session the session to be prepared
101  * @return NNFW_STATUS_NO_ERROR if successful
102  */
103 NNFW_STATUS nnfw_prepare(nnfw_session *session)
104 {
105   NNFW_RETURN_ERROR_IF_NULL(session);
106   return session->prepare();
107 }
108
109 /*
110  * Run inference
111  *
112  * @param session the session to run inference
113  * @return NNFW_STATUS_NO_ERROR if successful
114  */
115 NNFW_STATUS nnfw_run(nnfw_session *session)
116 {
117   NNFW_RETURN_ERROR_IF_NULL(session);
118   return session->run();
119 }
120
121 NNFW_STATUS nnfw_run_async(nnfw_session *session)
122 {
123   NNFW_RETURN_ERROR_IF_NULL(session);
124   return session->run_async();
125 }
126
127 NNFW_STATUS nnfw_await(nnfw_session *session)
128 {
129   NNFW_RETURN_ERROR_IF_NULL(session);
130   return session->await();
131 }
132
133 /*
134  * Set input
135  *
136  * @param session session to the input is to be set
137  * @param index index of input to be set (0-indexed)
138  * @param type type of the input
139  * @param buffer raw buffer for input
140  * @param length size of bytes of input
141  *
142  * @return NNFW_STATUS_NO_ERROR if successful
143  */
144
145 NNFW_STATUS nnfw_set_input(nnfw_session *session, uint32_t index, NNFW_TYPE type,
146                            const void *buffer, size_t length)
147 {
148   NNFW_RETURN_ERROR_IF_NULL(session);
149   return session->set_input(index, type, buffer, length);
150 }
151
152 /*
153  * Set output
154  *
155  * @param session session from inference output is to be extracted
156  * @param index index of output to be set (0-indexed)
157  * @param type type of the output
158  * @param buffer raw buffer for output
159  * @param length size of bytes of output
160  *
161  * @return NNFW_STATUS_NO_ERROR if successful
162  */
163
164 NNFW_STATUS nnfw_set_output(nnfw_session *session, uint32_t index, NNFW_TYPE type, void *buffer,
165                             size_t length)
166 {
167   NNFW_RETURN_ERROR_IF_NULL(session);
168   return session->set_output(index, type, buffer, length);
169 }
170
171 /*
172  * Get the number of inputs
173  *
174  * @param[in] session session from input information is to be extracted
175  * @param[out] number variable which the number of inputs is put into
176  *
177  * @return NNFW_STATUS_NO_ERROR if successful
178  */
179
180 NNFW_STATUS nnfw_input_size(nnfw_session *session, uint32_t *number)
181 {
182   NNFW_RETURN_ERROR_IF_NULL(session);
183   return session->input_size(number);
184 }
185
186 /*
187  * Get the number of outputs
188  *
189  * @param[in] session session from output information is to be extracted
190  * @param[out] number variable which the number of outputs is put into
191  *
192  * @return NNFW_STATUS_NO_ERROR if successful
193  */
194 NNFW_STATUS nnfw_output_size(nnfw_session *session, uint32_t *number)
195 {
196   NNFW_RETURN_ERROR_IF_NULL(session);
197   return session->output_size(number);
198 }
199
200 /*
201  * Set the layout of an input
202  * @note The input that does not call this has NNFW_LAYOUT_CHANNELS_LAST layout
203  *
204  * @param[in] session session from inference input is to be extracted
205  * @param[in] index   index of input to be set (0-indexed)
206  * @param[in] layout  layout to set to target input
207  *
208  * @return NNFW_STATUS_NO_ERROR if successful
209  */
210 NNFW_STATUS nnfw_set_input_layout(nnfw_session *session, uint32_t index, NNFW_LAYOUT layout)
211 {
212   NNFW_RETURN_ERROR_IF_NULL(session);
213   return session->set_input_layout(index, layout);
214 }
215
216 /*
217  * Set the layout of an output
218  * @note The output that does not call this has NNFW_LAYOUT_CHANNELS_LAST layout
219  *
220  * @param[in] session session from inference output is to be extracted
221  * @param[in] index   index of output to be set (0-indexed)
222  * @param[in] layout  layout to set to target output
223  *
224  * @return NNFW_STATUS_NO_ERROR if successful
225  */
226 NNFW_STATUS nnfw_set_output_layout(nnfw_session *session, uint32_t index, NNFW_LAYOUT layout)
227 {
228   NNFW_RETURN_ERROR_IF_NULL(session);
229   return session->set_output_layout(index, layout);
230 }
231
232 /*
233  * Get i-th input tensor info
234  *
235  * @param[in] session session from input information is to be extracted
236  * @param[in] index index of input
237  * @param[out] tensor_info nnfw_tensor_info
238  *
239  * @return NNFW_STATUS_NO_ERROR if successful
240  */
241 NNFW_STATUS nnfw_input_tensorinfo(nnfw_session *session, uint32_t index,
242                                   nnfw_tensorinfo *tensor_info)
243 {
244   NNFW_RETURN_ERROR_IF_NULL(session);
245   return session->input_tensorinfo(index, tensor_info);
246 }
247
248 /*
249  * Get i-th output tensor info
250  *
251  * @param[in] session session from output information is to be extracted
252  * @param[in] index index of output
253  * @param[out] tensor_info nnfw_tensor_info
254  *
255  * @return NNFW_STATUS_NO_ERROR if successful
256  */
257 NNFW_STATUS nnfw_output_tensorinfo(nnfw_session *session, uint32_t index,
258                                    nnfw_tensorinfo *tensor_info)
259 {
260   NNFW_RETURN_ERROR_IF_NULL(session);
261   return session->output_tensorinfo(index, tensor_info);
262 }
263
264 /*
265  * Register custom operation
266  * @param session session to register this operation
267  * @param id operation id
268  * @param info registration info ( eval function, etc. )
269  * @return NNFW_STATUS_NO_ERROR if successful
270  */
271 NNFW_STATUS nnfw_register_custom_op_info(nnfw_session *session, const char *id,
272                                          custom_kernel_registration_info *info)
273 {
274   NNFW_RETURN_ERROR_IF_NULL(session);
275   return session->register_custom_operation(id, info->eval_function);
276 }
277
278 NNFW_STATUS nnfw_apply_tensorinfo(nnfw_session *session, uint32_t index,
279                                   nnfw_tensorinfo tensor_info)
280 {
281   NNFW_RETURN_ERROR_IF_NULL(session);
282   return session->apply_tensorinfo(index, tensor_info);
283 }
284
285 NNFW_STATUS nnfw_set_input_tensorinfo(nnfw_session *session, uint32_t index,
286                                       const nnfw_tensorinfo *tensor_info)
287 {
288   NNFW_RETURN_ERROR_IF_NULL(session);
289   return session->set_input_tensorinfo(index, tensor_info);
290 }
291
292 /*
293  * Set available backends
294  *
295  * @param[in] session session to which a avilable backends are set
296  * @param[in] backends available backends on which nnfw uses
297  */
298 NNFW_STATUS nnfw_set_available_backends(nnfw_session *session, const char *backends)
299 {
300   NNFW_RETURN_ERROR_IF_NULL(session);
301   return session->set_available_backends(backends);
302 }
303
304 /*
305  * Set the operation's backend
306  *
307  * @param[in] session session to be modified
308  * @param[in] op operation to be set
309  * @param[in] backend bakcend on which operation run
310  *
311  * @return NNFW_STATUS_NO_ERROR if successful
312  */
313 NNFW_STATUS nnfw_set_op_backend(nnfw_session *session, const char *op, const char *backend)
314 {
315   NNFW_RETURN_ERROR_IF_NULL(session);
316   return session->set_op_backend(op, backend);
317 }
318
319 /*
320  * Retrieve uint32 type of nnfw information for given information ID.
321  *
322  * @param[in] session session to be queried on
323  * @param[in] information ID to be queried
324  * @param[out] val uint32 value to be returned
325  *
326  * @return @c NNFW_STATUS_NO_ERROR if successful
327  */
328 NNFW_STATUS nnfw_query_info_u32(nnfw_session *session, NNFW_INFO_ID id, uint32_t *val)
329 {
330   (void)session;
331   switch (id)
332   {
333     case NNFW_INFO_ID_VERSION:
334       if (val)
335       {
336         *val = NNFW_VERSION;
337         return NNFW_STATUS_NO_ERROR;
338       }
339       break;
340     default:
341       return NNFW_STATUS_ERROR;
342   }
343   // It should not be reached.
344   return NNFW_STATUS_ERROR;
345 }
346
347 NNFW_STATUS nnfw_load_circle_from_buffer(nnfw_session *session, uint8_t *buffer, size_t size)
348 {
349   NNFW_RETURN_ERROR_IF_NULL(session);
350   return session->load_circle_from_buffer(buffer, size);
351 }
352
353 NNFW_STATUS nnfw_load_model_from_modelfile(nnfw_session *session, const char *file_path)
354 {
355   NNFW_RETURN_ERROR_IF_NULL(session);
356   return session->load_model_from_modelfile(file_path);
357 }
358
359 NNFW_STATUS nnfw_input_tensorindex(nnfw_session *session, const char *tensorname, uint32_t *index)
360 {
361   NNFW_RETURN_ERROR_IF_NULL(session);
362   return session->input_tensorindex(tensorname, index);
363 }
364
365 NNFW_STATUS nnfw_output_tensorindex(nnfw_session *session, const char *tensorname, uint32_t *index)
366 {
367   NNFW_RETURN_ERROR_IF_NULL(session);
368   return session->output_tensorindex(tensorname, index);
369 }