ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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 346 of file BaseLoader.h.

347{
348 if (data_ptr->values() == nullptr)
349 {
350 return false;
351 }
352
353 int size = data_ptr->values()->size();
354 arr.reserve(size);
355 for (int i = 0; i < size; i++)
356 {
357 arr.emplace_back(static_cast<uint16_t>(data_ptr->values()->Get(i)));
358 }
359 return true;
360}
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 285 of file CircleLoader.cc.

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

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 293 of file CircleLoader.cc.

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

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 28 of file ModelLoader.cc.

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

◆ loadTFLiteModel()

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

Definition at line 158 of file TFLiteLoader.cc.

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

◆ loadTrainingInfo()

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

Definition at line 113 of file TrainInfoLoader.cc.

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

References size.

Variable Documentation

◆ TRAININFO_METADATA_NAME

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

Definition at line 27 of file TrainInfoLoader.cc.