# Builds every train / test file once, so all models see exactly the same rows. import glob, importlib.util, json, os, random, shutil, subprocess, sys, time MODE = os.environ.get("MODE", "full") ROWS = int(os.environ.get("ROWS", 12000 if MODE == "full" else 300)) SYNTH_REPEATS = int(os.environ.get("SYNTH_REPEATS", 3)) TYPED_REPEATS = int(os.environ.get("TYPED_REPEATS", 1)) EVAL_ROWS = 500 SEED = 3407 OUT = "/kaggle/working/data" os.makedirs(OUT, exist_ok = True) # Unsloth's dataset helpers are plain python: load the file directly so no GPU init happens here. unsloth_dir = importlib.util.find_spec("unsloth").submodule_search_locations[0] spec = importlib.util.spec_from_file_location("dd", f"{unsloth_dir}/models/decision_datasets.py") dd = importlib.util.module_from_spec(spec); spec.loader.exec_module(dd) # 1) The user's synthetic set: generator scripts in the claudeset dataset -> jsonl. cands = glob.glob("/kaggle/input/**/build.py", recursive = True) assert cands, "claudeset build.py not found under /kaggle/input" src = os.path.dirname(cands[0]) synth_dir = "/kaggle/working/synth" shutil.rmtree(synth_dir, ignore_errors = True) shutil.copytree(src, synth_dir) r = subprocess.run([sys.executable, "build.py", f"{unsloth_dir}/models/_decision_common.py"], cwd = synth_dir, capture_output = True, text = True) print(r.stdout[-3000:], r.stderr[-3000:]) if r.returncode != 0: print("build.py with Unsloth validation failed, retrying without it") r = subprocess.run([sys.executable, "build.py"], cwd = synth_dir, capture_output = True, text = True) print(r.stdout[-3000:], r.stderr[-3000:]) assert r.returncode == 0, "synthetic build failed" synth_train = [json.loads(l) for l in open(f"{synth_dir}/synth_decisions_train.jsonl")] synth_test = [json.loads(l) for l in open(f"{synth_dir}/synth_decisions_test.jsonl")] # 2) Test sets first, so the training data can be decontaminated against them. evals = {"typed_decisions": dd.load_source("typed_decisions", "test")} for name in ("banking77", "clinc150"): evals[name] = [dd.canonical_row(r) for r in dd.load_source(name, "test", limit = EVAL_ROWS, seed = 0)] evals["synthetic"] = synth_test eval_texts = [r["state"] for rows in evals.values() for r in rows] # 3) Public mixture (gated sources such as xlam are skipped if they fail). SOURCES = ["banking77", "clinc150", "mnli", "snli", "wanli", "boolq", "ag_news", "sst5", "mmlu", "commonsense_qa", "arc", "prompt_injections", "xlam"] ok = [] for name in SOURCES: try: dd.load_source(name, "train", limit = 1, seed = SEED); ok.append(name) except Exception as e: print(f"skipping {name}: {type(e).__name__}: {str(e)[:150]}") per_source = -(-ROWS // max(1, len(ok))) pools = {name: dd.load_source(name, "train", limit = 2 * per_source, seed = SEED) for name in ok} public = dd.build_decision_mixture(pools, n_rows = ROWS, seed = SEED, decontaminate_against = eval_texts) rng = random.Random(SEED) cleaner = dd.Decontaminator(eval_texts) typed_all = dd.load_source("typed_decisions", "train") # typed-decisions train/test share templated boilerplate, so any-13-gram overlap drops ~70% of its # official train split. Use the official split, minus real leaks: exact test states, rows whose # 13-grams are mostly (>= 50%) found in the test set, and any overlap with the other test sets. other_cleaner = dd.Decontaminator([r["state"] for k, rows in evals.items() if k != "typed_decisions" for r in rows]) typed_test_sh = set() for r in evals["typed_decisions"]: typed_test_sh |= cleaner._shingles(r["state"]) typed_test_exact = {json.dumps(r["state"], sort_keys = True) if not isinstance(r["state"], str) else r["state"] for r in evals["typed_decisions"]} typed_clean, why = [], {"exact": 0, "mostly_overlap": 0, "other_evals": 0, "any_overlap_old_rule": 0} for r in typed_all: key = json.dumps(r["state"], sort_keys = True) if not isinstance(r["state"], str) else r["state"] sh = cleaner._shingles(r["state"]) frac = len(sh & typed_test_sh) / max(1, len(sh)) if cleaner.contaminated(r): why["any_overlap_old_rule"] += 1 if key in typed_test_exact: why["exact"] += 1; continue if frac >= 0.5: why["mostly_overlap"] += 1; continue if other_cleaner.contaminated(r): why["other_evals"] += 1; continue typed_clean.append(r) print("typed-decisions train decontamination:", why, "kept", len(typed_clean), "of", len(typed_all)) # typed-decisions is the headline benchmark: several differently augmented copies of its train split. typed = [dd.augment_row(r, rng) for _ in range(TYPED_REPEATS) for r in typed_clean] synth_clean = [r for r in synth_train if not cleaner.contaminated(r)] # Synthetic rows: once as written, then augmented copies (renamed ids/options, paraphrases). synth_rows = [{k: r[k] for k in ("source", "state", "questions", "gold")} for r in synth_clean] synth_rows += [dd.augment_row(r, rng) for r in synth_clean for _ in range(max(0, SYNTH_REPEATS - 1))] if MODE != "full": typed = typed[:150] train = public + typed + synth_rows random.Random(SEED).shuffle(train) def dump(path, rows): with open(path, "w", encoding = "utf-8") as f: for r in rows: f.write(json.dumps({k: v for k, v in r.items() if k != "_variants"}, ensure_ascii = False, default = str) + "\n") dump(f"{OUT}/train.jsonl", train) for name, rows in evals.items(): if MODE != "full": rows = rows[:40] dump(f"{OUT}/eval_{name}.jsonl", rows) from collections import Counter summary = {"mode": MODE, "public": len(public), "typed": len(typed), "typed_unique": len(typed_clean), "typed_decontam": why, "typed_dropped_contaminated": len(typed_all) - len(typed_clean), "synthetic": len(synth_rows), "synth_train_unique": len(synth_clean), "train_total": len(train), "evals": {k: len(v) for k, v in evals.items()}, "sources": dict(Counter(r.get("source") for r in train))} json.dump(summary, open(f"{OUT}/summary.json", "w"), indent = 1) print(json.dumps(summary, indent = 1))