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
+79
-2
@@ -32,7 +32,7 @@
|
||||
#
|
||||
# Usage:
|
||||
# scripts/bootstrap.sh [--llm-url URL] [--codegen-url URL] [--llm-model MODEL]
|
||||
# [--codegen-model MODEL] [--copy-data SRC] [--voice] [-h]
|
||||
# [--codegen-model MODEL] [--copy-data SRC] [--voice] [--gpu MODE] [-h]
|
||||
#
|
||||
# Run as the human user; sudo is used internally for system bits.
|
||||
set -euo pipefail
|
||||
@@ -43,6 +43,7 @@ LLM_MODEL=""
|
||||
CODEGEN_MODEL=""
|
||||
COPY_DATA=""
|
||||
VOICE=0
|
||||
GPU_MODE="auto"
|
||||
|
||||
usage() {
|
||||
sed -n '2,35p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'
|
||||
@@ -53,6 +54,7 @@ usage() {
|
||||
echo " --codegen-model MODEL override codegen.model from config.json for this run"
|
||||
echo " --copy-data SRC rsync SRC (e.g. user@host:/path/to/semif-agent/data) to data/ — opt-in"
|
||||
echo " --voice install the optional voice-gateway stack + download its models"
|
||||
echo " --gpu MODE decision-engine llama.cpp backend: auto (default; a GPU build only when engine.gpu_layers is set), vulkan, rocm, cuda, cpu"
|
||||
echo " -h this help"
|
||||
exit "${1:-0}"
|
||||
}
|
||||
@@ -65,6 +67,7 @@ while [[ $# -gt 0 ]]; do
|
||||
--codegen-model) CODEGEN_MODEL="$2"; shift 2 ;;
|
||||
--copy-data) COPY_DATA="$2"; shift 2 ;;
|
||||
--voice) VOICE=1; shift ;;
|
||||
--gpu) GPU_MODE="$2"; shift 2 ;;
|
||||
-h|--help) usage 0 ;;
|
||||
*) echo "unknown option: $1" >&2; usage 1 ;;
|
||||
esac
|
||||
@@ -239,6 +242,65 @@ if [[ "$(git -C "$ENGINE" rev-parse HEAD 2>/dev/null)" != "$SEMIF_REF" ]]; then
|
||||
git -C "$ENGINE" checkout "$SEMIF_REF"
|
||||
fi
|
||||
|
||||
# --- stage 2b: pick the llama.cpp GPU backend ---------------------------------
|
||||
# The decision engine scores the GGUF through llama.cpp, which runs on CPU
|
||||
# unless llama-cpp-python was built with a GPU backend. A GPU-enabled build is
|
||||
# opt-in: llama.cpp's Vulkan backend splits the compute graph across CPU and
|
||||
# Vulkan even with `n_gpu_layers=0`, roughly doubling CPU latency, so a CPU-only
|
||||
# host must not get one. `--gpu <backend>` forces a build; `--gpu auto` (the
|
||||
# default) builds a GPU backend only when config.json asks for offload
|
||||
# (`engine.gpu_layers` nonzero).
|
||||
CONFIG_GPU_LAYERS="$(python3 - "$CONFIG" <<'PY'
|
||||
import json, sys
|
||||
try:
|
||||
print(int(json.load(open(sys.argv[1])).get("engine", {}).get("gpu_layers", 0) or 0))
|
||||
except Exception:
|
||||
print(0)
|
||||
PY
|
||||
)"
|
||||
detect_gpu_backend() {
|
||||
if command -v nvidia-smi >/dev/null 2>&1 && nvidia-smi >/dev/null 2>&1; then
|
||||
echo cuda; return
|
||||
fi
|
||||
if [[ -d /opt/rocm ]]; then
|
||||
echo rocm; return
|
||||
fi
|
||||
if command -v glslc >/dev/null 2>&1 && compgen -G "/usr/share/vulkan/icd.d/*.json" >/dev/null; then
|
||||
echo vulkan; return
|
||||
fi
|
||||
echo cpu
|
||||
}
|
||||
case "$GPU_MODE" in
|
||||
cpu) GPU_BACKEND=cpu ;;
|
||||
vulkan|rocm|cuda) GPU_BACKEND="$GPU_MODE" ;;
|
||||
auto)
|
||||
if [[ "$CONFIG_GPU_LAYERS" != 0 ]]; then
|
||||
GPU_BACKEND="$(detect_gpu_backend)"
|
||||
else
|
||||
GPU_BACKEND=cpu
|
||||
fi
|
||||
;;
|
||||
*) echo "unknown --gpu mode: $GPU_MODE (want auto, vulkan, rocm, cuda, cpu)" >&2; exit 2 ;;
|
||||
esac
|
||||
case "$GPU_BACKEND" in
|
||||
vulkan) LLAMA_CMAKE_ARGS="-DGGML_VULKAN=on" ;;
|
||||
rocm) LLAMA_CMAKE_ARGS="-DGGML_HIP=on" ;;
|
||||
cuda) LLAMA_CMAKE_ARGS="-DGGML_CUDA=on" ;;
|
||||
*) LLAMA_CMAKE_ARGS="" ;;
|
||||
esac
|
||||
echo "== decision-engine llama.cpp backend: $GPU_BACKEND (engine.gpu_layers=$CONFIG_GPU_LAYERS)"
|
||||
|
||||
if [[ "$GPU_BACKEND" = vulkan ]]; then
|
||||
VULKAN_PKGS=(libvulkan-dev glslc spirv-headers glslang-tools)
|
||||
vk_missing=()
|
||||
for p in "${VULKAN_PKGS[@]}"; do dpkg -s "$p" >/dev/null 2>&1 || vk_missing+=("$p"); done
|
||||
if [[ ${#vk_missing[@]} -gt 0 ]]; then
|
||||
echo "== installing Vulkan build prereqs: ${vk_missing[*]}"
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y "${vk_missing[@]}"
|
||||
fi
|
||||
fi
|
||||
|
||||
# --- stage 3: venv + deps -------------------------------------------------------
|
||||
if [[ ! -x "$PYTHON" ]]; then
|
||||
echo "== creating venv at $VENV"
|
||||
@@ -247,7 +309,22 @@ fi
|
||||
echo "== installing engine deps into $VENV"
|
||||
"$PIP" install --upgrade pip setuptools wheel
|
||||
"$PIP" install -e "$ENGINE" --no-deps
|
||||
CMAKE_BUILD_PARALLEL_LEVEL=6 MAKEFLAGS=-j6 "$PIP" install -r "$REPO_ROOT/requirements/staging.txt"
|
||||
CMAKE_ARGS="$LLAMA_CMAKE_ARGS" CMAKE_BUILD_PARALLEL_LEVEL=6 MAKEFLAGS=-j6 \
|
||||
"$PIP" install -r "$REPO_ROOT/requirements/staging.txt"
|
||||
|
||||
# Make the installed llama-cpp-python match the requested backend: pip ships a
|
||||
# CPU wheel and its wheel cache hands it back even when CMAKE_ARGS asks for a
|
||||
# GPU backend, so force a source build whenever offload support disagrees.
|
||||
installed_gpu=no
|
||||
"$PYTHON" -c 'import llama_cpp, sys; sys.exit(0 if llama_cpp.llama_supports_gpu_offload() else 1)' 2>/dev/null && installed_gpu=yes
|
||||
want_gpu=yes
|
||||
[[ "$GPU_BACKEND" = cpu ]] && want_gpu=no
|
||||
if [[ "$installed_gpu" != "$want_gpu" ]]; then
|
||||
LLAMA_PIN="$(grep -m1 -E '^llama-cpp-python==' "$REPO_ROOT/requirements/staging.txt")"
|
||||
echo "== rebuilding llama-cpp-python for the $GPU_BACKEND backend ($LLAMA_PIN)"
|
||||
CMAKE_ARGS="$LLAMA_CMAKE_ARGS" CMAKE_BUILD_PARALLEL_LEVEL=6 MAKEFLAGS=-j6 \
|
||||
"$PIP" install --no-deps --force-reinstall --no-cache-dir --no-binary llama-cpp-python "$LLAMA_PIN"
|
||||
fi
|
||||
"$PIP" install -e "$REPO_ROOT" --no-deps
|
||||
|
||||
# --- stage 3b: optional voice-gateway stack (--voice) --------------------------
|
||||
|
||||
Reference in new issue
Block a user