File size: 13,881 Bytes
f9d0103 | 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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | """Run resident Qwen inference, semantic probes, timing matrix and a real HTTP server."""
from __future__ import annotations
import argparse
import copy
import hashlib
import json
import socket
import threading
import time
from pathlib import Path
import httpx
import numpy as np
import torch
import uvicorn
from PIL import Image, ImageDraw
from veyra.data import read_records
from veyra.model import VeyraModel
from veyra.schema import DecisionRequest
from veyra.server import create_app
def latency_summary(values):
return {
"p50": float(np.percentile(values, 50)),
"p95": float(np.percentile(values, 95)),
"min": min(values),
"max": max(values),
"samples": values,
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--checkpoint", type=Path, required=True)
parser.add_argument("--records", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
parser.add_argument("--iterations", type=int, default=40)
args = parser.parse_args()
if args.output.exists():
raise FileExistsError("validation reports are immutable; use a new output path")
if args.iterations < 20:
raise ValueError("use at least 20 measured fresh-image requests per condition")
root = args.records.parent.resolve()
fixtures = root / "validation-fixtures"
fixtures.mkdir(exist_ok=True)
colors = [
"blue",
"red",
"green",
"yellow",
"purple",
"orange",
"black",
"pink",
"brown",
"cyan",
"gray",
"magenta",
"beige",
"maroon",
"navy",
"teal",
]
for size in (384, 1024):
for i in range(args.iterations + 3):
with Image.new("RGB", (size, size * 2 // 3), "white") as image:
draw = ImageDraw.Draw(image)
x, y = 15 + i % 13, size // 4 + i % 11
draw.rectangle((x, y, x + size // 5, y + size // 5), fill=colors[i % 4])
image.save(fixtures / f"{size}-{i}.png")
start = time.perf_counter()
model = VeyraModel.load(args.checkpoint, local_files_only=True)
load_seconds = time.perf_counter() - start
manifest = json.loads((args.checkpoint / "manifest.json").read_text(encoding="utf-8"))
report = {
"checkpoint_weights_sha256": manifest["weights_sha256"],
"checkpoint_manifest_sha256": hashlib.sha256(
(args.checkpoint / "manifest.json").read_bytes()
).hexdigest(),
"load_seconds": load_seconds,
"gpu": torch.cuda.get_device_name(),
"scope": "JSON parse, image decode, processor, Qwen, trained head, JSON serialize",
"feature_cache": False,
"serial_requests": True,
"warmups_per_condition": 3,
"excludes": ["model loading", "HTTP transport (reported separately)"],
"latency_target_ms": 300,
"matrix": {},
"protocol": {},
"probes": [],
}
def run(raw):
return model.predict(DecisionRequest.model_validate(raw), image_root=root)
def request(i=0, size=384, count=4, multi=False):
choices = colors[:count] if count != 2 else [colors[i % 4], colors[(i + 1) % 4]]
raw = {
"state": {
"text": "Use the picture as evidence.",
"images": [{"path": f"validation-fixtures/{size}-{i}.png"}],
},
"questions": {
"color": {
"type": "choice",
"instructions": "What is the color of the leftmost object?",
"criteria": {f"c{j}": color for j, color in enumerate(choices)},
}
},
}
if multi:
raw["questions"].update(
{
"shape": {"type": "noul", "instructions": "Is the leftmost object a square?"},
"count": {
"type": "score",
"instructions": "Rate the number of visible objects.",
"criteria": [f"Exactly {j} visible objects." for j in range(5)],
},
}
)
return raw
# Every measured image has distinct bytes; no image features or prefix states are reused.
for size, count, multi in (
(384, 2, False),
(384, 4, False),
(384, 8, False),
(384, 16, False),
(384, 4, True),
(1024, 4, False),
):
texts = [json.dumps(request(i, size, count, multi)) for i in range(args.iterations)]
for index in range(args.iterations, args.iterations + 3):
run(request(index, size, count, multi))
torch.cuda.synchronize()
torch.cuda.reset_peak_memory_stats()
timings, tokens, valid, correct = [], [], True, []
for i, text in enumerate(texts):
torch.cuda.synchronize()
start = time.perf_counter()
answer = run(json.loads(text))
json.dumps(answer)
torch.cuda.synchronize()
timings.append((time.perf_counter() - start) * 1000)
tokens.append(answer["usage"]["input_tokens"])
picked = answer["answers"]["color"]["choice"]
meaning = json.loads(text)["questions"]["color"]["criteria"][picked]
correct.append(meaning == colors[i % 4])
for value in answer["answers"].values():
valid &= abs(sum(value["probabilities"].values()) - 1) < 1e-5
timing = latency_summary(timings)
key = f"image{size}-candidates{count}-questions{3 if multi else 1}"
report["matrix"][key] = {
"latency_ms": timing,
"requests": len(timings),
"tokens_min_max": [min(tokens), max(tokens)],
"peak_allocated_mib": torch.cuda.max_memory_allocated() / 2**20,
"peak_reserved_mib": torch.cuda.max_memory_reserved() / 2**20,
"probabilities_valid": bool(valid),
"color_accuracy": sum(correct) / len(correct),
"p95_target_met": timing["p95"] <= 300,
}
print(key, timing["p50"], timing["p95"], flush=True)
raw = request(multi=True)
original = run(raw)
changed = copy.deepcopy(raw)
old_criteria = changed["questions"]["color"]["criteria"]
changed["questions"]["color"]["criteria"] = {
f"new-{k}": v for k, v in reversed(list(old_criteria.items()))
}
changed["questions"] = dict(reversed(list(changed["questions"].items())))
permuted = run(changed)
differences = [
abs(
original["answers"]["color"]["probabilities"][key]
- permuted["answers"]["color"]["probabilities"]["new-" + key]
)
for key in old_criteria
]
report["protocol"]["choice_id_order_max_probability_difference"] = max(differences)
report["protocol"]["choice_id_order_pass"] = max(differences) < 1e-5
report["protocol"]["zero_generated_tokens"] = original["usage"]["output_tokens"] == 0
for i, note in ((0, "blue_image"), (1, "red_image")):
probe = request(i)
if i == 0:
probe["state"]["text"] += (
" An unverified note claims the object is red. Trust the image if they disagree."
)
report["probes"].append({"name": note, "expected_choice": f"c{i}", "result": run(probe)})
changed = request()
changed["questions"]["color"]["instructions"] = (
"Apply this new mapping: blue means urgent; all other colors mean normal."
)
changed["questions"]["color"]["criteria"] = {"x": "urgent", "y": "normal"}
report["probes"].append(
{"name": "unseen_visual_policy", "expected_choice": "x", "result": run(changed)}
)
for weight, cutoff in ((3, 8), (12, 5), (6, 6), (9, 12)):
probe = {
"state": {"text": f"Parcel weight: {weight} kilograms."},
"questions": {
"route": {
"type": "choice",
"instructions": (
f"Route parcels weighing at least {cutoff} kilograms to the freight desk. "
"Send lighter parcels to the regular desk. Which desk receives this parcel?"
),
"criteria": {"f": "freight desk", "r": "regular desk"},
}
},
}
report["probes"].append(
{
"name": f"unseen_parcel_policy_{weight}_{cutoff}",
"expected_choice": "f" if weight >= cutoff else "r",
"result": run(probe),
}
)
missing = {
"state": {"text": "No image or description of the object was supplied."},
"questions": {
"visible": {
"type": "noul",
"instructions": "Is the unobserved object a square?",
}
},
}
report["probes"].append(
{
"name": "missing_evidence",
"target_true_probability": 0.5,
"target_note": "Uninformative soft annotation; not an empirical event frequency.",
"result": run(missing),
}
)
# Whole-model subset diagnostic uses dev records, distinct from held-out quality reports.
records = read_records(args.records)
subset_rows = []
for family in sorted({r.family for r in records}):
selected = [
r
for r in records
if r.split == "dev" and r.family == family and "-context-" not in r.id
][:12]
full_scores, single_scores, decisions_changed = [], [], []
for record in selected:
full = model.predict(record.request, image_root=root)["answers"]
for name, question in record.request.questions.items():
one = DecisionRequest(state=record.request.state, questions={name: question})
single = model.predict(one, image_root=root)["answers"][name]
first = max(full[name]["probabilities"], key=full[name]["probabilities"].get)
second = max(single["probabilities"], key=single["probabilities"].get)
full_scores.append(record.targets[name][first])
single_scores.append(record.targets[name][second])
decisions_changed.append(first != second)
subset_rows.append(
{
"family": family,
"questions": len(full_scores),
"full_expected_accuracy": float(np.mean(full_scores)),
"single_expected_accuracy": float(np.mean(single_scores)),
"changed_decisions": sum(decisions_changed),
}
)
report["dev_subset_diagnostic"] = subset_rows
# A real TCP connection measures transport, ASGI and worker-thread overhead as well.
sock = socket.socket()
sock.bind(("127.0.0.1", 0))
port = sock.getsockname()[1]
server = uvicorn.Server(uvicorn.Config(create_app(model, root), log_level="warning"))
thread = threading.Thread(target=server.run, kwargs={"sockets": [sock]}, daemon=True)
thread.start()
try:
deadline = time.monotonic() + 15
while not server.started and time.monotonic() < deadline:
time.sleep(0.05)
if not server.started:
raise RuntimeError("validation HTTP server did not become ready")
with httpx.Client(base_url=f"http://127.0.0.1:{port}", timeout=10) as client:
assert client.get("/health").json()["trained"]
assert client.get("/v1/schema").status_code == 200
for _ in range(3):
client.post("/v1/systemone", json=request()).raise_for_status()
timings = []
for i in range(args.iterations):
start = time.perf_counter()
response = client.post("/v1/systemone", json=request(i))
response.raise_for_status()
assert response.json()["usage"]["output_tokens"] == 0
timings.append((time.perf_counter() - start) * 1000)
report["http"] = {
"latency_ms": latency_summary(timings),
"requests": len(timings),
"local_loopback": True,
"p95_target_met": np.percentile(timings, 95) <= 300,
}
forbidden = request()
forbidden["state"]["images"][0]["path"] = "../../README.md"
report["protocol"]["http_path_escape_rejected"] = (
client.post("/v1/systemone", json=forbidden).status_code == 422
)
oversized = request()
oversized["state"]["text"] = "extra observation " * 2500
report["protocol"]["http_token_budget_rejected"] = (
client.post("/v1/systemone", json=oversized).status_code == 422
)
finally:
server.should_exit = True
thread.join(timeout=15)
sock.close()
# numpy scalar booleans are converted explicitly for portable JSON.
report["http"]["p95_target_met"] = bool(report["http"]["p95_target_met"])
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8", newline="\n")
for name in (
"choice_id_order_pass",
"zero_generated_tokens",
"http_path_escape_rejected",
"http_token_budget_rejected",
):
if not report["protocol"][name]:
raise AssertionError(f"protocol validation failed: {name}; see {args.output}")
print(
json.dumps(
{
"protocol": report["protocol"],
"subset": report["dev_subset_diagnostic"],
"http_p95": report["http"]["latency_ms"]["p95"],
}
),
flush=True,
)
if __name__ == "__main__":
main()
|