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:
gabby committed 2026-10-07 05:21:21 +00:00
1 parent 4e70a5889e
commit 17f16a6bfc
9 files changed
+329 -16

No files matched your search

+4
View File
@@ -177,6 +177,7 @@ def build_engine_config(config: dict, pins: dict | None = None) -> EngineConfig:
gguf = _anchor_path(override) if override else str(
REPO_ROOT / ".runtime" / "models" / os.path.basename(pin_eng.get("gguf_url", ""))
)
gpu_device = eng.get("gpu_device")
return EngineConfig(
backend=eng.get("backend", "llamacpp"),
source=pin_eng.get("source", ""),
@@ -184,6 +185,9 @@ def build_engine_config(config: dict, pins: dict | None = None) -> EngineConfig:
gguf=gguf,
context_tokens=int(eng.get("context_tokens", 4096)),
threads=eng.get("threads"),
gpu_layers=int(eng.get("gpu_layers", 0)),
gpu_device=None if gpu_device is None else int(gpu_device),
gpu_split=int(eng.get("gpu_split", 0)),
)
+81 -4
View File
@@ -2,10 +2,11 @@
Wraps the SemIf package (`semif_phase1`). The import and model load happen
lazily so the rest of the agent is pure stdlib and testable without SemIf
installed. On hardware that is neither CUDA nor Apple, use the shipped
llama.cpp backend: a local GGUF scored on CPU (or Vulkan if your llama.cpp
build enables it). This engine is only usable on a machine with SemIf and the
pinned GGUF available; elsewhere calls raise EngineUnavailable.
installed. The shipped llama.cpp backend scores a local GGUF; it runs on CPU by
default, and `EngineConfig.gpu_layers` offloads to a GPU when llama-cpp-python
was built with a GPU backend (Vulkan/ROCm/CUDA). This engine is only usable on a
machine with SemIf and the pinned GGUF available; elsewhere calls raise
EngineUnavailable.
"""
from __future__ import annotations
@@ -29,6 +30,67 @@ class EngineConfig:
gguf: str = ""
context_tokens: int = 4096
threads: int | None = None
# GPU offload (llama.cpp). 0 keeps the CPU-only default; a positive count
# offloads that many layers, -1 offloads all of them. Requires a
# llama-cpp-python built with a GPU backend (Vulkan/ROCm/CUDA).
gpu_layers: int = 0
gpu_device: int | None = None
gpu_split: int = 0
def _require_gpu_offload() -> None:
"""Fail loudly when GPU layers are requested but the build cannot offload."""
import llama_cpp
if not llama_cpp.llama_supports_gpu_offload():
raise EngineUnavailable(
"engine.gpu_layers is set but this llama-cpp-python build has no GPU "
"backend. Rebuild it with one, e.g. "
"CMAKE_ARGS='-DGGML_VULKAN=on' (Vulkan), '-DGGML_HIP=on' (ROCm) or "
"'-DGGML_CUDA=on' (CUDA)."
)
def _install_gpu_state_reset(model) -> None:
"""Force a full cache reset between scored prompts when running on the GPU.
SemIf's `_Engine.clear` calls `llama_memory_clear(memory, False)`: it resets
the cache metadata but leaves the stored data. The CPU backend tolerates that
(the slots are overwritten before use), but the Vulkan backend keeps stale
hybrid-attention/recurrent state, so repeated `score` calls drift and return
wrong probabilities. Clearing the data as well restores determinism and
matches the CPU path. Applied only when offloading, so CPU behaviour is
untouched.
"""
engine = model.engine
library = engine.lib
def clear():
library.llama_memory_clear(engine.memory, True)
engine.clear = clear
def _offloading_params(original, config: EngineConfig):
"""Wrap SemIf's CPU-only model-params factory to offload layers to the GPU.
`semif_phase1.llamacpp_backend` is CPU-only by construction: its
`_cpu_model_params` hardcodes `n_gpu_layers = 0`. The backend looks that name
up on its own module while loading, so wrapping it for the duration of the
load lets the same path offload without patching (and losing) the re-cloned
engine source. The caller restores the original afterwards, so a CPU engine
loaded later in the same process is unaffected.
"""
def offloading(library):
params = original(library)
params.n_gpu_layers = config.gpu_layers
params.split_mode = config.gpu_split
if config.gpu_device is not None:
params.main_gpu = config.gpu_device
return params
return offloading
class SemIfEngine:
@@ -59,6 +121,11 @@ class SemIfEngine:
"SemIf is not installed here. Install it on the target box with "
"`pip install -e '.[test,llamacpp]'`."
) from exc
if self.config.gpu_layers:
_require_gpu_offload()
original_params = backend._cpu_model_params
if self.config.gpu_layers:
backend._cpu_model_params = _offloading_params(original_params, self.config)
try:
model, tokenizer, metadata = backend.load_model(
self.config.source,
@@ -69,6 +136,16 @@ class SemIfEngine:
)
except Exception as exc:
raise EngineUnavailable(f"Failed to load the SemIf model: {exc}") from exc
finally:
backend._cpu_model_params = original_params
if self.config.gpu_layers:
_install_gpu_state_reset(model)
# The backend hardcodes n_gpu_layers=0 in its metadata; record what we
# actually asked llama.cpp to offload instead.
metadata = dict(metadata)
metadata["n_gpu_layers"] = self.config.gpu_layers
if self.config.gpu_device is not None:
metadata["main_gpu"] = self.config.gpu_device
self._model, self._tokenizer, self._metadata = model, tokenizer, metadata
return model, tokenizer, metadata