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

+11 -5
View File
@@ -4,11 +4,11 @@ if(NOT DEFINED LLAMA_CPP_DIR OR NOT EXISTS "${LLAMA_CPP_DIR}/include/llama.h")
message(FATAL_ERROR "Supply -DLLAMA_CPP_DIR=<pinned patched llama.cpp b11461 source>")
endif()
set(CMAKE_CXX_STANDARD 17)
# CPU-only builds remain small internal VM tools. Normal Windows x64 builds
# CPU-only builds remain small internal VM tools. Normal Windows/Linux x64 builds
# ship baseline and optimized CPU modules in the same GPU-capable package.
set(SEARCHI_ACCELERATION "auto" CACHE STRING "auto; explicit developer overrides: cpu, metal, vulkan, cuda")
set(SEARCHI_CPU_DISPATCH OFF)
if(WIN32 AND CMAKE_SIZEOF_VOID_P EQUAL 8 AND
if((WIN32 OR (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND SEARCHI_ACCELERATION MATCHES "^(auto|vulkan)$")) AND CMAKE_SIZEOF_VOID_P EQUAL 8 AND
(CMAKE_SYSTEM_PROCESSOR MATCHES "^(AMD64|amd64|x86_64|X86_64)$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "x64") AND
NOT SEARCHI_ACCELERATION STREQUAL "cpu")
set(SEARCHI_CPU_DISPATCH ON)
@@ -22,12 +22,18 @@ if(MSVC)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>" CACHE STRING "Internal static worker CRT" FORCE)
endif()
endif()
set(BUILD_SHARED_LIBS ${SEARCHI_CPU_DISPATCH} CACHE BOOL "Shared runtime for Windows CPU dispatch" FORCE)
set(BUILD_SHARED_LIBS ${SEARCHI_CPU_DISPATCH} CACHE BOOL "Shared runtime for portable CPU dispatch" FORCE)
set(GGML_BACKEND_DL ${SEARCHI_CPU_DISPATCH} CACHE BOOL "Load compatible packaged CPU backend" FORCE)
set(GGML_CPU_ALL_VARIANTS ${SEARCHI_CPU_DISPATCH} CACHE BOOL "Baseline and optimized CPU variants" FORCE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND SEARCHI_CPU_DISPATCH)
# Adjacent packaged libraries, independent of the build tree or cwd.
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
set(CMAKE_INSTALL_RPATH "$ORIGIN")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH OFF)
endif()
set(GGML_OPENMP OFF CACHE BOOL "Use bounded native llama thread pools without external OpenMP runtime" FORCE)
set(GGML_NATIVE OFF CACHE BOOL "Portable CPU kernels")
set(GGML_NATIVE OFF CACHE BOOL "Portable CPU kernels" FORCE)
# Normal distribution builds include GPU support and CPU fallback. CPU-only
# configuration is explicitly reserved for development/testing in the VM.
set(GGML_CUDA OFF CACHE BOOL "Production CUDA backend")
@@ -62,7 +68,7 @@ set(LLAMA_OPENSSL OFF CACHE BOOL "No network runtime dependencies" FORCE)
add_subdirectory("${LLAMA_CPP_DIR}" llama-build)
add_executable(searchi-gemma-worker worker.cpp)
target_include_directories(searchi-gemma-worker PRIVATE "${LLAMA_CPP_DIR}/vendor")
target_link_libraries(searchi-gemma-worker PRIVATE llama mtmd)
target_link_libraries(searchi-gemma-worker PRIVATE llama mtmd ${CMAKE_DL_LIBS})
if(SEARCHI_CPU_DISPATCH)
target_compile_definitions(searchi-gemma-worker PRIVATE SEARCHI_CPU_DISPATCH=1)
endif()
+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();