ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
onert::loader Namespace Reference

Namespaces

namespace  train
 

Data Structures

class  BaseLoader
 
class  ILoader
 

Functions

std::unique_ptr< ir::ModelloadCircleModel (const std::string &filename)
 
std::unique_ptr< ir::ModelloadCircleModel (uint8_t *buffer, size_t size)
 
std::unique_ptr< ir::ModelloadModel (const std::string &filename, const std::string &type)
 Create custom loader and load model from file.
 
std::unique_ptr< ir::ModelloadTFLiteModel (const std::string &filename)
 
std::unique_ptr< ir::train::TrainingInfoloadTrainingInfo (const uint8_t *buffer, const size_t size)
 
template<typename T >
bool Copy (const T *data_ptr, std::vector< uint16_t > &arr)
 

Variables

const char *const TRAININFO_METADATA_NAME = "CIRCLE_TRAINING"
 

Function Documentation

◆ Copy()

template<typename T >
bool onert::loader::Copy ( const T *  data_ptr,
std::vector< uint16_t > &  arr 
)

Definition at line 344 of file BaseLoader.h.

345{
346 if (data_ptr->values() == nullptr)
347 {
348 return false;
349 }
350
351 int size = data_ptr->values()->size();
352 arr.reserve(size);
353 for (int i = 0; i < size; i++)
354 {
355 arr.emplace_back(static_cast<uint16_t>(data_ptr->values()->Get(i)));
356 }
357 return true;
358}
int32_t size[5]
Definition Slice.cpp:35

References size.

Referenced by onert::loader::BaseLoader< LoaderDomain >::loadSparsity().

◆ loadCircleModel() [1/2]

std::unique_ptr< ir::Model > onert::loader::loadCircleModel ( const std::string &  filename)

Definition at line 283 of file CircleLoader.cc.

284{
285 auto model = std::make_unique<ir::Model>();
286 CircleLoader loader(model);
287 loader.loadFromFile(filename);
288 return model;
289}

Referenced by nnfw_session::load_circle_from_buffer().

◆ loadCircleModel() [2/2]

std::unique_ptr< ir::Model > onert::loader::loadCircleModel ( uint8_t *  buffer,
size_t  size 
)

Definition at line 291 of file CircleLoader.cc.

292{
293 auto model = std::make_unique<ir::Model>();
294 CircleLoader loader(model);
295 loader.loadFromBuffer(buffer, size);
296 return model;
297}

References size.

◆ loadModel()

std::unique_ptr< ir::Model > onert::loader::loadModel ( const std::string &  filename,
const std::string &  type 
)

Create custom loader and load model from file.

Parameters
[in]filenameFile path to load model from
[in]typeType of model to load
Returns
Loaded model.
Note
Throw exception if failed to load model

Definition at line 26 of file ModelLoader.cc.

27{
28 // Custom loader library name should be lib<type>_loader.so
29 std::string libname = "lib" + type + "_loader.so";
30
31 // Open custom loader library
32 void *handle = dlopen(libname.c_str(), RTLD_LAZY);
33 if (!handle)
34 throw std::runtime_error("Failed to open " + type + " loader");
35
36 // Get custom loader create function
37 using create_func_t = ILoader *(*)();
38 auto create_fn = reinterpret_cast<create_func_t>(dlsym(handle, "onert_loader_create"));
39 if (!create_fn)
40 {
41 dlclose(handle);
42 throw std::runtime_error("Failed to find loader create function");
43 }
44
45 // Get custom loader destroy function
46 using destroy_func_t = void (*)(ILoader *);
47 auto destroy_fn = reinterpret_cast<destroy_func_t>(dlsym(handle, "onert_loader_destroy"));
48 if (!destroy_fn)
49 {
50 dlclose(handle);
51 throw std::runtime_error("Failed to find loader destroy function");
52 }
53
54 // Create custom loader
55 auto loader = create_fn();
56 if (!loader)
57 {
58 dlclose(handle);
59 throw std::runtime_error("Failed to find loader create function");
60 }
61
62 // Load model
63 auto model = loader->loadFromFile(filename);
64
65 // Destroy custom loader
66 destroy_fn(loader);
67
68 // Close custom loader library
69 //
70 // NOTE:
71 // It assumes that custom loader will not be used frequently on runtime session.
72 // If custom loader is used frequently, it should not close custom loader library and
73 // save handler to reuse it.
74 dlclose(handle);
75
76 if (model)
77 return model;
78
79 throw std::runtime_error("Failed to load model " + filename);
80}

◆ loadTFLiteModel()

std::unique_ptr< ir::Model > onert::loader::loadTFLiteModel ( const std::string &  filename)

Definition at line 156 of file TFLiteLoader.cc.

157{
158 auto model = std::make_unique<ir::Model>();
159 TFLiteLoader loader(model);
160 loader.loadFromFile(filename);
161 return model;
162}

◆ loadTrainingInfo()

std::unique_ptr< ir::train::TrainingInfo > onert::loader::loadTrainingInfo ( const uint8_t *  buffer,
const size_t  size 
)

Definition at line 109 of file TrainInfoLoader.cc.

110{
111 assert(buffer != nullptr);
112
113 flatbuffers::Verifier v(buffer, size);
114 bool verified = circle::VerifyModelTrainingBuffer(v);
115 if (not verified)
116 throw std::runtime_error{"TrainingInfo buffer is not accessible"};
117
118 const circle::ModelTraining *circle_model =
119 circle::GetModelTraining(static_cast<const void *>(buffer));
120
121 assert(circle_model != nullptr);
122
123 auto tinfo = std::make_unique<ir::train::TrainingInfo>();
124 {
125 tinfo->setVersion(circle_model->version());
126 tinfo->setBatchSize(circle_model->batch_size());
127 tinfo->setOptimizerInfo(loadOptimizerInfo(circle_model));
128 tinfo->setLossInfo(loadLossInfo(circle_model));
129 tinfo->setTrainableOps(loadTrainableOps(circle_model));
130 }
131 return tinfo;
132}

References size.

Variable Documentation

◆ TRAININFO_METADATA_NAME

const char* const onert::loader::TRAININFO_METADATA_NAME = "CIRCLE_TRAINING"
inline

Definition at line 27 of file TrainInfoLoader.h.

Referenced by onert::exporter::CircleExporter::updateMetadata().