|
|
|
@ -66,26 +66,6 @@
|
|
|
|
|
#include "ggml-kompute.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// disable C++17 deprecation warning for std::codecvt_utf8
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
|
# pragma clang diagnostic push
|
|
|
|
|
# pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static std::wstring utf8_to_utf16(const std::string & str) {
|
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
|
|
|
|
return converter.from_bytes(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::string utf16_to_utf8(const std::wstring & str) {
|
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
|
|
|
|
return converter.to_bytes(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
|
# pragma clang diagnostic pop
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
|
|
using dl_handle = std::remove_pointer_t<HMODULE>;
|
|
|
|
@ -108,6 +88,11 @@ static dl_handle * dl_load_library(const std::wstring & path) {
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static dl_handle * dl_load_library(const std::string & path) {
|
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
|
|
|
|
return dl_load_library(converter.from_bytes(path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void * dl_get_sym(dl_handle * handle, const char * name) {
|
|
|
|
|
DWORD old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
|
|
|
|
|
SetErrorMode(old_mode | SEM_FAILCRITICALERRORS);
|
|
|
|
@ -129,8 +114,8 @@ struct dl_handle_deleter {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void * dl_load_library(const std::wstring & path) {
|
|
|
|
|
dl_handle * handle = dlopen(utf16_to_utf8(path).c_str(), RTLD_NOW | RTLD_LOCAL);
|
|
|
|
|
static void * dl_load_library(const std::string & path) {
|
|
|
|
|
dl_handle * handle = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
|
|
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
@ -217,11 +202,11 @@ struct ggml_backend_registry {
|
|
|
|
|
devices.push_back(device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ggml_backend_reg_t load_backend(const std::wstring & path, bool silent) {
|
|
|
|
|
ggml_backend_reg_t load_backend(const char * path, bool silent) {
|
|
|
|
|
dl_handle_ptr handle { dl_load_library(path) };
|
|
|
|
|
if (!handle) {
|
|
|
|
|
if (!silent) {
|
|
|
|
|
GGML_LOG_ERROR("%s: failed to load %s\n", __func__, utf16_to_utf8(path).c_str());
|
|
|
|
|
GGML_LOG_ERROR("%s: failed to load %s\n", __func__, path);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
@ -229,7 +214,7 @@ struct ggml_backend_registry {
|
|
|
|
|
auto score_fn = (ggml_backend_score_t) dl_get_sym(handle.get(), "ggml_backend_score");
|
|
|
|
|
if (score_fn && score_fn() == 0) {
|
|
|
|
|
if (!silent) {
|
|
|
|
|
GGML_LOG_INFO("%s: backend %s is not supported on this system\n", __func__, utf16_to_utf8(path).c_str());
|
|
|
|
|
GGML_LOG_INFO("%s: backend %s is not supported on this system\n", __func__, path);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
@ -237,7 +222,7 @@ struct ggml_backend_registry {
|
|
|
|
|
auto backend_init_fn = (ggml_backend_init_t) dl_get_sym(handle.get(), "ggml_backend_init");
|
|
|
|
|
if (!backend_init_fn) {
|
|
|
|
|
if (!silent) {
|
|
|
|
|
GGML_LOG_ERROR("%s: failed to find ggml_backend_init in %s\n", __func__, utf16_to_utf8(path).c_str());
|
|
|
|
|
GGML_LOG_ERROR("%s: failed to find ggml_backend_init in %s\n", __func__, path);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
@ -246,16 +231,16 @@ struct ggml_backend_registry {
|
|
|
|
|
if (!reg || reg->api_version != GGML_BACKEND_API_VERSION) {
|
|
|
|
|
if (!silent) {
|
|
|
|
|
if (!reg) {
|
|
|
|
|
GGML_LOG_ERROR("%s: failed to initialize backend from %s: ggml_backend_init returned NULL\n", __func__, utf16_to_utf8(path).c_str());
|
|
|
|
|
GGML_LOG_ERROR("%s: failed to initialize backend from %s: ggml_backend_init returned NULL\n", __func__, path);
|
|
|
|
|
} else {
|
|
|
|
|
GGML_LOG_ERROR("%s: failed to initialize backend from %s: incompatible API version (backend: %d, current: %d)\n",
|
|
|
|
|
__func__, utf16_to_utf8(path).c_str(), reg->api_version, GGML_BACKEND_API_VERSION);
|
|
|
|
|
__func__, path, reg->api_version, GGML_BACKEND_API_VERSION);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GGML_LOG_INFO("%s: loaded %s backend from %s\n", __func__, ggml_backend_reg_name(reg), utf16_to_utf8(path).c_str());
|
|
|
|
|
GGML_LOG_INFO("%s: loaded %s backend from %s\n", __func__, ggml_backend_reg_name(reg), path);
|
|
|
|
|
|
|
|
|
|
register_backend(reg, std::move(handle));
|
|
|
|
|
|
|
|
|
@ -391,14 +376,14 @@ ggml_backend_t ggml_backend_init_best(void) {
|
|
|
|
|
|
|
|
|
|
// Dynamic loading
|
|
|
|
|
ggml_backend_reg_t ggml_backend_load(const char * path) {
|
|
|
|
|
return get_reg().load_backend(utf8_to_utf16(path), false);
|
|
|
|
|
return get_reg().load_backend(path, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ggml_backend_unload(ggml_backend_reg_t reg) {
|
|
|
|
|
get_reg().unload_backend(reg, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::wstring get_executable_path() {
|
|
|
|
|
static std::string get_executable_path() {
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
// get executable path
|
|
|
|
|
std::vector<char> path;
|
|
|
|
@ -416,7 +401,7 @@ static std::wstring get_executable_path() {
|
|
|
|
|
if (last_slash != std::string::npos) {
|
|
|
|
|
base_path = base_path.substr(0, last_slash);
|
|
|
|
|
}
|
|
|
|
|
return utf8_to_utf16(base_path + "/");
|
|
|
|
|
return base_path + "/";
|
|
|
|
|
#elif defined(__linux__) || defined(__FreeBSD__)
|
|
|
|
|
std::string base_path = ".";
|
|
|
|
|
std::vector<char> path(1024);
|
|
|
|
@ -442,63 +427,57 @@ static std::wstring get_executable_path() {
|
|
|
|
|
path.resize(path.size() * 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utf8_to_utf16(base_path + "/");
|
|
|
|
|
return base_path + "/";
|
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
|
std::vector<wchar_t> path(MAX_PATH);
|
|
|
|
|
DWORD len = GetModuleFileNameW(NULL, path.data(), path.size());
|
|
|
|
|
std::vector<char> path(MAX_PATH);
|
|
|
|
|
DWORD len = GetModuleFileNameA(NULL, path.data(), path.size());
|
|
|
|
|
if (len == 0) {
|
|
|
|
|
return {};
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
std::wstring base_path(path.data(), len);
|
|
|
|
|
std::string base_path(path.data(), len);
|
|
|
|
|
// remove executable name
|
|
|
|
|
auto last_slash = base_path.find_last_of('\\');
|
|
|
|
|
if (last_slash != std::string::npos) {
|
|
|
|
|
base_path = base_path.substr(0, last_slash);
|
|
|
|
|
}
|
|
|
|
|
return base_path + L"\\";
|
|
|
|
|
#else
|
|
|
|
|
return {};
|
|
|
|
|
return base_path + "\\";
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::wstring backend_filename_prefix() {
|
|
|
|
|
static std::string backend_filename_prefix() {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
return L"ggml-";
|
|
|
|
|
return "ggml-";
|
|
|
|
|
#else
|
|
|
|
|
return L"libggml-";
|
|
|
|
|
return "libggml-";
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::wstring backend_filename_suffix() {
|
|
|
|
|
static std::string backend_filename_suffix() {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
return L".dll";
|
|
|
|
|
return ".dll";
|
|
|
|
|
#else
|
|
|
|
|
return L".so";
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::wstring path_separator() {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
return L"\\";
|
|
|
|
|
#else
|
|
|
|
|
return L"/";
|
|
|
|
|
return ".so";
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent, const char * user_search_path) {
|
|
|
|
|
// enumerate all the files that match [lib]ggml-name-*.[so|dll] in the search paths
|
|
|
|
|
// TODO: search system paths
|
|
|
|
|
std::wstring file_prefix = backend_filename_prefix() + utf8_to_utf16(name) + L"-";
|
|
|
|
|
std::vector<std::wstring> search_paths;
|
|
|
|
|
std::string file_prefix = backend_filename_prefix() + name + "-";
|
|
|
|
|
std::vector<std::string> search_paths;
|
|
|
|
|
if (user_search_path == nullptr) {
|
|
|
|
|
search_paths.push_back(L"." + path_separator());
|
|
|
|
|
search_paths.push_back("./");
|
|
|
|
|
search_paths.push_back(get_executable_path());
|
|
|
|
|
} else {
|
|
|
|
|
search_paths.push_back(utf8_to_utf16(user_search_path) + path_separator());
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
search_paths.push_back(std::string(user_search_path) + "\\");
|
|
|
|
|
#else
|
|
|
|
|
search_paths.push_back(std::string(user_search_path) + "/");
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int best_score = 0;
|
|
|
|
|
std::wstring best_path;
|
|
|
|
|
std::string best_path;
|
|
|
|
|
|
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
for (const auto & search_path : search_paths) {
|
|
|
|
@ -508,27 +487,27 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent,
|
|
|
|
|
fs::directory_iterator dir_it(search_path, fs::directory_options::skip_permission_denied);
|
|
|
|
|
for (const auto & entry : dir_it) {
|
|
|
|
|
if (entry.is_regular_file()) {
|
|
|
|
|
std::wstring filename = entry.path().filename().wstring();
|
|
|
|
|
std::wstring ext = entry.path().extension().wstring();
|
|
|
|
|
std::string filename = entry.path().filename().string();
|
|
|
|
|
std::string ext = entry.path().extension().string();
|
|
|
|
|
if (filename.find(file_prefix) == 0 && ext == backend_filename_suffix()) {
|
|
|
|
|
dl_handle_ptr handle { dl_load_library(entry.path().wstring()) };
|
|
|
|
|
dl_handle_ptr handle { dl_load_library(entry.path().c_str()) };
|
|
|
|
|
if (!handle && !silent) {
|
|
|
|
|
GGML_LOG_ERROR("%s: failed to load %s\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str());
|
|
|
|
|
GGML_LOG_ERROR("%s: failed to load %s\n", __func__, entry.path().string().c_str());
|
|
|
|
|
}
|
|
|
|
|
if (handle) {
|
|
|
|
|
auto score_fn = (ggml_backend_score_t) dl_get_sym(handle.get(), "ggml_backend_score");
|
|
|
|
|
if (score_fn) {
|
|
|
|
|
int s = score_fn();
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
GGML_LOG_DEBUG("%s: %s score: %d\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str(), s);
|
|
|
|
|
GGML_LOG_DEBUG("%s: %s score: %d\n", __func__, entry.path().string().c_str(), s);
|
|
|
|
|
#endif
|
|
|
|
|
if (s > best_score) {
|
|
|
|
|
best_score = s;
|
|
|
|
|
best_path = entry.path().wstring();
|
|
|
|
|
best_path = entry.path().string();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (!silent) {
|
|
|
|
|
GGML_LOG_INFO("%s: failed to find ggml_backend_score in %s\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str());
|
|
|
|
|
GGML_LOG_INFO("%s: failed to find ggml_backend_score in %s\n", __func__, entry.path().string().c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -540,15 +519,15 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent,
|
|
|
|
|
if (best_score == 0) {
|
|
|
|
|
// try to load the base backend
|
|
|
|
|
for (const auto & search_path : search_paths) {
|
|
|
|
|
std::wstring path = search_path + backend_filename_prefix() + utf8_to_utf16(name) + backend_filename_suffix();
|
|
|
|
|
std::string path = search_path + backend_filename_prefix() + name + backend_filename_suffix();
|
|
|
|
|
if (fs::exists(path)) {
|
|
|
|
|
return get_reg().load_backend(path, silent);
|
|
|
|
|
return get_reg().load_backend(path.c_str(), silent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return get_reg().load_backend(best_path, silent);
|
|
|
|
|
return get_reg().load_backend(best_path.c_str(), silent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ggml_backend_load_all() {
|
|
|
|
|