File size: 10,025 Bytes
8f3790a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""Standalone evaluation for a GGUF with llama.cpp (no harness dependency).

Two modes:

  smoke       start ``llama-server``, wait for ``/health``, run a set of chat
              prompts, record the responses and per-reply latency, shut the
              server down, and write a JSON report.
  perplexity  run ``llama-perplexity`` directly on a corpus and record PPL.

This replaces the original evaluation driver (which depended on a private
harness). It is intentionally stdlib-only apart from the model binaries.

Examples::

    python scripts/eval_llama_server.py smoke \
        --server-bin /path/to/llama-server \
        --gguf /path/to/Ternary-Bonsai-2-27B-PQ2_0.gguf \
        --ngl 99 --ctx 4096 --no-thinking \
        --out artifacts/eval/smoke.json

    python scripts/eval_llama_server.py perplexity \
        --perplexity-bin /path/to/llama-perplexity \
        --gguf /path/to/Ternary-Bonsai-2-27B-PQ2_0.gguf \
        --corpus artifacts/corpus/tinyshakespeare.txt \
        --ctx 512 --chunks 8 --ngl 99 \
        --out artifacts/eval/ppl.json

Remember: run one heavy ROCm process at a time, and pin the device with
``HIP_VISIBLE_DEVICES`` / ``CUDA_VISIBLE_DEVICES`` if the box is shared.
"""

from __future__ import annotations

import argparse
import hashlib
import json
import re
import subprocess
import sys
import time
import urllib.error
import urllib.request
from pathlib import Path

DEFAULT_PROMPTS = [
    "Reply with the single word: ok",
    "Name the capital of France.",
    "What is 17 times 3? Answer with the number only.",
    "Write one short sentence about the sea.",
    "Repeat exactly: ternary.",
]

PPL_RE = re.compile(r"Final estimate:\s*PPL\s*=\s*([0-9.eE+-]+)\s*(?:\+/?-\s*([0-9.eE+-]+))?")


def _sha256(path: str) -> str | None:
    try:
        digest = hashlib.sha256()
        with Path(path).open("rb") as handle:
            while chunk := handle.read(1 << 20):
                digest.update(chunk)
        return digest.hexdigest()
    except OSError:
        return None


def _post_json(url: str, payload: dict, timeout: float = 600.0) -> dict:
    data = json.dumps(payload).encode("utf-8")
    req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=timeout) as resp:
        return json.loads(resp.read().decode("utf-8"))


def _get(url: str, timeout: float = 5.0) -> bool:
    try:
        with urllib.request.urlopen(url, timeout=timeout) as resp:
            return 200 <= resp.status < 300
    except (urllib.error.URLError, OSError):
        return False


def _wait_healthy(base_url: str, timeout: float, proc: subprocess.Popen) -> None:
    deadline = time.time() + timeout
    while time.time() < deadline:
        if proc.poll() is not None:
            raise RuntimeError(f"llama-server exited early with code {proc.returncode}")
        if _get(f"{base_url}/health"):
            return
        time.sleep(1.0)
    raise TimeoutError(f"llama-server not healthy after {timeout:.0f}s")


def run_smoke(args: argparse.Namespace) -> dict:
    prompts = DEFAULT_PROMPTS
    if args.prompts_file:
        prompts = [
            line.strip()
            for line in Path(args.prompts_file).read_text(encoding="utf-8").splitlines()
            if line.strip()
        ]

    log_path = Path(args.log) if args.log else None
    log_handle = open(log_path, "w", encoding="utf-8") if log_path else subprocess.DEVNULL
    cmd = [
        args.server_bin,
        "-m", args.gguf,
        "--host", args.host,
        "--port", str(args.port),
        "-ngl", str(args.ngl),
        "-c", str(args.ctx),
    ]
    if args.extra:
        cmd += args.extra

    base_url = f"http://{args.host}:{args.port}"
    print(f"[eval] starting: {' '.join(cmd)}", flush=True)
    proc = subprocess.Popen(cmd, stdout=log_handle, stderr=subprocess.STDOUT)
    started = time.time()
    rows = []
    try:
        _wait_healthy(base_url, args.startup_timeout, proc)
        print(f"[eval] healthy after {time.time() - started:.1f}s", flush=True)
        for prompt in prompts:
            payload = {
                "model": "local",
                "messages": [{"role": "user", "content": prompt}],
                "max_tokens": args.max_tokens,
                "temperature": args.temperature,
            }
            if args.no_thinking:
                payload["chat_template_kwargs"] = {"enable_thinking": False}
            t0 = time.time()
            try:
                resp = _post_json(f"{base_url}/v1/chat/completions", payload)
                content = resp["choices"][0]["message"].get("content") or ""
                status = "PASS" if content.strip() else "EMPTY"
                tokens = (resp.get("usage") or {}).get("completion_tokens")
            except Exception as exc:  # noqa: BLE001 - report, do not abort the run
                content, status, tokens = f"<error: {exc}>", "ERROR", None
            rows.append({
                "prompt": prompt,
                "response": content,
                "status": status,
                "completion_tokens": tokens,
                "seconds": round(time.time() - t0, 3),
            })
            print(f"[eval] {status}: {prompt!r} -> {content[:80]!r}", flush=True)
    finally:
        proc.terminate()
        try:
            proc.wait(timeout=30)
        except subprocess.TimeoutExpired:
            proc.kill()
        if log_path:
            log_handle.close()

    passed = sum(1 for r in rows if r["status"] == "PASS")
    return {
        "mode": "smoke",
        "gguf": args.gguf,
        "server_bin": args.server_bin,
        "server_bin_sha256": _sha256(args.server_bin),
        "ngl": args.ngl,
        "ctx": args.ctx,
        "no_thinking": args.no_thinking,
        "prompts": len(rows),
        "passed": passed,
        "failed": len(rows) - passed,
        "seconds": round(time.time() - started, 3),
        "rows": rows,
    }


def run_perplexity(args: argparse.Namespace) -> dict:
    cmd = [
        args.perplexity_bin,
        "-m", args.gguf,
        "-f", args.corpus,
        "-ngl", str(args.ngl),
        "-c", str(args.ctx),
        "--chunks", str(args.chunks),
    ]
    if args.extra:
        cmd += args.extra

    print(f"[eval] starting: {' '.join(cmd)}", flush=True)
    started = time.time()
    proc = subprocess.run(cmd, capture_output=True, text=True, timeout=args.timeout)
    output = (proc.stdout or "") + (proc.stderr or "")
    match = PPL_RE.search(output)
    if not match:
        sys.stderr.write(output[-2000:])
        raise RuntimeError("could not parse perplexity from llama-perplexity output")
    ppl = float(match.group(1))
    stderr = float(match.group(2)) if match.group(2) else None
    return {
        "mode": "perplexity",
        "gguf": args.gguf,
        "perplexity_bin": args.perplexity_bin,
        "perplexity_bin_sha256": _sha256(args.perplexity_bin),
        "corpus": args.corpus,
        "ngl": args.ngl,
        "ctx": args.ctx,
        "chunks": args.chunks,
        "ppl": ppl,
        "stderr": stderr,
        "seconds": round(time.time() - started, 3),
        "returncode": proc.returncode,
    }


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    sub = parser.add_subparsers(dest="mode", required=True)

    smoke = sub.add_parser("smoke", help="serve the GGUF and run chat prompts")
    smoke.add_argument("--server-bin", required=True, help="path to llama-server")
    smoke.add_argument("--gguf", required=True)
    smoke.add_argument("--host", default="127.0.0.1")
    smoke.add_argument("--port", type=int, default=8090)
    smoke.add_argument("--ngl", default=0, help="GPU layers (int) or 'all'; default 0 is safe")
    smoke.add_argument("--ctx", type=int, default=4096)
    smoke.add_argument("--max-tokens", type=int, default=64)
    smoke.add_argument("--temperature", type=float, default=0.0)
    smoke.add_argument("--no-thinking", action="store_true", help="pass enable_thinking=false")
    smoke.add_argument("--prompts-file", default="", help="one prompt per line")
    smoke.add_argument("--startup-timeout", type=float, default=900.0)
    smoke.add_argument("--log", default="", help="server log path")
    smoke.add_argument("--extra", nargs=argparse.REMAINDER, default=[], help="extra flags passed to llama-server")
    smoke.add_argument("--allow-partial", action="store_true",
                       help="return zero even when prompts are empty/error responses")
    smoke.add_argument("--out", required=True)
    smoke.set_defaults(func=run_smoke)

    ppl = sub.add_parser("perplexity", help="run llama-perplexity on a corpus")
    ppl.add_argument("--perplexity-bin", required=True, help="path to llama-perplexity")
    ppl.add_argument("--gguf", required=True)
    ppl.add_argument("--corpus", required=True)
    ppl.add_argument("--ngl", default=0, help="GPU layers (int) or 'all'; default 0 is safe")
    ppl.add_argument("--ctx", type=int, default=512)
    ppl.add_argument("--chunks", type=int, default=8)
    ppl.add_argument("--timeout", type=float, default=3600.0)
    ppl.add_argument("--extra", nargs=argparse.REMAINDER, default=[], help="extra flags passed to llama-perplexity")
    ppl.add_argument("--out", required=True)
    ppl.set_defaults(func=run_perplexity)
    return parser


def main(argv=None) -> int:
    args = build_parser().parse_args(argv)
    if isinstance(args.ngl, str) and args.ngl.lower() == "all":
        args.ngl = 999
    result = args.func(args)
    out = Path(args.out)
    out.parent.mkdir(parents=True, exist_ok=True)
    out.write_text(json.dumps(result, indent=2), encoding="utf-8")
    print(f"[eval] wrote {out}", flush=True)
    if (result.get("mode") == "smoke" and result.get("failed", 0)
            and not getattr(args, "allow_partial", False)):
        return 2
    return 0


if __name__ == "__main__":
    raise SystemExit(main())