ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert-micro.h File Reference

Go to the source code of this file.

Data Structures

struct  nnfw_tensorinfo
 tensor info describes the type and shape of tensors More...
 
struct  nnfw_loss_info
 
struct  nnfw_adam_option
 
struct  nnfw_train_info
 Training information to prepare training. More...
 

Macros

#define NNFW_MAX_RANK   (6)
 Maximum rank expressible with nnfw.
 
#define NNFW_TRAINABLE_OPS_MAX_SIZE   (256)
 Maximum numer of trainable operations.
 

Typedefs

typedef struct nnfw_session nnfw_session
 
typedef struct nnfw_tensorinfo nnfw_tensorinfo
 tensor info describes the type and shape of tensors
 
typedef struct nnfw_loss_info nnfw_loss_info
 
typedef struct nnfw_adam_option nnfw_adam_option
 
typedef struct nnfw_train_info nnfw_train_info
 Training information to prepare training.
 

Enumerations

enum  NNFW_TYPE { NNFW_TYPE_TENSOR_FLOAT32 = 0 , NNFW_TYPE_TENSOR_INT32 = 1 }
 
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_TRAIN_LOSS { NNFW_TRAIN_LOSS_UNDEFINED = 0 , NNFW_TRAIN_LOSS_MEAN_SQUARED_ERROR = 1 , NNFW_TRAIN_LOSS_CATEGORICAL_CROSSENTROPY = 2 }
 
enum  NNFW_TRAIN_LOSS_REDUCTION { NNFW_TRAIN_LOSS_REDUCTION_UNDEFINED = 0 , NNFW_TRAIN_LOSS_REDUCTION_SUM_OVER_BATCH_SIZE = 1 , NNFW_TRAIN_LOSS_REDUCTION_SUM = 2 }
 
enum  NNFW_TRAIN_OPTIMIZER { NNFW_TRAIN_OPTIMIZER_UNDEFINED = 0 , NNFW_TRAIN_OPTIMIZER_SGD = 1 , NNFW_TRAIN_OPTIMIZER_ADAM = 2 }
 

Functions

NNFW_STATUS nnfw_train_set_traininfo (nnfw_session *session, const nnfw_train_info *info)
 Set training information.
 
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 *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_export_circle (nnfw_session *session, const char *path)
 Export current training model into circle model.
 
NNFW_STATUS nnfw_train_export_checkpoint (nnfw_session *session, const char *path)
 
NNFW_STATUS nnfw_train_import_checkpoint (nnfw_session *session, const char *path)
 
NNFW_STATUS nnfw_train_set_input (nnfw_session *session, uint32_t index, void *input, const nnfw_tensorinfo *input_info)
 Set training input.
 
NNFW_STATUS nnfw_train_set_expected (nnfw_session *session, uint32_t index, void *expected, const nnfw_tensorinfo *expected_info)
 Set training expected output.
 
NNFW_STATUS nnfw_train_get_loss (nnfw_session *session, uint32_t index, float *loss)
 Get loss value for expected output.
 
NNFW_STATUS nnfw_train_set_output (nnfw_session *session, uint32_t index, NNFW_TYPE type, void *buffer, size_t length)
 Set training output buffer.
 

Macro Definition Documentation

◆ NNFW_MAX_RANK

#define NNFW_MAX_RANK   (6)

Maximum rank expressible with nnfw.

Definition at line 109 of file onert-micro.h.

◆ NNFW_TRAINABLE_OPS_MAX_SIZE

#define NNFW_TRAINABLE_OPS_MAX_SIZE   (256)

Maximum numer of trainable operations.

Definition at line 179 of file onert-micro.h.

Typedef Documentation

◆ nnfw_adam_option

◆ nnfw_loss_info

◆ nnfw_session

typedef struct nnfw_session nnfw_session

Definition at line 72 of file onert-micro.h.

◆ 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]

◆ nnfw_train_info

Training information to prepare training.

Enumeration Type Documentation

◆ NNFW_STATUS

Result values returned from a call to an API function.

Enumerator
NNFW_STATUS_NO_ERROR 

Successful

NNFW_STATUS_ERROR 

An error code for general use. Mostly used when there is no specific value for that certain situation.

NNFW_STATUS_UNEXPECTED_NULL 

Unexpected null argument is given.

NNFW_STATUS_INVALID_STATE 

When a function was called but it is not valid for the current session state.

NNFW_STATUS_OUT_OF_MEMORY 

When it is out of memory

NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE 

When it was given an insufficient output buffer

NNFW_STATUS_DEPRECATED_API 

When API is deprecated

Definition at line 85 of file onert-micro.h.

86{
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_TRAIN_LOSS

Enumerator
NNFW_TRAIN_LOSS_UNDEFINED 
NNFW_TRAIN_LOSS_MEAN_SQUARED_ERROR 
NNFW_TRAIN_LOSS_CATEGORICAL_CROSSENTROPY 

Definition at line 139 of file onert-micro.h.

140{
NNFW_TRAIN_LOSS
@ NNFW_TRAIN_LOSS_MEAN_SQUARED_ERROR
@ NNFW_TRAIN_LOSS_UNDEFINED
@ NNFW_TRAIN_LOSS_CATEGORICAL_CROSSENTROPY

◆ NNFW_TRAIN_LOSS_REDUCTION

Enumerator
NNFW_TRAIN_LOSS_REDUCTION_UNDEFINED 

Undefined

NNFW_TRAIN_LOSS_REDUCTION_SUM_OVER_BATCH_SIZE 

Scalar sum divided by number of elements in losses

NNFW_TRAIN_LOSS_REDUCTION_SUM 

Scalar sum of weighted losses

Definition at line 146 of file onert-micro.h.

147{
NNFW_TRAIN_LOSS_REDUCTION
@ NNFW_TRAIN_LOSS_REDUCTION_UNDEFINED
@ NNFW_TRAIN_LOSS_REDUCTION_SUM
@ NNFW_TRAIN_LOSS_REDUCTION_SUM_OVER_BATCH_SIZE

◆ NNFW_TRAIN_OPTIMIZER

Enumerator
NNFW_TRAIN_OPTIMIZER_UNDEFINED 
NNFW_TRAIN_OPTIMIZER_SGD 
NNFW_TRAIN_OPTIMIZER_ADAM 

Definition at line 156 of file onert-micro.h.

157{
NNFW_TRAIN_OPTIMIZER
@ NNFW_TRAIN_OPTIMIZER_ADAM
@ NNFW_TRAIN_OPTIMIZER_SGD
@ NNFW_TRAIN_OPTIMIZER_UNDEFINED

◆ NNFW_TYPE

enum NNFW_TYPE
Enumerator
NNFW_TYPE_TENSOR_FLOAT32 

A tensor of 32 bit floating point

NNFW_TYPE_TENSOR_INT32 

A tensor of 32 bit signed integer

Definition at line 74 of file onert-micro.h.

75{
80} NNFW_TYPE;
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

Function Documentation

◆ nnfw_close_session()

NNFW_STATUS nnfw_close_session ( nnfw_session session)

Close a session instance.

After called, access to closed session by application will be invalid

Parameters
[in]sessionThe session to be closed
Returns
NNFW_STATUS_NO_ERROR if successful

Definition at line 60 of file nnfw_api.cc.

61{
62 delete session;
64}
SessionID session(const coco::Module *m)
Definition Session.cpp:48

References NNFW_STATUS_NO_ERROR.

Referenced by NNFW_SESSION::close_session(), and main().

◆ nnfw_create_session()

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

Parameters
[out]sessionThe session to be created
Returns
NNFW_STATUS_NO_ERROR if successful

Definition at line 390 of file onert-micro.cpp.

390{ return nnfw_session::create(session); }
static NNFW_STATUS create(nnfw_session **session)
Factory method. It creates and initialize nnfw_session.

References nnfw_session::create().

Referenced by main(), and NNFW_SESSION::NNFW_SESSION().

◆ nnfw_load_model_from_file()

NNFW_STATUS nnfw_load_model_from_file ( nnfw_session session,
const char *  package_file_path 
)

Load model from nnpackage file or directory.

The length of package_file_path must not exceed 1024 bytes including zero at the end.

Parameters
[in]sessionnnfw_session loading the given nnpackage file/dir
[in]package_file_pathPath to the nnpackage file or unzipped directory to be loaded
Returns
NNFW_STATUS_NO_ERROR if successful

Definition at line 392 of file onert-micro.cpp.

393{
394 return session->load_model_from_file(package_file_path);
395}

Referenced by main(), and NNFW_SESSION::NNFW_SESSION().

◆ nnfw_train()

NNFW_STATUS nnfw_train ( nnfw_session session,
bool  update_weights 
)

Train the model.

Note
This function should be called after nnfw_train_set_input and nnfw_train_set_expected for each input and expected output

In order to use update_weights as false, it should be called after nnfw_train_set_output.

Parameters
[in]sessionThe session to be trained
[in]update_weightsIf true, update weights of the model If false, do not update weights of the model (for validation)
Returns
NNFW_STATUS_NO_ERROR if successful

Definition at line 399 of file onert-micro.cpp.

400{
401 return session->train_run(update_weights);
402}

◆ nnfw_train_export_checkpoint()

NNFW_STATUS nnfw_train_export_checkpoint ( nnfw_session session,
const char *  path 
)

Definition at line 409 of file onert-micro.cpp.

410{
411 return session->train_export_checkpoint(path);
412}

◆ nnfw_train_export_circle()

NNFW_STATUS nnfw_train_export_circle ( nnfw_session session,
const char *  path 
)

Export current training model into circle model.

Note
This function should be called on training mode This function should be called after nnfw_train
Parameters
[in]sessionThe session to export inference model
[in]pathThe path to export inference model
Returns
NNFW_STATUS_NO_ERROR if successful

Definition at line 404 of file onert-micro.cpp.

405{
406 return session->train_export_circle(path);
407}

◆ nnfw_train_get_loss()

NNFW_STATUS nnfw_train_get_loss ( nnfw_session session,
uint32_t  index,
float *  loss 
)

Get loss value for expected output.

Note
This function should be called after nnfw_train
Parameters
[in]sessionThe session to get loss value
[in]indexThe index of loss value [0, number of expected outputs)
[out]lossThe loss value
Returns
NNFW_STATUS_NO_ERROR if successful

Definition at line 433 of file onert-micro.cpp.

434{
436 return session->train_get_loss(index, loss);
437}
#define NNFW_RETURN_ERROR_IF_NULL(p)

References NNFW_RETURN_ERROR_IF_NULL.

◆ nnfw_train_import_checkpoint()

NNFW_STATUS nnfw_train_import_checkpoint ( nnfw_session session,
const char *  path 
)

Definition at line 414 of file onert-micro.cpp.

415{
416 return session->train_import_checkpoint(path);
417}

◆ nnfw_train_prepare()

NNFW_STATUS nnfw_train_prepare ( nnfw_session session)

Prepare session to be ready for training.

Note
The session will be entered into training mode
   If training info is NOT set in session, this function returns @c NNFW_STATUS_ERROR .
   You should set training info using {@link nnfw_train_set_traininfo}.
Parameters
[in]sessionThe session to be prepared for training
Returns
NNFW_STATUS_NO_ERROR if successful

Definition at line 397 of file onert-micro.cpp.

397{ return session->train_prepare(); }

◆ nnfw_train_set_expected()

NNFW_STATUS nnfw_train_set_expected ( nnfw_session session,
uint32_t  index,
void *  expected,
const nnfw_tensorinfo expected_info 
)

Set training expected output.

Note
This function should be called after nnfw_train_prepare
Parameters
sessionThe session to be set training inputs and expected model outputs
indexThe index of training expected output
expectedThe expected buffers for training
expected_infoThe shape and type of expected buffer If it is nullptr, it will not change shape and batch size
Returns
NNFW_STATUS_NO_ERROR if successful

Definition at line 426 of file onert-micro.cpp.

428{
430 return session->train_set_expected(index, expected);
431}

References NNFW_RETURN_ERROR_IF_NULL.

◆ nnfw_train_set_input()

NNFW_STATUS nnfw_train_set_input ( nnfw_session session,
uint32_t  index,
void *  input,
const nnfw_tensorinfo input_info 
)

Set training input.

Note
This function should be called after nnfw_train_prepare
Parameters
[in]sessionThe session to be set training inputs and expected model outputs
[in]indexThe index of training input
[in]inputThe input buffers for training
[in]input_infoThe shape and type of input buffer If it is nullptr, it will not change shape and batch size
Returns
NNFW_STATUS_NO_ERROR if successful

Definition at line 419 of file onert-micro.cpp.

421{
423 return session->train_set_input(index, input);
424}

References NNFW_RETURN_ERROR_IF_NULL.

◆ nnfw_train_set_output()

NNFW_STATUS nnfw_train_set_output ( nnfw_session session,
uint32_t  index,
NNFW_TYPE  type,
void *  buffer,
size_t  length 
)

Set training output buffer.

This function must be called after nnfw_train_prepare, buffer given to this function can be reused for training. 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.

Parameters
[in]sessionSession from inference output is to be extracted
[in]indexIndex of output to be set (0-indexed)
[in]typeType of the output
[out]bufferRaw buffer for output
[in]lengthSize of bytes of output buffer
Returns
NNFW_STATUS_NO_ERROR if successful

Definition at line 445 of file onert-micro.cpp.

447{
449 return session->train_set_output(index, type, buffer, length);
450}

References NNFW_RETURN_ERROR_IF_NULL.

◆ nnfw_train_set_traininfo()

NNFW_STATUS nnfw_train_set_traininfo ( nnfw_session session,
const nnfw_train_info info 
)

Set training information.

Note
This function should be called after calling nnfw_load_model_from_file and before calling nnfw_train_prepare
Parameters
[in]sessionThe session to be set training information
[in]infoThe training information
Returns
NNFW_STATUS_NO_ERROR if successful

Definition at line 439 of file onert-micro.cpp.

440{
442 return session->train_set_traininfo(info);
443}
volatile const char info[]

References info, and NNFW_RETURN_ERROR_IF_NULL.