diff --git a/docs/ai-lab.md b/docs/ai-lab.md index 96b4abb..179a06c 100644 --- a/docs/ai-lab.md +++ b/docs/ai-lab.md @@ -45,7 +45,8 @@ or modify the model. The Debian user must be able to read the path printed by test. Reports and model output are saved with mode 0600 under the lab's `reports` directory. The report labels llama.cpp's prompt and generation speeds when its timing summary is present; a null value means timing was not recognized. -Review the log to confirm actual GPU offload. +The lab captures the process's controlling terminal so its output is included +in the private log. Review the log to confirm actual GPU offload. Each benchmark is one cold request, using the same prompt, maximum output, context size and temperature. CLI and Ollama prompt templates, sampling options, diff --git a/scripts/ai-lab.py b/scripts/ai-lab.py index bf7af98..0d0cec4 100644 --- a/scripts/ai-lab.py +++ b/scripts/ai-lab.py @@ -2,13 +2,19 @@ """An isolated llama.cpp build and inference benchmark for the Debian host.""" import json import hashlib +import errno +import fcntl import os +import pty from pathlib import Path import re +import select import shutil +import signal import subprocess import sys import tempfile +import termios import time import urllib.request from datetime import datetime, timezone @@ -42,6 +48,56 @@ def command(args, *, capture=False, timeout=None): return result +def terminal_command(args, timeout=600): + """Capture programs that write directly to their controlling terminal.""" + master, slave = pty.openpty() + process = None + output = bytearray() + + def attach_terminal(): + os.setsid() + fcntl.ioctl(0, termios.TIOCSCTTY, 0) + + try: + process = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave, preexec_fn=attach_terminal, close_fds=True) + os.close(slave) + slave = -1 + deadline = time.monotonic() + timeout + while True: + remaining = deadline - time.monotonic() + if remaining <= 0: + raise subprocess.TimeoutExpired(args, timeout) + ready, _, _ = select.select([master], [], [], min(remaining, 1)) + if not ready: + continue + try: + chunk = os.read(master, 65536) + except OSError as exc: + if exc.errno != errno.EIO: + raise + break + if not chunk: + break + output.extend(chunk) + if len(output) > 10 * 1024 * 1024: + raise RuntimeError("Benchmark terminal output exceeded 10 MiB") + status = process.wait(timeout=max(0.1, deadline - time.monotonic())) + text = output.decode("utf-8", errors="replace") + if status: + raise RuntimeError(f"{args[0]} failed (exit {status}): {text[-1500:]}") + return text + finally: + if process is not None and process.poll() is None: + try: + os.killpg(process.pid, signal.SIGKILL) + except ProcessLookupError: + pass + process.wait() + os.close(master) + if slave >= 0: + os.close(slave) + + def read_url(path, payload=None, timeout=15): data = json.dumps(payload).encode() if payload is not None else None request = urllib.request.Request(ENDPOINT + path, data=data, headers={"Content-Type": "application/json"}) @@ -244,8 +300,7 @@ def benchmark(backend): raise RuntimeError("Vulkan device must have a name such as Vulkan0") args = llama_args(path, backend, device) started = time.monotonic() - completed = command(args, capture=True, timeout=600) - log = completed.stdout + "\n" + completed.stderr + log = terminal_command(args, timeout=600) common.update({"device": device, "elapsed_seconds": round(time.monotonic() - started, 3), "llama_cpp_version": TAG, **parse_llama_timings(log), "note": common["note"] + " Confirm actual GPU offload in the log; unavailable timing values are null."}) make_report(backend, common, log) diff --git a/tests/jeannie/test_ai_lab.py b/tests/jeannie/test_ai_lab.py index 3076775..895f5c3 100644 --- a/tests/jeannie/test_ai_lab.py +++ b/tests/jeannie/test_ai_lab.py @@ -1,6 +1,7 @@ import importlib.util from pathlib import Path from types import SimpleNamespace +import sys import unittest from unittest import mock @@ -66,6 +67,11 @@ class AiLabTest(unittest.TestCase): self.assertEqual(result, {"prompt_tokens_per_second": 208.5, "generation_tokens_per_second": 44.3}) self.assertEqual(lab.parse_llama_timings("unrecognized"), {"prompt_tokens_per_second": None, "generation_tokens_per_second": None}) + def test_benchmark_captures_direct_controlling_terminal_output(self): + code = 'import os; f = os.open("/dev/tty", os.O_WRONLY); os.write(f, b"[ Prompt: 290.7 t/s | Generation: 44.4 t/s ]\\n"); os.close(f)' + log = lab.terminal_command([sys.executable, "-c", code], timeout=10) + self.assertEqual(lab.parse_llama_timings(log), {"prompt_tokens_per_second": 290.7, "generation_tokens_per_second": 44.4}) + def test_qwen_runs_request_no_thinking_and_greedy_sampling(self): colibri = lab.colibri_args() llama = lab.llama_args(Path("/demo/qwen.gguf"), "qwen-cpu", "none")