-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecall_bench_server.py
More file actions
294 lines (255 loc) · 10 KB
/
Copy pathrecall_bench_server.py
File metadata and controls
294 lines (255 loc) · 10 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""
recall_bench_server.py -- HTTP-server-based recall benchmark for dead-block GGUF.
Reuses all scoring/selection logic from recall_bench_compiled.py (imported).
Generation via HTTP against a running llama-server instead of llama_cpp.Llama.
Server lifecycle per model:
- Launch distrobox ai -- llama-server ... --port 8089 in background
- Poll /health until ok (120s timeout)
- Bench (POST /completion, single-client sequential)
- Kill server, wait for port release before next model
Usage:
python recall_bench_server.py \\
--model-a qwen2.5-3b-brainloop.gguf \\
--model-b cerebellum-deadblock-python.gguf \\
[--n 200] [--seed 42] [--out recall_results_deadblock.json]
"""
import argparse
import datetime
import json
import os
import re
import socket
import subprocess
import sys
import time
import urllib.request
import random
import torch
# Reuse all scoring/selection logic from recall_bench_compiled
from recall_bench_compiled import (
POST_CUTOFF_MODULES,
STATUS_FILE,
token_overlap,
content_overlap,
get_symbol_module,
is_post_cutoff,
load_docs_get_symbol,
compute_stats,
format_markdown_table,
append_status,
)
LLAMA_BIN = "/var/home/deucebucket/ai-drive/llama.cpp-stock/build/bin/llama-server"
SERVER_HOST = "127.0.0.1"
SERVER_PORT = 8089
SERVER_URL = f"http://{SERVER_HOST}:{SERVER_PORT}"
def wait_for_server(timeout: int = 120) -> bool:
deadline = time.time() + timeout
while time.time() < deadline:
try:
with urllib.request.urlopen(f"{SERVER_URL}/health", timeout=2) as r:
if r.status == 200:
return True
except Exception:
pass
time.sleep(1)
return False
def wait_for_port_release(timeout: int = 30) -> None:
"""Block until port 8089 is no longer in use. HARD ERROR if it stays occupied —
a stale server here means the next model's bench silently hits the wrong brain."""
deadline = time.time() + timeout
while time.time() < deadline:
try:
s = socket.create_connection((SERVER_HOST, SERVER_PORT), timeout=1)
s.close()
time.sleep(1)
except (ConnectionRefusedError, OSError):
return
raise RuntimeError(
f"port {SERVER_PORT} still occupied after {timeout}s — stale llama-server "
f"alive (distrobox wrapper kill does not reach it). Refusing to continue."
)
def assert_loaded_model(expected_model_path: str) -> None:
"""Ask the running server which model it actually loaded; hard-fail on mismatch.
This is the wiring assertion: no prompts are sent to an unverified server."""
import os
expected = os.path.basename(expected_model_path)
found = None
try:
with urllib.request.urlopen(f"{SERVER_URL}/props", timeout=5) as r:
props = json.loads(r.read())
found = props.get("model_path") or props.get("default_generation_settings", {}).get("model")
except Exception:
pass
if not found:
try:
with urllib.request.urlopen(f"{SERVER_URL}/v1/models", timeout=5) as r:
found = json.loads(r.read())["data"][0]["id"]
except Exception as e:
raise RuntimeError(f"cannot verify loaded model identity: {e}")
if expected not in str(found):
raise RuntimeError(
f"WIRING ERROR: server reports model '{found}', expected '{expected}'. "
f"A stale server from the previous model is still answering."
)
print(f"[+] Wiring verified: server is running {expected}")
def start_server(model_path: str) -> subprocess.Popen:
cmd = [
"distrobox", "enter", "ai", "--",
LLAMA_BIN,
"-m", model_path,
"-ngl", "99",
"--parallel", "4",
"-c", "24576",
"--host", SERVER_HOST,
"--port", str(SERVER_PORT),
"--log-disable",
]
print(f"[*] Starting server: {' '.join(cmd)}")
proc = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return proc
def stop_server(proc: subprocess.Popen) -> None:
# Terminating the distrobox wrapper does NOT kill llama-server inside the
# container — kill the actual server by pattern, then verify the port died.
if proc.poll() is None:
proc.terminate()
try:
proc.wait(timeout=15)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait()
subprocess.run(["pkill", "-f", f"llama-server.*--port {SERVER_PORT}"],
capture_output=True)
time.sleep(2)
subprocess.run(["pkill", "-9", "-f", f"llama-server.*--port {SERVER_PORT}"],
capture_output=True)
wait_for_port_release()
def complete_http(prompt: str, max_tokens: int = 60) -> str:
payload = json.dumps({
"prompt": prompt,
"n_predict": max_tokens,
"temperature": 0.0,
"stream": False,
}).encode()
req = urllib.request.Request(
f"{SERVER_URL}/completion",
data=payload,
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(req, timeout=60) as r:
body = json.loads(r.read().decode())
return body.get("content", "")
def run_model_http(docs: list, indices: list, max_tokens: int,
overlap_threshold: float, label: str) -> list:
results = []
total = len(indices)
hits = 0
for rank, idx in enumerate(indices):
symbol, doc = load_docs_get_symbol(docs, idx)
prompt = f"Question: How do I use {symbol} in Python?\nAnswer: "
completion = complete_http(prompt, max_tokens)
overlap = content_overlap(doc, completion)
is_hit = overlap >= overlap_threshold
if is_hit:
hits += 1
results.append({
"idx": idx,
"symbol": symbol,
"doc": doc,
"completion": completion.strip(),
"overlap": round(overlap, 4),
"hit": is_hit,
"post_cutoff": is_post_cutoff(symbol),
})
if (rank + 1) % 20 == 0 or (rank + 1) == total:
print(f" [{label}] {rank+1}/{total} running recall={hits/(rank+1):.3f}")
return results
def main():
parser = argparse.ArgumentParser(
description="HTTP-server recall benchmark: model-A vs model-B."
)
parser.add_argument("--model-a", required=True)
parser.add_argument("--model-b", required=True)
parser.add_argument("--n", type=int, default=200)
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--out", default="recall_results_deadblock.json")
parser.add_argument("--docs", default="python_13k_docs.pt")
parser.add_argument("--max-tokens", type=int, default=60)
parser.add_argument("--overlap-threshold", type=float, default=0.5)
args = parser.parse_args()
# Load docs and sample
print(f"[*] Loading docs from {args.docs}...")
docs = torch.load(args.docs, map_location="cpu", weights_only=False)
print(f"[*] Loaded {len(docs)} docs.")
random.seed(args.seed)
indices = random.sample(range(len(docs)), min(args.n, len(docs)))
print(f"[*] Sampled {len(indices)} symbols (seed={args.seed}).")
pc_sample = [i for i in indices
if is_post_cutoff(load_docs_get_symbol(docs, i)[0])]
print(f"[*] Post-cutoff symbols in sample: {len(pc_sample)}")
# ---- Model A ----
print(f"\n[*] Launching server for model A: {args.model_a}")
proc_a = start_server(args.model_a)
try:
if not wait_for_server(120):
print("[!] Server A failed to start. Aborting.")
stop_server(proc_a)
sys.exit(1)
assert_loaded_model(args.model_a)
print("[*] Server A ready. Running bench...")
results_a = run_model_http(docs, indices, args.max_tokens,
args.overlap_threshold, label="A")
finally:
print("[*] Stopping server A...")
stop_server(proc_a)
# ---- Model B ----
print(f"\n[*] Launching server for model B: {args.model_b}")
proc_b = start_server(args.model_b)
try:
if not wait_for_server(120):
print("[!] Server B failed to start. Aborting.")
stop_server(proc_b)
sys.exit(1)
assert_loaded_model(args.model_b)
print("[*] Server B ready. Running bench...")
results_b = run_model_http(docs, indices, args.max_tokens,
args.overlap_threshold, label="B")
finally:
print("[*] Stopping server B...")
stop_server(proc_b)
# ---- Wiring self-check: identical A/B under greedy decoding = miswired ----
n_same = sum(1 for a, b in zip(results_a, results_b)
if a["completion"] == b["completion"])
print(f"[*] Identical-completion fraction A vs B: {n_same}/{len(results_a)}")
if results_a and n_same == len(results_a) and args.model_a != args.model_b:
raise RuntimeError(
"WIRING ERROR: 100% identical completions between supposedly different "
"models — both passes hit the same server. Results discarded."
)
# ---- Stats ----
stats_a = compute_stats(results_a, baseline_results=results_a)
stats_b = compute_stats(results_b, baseline_results=results_a)
table = format_markdown_table(stats_a, stats_b, args.model_a, args.model_b)
print("\n" + table)
output = {
"meta": {
"model_a": args.model_a,
"model_b": args.model_b,
"n": args.n,
"seed": args.seed,
"max_tokens": args.max_tokens,
"overlap_threshold": args.overlap_threshold,
"timestamp": datetime.datetime.now().isoformat(),
"server_binary": LLAMA_BIN,
},
"stats_a": stats_a,
"stats_b": stats_b,
"results_a": results_a,
"results_b": results_b,
}
with open(args.out, "w") as fh:
json.dump(output, fh, indent=2)
print(f"[+] Results saved to {args.out}")
append_status(table, args.model_a, args.model_b, args.n, args.seed)
if __name__ == "__main__":
main()