2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "QuantizerLoader.h"
23 static const char *SHARED_LIB_EXT =
24 #if defined(__APPLE__) && defined(__MACH__)
35 QuantizerLoader &QuantizerLoader::instance()
37 static QuantizerLoader singleton;
41 int32_t QuantizerLoader::loadLibrary()
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();
50 if (handle == nullptr)
52 std::cerr << "Failed to load " << quantize_so << std::endl;
53 std::cerr << dlerror_msg << std::endl;
58 const char *factory_name = "create_quantizer";
59 auto factory = (factory_t)dlsym(handle, factory_name);
60 dlerror_msg = dlerror();
62 if (factory == nullptr)
64 std::cerr << "QuantizerLoader: unable to find function " << factory_name << dlerror_msg
70 auto destroyer = (quantizer_destory_t)dlsym(handle, "destroy_quantizer");
71 _quantizer = std::unique_ptr<IQuantizer, quantizer_destory_t>(factory(), destroyer);
73 if (_quantizer == nullptr)
75 std::cerr << "QuantizerLoader: unable to create quantizer" << std::endl;
81 // Save quantize library handle (avoid warning by handle lost without dlclose())
83 _dlhandle = std::unique_ptr<void, dlhandle_destroy_t>{handle, [filename = quantize_so](void *h) {
85 std::cerr << "Failed to unload backend " << filename << std::endl;
92 int32_t QuantizerLoader::unloadLibrary()
97 _quantizer.reset(nullptr);
98 _dlhandle.reset(nullptr);