ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert-micro.cpp File Reference
#include <string>
#include <memory>
#include <thread>
#include <vector>
#include <iostream>
#include <fstream>
#include "OMTrainingInterpreter.h"
#include "onert-micro.h"
#include <circle-generated/circle/schema_generated.h>
#include <circle-generated/circle/traininfo_generated.h>

Go to the source code of this file.

Data Structures

struct  nnfw_session
 

Macros

#define NNFW_RETURN_ERROR_IF_NULL(p)
 

Typedefs

using DataBuffer = std::vector< char >
 

Functions

DataBuffer readFile (const char *path)
 
NNFW_STATUS nnfw_create_session (nnfw_session **session)
 Create a new 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)
 Export circle checkpoint.
 
NNFW_STATUS nnfw_train_import_checkpoint (nnfw_session *session, const char *path)
 Import circle checkpoint.
 
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_traininfo (nnfw_session *session, const nnfw_train_info *info)
 Set training information.
 
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_RETURN_ERROR_IF_NULL

#define NNFW_RETURN_ERROR_IF_NULL (   p)
Value:
do \
{ \
if ((p) == NULL) \
} while (0)
@ NNFW_STATUS_UNEXPECTED_NULL
Definition onert-micro.h:95

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

31 { \
32 if ((p) == NULL) \
34 } while (0)

Typedef Documentation

◆ DataBuffer

using DataBuffer = std::vector<char>

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

Function Documentation

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

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

Load model from path to model or nnpackage.

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}
SessionID session(const coco::Module *m)
Definition Session.cpp:48

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

Export circle checkpoint.

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

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.

Export 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)

◆ nnfw_train_import_checkpoint()

NNFW_STATUS nnfw_train_import_checkpoint ( nnfw_session session,
const char *  path 
)

Import circle checkpoint.

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

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}

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

◆ readFile()

DataBuffer readFile ( const char *  path)

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

40{
41 std::ifstream file(path, std::ios::binary | std::ios::in);
42 if (!file.good())
43 {
44 std::string errmsg = "Failed to open file";
45 std::cerr << errmsg << std::endl;
46 exit(-1); // FIXME: proper way
47 }
48
49 file.seekg(0, std::ios::end);
50 auto fileSize = file.tellg();
51 file.seekg(0, std::ios::beg);
52
53 // reserve capacity
54 DataBuffer model_data(fileSize);
55
56 // read the data
57 file.read(model_data.data(), fileSize);
58 if (file.fail())
59 {
60 std::string errmsg = "Failed to read file";
61 std::cerr << errmsg << std::endl;
62 exit(-1); // FIXME: proper way
63 }
64
65 return model_data;
66}
model_data
Definition infer.py:82
std::vector< char > DataBuffer

Referenced by entry(), and nnfw_session::load_model_from_file().