2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @brief This file describes runtime API
32 * @brief Session to query with runtime
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.
37 * <p>Load model by calling {@link nnfw_load_model_from_file}
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.
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}
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>
55 * <p>Application can inference many times using one session,
56 * but next inference can do after prior inference end
58 * <p>Application cannot use muitiple model using one session
60 typedef struct nnfw_session nnfw_session;
65 * The type of tensor represented in {@link nnfw_tensorinfo}
69 /** A tensor of 32 bit floating point */
70 NNFW_TYPE_TENSOR_FLOAT32 = 0,
71 /** A tensor of 32 bit signed integer */
72 NNFW_TYPE_TENSOR_INT32 = 1,
74 * A tensor of 8 bit unsigned integers that represent real numbers.
76 * real_value = (integer_value - zeroPoint) * scale.
78 NNFW_TYPE_TENSOR_QUANT8_ASYMM = 2,
79 /** A tensor of boolean */
80 NNFW_TYPE_TENSOR_BOOL = 3,
82 /** A tensor of 8 bit unsigned integer */
83 NNFW_TYPE_TENSOR_UINT8 = 4,
85 /** A tensor of 64 bit signed integer */
86 NNFW_TYPE_TENSOR_INT64 = 5,
89 * A tensor of 8 bit signed integers that represent real numbers.
91 * real_value = (integer_value - zeroPoint) * scale.
93 NNFW_TYPE_TENSOR_QUANT8_ASYMM_SIGNED = 6,
98 * @brief Result values returned from a call to an API function
103 NNFW_STATUS_NO_ERROR = 0,
105 * An error code for general use.
106 * Mostly used when there is no specific value for that certain situation.
108 NNFW_STATUS_ERROR = 1,
109 /** Unexpected null argument is given. */
110 NNFW_STATUS_UNEXPECTED_NULL = 2,
111 /** When a function was called but it is not valid for the current session state. */
112 NNFW_STATUS_INVALID_STATE = 3,
113 /** When it is out of memory */
114 NNFW_STATUS_OUT_OF_MEMORY = 4,
115 /** When it was given an insufficient output buffer */
116 NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE = 5,
120 * @brief Data format of a tensor
124 /** Don't care layout */
125 NNFW_LAYOUT_NONE = 0,
127 * Channel last layout
128 * If rank is 4, layout is NHWC
130 NNFW_LAYOUT_CHANNELS_LAST = 1,
132 * Channel first layout
133 * If rank is 4, layout is NCHW
135 NNFW_LAYOUT_CHANNELS_FIRST = 2,
139 * @brief Information ID for retrieving information on nnfw (e.g. version)
143 /** nnfw runtime version
144 * Its value is uint32 in 0xMMmmmmPP, where MM = major, mmmm = minor, PP = patch.
146 NNFW_INFO_ID_VERSION = 0,
150 * @brief Maximum rank expressible with nnfw
152 #define NNFW_MAX_RANK (6)
155 * @brief tensor info describes the type and shape of tensors
157 * <p>This structure is used to describe input and output tensors.
158 * Application can get input and output tensor type and shape described in model by using
159 * {@link nnfw_input_tensorinfo} and {@link nnfw_output_tensorinfo}
161 * <p>Maximum rank is 6 (NNFW_MAX_RANK). And tensor's dimension value is filled in 'dims' field from
163 * For example, if tensor's rank is 4,
164 * application can get dimension value from dims[0], dims[1], dims[2], and dims[3]
166 typedef struct nnfw_tensorinfo
170 /** The number of dimensions (rank) */
173 * The dimension of tensor.
174 * Maximum rank is 6 (NNFW_MAX_RANK).
176 int32_t dims[NNFW_MAX_RANK];
180 * @brief Create a new session instance.
182 * <p>This only creates a session.
183 * Model is loaded after {@link nnfw_load_model_from_file} is invoked.
184 * And inference is performed after {@link nnfw_run} is invoked.
186 * <p>{@link nnfw_close_session} should be called once
187 * if session is no longer need
189 * @param[out] session The session to be created
190 * @return NNFW_STATUS_NO_ERROR if successful
192 NNFW_STATUS nnfw_create_session(nnfw_session **session);
195 * @brief Close a session instance
197 * After called, access to closed session by application will be invalid
199 * @param[in] session The session to be closed
200 * @return @c NNFW_STATUS_NO_ERROR if successful
202 NNFW_STATUS nnfw_close_session(nnfw_session *session);
205 * @brief Load model from nnpackage file or directory
207 * The length of \p package_file_path must not execeed 1024 bytes including zero at the end.
209 * @param[in] session nnfw_session loading the given nnpackage file/dir
210 * @param[in] package_file_path Path to the nnpackage file or unzipped directory to be loaded
212 * @return @c NNFW_STATUS_NO_ERROR if successful
214 NNFW_STATUS nnfw_load_model_from_file(nnfw_session *session, const char *package_file_path);
217 * @brief Apply i-th input's tensor info to resize input tensor
219 * This function should be called before {@link nnfw_prepare} is invoked, and
220 * should be called after {@link nnfw_load_model_from_file} is invoked
221 * See {@link nnfw_prepare} for information applying updated tensor info
222 * If this function is called many times for same index, tensor info is overwritten
224 * @deprecated Deprecated since 1.7.0. Use {@link nnfw_set_input_tensorinfo} instead.
226 * @param[in] session Session to the input tensor info is to be set
227 * @param[in] index Index of input to be applied (0-indexed)
228 * @param[in] tensor_info Tensor info to be applied
229 * @return @c NNFW_STATUS_NO_ERROR if successful, otherwise return @c NNFW_STATUS_ERROR
231 NNFW_STATUS nnfw_apply_tensorinfo(nnfw_session *session, uint32_t index,
232 nnfw_tensorinfo tensor_info);
235 * @brief Set input model's tensor info for resizing
237 * This function can be called at any time after calling {@link nnfw_model_load_from_file}. Changing
238 * input tensor's shape will cause shape inference for the model. There are two different types of
239 * shape inference - static and dynamic. Which one to use is depend on the current state of the
241 * When it is called after calling {@link nnfw_model_load_from_file} and before calling {@link
242 * nnfw_prepare}, this info will be used when {@link nnfw_prepare}. And it will perform static shape
243 * inference for all tensors.
244 * When it is called after calling {@link nnfw_prepare} or even after {@link nnfw_run}, this info
245 * will be used when {@link nnfw_run}. And the shapes of the tensors are determined on the fly.
246 * If this function is called many times for the same index, it is overwritten.
248 * @param[in] session Session to the input tensor info is to be set
249 * @param[in] index Index of input to be set (0-indexed)
250 * @param[in] tensor_info Tensor info to be set
251 * @return @c NNFW_STATUS_NO_ERROR if successful, otherwise return @c NNFW_STATUS_ERROR
253 NNFW_STATUS nnfw_set_input_tensorinfo(nnfw_session *session, uint32_t index,
254 const nnfw_tensorinfo *tensor_info);
257 * @brief Prepare session to be ready for inference
259 * This phase may finalize model compilation, scheduling, and additional settings.
260 * If {@link nnfw_apply_tensor} is called to apply input tensor info different with model
261 * before this function, tries to resize all tensors.
263 * @param[in] session the session to be prepared
264 * @return @c NNFW_STATUS_NO_ERROR if successful, otherwise return @c NNFW_STATUS_ERROR
266 NNFW_STATUS nnfw_prepare(nnfw_session *session);
269 * @brief Run inference
271 * <p>This function should be called after model is loaded by {@link nnfw_load_model_from_file},
272 * session is prepared for inference by {@link nnfw_prepare}, set input and output buffers
273 * by {@link nnfw_set_input} and {@link nnfw_set_output}.</p>
275 * <p>This function return after inference is finished.</p>
277 * @param[in] session The session to run inference
278 * @return @c NNFW_STATUS_NO_ERROR if successful
280 NNFW_STATUS nnfw_run(nnfw_session *session);
283 * @brief Run inference asynchronously
285 * <p>This function must be called after model is loaded by {@link nnfw_load_model_from_file},
286 * session is prepared for inference by {@link nnfw_prepare}, set input and output buffers
287 * by {@link nnfw_set_input} and {@link nnfw_set_output}.</p>
289 * <p>This function returns immediately after starting a thread to run the inference.
290 * To get the result of it or to do the next inference with {@link nnfw_run} or
291 * {@link nnfw_run_async}, {@link nnfw_await} must be called to ensure the current asynchronous
292 * inference has finished. Only one asynchronous inference is allowed at a time for a session.
293 * If this function is called while the previous one is still running, it returns an error.</p>
295 * @param[in] session The session to run inference
296 * @return @c NNFW_STATUS_NO_ERROR if successful
298 NNFW_STATUS nnfw_run_async(nnfw_session *session);
301 * @brief Wait for asynchronous run to finish
303 * <p>This function must be called after calling {@link nnfw_run_asnyc}, and can be called only once
304 * for a {@link nnfw_run_async} call.
306 * <p>When this function returns, it means that this session has finished the asynchronous run. Then
307 * the user can safely use the output data.</p>
309 * <p>This function returns after the asynchronous inference is finished.</p>
311 * @param[in] session The session to run inference
312 * @return @c NNFW_STATUS_NO_ERROR if successful
314 NNFW_STATUS nnfw_await(nnfw_session *session);
317 * @brief Set input buffer
319 * This function must be called after {@link nnfw_prepare}, \p buffer given to this function can be
320 * reused for many inferences. \p length must be greater or equal than the operand requires. To
321 * specify an optional input, you can either not call this for that input or call this with \p
322 * buffer of NULL and \p length of 0.
324 * @param[in] session Session to the input is to be set
325 * @param[in] index Index of input to be set (0-indexed)
326 * @param[in] type Type of the input
327 * @param[in] buffer Raw buffer for input
328 * @param[in] length Size of bytes of input buffer
330 * @return @c NNFW_STATUS_NO_ERROR if successful
332 NNFW_STATUS nnfw_set_input(nnfw_session *session, uint32_t index, NNFW_TYPE type,
333 const void *buffer, size_t length);
336 * @brief Set output buffer
338 * This function must be called after {@link nnfw_prepare}, \p buffer given to this function can be
339 * reused for many inferences. \p length must be greater or equal than the operand requires. An
340 * output operand can have unspecified shape and deduced dynamically during the execution. You must
341 * provide \p buffer large enough.
343 * @param[in] session Session from inference output is to be extracted
344 * @param[in] index Index of output to be set (0-indexed)
345 * @param[in] type Type of the output
346 * @param[out] buffer Raw buffer for output
347 * @param[in] length Size of bytes of output buffer
349 * @return @c NNFW_STATUS_NO_ERROR if successful
351 NNFW_STATUS nnfw_set_output(nnfw_session *session, uint32_t index, NNFW_TYPE type, void *buffer,
355 * @brief Get the number of inputs
357 * Application can call this function to get number of inputs defined in loaded model.
358 * This function should be called after {@link nnfw_load_model_from_file} is invoked to load model
360 * @param[in] session Session from input information is to be extracted
361 * @param[out] number Variable which the number of inputs is put into
363 * @return @c NNFW_STATUS_NO_ERROR if successful
365 NNFW_STATUS nnfw_input_size(nnfw_session *session, uint32_t *number);
368 * @brief Get the number of outputs
370 * Application can call this function to get number of outputs defined in loaded model.
371 * This function should be called after {@link nnfw_load_model_from_file} is invoked to load model
373 * @param[in] session Session from output information is to be extracted
374 * @param[out] number Variable which the number of outputs is put into
376 * @return @c NNFW_STATUS_NO_ERROR if successful
378 NNFW_STATUS nnfw_output_size(nnfw_session *session, uint32_t *number);
381 * @brief Set the layout of an input
383 * The input that does not call this has NNFW_LAYOUT_NHWC layout
385 * @param[in] session session from inference input is to be extracted
386 * @param[in] index index of input to be set (0-indexed)
387 * @param[in] layout layout to set to target input
389 * @return NNFW_STATUS_NO_ERROR if successful
391 NNFW_STATUS nnfw_set_input_layout(nnfw_session *session, uint32_t index, NNFW_LAYOUT layout);
394 * @brief Set the layout of an output
396 * The output that does not call this has NNFW_LAYOUT_NHWC layout
398 * @param[in] session session from inference output is to be extracted
399 * @param[in] index index of output to be set (0-indexed)
400 * @param[in] layout layout to set to target output
402 * @return NNFW_STATUS_NO_ERROR if successful
404 NNFW_STATUS nnfw_set_output_layout(nnfw_session *session, uint32_t index, NNFW_LAYOUT layout);
407 * @brief Get i-th input tensor info
409 * <p>Before {@link nnfw_prepare} is invoked, this function return tensor info in model,
410 * so updated tensor info by {@link nnfw_apply_tensorinfo} is not returned.</p>
412 * <p>After {@link nnfw_prepare} is invoked, this function return updated tensor info
413 * if tensor info is updated by {@link nnfw_apply_tensorinfo}.</p>
415 * @param[in] session Session from input information is to be extracted
416 * @param[in] index Index of input
417 * @param[out] tensor_info Tensor info (shape, type, etc)
419 * @return @c NNFW_STATUS_NO_ERROR if successful
421 NNFW_STATUS nnfw_input_tensorinfo(nnfw_session *session, uint32_t index,
422 nnfw_tensorinfo *tensor_info);
425 * @brief Get i-th output tensor info
427 * <p>After {@link nnfw_load_model_from_file} and before {@link nnfw_prepare} is invoked, it returns
428 * tensor info in the model.</p>
430 * <p>After {@link nnfw_prepare} and before {@link nnfw_run} is invoked, this function returns
431 * updated tensor info if tensor info is updated by {@link nnfw_set_input_tensorinfo}.</p>
433 * <p>After {@link nnfw_run} is invoked(at least once), it returns the updated tensor info during
434 * the latest execution.</p>
436 * @param[in] session Session from output information is to be extracted
437 * @param[in] index Index of output
438 * @param[out] tensor_info Tensor info (shape, type, etc)
440 * @return @c NNFW_STATUS_NO_ERROR if successful
442 NNFW_STATUS nnfw_output_tensorinfo(nnfw_session *session, uint32_t index,
443 nnfw_tensorinfo *tensor_info);
446 * @brief Set available backends
448 * This function should be called before {@link nnfw_prepare} is invoked.
450 * <p>Supported backends differs on each platforms.
451 * For example, `x86_64` supports "cpu" only.
452 * Multiple backends can be set and they must be separated by a semicolon (ex: "acl_cl;cpu").
453 * For each backend string, `libbackend_{backend}.so` will be dynamically loaded during
454 * {@link nnfw_prepare}.
455 * Among the multiple backends, the 1st element is used as the default backend.</p>
457 * @param[in] session session to which avilable backends are set
458 * @param[in] backends available backends on which nnfw uses
460 * @return @c NNFW_STATUS_NO_ERROR if successful
462 NNFW_STATUS nnfw_set_available_backends(nnfw_session *session, const char *backends);
465 * @brief Set the operation's backend
467 * This function should be called before {@link nnfw_prepare} is invoked.
469 * <p>The backend for op has higher priority than available backends specified by
470 * {@link nnfw_set_available_backends}.</p>
472 * @deprecated Deprecated since 1.8.0.
474 * @param[in] session session to be modified
475 * @param[in] op operation to be set
476 * @param[in] backend bakcend on which operation run
478 * @return @c NNFW_STATUS_NO_ERROR if successful
480 NNFW_STATUS nnfw_set_op_backend(nnfw_session *session, const char *op, const char *backend);
483 * @brief Retrieve uint32 type of nnfw information for given information ID.
485 * <p>Retrieves the information of property given by information id </p>
487 * @note: The input session could be null for global information (e.g. runtime version).*
489 * @param[in] session session to be queried on.
490 * @param[in] information ID to be queried
491 * @param[out] val uint32 value to be returned.
493 * @return @c NNFW_STATUS_NO_ERROR if successful
495 NNFW_STATUS nnfw_query_info_u32(nnfw_session *session, NNFW_INFO_ID id, uint32_t *val);