Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / runtime / onert / api / include / nnfw.h
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 /**
18  * @file  nnfw.h
19  * @brief This file describes runtime API
20  */
21 #ifndef __NNFW_H__
22 #define __NNFW_H__
23
24 #include <stddef.h>
25 #include <stdint.h>
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /**
32  * @brief Session to query with runtime
33  *
34  * <p>nnfw_session is started and passed by calling {@link nnfw_create_session}.
35  * Each session has its own inference environment, such as model to inference, backend usage, etc.
36  *
37  * <p>Load model by calling {@link nnfw_load_model_from_file}
38  *
39  * <p>After loading, prepare inference by calling {@link nnfw_prepare}.
40  * Application can set runtime environment before prepare by calling
41  * {@link nnfw_set_available_backends} and {@link nnfw_set_op_backend}, and it is optional.
42  *
43  * <p>Application can inference by calling {@link nnfw_run}.
44  * Before inference, application has responsibility to set input tensor to set input data by calling
45  * {@link nnfw_set_output}, and output tensor to get output by calling {@link nnfw_set_input}
46  *
47  * <p>To support input and output setting, application can get
48  * input and output tensor information by calling<ul>
49  * <li>{@link nnfw_input_size}</li>
50  * <li>{@link nnfw_output_size}</li>
51  * <li>{@link nnfw_input_tensorinfo}</li>
52  * <li>{@link nnfw_output_tensorinfo}</li>
53  * </ul>
54  *
55  * <p>Application can inference many times using one session,
56  * but next inference can do after prior inference end
57  *
58  * <p>Application cannot use muitiple model using one session
59  */
60 typedef struct nnfw_session nnfw_session;
61
62 /**
63  * @brief Tensor types
64  *
65  * The type of tensor represented in {@link nnfw_tensorinfo}
66  */
67 typedef enum {
68   /** A tensor of 32 bit floating point */
69   NNFW_TYPE_TENSOR_FLOAT32 = 0,
70   /** A tensor of 32 bit signed integer */
71   NNFW_TYPE_TENSOR_INT32 = 1,
72   /**
73    * A tensor of 8 bit integers that represent real numbers.
74    *
75    * real_value = (integer_value - zeroPoint) * scale.
76    */
77   NNFW_TYPE_TENSOR_QUANT8_ASYMM = 2,
78   /** A tensor of boolean */
79   NNFW_TYPE_TENSOR_BOOL = 3,
80
81   /** A tensor of 8 bit unsigned integer */
82   NNFW_TYPE_TENSOR_UINT8 = 4,
83
84   /** A tensor of 64 bit signed integer */
85   NNFW_TYPE_TENSOR_INT64 = 5,
86
87 } NNFW_TYPE;
88
89 /**
90  * @brief Result values returned from a call to an API function
91  */
92 typedef enum {
93   /** Successful */
94   NNFW_STATUS_NO_ERROR = 0,
95   /**
96    * An error code for general use.
97    * Mostly used when there is no specific value for that certain situation.
98    */
99   NNFW_STATUS_ERROR = 1,
100   /** Unexpected null argument is given. */
101   NNFW_STATUS_UNEXPECTED_NULL = 2,
102   /** When a function was called but it is not valid for the current session state. */
103   NNFW_STATUS_INVALID_STATE = 3,
104   /** When it is out of memory */
105   NNFW_STATUS_OUT_OF_MEMORY = 4,
106   /** When it was given an insufficient output buffer */
107   NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE = 5,
108 } NNFW_STATUS;
109
110 /**
111  * @brief Data format of a tensor
112  */
113 typedef enum {
114   /** Don't care layout */
115   NNFW_LAYOUT_NONE = 0,
116   /**
117    * Channel last layout
118    * If rank is 4, layout is NHWC
119    */
120   NNFW_LAYOUT_CHANNELS_LAST = 1,
121   /**
122    * Channel first layout
123    * If rank is 4, layout is NCHW
124    */
125   NNFW_LAYOUT_CHANNELS_FIRST = 2,
126 } NNFW_LAYOUT;
127
128 /**
129  * @brief Information ID for retrieving information on nnfw (e.g. version)
130  */
131 typedef enum {
132   /** nnfw runtime version
133    * Its value is uint32 in 0xMMmmmmPP, where MM = major, mmmm = minor, PP = patch.
134    */
135   NNFW_INFO_ID_VERSION = 0,
136 } NNFW_INFO_ID;
137
138 /**
139  * @brief Maximum rank expressible with nnfw
140  */
141 #define NNFW_MAX_RANK (6)
142
143 /**
144  * @brief tensor info describes the type and shape of tensors
145  *
146  * <p>This structure is used to describe input and output tensors.
147  * Application can get input and output tensor type and shape described in model by using
148  * {@link nnfw_input_tensorinfo} and {@link nnfw_output_tensorinfo}
149  *
150  * <p>Maximum rank is 6 (NNFW_MAX_RANK). And tensor's dimension value is filled in 'dims' field from
151  * index 0.
152  * For example, if tensor's rank is 4,
153  * application can get dimension value from dims[0], dims[1], dims[2], and dims[3]
154  */
155 typedef struct nnfw_tensorinfo
156 {
157   /** The data type */
158   NNFW_TYPE dtype;
159   /** The number of dimensions (rank) */
160   int32_t rank;
161   /**
162    * The dimension of tensor.
163    * Maximum rank is 6 (NNFW_MAX_RANK).
164    */
165   int32_t dims[NNFW_MAX_RANK];
166 } nnfw_tensorinfo;
167
168 /**
169  * @brief Create a new session instance.
170  *
171  * <p>This only creates a session.
172  * Model is loaded after {@link nnfw_load_model_from_file} is invoked.
173  * And inference is performed after {@link nnfw_run} is invoked.
174  *
175  * <p>{@link nnfw_close_session} should be called once
176  * if session is no longer need
177  *
178  * @param[out]  session The session to be created
179  * @return      NNFW_STATUS_NO_ERROR if successful
180  */
181 NNFW_STATUS nnfw_create_session(nnfw_session **session);
182
183 /**
184  * @brief Close a session instance
185  *
186  * After called, access to closed session by application will be invalid
187  *
188  * @param[in] session The session to be closed
189  * @return    @c NNFW_STATUS_NO_ERROR if successful
190  */
191 NNFW_STATUS nnfw_close_session(nnfw_session *session);
192
193 /**
194  * @brief     Load model from nnpackage file or directory
195  *
196  * The length of \p package_file_path must not execeed 1024 bytes including zero at the end.
197  *
198  * @param[in] session           nnfw_session loading the given nnpackage file/dir
199  * @param[in] package_file_path Path to the nnpackage file or unzipped directory to be loaded
200  *
201  * @return    @c NNFW_STATUS_NO_ERROR if successful
202  */
203 NNFW_STATUS nnfw_load_model_from_file(nnfw_session *session, const char *package_file_path);
204
205 /**
206  * @brief     Apply i-th input's tensor info to resize input tensor
207  *
208  * This function should be called before {@link nnfw_prepare} is invoked, and
209  * should be called after {@link nnfw_load_model_from_file} is invoked
210  * See {@link nnfw_prepare} for information applying updated tensor info
211  * If this function is called many times for same index, tensor info is overwritten
212  *
213  * @deprecated Deprecated since 1.7.0. Use {@link nnfw_set_input_tensorinfo} instead.
214  *
215  * @param[in] session     Session to the input tensor info is to be set
216  * @param[in] index       Index of input to be applied (0-indexed)
217  * @param[in] tensor_info Tensor info to be applied
218  * @return    @c NNFW_STATUS_NO_ERROR if successful, otherwise return @c NNFW_STATUS_ERROR
219  */
220 NNFW_STATUS nnfw_apply_tensorinfo(nnfw_session *session, uint32_t index,
221                                   nnfw_tensorinfo tensor_info);
222
223 /**
224  * @brief    Set input model's tensor info for resizing
225  *
226  * This function can be called at any time after calling {@link nnfw_model_load_from_file}. Changing
227  * input tensor's shape will cause shape inference for the model. There are two different types of
228  * shape inference - static and dynamic. Which one to use is depend on the current state of the
229  * session.
230  * When it is called after calling {@link nnfw_model_load_from_file} and before calling {@link
231  * nnfw_prepare}, this info will be used when {@link nnfw_prepare}. And it will perform static shape
232  * inference for all tensors.
233  * When it is called after calling {@link nnfw_prepare} or even after {@link nnfw_run}, this info
234  * will be used when {@link nnfw_run}. And the shapes of the tensors are determined on the fly.
235  * If this function is called many times for the same index, it is overwritten.
236  *
237  * @param[in] session     Session to the input tensor info is to be set
238  * @param[in] index       Index of input to be set (0-indexed)
239  * @param[in] tensor_info Tensor info to be set
240  * @return    @c NNFW_STATUS_NO_ERROR if successful, otherwise return @c NNFW_STATUS_ERROR
241  */
242 NNFW_STATUS nnfw_set_input_tensorinfo(nnfw_session *session, uint32_t index,
243                                       const nnfw_tensorinfo *tensor_info);
244
245 /**
246  * @brief     Prepare session to be ready for inference
247  *
248  * This phase may finalize model compilation, scheduling, and additional settings.
249  * If {@link nnfw_apply_tensor} is called to apply input tensor info different with model
250  * before this function, tries to resize all tensors.
251  *
252  * @param[in] session the session to be prepared
253  * @return    @c NNFW_STATUS_NO_ERROR if successful, otherwise return @c NNFW_STATUS_ERROR
254  */
255 NNFW_STATUS nnfw_prepare(nnfw_session *session);
256
257 /**
258  * @brief     Run inference
259  *
260  * <p>This function should be called after model is loaded by {@link nnfw_load_model_from_file},
261  * session is prepared for inference by {@link nnfw_prepare}, set input and output buffers
262  * by {@link nnfw_set_input} and {@link nnfw_set_output}.</p>
263  *
264  * <p>This function return after inference is finished.</p>
265  *
266  * @param[in] session The session to run inference
267  * @return    @c NNFW_STATUS_NO_ERROR if successful
268  */
269 NNFW_STATUS nnfw_run(nnfw_session *session);
270
271 /**
272  * @brief     Run inference asynchronously
273  *
274  * <p>This function must be called after model is loaded by {@link nnfw_load_model_from_file},
275  * session is prepared for inference by {@link nnfw_prepare}, set input and output buffers
276  * by {@link nnfw_set_input} and {@link nnfw_set_output}.</p>
277  *
278  * <p>This function returns immediately after starting a thread to run the inference.
279  * To get the result of it or to do the next inference with {@link nnfw_run} or
280  * {@link nnfw_run_async}, {@link nnfw_await} must be called to ensure the current asynchronous
281  * inference has finished. Only one asynchronous inference is allowed at a time for a session.
282  * If this function is called while the previous one is still running, it returns an error.</p>
283  *
284  * @param[in] session The session to run inference
285  * @return    @c NNFW_STATUS_NO_ERROR if successful
286  */
287 NNFW_STATUS nnfw_run_async(nnfw_session *session);
288
289 /**
290  * @brief     Wait for asynchronous run to finish
291  *
292  * <p>This function must be called after calling {@link nnfw_run_asnyc}, and can be called only once
293  * for a {@link nnfw_run_async} call.
294  *
295  * <p>When this function returns, it means that this session has finished the asynchronous run. Then
296  * the user can safely use the output data.</p>
297  *
298  * <p>This function returns after the asynchronous inference is finished.</p>
299  *
300  * @param[in] session The session to run inference
301  * @return    @c NNFW_STATUS_NO_ERROR if successful
302  */
303 NNFW_STATUS nnfw_await(nnfw_session *session);
304
305 /**
306  * @brief     Set input buffer
307  *
308  * This function must be called after {@link nnfw_prepare}, \p buffer given to this function can be
309  * reused for many inferences. \p length must be greater or equal than the operand requires. To
310  * specify an optional input, you can either not call this for that input or call this with \p
311  * buffer of NULL and \p length of 0.
312  *
313  * @param[in] session Session to the input is to be set
314  * @param[in] index   Index of input to be set (0-indexed)
315  * @param[in] type    Type of the input
316  * @param[in] buffer  Raw buffer for input
317  * @param[in] length  Size of bytes of input buffer
318  *
319  * @return    @c NNFW_STATUS_NO_ERROR if successful
320  */
321 NNFW_STATUS nnfw_set_input(nnfw_session *session, uint32_t index, NNFW_TYPE type,
322                            const void *buffer, size_t length);
323
324 /**
325  * @brief       Set output buffer
326  *
327  * This function must be called after {@link nnfw_prepare}, \p buffer given to this function can be
328  * reused for many inferences. \p length must be greater or equal than the operand requires. An
329  * output operand can have unspecified shape and deduced dynamically during the execution. You must
330  * provide \p buffer large enough.
331  *
332  * @param[in]   session Session from inference output is to be extracted
333  * @param[in]   index   Index of output to be set (0-indexed)
334  * @param[in]   type    Type of the output
335  * @param[out]  buffer  Raw buffer for output
336  * @param[in]   length  Size of bytes of output buffer
337  *
338  * @return      @c NNFW_STATUS_NO_ERROR if successful
339  */
340 NNFW_STATUS nnfw_set_output(nnfw_session *session, uint32_t index, NNFW_TYPE type, void *buffer,
341                             size_t length);
342
343 /**
344  * @brief       Get the number of inputs
345  *
346  * Application can call this function to get number of inputs defined in loaded model.
347  * This function should be called after {@link nnfw_load_model_from_file} is invoked to load model
348  *
349  * @param[in]   session Session from input information is to be extracted
350  * @param[out]  number  Variable which the number of inputs is put into
351  *
352  * @return      @c NNFW_STATUS_NO_ERROR if successful
353  */
354 NNFW_STATUS nnfw_input_size(nnfw_session *session, uint32_t *number);
355
356 /**
357  * @brief       Get the number of outputs
358  *
359  * Application can call this function to get number of outputs defined in loaded model.
360  * This function should be called after {@link nnfw_load_model_from_file} is invoked to load model
361  *
362  * @param[in]   session Session from output information is to be extracted
363  * @param[out]  number  Variable which the number of outputs is put into
364  *
365  * @return      @c NNFW_STATUS_NO_ERROR if successful
366  */
367 NNFW_STATUS nnfw_output_size(nnfw_session *session, uint32_t *number);
368
369 /**
370  * @brief Set the layout of an input
371  *
372  * The input that does not call this has NNFW_LAYOUT_NHWC layout
373  *
374  * @param[in] session session from inference input is to be extracted
375  * @param[in] index   index of input to be set (0-indexed)
376  * @param[in] layout  layout to set to target input
377  *
378  * @return NNFW_STATUS_NO_ERROR if successful
379  */
380 NNFW_STATUS nnfw_set_input_layout(nnfw_session *session, uint32_t index, NNFW_LAYOUT layout);
381
382 /**
383  * @brief Set the layout of an output
384  *
385  * The output that does not call this has NNFW_LAYOUT_NHWC layout
386  *
387  * @param[in] session session from inference output is to be extracted
388  * @param[in] index   index of output to be set (0-indexed)
389  * @param[in] layout  layout to set to target output
390  *
391  * @return NNFW_STATUS_NO_ERROR if successful
392  */
393 NNFW_STATUS nnfw_set_output_layout(nnfw_session *session, uint32_t index, NNFW_LAYOUT layout);
394
395 /**
396  * @brief       Get i-th input tensor info
397  *
398  * <p>Before {@link nnfw_prepare} is invoked, this function return tensor info in model,
399  * so updated tensor info by {@link nnfw_apply_tensorinfo} is not returned.</p>
400  *
401  * <p>After {@link nnfw_prepare} is invoked, this function return updated tensor info
402  * if tensor info is updated by {@link nnfw_apply_tensorinfo}.</p>
403  *
404  * @param[in]   session     Session from input information is to be extracted
405  * @param[in]   index       Index of input
406  * @param[out]  tensor_info Tensor info (shape, type, etc)
407  *
408  * @return      @c NNFW_STATUS_NO_ERROR if successful
409  */
410 NNFW_STATUS nnfw_input_tensorinfo(nnfw_session *session, uint32_t index,
411                                   nnfw_tensorinfo *tensor_info);
412
413 /**
414  * @brief     Get i-th output tensor info
415  *
416  * <p>After {@link nnfw_load_model_from_file} and before {@link nnfw_prepare} is invoked, it returns
417  * tensor info in the model.</p>
418  *
419  * <p>After {@link nnfw_prepare} and before {@link nnfw_run} is invoked, this function returns
420  * updated tensor info if tensor info is updated by {@link nnfw_set_input_tensorinfo}.</p>
421  *
422  * <p>After {@link nnfw_run} is invoked(at least once), it returns the updated tensor info during
423  * the latest execution.</p>
424  *
425  * @param[in]   session     Session from output information is to be extracted
426  * @param[in]   index       Index of output
427  * @param[out]  tensor_info Tensor info (shape, type, etc)
428  *
429  * @return      @c NNFW_STATUS_NO_ERROR if successful
430  */
431 NNFW_STATUS nnfw_output_tensorinfo(nnfw_session *session, uint32_t index,
432                                    nnfw_tensorinfo *tensor_info);
433
434 /**
435  * @brief     Set available backends
436  *
437  * This function should be called before {@link nnfw_prepare} is invoked.
438  *
439  * <p>Supported backends differs on each platforms.
440  * For example, `x86_64` supports "cpu" only.
441  * Multiple backends can be set and they must be separated by a semicolon (ex: "acl_cl;cpu").
442  * For each backend string, `libbackend_{backend}.so` will be dynamically loaded during
443  * {@link nnfw_prepare}.
444  * Among the multiple backends, the 1st element is used as the default backend.</p>
445  *
446  * @param[in] session session to which avilable backends are set
447  * @param[in] backends available backends on which nnfw uses
448  *
449  * @return @c NNFW_STATUS_NO_ERROR if successful
450  */
451 NNFW_STATUS nnfw_set_available_backends(nnfw_session *session, const char *backends);
452
453 /**
454  * @brief     Set the operation's backend
455  *
456  * This function should be called before {@link nnfw_prepare} is invoked.
457  *
458  * <p>The backend for op has higher priority than available backends specified by
459  * {@link nnfw_set_available_backends}.</p>
460  *
461  * @deprecated Deprecated since 1.8.0.
462  *
463  * @param[in] session session to be modified
464  * @param[in] op operation to be set
465  * @param[in] backend bakcend on which operation run
466  *
467  * @return @c NNFW_STATUS_NO_ERROR if successful
468  */
469 NNFW_STATUS nnfw_set_op_backend(nnfw_session *session, const char *op, const char *backend);
470
471 /**
472  * @brief     Retrieve uint32 type of nnfw information for given information ID.
473  *
474  * <p>Retrieves the information of property given by information id </p>
475  *
476  * @note: The input session could be null for global information (e.g. runtime version).*
477  *
478  * @param[in] session session to be queried on.
479  * @param[in] information ID to be queried
480  * @param[out] val uint32 value to be returned.
481  *
482  * @return @c NNFW_STATUS_NO_ERROR if successful
483  */
484 NNFW_STATUS nnfw_query_info_u32(nnfw_session *session, NNFW_INFO_ID id, uint32_t *val);
485
486 #ifdef __cplusplus
487 }
488 #endif
489
490 #endif