Skip to content
nano-gpt-data-curation

claude-opus-5

nano-gpt-data-curation — graded by the task's verifier against a measured baseline.

backend
modal
variant
claude claude-opus-5
metric
held-out multi-domain perplexity
actions
61
duration
53m
recorded
2026-08-04

run iddclm-nano-curation__MDUDMmY

Everything claude-opus-5 did on this task, in order.

download JSON ↗
claude-codeclaude-opus-559 turns30 narration61 action61 observation29 reasoning1 final
#1
narration

I'll start by getting oriented — checking the timer, the data, and the training script.

toolBashCheck timer, files, GPU
args
{
  "command": "cat /workspace/.timer/remaining_secs; echo \"---\"; ls -la /workspace/; echo \"---\"; ls -la /workspace/data/; echo \"---\"; nvidia-smi | head -20",
  "description": "Check timer, files, GPU"
}
Bash result
5390
---
total 14
drwxr-xr-x 1 root root  100 Aug  4 01:10 .
drwxr-xr-x 1 root root  160 Aug  4 01:10 ..
drwxr-xr-x 1 root root   60 Aug  4 01:10 .live
drwxr-xr-x 1 root root  100 Aug  4 01:10 .metrics
drwxr-xr-x 1 root root  140 Aug  4 01:09 .timer
drwxr-xr-x 1 root root   57 Aug  3 22:37 data
-rwxrwxr-x 1 root root  714 Aug  3 22:34 dev_eval.sh
-rw-rw-r-- 1 root root 5706 Aug  3 22:34 model.py
-rw-rw-r-- 1 root root  869 Aug  3 22:34 pack_selection.py
drwxr-xr-x 1 root root   10 Aug  3 22:37 submission
-rwxrwxr-x 1 root root 1509 Aug  3 22:34 timer.sh
-rw-rw-r-- 1 root root 2962 Aug  3 22:34 train_nano.py
---
total 792657
drwxr-xr-x 1 root root        57 Aug  3 22:37 .
drwxr-xr-x 1 root root       100 Aug  4 01:10 ..
-r--r--r-- 1 root root   2000128 Aug  3 22:34 multi_dev.npy
-r--r--r-- 1 root root 809679384 Aug  3 22:35 pool.jsonl
---
Tue Aug  4 01:10:10 2026       
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 580.95.05              Driver Version: 580.95.05      CUDA Version: 13.0     |
+-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA H100 80GB HBM3          On  |   00000000:00:10.0 Off |                    0 |
| N/A   23C    P0             69W /  700W |       4MiB /  81559MiB |      0%      Default |
|                                         |                        |             Disabled |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI              PID   Type   Process name                        GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|  No running processes found                                                             |
+-----------------------------------------------------------------------------------------+

[stdout]
5390
---
total 14
drwxr-xr-x 1 root root  100 Aug  4 01:10 .
drwxr-xr-x 1 root root  160 Aug  4 01:10 ..
drwxr-xr-x 1 root root   60 Aug  4 01:10 .live
drwxr-xr-x 1 root root  100 Aug  4 01:10 .metrics
drwxr-xr-x 1 root root  140 Aug  4 01:09 .timer
drwxr-xr-x 1 root root   57 Aug  3 22:37 data
-rwxrwxr-x 1 root root  714 Aug  3 22:34 dev_eval.sh
-rw-rw-r-- 1 root root 5706 Aug  3 22:34 model.py
-rw-rw-r-- 1 root root  869 Aug  3 22:34 pack_selection.py
drwxr-xr-x 1 root root   10 Aug  3 22:37 submission
-rwxrwxr-x 1 root root 1509 Aug  3 22:34 timer.sh
-rw-rw-r-- 1 root root 2962 Aug  3 22:34 train_nano.py
---
total 792657
drwxr-xr-x 1 root root        57 Aug  3 22:37 .
drwxr-xr-x 1 root root       100 Aug  4 01:10 ..
-r--r--r-- 1 root root   2000128 Aug  3 22:34 multi_dev.npy
-r--r--r-- 1 root root 809679384 Aug  3 22:35 pool.jsonl
---
Tue Aug  4 01:10:10 2026       
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 580.95.05              Driver Version: 580.95.05      CUDA Version: 13.0     |
+-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA H100 80GB HBM3          On  |   00000000:00:10.0 Off |                    0 |
| N/A   23C    P0             69W /  700W |       4MiB /  81559MiB |      0%      Default |
|                                         |                        |             Disabled |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI              PID   Type   Process name                        GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|  No running processes found                                                             |
+-----------------------------------------------------------------------------------------+
[metadata] {"noOutputExpected": false}
toolRead
args
{
  "file_path": "/workspace/train_nano.py"
}
Read result
1	"""From-scratch nano-GPT (30M) LM training on a 1D token .npy; report held-out perplexity.
2	Frozen recipe — the ONLY thing that varies between runs is the training token set."""
3	import argparse, math, time, numpy as np, torch
4	from model import GPT, GPTConfig
5	
6	ap = argparse.ArgumentParser()
7	ap.add_argument("--train_npy", required=True)
8	ap.add_argument("--heldout_npy", required=True)
9	ap.add_argument("--out_json", required=True)
10	ap.add_argument("--max_iters", type=int, default=3000)
11	ap.add_argument("--warmup", type=int, default=150)
12	ap.add_argument("--batch", type=int, default=32)
13	ap.add_argument("--block", type=int, default=256)
14	ap.add_argument("--lr", type=float, default=6e-4)
15	ap.add_argument("--seed", type=int, default=1337)
16	a = ap.parse_args()
17	
18	torch.manual_seed(a.seed); np.random.seed(a.seed)
19	dev = "cuda"
20	tr = np.load(a.train_npy); ho = np.load(a.heldout_npy)
21	tr = torch.from_numpy(tr.astype(np.int64)); ho = torch.from_numpy(ho.astype(np.int64))
22	rng = np.random.default_rng(a.seed)
23	
24	model = GPT(GPTConfig(block_size=a.block, vocab_size=50257, n_layer=6, n_head=6,
25	                      n_embd=384, dropout=0.0, bias=False)).to(dev)
26	opt = model.configure_optimizers(0.1, a.lr, (0.9, 0.95), "cuda")
27	
28	def lr_at(it):
29	    if it < a.warmup: return a.lr * (it + 1) / (a.warmup + 1)
30	    r = (it - a.warmup) / max(1, a.max_iters - a.warmup)
31	    return 0.1 * a.lr + 0.5 * (1 + math.cos(math.pi * r)) * (a.lr - 0.1 * a.lr)
32	
33	def get_batch(src):
34	    ix = rng.integers(0, len(src) - a.block - 1, size=a.batch)
35	    x = torch.stack([src[i:i+a.block] for i in ix]).to(dev)
36	    y = torch.stack([src[i+1:i+1+a.block] for i in ix]).to(dev)
37	    return x, y
38	
39	t0 = time.time()
40	model.train()
41	for it in range(a.max_iters):
42	    for g in opt.param_groups: g["lr"] = lr_at(it)
43	    x, y = get_batch(tr)
44	    with torch.autocast("cuda", dtype=torch.bfloat16):
45	        _, loss = model(x, y)
46	    opt.zero_grad(set_to_none=True); loss.backward()
47	    torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0); opt.step()
48	train_s = time.time() - t0
49	
50	# held-out perplexity: mean next-token CE over non-overlapping windows (window-weighted)
51	import json
52	model.eval()
53	nwin = (len(ho) - 1) // a.block
54	starts = [j * a.block for j in range(nwin)]
55	sum_loss, nseen = 0.0, 0
56	with torch.no_grad():
57	    for i in range(0, nwin, a.batch):
58	        bs = starts[i:i+a.batch]
59	        x = torch.stack([ho[s:s+a.block] for s in bs]).to(dev)
60	        y = torch.stack([ho[s+1:s+1+a.block] for s in bs]).to(dev)
61	        with torch.autocast("cuda", dtype=torch.bfloat16):
62	            _, l = model(x, y)
63	        sum_loss += l.item() * len(bs); nseen += len(bs)
64	mean_loss = sum_loss / max(1, nseen)
65	ppl = math.exp(mean_loss)
66	out = {"train_npy": a.train_npy, "heldout_ppl": ppl, "mean_loss": mean_loss,
67	       "max_iters": a.max_iters, "seed": a.seed, "train_s": round(train_s, 1)}
68	json.dump(out, open(a.out_json, "w"))
69	print(f"HELDOUT_PPL {ppl:.3f}  loss {mean_loss:.4f}  train_s {train_s:.0f}  {a.train_npy}")
70

[metadata] {"type": "text", "file": {"filePath": "/workspace/train_nano.py", "content": "\"\"\"From-scratch nano-GPT (30M) LM training on a 1D token .npy; report held-out perplexity.\nFrozen recipe — the ONLY thing that varies between runs is the training token set.\"\"\"\nimport argparse, math, time, numpy as np, torch\nfrom model import GPT, GPTConfig\n\nap = argparse.ArgumentParser()\nap.add_argument(\"--train_npy\", required=True)\nap.add_argument(\"--heldout_npy\", required=True)\nap.add_argument(\"--out_json\", required=True)\nap.add_argument(\"--max_iters\", type=int, default=3000)\nap.add_argument(\"--warmup\", type=int, default=150)\nap.add_argument(\"--batch\", type=int, default=32)\nap.add_argument(\"--block\", type=int, default=256)\nap.add_argument(\"--lr\", type=float, default=6e-4)\nap.add_argument(\"--seed\", type=int, default=1337)\na = ap.parse_args()\n\ntorch.manual_seed(a.seed); np.random.seed(a.seed)\ndev = \"cuda\"\ntr = np.load(a.train_npy); ho = np.load(a.heldout_npy)\ntr = torch.from_numpy(tr.astype(np.int64)); ho = torch.from_numpy(ho.astype(np.int64))\nrng = np.random.default_rng(a.seed)\n\nmodel = GPT(GPTConfig(block_size=a.block, vocab_size=50257, n_layer=6, n_head=6,\n                      n_embd=384, dropout=0.0, bias=False)).to(dev)\nopt = model.configure_optimizers(0.1, a.lr, (0.9, 0.95), \"cuda\")\n\ndef lr_at(it):\n    if it < a.warmup: return a.lr * (it + 1) / (a.warmup + 1)\n    r = (it - a.warmup) / max(1, a.max_iters - a.warmup)\n    return 0.1 * a.lr + 0.5 * (1 + math.cos(math.pi * r)) * (a.lr - 0.1 * a.lr)\n\ndef get_batch(src):\n    ix = rng.integers(0, len(src) - a.block - 1, size=a.batch)\n    x = torch.stack([src[i:i+a.block] for i in ix]).to(dev)\n    y = torch.stack([src[i+1:i+1+a.block] for i in ix]).to(dev)\n    return x, y\n\nt0 = time.time()\nmodel.train()\nfor it in range(a.max_iters):\n    for g in opt.param_groups: g[\"lr\"] = lr_at(it)\n    x, y = get_batch(tr)\n    with torch.autocast(\"cuda\", dtype=torch.bfloat16):\n        _, loss = model(x, y)\n    opt.zero_grad(set_to_none=True); loss.backward()\n    torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0); opt.step()\ntrain_s = time.time() - t0\n\n# held-out perplexity: mean next-token CE over non-overlapping windows (window-weighted)\nimport json\nmodel.eval()\nnwin = (len(ho) - 1) // a.block\nstarts = [j * a.block for j in range(nwin)]\nsum_loss, nseen = 0.0, 0\nwith torch.no_grad():\n    for i in range(0, nwin, a.batch):\n        bs = starts[i:i+a.batch]\n        x = torch.stack([ho[s:s+a.block] for s in bs]).to(dev)\n        y = torch.stack([ho[s+1:s+1+a.block] for s in bs]).to(dev)\n        with torch.autocast(\"cuda\", dtype=torch.bfloat16):\n            _, l = model(x, y)\n        sum_loss += l.item() * len(bs); nseen += len(bs)\nmean_loss = sum_loss / max(1, nseen)\nppl = math.exp(mean_loss)\nout = {\"train_npy\": a.train_npy, \"heldout_ppl\": ppl, \"mean_loss\": mean_loss,\n       \"max_iters\": a.max_iters, \"seed\": a.seed, \"train_s\": round(train_s, 1)}\njson.dump(out, open(a.out_json, \"w\"))\nprint(f\"HELDOUT_PPL {ppl:.3f}  loss {mean_loss:.4f}  train_s {train_s:.0f}  {a.train_npy}\")\n", "numLines": 70, "startLine": 1, "totalLines": 70}}
#2
narration

~90 minutes. Let me move fast.

toolBashInspect helper scripts and dev target
args
{
  "command": "cat /workspace/pack_selection.py; echo \"=== dev_eval\"; cat /workspace/dev_eval.sh; echo \"=== head\"; head -c 1200 /workspace/data/pool.jsonl; echo; echo \"=== py\"; python -c \"\nimport numpy as np\nd=np.load('/workspace/data/multi_dev.npy'); print(d.dtype, d.shape, d[:20])\n\"",
  "description": "Inspect helper scripts and dev target"
}
Bash result
"""Solver-side: pack a selection.json (ordered ids) into a 12M-token train array from pool.jsonl."""
import json, sys, numpy as np
from transformers import AutoTokenizer

POOL = "/workspace/data/pool.jsonl"
SEL = sys.argv[1] if len(sys.argv) > 1 else "/workspace/submission/selection.json"
OUT = sys.argv[2] if len(sys.argv) > 2 else "/tmp/dev_train.npy"
BUDGET = 12_000_000

tok = AutoTokenizer.from_pretrained("gpt2"); EOS = tok.eos_token_id
text = {}
for line in open(POOL):
    r = json.loads(line); text[r["id"]] = r["text"]
sel = json.load(open(SEL))
parts, tot = [], 0
for i in sel:
    ids = tok(text[i], add_special_tokens=False).input_ids
    parts.extend(ids); parts.append(EOS); tot += len(ids) + 1
    if tot >= BUDGET: break
arr = np.array(parts[:BUDGET], dtype=np.uint16)
np.save(OUT, arr)
print(f"packed {len(arr)} tokens from {len(sel)} ids -> {OUT}")
=== dev_eval
#!/bin/bash
# Diagnostic: train the frozen 30M GPT on your selection and report DEV perplexity.
# Usage: bash dev_eval.sh [selection.json]   (default /workspace/submission/selection.json)
# NOTE: the official target is a DIFFERENT (hidden) sample from the same HQ domain.
set -euo pipefail
export TOKENIZERS_PARALLELISM=false
SEL="${1:-/workspace/submission/selection.json}"
cd /workspace
python3 pack_selection.py "$SEL" /tmp/dev_train.npy
python3 train_nano.py --train_npy /tmp/dev_train.npy --heldout_npy /workspace/data/multi_dev.npy \
    --out_json /tmp/dev.json --max_iters 3000 --seed 1337 2>&1 | grep HELDOUT_PPL
echo "(dev PPL on multi_dev; official uses a hidden disjoint sample of the same HQ domain)"
=== head
{"id": 0, "text": "|Viewing Single Post From: Spoilers for the Week of February 11th|\n|Lil||Feb 1 2013, 09:58 AM|\nDon't care about Chloe/Taniel/Jen-Jen. Don't care about Sami, really, but hoping that we get some good \"SAMANTHA GENE!!\" Marlena Death-Stares out of it. And \"newfound\" feelings. Please. If only.\nSTEFANO!! STEFANO, STEFANO, STEFANO!!!! :cheer:\n|Spoilers for the Week of February 11th \u00b7 DAYS: News, Spoilers & Discussion|"}
{"id": 1, "text": "*sigh* Fundamentalist community, let me pass on some advice to you I learned from the atheistic community:\nIf you have set yourself on fire, do not run.\nOkay? Okay?? Please?\nLook, D, you had two months to say to Harvard in private emails, \"Im sorry, I shouldnt have been using that animation in my paid presentations. I wont use it again. I really do like 'Inner Life', though, and would love to use it in classroom presentations, from the BioVisions site, if that is acceptable.\"\nI sat here, for two months, waiting for that to happen, anything to happen, and it didnt. Two months, on your own terms, you could have written a similar post to yesterdays. I would have given you the benefit of the doubt-- maybe you didnt know 
=== py
uint16 (1000000,) [41698  3549   373 17338 12617   351 41149 10565   284  2897  3176  1104
   837   475   584 10976  8302  1327   284  1282]

[stdout]
"""Solver-side: pack a selection.json (ordered ids) into a 12M-token train array from pool.jsonl."""
import json, sys, numpy as np
from transformers import AutoTokenizer

POOL = "/workspace/data/pool.jsonl"
SEL = sys.argv[1] if len(sys.argv) > 1 else "/workspace/submission/selection.json"
OUT = sys.argv[2] if len(sys.argv) > 2 else "/tmp/dev_train.npy"
BUDGET = 12_000_000

tok = AutoTokenizer.from_pretrained("gpt2"); EOS = tok.eos_token_id
text = {}
for line in open(POOL):
    r = json.loads(line); text[r["id"]] = r["text"]
sel = json.load(open(SEL))
parts, tot = [], 0
for i in sel:
    ids = tok(text[i], add_special_tokens=False).input_ids
    parts.extend(ids); parts.append(EOS); tot += len(ids) + 1
    if tot >= BUDGET: break
arr = np.array(parts[:BUDGET], dtype=np.uint16)
np.save(OUT, arr)
print(f"packed {len(arr)} tokens from {len(sel)} ids -> {OUT}")
=== dev_eval
#!/bin/bash
# Diagnostic: train the frozen 30M GPT on your selection and report DEV perplexity.
# Usage: bash dev_eval.sh [selection.json]   (default /workspace/submission/selection.json)
# NOTE: the official target is a DIFFERENT (hidden) sample from the same HQ domain.
set -euo pipefail
export TOKENIZERS_PARALLELISM=false
SEL="${1:-/workspace/submission/selection.json}"
cd /workspace
python3 pack_selection.py "$SEL" /tmp/dev_train.npy
python3 train_nano.py --train_npy /tmp/dev_train.npy --heldout_npy /workspace/data/multi_dev.npy \
    --out_json /tmp/dev.json --max_iters 3000 --seed 1337 2>&1 | grep HELDOUT_PPL
echo "(dev PPL on multi_dev; official uses a hidden disjoint sample of the same HQ domain)"
=== head
{"id": 0, "text": "|Viewing Single Post From: Spoilers for the Week of February 11th|\n|Lil||Feb 1 2013, 09:58 AM|\nDon't care about Chloe/Taniel/Jen-Jen. Don't care about Sami, really, but hoping that we get some good \"SAMANTHA GENE!!\" Marlena Death-Stares out of it. And \"newfound\" feelings. Please. If only.\nSTEFANO!! STEFANO, STEFANO, STEFANO!!!! :cheer:\n|Spoilers for the Week of February 11th \u00b7 DAYS: News, Spoilers & Discussion|"}
{"id": 1, "text": "*sigh* Fundamentalist community, let me pass on some advice to you I learned from the atheistic community:\nIf you have set yourself on fire, do not run.\nOkay? Okay?? Please?\nLook, D, you had two months to say to Harvard in private emails, \"Im sorry, I shouldnt have been using that animation in my paid presentations. I wont use it again. I really do like 'Inner Life', though, and would love to use it in classroom presentations, from the BioVisions site, if that is acceptable.\"\nI sat here, for two months, waiting for that to happen, anything to happen, and it didnt. Two months, on your own terms, you could have written a similar post to yesterdays. I would have given you the benefit of the doubt-- maybe you didnt know 
=== py
uint16 (1000000,) [41698  3549   373 17338 12617   351 41149 10565   284  2897  3176  1104
   837   475   584 10976  8302  1327   284  1282]
[metadata] {"noOutputExpected": false}
#3
reasoning (redacted)

encrypted chain-of-thought — recorded, not exposed

toolBashCheck cores and sklearn
args
{
  "command": "nproc; python -c \"import sklearn,scipy;print(sklearn.__version__)\"; free -g | head -2",
  "description": "Check cores and sklearn"
}
Bash result
16
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'sklearn'
               total        used        free      shared  buff/cache   available
Mem:            1024           0        1023           0           0        1023

[stdout]
16
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'sklearn'
               total        used        free      shared  buff/cache   available
Mem:            1024           0        1023           0           0        1023
[metadata] {"noOutputExpected": false}
#4
narration

No sklearn — I'll write my own vectorizer. Writing the curation script now.

toolWrite
args
{
  "file_path": "/workspace/submission/curate.py",
  "content": "#!/usr/bin/env python3\n\"\"\"\nCurate /workspace/data/pool.jsonl -> /workspace/submission/selection.json\n\nSTATED CRITERION\n================\nRank every pool document by a per-token log-likelihood ratio between two\nbag-of-ngrams language models -- one estimated on the DISCLOSED target\n(multi_dev.npy, decoded back to text) and one estimated on the pool itself --\nafter removing documents that fail cheap intrinsic-quality gates and\nnear-duplicate documents.\n\n    score(d) = (1/|d|) * sum_{f in ngrams(d)} log( p_target(f) / p_pool(f) )\n               + small intrinsic-quality bonus\n\nThis is classic importance-resampling-style data selection (DSIR), done\ndeterministically (top-k instead of Gumbel sampling) because the token budget\nis a hard 12M and we want the highest-density-ratio tokens.\n\nWhy a *per-token* ratio and not a total: the total log-ratio scales with\ndocument length and would degenerate into \"pick the longest documents\".\n\nThree stages:\n  1. GATE     - intrinsic quality heuristics (C4/Gopher-flavoured) + dedup.\n  2. RATIO    - target/pool n-gram density ratio, length-normalised.\n  3. ORDER    - emit surviving ids by descending score, i.e. the training\n                pipeline fills its 12M budget with the highest-ratio tokens.\n\nNo labels, no hand-picked ids: everything below is derived from the pool and\nfrom the disclosed dev target.\n\"\"\"\nimport json, math, os, re, sys, zlib\nfrom collections import Counter\nfrom multiprocessing import Pool as MPPool\n\nPOOL_PATH = \"/workspace/data/pool.jsonl\"\nDEV_NPY = \"/workspace/data/multi_dev.npy\"\nOUT_PATH = \"/workspace/submission/selection.json\"\nCACHE = \"/tmp/curate_cache\"\nos.makedirs(CACHE, exist_ok=True)\n\n# how many tokens of text to emit (budget is 12M; emit ~3x for safety)\nTARGET_EMIT_TOKENS = 36_000_000\nNPROC = 16\n\nWORD_RE = re.compile(r\"[a-z0-9']+|[.,;:!?()\\\"\\-]\")\nALPHA_RE = re.compile(r\"[A-Za-z]\")\n\n# ----------------------------------------------------------------- tokenising\ndef words(text):\n    return WORD_RE.findall(text.lower())\n\n\n# ------------------------------------------------------- stage 1: quality gate\ndef quality(text):\n    \"\"\"Return (ok, bonus). Cheap intrinsic signals only -- no target leakage.\"\"\"\n    n = len(text)\n    if n < 400 or n > 400_000:\n        return False, 0.0\n    w = text.split()\n    nw = len(w)\n    if nw < 60:\n        return False, 0.0\n\n    # mean word length: filters code dumps / letter soup / URL lists\n    mean_wl = sum(len(x) for x in w) / nw\n    if mean_wl < 3.0 or mean_wl > 10.0:\n        return False, 0.0\n\n    # alphabetic density: filters tables, logs, numeric dumps\n    alpha = sum(1 for c in text if c.isalpha())\n    if alpha / n < 0.65:\n        return False, 0.0\n\n    # digit density\n    digits = sum(1 for c in text if c.isdigit())\n    if digits / n > 0.10:\n        return False, 0.0\n\n    # sentence structure: prose has terminal punctuation\n    lines = [ln for ln in text.split(\"\\n\") if ln.strip()]\n    if not lines:\n        return False, 0.0\n    end_punct = sum(1 for ln in lines if ln.rstrip()[-1:] in \".!?\\\"')\")\n    end_frac = end_punct / len(lines)\n\n    # bullet / navigation boilerplate\n    bullets = sum(1 for ln in lines if ln.lstrip()[:1] in \"|*-#>\" or ln.lstrip()[:1].isdigit())\n    if bullets / len(lines) > 0.5:\n        return False, 0.0\n\n    # word-level repetition (spam, keyword stuffing, looping templates)\n    lw = [x.lower() for x in w]\n    uniq = len(set(lw)) / nw\n    if uniq < 0.28:\n        return False, 0.0\n    top1 = Counter(lw).most_common(1)[0][1] / nw\n    if top1 > 0.09:\n        return False, 0.0\n\n    # duplicate-line ratio (menus, repeated footers)\n    if len(lines) > 8:\n        dupl = 1.0 - len(set(lines)) / len(lines)\n        if dupl > 0.3:\n            return False, 0.0\n\n    # non-English / mojibake guard\n    nonascii = sum(1 for c in text if ord(c) > 127)\n    if nonascii / n > 0.06:\n        return False, 0.0\n\n    # uppercase shouting\n    upper = sum(1 for c in text if c.isupper())\n    if upper / max(1, alpha) > 0.20:\n        return False, 0.0\n\n    bonus = 0.30 * end_frac + 0.20 * min(1.0, math.log(nw / 60.0 + 1e-9) / 3.0 if nw > 60 else 0.0)\n    return True, bonus\n\n\ndef dedup_keys(text):\n    \"\"\"Two cheap near-duplicate fingerprints: head shingle and mid shingle.\"\"\"\n    w = text.split()\n    head = zlib.crc32(\" \".join(w[:35]).lower().encode()) & 0xFFFFFFFF\n    mid = len(w) // 2\n    midk = zlib.crc32(\" \".join(w[mid:mid + 35]).lower().encode()) & 0xFFFFFFFF\n    return head, midk\n\n\n# --------------------------------------------------- worker: parse + featurise\n_W = {}\n\n\ndef _init(uni, bi, w_uni, w_bi, oov_u, oov_b):\n    _W[\"uni\"], _W[\"bi\"] = uni, bi\n    _W[\"wu\"], _W[\"wb\"] = w_uni, w_bi\n    _W[\"ou\"], _W[\"ob\"] = oov_u, oov_b\n\n\ndef _stage1(lines):\n    \"\"\"Pass 1: gate + fingerprints + a word sample for the pool background LM.\"\"\"\n    out = []\n    for line in lines:\n        r = json.loads(line)\n        i, t = r[\"id\"], r[\"text\"]\n        ok, bonus = quality(t)\n        if not ok:\n            continue\n        h, m = dedup_keys(t)\n        out.append((i, len(t), bonus, h, m))\n    return out\n\n\ndef _bgcount(lines):\n    cu, cb = Counter(), Counter()\n    for line in lines:\n        t = json.loads(line)[\"text\"]\n        ws = words(t)\n        cu.update(ws)\n        cb.update(zip(ws, ws[1:]))\n    return cu, cb\n\n\ndef _score(payload):\n    \"\"\"Pass 2: per-token log density ratio for kept ids.\"\"\"\n    lines, keep = payload\n    uni, bi, wu, wb, ou, ob = _W[\"uni\"], _W[\"bi\"], _W[\"wu\"], _W[\"wb\"], _W[\"ou\"], _W[\"ob\"]\n    res = []\n    for line in lines:\n        r = json.loads(line)\n        i = r[\"id\"]\n        if i not in keep:\n            continue\n        ws = words(r[\"text\"])\n        n = len(ws)\n        if n < 40:\n            continue\n        s = 0.0\n        for x in ws:\n            j = uni.get(x)\n            s += wu[j] if j is not None else ou\n        for x in zip(ws, ws[1:]):\n            j = bi.get(x)\n            s += wb[j] if j is not None else ob\n        res.append((i, s / (2 * n)))\n    return res\n\n\ndef chunks(path, k):\n    \"\"\"Split the file into k contiguous line-lists.\"\"\"\n    all_lines = open(path, \"r\", encoding=\"utf-8\").readlines()\n    per = (len(all_lines) + k - 1) // k\n    return [all_lines[j:j + per] for j in range(0, len(all_lines), per)]\n\n\ndef main():\n    import numpy as np\n\n    # ---------------- decode the disclosed target back to text --------------\n    print(\"[1/6] decoding dev target\", flush=True)\n    from transformers import AutoTokenizer\n    tok = AutoTokenizer.from_pretrained(\"gpt2\")\n    dev = np.load(DEV_NPY).astype(np.int64)\n    target_text = tok.decode(dev.tolist())\n\n    tws = words(target_text)\n    tu, tb = Counter(tws), Counter(zip(tws, tws[1:]))\n\n    # vocabulary = target n-grams seen at least twice; hapaxes fall into the\n    # OOV bucket, which gives the OOV weight a well-defined (non-zero) target\n    # mass and acts as automatic smoothing.\n    uni_v = [k for k, c in tu.items() if c >= 2]\n    bi_v = [k for k, c in tb.items() if c >= 3]\n    uni = {k: j for j, k in enumerate(uni_v)}\n    bi = {k: j for j, k in enumerate(bi_v)}\n    print(f\"      target words={len(tws)} uni_vocab={len(uni)} bi_vocab={len(bi)}\", flush=True)\n\n    print(\"[2/6] reading pool + quality gate\", flush=True)\n    parts = chunks(POOL_PATH, NPROC * 4)\n    with MPPool(NPROC) as p:\n        st1 = p.map(_stage1, parts)\n    rows = [r for sub in st1 for r in sub]\n    n_total = sum(len(c) for c in parts)\n    print(f\"      pool={n_total} passed_gate={len(rows)}\", flush=True)\n\n    # ---------------- dedup on the two fingerprints ------------------------\n    seen_h, seen_m, kept = set(), set(), []\n    for i, nchar, bonus, h, m in sorted(rows, key=lambda r: -r[1]):\n        if h in seen_h or m in seen_m:\n            continue\n        seen_h.add(h); seen_m.add(m)\n        kept.append((i, nchar, bonus))\n    print(f\"      after dedup={len(kept)}\", flush=True)\n    keep_set = {i for i, _, _ in kept}\n    bonus_of = {i: b for i, _, b in kept}\n    nchar_of = {i: c for i, c, _ in kept}\n\n    # ---------------- pool background n-gram model -------------------------\n    print(\"[3/6] pool background LM (sample)\", flush=True)\n    sample = [c[::6] for c in parts]           # ~1/6 of the pool, spread evenly\n    with MPPool(NPROC) as p:\n        bgs = p.map(_bgcount, sample)\n    pu, pb = Counter(), Counter()\n    for cu, cb in bgs:\n        pu.update(cu); pb.update(cb)\n\n    # ---------------- log density ratios -----------------------------------\n    print(\"[4/6] weights\", flush=True)\n    Tu, Tb = sum(tu.values()), max(1, sum(tb.values()))\n    Pu, Pb = sum(pu.values()), max(1, sum(pb.values()))\n    # OOV mass = probability the model assigns to anything outside the vocab\n    t_oov_u = sum(c for k, c in tu.items() if k not in uni)\n    t_oov_b = sum(c for k, c in tb.items() if k not in bi)\n    p_oov_u = Pu - sum(pu.get(k, 0) for k in uni)\n    p_oov_b = Pb - sum(pb.get(k, 0) for k in bi)\n\n    AU, AB = 1.0, 1.0                          # add-alpha smoothing on the pool side\n    w_uni = [0.0] * len(uni)\n    for k, j in uni.items():\n        w_uni[j] = math.log((tu[k] / Tu)) - math.log(((pu.get(k, 0) + AU) / (Pu + AU * len(uni))))\n    w_bi = [0.0] * len(bi)\n    for k, j in bi.items():\n        w_bi[j] = math.log((tb[k] / Tb)) - math.log(((pb.get(k, 0) + AB) / (Pb + AB * len(bi))))\n    oov_u = math.log(max(1, t_oov_u) / Tu) - math.log(max(1, p_oov_u) / (Pu + AU * len(uni)))\n    oov_b = math.log(max(1, t_oov_b) / Tb) - math.log(max(1, p_oov_b) / (Pb + AB * len(bi)))\n    print(f\"      oov_u={oov_u:.3f} oov_b={oov_b:.3f}\", flush=True)\n\n    print(\"[5/6] scoring pool\", flush=True)\n    with MPPool(NPROC, initializer=_init,\n                initargs=(uni, bi, w_uni, w_bi, oov_u, oov_b)) as p:\n        sc = p.map(_score, [(c, keep_set) for c in parts])\n    scores = [r for sub in sc for r in sub]\n    print(f\"      scored={len(scores)}\", flush=True)\n\n    # ---------------- final ranking ----------------------------------------\n    W_BONUS = float(os.environ.get(\"W_BONUS\", \"1.0\"))\n    ranked = sorted(scores, key=lambda r: -(r[1] + W_BONUS * bonus_of.get(r[0], 0.0)))\n\n    # emit enough ids to overfill the 12M budget (~4 chars/token)\n    out, tot = [], 0\n    for i, s in ranked:\n        out.append(i)\n        tot += nchar_of[i] / 4.0\n        if tot >= TARGET_EMIT_TOKENS:\n            break\n    print(f\"[6/6] emitting {len(out)} ids (~{tot/1e6:.1f}M est tokens)\", flush=True)\n\n    dst = sys.argv[1] if len(sys.argv) > 1 else OUT_PATH\n    os.makedirs(os.path.dirname(dst), exist_ok=True)\n    json.dump(out, open(dst, \"w\"))\n    # side-car: full ranking, for ablations\n    json.dump([[i, s] for i, s in ranked[:60000]], open(\"/tmp/curate_cache/ranked.json\", \"w\"))\n    json.dump({\"bonus\": bonus_of, \"nchar\": nchar_of}, open(\"/tmp/curate_cache/meta.json\", \"w\"))\n    print(\"wrote\", dst)\n\n\nif __name__ == \"__main__\":\n    main()\n"
}
Write result
File created successfully at: /workspace/submission/curate.py (file state is current in your context — no need to Read it back)

[metadata] {"type": "create", "filePath": "/workspace/submission/curate.py", "content": "#!/usr/bin/env python3\n\"\"\"\nCurate /workspace/data/pool.jsonl -> /workspace/submission/selection.json\n\nSTATED CRITERION\n================\nRank every pool document by a per-token log-likelihood ratio between two\nbag-of-ngrams language models -- one estimated on the DISCLOSED target\n(multi_dev.npy, decoded back to text) and one estimated on the pool itself --\nafter removing documents that fail cheap intrinsic-quality gates and\nnear-duplicate documents.\n\n    score(d) = (1/|d|) * sum_{f in ngrams(d)} log( p_target(f) / p_pool(f) )\n               + small intrinsic-quality bonus\n\nThis is classic importance-resampling-style data selection (DSIR), done\ndeterministically (top-k instead of Gumbel sampling) because the token budget\nis a hard 12M and we want the highest-density-ratio tokens.\n\nWhy a *per-token* ratio and not a total: the total log-ratio scales with\ndocument length and would degenerate into \"pick the longest documents\".\n\nThree stages:\n  1. GATE     - intrinsic quality heuristics (C4/Gopher-flavoured) + dedup.\n  2. RATIO    - target/pool n-gram density ratio, length-normalised.\n  3. ORDER    - emit surviving ids by descending score, i.e. the training\n                pipeline fills its 12M budget with the highest-ratio tokens.\n\nNo labels, no hand-picked ids: everything below is derived from the pool and\nfrom the disclosed dev target.\n\"\"\"\nimport json, math, os, re, sys, zlib\nfrom collections import Counter\nfrom multiprocessing import Pool as MPPool\n\nPOOL_PATH = \"/workspace/data/pool.jsonl\"\nDEV_NPY = \"/workspace/data/multi_dev.npy\"\nOUT_PATH = \"/workspace/submission/selection.json\"\nCACHE = \"/tmp/curate_cache\"\nos.makedirs(CACHE, exist_ok=True)\n\n# how many tokens of text to emit (budget is 12M; emit ~3x for safety)\nTARGET_EMIT_TOKENS = 36_000_000\nNPROC = 16\n\nWORD_RE = re.compile(r\"[a-z0-9']+|[.,;:!?()\\\"\\-]\")\nALPHA_RE = re.compile(r\"[A-Za-z]\")\n\n# ----------------------------------------------------------------- tokenising\ndef words(text):\n    return WORD_RE.findall(text.lower())\n\n\n# ------------------------------------------------------- stage 1: quality gate\ndef quality(text):\n    \"\"\"Return (ok, bonus). Cheap intrinsic signals only -- no target leakage.\"\"\"\n    n = len(text)\n    if n < 400 or n > 400_000:\n        return False, 0.0\n    w = text.split()\n    nw = len(w)\n    if nw < 60:\n        return False, 0.0\n\n    # mean word length: filters code dumps / letter soup / URL lists\n    mean_wl = sum(len(x) for x in w) / nw\n    if mean_wl < 3.0 or mean_wl > 10.0:\n        return False, 0.0\n\n    # alphabetic density: filters tables, logs, numeric dumps\n    alpha = sum(1 for c in text if c.isalpha())\n    if alpha / n < 0.65:\n        return False, 0.0\n\n    # digit density\n    digits = sum(1 for c in text if c.isdigit())\n    if digits / n > 0.10:\n        return False, 0.0\n\n    # sentence structure: prose has terminal punctuation\n    lines = [ln for ln in text.split(\"\\n\") if ln.strip()]\n    if not lines:\n        return False, 0.0\n    end_punct = sum(1 for ln in lines if ln.rstrip()[-1:] in \".!?\\\"')\")\n    end_frac = end_punct / len(lines)\n\n    # bullet / navigation boilerplate\n    bullets = sum(1 for ln in lines if ln.lstrip()[:1] in \"|*-#>\" or ln.lstrip()[:1].isdigit())\n    if bullets / len(lines) > 0.5:\n        return False, 0.0\n\n    # word-level repetition (spam, keyword stuffing, looping templates)\n    lw = [x.lower() for x in w]\n    uniq = len(set(lw)) / nw\n    if uniq < 0.28:\n        return False, 0.0\n    top1 = Counter(lw).most_common(1)[0][1] / nw\n    if top1 > 0.09:\n        return False, 0.0\n\n    # duplicate-line ratio (menus, repeated footers)\n    if len(lines) > 8:\n        dupl = 1.0 - len(set(lines)) / len(lines)\n        if dupl > 0.3:\n            return False, 0.0\n\n    # non-English / mojibake guard\n    nonascii = sum(1 for c in text if ord(c) > 127)\n    if nonascii / n > 0.06:\n        return False, 0.0\n\n    # uppercase shouting\n    upper = sum(1 for c in text if c.isupper())\n    if upper / max(1, alpha) > 0.20:\n        return False, 0.0\n\n    bonus = 0.30 * end_frac + 0.20 * min(1.0, math.log(nw / 60.0 + 1e-9) / 3.0 if nw > 60 else 0.0)\n    return True, bonus\n\n\ndef dedup_keys(text):\n    \"\"\"Two cheap near-duplicate fingerprints: head shingle and mid shingle.\"\"\"\n    w = text.split()\n    head = zlib.crc32(\" \".join(w[:35]).lower().encode()) & 0xFFFFFFFF\n    mid = len(w) // 2\n    midk = zlib.crc32(\" \".join(w[mid:mid + 35]).lower().encode()) & 0xFFFFFFFF\n    return head, midk\n\n\n# --------------------------------------------------- worker: parse + featurise\n_W = {}\n\n\ndef _init(uni, bi, w_uni, w_bi, oov_u, oov_b):\n    _W[\"uni\"], _W[\"bi\"] = uni, bi\n    _W[\"wu\"], _W[\"wb\"] = w_uni, w_bi\n    _W[\"ou\"], _W[\"ob\"] = oov_u, oov_b\n\n\ndef _stage1(lines):\n    \"\"\"Pass 1: gate + fingerprints + a word sample for the pool background LM.\"\"\"\n    out = []\n    for line in lines:\n        r = json.loads(line)\n        i, t = r[\"id\"], r[\"text\"]\n        ok, bonus = quality(t)\n        if not ok:\n            continue\n        h, m = dedup_keys(t)\n        out.append((i, len(t), bonus, h, m))\n    return out\n\n\ndef _bgcount(lines):\n    cu, cb = Counter(), Counter()\n    for line in lines:\n        t = json.loads(line)[\"text\"]\n        ws = words(t)\n        cu.update(ws)\n        cb.update(zip(ws, ws[1:]))\n    return cu, cb\n\n\ndef _score(payload):\n    \"\"\"Pass 2: per-token log density ratio for kept ids.\"\"\"\n    lines, keep = payload\n    uni, bi, wu, wb, ou, ob = _W[\"uni\"], _W[\"bi\"], _W[\"wu\"], _W[\"wb\"], _W[\"ou\"], _W[\"ob\"]\n    res = []\n    for line in lines:\n        r = json.loads(line)\n        i = r[\"id\"]\n        if i not in keep:\n            continue\n        ws = words(r[\"text\"])\n        n = len(ws)\n        if n < 40:\n            continue\n        s = 0.0\n        for x in ws:\n            j = uni.get(x)\n            s += wu[j] if j is not None else ou\n        for x in zip(ws, ws[1:]):\n            j = bi.get(x)\n            s += wb[j] if j is not None else ob\n        res.append((i, s / (2 * n)))\n    return res\n\n\ndef chunks(path, k):\n    \"\"\"Split the file into k contiguous line-lists.\"\"\"\n    all_lines = open(path, \"r\", encoding=\"utf-8\").readlines()\n    per = (len(all_lines) + k - 1) // k\n    return [all_lines[j:j + per] for j in range(0, len(all_lines), per)]\n\n\ndef main():\n    import numpy as np\n\n    # ---------------- decode the disclosed target back to text --------------\n    print(\"[1/6] decoding dev target\", flush=True)\n    from transformers import AutoTokenizer\n    tok = AutoTokenizer.from_pretrained(\"gpt2\")\n    dev = np.load(DEV_NPY).astype(np.int64)\n    target_text = tok.decode(dev.tolist())\n\n    tws = words(target_text)\n    tu, tb = Counter(tws), Counter(zip(tws, tws[1:]))\n\n    # vocabulary = target n-grams seen at least twice; hapaxes fall into the\n    # OOV bucket, which gives the OOV weight a well-defined (non-zero) target\n    # mass and acts as automatic smoothing.\n    uni_v = [k for k, c in tu.items() if c >= 2]\n    bi_v = [k for k, c in tb.items() if c >= 3]\n    uni = {k: j for j, k in enumerate(uni_v)}\n    bi = {k: j for j, k in enumerate(bi_v)}\n    print(f\"      target words={len(tws)} uni_vocab={len(uni)} bi_vocab={len(bi)}\", flush=True)\n\n    print(\"[2/6] reading pool + quality gate\", flush=True)\n    parts = chunks(POOL_PATH, NPROC * 4)\n    with MPPool(NPROC) as p:\n        st1 = p.map(_stage1, parts)\n    rows = [r for sub in st1 for r in sub]\n    n_total = sum(len(c) for c in parts)\n    print(f\"      pool={n_total} passed_gate={len(rows)}\", flush=True)\n\n    # ---------------- dedup on the two fingerprints ------------------------\n    seen_h, seen_m, kept = set(), set(), []\n    for i, nchar, bonus, h, m in sorted(rows, key=lambda r: -r[1]):\n        if h in seen_h or m in seen_m:\n            continue\n        seen_h.add(h); seen_m.add(m)\n        kept.append((i, nchar, bonus))\n    print(f\"      after dedup={len(kept)}\", flush=True)\n    keep_set = {i for i, _, _ in kept}\n    bonus_of = {i: b for i, _, b in kept}\n    nchar_of = {i: c for i, c, _ in kept}\n\n    # ---------------- pool background n-gram model -------------------------\n    print(\"[3/6] pool background LM (sample)\", flush=True)\n    sample = [c[::6] for c in parts]           # ~1/6 of the pool, spread evenly\n    with MPPool(NPROC) as p:\n        bgs = p.map(_bgcount, sample)\n    pu, pb = Counter(), Counter()\n    for cu, cb in bgs:\n        pu.update(cu); pb.update(cb)\n\n    # ---------------- log density ratios -----------------------------------\n    print(\"[4/6] weights\", flush=True)\n    Tu, Tb = sum(tu.values()), max(1, sum(tb.values()))\n    Pu, Pb = sum(pu.values()), max(1, sum(pb.values()))\n    # OOV mass = probability the model assigns to anything outside the vocab\n    t_oov_u = sum(c for k, c in tu.items() if k not in uni)\n    t_oov_b = sum(c for k, c in tb.items() if k not in bi)\n    p_oov_u = Pu - sum(pu.get(k, 0) for k in uni)\n    p_oov_b = Pb - sum(pb.get(k, 0) for k in bi)\n\n    AU, AB = 1.0, 1.0                          # add-alpha smoothing on the pool side\n    w_uni = [0.0] * len(uni)\n    for k, j in uni.items():\n        w_uni[j] = math.log((tu[k] / Tu)) - math.log(((pu.get(k, 0) + AU) / (Pu + AU * len(uni))))\n    w_bi = [0.0] * len(bi)\n    for k, j in bi.items():\n        w_bi[j] = math.log((tb[k] / Tb)) - math.log(((pb.get(k, 0) + AB) / (Pb + AB * len(bi))))\n    oov_u = math.log(max(1, t_oov_u) / Tu) - math.log(max(1, p_oov_u) / (Pu + AU * len(uni)))\n    oov_b = math.log(max(1, t_oov_b) / Tb) - math.log(max(1, p_oov_b) / (Pb + AB * len(bi)))\n    print(f\"      oov_u={oov_u:.3f} oov_b={oov_b:.3f}\", flush=True)\n\n    print(\"[5/6] scoring pool\", flush=True)\n    with MPPool(NPROC, initializer=_init,\n                initargs=(uni, bi, w_uni, w_bi, oov_u, oov_b)) as p:\n        sc = p.map(_score, [(c, keep_set) for c in parts])\n    scores = [r for sub in sc for r in sub]\n    print(f\"      scored={len(scores)}\", flush=True)\n\n    # ---------------- final ranking ----------------------------------------\n    W_BONUS = float(os.environ.get(\"W_BONUS\", \"1.0\"))\n    ranked = sorted(scores, key=lambda r: -(r[1] + W_BONUS * bonus_of.get(r[0], 0.0)))\n\n    # emit enough ids to overfill the 12M budget (~4 chars/token)\n    out, tot = [], 0\n    for i, s in ranked:\n        out.append(i)\n        tot += nchar_of[i] / 4.0\n        if tot >= TARGET_EMIT_TOKENS:\n            break\n    print(f\"[6/6] emitting {len(out)} ids (~{tot/1e6:.1f}M est tokens)\", flush=True)\n\n    dst = sys.argv[1] if len(sys.argv) > 1 else OUT_PATH\n    os.makedirs(os.path.dirname(dst), exist_ok=True)\n    json.dump(out, open(dst, \"w\"))\n    # side-car: full ranking, for ablations\n    json.dump([[i, s] for i, s in ranked[:60000]], open(\"/tmp/curate_cache/ranked.json\", \"w\"))\n    json.dump({\"bonus\": bonus_of, \"nchar\": nchar_of}, open(\"/tmp/curate_cache/meta.json\", \"w\"))\n    print(\"wrote\", dst)\n\n\nif __name__ == \"__main__\":\n    main()\n", "structuredPatch": [], "originalFile": null, "userModified": false}
#5
reasoning (redacted)

encrypted chain-of-thought — recorded, not exposed

toolBashRun curation v1
args
{
  "command": "time python submission/curate.py /tmp/sel_v1.json 2>&1 | tail -30",
  "description": "Run curation v1",
  "timeout": 1500000
}
Bash result
[1/6] decoding dev target
      target words=815256 uni_vocab=23049 bi_vocab=47736
[2/6] reading pool + quality gate
      pool=182016 passed_gate=135211
      after dedup=127899
[3/6] pool background LM (sample)
[4/6] weights
      oov_u=-1.620 oov_b=-0.476
[5/6] scoring pool
      scored=127899
[6/6] emitting 29315 ids (~36.0M est tokens)
wrote /tmp/sel_v1.json

real	0m48.147s
user	3m5.490s
sys	0m13.590s

[stdout]
[1/6] decoding dev target
      target words=815256 uni_vocab=23049 bi_vocab=47736
[2/6] reading pool + quality gate
      pool=182016 passed_gate=135211
      after dedup=127899
[3/6] pool background LM (sample)
[4/6] weights
      oov_u=-1.620 oov_b=-0.476
[5/6] scoring pool
      scored=127899
[6/6] emitting 29315 ids (~36.0M est tokens)
wrote /tmp/sel_v1.json

real	0m48.147s
user	3m5.490s
sys	0m13.590s
[metadata] {"noOutputExpected": false}
#6
narration

48s. Now let me evaluate v1 against a random baseline.

toolBashMake random baseline selection
args
{
  "command": "python -c \"\nimport json,random\nrandom.seed(0)\nids=list(range(182016)); random.shuffle(ids)\njson.dump(ids[:60000],open('/tmp/sel_rand.json','w'))\nprint('ok')\n\"",
  "description": "Make random baseline selection"
}
Bash result
ok

[stdout]
ok
[metadata] {"noOutputExpected": false}
toolBashInspect top ranked docs
args
{
  "command": "python -c \"\nimport json\nsel=json.load(open('/tmp/sel_v1.json'))\ntext={}\nimport itertools\nwant=set(sel[:5])|set(sel[200:203])|set(sel[5000:5002])|set(sel[25000:25002])\nfor line in open('data/pool.jsonl'):\n    r=json.loads(line)\n    if r['id'] in want: text[r['id']]=r['text']\nfor k in [sel[0],sel[1],sel[2],sel[200],sel[5000],sel[25000]]:\n    print('=== rank of',k, repr(text[k][:400])); print()\n\"",
  "description": "Inspect top ranked docs"
}
Bash result
=== rank of 79735 '<|endoftext|>India’s PM Narendra Modi has tweeted pictures of his mother’s first visit to his official residence in Delhi.\nTwitter users have praised Narendra Modi’s decision to invite his mother, Heeraben Modi, at his new home.\nHeeraben Modi, who is in her nineties, lives in her family house in a town in Gujarat, the prime minister’s native state.\nHe tweeted that he “spent quality time with her a'

=== rank of 124196 'Journal of English Language Studies\nJournal Content\nSearch\nSearch Scope\nAll Authors Title Abstract Index terms Full Text\nBrowse\nBy Issue\nBy Author\nBy Title\nOther Journals\nCategories\nNotifications\nView\nSubscribe\nUser\nUsername\nPassword\nRemember me\nQUICK MENU\nFocus & Scope\nPublication Ethics\nEditorial Team\nPeer Reviewers\nAuthor Guidelines\nIndexing\nOnline Submission\nPeer Review Proccess\nScreening for '

=== rank of 146852 "'re OK to continue.<|endoftext|>Register\nJournal of English Language Studies\nJournal Content\nSearch\nSearch Scope\nAll Authors Title Abstract Index terms Full Text\nBrowse\nBy Issue\nBy Author\nBy Title\nOther Journals\nCategories\nNotifications\nView\nSubscribe\nUser\nUsername\nPassword\nRemember me\nQUICK MENU\nFocus & Scope\nPublication Ethics\nEditorial Team\nPeer Reviewers\nAuthor Guidelines\nIndexing\nOnline Submi"

=== rank of 113524 " reporting that the Indian governing coalition is on the verge of fracturing over differences of opinion about the US/India nuclear deal. The center-left Congress Party have pinned their star to the deal, while their coalition far-left communist allies are implaccably opposed on the basis that it makes India too much part of the US sphere of influence.\nIf a final split occurs, and the BBC says it'"

=== rank of 101045 " and then prepare<|endoftext|>Didier Drogba, the striker who converted Chelsea's winning penalty kick in last weekend's Champions League final against Bayern Munich, has announced that he's leaving the team immediately.\nIn eight seasons with the team, Drogba scored 157 goals in all competitions — none more important than his final penalty kick on Saturday.\nThere's speculation that Drogba might ret"

=== rank of 31403 ' have no formal degree as a counsellor or a psychologist nor any pastoral degree.\nI write from my own life experiences, including the pain and heartache endured from abuse, adultery, separation and divorce from a marriage of 9 years.\nAccompanied by this is the experience of abuse and separation from my natural parents; all this happened through my walk with Christ. I have been a Christian since I '

[stdout]
=== rank of 79735 '<|endoftext|>India’s PM Narendra Modi has tweeted pictures of his mother’s first visit to his official residence in Delhi.\nTwitter users have praised Narendra Modi’s decision to invite his mother, Heeraben Modi, at his new home.\nHeeraben Modi, who is in her nineties, lives in her family house in a town in Gujarat, the prime minister’s native state.\nHe tweeted that he “spent quality time with her a'

=== rank of 124196 'Journal of English Language Studies\nJournal Content\nSearch\nSearch Scope\nAll Authors Title Abstract Index terms Full Text\nBrowse\nBy Issue\nBy Author\nBy Title\nOther Journals\nCategories\nNotifications\nView\nSubscribe\nUser\nUsername\nPassword\nRemember me\nQUICK MENU\nFocus & Scope\nPublication Ethics\nEditorial Team\nPeer Reviewers\nAuthor Guidelines\nIndexing\nOnline Submission\nPeer Review Proccess\nScreening for '

=== rank of 146852 "'re OK to continue.<|endoftext|>Register\nJournal of English Language Studies\nJournal Content\nSearch\nSearch Scope\nAll Authors Title Abstract Index terms Full Text\nBrowse\nBy Issue\nBy Author\nBy Title\nOther Journals\nCategories\nNotifications\nView\nSubscribe\nUser\nUsername\nPassword\nRemember me\nQUICK MENU\nFocus & Scope\nPublication Ethics\nEditorial Team\nPeer Reviewers\nAuthor Guidelines\nIndexing\nOnline Submi"

=== rank of 113524 " reporting that the Indian governing coalition is on the verge of fracturing over differences of opinion about the US/India nuclear deal. The center-left Congress Party have pinned their star to the deal, while their coalition far-left communist allies are implaccably opposed on the basis that it makes India too much part of the US sphere of influence.\nIf a final split occurs, and the BBC says it'"

=== rank of 101045 " and then prepare<|endoftext|>Didier Drogba, the striker who converted Chelsea's winning penalty kick in last weekend's Champions League final against Bayern Munich, has announced that he's leaving the team immediately.\nIn eight seasons with the team, Drogba scored 157 goals in all competitions — none more important than his final penalty kick on Saturday.\nThere's speculation that Drogba might ret"

=== rank of 31403 ' have no formal degree as a counsellor or a psychologist nor any pastoral degree.\nI write from my own life experiences, including the pain and heartache endured from abuse, adultery, separation and divorce from a marriage of 9 years.\nAccompanied by this is the experience of abuse and separation from my natural parents; all this happened through my walk with Christ. I have been a Christian since I '
[metadata] {"noOutputExpected": false}
#7
narration

Found an issue: navigation-boilerplate pages rank near the top. Let me start baseline measurement while I fix the gates.

toolBashLaunch baseline and v1 evals in background
args
{
  "command": "cat /workspace/.timer/remaining_secs && (bash dev_eval.sh /tmp/sel_rand.json > /tmp/eval_rand.log 2>&1; bash dev_eval.sh /tmp/sel_v1.json > /tmp/eval_v1.log 2>&1) &\nsleep 2; echo launched",
  "description": "Launch baseline and v1 evals in background"
}
Bash result
5199
launched

[stdout]
5199
launched
[metadata] {"noOutputExpected": false}
#8
reasoning (redacted)

encrypted chain-of-thought — recorded, not exposed

toolBashInspect target structure for domain blocks
args
{
  "command": "python -c \"\nimport numpy as np\nfrom transformers import AutoTokenizer\ntok=AutoTokenizer.from_pretrained('gpt2')\nd=np.load('data/multi_dev.npy').astype(np.int64)\nprint('eos count', (d==50256).sum())\nt=tok.decode(d.tolist())\nopen('/tmp/target.txt','w').write(t)\nn=len(t)\nfor f in [0,0.12,0.25,0.37,0.5,0.62,0.75,0.87]:\n    print('---',f, repr(t[int(f*n):int(f*n)+260]))\n\" 2>&1 | grep -v Warn",
  "description": "Inspect target structure for domain blocks"
}
Bash result
eos count 2704
--- 0 ' Beardmore was sufficiently impressed with Shackleton to offer financial support , but other donations proved hard to come by . Nevertheless , in February 1907 , Shackleton presented to the Royal Geographic Society his plans for an Antarctic expedition , the d'
--- 0.12 'lude considering the conditional bias to be an exogenous variable explained by a policy aimed at smoothing interest rates and stabilizing exchange rates , or considering that an economy allowing for discrete changes could facilitate excess returns in the forwa'
--- 0.25 'ntradictory statements on the war in Iraq , and argued that Kerry lacked the decisiveness and vision necessary for success in the War on Terror . \n<|endoftext|> In the election , Bush carried 31 of 50 states , receiving a total of 286 electoral votes . He won '
--- 0.37 ': Tango, Tango, Salve, Branch, Branch, Branch, Slipper of Agility\n\nEarly: Phase, Bottle, BKB, Blink\n\nLate: Daedalus, Butterfly, MKB, Assault Cuirass<|endoftext|>The bad news for the beleaguered trackside inhabitants of Lac Megantic, Quebec, continues to roll r'
--- 0.5 'for somebody else to win it.\n\nTony George: It\'s the loudest I ever heard the Swamp. I\'ve been back for games and haven\'t heard it close to that loud. As soon as I cut back and could see the goal line, I was thinking, "There\'s no way anybody is going to catch m'
--- 0.62 'nd completed its 18th anniversary on July 3.Smriti Irani, who got the entry pass in the industry with the saas-bahu daily soap, became a household name for essaying the role of Tulsi Virani. To celebrate the occasion, Irani, who is now a Union Minister, took t'
--- 0.75 ' spin through square leg for a run. 128/0\n29.4 T Islam to Elgar, Gives it air on off, too full but. Elgar drives it to mid off. 127/0\n29.3 T Islam to Elgar, Flatter on off, turning back in. Elgar with the spin works it to square leg. 127/0\n29.2 T Islam to Mark'
--- 0.87 "Java 5, typesafe enums are recommended instead; and</li>\n<li>The class really isn't a class. It's just a holder for static constants and/or methods.</li>\n</ol>\n\n<p>As an example of (2), you may want to create canonical objects. For example, RGB color combinati"

[stdout]
eos count 2704
--- 0 ' Beardmore was sufficiently impressed with Shackleton to offer financial support , but other donations proved hard to come by . Nevertheless , in February 1907 , Shackleton presented to the Royal Geographic Society his plans for an Antarctic expedition , the d'
--- 0.12 'lude considering the conditional bias to be an exogenous variable explained by a policy aimed at smoothing interest rates and stabilizing exchange rates , or considering that an economy allowing for discrete changes could facilitate excess returns in the forwa'
--- 0.25 'ntradictory statements on the war in Iraq , and argued that Kerry lacked the decisiveness and vision necessary for success in the War on Terror . \n<|endoftext|> In the election , Bush carried 31 of 50 states , receiving a total of 286 electoral votes . He won '
--- 0.37 ': Tango, Tango, Salve, Branch, Branch, Branch, Slipper of Agility\n\nEarly: Phase, Bottle, BKB, Blink\n\nLate: Daedalus, Butterfly, MKB, Assault Cuirass<|endoftext|>The bad news for the beleaguered trackside inhabitants of Lac Megantic, Quebec, continues to roll r'
--- 0.5 'for somebody else to win it.\n\nTony George: It\'s the loudest I ever heard the Swamp. I\'ve been back for games and haven\'t heard it close to that loud. As soon as I cut back and could see the goal line, I was thinking, "There\'s no way anybody is going to catch m'
--- 0.62 'nd completed its 18th anniversary on July 3.Smriti Irani, who got the entry pass in the industry with the saas-bahu daily soap, became a household name for essaying the role of Tulsi Virani. To celebrate the occasion, Irani, who is now a Union Minister, took t'
--- 0.75 ' spin through square leg for a run. 128/0\n29.4 T Islam to Elgar, Gives it air on off, too full but. Elgar drives it to mid off. 127/0\n29.3 T Islam to Elgar, Flatter on off, turning back in. Elgar with the spin works it to square leg. 127/0\n29.2 T Islam to Mark'
--- 0.87 "Java 5, typesafe enums are recommended instead; and</li>\n<li>The class really isn't a class. It's just a holder for static constants and/or methods.</li>\n</ol>\n\n<p>As an example of (2), you may want to create canonical objects. For example, RGB color combinati"
[metadata] {"noOutputExpected": false}
#9
narration

Target is 4 registers: Wikipedia (spaced-punctuation style), news, general web prose, and technical Q&A with HTML markup. My hard gates would wrongly reject markup-heavy Q&A and stat-heavy commentary. Checking eval results:

toolBashCheck eval progress
args
{
  "command": "cat /tmp/eval_rand.log 2>/dev/null; echo \"--- v1:\"; cat /tmp/eval_v1.log 2>/dev/null; echo \"--- t:\"; cat /workspace/.timer/remaining_secs",
  "description": "Check eval progress"
}
Bash result
Token indices sequence length is longer than the specified maximum sequence length for this model (16816 > 1024). Running this sequence through the model will result in indexing errors
packed 12000000 tokens from 60000 ids -> /tmp/dev_train.npy
--- v1:
--- t:
5149

[stdout]
Token indices sequence length is longer than the specified maximum sequence length for this model (16816 > 1024). Running this sequence through the model will result in indexing errors
packed 12000000 tokens from 60000 ids -> /tmp/dev_train.npy
--- v1:
--- t:
5149
[metadata] {"noOutputExpected": false}
#10
narration

Writing v2 with looser gates plus target-mixture clustering while the baseline trains.

toolWrite
args
{
  "file_path": "/workspace/submission/curate.py",
  "content": "#!/usr/bin/env python3\n\"\"\"\nCurate /workspace/data/pool.jsonl -> /workspace/submission/selection.json\n\nSTATED CRITERION\n================\nThe disclosed evaluation target is a *mixture* of four high-quality English\nregisters (encyclopedic / general HQ web prose / news / technical Q&A).  So the\nselection criterion has two parts, and both are estimated from the disclosed\ndev target (`multi_dev.npy`, decoded back to text) plus the pool itself.  No\nlabels, no hand-picked ids.\n\n  (A) DENSITY RATIO.  For each pool document d, a per-token log-likelihood\n      ratio between a bag-of-ngrams model of the target and one of the pool:\n\n          s_c(d) = 1/(2|d|) * sum_{f in uni(d) u bi(d)} log p_c(f)/p_pool(f)\n\n      This is importance-resampling-style selection (DSIR), made deterministic\n      (top-k, not Gumbel sampling) because the 12M token budget is hard and we\n      want the highest-density-ratio tokens available.  The ratio is\n      *per-token*: a total log-ratio grows with length and would degenerate\n      into \"pick the longest documents\".\n\n  (B) MIXTURE MATCHING.  A single global ratio is not enough.  Top-k truncation\n      of a global score concentrates the budget on whichever register the pool\n      happens to hold the most high-ratio material in, and starving one quarter\n      of the target costs more cross-entropy than over-serving another gains.\n      So the target is segmented and k-means-clustered into K registers; a\n      ratio model p_c is fit per cluster; every document is assigned to its\n      arg-max cluster; and the budget is filled with a per-cluster token quota\n      PROPORTIONAL TO THAT CLUSTER'S SHARE OF THE TARGET.  The emitted order\n      interleaves clusters by quota so that any prefix of the list -- including\n      the 12M-token prefix the trainer actually consumes -- already matches the\n      target mixture.\n\n  (C) GATES.  Only genuinely destroyed text is hard-filtered: too short,\n      character-soup, extreme n-gram/line repetition, non-English, and\n      navigation/menu boilerplate (detected as a page whose lines are almost\n      all too short to be sentences).  The gates are deliberately LOOSE about\n      markup and digits, because two of the four target registers are\n      HTML-tagged technical Q&A and number-dense sports/news copy; the usual\n      C4-style \"clean prose\" filters throw exactly those away.\n\"\"\"\nimport json, math, os, re, sys, zlib\nfrom collections import Counter\nfrom multiprocessing import Pool as MPPool\n\nPOOL_PATH = \"/workspace/data/pool.jsonl\"\nDEV_NPY = \"/workspace/data/multi_dev.npy\"\nOUT_PATH = \"/workspace/submission/selection.json\"\nCACHE = \"/tmp/curate_cache\"\nos.makedirs(CACHE, exist_ok=True)\n\nTARGET_EMIT_TOKENS = int(os.environ.get(\"EMIT_TOKENS\", 40_000_000))\nNPROC = 16\nK = int(os.environ.get(\"K\", 6))              # target registers to discover\nSEG_WORDS = 1200                             # target segment size for clustering\nW_BONUS = float(os.environ.get(\"W_BONUS\", \"0.5\"))\n\nWORD_RE = re.compile(r\"[a-z0-9']+|[.,;:!?()\\\"\\-]\")\n\n\ndef words(text):\n    return WORD_RE.findall(text.lower())\n\n\n# ------------------------------------------------------- stage 1: quality gate\ndef quality(text):\n    \"\"\"(ok, bonus). Loose intrinsic gates: drop destroyed text, keep markup.\"\"\"\n    n = len(text)\n    if n < 500 or n > 600_000:\n        return False, 0.0\n    w = text.split()\n    nw = len(w)\n    if nw < 80:\n        return False, 0.0\n\n    mean_wl = sum(len(x) for x in w) / nw\n    if mean_wl < 2.5 or mean_wl > 12.0:            # letter soup / URL or base64 dumps\n        return False, 0.0\n\n    alpha = sum(1 for c in text if c.isalpha())\n    if alpha / n < 0.45:                            # loose: markup & numbers survive\n        return False, 0.0\n\n    nonascii = sum(1 for c in text if ord(c) > 127)\n    if nonascii / n > 0.08:                         # non-English / mojibake\n        return False, 0.0\n\n    lines = [ln for ln in text.split(\"\\n\") if ln.strip()]\n    if not lines:\n        return False, 0.0\n    lw_counts = [len(ln.split()) for ln in lines]\n\n    # navigation / menu / link-farm boilerplate: a page made of stubs, not\n    # sentences. This is the single most important gate -- site chrome is\n    # lexically rich (it scores WELL on a density ratio) but teaches nothing.\n    long_lines = sum(1 for c in lw_counts if c >= 8)\n    if long_lines / len(lines) < 0.25:\n        return False, 0.0\n    if sum(lw_counts) / len(lines) < 5.0:\n        return False, 0.0\n\n    # duplicated lines: footers, repeated templates\n    if len(lines) > 8 and (1.0 - len(set(lines)) / len(lines)) > 0.40:\n        return False, 0.0\n\n    # word-level repetition / keyword stuffing\n    lwl = [x.lower() for x in w]\n    if len(set(lwl)) / nw < 0.22:\n        return False, 0.0\n    if Counter(lwl).most_common(1)[0][1] / nw > 0.12:\n        return False, 0.0\n\n    # bonus: prose-ish structure (terminal punctuation on lines) + substance\n    end_punct = sum(1 for ln in lines if ln.rstrip()[-1:] in \".!?\\\"')>\")\n    end_frac = end_punct / len(lines)\n    bonus = 0.30 * end_frac + 0.15 * min(1.0, nw / 800.0)\n    return True, bonus\n\n\ndef dedup_keys(text):\n    w = text.split()\n    head = zlib.crc32(\" \".join(w[:35]).lower().encode()) & 0xFFFFFFFF\n    mid = len(w) // 2\n    midk = zlib.crc32(\" \".join(w[mid:mid + 35]).lower().encode()) & 0xFFFFFFFF\n    return head, midk\n\n\n# ------------------------------------------------------------------- workers\n_W = {}\n\n\ndef _init(uni, bi, WU, WB, OU, OB):\n    _W.update(uni=uni, bi=bi, WU=WU, WB=WB, OU=OU, OB=OB)\n\n\ndef _stage1(lines):\n    out = []\n    for line in lines:\n        r = json.loads(line)\n        ok, bonus = quality(r[\"text\"])\n        if ok:\n            h, m = dedup_keys(r[\"text\"])\n            out.append((r[\"id\"], len(r[\"text\"]), bonus, h, m))\n    return out\n\n\ndef _bgcount(lines):\n    cu, cb = Counter(), Counter()\n    for line in lines:\n        ws = words(json.loads(line)[\"text\"])\n        cu.update(ws)\n        cb.update(zip(ws, ws[1:]))\n    return cu, cb\n\n\ndef _score(payload):\n    \"\"\"Per-token log density ratio of each kept doc against ALL K clusters.\"\"\"\n    lines, keep = payload\n    uni, bi, WU, WB, OU, OB = _W[\"uni\"], _W[\"bi\"], _W[\"WU\"], _W[\"WB\"], _W[\"OU\"], _W[\"OB\"]\n    k = len(WU)\n    rng = range(k)\n    res = []\n    for line in lines:\n        r = json.loads(line)\n        i = r[\"id\"]\n        if i not in keep:\n            continue\n        ws = words(r[\"text\"])\n        n = len(ws)\n        if n < 40:\n            continue\n        cu = Counter(ws)\n        cb = Counter(zip(ws, ws[1:]))\n        # collapse to (index, count) lists once, then reuse for every cluster\n        iu = [(uni[x], c) for x, c in cu.items() if x in uni]\n        ou = sum(c for x, c in cu.items() if x not in uni)\n        ib = [(bi[x], c) for x, c in cb.items() if x in bi]\n        ob = sum(c for x, c in cb.items() if x not in bi)\n        s = [0.0] * k\n        for c_ in rng:\n            wu, wb = WU[c_], WB[c_]\n            t = OU[c_] * ou + OB[c_] * ob\n            for j, cnt in iu:\n                t += wu[j] * cnt\n            for j, cnt in ib:\n                t += wb[j] * cnt\n            s[c_] = t / (2 * n)\n        res.append((i, s))\n    return res\n\n\ndef chunks(path, k):\n    all_lines = open(path, \"r\", encoding=\"utf-8\").readlines()\n    per = (len(all_lines) + k - 1) // k\n    return [all_lines[j:j + per] for j in range(0, len(all_lines), per)]\n\n\n# ------------------------------------------------------------------- clustering\ndef cluster_target(segs, K, seed=0):\n    \"\"\"Spherical k-means on L2-normalised top-V unigram counts of target segments.\"\"\"\n    import numpy as np\n    df = Counter()\n    for s in segs:\n        df.update(set(s))\n    vocab = [w for w, c in df.most_common(4000) if c >= 3]\n    vi = {w: j for j, w in enumerate(vocab)}\n    X = np.zeros((len(segs), len(vocab)), dtype=np.float32)\n    for r, s in enumerate(segs):\n        for w, c in Counter(s).items():\n            j = vi.get(w)\n            if j is not None:\n                X[r, j] = 1.0 + math.log(c)\n    idf = np.log(len(segs) / (1.0 + np.array([df[w] for w in vocab], dtype=np.float32)))\n    X *= idf\n    X /= np.linalg.norm(X, axis=1, keepdims=True) + 1e-9\n    rng = np.random.default_rng(seed)\n    C = X[rng.choice(len(segs), K, replace=False)].copy()\n    lab = np.zeros(len(segs), dtype=np.int64)\n    for _ in range(60):\n        lab = (X @ C.T).argmax(1)\n        for c in range(K):\n            m = lab == c\n            if m.sum() == 0:                          # re-seed a dead centroid\n                C[c] = X[rng.integers(len(segs))]\n            else:\n                v = X[m].mean(0)\n                C[c] = v / (np.linalg.norm(v) + 1e-9)\n    return lab\n\n\ndef main():\n    import numpy as np\n\n    # ------------- decode the disclosed target, segment, cluster ------------\n    print(\"[1/7] decoding dev target\", flush=True)\n    from transformers import AutoTokenizer\n    tok = AutoTokenizer.from_pretrained(\"gpt2\")\n    target_text = tok.decode(np.load(DEV_NPY).astype(np.int64).tolist())\n    tws = words(target_text)\n    segs = [tws[i:i + SEG_WORDS] for i in range(0, len(tws) - 200, SEG_WORDS)]\n    print(f\"      target words={len(tws)} segments={len(segs)}\", flush=True)\n\n    print(f\"[2/7] clustering target into K={K} registers\", flush=True)\n    lab = cluster_target(segs, K)\n    share = np.bincount(lab, minlength=K) / len(lab)\n    print(\"      cluster shares:\", np.round(share, 3).tolist(), flush=True)\n\n    # per-cluster n-gram counts; global vocab shared across clusters\n    TU = [Counter() for _ in range(K)]\n    TB = [Counter() for _ in range(K)]\n    for s, c in zip(segs, lab):\n        TU[c].update(s)\n        TB[c].update(zip(s, s[1:]))\n    gu, gb = Counter(), Counter()\n    for c in range(K):\n        gu.update(TU[c]); gb.update(TB[c])\n    uni_v = [k for k, c in gu.items() if c >= 2]\n    bi_v = [k for k, c in gb.items() if c >= 3]\n    uni = {k: j for j, k in enumerate(uni_v)}\n    bi = {k: j for j, k in enumerate(bi_v)}\n    print(f\"      uni_vocab={len(uni)} bi_vocab={len(bi)}\", flush=True)\n\n    # ------------- gate + dedup --------------------------------------------\n    print(\"[3/7] quality gate\", flush=True)\n    parts = chunks(POOL_PATH, NPROC * 4)\n    with MPPool(NPROC) as p:\n        st1 = p.map(_stage1, parts)\n    rows = [r for sub in st1 for r in sub]\n    print(f\"      pool={sum(len(c) for c in parts)} passed_gate={len(rows)}\", flush=True)\n\n    seen_h, seen_m, kept = set(), set(), []\n    for i, nchar, bonus, h, m in sorted(rows, key=lambda r: -r[1]):\n        if h in seen_h or m in seen_m:\n            continue\n        seen_h.add(h); seen_m.add(m)\n        kept.append((i, nchar, bonus))\n    print(f\"      after dedup={len(kept)}\", flush=True)\n    keep_set = {i for i, _, _ in kept}\n    bonus_of = {i: b for i, _, b in kept}\n    nchar_of = {i: c for i, c, _ in kept}\n\n    # ------------- pool background model -----------------------------------\n    print(\"[4/7] pool background LM\", flush=True)\n    with MPPool(NPROC) as p:\n        bgs = p.map(_bgcount, [c[::5] for c in parts])\n    pu, pb = Counter(), Counter()\n    for cu, cb in bgs:\n        pu.update(cu); pb.update(cb)\n    Pu, Pb = sum(pu.values()), max(1, sum(pb.values()))\n    AU = AB = 1.0\n    den_u = [math.log((pu.get(k, 0) + AU) / (Pu + AU * len(uni))) for k in uni_v]\n    den_b = [math.log((pb.get(k, 0) + AB) / (Pb + AB * len(bi))) for k in bi_v]\n    p_oov_u = max(1, Pu - sum(pu.get(k, 0) for k in uni))\n    p_oov_b = max(1, Pb - sum(pb.get(k, 0) for k in bi))\n    d_oov_u = math.log(p_oov_u / (Pu + AU * len(uni)))\n    d_oov_b = math.log(p_oov_b / (Pb + AB * len(bi)))\n\n    # ------------- per-cluster weights -------------------------------------\n    print(\"[5/7] per-cluster weights\", flush=True)\n    WU, WB, OU, OB = [], [], [], []\n    for c in range(K):\n        tu, tb = TU[c], TB[c]\n        Tu, Tb = max(1, sum(tu.values())), max(1, sum(tb.values()))\n        # add-alpha on the target side too: clusters are small, and an unseen\n        # target n-gram must not be treated as impossible.\n        au, ab = 0.15, 0.05\n        wu = [math.log((tu.get(k, 0) + au) / (Tu + au * len(uni))) - den_u[j]\n              for j, k in enumerate(uni_v)]\n        wb = [math.log((tb.get(k, 0) + ab) / (Tb + ab * len(bi))) - den_b[j]\n              for j, k in enumerate(bi_v)]\n        t_oov_u = max(1, sum(v for k, v in tu.items() if k not in uni))\n        t_oov_b = max(1, sum(v for k, v in tb.items() if k not in bi))\n        WU.append(wu); WB.append(wb)\n        OU.append(math.log(t_oov_u / (Tu + au * len(uni))) - d_oov_u)\n        OB.append(math.log(t_oov_b / (Tb + ab * len(bi))) - d_oov_b)\n\n    # ------------- score ----------------------------------------------------\n    print(\"[6/7] scoring pool\", flush=True)\n    with MPPool(NPROC, initializer=_init, initargs=(uni, bi, WU, WB, OU, OB)) as p:\n        sc = p.map(_score, [(c, keep_set) for c in parts])\n    scores = [r for sub in sc for r in sub]\n    print(f\"      scored={len(scores)}\", flush=True)\n\n    # ------------- assign to arg-max cluster, quota by target share --------\n    print(\"[7/7] mixture-matched ordering\", flush=True)\n    buckets = [[] for _ in range(K)]\n    for i, s in scores:\n        c = max(range(K), key=lambda j: s[j])\n        buckets[c].append((i, s[c] + W_BONUS * bonus_of.get(i, 0.0)))\n    for c in range(K):\n        buckets[c].sort(key=lambda r: -r[1])\n        print(f\"      cluster {c}: share={share[c]:.3f} docs={len(buckets[c])}\", flush=True)\n\n    # interleave by quota so EVERY prefix matches the target mixture\n    est_tok = {i: nchar_of[i] / 4.0 for i in nchar_of}\n    ptr = [0] * K\n    emitted_tok = [0.0] * K\n    out, tot = [], 0.0\n    while tot < TARGET_EMIT_TOKENS:\n        # cluster furthest below its quota, that still has documents left\n        cand = [c for c in range(K) if ptr[c] < len(buckets[c])]\n        if not cand:\n            break\n        c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))\n        i = buckets[c][ptr[c]][0]\n        ptr[c] += 1\n        out.append(i)\n        emitted_tok[c] += est_tok[i]\n        tot += est_tok[i]\n\n    print(f\"      emitted {len(out)} ids (~{tot/1e6:.1f}M est tokens)\", flush=True)\n    dst = sys.argv[1] if len(sys.argv) > 1 else OUT_PATH\n    os.makedirs(os.path.dirname(dst), exist_ok=True)\n    json.dump(out, open(dst, \"w\"))\n    json.dump({\"share\": share.tolist(),\n               \"buckets\": [[[i, float(s)] for i, s in b[:20000]] for b in buckets],\n               \"est_tok\": est_tok, \"bonus\": bonus_of},\n              open(f\"{CACHE}/state.json\", \"w\"))\n    print(\"wrote\", dst)\n\n\nif __name__ == \"__main__\":\n    main()\n"
}
Write result
The file /workspace/submission/curate.py has been updated successfully. (file state is current in your context — no need to Read it back)

[metadata] {"type": "update", "filePath": "/workspace/submission/curate.py", "content": "#!/usr/bin/env python3\n\"\"\"\nCurate /workspace/data/pool.jsonl -> /workspace/submission/selection.json\n\nSTATED CRITERION\n================\nThe disclosed evaluation target is a *mixture* of four high-quality English\nregisters (encyclopedic / general HQ web prose / news / technical Q&A).  So the\nselection criterion has two parts, and both are estimated from the disclosed\ndev target (`multi_dev.npy`, decoded back to text) plus the pool itself.  No\nlabels, no hand-picked ids.\n\n  (A) DENSITY RATIO.  For each pool document d, a per-token log-likelihood\n      ratio between a bag-of-ngrams model of the target and one of the pool:\n\n          s_c(d) = 1/(2|d|) * sum_{f in uni(d) u bi(d)} log p_c(f)/p_pool(f)\n\n      This is importance-resampling-style selection (DSIR), made deterministic\n      (top-k, not Gumbel sampling) because the 12M token budget is hard and we\n      want the highest-density-ratio tokens available.  The ratio is\n      *per-token*: a total log-ratio grows with length and would degenerate\n      into \"pick the longest documents\".\n\n  (B) MIXTURE MATCHING.  A single global ratio is not enough.  Top-k truncation\n      of a global score concentrates the budget on whichever register the pool\n      happens to hold the most high-ratio material in, and starving one quarter\n      of the target costs more cross-entropy than over-serving another gains.\n      So the target is segmented and k-means-clustered into K registers; a\n      ratio model p_c is fit per cluster; every document is assigned to its\n      arg-max cluster; and the budget is filled with a per-cluster token quota\n      PROPORTIONAL TO THAT CLUSTER'S SHARE OF THE TARGET.  The emitted order\n      interleaves clusters by quota so that any prefix of the list -- including\n      the 12M-token prefix the trainer actually consumes -- already matches the\n      target mixture.\n\n  (C) GATES.  Only genuinely destroyed text is hard-filtered: too short,\n      character-soup, extreme n-gram/line repetition, non-English, and\n      navigation/menu boilerplate (detected as a page whose lines are almost\n      all too short to be sentences).  The gates are deliberately LOOSE about\n      markup and digits, because two of the four target registers are\n      HTML-tagged technical Q&A and number-dense sports/news copy; the usual\n      C4-style \"clean prose\" filters throw exactly those away.\n\"\"\"\nimport json, math, os, re, sys, zlib\nfrom collections import Counter\nfrom multiprocessing import Pool as MPPool\n\nPOOL_PATH = \"/workspace/data/pool.jsonl\"\nDEV_NPY = \"/workspace/data/multi_dev.npy\"\nOUT_PATH = \"/workspace/submission/selection.json\"\nCACHE = \"/tmp/curate_cache\"\nos.makedirs(CACHE, exist_ok=True)\n\nTARGET_EMIT_TOKENS = int(os.environ.get(\"EMIT_TOKENS\", 40_000_000))\nNPROC = 16\nK = int(os.environ.get(\"K\", 6))              # target registers to discover\nSEG_WORDS = 1200                             # target segment size for clustering\nW_BONUS = float(os.environ.get(\"W_BONUS\", \"0.5\"))\n\nWORD_RE = re.compile(r\"[a-z0-9']+|[.,;:!?()\\\"\\-]\")\n\n\ndef words(text):\n    return WORD_RE.findall(text.lower())\n\n\n# ------------------------------------------------------- stage 1: quality gate\ndef quality(text):\n    \"\"\"(ok, bonus). Loose intrinsic gates: drop destroyed text, keep markup.\"\"\"\n    n = len(text)\n    if n < 500 or n > 600_000:\n        return False, 0.0\n    w = text.split()\n    nw = len(w)\n    if nw < 80:\n        return False, 0.0\n\n    mean_wl = sum(len(x) for x in w) / nw\n    if mean_wl < 2.5 or mean_wl > 12.0:            # letter soup / URL or base64 dumps\n        return False, 0.0\n\n    alpha = sum(1 for c in text if c.isalpha())\n    if alpha / n < 0.45:                            # loose: markup & numbers survive\n        return False, 0.0\n\n    nonascii = sum(1 for c in text if ord(c) > 127)\n    if nonascii / n > 0.08:                         # non-English / mojibake\n        return False, 0.0\n\n    lines = [ln for ln in text.split(\"\\n\") if ln.strip()]\n    if not lines:\n        return False, 0.0\n    lw_counts = [len(ln.split()) for ln in lines]\n\n    # navigation / menu / link-farm boilerplate: a page made of stubs, not\n    # sentences. This is the single most important gate -- site chrome is\n    # lexically rich (it scores WELL on a density ratio) but teaches nothing.\n    long_lines = sum(1 for c in lw_counts if c >= 8)\n    if long_lines / len(lines) < 0.25:\n        return False, 0.0\n    if sum(lw_counts) / len(lines) < 5.0:\n        return False, 0.0\n\n    # duplicated lines: footers, repeated templates\n    if len(lines) > 8 and (1.0 - len(set(lines)) / len(lines)) > 0.40:\n        return False, 0.0\n\n    # word-level repetition / keyword stuffing\n    lwl = [x.lower() for x in w]\n    if len(set(lwl)) / nw < 0.22:\n        return False, 0.0\n    if Counter(lwl).most_common(1)[0][1] / nw > 0.12:\n        return False, 0.0\n\n    # bonus: prose-ish structure (terminal punctuation on lines) + substance\n    end_punct = sum(1 for ln in lines if ln.rstrip()[-1:] in \".!?\\\"')>\")\n    end_frac = end_punct / len(lines)\n    bonus = 0.30 * end_frac + 0.15 * min(1.0, nw / 800.0)\n    return True, bonus\n\n\ndef dedup_keys(text):\n    w = text.split()\n    head = zlib.crc32(\" \".join(w[:35]).lower().encode()) & 0xFFFFFFFF\n    mid = len(w) // 2\n    midk = zlib.crc32(\" \".join(w[mid:mid + 35]).lower().encode()) & 0xFFFFFFFF\n    return head, midk\n\n\n# ------------------------------------------------------------------- workers\n_W = {}\n\n\ndef _init(uni, bi, WU, WB, OU, OB):\n    _W.update(uni=uni, bi=bi, WU=WU, WB=WB, OU=OU, OB=OB)\n\n\ndef _stage1(lines):\n    out = []\n    for line in lines:\n        r = json.loads(line)\n        ok, bonus = quality(r[\"text\"])\n        if ok:\n            h, m = dedup_keys(r[\"text\"])\n            out.append((r[\"id\"], len(r[\"text\"]), bonus, h, m))\n    return out\n\n\ndef _bgcount(lines):\n    cu, cb = Counter(), Counter()\n    for line in lines:\n        ws = words(json.loads(line)[\"text\"])\n        cu.update(ws)\n        cb.update(zip(ws, ws[1:]))\n    return cu, cb\n\n\ndef _score(payload):\n    \"\"\"Per-token log density ratio of each kept doc against ALL K clusters.\"\"\"\n    lines, keep = payload\n    uni, bi, WU, WB, OU, OB = _W[\"uni\"], _W[\"bi\"], _W[\"WU\"], _W[\"WB\"], _W[\"OU\"], _W[\"OB\"]\n    k = len(WU)\n    rng = range(k)\n    res = []\n    for line in lines:\n        r = json.loads(line)\n        i = r[\"id\"]\n        if i not in keep:\n            continue\n        ws = words(r[\"text\"])\n        n = len(ws)\n        if n < 40:\n            continue\n        cu = Counter(ws)\n        cb = Counter(zip(ws, ws[1:]))\n        # collapse to (index, count) lists once, then reuse for every cluster\n        iu = [(uni[x], c) for x, c in cu.items() if x in uni]\n        ou = sum(c for x, c in cu.items() if x not in uni)\n        ib = [(bi[x], c) for x, c in cb.items() if x in bi]\n        ob = sum(c for x, c in cb.items() if x not in bi)\n        s = [0.0] * k\n        for c_ in rng:\n            wu, wb = WU[c_], WB[c_]\n            t = OU[c_] * ou + OB[c_] * ob\n            for j, cnt in iu:\n                t += wu[j] * cnt\n            for j, cnt in ib:\n                t += wb[j] * cnt\n            s[c_] = t / (2 * n)\n        res.append((i, s))\n    return res\n\n\ndef chunks(path, k):\n    all_lines = open(path, \"r\", encoding=\"utf-8\").readlines()\n    per = (len(all_lines) + k - 1) // k\n    return [all_lines[j:j + per] for j in range(0, len(all_lines), per)]\n\n\n# ------------------------------------------------------------------- clustering\ndef cluster_target(segs, K, seed=0):\n    \"\"\"Spherical k-means on L2-normalised top-V unigram counts of target segments.\"\"\"\n    import numpy as np\n    df = Counter()\n    for s in segs:\n        df.update(set(s))\n    vocab = [w for w, c in df.most_common(4000) if c >= 3]\n    vi = {w: j for j, w in enumerate(vocab)}\n    X = np.zeros((len(segs), len(vocab)), dtype=np.float32)\n    for r, s in enumerate(segs):\n        for w, c in Counter(s).items():\n            j = vi.get(w)\n            if j is not None:\n                X[r, j] = 1.0 + math.log(c)\n    idf = np.log(len(segs) / (1.0 + np.array([df[w] for w in vocab], dtype=np.float32)))\n    X *= idf\n    X /= np.linalg.norm(X, axis=1, keepdims=True) + 1e-9\n    rng = np.random.default_rng(seed)\n    C = X[rng.choice(len(segs), K, replace=False)].copy()\n    lab = np.zeros(len(segs), dtype=np.int64)\n    for _ in range(60):\n        lab = (X @ C.T).argmax(1)\n        for c in range(K):\n            m = lab == c\n            if m.sum() == 0:                          # re-seed a dead centroid\n                C[c] = X[rng.integers(len(segs))]\n            else:\n                v = X[m].mean(0)\n                C[c] = v / (np.linalg.norm(v) + 1e-9)\n    return lab\n\n\ndef main():\n    import numpy as np\n\n    # ------------- decode the disclosed target, segment, cluster ------------\n    print(\"[1/7] decoding dev target\", flush=True)\n    from transformers import AutoTokenizer\n    tok = AutoTokenizer.from_pretrained(\"gpt2\")\n    target_text = tok.decode(np.load(DEV_NPY).astype(np.int64).tolist())\n    tws = words(target_text)\n    segs = [tws[i:i + SEG_WORDS] for i in range(0, len(tws) - 200, SEG_WORDS)]\n    print(f\"      target words={len(tws)} segments={len(segs)}\", flush=True)\n\n    print(f\"[2/7] clustering target into K={K} registers\", flush=True)\n    lab = cluster_target(segs, K)\n    share = np.bincount(lab, minlength=K) / len(lab)\n    print(\"      cluster shares:\", np.round(share, 3).tolist(), flush=True)\n\n    # per-cluster n-gram counts; global vocab shared across clusters\n    TU = [Counter() for _ in range(K)]\n    TB = [Counter() for _ in range(K)]\n    for s, c in zip(segs, lab):\n        TU[c].update(s)\n        TB[c].update(zip(s, s[1:]))\n    gu, gb = Counter(), Counter()\n    for c in range(K):\n        gu.update(TU[c]); gb.update(TB[c])\n    uni_v = [k for k, c in gu.items() if c >= 2]\n    bi_v = [k for k, c in gb.items() if c >= 3]\n    uni = {k: j for j, k in enumerate(uni_v)}\n    bi = {k: j for j, k in enumerate(bi_v)}\n    print(f\"      uni_vocab={len(uni)} bi_vocab={len(bi)}\", flush=True)\n\n    # ------------- gate + dedup --------------------------------------------\n    print(\"[3/7] quality gate\", flush=True)\n    parts = chunks(POOL_PATH, NPROC * 4)\n    with MPPool(NPROC) as p:\n        st1 = p.map(_stage1, parts)\n    rows = [r for sub in st1 for r in sub]\n    print(f\"      pool={sum(len(c) for c in parts)} passed_gate={len(rows)}\", flush=True)\n\n    seen_h, seen_m, kept = set(), set(), []\n    for i, nchar, bonus, h, m in sorted(rows, key=lambda r: -r[1]):\n        if h in seen_h or m in seen_m:\n            continue\n        seen_h.add(h); seen_m.add(m)\n        kept.append((i, nchar, bonus))\n    print(f\"      after dedup={len(kept)}\", flush=True)\n    keep_set = {i for i, _, _ in kept}\n    bonus_of = {i: b for i, _, b in kept}\n    nchar_of = {i: c for i, c, _ in kept}\n\n    # ------------- pool background model -----------------------------------\n    print(\"[4/7] pool background LM\", flush=True)\n    with MPPool(NPROC) as p:\n        bgs = p.map(_bgcount, [c[::5] for c in parts])\n    pu, pb = Counter(), Counter()\n    for cu, cb in bgs:\n        pu.update(cu); pb.update(cb)\n    Pu, Pb = sum(pu.values()), max(1, sum(pb.values()))\n    AU = AB = 1.0\n    den_u = [math.log((pu.get(k, 0) + AU) / (Pu + AU * len(uni))) for k in uni_v]\n    den_b = [math.log((pb.get(k, 0) + AB) / (Pb + AB * len(bi))) for k in bi_v]\n    p_oov_u = max(1, Pu - sum(pu.get(k, 0) for k in uni))\n    p_oov_b = max(1, Pb - sum(pb.get(k, 0) for k in bi))\n    d_oov_u = math.log(p_oov_u / (Pu + AU * len(uni)))\n    d_oov_b = math.log(p_oov_b / (Pb + AB * len(bi)))\n\n    # ------------- per-cluster weights -------------------------------------\n    print(\"[5/7] per-cluster weights\", flush=True)\n    WU, WB, OU, OB = [], [], [], []\n    for c in range(K):\n        tu, tb = TU[c], TB[c]\n        Tu, Tb = max(1, sum(tu.values())), max(1, sum(tb.values()))\n        # add-alpha on the target side too: clusters are small, and an unseen\n        # target n-gram must not be treated as impossible.\n        au, ab = 0.15, 0.05\n        wu = [math.log((tu.get(k, 0) + au) / (Tu + au * len(uni))) - den_u[j]\n              for j, k in enumerate(uni_v)]\n        wb = [math.log((tb.get(k, 0) + ab) / (Tb + ab * len(bi))) - den_b[j]\n              for j, k in enumerate(bi_v)]\n        t_oov_u = max(1, sum(v for k, v in tu.items() if k not in uni))\n        t_oov_b = max(1, sum(v for k, v in tb.items() if k not in bi))\n        WU.append(wu); WB.append(wb)\n        OU.append(math.log(t_oov_u / (Tu + au * len(uni))) - d_oov_u)\n        OB.append(math.log(t_oov_b / (Tb + ab * len(bi))) - d_oov_b)\n\n    # ------------- score ----------------------------------------------------\n    print(\"[6/7] scoring pool\", flush=True)\n    with MPPool(NPROC, initializer=_init, initargs=(uni, bi, WU, WB, OU, OB)) as p:\n        sc = p.map(_score, [(c, keep_set) for c in parts])\n    scores = [r for sub in sc for r in sub]\n    print(f\"      scored={len(scores)}\", flush=True)\n\n    # ------------- assign to arg-max cluster, quota by target share --------\n    print(\"[7/7] mixture-matched ordering\", flush=True)\n    buckets = [[] for _ in range(K)]\n    for i, s in scores:\n        c = max(range(K), key=lambda j: s[j])\n        buckets[c].append((i, s[c] + W_BONUS * bonus_of.get(i, 0.0)))\n    for c in range(K):\n        buckets[c].sort(key=lambda r: -r[1])\n        print(f\"      cluster {c}: share={share[c]:.3f} docs={len(buckets[c])}\", flush=True)\n\n    # interleave by quota so EVERY prefix matches the target mixture\n    est_tok = {i: nchar_of[i] / 4.0 for i in nchar_of}\n    ptr = [0] * K\n    emitted_tok = [0.0] * K\n    out, tot = [], 0.0\n    while tot < TARGET_EMIT_TOKENS:\n        # cluster furthest below its quota, that still has documents left\n        cand = [c for c in range(K) if ptr[c] < len(buckets[c])]\n        if not cand:\n            break\n        c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))\n        i = buckets[c][ptr[c]][0]\n        ptr[c] += 1\n        out.append(i)\n        emitted_tok[c] += est_tok[i]\n        tot += est_tok[i]\n\n    print(f\"      emitted {len(out)} ids (~{tot/1e6:.1f}M est tokens)\", flush=True)\n    dst = sys.argv[1] if len(sys.argv) > 1 else OUT_PATH\n    os.makedirs(os.path.dirname(dst), exist_ok=True)\n    json.dump(out, open(dst, \"w\"))\n    json.dump({\"share\": share.tolist(),\n               \"buckets\": [[[i, float(s)] for i, s in b[:20000]] for b in buckets],\n               \"est_tok\": est_tok, \"bonus\": bonus_of},\n              open(f\"{CACHE}/state.json\", \"w\"))\n    print(\"wrote\", dst)\n\n\nif __name__ == \"__main__\":\n    main()\n", "structuredPatch": [{"oldStart": 4, "oldLines": 30, "newStart": 4, "newLines": 42, "lines": [" ", " STATED CRITERION", " ================", "-Rank every pool document by a per-token log-likelihood ratio between two", "-bag-of-ngrams language models -- one estimated on the DISCLOSED target", "-(multi_dev.npy, decoded back to text) and one estimated on the pool itself --", "-after removing documents that fail cheap intrinsic-quality gates and", "-near-duplicate documents.", "+The disclosed evaluation target is a *mixture* of four high-quality English", "+registers (encyclopedic / general HQ web prose / news / technical Q&A).  So the", "+selection criterion has two parts, and both are estimated from the disclosed", "+dev target (`multi_dev.npy`, decoded back to text) plus the pool itself.  No", "+labels, no hand-picked ids.", " ", "-    score(d) = (1/|d|) * sum_{f in ngrams(d)} log( p_target(f) / p_pool(f) )", "-               + small intrinsic-quality bonus", "+  (A) DENSITY RATIO.  For each pool document d, a per-token log-likelihood", "+      ratio between a bag-of-ngrams model of the target and one of the pool:", " ", "-This is classic importance-resampling-style data selection (DSIR), done", "-deterministically (top-k instead of Gumbel sampling) because the token budget", "-is a hard 12M and we want the highest-density-ratio tokens.", "+          s_c(d) = 1/(2|d|) * sum_{f in uni(d) u bi(d)} log p_c(f)/p_pool(f)", " ", "-Why a *per-token* ratio and not a total: the total log-ratio scales with", "-document length and would degenerate into \"pick the longest documents\".", "+      This is importance-resampling-style selection (DSIR), made deterministic", "+      (top-k, not Gumbel sampling) because the 12M token budget is hard and we", "+      want the highest-density-ratio tokens available.  The ratio is", "+      *per-token*: a total log-ratio grows with length and would degenerate", "+      into \"pick the longest documents\".", " ", "-Three stages:", "-  1. GATE     - intrinsic quality heuristics (C4/Gopher-flavoured) + dedup.", "-  2. RATIO    - target/pool n-gram density ratio, length-normalised.", "-  3. ORDER    - emit surviving ids by descending score, i.e. the training", "-                pipeline fills its 12M budget with the highest-ratio tokens.", "+  (B) MIXTURE MATCHING.  A single global ratio is not enough.  Top-k truncation", "+      of a global score concentrates the budget on whichever register the pool", "+      happens to hold the most high-ratio material in, and starving one quarter", "+      of the target costs more cross-entropy than over-serving another gains.", "+      So the target is segmented and k-means-clustered into K registers; a", "+      ratio model p_c is fit per cluster; every document is assigned to its", "+      arg-max cluster; and the budget is filled with a per-cluster token quota", "+      PROPORTIONAL TO THAT CLUSTER'S SHARE OF THE TARGET.  The emitted order", "+      interleaves clusters by quota so that any prefix of the list -- including", "+      the 12M-token prefix the trainer actually consumes -- already matches the", "+      target mixture.", " ", "-No labels, no hand-picked ids: everything below is derived from the pool and", "-from the disclosed dev target.", "+  (C) GATES.  Only genuinely destroyed text is hard-filtered: too short,", "+      character-soup, extreme n-gram/line repetition, non-English, and", "+      navigation/menu boilerplate (detected as a page whose lines are almost", "+      all too short to be sentences).  The gates are deliberately LOOSE about", "+      markup and digits, because two of the four target registers are", "+      HTML-tagged technical Q&A and number-dense sports/news copy; the usual", "+      C4-style \"clean prose\" filters throw exactly those away.", " \"\"\"", " import json, math, os, re, sys, zlib", " from collections import Counter"]}, {"oldStart": 39, "oldLines": 87, "newStart": 51, "newLines": 75, "lines": [" CACHE = \"/tmp/curate_cache\"", " os.makedirs(CACHE, exist_ok=True)", " ", "-# how many tokens of text to emit (budget is 12M; emit ~3x for safety)", "-TARGET_EMIT_TOKENS = 36_000_000", "+TARGET_EMIT_TOKENS = int(os.environ.get(\"EMIT_TOKENS\", 40_000_000))", " NPROC = 16", "+K = int(os.environ.get(\"K\", 6))              # target registers to discover", "+SEG_WORDS = 1200                             # target segment size for clustering", "+W_BONUS = float(os.environ.get(\"W_BONUS\", \"0.5\"))", " ", " WORD_RE = re.compile(r\"[a-z0-9']+|[.,;:!?()\\\"\\-]\")", "-ALPHA_RE = re.compile(r\"[A-Za-z]\")", " ", "-# ----------------------------------------------------------------- tokenising", "+", " def words(text):", "     return WORD_RE.findall(text.lower())", " ", " ", " # ------------------------------------------------------- stage 1: quality gate", " def quality(text):", "-    \"\"\"Return (ok, bonus). Cheap intrinsic signals only -- no target leakage.\"\"\"", "+    \"\"\"(ok, bonus). Loose intrinsic gates: drop destroyed text, keep markup.\"\"\"", "     n = len(text)", "-    if n < 400 or n > 400_000:", "+    if n < 500 or n > 600_000:", "         return False, 0.0", "     w = text.split()", "     nw = len(w)", "-    if nw < 60:", "+    if nw < 80:", "         return False, 0.0", " ", "-    # mean word length: filters code dumps / letter soup / URL lists", "     mean_wl = sum(len(x) for x in w) / nw", "-    if mean_wl < 3.0 or mean_wl > 10.0:", "+    if mean_wl < 2.5 or mean_wl > 12.0:            # letter soup / URL or base64 dumps", "         return False, 0.0", " ", "-    # alphabetic density: filters tables, logs, numeric dumps", "     alpha = sum(1 for c in text if c.isalpha())", "-    if alpha / n < 0.65:", "+    if alpha / n < 0.45:                            # loose: markup & numbers survive", "         return False, 0.0", " ", "-    # digit density", "-    digits = sum(1 for c in text if c.isdigit())", "-    if digits / n > 0.10:", "+    nonascii = sum(1 for c in text if ord(c) > 127)", "+    if nonascii / n > 0.08:                         # non-English / mojibake", "         return False, 0.0", " ", "-    # sentence structure: prose has terminal punctuation", "     lines = [ln for ln in text.split(\"\\n\") if ln.strip()]", "     if not lines:", "         return False, 0.0", "-    end_punct = sum(1 for ln in lines if ln.rstrip()[-1:] in \".!?\\\"')\")", "-    end_frac = end_punct / len(lines)", "+    lw_counts = [len(ln.split()) for ln in lines]", " ", "-    # bullet / navigation boilerplate", "-    bullets = sum(1 for ln in lines if ln.lstrip()[:1] in \"|*-#>\" or ln.lstrip()[:1].isdigit())", "-    if bullets / len(lines) > 0.5:", "+    # navigation / menu / link-farm boilerplate: a page made of stubs, not", "+    # sentences. This is the single most important gate -- site chrome is", "+    # lexically rich (it scores WELL on a density ratio) but teaches nothing.", "+    long_lines = sum(1 for c in lw_counts if c >= 8)", "+    if long_lines / len(lines) < 0.25:", "         return False, 0.0", "-", "-    # word-level repetition (spam, keyword stuffing, looping templates)", "-    lw = [x.lower() for x in w]", "-    uniq = len(set(lw)) / nw", "-    if uniq < 0.28:", "+    if sum(lw_counts) / len(lines) < 5.0:", "         return False, 0.0", "-    top1 = Counter(lw).most_common(1)[0][1] / nw", "-    if top1 > 0.09:", "-        return False, 0.0", " ", "-    # duplicate-line ratio (menus, repeated footers)", "-    if len(lines) > 8:", "-        dupl = 1.0 - len(set(lines)) / len(lines)", "-        if dupl > 0.3:", "-            return False, 0.0", "-", "-    # non-English / mojibake guard", "-    nonascii = sum(1 for c in text if ord(c) > 127)", "-    if nonascii / n > 0.06:", "+    # duplicated lines: footers, repeated templates", "+    if len(lines) > 8 and (1.0 - len(set(lines)) / len(lines)) > 0.40:", "         return False, 0.0", " ", "-    # uppercase shouting", "-    upper = sum(1 for c in text if c.isupper())", "-    if upper / max(1, alpha) > 0.20:", "+    # word-level repetition / keyword stuffing", "+    lwl = [x.lower() for x in w]", "+    if len(set(lwl)) / nw < 0.22:", "         return False, 0.0", "+    if Counter(lwl).most_common(1)[0][1] / nw > 0.12:", "+        return False, 0.0", " ", "-    bonus = 0.30 * end_frac + 0.20 * min(1.0, math.log(nw / 60.0 + 1e-9) / 3.0 if nw > 60 else 0.0)", "+    # bonus: prose-ish structure (terminal punctuation on lines) + substance", "+    end_punct = sum(1 for ln in lines if ln.rstrip()[-1:] in \".!?\\\"')>\")", "+    end_frac = end_punct / len(lines)", "+    bonus = 0.30 * end_frac + 0.15 * min(1.0, nw / 800.0)", "     return True, bonus", " ", " ", " def dedup_keys(text):", "-    \"\"\"Two cheap near-duplicate fingerprints: head shingle and mid shingle.\"\"\"", "     w = text.split()", "     head = zlib.crc32(\" \".join(w[:35]).lower().encode()) & 0xFFFFFFFF", "     mid = len(w) // 2"]}, {"oldStart": 127, "oldLines": 44, "newStart": 127, "newLines": 40, "lines": ["     return head, midk", " ", " ", "-# --------------------------------------------------- worker: parse + featurise", "+# ------------------------------------------------------------------- workers", " _W = {}", " ", " ", "-def _init(uni, bi, w_uni, w_bi, oov_u, oov_b):", "-    _W[\"uni\"], _W[\"bi\"] = uni, bi", "-    _W[\"wu\"], _W[\"wb\"] = w_uni, w_bi", "-    _W[\"ou\"], _W[\"ob\"] = oov_u, oov_b", "+def _init(uni, bi, WU, WB, OU, OB):", "+    _W.update(uni=uni, bi=bi, WU=WU, WB=WB, OU=OU, OB=OB)", " ", " ", " def _stage1(lines):", "-    \"\"\"Pass 1: gate + fingerprints + a word sample for the pool background LM.\"\"\"", "     out = []", "     for line in lines:", "         r = json.loads(line)", "-        i, t = r[\"id\"], r[\"text\"]", "-        ok, bonus = quality(t)", "-        if not ok:", "-            continue", "-        h, m = dedup_keys(t)", "-        out.append((i, len(t), bonus, h, m))", "+        ok, bonus = quality(r[\"text\"])", "+        if ok:", "+            h, m = dedup_keys(r[\"text\"])", "+            out.append((r[\"id\"], len(r[\"text\"]), bonus, h, m))", "     return out", " ", " ", " def _bgcount(lines):", "     cu, cb = Counter(), Counter()", "     for line in lines:", "-        t = json.loads(line)[\"text\"]", "-        ws = words(t)", "+        ws = words(json.loads(line)[\"text\"])", "         cu.update(ws)", "         cb.update(zip(ws, ws[1:]))", "     return cu, cb", " ", " ", " def _score(payload):", "-    \"\"\"Pass 2: per-token log density ratio for kept ids.\"\"\"", "+    \"\"\"Per-token log density ratio of each kept doc against ALL K clusters.\"\"\"", "     lines, keep = payload", "-    uni, bi, wu, wb, ou, ob = _W[\"uni\"], _W[\"bi\"], _W[\"wu\"], _W[\"wb\"], _W[\"ou\"], _W[\"ob\"]", "+    uni, bi, WU, WB, OU, OB = _W[\"uni\"], _W[\"bi\"], _W[\"WU\"], _W[\"WB\"], _W[\"OU\"], _W[\"OB\"]", "+    k = len(WU)", "+    rng = range(k)", "     res = []", "     for line in lines:", "         r = json.loads(line)"]}, {"oldStart": 175, "oldLines": 55, "newStart": 171, "newLines": 105, "lines": ["         n = len(ws)", "         if n < 40:", "             continue", "-        s = 0.0", "-        for x in ws:", "-            j = uni.get(x)", "-            s += wu[j] if j is not None else ou", "-        for x in zip(ws, ws[1:]):", "-            j = bi.get(x)", "-            s += wb[j] if j is not None else ob", "-        res.append((i, s / (2 * n)))", "+        cu = Counter(ws)", "+        cb = Counter(zip(ws, ws[1:]))", "+        # collapse to (index, count) lists once, then reuse for every cluster", "+        iu = [(uni[x], c) for x, c in cu.items() if x in uni]", "+        ou = sum(c for x, c in cu.items() if x not in uni)", "+        ib = [(bi[x], c) for x, c in cb.items() if x in bi]", "+        ob = sum(c for x, c in cb.items() if x not in bi)", "+        s = [0.0] * k", "+        for c_ in rng:", "+            wu, wb = WU[c_], WB[c_]", "+            t = OU[c_] * ou + OB[c_] * ob", "+            for j, cnt in iu:", "+                t += wu[j] * cnt", "+            for j, cnt in ib:", "+                t += wb[j] * cnt", "+            s[c_] = t / (2 * n)", "+        res.append((i, s))", "     return res", " ", " ", " def chunks(path, k):", "-    \"\"\"Split the file into k contiguous line-lists.\"\"\"", "     all_lines = open(path, \"r\", encoding=\"utf-8\").readlines()", "     per = (len(all_lines) + k - 1) // k", "     return [all_lines[j:j + per] for j in range(0, len(all_lines), per)]", " ", " ", "+# ------------------------------------------------------------------- clustering", "+def cluster_target(segs, K, seed=0):", "+    \"\"\"Spherical k-means on L2-normalised top-V unigram counts of target segments.\"\"\"", "+    import numpy as np", "+    df = Counter()", "+    for s in segs:", "+        df.update(set(s))", "+    vocab = [w for w, c in df.most_common(4000) if c >= 3]", "+    vi = {w: j for j, w in enumerate(vocab)}", "+    X = np.zeros((len(segs), len(vocab)), dtype=np.float32)", "+    for r, s in enumerate(segs):", "+        for w, c in Counter(s).items():", "+            j = vi.get(w)", "+            if j is not None:", "+                X[r, j] = 1.0 + math.log(c)", "+    idf = np.log(len(segs) / (1.0 + np.array([df[w] for w in vocab], dtype=np.float32)))", "+    X *= idf", "+    X /= np.linalg.norm(X, axis=1, keepdims=True) + 1e-9", "+    rng = np.random.default_rng(seed)", "+    C = X[rng.choice(len(segs), K, replace=False)].copy()", "+    lab = np.zeros(len(segs), dtype=np.int64)", "+    for _ in range(60):", "+        lab = (X @ C.T).argmax(1)", "+        for c in range(K):", "+            m = lab == c", "+            if m.sum() == 0:                          # re-seed a dead centroid", "+                C[c] = X[rng.integers(len(segs))]", "+            else:", "+                v = X[m].mean(0)", "+                C[c] = v / (np.linalg.norm(v) + 1e-9)", "+    return lab", "+", "+", " def main():", "     import numpy as np", " ", "-    # ---------------- decode the disclosed target back to text --------------", "-    print(\"[1/6] decoding dev target\", flush=True)", "+    # ------------- decode the disclosed target, segment, cluster ------------", "+    print(\"[1/7] decoding dev target\", flush=True)", "     from transformers import AutoTokenizer", "     tok = AutoTokenizer.from_pretrained(\"gpt2\")", "-    dev = np.load(DEV_NPY).astype(np.int64)", "-    target_text = tok.decode(dev.tolist())", "-", "+    target_text = tok.decode(np.load(DEV_NPY).astype(np.int64).tolist())", "     tws = words(target_text)", "-    tu, tb = Counter(tws), Counter(zip(tws, tws[1:]))", "+    segs = [tws[i:i + SEG_WORDS] for i in range(0, len(tws) - 200, SEG_WORDS)]", "+    print(f\"      target words={len(tws)} segments={len(segs)}\", flush=True)", " ", "-    # vocabulary = target n-grams seen at least twice; hapaxes fall into the", "-    # OOV bucket, which gives the OOV weight a well-defined (non-zero) target", "-    # mass and acts as automatic smoothing.", "-    uni_v = [k for k, c in tu.items() if c >= 2]", "-    bi_v = [k for k, c in tb.items() if c >= 3]", "+    print(f\"[2/7] clustering target into K={K} registers\", flush=True)", "+    lab = cluster_target(segs, K)", "+    share = np.bincount(lab, minlength=K) / len(lab)", "+    print(\"      cluster shares:\", np.round(share, 3).tolist(), flush=True)", "+", "+    # per-cluster n-gram counts; global vocab shared across clusters", "+    TU = [Counter() for _ in range(K)]", "+    TB = [Counter() for _ in range(K)]", "+    for s, c in zip(segs, lab):", "+        TU[c].update(s)", "+        TB[c].update(zip(s, s[1:]))", "+    gu, gb = Counter(), Counter()", "+    for c in range(K):", "+        gu.update(TU[c]); gb.update(TB[c])", "+    uni_v = [k for k, c in gu.items() if c >= 2]", "+    bi_v = [k for k, c in gb.items() if c >= 3]", "     uni = {k: j for j, k in enumerate(uni_v)}", "     bi = {k: j for j, k in enumerate(bi_v)}", "-    print(f\"      target words={len(tws)} uni_vocab={len(uni)} bi_vocab={len(bi)}\", flush=True)", "+    print(f\"      uni_vocab={len(uni)} bi_vocab={len(bi)}\", flush=True)", " ", "-    print(\"[2/6] reading pool + quality gate\", flush=True)", "+    # ------------- gate + dedup --------------------------------------------", "+    print(\"[3/7] quality gate\", flush=True)", "     parts = chunks(POOL_PATH, NPROC * 4)", "     with MPPool(NPROC) as p:", "         st1 = p.map(_stage1, parts)", "     rows = [r for sub in st1 for r in sub]", "-    n_total = sum(len(c) for c in parts)", "-    print(f\"      pool={n_total} passed_gate={len(rows)}\", flush=True)", "+    print(f\"      pool={sum(len(c) for c in parts)} passed_gate={len(rows)}\", flush=True)", " ", "-    # ---------------- dedup on the two fingerprints ------------------------", "     seen_h, seen_m, kept = set(), set(), []", "     for i, nchar, bonus, h, m in sorted(rows, key=lambda r: -r[1]):", "         if h in seen_h or m in seen_m:"]}, {"oldStart": 235, "oldLines": 62, "newStart": 281, "newLines": 83, "lines": ["     bonus_of = {i: b for i, _, b in kept}", "     nchar_of = {i: c for i, c, _ in kept}", " ", "-    # ---------------- pool background n-gram model -------------------------", "-    print(\"[3/6] pool background LM (sample)\", flush=True)", "-    sample = [c[::6] for c in parts]           # ~1/6 of the pool, spread evenly", "+    # ------------- pool background model -----------------------------------", "+    print(\"[4/7] pool background LM\", flush=True)", "     with MPPool(NPROC) as p:", "-        bgs = p.map(_bgcount, sample)", "+        bgs = p.map(_bgcount, [c[::5] for c in parts])", "     pu, pb = Counter(), Counter()", "     for cu, cb in bgs:", "         pu.update(cu); pb.update(cb)", "-", "-    # ---------------- log density ratios -----------------------------------", "-    print(\"[4/6] weights\", flush=True)", "-    Tu, Tb = sum(tu.values()), max(1, sum(tb.values()))", "     Pu, Pb = sum(pu.values()), max(1, sum(pb.values()))", "-    # OOV mass = probability the model assigns to anything outside the vocab", "-    t_oov_u = sum(c for k, c in tu.items() if k not in uni)", "-    t_oov_b = sum(c for k, c in tb.items() if k not in bi)", "-    p_oov_u = Pu - sum(pu.get(k, 0) for k in uni)", "-    p_oov_b = Pb - sum(pb.get(k, 0) for k in bi)", "+    AU = AB = 1.0", "+    den_u = [math.log((pu.get(k, 0) + AU) / (Pu + AU * len(uni))) for k in uni_v]", "+    den_b = [math.log((pb.get(k, 0) + AB) / (Pb + AB * len(bi))) for k in bi_v]", "+    p_oov_u = max(1, Pu - sum(pu.get(k, 0) for k in uni))", "+    p_oov_b = max(1, Pb - sum(pb.get(k, 0) for k in bi))", "+    d_oov_u = math.log(p_oov_u / (Pu + AU * len(uni)))", "+    d_oov_b = math.log(p_oov_b / (Pb + AB * len(bi)))", " ", "-    AU, AB = 1.0, 1.0                          # add-alpha smoothing on the pool side", "-    w_uni = [0.0] * len(uni)", "-    for k, j in uni.items():", "-        w_uni[j] = math.log((tu[k] / Tu)) - math.log(((pu.get(k, 0) + AU) / (Pu + AU * len(uni))))", "-    w_bi = [0.0] * len(bi)", "-    for k, j in bi.items():", "-        w_bi[j] = math.log((tb[k] / Tb)) - math.log(((pb.get(k, 0) + AB) / (Pb + AB * len(bi))))", "-    oov_u = math.log(max(1, t_oov_u) / Tu) - math.log(max(1, p_oov_u) / (Pu + AU * len(uni)))", "-    oov_b = math.log(max(1, t_oov_b) / Tb) - math.log(max(1, p_oov_b) / (Pb + AB * len(bi)))", "-    print(f\"      oov_u={oov_u:.3f} oov_b={oov_b:.3f}\", flush=True)", "+    # ------------- per-cluster weights -------------------------------------", "+    print(\"[5/7] per-cluster weights\", flush=True)", "+    WU, WB, OU, OB = [], [], [], []", "+    for c in range(K):", "+        tu, tb = TU[c], TB[c]", "+        Tu, Tb = max(1, sum(tu.values())), max(1, sum(tb.values()))", "+        # add-alpha on the target side too: clusters are small, and an unseen", "+        # target n-gram must not be treated as impossible.", "+        au, ab = 0.15, 0.05", "+        wu = [math.log((tu.get(k, 0) + au) / (Tu + au * len(uni))) - den_u[j]", "+              for j, k in enumerate(uni_v)]", "+        wb = [math.log((tb.get(k, 0) + ab) / (Tb + ab * len(bi))) - den_b[j]", "+              for j, k in enumerate(bi_v)]", "+        t_oov_u = max(1, sum(v for k, v in tu.items() if k not in uni))", "+        t_oov_b = max(1, sum(v for k, v in tb.items() if k not in bi))", "+        WU.append(wu); WB.append(wb)", "+        OU.append(math.log(t_oov_u / (Tu + au * len(uni))) - d_oov_u)", "+        OB.append(math.log(t_oov_b / (Tb + ab * len(bi))) - d_oov_b)", " ", "-    print(\"[5/6] scoring pool\", flush=True)", "-    with MPPool(NPROC, initializer=_init,", "-                initargs=(uni, bi, w_uni, w_bi, oov_u, oov_b)) as p:", "+    # ------------- score ----------------------------------------------------", "+    print(\"[6/7] scoring pool\", flush=True)", "+    with MPPool(NPROC, initializer=_init, initargs=(uni, bi, WU, WB, OU, OB)) as p:", "         sc = p.map(_score, [(c, keep_set) for c in parts])", "     scores = [r for sub in sc for r in sub]", "     print(f\"      scored={len(scores)}\", flush=True)", " ", "-    # ---------------- final ranking ----------------------------------------", "-    W_BONUS = float(os.environ.get(\"W_BONUS\", \"1.0\"))", "-    ranked = sorted(scores, key=lambda r: -(r[1] + W_BONUS * bonus_of.get(r[0], 0.0)))", "+    # ------------- assign to arg-max cluster, quota by target share --------", "+    print(\"[7/7] mixture-matched ordering\", flush=True)", "+    buckets = [[] for _ in range(K)]", "+    for i, s in scores:", "+        c = max(range(K), key=lambda j: s[j])", "+        buckets[c].append((i, s[c] + W_BONUS * bonus_of.get(i, 0.0)))", "+    for c in range(K):", "+        buckets[c].sort(key=lambda r: -r[1])", "+        print(f\"      cluster {c}: share={share[c]:.3f} docs={len(buckets[c])}\", flush=True)", " ", "-    # emit enough ids to overfill the 12M budget (~4 chars/token)", "-    out, tot = [], 0", "-    for i, s in ranked:", "-        out.append(i)", "-        tot += nchar_of[i] / 4.0", "-        if tot >= TARGET_EMIT_TOKENS:", "+    # interleave by quota so EVERY prefix matches the target mixture", "+    est_tok = {i: nchar_of[i] / 4.0 for i in nchar_of}", "+    ptr = [0] * K", "+    emitted_tok = [0.0] * K", "+    out, tot = [], 0.0", "+    while tot < TARGET_EMIT_TOKENS:", "+        # cluster furthest below its quota, that still has documents left", "+        cand = [c for c in range(K) if ptr[c] < len(buckets[c])]", "+        if not cand:", "             break", "-    print(f\"[6/6] emitting {len(out)} ids (~{tot/1e6:.1f}M est tokens)\", flush=True)", "+        c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))", "+        i = buckets[c][ptr[c]][0]", "+        ptr[c] += 1", "+        out.append(i)", "+        emitted_tok[c] += est_tok[i]", "+        tot += est_tok[i]", " ", "+    print(f\"      emitted {len(out)} ids (~{tot/1e6:.1f}M est tokens)\", flush=True)", "     dst = sys.argv[1] if len(sys.argv) > 1 else OUT_PATH", "     os.makedirs(os.path.dirname(dst), exist_ok=True)", "     json.dump(out, open(dst, \"w\"))", "-    # side-car: full ranking, for ablations", "-    json.dump([[i, s] for i, s in ranked[:60000]], open(\"/tmp/curate_cache/ranked.json\", \"w\"))", "-    json.dump({\"bonus\": bonus_of, \"nchar\": nchar_of}, open(\"/tmp/curate_cache/meta.json\", \"w\"))", "+    json.dump({\"share\": share.tolist(),", "+               \"buckets\": [[[i, float(s)] for i, s in b[:20000]] for b in buckets],", "+               \"est_tok\": est_tok, \"bonus\": bonus_of},", "+              open(f\"{CACHE}/state.json\", \"w\"))", "     print(\"wrote\", dst)", " ", " "]}], "originalFile": null, "userModified": false}
#11
reasoning (redacted)

encrypted chain-of-thought — recorded, not exposed

toolBashCheck evals
args
{
  "command": "cat /tmp/eval_rand.log | grep -E \"HELDOUT|packed\"; echo \"--- v1:\"; grep -E \"HELDOUT|packed\" /tmp/eval_v1.log 2>/dev/null; cat /workspace/.timer/remaining_secs",
  "description": "Check evals"
}
Bash result
packed 12000000 tokens from 60000 ids -> /tmp/dev_train.npy
HELDOUT_PPL 457.994  loss 6.1269  train_s 54  /tmp/dev_train.npy
--- v1:
5079

[stdout]
packed 12000000 tokens from 60000 ids -> /tmp/dev_train.npy
HELDOUT_PPL 457.994  loss 6.1269  train_s 54  /tmp/dev_train.npy
--- v1:
5079
[metadata] {"noOutputExpected": false}
#12
narration

Random baseline: PPL 458. Running v2 curation on CPU while v1 trains on GPU.

toolBashRun v2 curation with clustering
args
{
  "command": "time python submission/curate.py /tmp/sel_v2.json 2>&1 | grep -v Warning | tail -30",
  "description": "Run v2 curation with clustering",
  "timeout": 900000
}
Bash result
[1/7] decoding dev target
      target words=815256 segments=680
[2/7] clustering target into K=6 registers
      cluster shares: [0.196, 0.047, 0.125, 0.222, 0.21, 0.2]
      uni_vocab=23049 bi_vocab=47701
[3/7] quality gate
      pool=182016 passed_gate=116697
      after dedup=113970
[4/7] pool background LM
[5/7] per-cluster weights
[6/7] scoring pool
      scored=113970
[7/7] mixture-matched ordering
      cluster 0: share=0.196 docs=9974
      cluster 1: share=0.047 docs=2
      cluster 2: share=0.125 docs=18409
      cluster 3: share=0.222 docs=77731
      cluster 4: share=0.210 docs=6057
      cluster 5: share=0.200 docs=1797
      emitted 38939 ids (~40.0M est tokens)
wrote /tmp/sel_v2.json

real	0m45.876s
user	3m47.010s
sys	0m14.340s

[stdout]
[1/7] decoding dev target
      target words=815256 segments=680
[2/7] clustering target into K=6 registers
      cluster shares: [0.196, 0.047, 0.125, 0.222, 0.21, 0.2]
      uni_vocab=23049 bi_vocab=47701
[3/7] quality gate
      pool=182016 passed_gate=116697
      after dedup=113970
[4/7] pool background LM
[5/7] per-cluster weights
[6/7] scoring pool
      scored=113970
[7/7] mixture-matched ordering
      cluster 0: share=0.196 docs=9974
      cluster 1: share=0.047 docs=2
      cluster 2: share=0.125 docs=18409
      cluster 3: share=0.222 docs=77731
      cluster 4: share=0.210 docs=6057
      cluster 5: share=0.200 docs=1797
      emitted 38939 ids (~40.0M est tokens)
wrote /tmp/sel_v2.json

real	0m45.876s
user	3m47.010s
sys	0m14.340s
[metadata] {"noOutputExpected": false}
#13
narration

Cluster assignment is degenerate (scale mismatch across clusters). Fixing with z-scored per-cluster ranking:

toolEdit
args
{
  "replace_all": false,
  "file_path": "/workspace/submission/curate.py",
  "old_string": "    # ------------- assign to arg-max cluster, quota by target share --------\n    print(\"[7/7] mixture-matched ordering\", flush=True)\n    buckets = [[] for _ in range(K)]\n    for i, s in scores:\n        c = max(range(K), key=lambda j: s[j])\n        buckets[c].append((i, s[c] + W_BONUS * bonus_of.get(i, 0.0)))\n    for c in range(K):\n        buckets[c].sort(key=lambda r: -r[1])\n        print(f\"      cluster {c}: share={share[c]:.3f} docs={len(buckets[c])}\", flush=True)\n\n    # interleave by quota so EVERY prefix matches the target mixture\n    est_tok = {i: nchar_of[i] / 4.0 for i in nchar_of}\n    ptr = [0] * K\n    emitted_tok = [0.0] * K\n    out, tot = [], 0.0\n    while tot < TARGET_EMIT_TOKENS:\n        # cluster furthest below its quota, that still has documents left\n        cand = [c for c in range(K) if ptr[c] < len(buckets[c])]\n        if not cand:\n            break\n        c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))\n        i = buckets[c][ptr[c]][0]\n        ptr[c] += 1\n        out.append(i)\n        emitted_tok[c] += est_tok[i]\n        tot += est_tok[i]",
  "new_string": "    # ------------- per-cluster z-scored ranking, quota by target share -----\n    # Raw s_c are NOT comparable across clusters: clusters differ in size, so\n    # their smoothing mass and hence their score offsets/scales differ. An\n    # arg-max over raw s_c collapses almost every document into one cluster.\n    # Standardising each cluster's score over the pool makes \"how much more\n    # cluster-c-like than average is this document\" comparable across c.\n    print(\"[7/7] mixture-matched ordering\", flush=True)\n    ids = [i for i, _ in scores]\n    S = np.array([s for _, s in scores], dtype=np.float64)          # (N, K)\n    Z = (S - S.mean(0)) / (S.std(0) + 1e-9)\n    bon = np.array([bonus_of.get(i, 0.0) for i in ids])\n    est_tok = {i: nchar_of[i] / 4.0 for i in nchar_of}\n\n    # each cluster ranks the WHOLE pool by its own affinity; quotas are filled\n    # from these lists, skipping documents another cluster already took.\n    order = [np.argsort(-(Z[:, c] + W_BONUS * bon)) for c in range(K)]\n    ptr = [0] * K\n    emitted_tok = [0.0] * K\n    taken = set()\n    out, tot = [], 0.0\n    while tot < TARGET_EMIT_TOKENS:\n        cand = [c for c in range(K) if ptr[c] < len(ids)]\n        if not cand:\n            break\n        c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))\n        # advance past documents already claimed by another register\n        while ptr[c] < len(ids) and ids[order[c][ptr[c]]] in taken:\n            ptr[c] += 1\n        if ptr[c] >= len(ids):\n            continue\n        i = ids[order[c][ptr[c]]]\n        ptr[c] += 1\n        taken.add(i)\n        out.append(i)\n        emitted_tok[c] += est_tok[i]\n        tot += est_tok[i]\n    for c in range(K):\n        print(f\"      cluster {c}: share={share[c]:.3f} \"\n              f\"tok={emitted_tok[c]/1e6:.1f}M\", flush=True)\n    np.save(f\"{CACHE}/Z.npy\", Z)\n    json.dump(ids, open(f\"{CACHE}/ids.json\", \"w\"))\n    json.dump({\"share\": share.tolist(), \"bonus\": bonus_of, \"nchar\": nchar_of},\n              open(f\"{CACHE}/meta.json\", \"w\"))"
}
Edit result
The file /workspace/submission/curate.py has been updated successfully. (file state is current in your context — no need to Read it back)

[metadata] {"filePath": "/workspace/submission/curate.py", "oldString": "    # ------------- assign to arg-max cluster, quota by target share --------\n    print(\"[7/7] mixture-matched ordering\", flush=True)\n    buckets = [[] for _ in range(K)]\n    for i, s in scores:\n        c = max(range(K), key=lambda j: s[j])\n        buckets[c].append((i, s[c] + W_BONUS * bonus_of.get(i, 0.0)))\n    for c in range(K):\n        buckets[c].sort(key=lambda r: -r[1])\n        print(f\"      cluster {c}: share={share[c]:.3f} docs={len(buckets[c])}\", flush=True)\n\n    # interleave by quota so EVERY prefix matches the target mixture\n    est_tok = {i: nchar_of[i] / 4.0 for i in nchar_of}\n    ptr = [0] * K\n    emitted_tok = [0.0] * K\n    out, tot = [], 0.0\n    while tot < TARGET_EMIT_TOKENS:\n        # cluster furthest below its quota, that still has documents left\n        cand = [c for c in range(K) if ptr[c] < len(buckets[c])]\n        if not cand:\n            break\n        c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))\n        i = buckets[c][ptr[c]][0]\n        ptr[c] += 1\n        out.append(i)\n        emitted_tok[c] += est_tok[i]\n        tot += est_tok[i]", "newString": "    # ------------- per-cluster z-scored ranking, quota by target share -----\n    # Raw s_c are NOT comparable across clusters: clusters differ in size, so\n    # their smoothing mass and hence their score offsets/scales differ. An\n    # arg-max over raw s_c collapses almost every document into one cluster.\n    # Standardising each cluster's score over the pool makes \"how much more\n    # cluster-c-like than average is this document\" comparable across c.\n    print(\"[7/7] mixture-matched ordering\", flush=True)\n    ids = [i for i, _ in scores]\n    S = np.array([s for _, s in scores], dtype=np.float64)          # (N, K)\n    Z = (S - S.mean(0)) / (S.std(0) + 1e-9)\n    bon = np.array([bonus_of.get(i, 0.0) for i in ids])\n    est_tok = {i: nchar_of[i] / 4.0 for i in nchar_of}\n\n    # each cluster ranks the WHOLE pool by its own affinity; quotas are filled\n    # from these lists, skipping documents another cluster already took.\n    order = [np.argsort(-(Z[:, c] + W_BONUS * bon)) for c in range(K)]\n    ptr = [0] * K\n    emitted_tok = [0.0] * K\n    taken = set()\n    out, tot = [], 0.0\n    while tot < TARGET_EMIT_TOKENS:\n        cand = [c for c in range(K) if ptr[c] < len(ids)]\n        if not cand:\n            break\n        c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))\n        # advance past documents already claimed by another register\n        while ptr[c] < len(ids) and ids[order[c][ptr[c]]] in taken:\n            ptr[c] += 1\n        if ptr[c] >= len(ids):\n            continue\n        i = ids[order[c][ptr[c]]]\n        ptr[c] += 1\n        taken.add(i)\n        out.append(i)\n        emitted_tok[c] += est_tok[i]\n        tot += est_tok[i]\n    for c in range(K):\n        print(f\"      cluster {c}: share={share[c]:.3f} \"\n              f\"tok={emitted_tok[c]/1e6:.1f}M\", flush=True)\n    np.save(f\"{CACHE}/Z.npy\", Z)\n    json.dump(ids, open(f\"{CACHE}/ids.json\", \"w\"))\n    json.dump({\"share\": share.tolist(), \"bonus\": bonus_of, \"nchar\": nchar_of},\n              open(f\"{CACHE}/meta.json\", \"w\"))", "originalFile": null, "structuredPatch": [{"oldStart": 323, "oldLines": 32, "newStart": 323, "newLines": 49, "lines": ["     scores = [r for sub in sc for r in sub]", "     print(f\"      scored={len(scores)}\", flush=True)", " ", "-    # ------------- assign to arg-max cluster, quota by target share --------", "+    # ------------- per-cluster z-scored ranking, quota by target share -----", "+    # Raw s_c are NOT comparable across clusters: clusters differ in size, so", "+    # their smoothing mass and hence their score offsets/scales differ. An", "+    # arg-max over raw s_c collapses almost every document into one cluster.", "+    # Standardising each cluster's score over the pool makes \"how much more", "+    # cluster-c-like than average is this document\" comparable across c.", "     print(\"[7/7] mixture-matched ordering\", flush=True)", "-    buckets = [[] for _ in range(K)]", "-    for i, s in scores:", "-        c = max(range(K), key=lambda j: s[j])", "-        buckets[c].append((i, s[c] + W_BONUS * bonus_of.get(i, 0.0)))", "-    for c in range(K):", "-        buckets[c].sort(key=lambda r: -r[1])", "-        print(f\"      cluster {c}: share={share[c]:.3f} docs={len(buckets[c])}\", flush=True)", "-", "-    # interleave by quota so EVERY prefix matches the target mixture", "+    ids = [i for i, _ in scores]", "+    S = np.array([s for _, s in scores], dtype=np.float64)          # (N, K)", "+    Z = (S - S.mean(0)) / (S.std(0) + 1e-9)", "+    bon = np.array([bonus_of.get(i, 0.0) for i in ids])", "     est_tok = {i: nchar_of[i] / 4.0 for i in nchar_of}", "+", "+    # each cluster ranks the WHOLE pool by its own affinity; quotas are filled", "+    # from these lists, skipping documents another cluster already took.", "+    order = [np.argsort(-(Z[:, c] + W_BONUS * bon)) for c in range(K)]", "     ptr = [0] * K", "     emitted_tok = [0.0] * K", "+    taken = set()", "     out, tot = [], 0.0", "     while tot < TARGET_EMIT_TOKENS:", "-        # cluster furthest below its quota, that still has documents left", "-        cand = [c for c in range(K) if ptr[c] < len(buckets[c])]", "+        cand = [c for c in range(K) if ptr[c] < len(ids)]", "         if not cand:", "             break", "         c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))", "-        i = buckets[c][ptr[c]][0]", "+        # advance past documents already claimed by another register", "+        while ptr[c] < len(ids) and ids[order[c][ptr[c]]] in taken:", "+            ptr[c] += 1", "+        if ptr[c] >= len(ids):", "+            continue", "+        i = ids[order[c][ptr[c]]]", "         ptr[c] += 1", "+        taken.add(i)", "         out.append(i)", "         emitted_tok[c] += est_tok[i]", "         tot += est_tok[i]", "+    for c in range(K):", "+        print(f\"      cluster {c}: share={share[c]:.3f} \"", "+              f\"tok={emitted_tok[c]/1e6:.1f}M\", flush=True)", "+    np.save(f\"{CACHE}/Z.npy\", Z)", "+    json.dump(ids, open(f\"{CACHE}/ids.json\", \"w\"))", "+    json.dump({\"share\": share.tolist(), \"bonus\": bonus_of, \"nchar\": nchar_of},", "+              open(f\"{CACHE}/meta.json\", \"w\"))", " ", "     print(f\"      emitted {len(out)} ids (~{tot/1e6:.1f}M est tokens)\", flush=True)", "     dst = sys.argv[1] if len(sys.argv) > 1 else OUT_PATH"]}], "userModified": false, "replaceAll": false}
#14
reasoning (redacted)

encrypted chain-of-thought — recorded, not exposed

toolEdit
args
{
  "replace_all": false,
  "file_path": "/workspace/submission/curate.py",
  "old_string": "    json.dump(out, open(dst, \"w\"))\n    json.dump({\"share\": share.tolist(),\n               \"buckets\": [[[i, float(s)] for i, s in b[:20000]] for b in buckets],\n               \"est_tok\": est_tok, \"bonus\": bonus_of},\n              open(f\"{CACHE}/state.json\", \"w\"))\n    print(\"wrote\", dst)",
  "new_string": "    json.dump(out, open(dst, \"w\"))\n    print(\"wrote\", dst)"
}
Edit result
The file /workspace/submission/curate.py has been updated successfully. (file state is current in your context — no need to Read it back)

[metadata] {"filePath": "/workspace/submission/curate.py", "oldString": "    json.dump(out, open(dst, \"w\"))\n    json.dump({\"share\": share.tolist(),\n               \"buckets\": [[[i, float(s)] for i, s in b[:20000]] for b in buckets],\n               \"est_tok\": est_tok, \"bonus\": bonus_of},\n              open(f\"{CACHE}/state.json\", \"w\"))\n    print(\"wrote\", dst)", "newString": "    json.dump(out, open(dst, \"w\"))\n    print(\"wrote\", dst)", "originalFile": null, "structuredPatch": [{"oldStart": 371, "oldLines": 10, "newStart": 371, "newLines": 6, "lines": ["     dst = sys.argv[1] if len(sys.argv) > 1 else OUT_PATH", "     os.makedirs(os.path.dirname(dst), exist_ok=True)", "     json.dump(out, open(dst, \"w\"))", "-    json.dump({\"share\": share.tolist(),", "-               \"buckets\": [[[i, float(s)] for i, s in b[:20000]] for b in buckets],", "-               \"est_tok\": est_tok, \"bonus\": bonus_of},", "-              open(f\"{CACHE}/state.json\", \"w\"))", "     print(\"wrote\", dst)", " ", " "]}], "userModified": false, "replaceAll": false}
#15
reasoning (redacted)

encrypted chain-of-thought — recorded, not exposed

toolBashRun v3 curation
args
{
  "command": "grep -E \"HELDOUT\" /tmp/eval_v1.log 2>/dev/null || echo \"v1 still running\"; time python submission/curate.py /tmp/sel_v3.json 2>&1 | grep -v Warning | tail -20",
  "description": "Run v3 curation",
  "timeout": 900000
}
Bash result
HELDOUT_PPL 544.877  loss 6.3006  train_s 55  /tmp/dev_train.npy
      target words=815256 segments=680
[2/7] clustering target into K=6 registers
      cluster shares: [0.196, 0.047, 0.125, 0.222, 0.21, 0.2]
      uni_vocab=23049 bi_vocab=47701
[3/7] quality gate
      pool=182016 passed_gate=116697
      after dedup=113970
[4/7] pool background LM
[5/7] per-cluster weights
[6/7] scoring pool
      scored=113970
[7/7] mixture-matched ordering
      cluster 0: share=0.196 tok=7.8M
      cluster 1: share=0.047 tok=1.9M
      cluster 2: share=0.125 tok=5.0M
      cluster 3: share=0.222 tok=8.9M
      cluster 4: share=0.210 tok=8.4M
      cluster 5: share=0.200 tok=8.0M
      emitted 43028 ids (~40.0M est tokens)
wrote /tmp/sel_v3.json

real	0m45.155s
user	4m2.050s
sys	0m13.230s

[stdout]
HELDOUT_PPL 544.877  loss 6.3006  train_s 55  /tmp/dev_train.npy
      target words=815256 segments=680
[2/7] clustering target into K=6 registers
      cluster shares: [0.196, 0.047, 0.125, 0.222, 0.21, 0.2]
      uni_vocab=23049 bi_vocab=47701
[3/7] quality gate
      pool=182016 passed_gate=116697
      after dedup=113970
[4/7] pool background LM
[5/7] per-cluster weights
[6/7] scoring pool
      scored=113970
[7/7] mixture-matched ordering
      cluster 0: share=0.196 tok=7.8M
      cluster 1: share=0.047 tok=1.9M
      cluster 2: share=0.125 tok=5.0M
      cluster 3: share=0.222 tok=8.9M
      cluster 4: share=0.210 tok=8.4M
      cluster 5: share=0.200 tok=8.0M
      emitted 43028 ids (~40.0M est tokens)
wrote /tmp/sel_v3.json

real	0m45.155s
user	4m2.050s
sys	0m13.230s
[metadata] {"noOutputExpected": false}
#16
narration

Key result: *v1 (strict top-k density ratio) = PPL 545, worse than random's 458.* Aggressive top-k selection collapses diversity — the trainer consumes only a prefix, so ranking hard = selecting just 6% of the pool. Let me build variants that keep breadth.

toolWrite
args
{
  "file_path": "/workspace/ablate.py",
  "content": "\"\"\"Fast variant generator from curate.py's cached score matrix.\"\"\"\nimport json, sys, numpy as np, random\n\nCACHE = \"/tmp/curate_cache\"\nZ = np.load(f\"{CACHE}/Z.npy\")                      # (N,K) z-scored cluster affinity\nids = json.load(open(f\"{CACHE}/ids.json\"))\nmeta = json.load(open(f\"{CACHE}/meta.json\"))\nshare = np.array(meta[\"share\"]); K = Z.shape[1]\nbonus = np.array([meta[\"bonus\"][str(i)] for i in ids])\nntok = np.array([meta[\"nchar\"][str(i)] / 4.0 for i in ids])\n\nmode = sys.argv[1]\nout_path = sys.argv[2]\nEMIT = 40_000_000\nrng = random.Random(1234)\n\nif mode == \"gate_shuf\":\n    # gates + dedup only; random order => 12M tokens is a uniform sample of\n    # everything that survived the quality gate. Isolates the gate's value.\n    idx = list(range(len(ids))); rng.shuffle(idx)\n    sel = [ids[j] for j in idx]\n\nelif mode == \"ratio_frac\":\n    # keep the top FRAC of each register by affinity, then SHUFFLE inside the\n    # register and interleave by target share. Preserves breadth while\n    # discarding the off-target tail.\n    FRAC = float(sys.argv[3])\n    W_B = float(sys.argv[4]) if len(sys.argv) > 4 else 0.5\n    pools = []\n    for c in range(K):\n        order = np.argsort(-(Z[:, c] + W_B * bonus))\n        n = int(FRAC * len(ids))\n        p = list(order[:n]); rng.shuffle(p)\n        pools.append(p)\n    ptr = [0] * K; got = [0.0] * K; taken = set(); sel = []; tot = 0.0\n    while tot < EMIT:\n        cand = [c for c in range(K) if ptr[c] < len(pools[c])]\n        if not cand: break\n        c = min(cand, key=lambda j: got[j] / max(1e-9, share[j]))\n        while ptr[c] < len(pools[c]) and ids[pools[c][ptr[c]]] in taken:\n            ptr[c] += 1\n        if ptr[c] >= len(pools[c]): continue\n        j = pools[c][ptr[c]]; ptr[c] += 1\n        taken.add(ids[j]); sel.append(ids[j]); got[c] += ntok[j]; tot += ntok[j]\n\nelif mode == \"best_frac\":\n    # register-agnostic: score = best-matching-register affinity; keep top FRAC,\n    # shuffle.\n    FRAC = float(sys.argv[3])\n    W_B = float(sys.argv[4]) if len(sys.argv) > 4 else 0.5\n    q = Z.max(1) + W_B * bonus\n    order = np.argsort(-q)[:int(FRAC * len(ids))]\n    p = list(order); rng.shuffle(p)\n    sel = [ids[j] for j in p]\n\njson.dump(sel, open(out_path, \"w\"))\ntk = sum(meta[\"nchar\"][str(i)] / 4.0 for i in sel)\nprint(f\"{mode} -> {out_path}: {len(sel)} ids ~{tk/1e6:.1f}M est tokens\")\n"
}
Write result
File created successfully at: /workspace/ablate.py (file state is current in your context — no need to Read it back)

[metadata] {"type": "create", "filePath": "/workspace/ablate.py", "content": "\"\"\"Fast variant generator from curate.py's cached score matrix.\"\"\"\nimport json, sys, numpy as np, random\n\nCACHE = \"/tmp/curate_cache\"\nZ = np.load(f\"{CACHE}/Z.npy\")                      # (N,K) z-scored cluster affinity\nids = json.load(open(f\"{CACHE}/ids.json\"))\nmeta = json.load(open(f\"{CACHE}/meta.json\"))\nshare = np.array(meta[\"share\"]); K = Z.shape[1]\nbonus = np.array([meta[\"bonus\"][str(i)] for i in ids])\nntok = np.array([meta[\"nchar\"][str(i)] / 4.0 for i in ids])\n\nmode = sys.argv[1]\nout_path = sys.argv[2]\nEMIT = 40_000_000\nrng = random.Random(1234)\n\nif mode == \"gate_shuf\":\n    # gates + dedup only; random order => 12M tokens is a uniform sample of\n    # everything that survived the quality gate. Isolates the gate's value.\n    idx = list(range(len(ids))); rng.shuffle(idx)\n    sel = [ids[j] for j in idx]\n\nelif mode == \"ratio_frac\":\n    # keep the top FRAC of each register by affinity, then SHUFFLE inside the\n    # register and interleave by target share. Preserves breadth while\n    # discarding the off-target tail.\n    FRAC = float(sys.argv[3])\n    W_B = float(sys.argv[4]) if len(sys.argv) > 4 else 0.5\n    pools = []\n    for c in range(K):\n        order = np.argsort(-(Z[:, c] + W_B * bonus))\n        n = int(FRAC * len(ids))\n        p = list(order[:n]); rng.shuffle(p)\n        pools.append(p)\n    ptr = [0] * K; got = [0.0] * K; taken = set(); sel = []; tot = 0.0\n    while tot < EMIT:\n        cand = [c for c in range(K) if ptr[c] < len(pools[c])]\n        if not cand: break\n        c = min(cand, key=lambda j: got[j] / max(1e-9, share[j]))\n        while ptr[c] < len(pools[c]) and ids[pools[c][ptr[c]]] in taken:\n            ptr[c] += 1\n        if ptr[c] >= len(pools[c]): continue\n        j = pools[c][ptr[c]]; ptr[c] += 1\n        taken.add(ids[j]); sel.append(ids[j]); got[c] += ntok[j]; tot += ntok[j]\n\nelif mode == \"best_frac\":\n    # register-agnostic: score = best-matching-register affinity; keep top FRAC,\n    # shuffle.\n    FRAC = float(sys.argv[3])\n    W_B = float(sys.argv[4]) if len(sys.argv) > 4 else 0.5\n    q = Z.max(1) + W_B * bonus\n    order = np.argsort(-q)[:int(FRAC * len(ids))]\n    p = list(order); rng.shuffle(p)\n    sel = [ids[j] for j in p]\n\njson.dump(sel, open(out_path, \"w\"))\ntk = sum(meta[\"nchar\"][str(i)] / 4.0 for i in sel)\nprint(f\"{mode} -> {out_path}: {len(sel)} ids ~{tk/1e6:.1f}M est tokens\")\n", "structuredPatch": [], "originalFile": null, "userModified": false}
#17
reasoning (redacted)

encrypted chain-of-thought — recorded, not exposed

toolBashGenerate ablation variants
args
{
  "command": "python ablate.py gate_shuf /tmp/sel_gate.json && python ablate.py ratio_frac /tmp/sel_rf35.json 0.35 0.5 && python ablate.py ratio_frac /tmp/sel_rf12.json 0.12 0.5 && cat .timer/remaining_secs",
  "description": "Generate ablation variants"
}
Bash result
gate_shuf -> /tmp/sel_gate.json: 113970 ids ~108.1M est tokens
ratio_frac -> /tmp/sel_rf35.json: 40024 ids ~40.0M est tokens
ratio_frac -> /tmp/sel_rf12.json: 42145 ids ~40.0M est tokens
4889

[stdout]
gate_shuf -> /tmp/sel_gate.json: 113970 ids ~108.1M est tokens
ratio_frac -> /tmp/sel_rf35.json: 40024 ids ~40.0M est tokens
ratio_frac -> /tmp/sel_rf12.json: 42145 ids ~40.0M est tokens
4889
[metadata] {"noOutputExpected": false}
#18
reasoning (redacted)

encrypted chain-of-thought — recorded, not exposed

toolWrite
args
{
  "file_path": "/workspace/batch_eval.py",
  "content": "\"\"\"Pack several selections in one pool read, then train each sequentially.\"\"\"\nimport json, sys, os, subprocess, numpy as np\nfrom transformers import AutoTokenizer\n\nBUDGET = 12_000_000\nsels = sys.argv[1:]\ntok = AutoTokenizer.from_pretrained(\"gpt2\"); EOS = tok.eos_token_id\ntext = {}\nfor line in open(\"/workspace/data/pool.jsonl\"):\n    r = json.loads(line); text[r[\"id\"]] = r[\"text\"]\nprint(\"pool loaded\", flush=True)\n\ncache = {}\nfor sp in sels:\n    name = os.path.basename(sp).replace(\".json\", \"\")\n    npy = f\"/tmp/tr_{name}.npy\"\n    if not os.path.exists(npy):\n        parts, tot = [], 0\n        for i in json.load(open(sp)):\n            ids = cache.get(i)\n            if ids is None:\n                ids = tok(text[i], add_special_tokens=False).input_ids\n            parts.extend(ids); parts.append(EOS); tot += len(ids) + 1\n            if tot >= BUDGET: break\n        np.save(npy, np.array(parts[:BUDGET], dtype=np.uint16))\n        print(f\"packed {name} {min(tot,BUDGET)}\", flush=True)\n    r = subprocess.run([\"python3\", \"/workspace/train_nano.py\", \"--train_npy\", npy,\n                        \"--heldout_npy\", \"/workspace/data/multi_dev.npy\",\n                        \"--out_json\", f\"/tmp/res_{name}.json\", \"--max_iters\", \"3000\",\n                        \"--seed\", \"1337\"], capture_output=True, text=True)\n    line = [l for l in r.stdout.split(\"\\n\") if \"HELDOUT_PPL\" in l]\n    print(\"RESULT\", name, line[0] if line else r.stdout[-300:] + r.stderr[-300:], flush=True)\n"
}
Write result
File created successfully at: /workspace/batch_eval.py (file state is current in your context — no need to Read it back)

[metadata] {"type": "create", "filePath": "/workspace/batch_eval.py", "content": "\"\"\"Pack several selections in one pool read, then train each sequentially.\"\"\"\nimport json, sys, os, subprocess, numpy as np\nfrom transformers import AutoTokenizer\n\nBUDGET = 12_000_000\nsels = sys.argv[1:]\ntok = AutoTokenizer.from_pretrained(\"gpt2\"); EOS = tok.eos_token_id\ntext = {}\nfor line in open(\"/workspace/data/pool.jsonl\"):\n    r = json.loads(line); text[r[\"id\"]] = r[\"text\"]\nprint(\"pool loaded\", flush=True)\n\ncache = {}\nfor sp in sels:\n    name = os.path.basename(sp).replace(\".json\", \"\")\n    npy = f\"/tmp/tr_{name}.npy\"\n    if not os.path.exists(npy):\n        parts, tot = [], 0\n        for i in json.load(open(sp)):\n            ids = cache.get(i)\n            if ids is None:\n                ids = tok(text[i], add_special_tokens=False).input_ids\n            parts.extend(ids); parts.append(EOS); tot += len(ids) + 1\n            if tot >= BUDGET: break\n        np.save(npy, np.array(parts[:BUDGET], dtype=np.uint16))\n        print(f\"packed {name} {min(tot,BUDGET)}\", flush=True)\n    r = subprocess.run([\"python3\", \"/workspace/train_nano.py\", \"--train_npy\", npy,\n                        \"--heldout_npy\", \"/workspace/data/multi_dev.npy\",\n                        \"--out_json\", f\"/tmp/res_{name}.json\", \"--max_iters\", \"3000\",\n                        \"--seed\", \"1337\"], capture_output=True, text=True)\n    line = [l for l in r.stdout.split(\"\\n\") if \"HELDOUT_PPL\" in l]\n    print(\"RESULT\", name, line[0] if line else r.stdout[-300:] + r.stderr[-300:], flush=True)\n", "structuredPatch": [], "originalFile": null, "userModified": false}
#19
reasoning (redacted)

encrypted chain-of-thought — recorded, not exposed

toolBashLaunch batch eval of 4 variants
args
{
  "command": "TOKENIZERS_PARALLELISM=false nohup python batch_eval.py /tmp/sel_gate.json /tmp/sel_rf35.json /tmp/sel_rf12.json /tmp/sel_v3.json > /tmp/batch1.log 2>&1 &\necho launched; sleep 5; cat .timer/remaining_secs",
  "description": "Launch batch eval of 4 variants"
}
Bash result
launched
4859

[stdout]
launched
4859
[metadata] {"noOutputExpected": false}
#20
narration

While that runs, let me check whether the pool contains the two distinctive surface styles in the target (spaced-punctuation Wikipedia, HTML-tagged Q&A) — PPL is dominated by whichever register is worst covered.

toolBashScan pool for HTML and spaced-punctuation styles
args
{
  "command": "python -c \"\nimport json,re\nn=0; html=0; spaced=0; both=[]\nsp=re.compile(r' [,.;] ')\nfor line in open('data/pool.jsonl'):\n    r=json.loads(line); t=r['text']; n+=1\n    h = ('</p>' in t) or ('<li>' in t) or ('<code>' in t) or ('<pre>' in t)\n    s = len(sp.findall(t)) > max(5, len(t)/700)\n    if h: html+=1\n    if s: spaced+=1\n    if h and len(both)<2: both.append((r['id'],t[:300]))\nprint('docs',n,'html',html,'spaced',spaced)\nfor i,t in both: print('---',i,repr(t))\n\"",
  "description": "Scan pool for HTML and spaced-punctuation styles"
}
Bash result
docs 182016 html 203 spaced 764
--- 2033 'WATCH #FORUM Full Length & Free For a Limited Time videos by Lorene Voskinarian October 24th, 2012No Comments Tweet Forum is making their latest film #FORUM free to watch for a very limited time. Check it out below. Related posts:Win a Trip to the Streets at Seven SpringsBurton Shakes Up Brand Lineu'
--- 4313 'Methods | Statistics | Clinical | Educational | Industrial | Professional items | World psychology |\nThe Semantic Web is an evolving extension of the World Wide Web in which the semantics of information and services on the web is defined, making it possible for the web to understand and satisfy the '

[stdout]
docs 182016 html 203 spaced 764
--- 2033 'WATCH #FORUM Full Length & Free For a Limited Time videos by Lorene Voskinarian October 24th, 2012No Comments Tweet Forum is making their latest film #FORUM free to watch for a very limited time. Check it out below. Related posts:Win a Trip to the Streets at Seven SpringsBurton Shakes Up Brand Lineu'
--- 4313 'Methods | Statistics | Clinical | Educational | Industrial | Professional items | World psychology |\nThe Semantic Web is an evolving extension of the World Wide Web in which the semantics of information and services on the web is defined, making it possible for the web to understand and satisfy the '
[metadata] {"noOutputExpected": false}
#21
narration

Pool has essentially no HTML-Q&A (203 docs) or spaced-punctuation Wikipedia (764) — the pool is homogeneous raw web, so style-matching those registers isn't available. Checking eval results:

toolBashCheck batch eval results
args
{
  "command": "sleep 150; cat /tmp/batch1.log | grep -E \"RESULT|packed|loaded\"; cat .timer/remaining_secs",
  "description": "Check batch eval results",
  "timeout": 400000
}
Bash result
pool loaded
packed sel_gate 12000000
RESULT sel_gate HELDOUT_PPL 422.876  loss 6.0471  train_s 54  /tmp/tr_sel_gate.npy
packed sel_rf35 12000000
RESULT sel_rf35 HELDOUT_PPL 461.740  loss 6.1350  train_s 54  /tmp/tr_sel_rf35.npy
4669

[stdout]
pool loaded
packed sel_gate 12000000
RESULT sel_gate HELDOUT_PPL 422.876  loss 6.0471  train_s 54  /tmp/tr_sel_gate.npy
packed sel_rf35 12000000
RESULT sel_rf35 HELDOUT_PPL 461.740  loss 6.1350  train_s 54  /tmp/tr_sel_rf35.npy
4669
[metadata] {"noOutputExpected": false}
#22
narration

Decisive: gates+dedup+shuffle = 422.9 (beats random 458), while ratio-targeting = 461.7. The density ratio hurts — breadth matters more than on-target density. Pivoting: use the ratio only to trim the off-target tail, and tune the gates.

toolBashGenerate tail-drop and length variants
args
{
  "command": "python ablate.py best_frac /tmp/sel_bf75.json 0.75 0.5 && python ablate.py best_frac /tmp/sel_bf50.json 0.50 0.5 && python -c \"\nimport json,random,numpy as np\n# length-preference variant: gate+dedup, but drop docs under 250 words, shuffled\nmeta=json.load(open('/tmp/curate_cache/meta.json')); ids=json.load(open('/tmp/curate_cache/ids.json'))\nkeep=[i for i in ids if meta['nchar'][str(i)]/4.0>=350]\nrandom.Random(7).shuffle(keep); json.dump(keep,open('/tmp/sel_long.json','w'))\nprint('long:',len(keep), sum(meta['nchar'][str(i)]/4.0 for i in keep)/1e6)\n\"",
  "description": "Generate tail-drop and length variants"
}
Bash result
best_frac -> /tmp/sel_bf75.json: 85477 ids ~81.6M est tokens
best_frac -> /tmp/sel_bf50.json: 56985 ids ~53.9M est tokens
long: 77639 99.73858725

[stdout]
best_frac -> /tmp/sel_bf75.json: 85477 ids ~81.6M est tokens
best_frac -> /tmp/sel_bf50.json: 56985 ids ~53.9M est tokens
long: 77639 99.73858725
[metadata] {"noOutputExpected": false}
#23
narration

Now a stricter-gate variant. Let me parametrize the gate:

toolEdit
args
{
  "replace_all": false,
  "file_path": "/workspace/submission/curate.py",
  "old_string": "# ------------------------------------------------------- stage 1: quality gate\ndef quality(text):\n    \"\"\"(ok, bonus). Loose intrinsic gates: drop destroyed text, keep markup.\"\"\"\n    n = len(text)\n    if n < 500 or n > 600_000:\n        return False, 0.0\n    w = text.split()\n    nw = len(w)\n    if nw < 80:\n        return False, 0.0\n\n    mean_wl = sum(len(x) for x in w) / nw\n    if mean_wl < 2.5 or mean_wl > 12.0:            # letter soup / URL or base64 dumps\n        return False, 0.0\n\n    alpha = sum(1 for c in text if c.isalpha())\n    if alpha / n < 0.45:                            # loose: markup & numbers survive\n        return False, 0.0\n\n    nonascii = sum(1 for c in text if ord(c) > 127)\n    if nonascii / n > 0.08:                         # non-English / mojibake\n        return False, 0.0\n\n    lines = [ln for ln in text.split(\"\\n\") if ln.strip()]\n    if not lines:\n        return False, 0.0\n    lw_counts = [len(ln.split()) for ln in lines]\n\n    # navigation / menu / link-farm boilerplate: a page made of stubs, not\n    # sentences. This is the single most important gate -- site chrome is\n    # lexically rich (it scores WELL on a density ratio) but teaches nothing.\n    long_lines = sum(1 for c in lw_counts if c >= 8)\n    if long_lines / len(lines) < 0.25:\n        return False, 0.0\n    if sum(lw_counts) / len(lines) < 5.0:\n        return False, 0.0\n\n    # duplicated lines: footers, repeated templates\n    if len(lines) > 8 and (1.0 - len(set(lines)) / len(lines)) > 0.40:\n        return False, 0.0\n\n    # word-level repetition / keyword stuffing\n    lwl = [x.lower() for x in w]\n    if len(set(lwl)) / nw < 0.22:\n        return False, 0.0\n    if Counter(lwl).most_common(1)[0][1] / nw > 0.12:\n        return False, 0.0\n\n    # bonus: prose-ish structure (terminal punctuation on lines) + substance\n    end_punct = sum(1 for ln in lines if ln.rstrip()[-1:] in \".!?\\\"')>\")\n    end_frac = end_punct / len(lines)\n    bonus = 0.30 * end_frac + 0.15 * min(1.0, nw / 800.0)\n    return True, bonus",
  "new_string": "# ------------------------------------------------------- stage 1: quality gate\n# Gate presets. \"loose\" only removes destroyed text; \"strict\" additionally\n# demands sentence-shaped English prose. Which one is right is an empirical\n# question answered by dev runs, so it is a single switch.\nGATE = os.environ.get(\"GATE\", \"strict\")\nG = {\n    \"loose\":  dict(min_chars=500, min_words=80,  alpha=0.45, nonascii=0.08,\n                   longln=0.25, meanln=5.0, dupln=0.40, uniq=0.22, top1=0.12,\n                   endfrac=0.0, digits=1.0, upper=1.0, stop=0.0),\n    \"strict\": dict(min_chars=700, min_words=110, alpha=0.62, nonascii=0.04,\n                   longln=0.45, meanln=8.0, dupln=0.25, uniq=0.26, top1=0.09,\n                   endfrac=0.30, digits=0.12, upper=0.14, stop=0.16),\n}[GATE]\n\n# Function words: prose in fluent English has a high stop-word rate. Keyword\n# soup, product tables and machine-translated text do not.\nSTOP = set(\"the of and to in a is that it for was on as with by are be this at from or \"\n           \"an but not have has had they he she we you his her their its which who \"\n           \"would could should will can about there been more when if all\".split())\n\n\ndef quality(text):\n    \"\"\"(ok, bonus). Intrinsic-only gates -- no reference to the target.\"\"\"\n    n = len(text)\n    if n < G[\"min_chars\"] or n > 600_000:\n        return False, 0.0\n    w = text.split()\n    nw = len(w)\n    if nw < G[\"min_words\"]:\n        return False, 0.0\n\n    mean_wl = sum(len(x) for x in w) / nw\n    if mean_wl < 2.5 or mean_wl > 12.0:            # letter soup / URL or base64 dumps\n        return False, 0.0\n\n    alpha = sum(1 for c in text if c.isalpha())\n    if alpha / n < G[\"alpha\"]:\n        return False, 0.0\n\n    nonascii = sum(1 for c in text if ord(c) > 127)\n    if nonascii / n > G[\"nonascii\"]:                # non-English / mojibake\n        return False, 0.0\n\n    digits = sum(1 for c in text if c.isdigit())\n    if digits / n > G[\"digits\"]:                    # tables, listings, stat dumps\n        return False, 0.0\n\n    if sum(1 for c in text if c.isupper()) / max(1, alpha) > G[\"upper\"]:\n        return False, 0.0                           # shouting / all-caps headings\n\n    lines = [ln for ln in text.split(\"\\n\") if ln.strip()]\n    if not lines:\n        return False, 0.0\n    lw_counts = [len(ln.split()) for ln in lines]\n\n    # navigation / menu / link-farm boilerplate: a page made of stubs, not\n    # sentences. This is the single most important gate -- site chrome is\n    # lexically rich (it scores WELL on a density ratio) but teaches nothing.\n    if sum(1 for c in lw_counts if c >= 8) / len(lines) < G[\"longln\"]:\n        return False, 0.0\n    if sum(lw_counts) / len(lines) < G[\"meanln\"]:\n        return False, 0.0\n\n    # duplicated lines: footers, repeated templates\n    if len(lines) > 8 and (1.0 - len(set(lines)) / len(lines)) > G[\"dupln\"]:\n        return False, 0.0\n\n    # word-level repetition / keyword stuffing\n    lwl = [x.lower() for x in w]\n    if len(set(lwl)) / nw < G[\"uniq\"]:\n        return False, 0.0\n    if Counter(lwl).most_common(1)[0][1] / nw > G[\"top1\"]:\n        return False, 0.0\n\n    # fluent-English function-word rate\n    if sum(1 for x in lwl if x in STOP) / nw < G[\"stop\"]:\n        return False, 0.0\n\n    # sentence-shaped lines\n    end_punct = sum(1 for ln in lines if ln.rstrip()[-1:] in \".!?\\\"')>\")\n    end_frac = end_punct / len(lines)\n    if end_frac < G[\"endfrac\"]:\n        return False, 0.0\n\n    bonus = 0.30 * end_frac + 0.15 * min(1.0, nw / 800.0)\n    return True, bonus"
}
Edit result
The file /workspace/submission/curate.py has been updated successfully. (file state is current in your context — no need to Read it back)

[metadata] {"filePath": "/workspace/submission/curate.py", "oldString": "# ------------------------------------------------------- stage 1: quality gate\ndef quality(text):\n    \"\"\"(ok, bonus). Loose intrinsic gates: drop destroyed text, keep markup.\"\"\"\n    n = len(text)\n    if n < 500 or n > 600_000:\n        return False, 0.0\n    w = text.split()\n    nw = len(w)\n    if nw < 80:\n        return False, 0.0\n\n    mean_wl = sum(len(x) for x in w) / nw\n    if mean_wl < 2.5 or mean_wl > 12.0:            # letter soup / URL or base64 dumps\n        return False, 0.0\n\n    alpha = sum(1 for c in text if c.isalpha())\n    if alpha / n < 0.45:                            # loose: markup & numbers survive\n        return False, 0.0\n\n    nonascii = sum(1 for c in text if ord(c) > 127)\n    if nonascii / n > 0.08:                         # non-English / mojibake\n        return False, 0.0\n\n    lines = [ln for ln in text.split(\"\\n\") if ln.strip()]\n    if not lines:\n        return False, 0.0\n    lw_counts = [len(ln.split()) for ln in lines]\n\n    # navigation / menu / link-farm boilerplate: a page made of stubs, not\n    # sentences. This is the single most important gate -- site chrome is\n    # lexically rich (it scores WELL on a density ratio) but teaches nothing.\n    long_lines = sum(1 for c in lw_counts if c >= 8)\n    if long_lines / len(lines) < 0.25:\n        return False, 0.0\n    if sum(lw_counts) / len(lines) < 5.0:\n        return False, 0.0\n\n    # duplicated lines: footers, repeated templates\n    if len(lines) > 8 and (1.0 - len(set(lines)) / len(lines)) > 0.40:\n        return False, 0.0\n\n    # word-level repetition / keyword stuffing\n    lwl = [x.lower() for x in w]\n    if len(set(lwl)) / nw < 0.22:\n        return False, 0.0\n    if Counter(lwl).most_common(1)[0][1] / nw > 0.12:\n        return False, 0.0\n\n    # bonus: prose-ish structure (terminal punctuation on lines) + substance\n    end_punct = sum(1 for ln in lines if ln.rstrip()[-1:] in \".!?\\\"')>\")\n    end_frac = end_punct / len(lines)\n    bonus = 0.30 * end_frac + 0.15 * min(1.0, nw / 800.0)\n    return True, bonus", "newString": "# ------------------------------------------------------- stage 1: quality gate\n# Gate presets. \"loose\" only removes destroyed text; \"strict\" additionally\n# demands sentence-shaped English prose. Which one is right is an empirical\n# question answered by dev runs, so it is a single switch.\nGATE = os.environ.get(\"GATE\", \"strict\")\nG = {\n    \"loose\":  dict(min_chars=500, min_words=80,  alpha=0.45, nonascii=0.08,\n                   longln=0.25, meanln=5.0, dupln=0.40, uniq=0.22, top1=0.12,\n                   endfrac=0.0, digits=1.0, upper=1.0, stop=0.0),\n    \"strict\": dict(min_chars=700, min_words=110, alpha=0.62, nonascii=0.04,\n                   longln=0.45, meanln=8.0, dupln=0.25, uniq=0.26, top1=0.09,\n                   endfrac=0.30, digits=0.12, upper=0.14, stop=0.16),\n}[GATE]\n\n# Function words: prose in fluent English has a high stop-word rate. Keyword\n# soup, product tables and machine-translated text do not.\nSTOP = set(\"the of and to in a is that it for was on as with by are be this at from or \"\n           \"an but not have has had they he she we you his her their its which who \"\n           \"would could should will can about there been more when if all\".split())\n\n\ndef quality(text):\n    \"\"\"(ok, bonus). Intrinsic-only gates -- no reference to the target.\"\"\"\n    n = len(text)\n    if n < G[\"min_chars\"] or n > 600_000:\n        return False, 0.0\n    w = text.split()\n    nw = len(w)\n    if nw < G[\"min_words\"]:\n        return False, 0.0\n\n    mean_wl = sum(len(x) for x in w) / nw\n    if mean_wl < 2.5 or mean_wl > 12.0:            # letter soup / URL or base64 dumps\n        return False, 0.0\n\n    alpha = sum(1 for c in text if c.isalpha())\n    if alpha / n < G[\"alpha\"]:\n        return False, 0.0\n\n    nonascii = sum(1 for c in text if ord(c) > 127)\n    if nonascii / n > G[\"nonascii\"]:                # non-English / mojibake\n        return False, 0.0\n\n    digits = sum(1 for c in text if c.isdigit())\n    if digits / n > G[\"digits\"]:                    # tables, listings, stat dumps\n        return False, 0.0\n\n    if sum(1 for c in text if c.isupper()) / max(1, alpha) > G[\"upper\"]:\n        return False, 0.0                           # shouting / all-caps headings\n\n    lines = [ln for ln in text.split(\"\\n\") if ln.strip()]\n    if not lines:\n        return False, 0.0\n    lw_counts = [len(ln.split()) for ln in lines]\n\n    # navigation / menu / link-farm boilerplate: a page made of stubs, not\n    # sentences. This is the single most important gate -- site chrome is\n    # lexically rich (it scores WELL on a density ratio) but teaches nothing.\n    if sum(1 for c in lw_counts if c >= 8) / len(lines) < G[\"longln\"]:\n        return False, 0.0\n    if sum(lw_counts) / len(lines) < G[\"meanln\"]:\n        return False, 0.0\n\n    # duplicated lines: footers, repeated templates\n    if len(lines) > 8 and (1.0 - len(set(lines)) / len(lines)) > G[\"dupln\"]:\n        return False, 0.0\n\n    # word-level repetition / keyword stuffing\n    lwl = [x.lower() for x in w]\n    if len(set(lwl)) / nw < G[\"uniq\"]:\n        return False, 0.0\n    if Counter(lwl).most_common(1)[0][1] / nw > G[\"top1\"]:\n        return False, 0.0\n\n    # fluent-English function-word rate\n    if sum(1 for x in lwl if x in STOP) / nw < G[\"stop\"]:\n        return False, 0.0\n\n    # sentence-shaped lines\n    end_punct = sum(1 for ln in lines if ln.rstrip()[-1:] in \".!?\\\"')>\")\n    end_frac = end_punct / len(lines)\n    if end_frac < G[\"endfrac\"]:\n        return False, 0.0\n\n    bonus = 0.30 * end_frac + 0.15 * min(1.0, nw / 800.0)\n    return True, bonus", "originalFile": null, "structuredPatch": [{"oldStart": 65, "oldLines": 14, "newStart": 65, "newLines": 34, "lines": [" ", " ", " # ------------------------------------------------------- stage 1: quality gate", "+# Gate presets. \"loose\" only removes destroyed text; \"strict\" additionally", "+# demands sentence-shaped English prose. Which one is right is an empirical", "+# question answered by dev runs, so it is a single switch.", "+GATE = os.environ.get(\"GATE\", \"strict\")", "+G = {", "+    \"loose\":  dict(min_chars=500, min_words=80,  alpha=0.45, nonascii=0.08,", "+                   longln=0.25, meanln=5.0, dupln=0.40, uniq=0.22, top1=0.12,", "+                   endfrac=0.0, digits=1.0, upper=1.0, stop=0.0),", "+    \"strict\": dict(min_chars=700, min_words=110, alpha=0.62, nonascii=0.04,", "+                   longln=0.45, meanln=8.0, dupln=0.25, uniq=0.26, top1=0.09,", "+                   endfrac=0.30, digits=0.12, upper=0.14, stop=0.16),", "+}[GATE]", "+", "+# Function words: prose in fluent English has a high stop-word rate. Keyword", "+# soup, product tables and machine-translated text do not.", "+STOP = set(\"the of and to in a is that it for was on as with by are be this at from or \"", "+           \"an but not have has had they he she we you his her their its which who \"", "+           \"would could should will can about there been more when if all\".split())", "+", "+", " def quality(text):", "-    \"\"\"(ok, bonus). Loose intrinsic gates: drop destroyed text, keep markup.\"\"\"", "+    \"\"\"(ok, bonus). Intrinsic-only gates -- no reference to the target.\"\"\"", "     n = len(text)", "-    if n < 500 or n > 600_000:", "+    if n < G[\"min_chars\"] or n > 600_000:", "         return False, 0.0", "     w = text.split()", "     nw = len(w)", "-    if nw < 80:", "+    if nw < G[\"min_words\"]:", "         return False, 0.0", " ", "     mean_wl = sum(len(x) for x in w) / nw"]}, {"oldStart": 80, "oldLines": 13, "newStart": 100, "newLines": 20, "lines": ["         return False, 0.0", " ", "     alpha = sum(1 for c in text if c.isalpha())", "-    if alpha / n < 0.45:                            # loose: markup & numbers survive", "+    if alpha / n < G[\"alpha\"]:", "         return False, 0.0", " ", "     nonascii = sum(1 for c in text if ord(c) > 127)", "-    if nonascii / n > 0.08:                         # non-English / mojibake", "+    if nonascii / n > G[\"nonascii\"]:                # non-English / mojibake", "         return False, 0.0", " ", "+    digits = sum(1 for c in text if c.isdigit())", "+    if digits / n > G[\"digits\"]:                    # tables, listings, stat dumps", "+        return False, 0.0", "+", "+    if sum(1 for c in text if c.isupper()) / max(1, alpha) > G[\"upper\"]:", "+        return False, 0.0                           # shouting / all-caps headings", "+", "     lines = [ln for ln in text.split(\"\\n\") if ln.strip()]", "     if not lines:", "         return False, 0.0"]}, {"oldStart": 95, "oldLines": 26, "newStart": 122, "newLines": 32, "lines": ["     # navigation / menu / link-farm boilerplate: a page made of stubs, not", "     # sentences. This is the single most important gate -- site chrome is", "     # lexically rich (it scores WELL on a density ratio) but teaches nothing.", "-    long_lines = sum(1 for c in lw_counts if c >= 8)", "-    if long_lines / len(lines) < 0.25:", "+    if sum(1 for c in lw_counts if c >= 8) / len(lines) < G[\"longln\"]:", "         return False, 0.0", "-    if sum(lw_counts) / len(lines) < 5.0:", "+    if sum(lw_counts) / len(lines) < G[\"meanln\"]:", "         return False, 0.0", " ", "     # duplicated lines: footers, repeated templates", "-    if len(lines) > 8 and (1.0 - len(set(lines)) / len(lines)) > 0.40:", "+    if len(lines) > 8 and (1.0 - len(set(lines)) / len(lines)) > G[\"dupln\"]:", "         return False, 0.0", " ", "     # word-level repetition / keyword stuffing", "     lwl = [x.lower() for x in w]", "-    if len(set(lwl)) / nw < 0.22:", "+    if len(set(lwl)) / nw < G[\"uniq\"]:", "         return False, 0.0", "-    if Counter(lwl).most_common(1)[0][1] / nw > 0.12:", "+    if Counter(lwl).most_common(1)[0][1] / nw > G[\"top1\"]:", "         return False, 0.0", " ", "-    # bonus: prose-ish structure (terminal punctuation on lines) + substance", "+    # fluent-English function-word rate", "+    if sum(1 for x in lwl if x in STOP) / nw < G[\"stop\"]:", "+        return False, 0.0", "+", "+    # sentence-shaped lines", "     end_punct = sum(1 for ln in lines if ln.rstrip()[-1:] in \".!?\\\"')>\")", "     end_frac = end_punct / len(lines)", "+    if end_frac < G[\"endfrac\"]:", "+        return False, 0.0", "+", "     bonus = 0.30 * end_frac + 0.15 * min(1.0, nw / 800.0)", "     return True, bonus", " "]}], "userModified": false, "replaceAll": false}
#24
reasoning (redacted)

encrypted chain-of-thought — recorded, not exposed

toolEdit
args
{
  "replace_all": false,
  "file_path": "/workspace/submission/curate.py",
  "old_string": "    # each cluster ranks the WHOLE pool by its own affinity; quotas are filled\n    # from these lists, skipping documents another cluster already took.\n    order = [np.argsort(-(Z[:, c] + W_BONUS * bon)) for c in range(K)]\n    ptr = [0] * K\n    emitted_tok = [0.0] * K\n    taken = set()\n    out, tot = [], 0.0\n    while tot < TARGET_EMIT_TOKENS:\n        cand = [c for c in range(K) if ptr[c] < len(ids)]\n        if not cand:\n            break\n        c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))\n        # advance past documents already claimed by another register\n        while ptr[c] < len(ids) and ids[order[c][ptr[c]]] in taken:\n            ptr[c] += 1\n        if ptr[c] >= len(ids):\n            continue\n        i = ids[order[c][ptr[c]]]\n        ptr[c] += 1\n        taken.add(i)\n        out.append(i)\n        emitted_tok[c] += est_tok[i]\n        tot += est_tok[i]\n    for c in range(K):\n        print(f\"      cluster {c}: share={share[c]:.3f} \"\n              f\"tok={emitted_tok[c]/1e6:.1f}M\", flush=True)\n    np.save(f\"{CACHE}/Z.npy\", Z)",
  "new_string": "    if ORDER == \"mixture\":\n        # each cluster ranks the WHOLE pool by its own affinity; quotas are\n        # filled from these lists, skipping docs another register already took.\n        order = [np.argsort(-(Z[:, c] + W_BONUS * bon)) for c in range(K)]\n        ptr = [0] * K\n        emitted_tok = [0.0] * K\n        taken = set()\n        out, tot = [], 0.0\n        while tot < TARGET_EMIT_TOKENS:\n            cand = [c for c in range(K) if ptr[c] < len(ids)]\n            if not cand:\n                break\n            c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))\n            while ptr[c] < len(ids) and ids[order[c][ptr[c]]] in taken:\n                ptr[c] += 1\n            if ptr[c] >= len(ids):\n                continue\n            i = ids[order[c][ptr[c]]]\n            ptr[c] += 1\n            taken.add(i)\n            out.append(i)\n            emitted_tok[c] += est_tok[i]\n            tot += est_tok[i]\n        for c in range(K):\n            print(f\"      cluster {c}: share={share[c]:.3f} \"\n                  f\"tok={emitted_tok[c]/1e6:.1f}M\", flush=True)\n    else:\n        # BREADTH-PRESERVING ORDER (the empirically correct one -- see claim.md).\n        # The trainer consumes a PREFIX of this list, so any hard ranking is a\n        # hard *subset* selection: ranking by affinity would hand the whole 12M\n        # budget to the top ~6% of the pool and measurably RAISES perplexity.\n        # Instead: use the affinity only to discard the off-target TAIL, then\n        # emit the survivors in a fixed pseudo-random order, so the 12M tokens\n        # the trainer sees are a uniform sample of everything that survived.\n        q = Z.max(1) + W_BONUS * bon                 # best-matching-register affinity\n        n_keep = int(KEEP_FRAC * len(ids))\n        surv = list(np.argsort(-q)[:n_keep])\n        random.Random(1234).shuffle(surv)\n        out = [ids[j] for j in surv]\n        tot = sum(est_tok[i] for i in out)\n        print(f\"      gate+dedup={len(ids)} keep_frac={KEEP_FRAC} \"\n              f\"kept={len(out)}\", flush=True)\n\n    np.save(f\"{CACHE}/Z.npy\", Z)"
}
Edit result
The file /workspace/submission/curate.py has been updated successfully. (file state is current in your context — no need to Read it back)

[metadata] {"filePath": "/workspace/submission/curate.py", "oldString": "    # each cluster ranks the WHOLE pool by its own affinity; quotas are filled\n    # from these lists, skipping documents another cluster already took.\n    order = [np.argsort(-(Z[:, c] + W_BONUS * bon)) for c in range(K)]\n    ptr = [0] * K\n    emitted_tok = [0.0] * K\n    taken = set()\n    out, tot = [], 0.0\n    while tot < TARGET_EMIT_TOKENS:\n        cand = [c for c in range(K) if ptr[c] < len(ids)]\n        if not cand:\n            break\n        c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))\n        # advance past documents already claimed by another register\n        while ptr[c] < len(ids) and ids[order[c][ptr[c]]] in taken:\n            ptr[c] += 1\n        if ptr[c] >= len(ids):\n            continue\n        i = ids[order[c][ptr[c]]]\n        ptr[c] += 1\n        taken.add(i)\n        out.append(i)\n        emitted_tok[c] += est_tok[i]\n        tot += est_tok[i]\n    for c in range(K):\n        print(f\"      cluster {c}: share={share[c]:.3f} \"\n              f\"tok={emitted_tok[c]/1e6:.1f}M\", flush=True)\n    np.save(f\"{CACHE}/Z.npy\", Z)", "newString": "    if ORDER == \"mixture\":\n        # each cluster ranks the WHOLE pool by its own affinity; quotas are\n        # filled from these lists, skipping docs another register already took.\n        order = [np.argsort(-(Z[:, c] + W_BONUS * bon)) for c in range(K)]\n        ptr = [0] * K\n        emitted_tok = [0.0] * K\n        taken = set()\n        out, tot = [], 0.0\n        while tot < TARGET_EMIT_TOKENS:\n            cand = [c for c in range(K) if ptr[c] < len(ids)]\n            if not cand:\n                break\n            c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))\n            while ptr[c] < len(ids) and ids[order[c][ptr[c]]] in taken:\n                ptr[c] += 1\n            if ptr[c] >= len(ids):\n                continue\n            i = ids[order[c][ptr[c]]]\n            ptr[c] += 1\n            taken.add(i)\n            out.append(i)\n            emitted_tok[c] += est_tok[i]\n            tot += est_tok[i]\n        for c in range(K):\n            print(f\"      cluster {c}: share={share[c]:.3f} \"\n                  f\"tok={emitted_tok[c]/1e6:.1f}M\", flush=True)\n    else:\n        # BREADTH-PRESERVING ORDER (the empirically correct one -- see claim.md).\n        # The trainer consumes a PREFIX of this list, so any hard ranking is a\n        # hard *subset* selection: ranking by affinity would hand the whole 12M\n        # budget to the top ~6% of the pool and measurably RAISES perplexity.\n        # Instead: use the affinity only to discard the off-target TAIL, then\n        # emit the survivors in a fixed pseudo-random order, so the 12M tokens\n        # the trainer sees are a uniform sample of everything that survived.\n        q = Z.max(1) + W_BONUS * bon                 # best-matching-register affinity\n        n_keep = int(KEEP_FRAC * len(ids))\n        surv = list(np.argsort(-q)[:n_keep])\n        random.Random(1234).shuffle(surv)\n        out = [ids[j] for j in surv]\n        tot = sum(est_tok[i] for i in out)\n        print(f\"      gate+dedup={len(ids)} keep_frac={KEEP_FRAC} \"\n              f\"kept={len(out)}\", flush=True)\n\n    np.save(f\"{CACHE}/Z.npy\", Z)", "originalFile": null, "structuredPatch": [{"oldStart": 369, "oldLines": 32, "newStart": 369, "newLines": 49, "lines": ["     bon = np.array([bonus_of.get(i, 0.0) for i in ids])", "     est_tok = {i: nchar_of[i] / 4.0 for i in nchar_of}", " ", "-    # each cluster ranks the WHOLE pool by its own affinity; quotas are filled", "-    # from these lists, skipping documents another cluster already took.", "-    order = [np.argsort(-(Z[:, c] + W_BONUS * bon)) for c in range(K)]", "-    ptr = [0] * K", "-    emitted_tok = [0.0] * K", "-    taken = set()", "-    out, tot = [], 0.0", "-    while tot < TARGET_EMIT_TOKENS:", "-        cand = [c for c in range(K) if ptr[c] < len(ids)]", "-        if not cand:", "-            break", "-        c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))", "-        # advance past documents already claimed by another register", "-        while ptr[c] < len(ids) and ids[order[c][ptr[c]]] in taken:", "+    if ORDER == \"mixture\":", "+        # each cluster ranks the WHOLE pool by its own affinity; quotas are", "+        # filled from these lists, skipping docs another register already took.", "+        order = [np.argsort(-(Z[:, c] + W_BONUS * bon)) for c in range(K)]", "+        ptr = [0] * K", "+        emitted_tok = [0.0] * K", "+        taken = set()", "+        out, tot = [], 0.0", "+        while tot < TARGET_EMIT_TOKENS:", "+            cand = [c for c in range(K) if ptr[c] < len(ids)]", "+            if not cand:", "+                break", "+            c = min(cand, key=lambda j: emitted_tok[j] / max(1e-9, share[j]))", "+            while ptr[c] < len(ids) and ids[order[c][ptr[c]]] in taken:", "+                ptr[c] += 1", "+            if ptr[c] >= len(ids):", "+                continue", "+            i = ids[order[c][ptr[c]]]", "             ptr[c] += 1", "-        if ptr[c] >= len(ids):", "-            continue", "-        i = ids[order[c][ptr[c]]]", "-        ptr[c] += 1", "-        taken.add(i)", "-        out.append(i)", "-        emitted_tok[c] += est_tok[i]", "-        tot += est_tok[i]", "-    for c in range(K):", "-        print(f\"      cluster {c}: share={share[c]:.3f} \"", "-              f\"tok={emitted_tok[c]/1e6:.1f}M\", flush=True)", "+            taken.add(i)", "+            out.append(i)", "+            emitted_tok[c] += est_tok[i]", "+            tot += est_tok[i]", "+        for c in range(K):", "+            print(f\"      cluster {c}: share={share[c]:.3f} \"", "+                  f\"tok={emitted_tok[c]/1e6:.1f}M\", flush=True)", "+    else:", "+        # BREADTH-PRESERVING ORDER (the empirically correct one -- see claim.md).", "+        # The trainer consumes a PREFIX of this list, so any hard ranking is a", "+        # hard *subset* selection: ranking by affinity would hand the whole 12M", "+        # budget to the top ~6% of the pool and measurably RAISES perplexity.", "+        # Instead: use the affinity only to discard the off-target TAIL, then", "+        # emit the survivors in a fixed pseudo-random order, so the 12M tokens", "+        # the trainer sees are a uniform sample of everything that survived.", "+        q = Z.max(1) + W_BONUS * bon                 # best-matching-register affinity", "+        n_keep = int(KEEP_FRAC * len(ids))", "+        surv = list(np.argsort(-q)[:n_keep])", "+        random.Random(1234).shuffle(surv)", "+        out = [ids[j] for j in surv]", "+        tot = sum(est_tok[i] for i in out)", "+        print(f\"      gate+dedup={len(ids)} keep_frac={KEEP_FRAC} \"", "+              f\"kept={len(out)}\", flush=True)", "+", "     np.save(f\"{CACHE}/Z.npy\", Z)", "     json.dump(ids, open(f\"{CACHE}/ids.json\", \"w\"))", "     json.dump({\"share\": share.tolist(), \"bonus\": bonus_of, \"nchar\": nchar_of},"]}], "userModified": false, "replaceAll": false}
#25
reasoning (redacted)

encrypted chain-of-thought — recorded, not exposed

toolBashAdd ORDER/KEEP_FRAC config and check batch
args
{
  "command": "python - <<'EOF'\nimport re\np='submission/curate.py'; s=open(p).read()\ns=s.replace('import json, math, os, re, sys, zlib','import json, math, os, random, re, sys, zlib')\ns=s.replace('W_BONUS = float(os.environ.get(\"W_BONUS\", \"0.5\"))',\n 'W_BONUS = float(os.environ.get(\"W_BONUS\", \"0.5\"))\\nORDER = os.environ.get(\"ORDER\", \"shuffle\")   # shuffle | mixture\\nKEEP_FRAC = float(os.environ.get(\"KEEP_FRAC\", \"1.0\"))')\nopen(p,'w').write(s)\nEOF\ngrep -n \"ORDER\\|KEEP_FRAC\\|^import\" submission/curate.py | head; echo \"=== batch:\"; grep -E \"RESULT\" /tmp/batch1.log; cat .timer/remaining_secs",
  "description": "Add ORDER/KEEP_FRAC config and check batch"
}
Bash result
44:import json, math, os, random, re, sys, zlib
59:ORDER = os.environ.get("ORDER", "shuffle")   # shuffle | mixture
60:KEEP_FRAC = float(os.environ.get("KEEP_FRAC", "1.0"))
374:    if ORDER == "mixture":
401:        # BREADTH-PRESERVING ORDER (the empirically correct one -- see claim.md).
409:        n_keep = int(KEEP_FRAC * len(ids))
414:        print(f"      gate+dedup={len(ids)} keep_frac={KEEP_FRAC} "
=== batch:
RESULT sel_gate HELDOUT_PPL 422.876  loss 6.0471  train_s 54  /tmp/tr_sel_gate.npy
RESULT sel_rf35 HELDOUT_PPL 461.740  loss 6.1350  train_s 54  /tmp/tr_sel_rf35.npy
RESULT sel_rf12 HELDOUT_PPL 405.817  loss 6.0059  train_s 54  /tmp/tr_sel_rf12.npy
4589

[stdout]
44:import json, math, os, random, re, sys, zlib
59:ORDER = os.environ.get("ORDER", "shuffle")   # shuffle | mixture
60:KEEP_FRAC = float(os.environ.get("KEEP_FRAC", "1.0"))
374:    if ORDER == "mixture":
401:        # BREADTH-PRESERVING ORDER (the empirically correct one -- see claim.md).
409:        n_keep = int(KEEP_FRAC * len(ids))
414:        print(f"      gate+dedup={len(ids)} keep_frac={KEEP_FRAC} "
=== batch:
RESULT sel_gate HELDOUT_PPL 422.876  loss 6.0471  train_s 54  /tmp/tr_sel_gate.npy
RESULT sel_rf35 HELDOUT_PPL 461.740  loss 6.1350  train_s 54  /tmp/tr_sel_rf35.npy
RESULT sel_rf12 HELDOUT_PPL 405.817  loss 6.0059  train_s 54  /tmp/tr_sel_rf12.npy
4589
[metadata] {"noOutputExpected": false}