Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / odc / QuantizerLoader.cc
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "QuantizerLoader.h"
18
19 #include <dlfcn.h>
20 #include <iostream>
21 #include <string>
22
23 static const char *SHARED_LIB_EXT =
24 #if defined(__APPLE__) && defined(__MACH__)
25   ".dylib";
26 #else
27   ".so";
28 #endif
29
30 namespace onert
31 {
32 namespace odc
33 {
34
35 QuantizerLoader &QuantizerLoader::instance()
36 {
37   static QuantizerLoader singleton;
38   return singleton;
39 }
40
41 int32_t QuantizerLoader::loadLibrary()
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 }
91
92 int32_t QuantizerLoader::unloadLibrary()
93 {
94   if (get() == nullptr)
95     return 0;
96
97   _quantizer.reset(nullptr);
98   _dlhandle.reset(nullptr);
99
100   return 0;
101 }
102
103 } // namespace odc
104 } // namespace onert