| """Assemble model-4's stage-2 tables into labelled train / unlabelled test frames. |
| |
| WHY THIS EXISTS. model-4 already scored every test candidate and left the result on disk: |
| `stage2/test_scores.parquet`, 9,980,755 rows of (q, c, p1, p, ce) - the table that produced |
| leaderboard 0.979241. The train side is there too, with labels. So the whole feature-and- |
| decision track can be run on existing scores, with no retrieval and no cross-encoder pass. |
| That is the difference between a submission in an hour and a submission in six. |
| |
| THE SPLIT IS CLEAN, WHICH MATTERS MORE THAN THE SPEED. `s2/train.parquet` covers 296,148 S1: |
| 197,435 from g_stack and 98,713 from g_val, and **zero** from g_ce. So fitting on g_stack and |
| reading g_val is an honest held-out measurement against a cross-encoder that never saw either. |
| Every earlier number in this project came from a 20k cache drawn with `RandomState(42)` where |
| the split used `default_rng(42)` - not the val split at all, and about 7% seen in training. |
| This replaces that. |
| |
| ALIGNMENT. `ce_scores/<split>/shard_*.npy` concatenate in shard order to exactly the row count |
| of the matching `s2/<split>.parquet`. That is checked, not assumed; a silent off-by-one shard |
| would attach every cross-encoder score to the wrong pair and still produce a plausible file. |
| """ |
| from __future__ import annotations |
|
|
| import argparse |
| import glob |
| import os |
| import sys |
|
|
| import numpy as np |
| import pandas as pd |
|
|
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| from berx import paths as P |
|
|
| M4 = os.environ.get("BERX_M4", "/scratch/user4/dai/model-4/work") |
|
|
|
|
| def load_ce(split: str, n_expect: int) -> np.ndarray: |
| fs = sorted(glob.glob(os.path.join(M4, "ce_scores", split, "shard_*.npy"))) |
| assert fs, f"no ce score shards under {M4}/ce_scores/{split}" |
| parts = [np.load(f) for f in fs] |
| ce = np.concatenate(parts) |
| assert len(ce) == n_expect, ( |
| f"ce_scores/{split} has {len(ce):,} scores but s2/{split}.parquet has {n_expect:,} " |
| f"rows. They are positionally aligned, so this mismatch means a shard is missing or " |
| f"out of order - every score would attach to the wrong pair.") |
| print(f" ce {split}: {len(fs)} shards, {len(ce):,} scores") |
| return ce.astype(np.float32) |
|
|
|
|
| def main(): |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--split", default="both", choices=["train", "test", "both"]) |
| a = ap.parse_args() |
|
|
| splits = ["train", "test"] if a.split == "both" else [a.split] |
| sp = np.load(os.path.join(M4, "splits.npz")) |
|
|
| for split in splits: |
| f = os.path.join(M4, "s2", f"{split}.parquet") |
| df = pd.read_parquet(f) |
| print(f"\n{f}: {len(df):,} rows, cols {list(df.columns)}") |
| df["ce"] = load_ce(split, len(df)) |
|
|
| if split == "train": |
| g_stack, g_val, g_ce = set(sp["g_stack"].tolist()), set(sp["g_val"].tolist()), \ |
| set(sp["g_ce"].tolist()) |
| q = df["q"].to_numpy() |
| grp = np.where(np.isin(q, list(g_val)), "val", |
| np.where(np.isin(q, list(g_stack)), "stack", "other")) |
| df["grp"] = grp |
| leak = int(np.isin(q, list(g_ce)).sum()) |
| assert leak == 0, ( |
| f"{leak:,} rows belong to g_ce entities. The cross-encoder trained on those, " |
| f"so any gain measured with them is partly leakage.") |
| print(f" groups: {pd.Series(grp).value_counts().to_dict()}") |
| print(f" label rate {df['y'].mean():.4%} g_ce leakage: none") |
| |
| |
| df.attrs["n_true"] = None |
|
|
| out = P.work("m4", f"{split}.parquet") |
| df.to_parquet(out, index=False, compression="zstd") |
| print(f" wrote {out} ({os.path.getsize(out)/2**20:.0f} MB)") |
|
|
| |
| |
| if "train" in splits: |
| gtq, gtc = sp["gt_q"], sp["gt_c"] |
| np.savez_compressed(P.work("m4", "truth.npz"), gt_q=gtq, gt_c=gtc, |
| g_stack=sp["g_stack"], g_val=sp["g_val"]) |
| print(f"\n wrote truth.npz {len(gtq):,} ground-truth pairs") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|