ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::odc::QuantizerLoader Class Reference

Class to manage loading and unloading of dynamic library containing implementation of IQuantizer interface. More...

#include <QuantizerLoader.h>

Public Types

using dlhandle_destroy_t = std::function< void(void *)>
 Typedef for function pointer to destroy loaded library handle.
 
using factory_t = IQuantizer *(*)()
 Typedef for function pointer to create instance of IQuantizer.
 
using quantizer_destory_t = void(*)(IQuantizer *)
 Typedef for function pointer to destroy instance of IQuantizer.
 

Public Member Functions

int32_t loadLibrary ()
 Load dynamic library containing implementation of IQuantizer.
 
int32_t unloadLibrary ()
 Unload dynamic library containing implementation of IQuantizer.
 
IQuantizerget () const
 Get instance of IQuantizer created through factory method.
 

Static Public Member Functions

static QuantizerLoaderinstance ()
 Get singleton instance of QuantizerLoader.
 

Detailed Description

Class to manage loading and unloading of dynamic library containing implementation of IQuantizer interface.

Definition at line 34 of file QuantizerLoader.h.

Member Typedef Documentation

◆ dlhandle_destroy_t

using onert::odc::QuantizerLoader::dlhandle_destroy_t = std::function<void(void *)>

Typedef for function pointer to destroy loaded library handle.

Definition at line 40 of file QuantizerLoader.h.

◆ factory_t

Typedef for function pointer to create instance of IQuantizer.

Definition at line 44 of file QuantizerLoader.h.

◆ quantizer_destory_t

Typedef for function pointer to destroy instance of IQuantizer.

Definition at line 48 of file QuantizerLoader.h.

Member Function Documentation

◆ get()

IQuantizer * onert::odc::QuantizerLoader::get ( ) const
inline

Get instance of IQuantizer created through factory method.

Returns
Pointer to instance of IQuantizer

Definition at line 78 of file QuantizerLoader.h.

78{ return _quantizer.get(); }

Referenced by loadLibrary(), and unloadLibrary().

◆ instance()

QuantizerLoader & onert::odc::QuantizerLoader::instance ( )
static

Get singleton instance of QuantizerLoader.

Returns
Reference to singleton instance of QuantizerLoader

Definition at line 35 of file QuantizerLoader.cc.

36{
37 static QuantizerLoader singleton;
38 return singleton;
39}

Referenced by onert::odc::QuantizeManager::deleteMinMaxFile(), onert::odc::QuantizeManager::quantize(), onert::odc::QuantizeManager::readyForQuantize(), and onert::odc::QuantizeManager::setMinMaxRecordsThreshold().

◆ loadLibrary()

int32_t onert::odc::QuantizerLoader::loadLibrary ( )

Load dynamic library containing implementation of IQuantizer.

Returns
0 if success, otherwise errno value

Definition at line 41 of file QuantizerLoader.cc.

42{
43 if (get() != nullptr)
44 return 0;
45
46 const std::string quantize_so = std::string("libonert_odc") + SHARED_LIB_EXT;
47 void *handle = dlopen(quantize_so.c_str(), RTLD_LAZY | RTLD_LOCAL);
48 auto dlerror_msg = dlerror();
49
50 if (handle == nullptr)
51 {
52 std::cerr << "Failed to load " << quantize_so << std::endl;
53 std::cerr << dlerror_msg << std::endl;
54 return 1;
55 }
56
57 {
58 const char *factory_name = "create_quantizer";
59 auto factory = (factory_t)dlsym(handle, factory_name);
60 dlerror_msg = dlerror();
61
62 if (factory == nullptr)
63 {
64 std::cerr << "QuantizerLoader: unable to find function " << factory_name << dlerror_msg
65 << std::endl;
66 dlclose(handle);
67 return 1;
68 }
69
70 auto destroyer = (quantizer_destory_t)dlsym(handle, "destroy_quantizer");
71 _quantizer = std::unique_ptr<IQuantizer, quantizer_destory_t>(factory(), destroyer);
72
73 if (_quantizer == nullptr)
74 {
75 std::cerr << "QuantizerLoader: unable to create quantizer" << std::endl;
76 dlclose(handle);
77 return 1;
78 }
79 }
80
81 // Save quantize library handle (avoid warning by handle lost without dlclose())
82 // clang-format off
83 _dlhandle = std::unique_ptr<void, dlhandle_destroy_t>{handle, [filename = quantize_so](void *h) {
84 if (dlclose(h) != 0)
85 std::cerr << "Failed to unload backend " << filename << std::endl;
86 }};
87 // clang-format on
88
89 return 0;
90}
IQuantizer *(*)() factory_t
Typedef for function pointer to create instance of IQuantizer.
IQuantizer * get() const
Get instance of IQuantizer created through factory method.
void(*)(IQuantizer *) quantizer_destory_t
Typedef for function pointer to destroy instance of IQuantizer.

References get().

◆ unloadLibrary()

int32_t onert::odc::QuantizerLoader::unloadLibrary ( )

Unload dynamic library containing implementation of IQuantizer.

Returns
0 if success, otherwise errno value

Definition at line 92 of file QuantizerLoader.cc.

93{
94 if (get() == nullptr)
95 return 0;
96
97 _quantizer.reset(nullptr);
98 _dlhandle.reset(nullptr);
99
100 return 0;
101}

References get().


The documentation for this class was generated from the following files: