ONE - On-device Neural Engine
|
This file describes runtime API. More...
#include <stddef.h>
#include <stdint.h>
Go to the source code of this file.
Data Structures | |
struct | nnfw_tensorinfo |
tensor info describes the type and shape of tensors More... | |
Macros | |
#define | NNFW_MAX_RANK (6) |
Maximum rank expressible with nnfw. | |
Typedefs | |
typedef struct nnfw_session | nnfw_session |
Session to query with runtime. | |
typedef struct nnfw_tensorinfo | nnfw_tensorinfo |
tensor info describes the type and shape of tensors | |
Enumerations | |
enum | NNFW_TYPE { NNFW_TYPE_TENSOR_FLOAT32 = 0 , NNFW_TYPE_TENSOR_INT32 = 1 , NNFW_TYPE_TENSOR_QUANT8_ASYMM = 2 , NNFW_TYPE_TENSOR_BOOL = 3 , NNFW_TYPE_TENSOR_UINT8 = 4 , NNFW_TYPE_TENSOR_INT64 = 5 , NNFW_TYPE_TENSOR_QUANT8_ASYMM_SIGNED = 6 , NNFW_TYPE_TENSOR_QUANT16_SYMM_SIGNED = 7 } |
Tensor types. More... | |
enum | NNFW_STATUS { NNFW_STATUS_NO_ERROR = 0 , NNFW_STATUS_ERROR = 1 , NNFW_STATUS_UNEXPECTED_NULL = 2 , NNFW_STATUS_INVALID_STATE = 3 , NNFW_STATUS_OUT_OF_MEMORY = 4 , NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE = 5 , NNFW_STATUS_DEPRECATED_API = 6 } |
Result values returned from a call to an API function. More... | |
enum | NNFW_LAYOUT { NNFW_LAYOUT_NONE = 0 , NNFW_LAYOUT_CHANNELS_LAST = 1 , NNFW_LAYOUT_CHANNELS_FIRST = 2 } |
Data format of a tensor. More... | |
enum | NNFW_INFO_ID { NNFW_INFO_ID_VERSION = 0 } |
Information ID for retrieving information on nnfw (e.g. version) More... | |
Functions | |
NNFW_STATUS | nnfw_create_session (nnfw_session **session) |
Create a new session instance. | |
NNFW_STATUS | nnfw_close_session (nnfw_session *session) |
Close a session instance. | |
NNFW_STATUS | nnfw_load_model_from_file (nnfw_session *session, const char *path) |
Load model from path to model or nnpackage. | |
NNFW_STATUS | nnfw_apply_tensorinfo (nnfw_session *session, uint32_t index, nnfw_tensorinfo tensor_info) |
Apply i-th input's tensor info to resize input tensor. | |
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. | |
NNFW_STATUS | nnfw_prepare (nnfw_session *session) |
Prepare session to be ready for inference. | |
NNFW_STATUS | nnfw_run (nnfw_session *session) |
Run inference. | |
NNFW_STATUS | nnfw_run_async (nnfw_session *session) |
Run inference asynchronously. | |
NNFW_STATUS | nnfw_await (nnfw_session *session) |
Wait for asynchronous run to finish. | |
NNFW_STATUS | nnfw_set_input (nnfw_session *session, uint32_t index, NNFW_TYPE type, const void *buffer, size_t length) |
Set input buffer. | |
NNFW_STATUS | nnfw_set_output (nnfw_session *session, uint32_t index, NNFW_TYPE type, void *buffer, size_t length) |
Set output buffer. | |
NNFW_STATUS | nnfw_input_size (nnfw_session *session, uint32_t *number) |
Get the number of inputs. | |
NNFW_STATUS | nnfw_output_size (nnfw_session *session, uint32_t *number) |
Get the number of outputs. | |
NNFW_STATUS | nnfw_set_input_layout (nnfw_session *session, uint32_t index, NNFW_LAYOUT layout) |
Set the layout of an input. | |
NNFW_STATUS | nnfw_set_output_layout (nnfw_session *session, uint32_t index, NNFW_LAYOUT layout) |
Set the layout of an output. | |
NNFW_STATUS | nnfw_input_tensorinfo (nnfw_session *session, uint32_t index, nnfw_tensorinfo *tensor_info) |
Get i-th input tensor info. | |
NNFW_STATUS | nnfw_output_tensorinfo (nnfw_session *session, uint32_t index, nnfw_tensorinfo *tensor_info) |
Get i-th output tensor info. | |
NNFW_STATUS | nnfw_set_available_backends (nnfw_session *session, const char *backends) |
Set available backends. | |
NNFW_STATUS | nnfw_set_op_backend (nnfw_session *session, const char *op, const char *backend) |
Set the operation's backend. | |
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. | |
NNFW_STATUS | nnfw_set_workspace (nnfw_session *session, const char *dir) |
Set runtime's workspace directory. | |
This file describes runtime API.
Definition in file nnfw.h.
#define NNFW_MAX_RANK (6) |
typedef struct nnfw_session nnfw_session |
Session to query with runtime.
nnfw_session is started and passed by calling nnfw_create_session
. Each session has its own inference environment, such as model to inference, backend usage, etc.
Load model by calling nnfw_load_model_from_file
After loading, prepare inference by calling nnfw_prepare
. Application can set runtime environment before prepare by calling nnfw_set_available_backends
, and it is optional.
Application can inference by calling nnfw_run
. Before inference, application has responsibility to set input tensor to set input data by calling nnfw_set_output
, and output tensor to get output by calling nnfw_set_input
To support input and output setting, application can get input and output tensor information by calling
Application can inference many times using one session, but next inference can do after prior inference end
Application cannot use muitiple model using one session
typedef struct nnfw_tensorinfo nnfw_tensorinfo |
tensor info describes the type and shape of tensors
This structure is used to describe input and output tensors. Application can get input and output tensor type and shape described in model by using nnfw_input_tensorinfo
and nnfw_output_tensorinfo
Maximum rank is 6 (NNFW_MAX_RANK). And tensor's dimension value is filled in 'dims' field from index 0. For example, if tensor's rank is 4, application can get dimension value from dims[0], dims[1], dims[2], and dims[3]
enum NNFW_INFO_ID |
Information ID for retrieving information on nnfw (e.g. version)
Enumerator | |
---|---|
NNFW_INFO_ID_VERSION | nnfw runtime version Its value is uint32 in 0xMMmmmmPP, where MM = major, mmmm = minor, PP = patch. |
Definition at line 152 of file nnfw.h.
enum NNFW_LAYOUT |
Data format of a tensor.
Enumerator | |
---|---|
NNFW_LAYOUT_NONE | Don't care layout |
NNFW_LAYOUT_CHANNELS_LAST | Channel last layout If rank is 4, layout is NHWC |
NNFW_LAYOUT_CHANNELS_FIRST | Channel first layout If rank is 4, layout is NCHW |
Definition at line 133 of file nnfw.h.
enum NNFW_STATUS |
Result values returned from a call to an API function.
Definition at line 109 of file nnfw.h.
enum NNFW_TYPE |
Tensor types.
The type of tensor represented in nnfw_tensorinfo
Definition at line 67 of file nnfw.h.
NNFW_STATUS nnfw_apply_tensorinfo | ( | nnfw_session * | session, |
uint32_t | index, | ||
nnfw_tensorinfo | tensor_info | ||
) |
Apply i-th input's tensor info to resize input tensor.
This function should be called before nnfw_prepare
is invoked, and should be called after nnfw_load_model_from_file
is invoked See nnfw_prepare
for information applying updated tensor info If this function is called many times for same index, tensor info is overwritten
[in] | session | Session to the input tensor info is to be set |
[in] | index | Index of input to be applied (0-indexed) |
[in] | tensor_info | Tensor info to be applied |
NNFW_STATUS_NO_ERROR
if successful, otherwise return NNFW_STATUS_ERROR
Definition at line 155 of file nnfw_api.cc.
References nnfw_session::deprecated().
NNFW_STATUS nnfw_await | ( | nnfw_session * | session | ) |
Wait for asynchronous run to finish.
This function must be called after calling nnfw_run_async
, and can be called only once for a nnfw_run_async
call.
When this function returns, it means that this session has finished the asynchronous run. Then the user can safely use the output data.
This function returns after the asynchronous inference is finished.
[in] | session | The session to run inference |
NNFW_STATUS_NO_ERROR
if successful Definition at line 90 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by NNFW_SESSION::wait().
NNFW_STATUS nnfw_close_session | ( | nnfw_session * | session | ) |
Close a session instance.
After called, access to closed session by application will be invalid
[in] | session | The session to be closed |
NNFW_STATUS_NO_ERROR
if successful Definition at line 60 of file nnfw_api.cc.
NNFW_STATUS nnfw_create_session | ( | nnfw_session ** | session | ) |
Create a new session instance.
This only creates a session. Model is loaded after nnfw_load_model_from_file
is invoked. And inference is performed after nnfw_run
is invoked.
nnfw_close_session
should be called once if session is no longer needed
[out] | session | The session to be created |
Definition at line 390 of file onert-micro.cpp.
References nnfw_session::create().
NNFW_STATUS nnfw_input_size | ( | nnfw_session * | session, |
uint32_t * | number | ||
) |
Get the number of inputs.
Application can call this function to get number of inputs defined in loaded model. This function should be called after nnfw_load_model_from_file
is invoked to load model
[in] | session | Session from input information is to be extracted |
[out] | number | Variable which the number of inputs is put into |
NNFW_STATUS_NO_ERROR
if successful Definition at line 110 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by NNFW_SESSION::input_size().
NNFW_STATUS nnfw_input_tensorinfo | ( | nnfw_session * | session, |
uint32_t | index, | ||
nnfw_tensorinfo * | tensor_info | ||
) |
Get i-th input tensor info.
Before nnfw_prepare
is invoked, this function return tensor info in model, so updated tensor info by nnfw_apply_tensorinfo
is not returned.
After nnfw_prepare
is invoked, this function return updated tensor info if tensor info is updated by nnfw_apply_tensorinfo
.
[in] | session | Session from input information is to be extracted |
[in] | index | Index of input |
[out] | tensor_info | Tensor info (shape, type, etc) |
NNFW_STATUS_NO_ERROR
if successful Definition at line 134 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by NNFW_SESSION::input_tensorinfo(), main(), and NNFW_SESSION::set_input().
NNFW_STATUS nnfw_load_model_from_file | ( | nnfw_session * | session, |
const char * | path | ||
) |
Load model from path to model or nnpackage.
The length of path
must not exceed 1024 bytes including zero at the end.
[in] | session | nnfw_session loading the given file/dir |
[in] | path | Path to the model file or nnpackage directory to be loaded |
NNFW_STATUS_NO_ERROR
if successfulLoad model from path to model or nnpackage.
The length of package_file_path
must not exceed 1024 bytes including zero at the end.
[in] | session | nnfw_session loading the given nnpackage file/dir |
[in] | package_file_path | Path to the nnpackage file or unzipped directory to be loaded |
NNFW_STATUS_NO_ERROR
if successful Definition at line 392 of file onert-micro.cpp.
References NNFW_RETURN_ERROR_IF_NULL.
NNFW_STATUS nnfw_output_size | ( | nnfw_session * | session, |
uint32_t * | number | ||
) |
Get the number of outputs.
Application can call this function to get number of outputs defined in loaded model. This function should be called after nnfw_load_model_from_file
is invoked to load model
[in] | session | Session from output information is to be extracted |
[out] | number | Variable which the number of outputs is put into |
NNFW_STATUS_NO_ERROR
if successful Definition at line 116 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by NNFW_SESSION::output_size().
NNFW_STATUS nnfw_output_tensorinfo | ( | nnfw_session * | session, |
uint32_t | index, | ||
nnfw_tensorinfo * | tensor_info | ||
) |
Get i-th output tensor info.
After nnfw_load_model_from_file
and before nnfw_prepare
is invoked, it returns tensor info in the model.
After nnfw_prepare
and before nnfw_run
is invoked, this function returns updated tensor info if tensor info is updated by nnfw_set_input_tensorinfo
.
After nnfw_run
is invoked(at least once), it returns the updated tensor info during the latest execution.
[in] | session | Session from output information is to be extracted |
[in] | index | Index of output |
[out] | tensor_info | Tensor info (shape, type, etc) |
NNFW_STATUS_NO_ERROR
if successful Definition at line 141 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by main(), NNFW_SESSION::output_tensorinfo(), and NNFW_SESSION::set_output().
NNFW_STATUS nnfw_prepare | ( | nnfw_session * | session | ) |
Prepare session to be ready for inference.
This phase may finalize model compilation, scheduling, and additional settings. If nnfw_apply_tensorinfo
is called to apply input tensor info different with model before this function, tries to resize all tensors.
[in] | session | the session to be prepared |
NNFW_STATUS_NO_ERROR
if successful, otherwise return NNFW_STATUS_ERROR
Definition at line 72 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by main(), and NNFW_SESSION::NNFW_SESSION().
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.
Retrieves the information of property given by information id
[in] | session | session to be queried on. |
[in] | id | ID to be queried |
[out] | val | uint32 value to be returned. |
NNFW_STATUS_NO_ERROR
if successful Definition at line 178 of file nnfw_api.cc.
References NNFW_INFO_ID_VERSION, NNFW_STATUS_ERROR, NNFW_STATUS_NO_ERROR, and NNFW_VERSION.
NNFW_STATUS nnfw_run | ( | nnfw_session * | session | ) |
Run inference.
This function should be called after model is loaded by nnfw_load_model_from_file
, session is prepared for inference by nnfw_prepare
, set input and output buffers by nnfw_set_input
and nnfw_set_output
.
This function return after inference is finished.
[in] | session | The session to run inference |
NNFW_STATUS_NO_ERROR
if successful Definition at line 78 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by main(), and NNFW_SESSION::run().
NNFW_STATUS nnfw_run_async | ( | nnfw_session * | session | ) |
Run inference asynchronously.
This function must be called after model is loaded by nnfw_load_model_from_file
, session is prepared for inference by nnfw_prepare
, set input and output buffers by nnfw_set_input
and nnfw_set_output
.
This function returns immediately after starting a thread to run the inference. To get the result of it or to do the next inference with nnfw_run
or nnfw_run_async
, nnfw_await
must be called to ensure the current asynchronous inference has finished. Only one asynchronous inference is allowed at a time for a session. If this function is called while the previous one is still running, it returns an error.
[in] | session | The session to run inference |
NNFW_STATUS_NO_ERROR
if successful Definition at line 84 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by NNFW_SESSION::run_async().
NNFW_STATUS nnfw_set_available_backends | ( | nnfw_session * | session, |
const char * | backends | ||
) |
Set available backends.
This function should be called before nnfw_prepare
is invoked.
Supported backends differs on each platforms. For example, x86_64
supports "cpu" only. Multiple backends can be set and they must be separated by a semicolon (ex: "acl_cl;cpu"). For each backend string, libbackend_{backend}.so
will be dynamically loaded during nnfw_prepare
. Among the multiple backends, the 1st element is used as the default backend.
[in] | session | session to which avilable backends are set |
[in] | backends | available backends on which nnfw uses |
NNFW_STATUS_NO_ERROR
if successful Definition at line 167 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by NNFW_SESSION::NNFW_SESSION().
NNFW_STATUS nnfw_set_input | ( | nnfw_session * | session, |
uint32_t | index, | ||
NNFW_TYPE | type, | ||
const void * | buffer, | ||
size_t | length | ||
) |
Set input buffer.
This function must be called after nnfw_prepare
, buffer
given to this function can be reused for many inferences. length
must be greater or equal than the operand requires. To specify an optional input, you can either not call this for that input or call this with buffer
of NULL and length
of 0. If you set NNFW_TYPE_TENSOR_FLOAT32
type and model has quantized input type on given index, runtime will set quantized data type model input by converting from float buffer data internally.
[in] | session | Session to the input is to be set |
[in] | index | Index of input to be set (0-indexed) |
[in] | type | Type of the input |
[in] | buffer | Raw buffer for input |
[in] | length | Size of bytes of input buffer |
NNFW_STATUS_NO_ERROR
if successful Definition at line 96 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by main(), and NNFW_SESSION::set_input().
NNFW_STATUS nnfw_set_input_layout | ( | nnfw_session * | session, |
uint32_t | index, | ||
NNFW_LAYOUT | layout | ||
) |
Set the layout of an input.
The input that does not call this has NNFW_LAYOUT_NHWC layout
[in] | session | session from inference input is to be extracted |
[in] | index | index of input to be set (0-indexed) |
[in] | layout | layout to set to target input |
Definition at line 122 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by NNFW_SESSION::set_input_layout().
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.
This function can be called at any time after calling nnfw_load_model_from_file
. Changing input tensor's shape will cause shape inference for the model. There are two different types of shape inference - static and dynamic. Which one to use is depend on the current state of the session. When it is called after calling nnfw_load_model_from_file
and before calling nnfw_prepare
, this info will be used when nnfw_prepare
. And it will perform static shape inference for all tensors. When it is called after calling nnfw_prepare
or even after nnfw_run
, this info will be used when nnfw_run
. And the shapes of the tensors are determined on the fly. If this function is called many times for the same index, it is overwritten. tensor_info's dtype field is ignored.
[in] | session | Session to the input tensor info is to be set |
[in] | index | Index of input to be set (0-indexed) |
[in] | tensor_info | Tensor info to be set |
NNFW_STATUS_NO_ERROR
if successful, otherwise return NNFW_STATUS_ERROR
Definition at line 160 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by NNFW_SESSION::set_input_tensorinfo().
NNFW_STATUS nnfw_set_op_backend | ( | nnfw_session * | session, |
const char * | op, | ||
const char * | backend | ||
) |
Set the operation's backend.
This function should be called before nnfw_prepare
is invoked.
The backend for op has higher priority than available backends specified by nnfw_set_available_backends
.
[in] | session | session to be modified |
[in] | op | operation to be set |
[in] | backend | bakcend on which operation run |
NNFW_STATUS_NO_ERROR
if successful Definition at line 173 of file nnfw_api.cc.
References nnfw_session::deprecated().
Referenced by main().
NNFW_STATUS nnfw_set_output | ( | nnfw_session * | session, |
uint32_t | index, | ||
NNFW_TYPE | type, | ||
void * | buffer, | ||
size_t | length | ||
) |
Set output buffer.
This function must be called after nnfw_prepare
, buffer
given to this function can be reused for many inferences. length
must be greater or equal than the operand requires. An output operand can have unspecified shape and deduced dynamically during the execution. You must provide buffer
large enough. If you set NNFW_TYPE_TENSOR_FLOAT32
type and model has quantized output type on given index, runtime will set dequantized float buffer data from quantize data type model output internally.
[in] | session | Session from inference output is to be extracted |
[in] | index | Index of output to be set (0-indexed) |
[in] | type | Type of the output |
[out] | buffer | Raw buffer for output |
[in] | length | Size of bytes of output buffer |
NNFW_STATUS_NO_ERROR
if successful Definition at line 103 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by main(), and NNFW_SESSION::set_output().
NNFW_STATUS nnfw_set_output_layout | ( | nnfw_session * | session, |
uint32_t | index, | ||
NNFW_LAYOUT | layout | ||
) |
Set the layout of an output.
The output that does not call this has NNFW_LAYOUT_NHWC layout
[in] | session | session from inference output is to be extracted |
[in] | index | index of output to be set (0-indexed) |
[in] | layout | layout to set to target output |
Definition at line 128 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.
Referenced by NNFW_SESSION::set_output_layout().
NNFW_STATUS nnfw_set_workspace | ( | nnfw_session * | session, |
const char * | dir | ||
) |
Set runtime's workspace directory.
This function sets the directory to be used as a workspace. System should allow read and write access to the directory for the runtime. Default workspace is running directory of the application. This function should be called before nnfw_load_model_from_file
is invoked.
[in] | session | session to be queried on. |
[in] | dir | workspace directory path |
NNFW_STATUS_NO_ERROR
if successful Definition at line 230 of file nnfw_api.cc.
References NNFW_RETURN_ERROR_IF_NULL.