Files
nvdriver/tools/llama_cpp_parity.py

98 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Real-world llama.cpp (CUDA 12.9.1 sm_70) GGUF inference parity runner.
Runs unmodified target/llama.cpp/build/bin/llama-completion on real
SmolLM2-135M-Instruct-Q4_K_M.gguf with deterministic greedy decoding
(--temp 0 --seed 42 -ngl 99 -n 64 --no-display-prompt) and emits JSON with
exact output text and SHA-256 digest for stock vs nvidia_rs.ko parity.
"""
import hashlib
import json
import os
from pathlib import Path
import subprocess
import sys
REPO_ROOT = Path(__file__).resolve().parents[1]
def _pick_path(env_var: str, rel_path: str) -> Path:
override = os.environ.get(env_var)
if override:
return Path(override).resolve()
local = REPO_ROOT / rel_path
if local.exists():
return local
vm_path = Path("/repo") / rel_path
if vm_path.exists():
return vm_path
return local
def resolve_llama_paths() -> tuple[Path, Path, str]:
bin_path = _pick_path("LLAMA_COMPLETION", "target/llama.cpp/build/bin/llama-completion")
model_path = _pick_path("LLAMA_MODEL", "target/models/smollm2-135m-instruct-q4_k_m.gguf")
cuda_lib = os.environ.get("CUDA_LIB_DIR", "/usr/local/cuda-12.9/lib64")
ld_parts = [str(bin_path.parent)]
if cuda_lib:
ld_parts.append(cuda_lib)
return bin_path, model_path, ":".join(ld_parts)
def main() -> int:
bin_path, model_path, ld = resolve_llama_paths()
if not bin_path.is_file():
print(json.dumps({"passed": False, "error": f"llama-completion binary not found: {bin_path}"}))
return 1
if not model_path.is_file():
print(json.dumps({"passed": False, "error": f"GGUF model not found: {model_path}"}))
return 1
env = os.environ.copy()
env["LD_LIBRARY_PATH"] = ld + (":" + env["LD_LIBRARY_PATH"] if env.get("LD_LIBRARY_PATH") else "")
prompt = "Explain why GPUs accelerate matrix multiplication in two sentences:"
cmd = [
str(bin_path),
"-m", str(model_path),
"-p", prompt,
"-n", "64",
"-ngl", "99",
"--temp", "0",
"--seed", "42",
"-no-cnv",
"--no-display-prompt",
"--simple-io",
]
try:
proc = subprocess.run(cmd, env=env, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=180)
except subprocess.TimeoutExpired as exc:
print(json.dumps({"passed": False, "error": f"llama-completion timed out after {exc.timeout}s"}))
return 1
out_text = proc.stdout.strip()
digest = hashlib.sha256(out_text.encode("utf-8")).hexdigest()[:16]
# Count CUDA devices / GPU offload indicators reported in stderr
cuda_devs = [
ln.strip()
for ln in proc.stderr.splitlines()
if any(k in ln for k in ("CUDA0", "CUDA1", "Tesla V100", "ggml_cuda_init", "offloaded", "graphs reused"))
]
passed = proc.returncode == 0 and len(out_text) > 20 and len(cuda_devs) > 0
result = {
"passed": passed,
"rc": proc.returncode,
"model": model_path.name,
"cuda_devices": cuda_devs[:10],
"output_len": len(out_text),
"output_digest": digest,
"output_text": out_text,
}
if not passed:
result["stderr_tail"] = proc.stderr[-1000:]
print(json.dumps(result))
return 0 if passed else 1
if __name__ == "__main__":
raise SystemExit(main())