Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
127 lines
5.2 KiB
Python
127 lines
5.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Bitwise comparison of the evidence lines of laptop identity runs (scripts/laptop/identity-gate.sh).
|
|
|
|
The laptop bench (`original_serve_laptop_bench`) is run with LELEKA_BRINGUP_TRACE=1 LELEKA_TRACE_LOGPROBS=16 LELEKA_TRACE_DRAFT=1
|
|
LELEKA_TRACE_PREFILL_R=1 and serves every prompt twice in one process (pass 0, pass 1). The evidence of a request is, in order of the log:
|
|
|
|
PREFILL-R lines residual hash of every prompt chunk
|
|
DBG logprobs window the window's inputs and argmax ids, followed by its tab-separated top-16 rows (position, target, lp, top1, lp, ...)
|
|
DRAFT lines position, accepted row, first cell, draft ids and the IEEE bits of the probabilities
|
|
LAPTOP output the output token ids of the first pass (printed once per prompt)
|
|
|
|
identity_compare.py --prompts N LABEL=LOG [LABEL=LOG ...] --pair A B [--pair C D ...]
|
|
identity_compare.py --self-test
|
|
|
|
Each log is split into passes by its `LAPTOP request` lines (N prompts per pass). A pair `A B` compares pass 0 and pass 1 of the two runs line
|
|
by line; a pair `A A` is the floor inside one process (pass 1 against pass 0). Verdict per pair: IDENTICAL (same number of lines, every line equal) or the
|
|
first differing line with its index. Exit 0 only when every pair is identical and at least one evidence line was compared per category.
|
|
"""
|
|
import argparse
|
|
import sys
|
|
|
|
CATEGORIES = ("PREFILL-R", "logprobs window", "DRAFT", "LAPTOP output")
|
|
|
|
|
|
def category(line):
|
|
if line.startswith("PREFILL-R"):
|
|
return "PREFILL-R"
|
|
if line.startswith("DBG logprobs window"):
|
|
return "logprobs window"
|
|
if line.startswith("DRAFT "):
|
|
return "DRAFT"
|
|
if line.startswith("LAPTOP output"):
|
|
return "LAPTOP output"
|
|
return None
|
|
|
|
|
|
def is_row(line):
|
|
"""A top-k row of the line before it: tab separated, starts with the position."""
|
|
return "\t" in line and line[:1].isdigit()
|
|
|
|
|
|
def passes(text, prompts):
|
|
"""The evidence lines of each pass: [(category, line), ...] per pass index."""
|
|
out, requests, previous = {}, 0, None
|
|
for raw in text.splitlines():
|
|
line = raw.rstrip("\r\n")
|
|
if line.startswith("LAPTOP request"):
|
|
requests += 1
|
|
previous = None
|
|
continue
|
|
kind = category(line)
|
|
if kind is None and previous == "logprobs window" and is_row(line):
|
|
kind = "logprobs row"
|
|
if kind is None:
|
|
previous = None if not is_row(line) else previous
|
|
continue
|
|
previous = kind if kind in ("logprobs window", "logprobs row") else None
|
|
out.setdefault(requests // prompts, []).append((kind, line))
|
|
return out
|
|
|
|
|
|
def compare(a, b, label):
|
|
kinds = {}
|
|
for kind, _ in a:
|
|
kinds[kind] = kinds.get(kind, 0) + 1
|
|
if len(a) != len(b):
|
|
print(f"{label}: DIFFERENT, {len(a)} against {len(b)} evidence lines")
|
|
return False
|
|
for i, ((ka, la), (kb, lb)) in enumerate(zip(a, b)):
|
|
if la != lb:
|
|
print(f"{label}: DIFFERENT at evidence line {i} ({ka})\n A: {la[:300]}\n B: {lb[:300]}")
|
|
return False
|
|
missing = [c for c in ("PREFILL-R", "logprobs window", "logprobs row", "DRAFT") if kinds.get(c, 0) == 0]
|
|
if not a or missing:
|
|
print(f"{label}: NO EVIDENCE ({len(a)} lines, missing {missing or 'everything'}): not a pass")
|
|
return False
|
|
print(f"{label}: IDENTICAL, {len(a)} lines " + ", ".join(f"{k} {n}" for k, n in sorted(kinds.items())))
|
|
return True
|
|
|
|
|
|
def run(args):
|
|
logs = {}
|
|
for item in args.logs:
|
|
label, _, path = item.partition("=")
|
|
with open(path, encoding="utf-8", errors="replace") as handle:
|
|
logs[label] = passes(handle.read(), args.prompts)
|
|
ok = True
|
|
for first, second in args.pair:
|
|
for index in (0, 1):
|
|
if first == second:
|
|
if index == 1:
|
|
ok &= compare(logs[first].get(0, []), logs[first].get(1, []), f"{first} pass 1 against pass 0 (A/A floor)")
|
|
continue
|
|
ok &= compare(logs[first].get(index, []), logs[second].get(index, []), f"{second} against {first}, pass {index}")
|
|
print("VERDICT:", "IDENTICAL" if ok else "DIFFERENT or no evidence")
|
|
return 0 if ok else 1
|
|
|
|
|
|
def self_test():
|
|
def log(extra=""):
|
|
lines = []
|
|
for _ in range(2): # two passes of one prompt
|
|
lines += ["PREFILL-R first=0 rows=2048 fnv1a64=abc", "DBG logprobs window position=5 inputs=[1, 2] argmax_ids=[3]",
|
|
"5\t3\t-0.1\t3\t-0.1\t1", "DRAFT position=5 accepted_row=0 cell=9 ids=[4] pbits=[3f800000]" + extra,
|
|
"LAPTOP request 1: x"]
|
|
return "\n".join(lines)
|
|
same, other = passes(log(), 1), passes(log("1"), 1)
|
|
assert compare(same[0], same[1], "floor")
|
|
assert not compare(same[0], other[1], "different")
|
|
assert not compare([], [], "empty")
|
|
print("self-test ok")
|
|
return 0
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--prompts", type=int, default=1)
|
|
parser.add_argument("--pair", nargs=2, action="append", default=[])
|
|
parser.add_argument("--self-test", action="store_true")
|
|
parser.add_argument("logs", nargs="*")
|
|
args = parser.parse_args()
|
|
return self_test() if args.self_test else run(args)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|