generate.cpp stops on {248044, 248046}; the GGUF eos_token_id is only 248046, so the port ran on
past an <|endoftext|> (up to max_new, emitting <|im_start|> loops). The stop set is now the model's EOS
plus <|endoftext|> in the serve backend, the smokes and the laptop probe.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
109 lines
4.5 KiB
Python
109 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Layer-by-layer comparison of the port's and the Original's residual stream on one token path (docs/quality-gate.md).
|
|
|
|
usage: q_ladder.py LADDER_DIR TARGET_POS [CASE_IDS_FILE]
|
|
|
|
LADDER_DIR holds what job 9970 wrote: port.log (the port's `DBG residual window seq= first= rows= inputs=[..]` lines) and port/l_win*.f32,
|
|
orig/l_win*.f32 plus ../runs/ladder-orig/err.txt (the Original's `NUMRESID seq= pos0= T= tokens=` lines). A dump is
|
|
[96 frames][rows][4*2560] f32, frame = 2*layer + half. For every position both engines computed on the path, the relative L2 distance
|
|
|port - orig| / |orig| per frame is formed; the report lists, per frame, the value at TARGET_POS next to the median over all other positions.
|
|
"""
|
|
import array, math, os, re, statistics, sys
|
|
|
|
W = 4 * 2560
|
|
|
|
|
|
def rd(path):
|
|
a = array.array("f")
|
|
with open(path, "rb") as f:
|
|
a.frombytes(f.read())
|
|
return a
|
|
|
|
|
|
def windows_port(log):
|
|
out = []
|
|
for line in open(log, errors="replace"):
|
|
m = re.match(r"DBG residual window seq=(\d+) first=(\d+) rows=(\d+) inputs=\[([0-9, ]+)\]", line)
|
|
if m:
|
|
out.append((int(m[2]), [int(x) for x in m[4].split(",")], int(m[1])))
|
|
return out
|
|
|
|
|
|
def windows_orig(err):
|
|
out = []
|
|
for line in open(err, errors="replace"):
|
|
m = re.match(r"NUMRESID seq=(\d+) pos0=(\d+) T=(\d+) tokens=([0-9,]+)", line)
|
|
if m:
|
|
out.append((int(m[2]), [int(x) for x in m[4].split(",")], int(m[1])))
|
|
return out
|
|
|
|
|
|
def valid(wins, truth):
|
|
out = {}
|
|
for wi, (p0, toks, _) in enumerate(wins):
|
|
for k in range(len(toks)):
|
|
if p0 + k < len(truth) and toks[: k + 1] == truth[p0: p0 + k + 1]:
|
|
out.setdefault(p0 + k, (wi, k))
|
|
else:
|
|
break
|
|
return out
|
|
|
|
|
|
def main(argv):
|
|
d, target = argv[0], int(argv[1])
|
|
truth = [int(x) for x in open(argv[2] if len(argv) > 2 else os.path.join(d, "..", "cases", "B1.ids")).read().strip().split(",")]
|
|
pw = windows_port(os.path.join(d, "port.log"))
|
|
ow = windows_orig(os.path.join(d, "..", "runs", "ladder-orig", "err.txt"))
|
|
pv, ov = valid(pw, truth), valid(ow, truth)
|
|
common = sorted(set(pv) & set(ov))
|
|
print(f"port windows {len(pw)}, Original windows {len(ow)}, rows on the path: port {len(pv)} Original {len(ov)} common {len(common)}")
|
|
cache = {}
|
|
|
|
def frames(path):
|
|
if path not in cache:
|
|
cache[path] = rd(path)
|
|
return cache[path]
|
|
|
|
rel = {} # pos -> [96 relL2]
|
|
for pos in common:
|
|
(pwi, pk), (owi, ok) = pv[pos], ov[pos]
|
|
pl = os.path.join(d, "port", f"l_win{pw[pwi][2]:05d}_pos{pw[pwi][0]}.f32")
|
|
ol = os.path.join(d, "orig", f"l_win{ow[owi][2]:05d}_pos{ow[owi][0]}.f32")
|
|
if not (os.path.exists(pl) and os.path.exists(ol)):
|
|
continue
|
|
pT, oT = len(pw[pwi][1]), len(ow[owi][1])
|
|
a, b = frames(pl), frames(ol)
|
|
row = []
|
|
for fr in range(96):
|
|
x = a[(fr * pT + pk) * W: (fr * pT + pk + 1) * W]
|
|
y = b[(fr * oT + ok) * W: (fr * oT + ok + 1) * W]
|
|
num = sum((u - v) ** 2 for u, v in zip(x, y))
|
|
den = sum(v * v for v in y)
|
|
row.append(math.sqrt(num / den) if den else float("nan"))
|
|
rel[pos] = row
|
|
if len(cache) > 4:
|
|
cache.pop(next(iter(cache)))
|
|
if target not in rel:
|
|
print("target position not compared; positions:", sorted(rel)[:5], "...", sorted(rel)[-5:])
|
|
return
|
|
others = [p for p in rel if p != target]
|
|
print(f"positions compared {len(rel)} (target {target}, input token {truth[target]}); relL2 per frame (frame = 2*layer+half)")
|
|
print("frame layer.half target median(others) ratio rank among rows")
|
|
first = None
|
|
for fr in range(96):
|
|
med = statistics.median(rel[p][fr] for p in others)
|
|
t = rel[target][fr]
|
|
rank = 1 + sum(1 for p in others if rel[p][fr] > t)
|
|
ratio = t / med if med else float("nan")
|
|
if first is None and ratio > 3 and fr > 0:
|
|
first = fr
|
|
if fr < 8 or fr % 8 == 0 or fr == 95 or (first is not None and fr <= first + 2):
|
|
print(f"{fr:4d} {fr // 2:2d}.{fr % 2} {t:.3e} {med:.3e} {ratio:6.2f} {rank}/{len(rel)}")
|
|
print("first frame with target > 3x the median of the other rows:", first)
|
|
worst = sorted(((statistics.mean(rel[p][8:]), p) for p in rel), reverse=True)[:5]
|
|
print("rows with the largest mean relL2 over frames 8..95:", [(p, f"{v:.3f}") for v, p in worst])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv[1:])
|