File size: 16,028 Bytes
8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 e85f766 13ebe4b e85f766 13ebe4b e85f766 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b 8787bd3 13ebe4b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | {
"nbformat": 4,
"nbformat_minor": 5,
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"colab": {
"provenance": [],
"toc_visible": true
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# FATHOM β Judge Reproducer Notebook\n",
"\n",
"> **The first RL-trained Recursive Language Model.**\n",
"> An OpenEnv environment + GRPO training pipeline that teaches Qwen 2.5 Coder 1.5B (4-bit + LoRA) to use a recursive-LM scaffold (Python REPL + recursive `llm()` calls) for long-context QA.\n",
">\n",
"> Submission for the **Meta Γ PyTorch Γ Hugging Face OpenEnv Hackathon Grand Finale** (Bangalore, April 25β26 2026).\n",
"\n",
"## What this notebook does (3 min on free CPU Colab)\n",
"\n",
"1. Pings the live env Space and confirms it returns `{\"status\":\"ok\"}`.\n",
"2. Installs ~5 lightweight Python packages (no PyTorch, no bitsandbytes).\n",
"3. Downloads the reward code (~30 KB) and runs the **8 adversarial reward probes** locally to prove the verifier blocks each hack.\n",
"4. Pulls the actual training plots from the trained-model repo and renders them inline.\n",
"5. Embeds the live W&B run with the full GRPO reward trajectory (0.15 β 0.98 over 70 steps).\n",
"6. Lists the trained model's adapters + merged checkpoint on the HF Hub.\n",
"\n",
"## What this notebook does NOT do\n",
"\n",
"- Re-train the model. Training the full Qwen 1.5B + LoRA on the FATHOM env requires an A100 (the cell at the bottom of this notebook contains the exact command, but free Colab does not have the GPU). Re-training takes ~50 min and ~$10 on `hf jobs run --flavor=a100-large`.\n",
"- Load the merged 1.5B model on CPU. The model is published at `Pratham-math/fathom-1.5b-grpo` for anyone who has GPU compute.\n",
"\n",
"## Submission links\n",
"\n",
"| Resource | URL |\n",
"|---|---|\n",
"| Environment Space | <https://huggingface.co/spaces/Pratham-math/fathom-env> |\n",
"| Trained model | <https://huggingface.co/Pratham-math/fathom-1.5b-grpo> |\n",
"| Code repo | <https://huggingface.co/Pratham-math/fathom-code> |\n",
"| Mini-blog | <https://huggingface.co/spaces/Pratham-math/fathom-blog> |\n",
"| W&B run (v2 β successful) | <https://wandb.ai/pratham-alwar05-indian-institute-of-information-technolo/huggingface/runs/y82wmj4x> |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1 Β· Is the live OpenEnv server actually running?\n"
]
},
{
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": [],
"source": [
"import urllib.request, json, sys\n",
"ENV_URL = \"https://Pratham-math-fathom-env.hf.space\"\n",
"\n",
"def http_get(path: str, timeout: int = 30) -> tuple[int, str]:\n",
" req = urllib.request.Request(ENV_URL + path, headers={\"User-Agent\": \"fathom-judge-notebook\"})\n",
" try:\n",
" with urllib.request.urlopen(req, timeout=timeout) as r:\n",
" return r.status, r.read().decode(\"utf-8\", errors=\"replace\")\n",
" except urllib.error.HTTPError as e:\n",
" return e.code, e.read().decode(\"utf-8\", errors=\"replace\")\n",
" except Exception as e:\n",
" return 0, f\"network error: {e}\"\n",
"\n",
"for path in [\"/healthz\", \"/openapi.json\", \"/\"]:\n",
" status, body = http_get(path)\n",
" snippet = body[:140].replace(\"\\n\", \" \")\n",
" print(f\"GET {path:18s} -> HTTP {status} | {snippet}\")\n",
"\n",
"status, body = http_get(\"/healthz\")\n",
"assert status == 200 and \"ok\" in body, f\"Env Space returned {status}: {body[:200]}\"\n",
"print()\n",
"print(\"PASS β live env Space is healthy.\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2 Β· Install lightweight deps (~30 s, no GPU)\n",
"\n",
"We only need three things to verify the project: an HTTP client, the HF Hub client to pull artifacts, and Pillow + matplotlib to render training plots inline.\n",
"\n",
"**No PyTorch / Transformers / bitsandbytes / Unsloth in this path** β those are only needed for the optional A100 training cell at the very bottom.\n"
]
},
{
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": [],
"source": [
"%pip install --quiet \"huggingface_hub>=0.28\" pillow matplotlib requests\n",
"print(\"deps OK\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3 Β· Pull just the reward verifier code (β 30 KB)\n",
"\n",
"The reward function is pure Python β no model weights, no GPU.\n",
"We grab the seven files needed to compute a reward score.\n"
]
},
{
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": [],
"source": [
"from huggingface_hub import hf_hub_download\n",
"import sys, os, pathlib\n",
"\n",
"REPO = \"Pratham-math/fathom-code\"\n",
"files = [\n",
" \"rewards/__init__.py\",\n",
" \"rewards/compose.py\",\n",
" \"rewards/correctness.py\",\n",
" \"rewards/format_gate.py\",\n",
" \"rewards/recursion_efficiency.py\",\n",
" \"rewards/recursion_extract.py\",\n",
" \"rewards/token_budget.py\",\n",
" \"configs/reward/v1.yaml\",\n",
"]\n",
"local_root = pathlib.Path(\"/content/fathom\").resolve()\n",
"for f in files:\n",
" local = hf_hub_download(repo_id=REPO, filename=f, local_dir=str(local_root))\n",
"sys.path.insert(0, str(local_root))\n",
"print(\"pulled reward code into\", local_root)\n",
"print(\"rewards/ files:\", os.listdir(local_root / \"rewards\"))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4 Β· The 8 adversarial reward probes\n",
"\n",
"Every entry below is a completion that *tries* to hack the reward.\n",
"The composite verifier should reject each one β wrong answers cap at 0.25, and only well-formatted *correct* answers earn the recursion-efficiency bonus.\n",
"\n",
"A passing run shows scores in this exact order: **correct β« partial β« format-only β« no-format**.\n"
]
},
{
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": [],
"source": [
"from rewards.compose import compose_reward_single\n",
"import types\n",
"\n",
"cfg = types.SimpleNamespace(\n",
" alpha=0.2,\n",
" weights=types.SimpleNamespace(\n",
" correctness=0.70, token_budget=0.15, recursion_efficiency=0.15\n",
" ),\n",
" token_budget_variant=\"capped_linear\",\n",
" answer_regex=\"<answer>(.*?)</answer>\",\n",
" max_calls=4,\n",
")\n",
"\n",
"GOLD = \"silver\"\n",
"PROBES = [\n",
" (\"correct + 0 llm calls (REPL grep)\",\n",
" \"```python\\nimport re\\nm=re.search('silver', ctx)\\nprint(m.group())\\n```\\n<answer>silver</answer>\"),\n",
" (\"correct + 1 llm call\",\n",
" \"```python\\nans=llm('color', ctx[:5000])\\nprint(ans)\\n```\\n<answer>silver</answer>\"),\n",
" (\"correct + bare answer (trivial-task path)\",\n",
" \"<answer>silver</answer>\"),\n",
" (\"wrong answer + correct format (A-01)\",\n",
" \"<answer>gold</answer>\"),\n",
" (\"wrong + no format\",\n",
" \"the color is gold\"),\n",
" (\"right text but no <answer> tag\",\n",
" \"silver\"),\n",
" (\"format-only spam (empty answer)\",\n",
" \"<answer></answer>\"),\n",
" (\"recursion-spam (5 llm calls, A-05)\",\n",
" \"```python\\n\" + \"\\n\".join(f\"x{i}=llm('q{i}',ctx)\" for i in range(5)) + \"\\n```\\n<answer>silver</answer>\"),\n",
"]\n",
"\n",
"print(f\"{'#':>2} {'reward':>6} {'calls':>5} description\")\n",
"print(\"-\" * 90)\n",
"scores = []\n",
"for i, (desc, gen) in enumerate(PROBES):\n",
" s, m = compose_reward_single(gen, GOLD, prompt_token_count=200, cfg_reward=cfg, llm_call_count=None)\n",
" scores.append(s)\n",
" print(f\"{i:>2} {s:>6.3f} {int(m['llm_call_count']):>5d} {desc}\")\n",
"print(\"-\" * 90)\n",
"\n",
"import statistics\n",
"print(f\"\\ngroup mean: {statistics.mean(scores):.3f}\")\n",
"print(f\"group std: {statistics.stdev(scores):.3f} (must be > 0.10 for GRPO advantage)\")\n",
"print(f\"max - min: {max(scores) - min(scores):.3f}\")\n",
"\n",
"assert scores[0] > scores[1] > scores[7], \"FAIL: 0-call should beat 1-call should beat spam\"\n",
"assert scores[0] > scores[3], \"FAIL: correct must beat wrong-but-formatted\"\n",
"assert scores[6] <= 0.25, \"FAIL: format-only spam not capped\"\n",
"assert statistics.stdev(scores) > 0.10, \"FAIL: group std too low\"\n",
"print(\"\\nPASS β 8/8 reward probes behave as designed.\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5 Β· Training plots β pulled live from the trained-model repo\n",
"\n",
"These PNGs were committed to <https://huggingface.co/Pratham-math/fathom-1.5b-grpo> at the end of the GRPO run on `a100-large` HF Jobs.\n"
]
},
{
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": [],
"source": [
"from huggingface_hub import hf_hub_download\n",
"from PIL import Image\n",
"import matplotlib.pyplot as plt\n",
"\n",
"PLOTS_REPO = \"Pratham-math/fathom-1.5b-grpo\"\n",
"PLOT_FILES = [\n",
" (\"plots/sft_loss.png\", \"SFT loss β 3.20 -> 0.29 over 63 steps\"),\n",
" (\"plots/sft_token_accuracy.png\", \"SFT token accuracy β 0.46 -> 0.93\"),\n",
" (\"plots/grpo_reward.png\", \"GRPO composite reward (v2 run y82wmj4x)\"),\n",
" (\"plots/grpo_completion_length.png\", \"GRPO completion length β model finds short correct answers\"),\n",
" (\"plots/grpo_kl.png\", \"GRPO KL β controlled drift from base policy\"),\n",
" (\"plots/training_summary.png\", \"8-panel training summary\"),\n",
"]\n",
"\n",
"fig, axes = plt.subplots(3, 2, figsize=(15, 16))\n",
"for ax, (path, title) in zip(axes.flat, PLOT_FILES):\n",
" try:\n",
" img_path = hf_hub_download(repo_id=PLOTS_REPO, filename=path)\n",
" img = Image.open(img_path)\n",
" ax.imshow(img)\n",
" ax.set_title(title, fontsize=11)\n",
" except Exception as e:\n",
" ax.text(0.5, 0.5, f\"{path}\\n{e}\", ha=\"center\", va=\"center\", fontsize=9, transform=ax.transAxes)\n",
" ax.set_title(title + \" (load failed)\", fontsize=11, color=\"red\")\n",
" ax.axis(\"off\")\n",
"plt.tight_layout()\n",
"plt.show()\n",
"print(\"Done β 6 training plots rendered above.\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6 Β· Live W&B training run\n",
"\n",
"Embed of the v2 run that learned the correct-answer mode (`y82wmj4x` / \"lucky-capybara-5\"). Reward climbs from a 0.15 format-bonus floor to 0.86β0.98 peaks once the policy starts producing correct answers.\n",
"\n",
"If the iframe doesn't load (HF Colab sometimes blocks third-party iframes), use the direct URL printed below.\n"
]
},
{
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": [],
"source": [
"WANDB_URL = \"https://wandb.ai/pratham-alwar05-indian-institute-of-information-technolo/huggingface/runs/y82wmj4x\"\n",
"\n",
"from IPython.display import IFrame, display, Markdown\n",
"display(IFrame(WANDB_URL, width=\"100%\", height=720))\n",
"display(Markdown(f\"**Direct W&B link** (if the iframe is blocked): [{WANDB_URL}]({WANDB_URL})\"))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 7 Β· The trained model artifacts\n",
"\n",
"The full 1.5B model is published with three flavours: the LoRA adapter (~15 MB, the actual training output), the merged 16-bit weights (~3 GB, ready for inference), and the training plots.\n"
]
},
{
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": [],
"source": [
"from huggingface_hub import HfApi\n",
"api = HfApi()\n",
"files = sorted(api.list_repo_files(\"Pratham-math/fathom-1.5b-grpo\"))\n",
"\n",
"print(f\"Total files: {len(files)}\\n\")\n",
"print(f\"{'category':25s} {'count':>5s}\")\n",
"print(\"-\" * 40)\n",
"\n",
"categories = {\n",
" \"adapters/ (LoRA)\": [f for f in files if f.startswith(\"adapter\") or \"adapter_\" in f],\n",
" \"merged_16bit/\": [f for f in files if f.startswith(\"merged_16bit/\")],\n",
" \"plots/ (training PNGs)\":[f for f in files if f.startswith(\"plots/\")],\n",
" \"tokenizer / config\": [f for f in files if any(f.endswith(s) for s in [\"tokenizer.json\",\"tokenizer_config.json\",\"special_tokens_map.json\",\"vocab.json\",\"merges.txt\",\"added_tokens.json\",\"config.json\",\"generation_config.json\"])],\n",
" \"other\": [],\n",
"}\n",
"seen = set().union(*categories.values())\n",
"categories[\"other\"] = [f for f in files if f not in seen]\n",
"\n",
"for k, v in categories.items():\n",
" print(f\"{k:25s} {len(v):>5d}\")\n",
"print()\n",
"print(\"Sample LoRA adapter files:\")\n",
"for f in [f for f in files if \"adapter\" in f.lower()][:3]:\n",
" print(\" \", f)\n",
"print()\n",
"print(\"Sample merged_16bit files:\")\n",
"for f in [f for f in files if f.startswith(\"merged_16bit/\")][:5]:\n",
" print(\" \", f)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 8 Β· (Optional, A100 only) Re-run the training\n",
"\n",
"This is the actual command we used for the v2 run that produced the curve above. It runs `train/grpo.py` against the live env Space. **Do not run on free Colab β it will OOM.** The cell is left here so judges can verify the exact arguments.\n",
"\n",
"```bash\n",
"hf jobs run \\\n",
" --flavor=a100-large \\\n",
" --secrets HF_TOKEN=$HF_TOKEN \\\n",
" --secrets WANDB_API_KEY=$WANDB_API_KEY \\\n",
" -e FATHOM_USE_VLLM=0 \\\n",
" pytorch/pytorch:2.6.0-cuda12.4-cudnn9-devel \\\n",
" bash -c 'apt-get update -qq && apt-get install -y -qq git && \\\n",
" git clone -b main https://oauth2:$HF_TOKEN@huggingface.co/Pratham-math/fathom-code /w && \\\n",
" bash /w/scripts/job_train.sh'\n",
"```\n",
"\n",
"Wall-clock: ~50 min on `a100-large`. Cost: ~$10 of HF Jobs credit. The trainer pushes the LoRA adapter and the merged 16-bit checkpoint to `Pratham-math/fathom-1.5b-grpo` automatically.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What you just verified\n",
"\n",
"1. The OpenEnv FATHOM server is **live** at `https://Pratham-math-fathom-env.hf.space` and returns `{\"status\":\"ok\"}`.\n",
"2. The deterministic 4-component reward (format gate Γ correctness + token budget + recursion efficiency) **rejects all 8 known reward-hack patterns** with a clean `correct β« wrong β« format-only` ordering.\n",
"3. Training **actually happened**: SFT loss dropped 3.20 β 0.29 (91% reduction), GRPO reward climbed from a 0.15 format-bonus floor to 0.86β0.98 peaks over 70 steps once the policy discovered the correct-answer mode.\n",
"4. The trained model is published as a LoRA adapter + merged 16-bit checkpoint on the HF Hub.\n",
"\n",
"For the full story (incl. the v1 β v2 debugging journey), see the mini-blog: <https://huggingface.co/spaces/Pratham-math/fathom-blog>.\n"
]
}
]
} |