Package portable Linux runtime and verify Google CPU search

This commit is contained in:
karl committed 2026-10-13 00:43:20 +02:00
1 parent e3558a0104
commit 0b56ab553c
22 files changed
+1585 -16

No files matched your search

+31 -1
View File
@@ -16,10 +16,17 @@
#include <string>
#include <vector>
#ifdef SEARCHI_CPU_DISPATCH
#include <filesystem>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <filesystem>
#elif defined(__linux__)
#include <dlfcn.h>
#include <unistd.h>
#else
#error Unsupported platform for packaged CPU dispatch
#endif
#endif
using json = nlohmann::json;
@@ -91,10 +98,19 @@ public:
#ifdef SEARCHI_CPU_DISPATCH
// Only load our adjacent modules: no current-directory or environment
// plugin discovery. CPU-only startup never loads the Vulkan module.
#ifdef _WIN32
std::vector<wchar_t> executable(32768);
DWORD length = GetModuleFileNameW(nullptr, executable.data(), DWORD(executable.size()));
require(length > 0 && length < executable.size(), "Cannot locate packaged worker backends");
const auto directory = std::filesystem::path(std::wstring(executable.data(), length)).parent_path().u8string();
#else
// /proc identifies this executable even when launched through PATH or
// from another working directory. Reject truncation before discovery.
std::vector<char> executable(32768);
const auto length = readlink("/proc/self/exe", executable.data(), executable.size());
require(length > 0 && size_t(length) < executable.size(), "Cannot locate packaged worker backends");
const auto directory = std::filesystem::path(std::string(executable.data(), size_t(length))).parent_path().string();
#endif
if (want_gpu) {
ggml_backend_load_all_from_path(directory.c_str());
} else {
@@ -103,6 +119,7 @@ public:
require(ggml_backend_reg_by_name("CPU") != nullptr, "Cannot load compatible packaged CPU backend");
const auto cpu_reg = ggml_backend_reg_by_name("CPU");
const auto feature_proc = ggml_backend_reg_get_proc_address(cpu_reg, "ggml_backend_get_features");
#ifdef _WIN32
HMODULE cpu_module = nullptr;
if (feature_proc && GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(feature_proc), &cpu_module)) {
@@ -112,6 +129,19 @@ public:
}
}
vulkan_module_loaded = GetModuleHandleW(L"ggml-vulkan.dll") != nullptr;
#else
Dl_info cpu_module{};
if (feature_proc && dladdr(feature_proc, &cpu_module) && cpu_module.dli_fname) {
cpu_backend_module = std::filesystem::path(cpu_module.dli_fname).filename().string();
}
// RTLD_NOLOAD observes module presence without initiating provider
// loading; forced CPU startup must remain independent of Vulkan.
const auto vulkan_path = std::filesystem::path(directory) / "libggml-vulkan.so";
if (void * module = dlopen(vulkan_path.c_str(), RTLD_NOW | RTLD_NOLOAD)) {
vulkan_module_loaded = true;
dlclose(module);
}
#endif
#endif
llama_backend_init();
bool use_gpu = want_gpu && llama_supports_gpu_offload();