# -*- coding: utf-8 -*- """P10d: Phase 9 の 2.0558 プランの正体と、マッピング修正後の再現性 (dense 実 forward). Phase 9 の 2.0558 は p9_plan.npz plan_0875 (off-by-one 修正前の npz) の実測。 このプランは異種 (8 反復 70% / 5 反復 24% / 3–4 反復 4%) で、定数 ghost 尾部 (≡ uniform7, 2.1251) とは別物。ここでは A: そのプランそのまま ghost → 2.0558 が再現するか B: マッピング修正相当 (p → min(p+1, T-1)) ghost → 修正後のプランナでも利得が残るか C: A の素 skip (ghost 無し) → この異種プランで ghost が何 nat 効いているか を dense b_s1 (5k step) で実測する。 """ import argparse, json, os, sys, time import numpy as np import torch HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) os.chdir(HERE) import exp_v34_data as E from p10_ghost_moe import build_dense, gate_from_plan @torch.no_grad() def eval_plan(m, stream, L, name, plan_all, ghost, T=8): tot_ce, tot_tok, tot_it, nwin = 0.0, 0, 0, 0 t0 = time.time() for wi, (x, y) in enumerate(stream.eval_batches(1)): x = x.to(E.DEV); y = y.to(E.DEV) pl = plan_all[wi * L:(wi + 1) * L] g = gate_from_plan(pl, L, T).to(E.DEV) m.core.pos_iters = 0 o = m(x, targets=y, chunk_gate=g, ghost=ghost) tot_ce += float(o['final_ce']) * y.numel() tot_tok += y.numel(); tot_it += m.core.pos_iters; nwin += 1 if nwin % 150 == 0: print(f'[{name}] {nwin} win CE={tot_ce/tot_tok:.4f} ' f'({time.time()-t0:.0f}s)', flush=True) res = {'ce': tot_ce / tot_tok, 'pos_ratio': tot_it / tot_tok, 'win': nwin, 'sec': time.time() - t0} print(f'[{name}] CE={res["ce"]:.4f} pos={res["pos_ratio"]:.3f}', flush=True) return res def main(): p = argparse.ArgumentParser() p.add_argument('--dense-ckpt', default='ckpt/b_s1.pt') p.add_argument('--plan-npz', default='results_p9/p9_plan.npz') p.add_argument('--plan-key', default='plan_0875') p.add_argument('--L', type=int, default=512) p.add_argument('--out', default='results/p10d_dense_planmap.json') a = p.parse_args() z = np.load(a.plan_npz) pl = z[a.plan_key].astype(np.int64) pl_shift = np.minimum(pl + 1, 7) md = build_dense(a.dense_ckpt) stream = E.get_eval_stream('data/tokens_valid.bin', a.L) res = {} res['plan_orig_ghost'] = eval_plan(md, stream, a.L, 'orig_ghost', pl, True) res['plan_orig_skip'] = eval_plan(md, stream, a.L, 'orig_skip', pl, False) res['plan_shift_ghost'] = eval_plan(md, stream, a.L, 'shift_ghost', pl_shift, True) res['hist'] = {'orig': np.bincount(pl + 1, minlength=9).tolist(), 'shift': np.bincount(pl_shift + 1, minlength=9).tolist()} os.makedirs(os.path.dirname(a.out), exist_ok=True) with open(a.out, 'w') as f: json.dump(res, f, indent=1) print(f'[p10d] wrote {a.out}', flush=True) if __name__ == '__main__': main()