mirror of
				https://github.com/ggml-org/llama.cpp.git
				synced 2025-11-04 09:32:00 +00:00 
			
		
		
		
	ggml-backend : keep paths in native string type when possible (#12144)
This commit is contained in:
		@@ -2,10 +2,8 @@
 | 
				
			|||||||
#include "ggml-backend.h"
 | 
					#include "ggml-backend.h"
 | 
				
			||||||
#include "ggml-impl.h"
 | 
					#include "ggml-impl.h"
 | 
				
			||||||
#include <algorithm>
 | 
					#include <algorithm>
 | 
				
			||||||
#include <codecvt>
 | 
					 | 
				
			||||||
#include <cstring>
 | 
					#include <cstring>
 | 
				
			||||||
#include <filesystem>
 | 
					#include <filesystem>
 | 
				
			||||||
#include <locale>
 | 
					 | 
				
			||||||
#include <memory>
 | 
					#include <memory>
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
#include <type_traits>
 | 
					#include <type_traits>
 | 
				
			||||||
@@ -72,14 +70,15 @@
 | 
				
			|||||||
#    pragma clang diagnostic ignored "-Wdeprecated-declarations"
 | 
					#    pragma clang diagnostic ignored "-Wdeprecated-declarations"
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static std::wstring utf8_to_utf16(const std::string & str) {
 | 
					namespace fs = std::filesystem;
 | 
				
			||||||
    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) {
 | 
					static std::string path_str(const fs::path & path) {
 | 
				
			||||||
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
 | 
					    std::string u8path;
 | 
				
			||||||
    return converter.to_bytes(str);
 | 
					    try {
 | 
				
			||||||
 | 
					        u8path = path.u8string();
 | 
				
			||||||
 | 
					    } catch (...) {
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return u8path;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if defined(__clang__)
 | 
					#if defined(__clang__)
 | 
				
			||||||
@@ -96,12 +95,12 @@ struct dl_handle_deleter {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static dl_handle * dl_load_library(const std::wstring & path) {
 | 
					static dl_handle * dl_load_library(const fs::path & path) {
 | 
				
			||||||
    // suppress error dialogs for missing DLLs
 | 
					    // suppress error dialogs for missing DLLs
 | 
				
			||||||
    DWORD old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
 | 
					    DWORD old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
 | 
				
			||||||
    SetErrorMode(old_mode | SEM_FAILCRITICALERRORS);
 | 
					    SetErrorMode(old_mode | SEM_FAILCRITICALERRORS);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    HMODULE handle = LoadLibraryW(path.c_str());
 | 
					    HMODULE handle = LoadLibraryW(path.wstring().c_str());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    SetErrorMode(old_mode);
 | 
					    SetErrorMode(old_mode);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -129,8 +128,8 @@ struct dl_handle_deleter {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void * dl_load_library(const std::wstring & path) {
 | 
					static void * dl_load_library(const fs::path & path) {
 | 
				
			||||||
    dl_handle * handle = dlopen(utf16_to_utf8(path).c_str(), RTLD_NOW | RTLD_LOCAL);
 | 
					    dl_handle * handle = dlopen(path.string().c_str(), RTLD_NOW | RTLD_LOCAL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return handle;
 | 
					    return handle;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -217,11 +216,11 @@ struct ggml_backend_registry {
 | 
				
			|||||||
        devices.push_back(device);
 | 
					        devices.push_back(device);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ggml_backend_reg_t load_backend(const std::wstring & path, bool silent) {
 | 
					    ggml_backend_reg_t load_backend(const fs::path & path, bool silent) {
 | 
				
			||||||
        dl_handle_ptr handle { dl_load_library(path) };
 | 
					        dl_handle_ptr handle { dl_load_library(path) };
 | 
				
			||||||
        if (!handle) {
 | 
					        if (!handle) {
 | 
				
			||||||
            if (!silent) {
 | 
					            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_str(path).c_str());
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return nullptr;
 | 
					            return nullptr;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -229,7 +228,7 @@ struct ggml_backend_registry {
 | 
				
			|||||||
        auto score_fn = (ggml_backend_score_t) dl_get_sym(handle.get(), "ggml_backend_score");
 | 
					        auto score_fn = (ggml_backend_score_t) dl_get_sym(handle.get(), "ggml_backend_score");
 | 
				
			||||||
        if (score_fn && score_fn() == 0) {
 | 
					        if (score_fn && score_fn() == 0) {
 | 
				
			||||||
            if (!silent) {
 | 
					            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_str(path).c_str());
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return nullptr;
 | 
					            return nullptr;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -237,7 +236,7 @@ struct ggml_backend_registry {
 | 
				
			|||||||
        auto backend_init_fn = (ggml_backend_init_t) dl_get_sym(handle.get(), "ggml_backend_init");
 | 
					        auto backend_init_fn = (ggml_backend_init_t) dl_get_sym(handle.get(), "ggml_backend_init");
 | 
				
			||||||
        if (!backend_init_fn) {
 | 
					        if (!backend_init_fn) {
 | 
				
			||||||
            if (!silent) {
 | 
					            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_str(path).c_str());
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return nullptr;
 | 
					            return nullptr;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -246,16 +245,17 @@ struct ggml_backend_registry {
 | 
				
			|||||||
        if (!reg || reg->api_version != GGML_BACKEND_API_VERSION) {
 | 
					        if (!reg || reg->api_version != GGML_BACKEND_API_VERSION) {
 | 
				
			||||||
            if (!silent) {
 | 
					            if (!silent) {
 | 
				
			||||||
                if (!reg) {
 | 
					                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_str(path).c_str());
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    GGML_LOG_ERROR("%s: failed to initialize backend from %s: incompatible API version (backend: %d, current: %d)\n",
 | 
					                    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_str(path).c_str(), reg->api_version, GGML_BACKEND_API_VERSION);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return nullptr;
 | 
					            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_str(path).c_str());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        register_backend(reg, std::move(handle));
 | 
					        register_backend(reg, std::move(handle));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -391,14 +391,14 @@ ggml_backend_t ggml_backend_init_best(void) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Dynamic loading
 | 
					// Dynamic loading
 | 
				
			||||||
ggml_backend_reg_t ggml_backend_load(const char * path) {
 | 
					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) {
 | 
					void ggml_backend_unload(ggml_backend_reg_t reg) {
 | 
				
			||||||
    get_reg().unload_backend(reg, true);
 | 
					    get_reg().unload_backend(reg, true);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static std::wstring get_executable_path() {
 | 
					static fs::path get_executable_path() {
 | 
				
			||||||
#if defined(__APPLE__)
 | 
					#if defined(__APPLE__)
 | 
				
			||||||
    // get executable path
 | 
					    // get executable path
 | 
				
			||||||
    std::vector<char> path;
 | 
					    std::vector<char> path;
 | 
				
			||||||
@@ -416,7 +416,7 @@ static std::wstring get_executable_path() {
 | 
				
			|||||||
    if (last_slash != std::string::npos) {
 | 
					    if (last_slash != std::string::npos) {
 | 
				
			||||||
        base_path = base_path.substr(0, last_slash);
 | 
					        base_path = base_path.substr(0, last_slash);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return utf8_to_utf16(base_path + "/");
 | 
					    return base_path + "/";
 | 
				
			||||||
#elif defined(__linux__) || defined(__FreeBSD__)
 | 
					#elif defined(__linux__) || defined(__FreeBSD__)
 | 
				
			||||||
    std::string base_path = ".";
 | 
					    std::string base_path = ".";
 | 
				
			||||||
    std::vector<char> path(1024);
 | 
					    std::vector<char> path(1024);
 | 
				
			||||||
@@ -442,7 +442,7 @@ static std::wstring get_executable_path() {
 | 
				
			|||||||
        path.resize(path.size() * 2);
 | 
					        path.resize(path.size() * 2);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return utf8_to_utf16(base_path + "/");
 | 
					    return base_path + "/";
 | 
				
			||||||
#elif defined(_WIN32)
 | 
					#elif defined(_WIN32)
 | 
				
			||||||
    std::vector<wchar_t> path(MAX_PATH);
 | 
					    std::vector<wchar_t> path(MAX_PATH);
 | 
				
			||||||
    DWORD len = GetModuleFileNameW(NULL, path.data(), path.size());
 | 
					    DWORD len = GetModuleFileNameW(NULL, path.data(), path.size());
 | 
				
			||||||
@@ -461,74 +461,69 @@ static std::wstring get_executable_path() {
 | 
				
			|||||||
#endif
 | 
					#endif
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static std::wstring backend_filename_prefix() {
 | 
					static fs::path backend_filename_prefix() {
 | 
				
			||||||
#ifdef _WIN32
 | 
					#ifdef _WIN32
 | 
				
			||||||
    return L"ggml-";
 | 
					    return fs::u8path("ggml-");
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
    return L"libggml-";
 | 
					    return fs::u8path("libggml-");
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static std::wstring backend_filename_suffix() {
 | 
					static fs::path backend_filename_extension() {
 | 
				
			||||||
#ifdef _WIN32
 | 
					#ifdef _WIN32
 | 
				
			||||||
    return L".dll";
 | 
					    return fs::u8path(".dll");
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
    return L".so";
 | 
					    return fs::u8path(".so");
 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static std::wstring path_separator() {
 | 
					 | 
				
			||||||
#ifdef _WIN32
 | 
					 | 
				
			||||||
    return L"\\";
 | 
					 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
    return L"/";
 | 
					 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent, const char * user_search_path) {
 | 
					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
 | 
					    // enumerate all the files that match [lib]ggml-name-*.[so|dll] in the search paths
 | 
				
			||||||
     // TODO: search system paths
 | 
					    const fs::path name_path = fs::u8path(name);
 | 
				
			||||||
    std::wstring file_prefix = backend_filename_prefix() + utf8_to_utf16(name) + L"-";
 | 
					    const fs::path file_prefix = backend_filename_prefix().native() + name_path.native() + fs::u8path("-").native();
 | 
				
			||||||
    std::vector<std::wstring> search_paths;
 | 
					    const fs::path file_extension = backend_filename_extension();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    std::vector<fs::path> search_paths;
 | 
				
			||||||
    if (user_search_path == nullptr) {
 | 
					    if (user_search_path == nullptr) {
 | 
				
			||||||
        search_paths.push_back(L"." + path_separator());
 | 
					        // default search paths: executable directory, current directory
 | 
				
			||||||
        search_paths.push_back(get_executable_path());
 | 
					        search_paths.push_back(get_executable_path());
 | 
				
			||||||
 | 
					        search_paths.push_back(fs::current_path());
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        search_paths.push_back(utf8_to_utf16(user_search_path) + path_separator());
 | 
					        search_paths.push_back(user_search_path);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    int best_score = 0;
 | 
					    int best_score = 0;
 | 
				
			||||||
    std::wstring best_path;
 | 
					    fs::path best_path;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    namespace fs = std::filesystem;
 | 
					 | 
				
			||||||
    for (const auto & search_path : search_paths) {
 | 
					    for (const auto & search_path : search_paths) {
 | 
				
			||||||
        if (!fs::exists(search_path)) {
 | 
					        if (!fs::exists(search_path)) {
 | 
				
			||||||
 | 
					            GGML_LOG_DEBUG("%s: search path %s does not exist\n", __func__, path_str(search_path).c_str());
 | 
				
			||||||
            continue;
 | 
					            continue;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        fs::directory_iterator dir_it(search_path, fs::directory_options::skip_permission_denied);
 | 
					        fs::directory_iterator dir_it(search_path, fs::directory_options::skip_permission_denied);
 | 
				
			||||||
        for (const auto & entry : dir_it) {
 | 
					        for (const auto & entry : dir_it) {
 | 
				
			||||||
            if (entry.is_regular_file()) {
 | 
					            if (entry.is_regular_file()) {
 | 
				
			||||||
                std::wstring filename = entry.path().filename().wstring();
 | 
					                auto filename = entry.path().filename().native();
 | 
				
			||||||
                std::wstring ext = entry.path().extension().wstring();
 | 
					                auto ext = entry.path().extension().native();
 | 
				
			||||||
                if (filename.find(file_prefix) == 0 && ext == backend_filename_suffix()) {
 | 
					                if (filename.find(file_prefix) == 0 && ext == file_extension) {
 | 
				
			||||||
                    dl_handle_ptr handle { dl_load_library(entry.path().wstring()) };
 | 
					                    dl_handle_ptr handle { dl_load_library(entry) };
 | 
				
			||||||
                    if (!handle && !silent) {
 | 
					                    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__, path_str(entry.path()).c_str());
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                    if (handle) {
 | 
					                    if (handle) {
 | 
				
			||||||
                        auto score_fn = (ggml_backend_score_t) dl_get_sym(handle.get(), "ggml_backend_score");
 | 
					                        auto score_fn = (ggml_backend_score_t) dl_get_sym(handle.get(), "ggml_backend_score");
 | 
				
			||||||
                        if (score_fn) {
 | 
					                        if (score_fn) {
 | 
				
			||||||
                            int s = score_fn();
 | 
					                            int s = score_fn();
 | 
				
			||||||
#ifndef NDEBUG
 | 
					#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__, path_str(entry.path()).c_str(), s);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
                            if (s > best_score) {
 | 
					                            if (s > best_score) {
 | 
				
			||||||
                                best_score = s;
 | 
					                                best_score = s;
 | 
				
			||||||
                                best_path = entry.path().wstring();
 | 
					                                best_path = entry.path();
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
                        } else {
 | 
					                        } else {
 | 
				
			||||||
                            if (!silent) {
 | 
					                            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__, path_str(entry.path()).c_str());
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
@@ -540,7 +535,8 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent,
 | 
				
			|||||||
    if (best_score == 0) {
 | 
					    if (best_score == 0) {
 | 
				
			||||||
        // try to load the base backend
 | 
					        // try to load the base backend
 | 
				
			||||||
        for (const auto & search_path : search_paths) {
 | 
					        for (const auto & search_path : search_paths) {
 | 
				
			||||||
            std::wstring path = search_path + backend_filename_prefix() + utf8_to_utf16(name) + backend_filename_suffix();
 | 
					            fs::path filename = backend_filename_prefix().native() + name_path.native() + backend_filename_extension().native();
 | 
				
			||||||
 | 
					            fs::path path = search_path.native() + filename.native();
 | 
				
			||||||
            if (fs::exists(path)) {
 | 
					            if (fs::exists(path)) {
 | 
				
			||||||
                return get_reg().load_backend(path, silent);
 | 
					                return get_reg().load_backend(path, silent);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user