rli14's picture
rsm_rise code snapshot (HF_PUSH_ALL) (part 11)
c6a8401 verified
Raw History Blame Contribute Delete
5.23 kB
"""Beam vs best-of-N paired table (light: reads JSON only). Usage: python3 scripts/beam/table.py [--vsmax]
run 6: all 7 paper tasks, proposer seeds (450-episode extension), 7-task mean rows, and --vsmax (quantile/LCB vs the
'max' beam of the same depth/beta_t, paired on the same episodes)."""
import json, glob, os, random, collections, re, sys
# task -> BoN chain cell at the paper beta (status/rebuttal_beta_table150.json); {es} eval seed, {ps} proposer seed
BASE = {
'square_sub_20100': 'runs/square_sub_20100/eval*/E1-Mb-R0.98-11-ch-N128-argmax-ONACTORP16lo-es{es}-sS0As{ps}.json',
'square_expert_play': 'runs/square_expert_play/eval_onactor/E1-L40b-R0.90-73-ch-N128-argmax-ONACTOR-es{es}-sS0As{ps}.json',
'two_piece_expert_suboptimal': 'runs/two_piece_expert_suboptimal/eval*/E1-Ms100k-R0.95-33-ch-N128-argmax-ONACTORP8pr-es{es}-sS0As{ps}.json',
'two_piece_expert_play': 'runs/two_piece_expert_play/eval_onactor/E1-L40b-R0.98-11-ch-N128-argmax-ONACTOR-es{es}-sS0As{ps}.json',
'mug_expert_suboptimal': 'runs/mug_expert_suboptimal/eval_onactor/E1-Ms1b-R0.98-11-ch-N128-argmax-ONS3H1P16lo-es{es}-sS3H1s{ps}.json',
'threading_expert_play': 'runs/threading_expert_play/eval*/E1-Mb-R0.98-11-ch-N128-argmax-ONS0AidqlE500P8pr-es{es}-sS0AidqlE500s{ps}.json',
'two_piece_expert_play_tipping': 'runs/two_piece_expert_play_tipping/eval_onactor/E1-Mb-R0.95-33-ch-N128-argmax-ONACTORF4-es{es}-sS0As{ps}.json'}
LABEL = {'square_sub_20100': 'Square (Sub.)', 'square_expert_play': 'Square (Cov.)', 'two_piece_expert_suboptimal': 'Piece (Sub.)',
'two_piece_expert_play': 'Piece (Cov.)', 'mug_expert_suboptimal': 'Mug (Sub.)', 'threading_expert_play': 'Thread (Cov.)',
'two_piece_expert_play_tipping': 'Piece-tip (Cov.)'}
PAPER = {'square_sub_20100': 54.0, 'square_expert_play': 20.9, 'two_piece_expert_suboptimal': 47.1, 'two_piece_expert_play': 48.7,
'mug_expert_suboptimal': 23.1, 'threading_expert_play': 25.8, 'two_piece_expert_play_tipping': 7.3}
VSMAX = '--vsmax' in sys.argv
def succ(f): return [bool(e['success']) for e in json.load(open(f))['episodes']][:50]
def boot(d, B=4000):
random.seed(0); n = len(d)
bs = sorted(sum(random.choice(d) for _ in range(n)) / n for _ in range(B))
return bs[int(.025 * B)], bs[int(.975 * B) - 1]
def tag_of(f): return os.path.basename(f).split('-')[3]
def ps_of(f): return int(re.search(r's(\d)\.json$', f).group(1))
def es_of(f): return int(re.search(r'-es(\d)-', f).group(1))
def maxtag(t): return re.sub(r's(rm|q\d+|l\d+)$', '', t)
rows, per_task = [], collections.defaultdict(dict) # per_task[tag][task] = (beam list, ref list) on s0 x es0-2
for task, pat in BASE.items():
fs = sorted(glob.glob(f'runs/{task}/eval_beam/E1B-*.json'))
for tag in sorted({tag_of(f) for f in fs}):
if VSMAX and maxtag(tag) == tag: continue
b, o, b0, o0, tp, ts, rk, nf = [], [], [], [], 0, 0, collections.Counter(), 0
for fb in [f for f in fs if tag_of(f) == tag]:
es, ps = es_of(fb), ps_of(fb)
if VSMAX:
g = [fb.replace(f'-{tag}-', f'-{maxtag(tag)}-')]
else:
g = glob.glob(pat.format(es=es, ps=ps))
if not g or not os.path.exists(g[0]): continue
sb, so = succ(fb), succ(g[0]); n = min(len(sb), len(so)); b += sb[:n]; o += so[:n]; nf += 1
if ps == 0 and es in (0, 1, 2): b0 += sb[:n]; o0 += so[:n]
pt = json.load(open(fb))['planner_time']; tp += pt['t_sample_s'] + pt['t_score_s']; ts += pt['steps_total']
if os.path.exists(fb + '.beamlog.jsonl'):
for l in open(fb + '.beamlog.jsonl'):
for bb in json.loads(l).get('beam', []): rk[min(bb['rank_l1'], 9)] += 1
if not b: continue
if len(b0) == 150: per_task[tag][task] = (b0, o0)
n = len(b); d = [x - y for x, y in zip(b, o)]; lo, hi = boot(d); tot = sum(rk.values())
rows.append(f"| {LABEL[task]} | {tag} | {n} | {100*sum(b)/n:.1f} | {100*sum(o)/n:.1f} | {100*sum(d)/n:+.1f} [{100*lo:+.1f},{100*hi:+.1f}] | "
f"{tp/max(ts,1):.3f} | {100*rk[0]/max(tot,1):.0f}% |")
ref = "max beam (same d, beta_t)" if VSMAX else "BoN"
print(f"| task | beam cfg | eps | beam % | {ref} % | diff [95% paired boot] | plan s/decision | beam keeps BoN's root |")
print("|---|---|---|---|---|---|---|---|"); print("\n".join(rows))
# task-mean rows (s0 x es0-2, 150 eps per task) for configs present on several tasks; bootstrap resamples within task
print(f"\n| beam cfg | tasks | mean beam % | mean {ref} % | mean diff [95% boot] | paper Table 1 RSM mean (same tasks) |")
print("|---|---|---|---|---|---|")
for tag, T in sorted(per_task.items()):
if len(T) < 2: continue
ks = sorted(T); mb = sum(sum(T[k][0]) / 150 for k in ks) / len(ks); mo = sum(sum(T[k][1]) / 150 for k in ks) / len(ks)
random.seed(1); ds = {k: [x - y for x, y in zip(*T[k])] for k in ks}; bs = []
for _ in range(2000):
bs.append(sum(sum(random.choice(ds[k]) for _ in range(150)) / 150 for k in ks) / len(ks))
bs.sort(); pm = sum(PAPER[k] for k in ks) / len(ks)
print(f"| {tag} | {len(ks)} | {100*mb:.1f} | {100*mo:.1f} | {100*(mb-mo):+.1f} [{100*bs[50]:+.1f},{100*bs[1949]:+.1f}] | {pm:.1f} |")