#!/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 import subprocess import sys def main() -> int: bin_path = "/repo/target/llama.cpp/build/bin/llama-completion" model_path = "/repo/target/models/smollm2-135m-instruct-q4_k_m.gguf" env = os.environ.copy() ld = "/repo/target/llama.cpp/build/bin:/usr/local/cuda-12.9/lib64" 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 = [ bin_path, "-m", model_path, "-p", prompt, "-n", "64", "-ngl", "99", "--temp", "0", "--seed", "42", "-no-cnv", "--no-display-prompt", "--simple-io", ] proc = subprocess.run(cmd, env=env, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=180) 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": "SmolLM2-135M-Instruct-Q4_K_M.gguf", "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())