engine: opt-in GPU offload for the decision engine
Let the SemIf decision engine run on a GPU via llama.cpp. SemIf's llamacpp backend is CPU-only by construction (it hardcodes n_gpu_layers=0), so the agent wraps `_cpu_model_params` for the duration of the load and restores it after — no patch to the re-cloned engine source. - `engine.gpu_layers` / `gpu_device` / `gpu_split` in config (default 0 = CPU). - On the GPU path, force a full cache reset (`llama_memory_clear(mem, True)`): SemIf's metadata-only clear leaves stale hybrid-attention state on Vulkan, so repeated scores drift (0.08 -> 0.36 -> 0.43) and return wrong probabilities. - Fail loudly when `gpu_layers` is set but the build has no GPU backend. - bootstrap.sh: opt-in `--gpu auto|vulkan|rocm|cuda|cpu`. A GPU build is opt-in because llama.cpp's Vulkan backend splits the graph across CPU and Vulkan even at `n_gpu_layers=0`, roughly halving CPU throughput, so a CPU-only host must keep the CPU wheel. Detects the backend, installs the Vulkan build prereqs, and rebuilds when offload support disagrees (pip's wheel cache otherwise serves the CPU wheel back). - Bump the SemIf pin 1f2dea3 -> 23cf1f3. - Tests: unit coverage for the config mapping + loader patches; an integration test asserting GPU determinism and CPU parity. - Offload needs the accelerator to itself (~2.9 GiB for the 4B Q4 model): a GPU already holding another model makes the engine slower than CPU and can abort with `vk::DeviceLostError`.
This commit is contained in:
1 parent
4e70a5889e
commit
17f16a6bfc
9 files changed
+329
-16
No files matched your search
@@ -0,0 +1,79 @@
|
||||
"""Engine config wiring: the per-machine GPU-offload knobs and the loader patch.
|
||||
|
||||
Pure stdlib: no SemIf, no llama.cpp, no GGUF. The SemIf backend is CPU-only by
|
||||
construction, so the patch that lets it offload is exercised against a fake
|
||||
backend module.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
from semif_agent.cli import build_engine_config
|
||||
from semif_agent.engine import (
|
||||
EngineConfig,
|
||||
_install_gpu_state_reset,
|
||||
_offloading_params,
|
||||
)
|
||||
|
||||
PINS = {
|
||||
"engine": {
|
||||
"source": "Qwen/Qwen3.5-4B",
|
||||
"revision": "0" * 40,
|
||||
"gguf_url": "https://example.com/Qwen_Qwen3.5-4B-Q4_K_M.gguf",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def test_engine_config_defaults_to_cpu():
|
||||
cfg = build_engine_config({"engine": {"backend": "llamacpp"}}, PINS)
|
||||
assert cfg.gpu_layers == 0
|
||||
assert cfg.gpu_device is None
|
||||
assert cfg.gpu_split == 0
|
||||
|
||||
|
||||
def test_engine_config_reads_gpu_knobs():
|
||||
cfg = build_engine_config(
|
||||
{"engine": {"gpu_layers": -1, "gpu_device": 1, "gpu_split": 0}}, PINS
|
||||
)
|
||||
assert cfg.gpu_layers == -1
|
||||
assert cfg.gpu_device == 1
|
||||
assert cfg.gpu_split == 0
|
||||
|
||||
|
||||
def test_gpu_offload_wrapper_rewrites_model_params():
|
||||
def original(library):
|
||||
return SimpleNamespace(n_gpu_layers=0, main_gpu=0, split_mode=1)
|
||||
|
||||
offloading = _offloading_params(
|
||||
original, EngineConfig(gpu_layers=99, gpu_device=1, gpu_split=0)
|
||||
)
|
||||
params = offloading(object())
|
||||
assert params.n_gpu_layers == 99
|
||||
assert params.main_gpu == 1
|
||||
assert params.split_mode == 0
|
||||
|
||||
|
||||
def test_gpu_offload_wrapper_leaves_main_gpu_when_device_unset():
|
||||
def original(library):
|
||||
return SimpleNamespace(n_gpu_layers=0, main_gpu=7, split_mode=1)
|
||||
|
||||
offloading = _offloading_params(original, EngineConfig(gpu_layers=-1))
|
||||
params = offloading(object())
|
||||
assert params.n_gpu_layers == -1
|
||||
assert params.main_gpu == 7 # untouched
|
||||
assert params.split_mode == 0
|
||||
|
||||
|
||||
def test_gpu_state_reset_clears_cache_data():
|
||||
calls = []
|
||||
|
||||
class FakeLib:
|
||||
def llama_memory_clear(self, memory, data):
|
||||
calls.append((memory, data))
|
||||
|
||||
engine = SimpleNamespace(lib=FakeLib(), memory="mem")
|
||||
engine.clear = lambda: engine.lib.llama_memory_clear(engine.memory, False)
|
||||
_install_gpu_state_reset(SimpleNamespace(engine=engine))
|
||||
engine.clear()
|
||||
assert calls == [("mem", True)]
|
||||
Reference in new issue
Block a user