Files

77 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Row-by-row comparison of two port smoke logs of the SAME teacher-forced corpus (scripts/quality/q-common.sh port_run).
The question it answers is the one of a change that must not alter normal output: are the top-16
log-probability rows of build B equal to those of build A, bit for bit? A row is keyed by (position, the window's inputs up to the row),
so two runs that cut their windows differently still meet on the rows whose context is the same. For rows that are not identical it
prints the deviation of the reference's top-1 (d_top1), the argmax flips and the largest differences, so the same log pair can also be
read with the yardsticks of q_tail_gate.py (p99 / max / flip rate against the run-to-run control of two runs of build A).
usage: q_pair.py LOG_A LOG_B [--pass N] [--control LOG_A2] exit 0 if every common forced row is identical
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import q_lib
def rows_of(log, wanted_pass):
out = {}
for run in q_lib.parse_port_log(log):
if not run["forced"] or run["pass"] != wanted_pass:
continue
for position, inputs, rows in run["windows"]:
for r, row in enumerate(rows):
out.setdefault((run["index"], position + r, tuple(inputs[: r + 1])), row)
return out
def render(row):
return ' '.join(f'{t}:{lp!r}' for t, lp in row.topk)
def compare(a, b, label):
common = sorted(set(a) & set(b))
same = [k for k in common if render(a[k]) == render(b[k])]
diff = [k for k in common if k not in set(same)]
flips = [k for k in diff if a[k].top1 != b[k].top1]
d1 = sorted((abs(a[k].topk[0][1] - (b[k].lp(a[k].top1) if b[k].lp(a[k].top1) is not None else float('nan'))) for k in diff), reverse=True)
d1 = [x for x in d1 if x == x]
print(f'{label}: rows A {len(a)}, rows B {len(b)}, common {len(common)}: identical {len(same)}, different {len(diff)}, argmax flips {len(flips)}')
if diff:
print(f' d_top1 of the different rows: max {d1[0] if d1 else float("nan"):.4f}, p99 {q_lib.quantile(d1, .99):.4f}, median {q_lib.quantile(d1, .5):.4f}')
for k in diff[:5]:
print(f' differs: request {k[0]} position {k[1]}: A {render(a[k])[:90]} | B {render(b[k])[:90]}')
return len(common), len(diff), len(flips), d1
def main():
args = sys.argv[1:]
wanted, control = 0, None
if '--pass' in args:
i = args.index('--pass')
wanted = int(args[i + 1])
del args[i:i + 2]
if '--control' in args:
i = args.index('--control')
control = args[i + 1]
del args[i:i + 2]
if len(args) != 2:
print(__doc__)
sys.exit(2)
a, b = rows_of(args[0], wanted), rows_of(args[1], wanted)
if not a or not b:
print('no forced rows in a log (was the run teacher-forced? LELEKA_SMOKE_FORCE_DIR, LELEKA_TRACE_LOGPROBS=16)')
sys.exit(2)
n, bad, flips, _ = compare(a, b, 'B against A')
if control:
compare(a, rows_of(control, wanted), 'run-to-run control (A2 against A)')
ok = n > 0 and bad == 0
print('VERDICT:', 'IDENTICAL' if ok else 'DIFFERENT (read it against the control)')
sys.exit(0 if ok else 1)
if __name__ == '__main__':
main()