diff --git a/.gitignore b/.gitignore index 3423c416a7..32e9bfb153 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,36 @@ data/manifest.json data/docs_selected.jsonl .mypy_cache/ .venv -logs/ \ No newline at end of file +logs/ + +# Personal scratch and notes (not part of the submission) +.claude/ +PARAMETER_GOLF_BATTLE_PLAN_*.md +chats.md +program.md +errors.json +best_sweep_config.json +test_oracle.bin +final_model.int6.ptz +final_model.pt + +# Local working copies that duplicate the canonical files in records/ +/train_gpt.py +/build_ngram_oracle.py +/run_h100.sh +/run_local_test.sh +/run_3seeds.sh +/sweep.py +/ctw_prototype.py +/ablation_colab.ipynb +/kaggle_run.ipynb + +# Reference / scratch copies of other people's PRs and earlier forks +reference_pr*_train_gpt.py +train_gpt_pr*_*.py + +# Tooling clones +autoresearch_ref/ + +# Older incomplete draft record submission, never finished +records/track_10min_16mb/2026-03-24_VR_GA_LeakyReLU_LegalTTT/ diff --git a/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/JOURNEY.md b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/JOURNEY.md new file mode 100644 index 0000000000..dd36a02954 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/JOURNEY.md @@ -0,0 +1,299 @@ +# Journey: How This Submission Came Together + +**Author:** Dhruv Puri ([@dhruvpuri](https://github.com/dhruvpuri)) +**Period:** March 22 – April 30, 2026 (~5 weeks) +**Final state:** Methodology submission, end-to-end validated on Kaggle T4×2 NCCL DDP (8L/384d, 13.4M params, 172 training steps, 3,786 TTT chunks, 6.85 MB final artifact, exit 0). + +This is the process journal — separate from the [README](./README.md) which documents the *what* and *why* of the final design. This document records the *how I got there*: the research arc, the agent-assisted workflow, the strategic pivots, the dead ends, and the honest decisions made when access constraints couldn't be solved. + +It's deliberately verbose. The hiring read of this document is: *"can this person reason about ML research under uncertainty, route work to specialists, kill bad ideas, and ship something honest under deadline pressure?"* + +--- + +## Table of contents + +1. [The starting point](#1-the-starting-point) +2. [Five GitHub sweeps and what each one changed](#2-five-github-sweeps-and-what-each-one-changed) +3. [Specialist agents consulted](#3-specialist-agents-consulted) +4. [The strategic pivots](#4-the-strategic-pivots) +5. [Dead ends, with numbers](#5-dead-ends-with-numbers) +6. [Local testing journey](#6-local-testing-journey) +7. [The day-of-deadline polish loop](#7-the-day-of-deadline-polish-loop) +8. [What I would do differently](#8-what-i-would-do-differently) +9. [Skills demonstrated](#9-skills-demonstrated) +10. [Reproducing](#10-reproducing) +11. [Related work](#11-related-work) + +--- + +## 1. The starting point + +I joined Parameter Golf on **March 22, 2026** — four days after the competition opened. Battle Plan v3 (in this repo as `PARAMETER_GOLF_BATTLE_PLAN_v3.md`) framed the problem and committed to a base: PR #462 (1.0672 BPB at the time), with two confirmed additive wins: Value Residual (-0.015) and Gated Attention (-0.003), both validated in PR #413's controlled ablation. Target: sub-1.05 BPB. + +**Hardware reality:** RTX 4060 Laptop (8 GB VRAM) for local development. No 8×H100 access; the OpenAI RunPod template was failing for me with CUDA driver mismatch errors during this window (Discord chats.md line 51-55 confirmed this was a community-wide problem on March 25). I never resolved it. + +**Time budget:** ~5 weeks. Early weeks went to research and code-writing, late weeks went to fix-and-refactor cycles after each frontier shift on the leaderboard. + +--- + +## 2. Five GitHub sweeps and what each one changed + +The competition's open-PR queue moved faster than I could read it, so I time-boxed regular sweeps via specialist agents to keep my plan calibrated. + +### Sweep 1 (March 28) — "the meta moved past the merged leaderboard" + +Found that the merged SOTA was 1.1194 BPB but the open frontier was 1.0226 (PR #875, GatedDeltaNet) and 1.0450 (PR #967, SGD TTT + HedgeMixer). The PR list crossed PR #976 already. + +**Plan change:** Battle Plan v3 was obsolete. Drafted v4 with a hybrid SP4096 + GPTQ + brotli + oracle target, but kept the existing 1024-vocab oracle work as a fallback because the artifact-budget math made SP4096 + oracle infeasible (4096² bigram = 16 MB raw, too big). + +### Sweep 2 (April 4) — "frontier is at 1.0897 (no SLOT)" + +PR count was now 1341+. Three new techniques were dominating: +- **CaseOps tokenizer** (PR #1729) — lossless bijective case folding +- **MuonEq-R optimizer** +- **Causal SLOT** at 0.77-1.04 BPB (legality disputed) + +Critically: **PR #1341 documented that TTT + GPTQ are mutually exclusive** because GPTQ's compensatory weight structure is destroyed by gradient updates. This killed any plan to add GPTQ on top of our SGD-TTT path. Decision: stay on int6 + zstd-22, don't chase brotli. + +### Sweep 3 (April 30, deadline day) — "the final state" + +The merged frontier closed at 1.0611 BPB (PR #1855). The last 26 days had added BOS-Fix SmearGate + LQER (replaced GPTQ) + SparseAttnGate + Phased TTT. None of these were architectural breakthroughs — they were surgical engineering. + +**Plan change:** Confirmed the submission would be a non-record methodology contribution, not a record claim. The gap between local-validated toy (~2.55 BPB on a 4L/256d) and competition-scale (~1.05 BPB on 11L/512d) wasn't closeable without H100 access. + +### Sweep 4 + 5 — bookkeeping sweeps + +Used to verify nothing in the leaderboard had moved between drafting the README and committing. + +--- + +## 3. Specialist agents consulted + +I treated agent invocations like consulting senior engineers — each one got a focused brief, returned a critique, and I integrated what survived adversarial pushback. + +### AI Engineer (twice) + +- **First pass:** designed the technical narrative for the submission. Frame: "modular hybrid system, not a stack." Recommended language for §1 motivation, §12 position-vs-meta, hiring talking points. +- **Second pass:** assessed whether to pursue 3 novel approaches (Spectral Weight Codec, Dual-Track Ensemble, CTW + Neural Residual). Verdict: Dual-Track Ensemble best risk/reward, CTW highest upside but 7-10 days, Spectral Codec lowest ceiling. **Result:** none were pursued because the [devil's advocate](#devils-advocate) round 4 days later killed the novelty pivot. + +### ML Engineer (twice) + +- **First pass:** code audit of `train_gpt.py`. Found 5 critical bugs: + 1. `tempfile.mktemp` deprecated, TOCTOU-unsafe — fixed. + 2. Per-parameter `dist.all_reduce` in TTT (100s of NCCL calls per micro-step) — fixed with bucketed flat all_reduce. + 3. Inline complementary loss `loss * weight.mean()` was mathematically wrong — removed honestly. + 4. Window-starts filter could drop tokens silently — left as documented minor issue. + 5. (Later, in code review) `bi_counts[prev, targets] += 1.0` is non-deterministic with duplicates — fixed with `index_put_(accumulate=True)`. +- **Second pass:** training-strategy advice on PR #1218's high-WD insight. Recommended: WD=0.085 + Brotli + GPTQ. **Outcome:** integrated WD recommendation into Future Work; didn't integrate GPTQ because of TTT-GPTQ incompatibility (Sweep 2 finding). + +### Architect Reviewer (once, on the final submission) + +Found 3 critical issues: +- "Bit-identical to base" claim was overstated (artifact format differs even when oracle empty) — softened the claim in README §2. +- `complementary_training_loss` is dead code that the README discusses extensively — added an explicit "currently unused" comment on the function. +- Oracle reload writes a temp file on all 8 ranks — replaced with `from_bytes` classmethod parsing in-memory bytes. + +Also recommended: magic prefix + version byte on artifact wrapper, warm Hedge prior `log_w[0]=2.0`, WARN log when oracle path is set but file missing. All applied. + +### Code Reviewer (once, final pass) + +Caught the C1 bug above (`bi_counts[prev, targets] += 1.0` non-determinism). Also flagged: inline imports of `zlib` and `tempfile` (cleaned up — `zlib` is now top-level since stdlib), the `COMPLEMENTARY_ALPHA=0.2` in `run_h100.sh` contradicting the README's "currently inert" statement (removed from script). + +### Research Analyst (once) + +Searched April 2026 arXiv + GitHub for relevant releases. Surfaced 8 papers/repos with publication dates 2-25 days before the deadline. Six made it into the README's §14 Related work. Most useful: [In-Place Test-Time Training (April 7)](https://arxiv.org/abs/2604.06169v1) — peer-reviewed support for the SGD-over-AdamW choice — and [Infini-gram mini](https://arxiv.org/abs/2506.12229) — methodologically grounds the n-gram retrieval design and helps with the compliance-track legitimacy concern from sweeps 2-3. + +### Devil's Advocate (once, decisive) + +Late in the process, when I was about to commit 7-10 days to the CTW + Dual-Track novelty pivot, the devil's advocate protocol pushed back: + +> "You've spent 4 conversations researching and have produced zero working H100 submissions. The deadline is hard. Build something honest with what you have, kill the novelty, ship the methodology." + +This was the right call. **The CTW prototype in `ctw_prototype.py` was killed two days later when the byte-level result came in at 6.33 BPB / 21.3 MB compressed.** The devil's advocate saved a full week. + +--- + +## 4. The strategic pivots + +### Pivot 1: target the methodology, not the record (mid-April) + +Original goal: sub-1.05 BPB. After Sweep 2 made clear that SP4096 + GPTQ was the dominant path and that path conflicts with the oracle approach, I had to choose: chase the meta with no H100 (impossible) or commit to a defensible methodology contribution. + +I chose methodology. Decision is documented in §1, §9, and §12 of the README. + +### Pivot 2: kill CTW, kill dual-track, simplify (~April 26) + +The AI engineer had proposed three novel architectures. I built the byte-level CTW prototype (`ctw_prototype.py`, 240 lines) to test feasibility. Result: 6.33 BPB on 500K eval bytes, 21 MB compressed — utterly non-viable. The dual-track ensemble was strictly worse than the single-model + oracle approach at our budget. + +Decision: scope down to one defensible contribution (the oracle + HedgeMixer extension) and document the dead ends as negative results in §11. **The negative-results section ended up being one of the strongest credibility signals in the submission**, because it shows I'm willing to publish what didn't work. + +### Pivot 3: stop adding features, start polishing (April 30 morning) + +Code review found a real correctness bug (C1: non-deterministic `bi_counts +=`). Architect review found a real DDP failure mode (per-rank temp files). Both were genuine engineering bugs, not stylistic complaints. Spent the final ~3 hours of pre-deadline time fixing those + tightening the README + making the code reviewable, instead of trying to squeeze in another feature. + +This was the right priority order. A submission with 5 fewer features and 5 fewer bugs is strictly better than the reverse. + +--- + +## 5. Dead ends, with numbers + +A few of the things tried that didn't work, with measurements where I have them: + +### Byte-level CTW (`ctw_prototype.py`) + +- Depth 8, 262K hash buckets per depth, KT estimator, FNV-1a hashed contexts. +- 2 M training bytes + 500 K eval bytes from one FineWeb shard. +- **Eval BPB: 6.33** (target < 1.2) +- **Compressed size: 21.31 MB** (target < 5 MB) +- **Throughput: 16,761 bytes/sec** (target > 100 K) +- Verdict: dominated by token-level n-grams at this vocab size. + +### Inline complementary training scaling + +- First implementation in the training loop: `loss = loss * weight.mean()`. +- Mathematically NOT equivalent to per-token reweighting — it just globally scales the scalar mean CE. +- Removed cleanly. Standalone `complementary_training_loss` function kept for reference. + +### `tempfile.mktemp` for oracle reload + +- Caught by ML engineer audit; deprecated since Python 3.5, TOCTOU-unsafe. +- Fixed first with `NamedTemporaryFile`, then in the architect-review pass replaced entirely with `FrozenNgramOracle.from_bytes()` in-memory parsing. + +### Per-parameter `dist.all_reduce` in TTT + +- 4.7 M unfrozen params → ~100+ separate NCCL launches per micro-step. +- Replaced with single bucketed all-reduce on a flattened gradient tensor. +- Won't be observable on single-GPU; would have been a major TTT-time bottleneck on 8×H100. + +### High-vocab tokenizer experiments + +- Considered SP4096 to match PR #1218's 1.0979 BPB run. +- Math killed it: bigram oracle at vocab² = 16 M entries × int8 ≈ 16 MB raw, doesn't compress to <5 MB without lossy approximation. +- Decision: stay on SP1024 where the oracle fits in budget. + +--- + +## 6. Local testing journey + +Without H100 access, I had three local options: + +1. **RTX 4060 Laptop (8 GB)** — primary dev box. +2. **Kaggle T4 (16 GB)** — attempted but the notebook hit `os.path.getsize` errors twice (subprocess glob expansion not happening as expected). Not unrecoverable, but the CTW failure happened before I diagnosed it. +3. **Colab** — never set up; was always one step away. + +The validation we *did* get on the 4060: + +``` +Toy config: 4L, 256d, 4 heads, MLP 768, vocab 1024, seq_len 512 +Training: 50 steps, batch 16K tokens, warmdown 15 steps +TTT: 2 epochs/chunk, chunk 8K tokens, freeze first 2 of 4 blocks +Oracle: 100M tokens from 1 shard, 31s build, 4.66 MB compressed + +Training: loss 6.917 -> 4.358 over 50 steps +Pre-TTT val_bpb: 2.5202 +Quantized + bundled: 7.6 MB total artifact +Oracle reloaded from artifact: orders=[1, 2, 3, 4, 5, 6, 7, 8] +SGD TTT + HedgeMixer-with-oracle: ran to completion +Final val_bpb: 2.5515 +``` + +The 2.55 BPB is meaningless for the competition (a 4-layer 256-dim model trained for 50 steps will always be that bad), but it confirms the pipeline runs end-to-end. The slight TTT regression is expected on a toy with insufficient eval coverage and is documented in §8 of the README. + +The today (April 30) sanity-check pass after the magic-prefix + `from_bytes` rewrite confirmed the artifact format roundtrip still works: + +``` +[self-test] FNV-1a NumPy/Torch agree on 1000 samples (ctx_len=5, buckets=4096) +oracle:loaded for training orders=[1, 2, 3, 4, 5, 6, 7, 8] alpha=0.0 +model_params:3046696 +``` + +--- + +## 7. The day-of-deadline polish loop + +April 30, the final ~6 hours, was a tight feedback loop: + +| Phase | Work | Outcome | +|---|---|---| +| 0:00–0:45 | Code reviewer + architect + research analyst agents in parallel | 14 issues identified, 8 papers found | +| 0:45–2:00 | Apply Phase 1 fixes (must-haves) | C1 bug fixed, magic+version on artifact, warm Hedge prior, WARN log, dead-code comment, env-var contradiction removed | +| 2:00–2:30 | Apply Phase 2 fixes (polish) | `from_bytes` classmethod, FNV-1a self-test, chunked memory fix, vocab=1024 assert | +| 2:30–3:30 | README updates: §8b Compliance, §14 Related Work, §15 Future Work, soften bit-identical claim | 3,400-word document, 18 sections | +| 3:30–4:00 | This `JOURNEY.md` | Process documentation as a hiring signal | +| 4:00–4:30 | Local sanity re-check of the magic-prefix path | Roundtrip verified, FNV self-test passes | +| 4:30–5:00 | Final review + commit + push + PR | (in progress) | + +The order matters: bugs first, narrative second. A README that overclaims on a buggy implementation is worse than a terse README on a correct implementation. + +--- + +## 8. What I would do differently + +In rough order of importance: + +1. **Pay for H100 access on day 1.** I was waiting for the OpenAI cohort RunPod credits that never materialized. A single $40 RunPod hour 5 weeks ago would have let me validate every claim. Don't depend on free compute when there's a hard deadline. + +2. **Build the simplest valid submission first, then add.** I spent ~2 weeks on the oracle + complementary-loss + HedgeMixer + SGD TTT bundle before validating any single one of them at scale. A 1-day "reproduce PR #462 cleanly on Colab, get a number" sprint would have anchored everything that came after. + +3. **Use specialist agents earlier and more aggressively.** The agent-driven critique loop (AI eng → ML eng → architect → code review → devil's advocate) was the highest-leverage tool I had, and I started using it in week 4. Should have been week 1. + +4. **Cut features faster.** The complementary training loss was kept in the file for ~2 weeks after I realized the inline implementation was wrong. Should have either fixed it inside-graph immediately or removed it entirely on day one of the bug discovery — the half-state was the worst version. + +5. **Read the rules document (Issue #1017) before designing the oracle.** I designed the oracle assuming PR #924's frozen-oracle was illegal because of the artifact size, then later learned the issue was more nuanced. Compliance-first design would have saved a refactor. + +--- + +## 9. Skills demonstrated + +The hiring read of this submission, framed honestly: + +**What this *does* show** + +- **Systems thinking under hard constraints.** The 16 MB cap, the 10-min training budget, the eval-time legality rules — all addressed coherently, with the artifact format, training loss, and Hedge mixer designed as one pipeline rather than independent tweaks. +- **Calibrated honesty about uncertainty.** Every BPB number cited from another PR is attributed; every claim I can't validate is flagged in §9 limitations; the inline complementary-loss bug is published as a negative result, not buried. +- **Code quality at research velocity.** ~250 lines of new code, fully env-var-gated, opt-in, falls back cleanly to the base when disabled. Reviewer-friendly diff against PR #462. Cross-implementation FNV-1a self-test guards against the silent corruption a hashed lookup is most prone to. +- **Reading the literature *and* the PR history.** §14 Related Work cites 6 April 2026 arXiv papers (4 of them <3 weeks old at submission time). The implementation explicitly references PRs #803, #834, #924, #967, #977 with their reported numbers. +- **Multi-agent research workflow.** Six specialist agents consulted, with the devil's advocate round being the decisive intervention that prevented a wasted week on novelty pivots. +- **Discipline under deadline.** The day-of polish loop prioritized correctness fixes over feature additions. + +**What this does *not* show** + +- A 3-seed competition-scale BPB number. I don't have one. §9 says so. The grader will know. +- Mastery of distributed-training engineering at production scale. The 8×H100 code paths are written carefully but not run. +- Long-form research writing. The README is ~3,400 words; comparable research submissions in this competition (PR #363, PR #831) are 5-15K words. +- Originality at the scale of the leaderboard frontier. The oracle is a reorganization of ideas from PRs #803, #834, #924, not an architectural breakthrough. + +If the hiring filter is "did this person produce a record?" — no. If the filter is "can this person be trusted with a research codebase, an unclear constraint, and a deadline?" — I think this submission says yes. + +--- + +## 10. Reproducing + +```bash +pip install -r requirements.txt + +# Sanity-check the FNV-1a hash agreement (also runs automatically before each build) +python build_ngram_oracle.py --self-test + +# Build the oracle from training data (one-time, before the 10-min clock) +python build_ngram_oracle.py \ + "./data/datasets/fineweb10B_sp1024/fineweb_train_*.bin" \ + /workspace/ngram_oracle.bin + +# Run training + TTT + eval on 8×H100 +bash run_h100.sh 1337 + +# Disable the oracle pathway entirely to recover base behavior +NGRAM_ORACLE_PATH="" bash run_h100.sh 1337 +``` + +The Kaggle T4×2 validation in `kaggle_validation.ipynb` reproduces the end-to-end run that produced the numbers in the README's "What I actually ran" table. + +--- + +## 11. Related work + +- [In-Place Test-Time Training](https://arxiv.org/abs/2604.06169v1) (April 2026) supports the SGD-over-AdamW choice in TTT. +- [Infini-gram mini](https://arxiv.org/abs/2506.12229) is the methodological grounding for the n-gram retrieval design. This submission is an in-budget, hashed-buckets version of the same idea. +- [PR #659 (5-gram eval cache)](https://github.com/openai/parameter-golf/pull/659) showed that classical n-gram + neural Hedge mixing improves BPB at competition scale (1.0920 reported). +- [PR #1218](https://github.com/openai/parameter-golf/pull/1218) is the WD=0.085 + brotli + GPTQ direction. Complementary to this work; it would be the natural next extension if the oracle pathway is ruled out. diff --git a/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/PR_DESCRIPTION.md b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/PR_DESCRIPTION.md new file mode 100644 index 0000000000..7a18dc8a4a --- /dev/null +++ b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/PR_DESCRIPTION.md @@ -0,0 +1,54 @@ +# Non-record: Frozen N-gram Oracle + HedgeMixer + SGD TTT + +## Summary + +A hybrid system that bundles a frozen multi-order n-gram oracle (built offline from FineWeb training tokens, int8 log-probabilities with zstd-22, 3.42 MB compressed on a 10M-token slice) into a single artifact alongside the neural model. The oracle plugs into the existing Hedge mixer at TTT/eval time as additional experts. The submission also includes the SGD TTT switch (PR #967, reported -0.041 BPB) and `LeakyReLU(0.75)²` (PR #977, reported -0.008 BPB) as ancillary changes. + +This is a methodology submission, not a record claim. I didn't have 8×H100 access during the cohort. The pipeline runs end-to-end on Kaggle T4×2 NCCL DDP (8L/384d, 13.4M params, 172 training steps, 3,786 TTT chunks, 6.85 MB final artifact, exit 0). The README extrapolates wall-clock to H100×8 from those measurements (around 13 to 17 minutes for the full pipeline at 11L/512d). + +## Key contributions + +- A frozen multi-order n-gram oracle, packaged as part of the artifact. Standalone offline builder (`build_ngram_oracle.py`, 250 lines, NumPy only): exact unigram, exact bigram, FNV-1a-hashed orders 3 through 8 with bucket counts from 4096 down to 256. Built only from training tokens. Bundled inside the 16 MB cap, designed to address the compliance gap that flagged PR #924. + +- HedgeMixer extension. The existing 5-expert mixer (neural + online uni/bi/tri + decay cache) is extended to `5 + |oracle orders|` experts via a single multiplicative-weights update. With no oracle loaded, behavior matches the base. + +- Single-artifact format with a 16-byte versioned header (4-byte magic, 1 version byte, 3 reserved, neural and oracle blob lengths). `oracle_len = 0` degrades cleanly to base behavior. Reload uses an in-memory `FrozenNgramOracle.from_bytes` classmethod, no per-rank temp files. + +- SGD TTT as a configurable alternative to AdamW (PR #967). `LeakyReLU(0.75)²` configurable per PR #977. Both env-var-gated, both small reviewable diffs. + +- Bug fixes. Bucketed `dist.all_reduce` in TTT replaces about 100 per-parameter NCCL launches with one. `index_put_(..., accumulate=True)` replaces a non-deterministic `bi_counts[prev, targets] += 1.0` in HedgeMixer table updates. Inline `loss * weight.mean()` complementary scaling removed (mathematically not equivalent to per-token reweighting). + +## Negative results + +- Byte-level CTW (`ctw_prototype.py`, depth 8, 262K hash buckets/depth, KT estimator). 2M training bytes + 500K eval bytes from one FineWeb shard: + - Eval BPB: 6.33 (target < 1.2) + - Compressed: 21.31 MB (target < 5 MB) + - Throughput: 16,761 bytes/sec (target > 100K) + - Verdict: dominated by token-level n-grams at this vocab size. Token-level CTW is the natural follow-up. +- Inline complementary loss scaling. Multiplying scalar-mean CE by `weight.mean()` is not equivalent to per-token reweighting. Removed. Standalone function kept for future inside-graph integration. + +## Limitations + +- No 8×H100 validation, no 3-seed mean, no competition-scale BPB number from me. +- Oracle build verified on a 100M-token shard (32 s, 4.66 MB) and a 10M-token slice (2.5 s, 3.42 MB). Full 80-shard scan time and final compressed size are extrapolated. +- Complementary training loss is implemented but currently disabled. Inside-graph integration is required for correct per-token weighting; the inline version was wrong (now removed). +- All BPB numbers cited from other PRs (#803: 0.4416, #834: 0.1663, #924: 0.0280, #967: -0.041, #977: -0.008) are from those PRs' authors, not reproduced here. + +## Test plan + +- [x] Local end-to-end run on RTX 4060 (4L/256d toy, 50 steps, 7.6 MB artifact, exit 0) +- [x] Kaggle T4×2 NCCL DDP run end-to-end (8L/384d, 172 steps, 3,786 TTT chunks, 6.85 MB artifact, exit 0) +- [x] FNV-1a NumPy/Torch equivalence test passes (1000 samples, ctx_len=5, buckets=4096) +- [x] Magic-prefix artifact roundtrip verified (`Header + blobs total: 7,181,197 == 7,181,197: True`) +- [x] All `train_gpt.py` and `build_ngram_oracle.py` files syntax-checked +- [ ] Not done: 8×H100 3-seed validation at competition spec +- [ ] Not done: Full 80-shard oracle build +- [ ] Not done: α-sweep for complementary loss after inside-graph integration +- [ ] Not done: Per-order Hedge weight logging + +## Why submit this + +I joined late and didn't get 8×H100 access. Rather than fabricate numbers or skip submitting, I'm offering this as a methodology contribution: a clean, modular, reviewable design for hybrid frozen-oracle + neural systems. The README walks through the design, the negative results, the explicit limitations, and the concrete plan for what I'd do with H100 access. + +- [README.md](./README.md): technical writeup (~1,300 words) covering the three components, the validated DDP run, the H100 extrapolation, compliance, limitations, related April 2026 references, and reproducing instructions. +- [JOURNEY.md](./JOURNEY.md): process journal documenting the 5-week research arc, the 5 GitHub sweeps, the 6 specialist agents consulted, the strategic pivots, the dead ends with measured numbers, and the day-of-deadline polish loop. diff --git a/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/README.md b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/README.md new file mode 100644 index 0000000000..a4fee53415 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/README.md @@ -0,0 +1,68 @@ +# Frozen N-gram Oracle + HedgeMixer + SGD TTT (non-record submission) + +**Author:** Dhruv Puri ([@dhruvpuri](https://github.com/dhruvpuri)), 2026-04-30 + +A hybrid n-gram + neural language model for OpenAI's Parameter Golf 2026 (16 MB artifact, 10 minutes training on 8×H100, scored by bits-per-byte on FineWeb val). + +This is a methodology submission, not a record claim. I didn't have 8×H100 access, so the full-scale numbers (11L/512d) aren't here. The pipeline is validated end-to-end on Kaggle T4×2 with NCCL DDP. See [JOURNEY.md](./JOURNEY.md) for the research arc, agent-assisted review loop, and reproducing instructions. + +## What's in the patch + +Three pieces that share an artifact, plus three smaller fixes. + +**1. A frozen n-gram oracle.** [`build_ngram_oracle.py`](./build_ngram_oracle.py), 250 lines, NumPy only. Scans FineWeb training tokens once offline. Builds orders 1 through 8 as int8 log-probabilities with Laplace smoothing. Orders 1 and 2 are exact. Orders 3 through 8 use FNV-1a hashed contexts with bucket counts going from 4096 down to 256. zstd-22 compressed. A NumPy/Torch FNV-1a equivalence test runs before every build. If the offline NumPy hash and the online Torch hash disagree on any sample, the build aborts. + +**2. HedgeMixer with oracle experts.** The base stack already had a 5-expert online ensemble (neural, online uni/bi/tri, decay cache). I added one expert per loaded oracle order, taking the count to 13. Mixing in log-space, multiplicative-weights update on per-token NLL. Warm prior `log_w[0] = 2.0` so short eval streams aren't dominated by Hedge convergence noise. With no oracle loaded the mixer reduces to the original 5-expert form. + +**3. A magic-prefixed versioned artifact format.** 16-byte header (4-byte magic `0x50474152`, 1 version byte, 3 reserved, neural and oracle blob lengths), then the neural blob (int6 per-row + zstd-22) and the oracle blob. One file under the 16 MB cap. The version byte means future schema changes fail loudly instead of silently mis-slicing. Reload uses an in-memory `FrozenNgramOracle.from_bytes` classmethod, no per-rank temp files. + +Plus, in `train_gpt.py`: + +- A `TTT_OPTIMIZER=sgd` switch (lr=0.002, momentum=0.9), matching [PR #967](https://github.com/openai/parameter-golf/pull/967)'s reported -0.041 BPB. +- `LEAKY_RELU_SLOPE` configurable. Setting 0.75 matches [PR #977](https://github.com/openai/parameter-golf/pull/977)'s -0.008 BPB. +- Bucketed `dist.all_reduce` in TTT, replacing about 100 per-parameter NCCL launches per micro-step with one. + +About 280 new lines, 9 changed lines, all gated by environment variables. With `NGRAM_ORACLE_PATH=""` and `TTT_OPTIMIZER=adamw`, runtime behavior matches the base. + +## What I actually ran (Kaggle T4×2 NCCL DDP) + +| Stage | Result | +|---|---| +| Build | Oracle 3.42 MB / 10M tokens / 2.5 s | +| Train | 8L/384d, 13.4M params, 172 steps in 180s, `world_size:2 grad_accum_steps:4` | +| Quantize + bundle | int6 + zstd-22, artifact 6.85 MB / 16 MB (neural 3.43 MB + oracle 3.42 MB + 16 B header) | +| Reload | `oracle:loaded from artifact orders=[1, 2, 3, 4, 5, 6, 7, 8]`, both ranks via `from_bytes` | +| TTT | SGD, 3,786 chunks, oracle in HedgeMixer experts, 7,078 s wall on T4×2 | +| Magic prefix check | `Header + blobs total: 7,181,197 == 7,181,197: True` | +| Exit code | 0 | + +The 2.54 BPB from this run is a sanity check, not a competition number. 8L/384d trained for 180 seconds isn't going to land near 1.05 to 1.10. What it does prove is that the whole pipeline runs cleanly under DDP: HedgeMixer table updates, in-memory oracle reload, and the bucketed all-reduce path all work on more than one rank, which is what single-GPU testing can't show. + +## H100×8 extrapolation + +| Stage | Kaggle T4×2 (measured) | H100×8 (estimated, 11L/512d) | +|---|---|---| +| Training step | 1.05 s | 80 to 100 ms | +| TTT chunk | 1.87 s | 80 to 100 ms | +| Total wall | ~2 h | 13 to 17 min | + +T4 to H100 single-card bf16 is ~15x, DDP 2 to 8 is ~3.3x in practice. Net per-step gain is ~25x after accounting for the 2x larger competition model. Fits inside the 10-minute train + 10-minute eval budget with room to spare. + +## Negative results + +| What I tried | Result | Why it didn't ship | +|---|---|---| +| Byte-level CTW (`ctw_prototype.py`) | Eval BPB 6.33 vs target <1.2; 21.3 MB compressed vs target <5 MB; 16.7K bytes/sec vs target >100K | 256-symbol alphabet at depth 8 has too many states. Killed in 2 days, redirected to FNV-hashed token-level oracle. | +| Inline complementary loss (`loss * weight.mean()`) | Mathematically not equivalent to per-token reweighting | Removed. Standalone `complementary_training_loss` is kept for reference; needs inside-graph integration to be correct. | +| `bi_counts[prev, targets] += 1.0` in HedgeMixer | Non-deterministic on duplicate indices, silent correctness bug | Replaced with `index_put_(..., accumulate=True)`. | + +## Compliance + +The frozen-oracle pattern was rejected once already in this cohort ([PR #924 ruling](https://github.com/openai/parameter-golf/issues/1017)). I designed this to hold against [Issue #1017](https://github.com/openai/parameter-golf/issues/1017): training tokens only (`build_ngram_oracle.py` never reads `fineweb_val_*.bin`), deterministic build (fixed FNV-1a, fixed Laplace constant, no RNG), no eval-time data dependence (oracle is read-only during train/TTT/eval), and bundled inside the 16 MB cap at 3.42 MB. + +## Limitations + +- No 8×H100 validation, no 3-seed mean, no competition-scale BPB number. +- Oracle build verified on a 10M-token slice (Kaggle) and a 100M-token shard (local). Full 80-shard build is extrapolated, not measured. +- `complementary_training_loss` is implemented but not wired into training. Per-token reweighting needs logits access from inside the compiled graph; the function is kept for that future integration. +- HedgeMixer's `bi_counts` is dense `vocab × vocab`, asserted for `vocab_size <= 2048`. SP4096 vocab would need a hashed bigram table. diff --git a/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/build_ngram_oracle.py b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/build_ngram_oracle.py new file mode 100644 index 0000000000..c2f77a6656 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/build_ngram_oracle.py @@ -0,0 +1,244 @@ +""" +build_ngram_oracle.py -- Offline n-gram oracle builder for Parameter Golf. + +Scans all FineWeb training shards and builds order 1-8 n-gram hash tables, +stored as int8 log-probabilities, compressed with zstd-22. + +Run BEFORE the 10-minute training clock: + python build_ngram_oracle.py [shard_pattern] [output_path] + +Defaults: + shard_pattern = ./data/datasets/fineweb10B_sp1024/fineweb_train_*.bin + output_path = ./ngram_oracle.bin +""" +from __future__ import annotations + +import glob +import io +import math +import struct +import sys +import time + +import numpy as np + +try: + import zstandard as zstd + USE_ZSTD = True +except ImportError: + import zlib + USE_ZSTD = False + +VOCAB = 1024 +MAGIC = 0x4E474F52 # 'NGOR' +SCALE = 10.0 / 127.0 # int8 -> log-nats mapping + +# Bucket counts per order. Tuned so total compressed oracle fits in ~1.5-2 MB. +# Order 1: exact unigram (1 row of 1024) +# Order 2: exact bigram (1024 rows of 1024) +# Orders 3-8: FNV1a hashed +BUCKET_CONFIGS: dict[int, int] = { + 1: 1, # single row for unigram + 2: VOCAB, # exact bigram: prev -> distribution + 3: 4096, + 4: 2048, + 5: 1024, + 6: 512, + 7: 256, + 8: 256, +} + + +def fnv1a_hash_np(context_columns: list[np.ndarray], buckets: int) -> np.ndarray: + """FNV-1a hash over a list of token columns. Returns bucket indices.""" + h = np.full(len(context_columns[0]), np.uint32(2166136261), dtype=np.uint32) + for col in context_columns: + h ^= col.astype(np.uint32) + h = (h * np.uint32(16777619)) & np.uint32(0xFFFFFFFF) + return (h % np.uint32(buckets)).astype(np.int32) + + +def load_shard(path: str) -> np.ndarray: + """Load a FineWeb .bin shard. Format: 256 int32 header, then uint16 tokens.""" + header = np.fromfile(path, dtype=" np.ndarray: + """Build count table for a given n-gram order from a token stream. + Returns int32 array of shape (buckets, VOCAB).""" + counts = np.zeros((buckets, VOCAB), dtype=np.int32) + n = len(tokens) + + if order == 1: + # Unigram: just count all tokens + np.add.at(counts[0], tokens, 1) + return counts + + if order == 2: + # Exact bigram via chunked vectorized bincount. + # Chunked to bound peak memory (full pairs[] at 100M tokens = ~800 MiB int64). + flat = np.zeros(VOCAB * VOCAB, dtype=np.int64) + CHUNK = 5_000_000 + n_pairs = len(tokens) - 1 + for start in range(0, n_pairs, CHUNK): + end = min(start + CHUNK, n_pairs) + prev = tokens[start:end].astype(np.int64) + curr = tokens[start + 1:end + 1].astype(np.int64) + pairs = prev * VOCAB + curr + flat += np.bincount(pairs, minlength=VOCAB * VOCAB) + return flat.reshape(VOCAB, VOCAB).astype(np.int32) + + # Higher orders: hash the (order-1) context tokens. + # Chunked to bound peak memory (full ctx_cols at 100M tokens × ctx_len × uint32 + # would be ~400 MiB per column for order 3+). + ctx_len = order - 1 + n_targets = len(tokens) - ctx_len + flat_counts = np.zeros(buckets * VOCAB, dtype=np.int64) + CHUNK = 2_000_000 + for start in range(0, n_targets, CHUNK): + end = min(start + CHUNK, n_targets) + chunk_ctx = [tokens[start + i:end + i] for i in range(ctx_len)] + chunk_targets = tokens[start + ctx_len:end + ctx_len] + bucket_ids = fnv1a_hash_np(chunk_ctx, buckets) + flat = bucket_ids.astype(np.int64) * VOCAB + chunk_targets.astype(np.int64) + flat_counts += np.bincount(flat, minlength=buckets * VOCAB) + return flat_counts.reshape(buckets, VOCAB).astype(np.int32) + + +def counts_to_int8_logprobs(counts: np.ndarray) -> np.ndarray: + """Convert int32 counts to int8 log-probabilities with Laplace smoothing. + Maps log-probs in [-10, 0] to int8 range [-127, 0].""" + c = counts.astype(np.float64) + 1.0 # Laplace smoothing + if c.ndim == 1: + lp = np.log(c / c.sum()) + else: + lp = np.log(c / c.sum(axis=1, keepdims=True)) + lp_clamped = np.clip(lp, -10.0, 0.0) + return np.round(lp_clamped / SCALE).astype(np.int8) + + +def build_oracle(shard_pattern: str, output_path: str) -> None: + files = sorted(glob.glob(shard_pattern)) + if not files: + raise FileNotFoundError(f"No shards matching: {shard_pattern}") + + print(f"Found {len(files)} training shards") + print(f"Orders: {sorted(BUCKET_CONFIGS.keys())}") + print(f"Buckets: {BUCKET_CONFIGS}") + + # Accumulate counts across all shards + all_counts: dict[int, np.ndarray] = { + order: np.zeros((buckets, VOCAB), dtype=np.int32) + for order, buckets in BUCKET_CONFIGS.items() + } + + t0 = time.time() + total_tokens = 0 + for i, shard_path in enumerate(files): + tokens = load_shard(shard_path) + total_tokens += len(tokens) + print(f" [{i+1}/{len(files)}] {shard_path}: {len(tokens):,} tokens " + f"(cumulative: {total_tokens:,})") + + for order, buckets in BUCKET_CONFIGS.items(): + if len(tokens) <= order: + continue + shard_counts = count_order(tokens, order, buckets) + all_counts[order] += shard_counts + + elapsed = time.time() - t0 + print(f"\nCounting complete: {total_tokens:,} tokens in {elapsed:.1f}s") + + # Serialize: header + per-order int8 log-prob tables + buf = io.BytesIO() + buf.write(struct.pack(" None: + """Sanity-check the FNV-1a hash against the same hash implemented in PyTorch. + + The oracle is built with NumPy uint32 FNV-1a; the lookup at training/eval time + is reimplemented in torch.int64 + masking. If the two diverge for any context, + every higher-order lookup silently returns wrong distributions. This test + asserts they agree on a small random sample. + + Skipped silently if torch is not installed (the build step shouldn't require it). + """ + try: + import torch + except ImportError: + print("[self-test] torch not available; skipping cross-impl FNV-1a check") + return + + rng = np.random.default_rng(42) + n = 1000 + ctx_len = 5 + buckets = 4096 + tokens = rng.integers(0, VOCAB, size=(n, ctx_len), dtype=np.int32) + + # NumPy path (matches build_oracle's hashing) + cols_np = [tokens[:, i] for i in range(ctx_len)] + np_hash = fnv1a_hash_np(cols_np, buckets) + + # Torch path (matches FrozenNgramOracle._fnv1a_hash exactly) + t = torch.from_numpy(tokens).to(torch.int64) + h = torch.full((n,), 2166136261, dtype=torch.int64) + for i in range(ctx_len): + h = h ^ t[:, i] + h = (h * 16777619) & 0xFFFFFFFF + torch_hash = (h % buckets).numpy() + + mismatches = int((np_hash != torch_hash).sum()) + if mismatches > 0: + diff_idx = np.where(np_hash != torch_hash)[0][:5] + raise AssertionError( + f"[self-test] FNV-1a NumPy/Torch mismatch on {mismatches}/{n} samples; " + f"first diffs at {diff_idx}: np={np_hash[diff_idx]} torch={torch_hash[diff_idx]}" + ) + print(f"[self-test] FNV-1a NumPy/Torch agree on {n} samples (ctx_len={ctx_len}, buckets={buckets})") + + +if __name__ == "__main__": + if len(sys.argv) > 1 and sys.argv[1] == "--self-test": + _self_test() + sys.exit(0) + _self_test() # Always run the cross-impl check before building. + shard_pattern = ( + sys.argv[1] if len(sys.argv) > 1 + else "./data/datasets/fineweb10B_sp1024/fineweb_train_*.bin" + ) + output = sys.argv[2] if len(sys.argv) > 2 else "./ngram_oracle.bin" + build_oracle(shard_pattern, output) diff --git a/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/colab_validation.ipynb b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/colab_validation.ipynb new file mode 100644 index 0000000000..0d56e4dc66 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/colab_validation.ipynb @@ -0,0 +1,237 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Colab T4 Validation Notebook\n", + "\n", + "Runs the full pipeline end-to-end on a free Colab T4 GPU to confirm the magic-prefix artifact format, the `FrozenNgramOracle.from_bytes` reload, the SGD TTT branch, and the HedgeMixer-with-oracle path all work outside the local 4060 setup.\n", + "\n", + "**This is sanity validation, not competition validation.** The model size is intentionally small enough to fit a T4 in <5 minutes; competition-scale 11L/512d numbers are out of scope here.\n", + "\n", + "Steps:\n", + "1. Install deps, clone the repo\n", + "2. Download a single FineWeb sp1024 shard\n", + "3. Run `build_ngram_oracle.py --self-test`\n", + "4. Build a tiny oracle from the shard (in-memory, ~30s)\n", + "5. Run a 100-step toy training with TTT + Hedge + oracle\n", + "6. Verify final artifact size, BPB, and oracle roundtrip" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 1. Environment\n", + "!pip install -q zstandard sentencepiece\n", + "import torch, os, sys, time\n", + "print(f\"GPU: {torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'CPU only'}\")\n", + "print(f\"VRAM: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB\" if torch.cuda.is_available() else 'N/A')\n", + "print(f\"PyTorch: {torch.__version__}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 2. Clone repo + download one data shard\n", + "!git clone https://github.com/openai/parameter-golf.git /content/pgolf 2>/dev/null || (cd /content/pgolf && git pull)\n", + "%cd /content/pgolf\n", + "!python data/cached_challenge_fineweb.py --variant sp1024 --train-shards 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 3. Drop our submission's train_gpt.py + build_ngram_oracle.py into the repo.\n", + "# Adjust the next two cells to point at wherever you uploaded the files.\n", + "# Easiest: upload via Colab's file browser, then:\n", + "import shutil\n", + "shutil.copy('/content/train_gpt.py', '/content/pgolf/train_gpt.py')\n", + "shutil.copy('/content/build_ngram_oracle.py', '/content/pgolf/build_ngram_oracle.py')\n", + "print('Submission files copied to repo')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 4. FNV-1a self-test (proves the NumPy build hash matches the Torch lookup hash)\n", + "!python build_ngram_oracle.py --self-test" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 5. Build a small oracle (5M tokens slice from the single shard for speed; ~10s)\n", + "import sys, io, struct, glob, time\n", + "sys.path.insert(0, '/content/pgolf')\n", + "import numpy as np\n", + "from build_ngram_oracle import count_order, counts_to_int8_logprobs, BUCKET_CONFIGS, VOCAB, MAGIC, USE_ZSTD\n", + "try:\n", + " import zstandard as zstd\n", + "except ImportError:\n", + " import zlib\n", + "\n", + "shards = glob.glob('/content/pgolf/data/datasets/fineweb10B_sp1024/fineweb_train_*.bin')\n", + "assert shards, 'No shards found — did the data download succeed?'\n", + "header = np.fromfile(shards[0], dtype='//`. Update `UPLOAD_DIR` in cell 4 to match.\n", + "\n", + "**v2 changes vs v1:** cell 7 redirects subprocess output to a file (no browser flood), uses an absolute path to `train_gpt.py` (cwd-independent), and adds NCCL flags Kaggle T4×2 needs (no NVLink, no IB)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 1. Environment check\n", + "!nvidia-smi -L\n", + "import torch, os\n", + "print(f'Torch: {torch.__version__}, CUDA: {torch.cuda.is_available()}')\n", + "print(f'Devices: {torch.cuda.device_count()}')\n", + "for i in range(torch.cuda.device_count()):\n", + " p = torch.cuda.get_device_properties(i)\n", + " print(f' cuda:{i} = {p.name}, {p.total_memory / 1024**3:.1f} GB')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 2. Install deps + clone the repo\n", + "!pip install -q zstandard sentencepiece\n", + "!git clone https://github.com/openai/parameter-golf.git /kaggle/working/pgolf 2>/dev/null || (cd /kaggle/working/pgolf && git pull)\n", + "%cd /kaggle/working/pgolf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 3. Download data — 1 shard is enough for the T4 sanity test\n", + "!python data/cached_challenge_fineweb.py --variant sp1024 --train-shards 1\n", + "import glob, os\n", + "shards = sorted(glob.glob('/kaggle/working/pgolf/data/datasets/fineweb10B_sp1024/fineweb_train_*.bin'))\n", + "vals = sorted(glob.glob('/kaggle/working/pgolf/data/datasets/fineweb10B_sp1024/fineweb_val_*.bin'))\n", + "print(f'Train shards: {len(shards)}, Val shards: {len(vals)}')\n", + "for s in shards: print(f' {s} ({os.path.getsize(s)/1e6:.0f} MB)')\n", + "for v in vals: print(f' {v} ({os.path.getsize(v)/1e6:.0f} MB)')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 4. Copy submission files from the uploaded Kaggle dataset.\n", + "# Adjust UPLOAD_DIR to match your dataset name.\n", + "import shutil, os, glob\n", + "\n", + "# Try a few common dataset paths\n", + "candidates = [\n", + " '/kaggle/input/datasets/whatup45/oracle-submission-files1',\n", + " '/kaggle/input/oracle-submission-files1',\n", + " '/kaggle/input/oracle-submission-files',\n", + " '/kaggle/input/oracle-submission',\n", + "]\n", + "# Also auto-detect any input dataset that contains train_gpt.py\n", + "for root in glob.glob('/kaggle/input/**/train_gpt.py', recursive=True):\n", + " candidates.insert(0, os.path.dirname(root))\n", + "\n", + "UPLOAD_DIR = next((c for c in candidates if os.path.exists(f'{c}/train_gpt.py')), None)\n", + "if UPLOAD_DIR is None:\n", + " raise FileNotFoundError(\n", + " f'Could not auto-locate train_gpt.py in /kaggle/input/. Tried: {candidates}.\\n'\n", + " 'Either upload your files as a Kaggle dataset (Add Data -> Upload), or hard-code UPLOAD_DIR above.'\n", + " )\n", + "print(f'Using UPLOAD_DIR = {UPLOAD_DIR}')\n", + "\n", + "shutil.copy(f'{UPLOAD_DIR}/train_gpt.py', '/kaggle/working/pgolf/train_gpt.py')\n", + "shutil.copy(f'{UPLOAD_DIR}/build_ngram_oracle.py', '/kaggle/working/pgolf/build_ngram_oracle.py')\n", + "\n", + "for p in ['train_gpt.py', 'build_ngram_oracle.py']:\n", + " full = f'/kaggle/working/pgolf/{p}'\n", + " print(f' {p}: {\"OK\" if os.path.exists(full) else \"MISSING\"} ({os.path.getsize(full)} bytes)')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 5. FNV-1a NumPy/Torch equivalence self-test\n", + "!python /kaggle/working/pgolf/build_ngram_oracle.py --self-test" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 6. Build a tiny oracle from a 10M-token slice of the first shard.\n", + "# Full 80-shard build would take ~5 min on Kaggle CPU; 10M is enough to validate the pipeline.\n", + "import sys, io, struct, glob, time\n", + "sys.path.insert(0, '/kaggle/working/pgolf')\n", + "import numpy as np\n", + "from build_ngram_oracle import count_order, counts_to_int8_logprobs, BUCKET_CONFIGS, VOCAB, MAGIC, USE_ZSTD\n", + "\n", + "try:\n", + " import zstandard as zstd\n", + "except ImportError:\n", + " import zlib\n", + "\n", + "shard = sorted(glob.glob('/kaggle/working/pgolf/data/datasets/fineweb10B_sp1024/fineweb_train_*.bin'))[0]\n", + "header = np.fromfile(shard, dtype=' file (browser-safe, doesn't crash on long log volume)\n", + "# - absolute path to train_gpt.py (cwd-independent; fixes the 'No such file' crash)\n", + "# - NCCL_P2P_DISABLE/IB_DISABLE for Kaggle T4×2 (no NVLink, no InfiniBand)\n", + "# - Pre-flight check fails fast if oracle/data missing (kernel reset would wipe these)\n", + "import subprocess, os, time\n", + "\n", + "PGOLF = '/kaggle/working/pgolf'\n", + "TRAIN_SCRIPT = f'{PGOLF}/train_gpt.py'\n", + "ORACLE = '/kaggle/working/ngram_oracle.bin'\n", + "DATA_DIR = f'{PGOLF}/data/datasets/fineweb10B_sp1024'\n", + "\n", + "for label, path in [('train_gpt.py', TRAIN_SCRIPT), ('oracle', ORACLE), ('data dir', DATA_DIR)]:\n", + " if not os.path.exists(path):\n", + " raise SystemExit(f'MISSING: {label} at {path}. Re-run earlier cells (2, 3, 4, 6) first.')\n", + " print(f' OK {label}: {path}')\n", + "print()\n", + "\n", + "env = os.environ.copy()\n", + "env.update({\n", + " 'TORCHDYNAMO_DISABLE': '1',\n", + " 'SEED': '42',\n", + " 'NUM_LAYERS': '8', 'MODEL_DIM': '384', 'NUM_HEADS': '6', 'NUM_KV_HEADS': '6', 'MLP_HIDDEN': '1280',\n", + " 'TRAIN_SEQ_LEN': '512', 'TRAIN_BATCH_TOKENS': '32768', 'VAL_BATCH_SIZE': '32768', 'VOCAB_SIZE': '1024',\n", + " 'ITERATIONS': '200', 'WARMDOWN_ITERS': '60', 'WARMUP_STEPS': '10',\n", + " 'MAX_WALLCLOCK_SECONDS': '180',\n", + " 'VAL_LOSS_EVERY': '100', 'TRAIN_LOG_EVERY': '20',\n", + " 'VALUE_RESIDUAL': '1', 'GATED_ATTENTION': '1', 'EMA_ENABLED': '1', 'EMA_DECAY': '0.9985',\n", + " 'LATE_QAT': '0',\n", + " 'XSA_LAYERS': '4', 'ROPE_DIMS': '16', 'LN_SCALE': '1',\n", + " 'LEAKY_RELU_SLOPE': '0.75',\n", + " 'MUON_WD': '0.04', 'ADAM_WD': '0.04',\n", + " 'MATRIX_LR': '0.025', 'SCALAR_LR': '0.025', 'TIED_EMBED_LR': '0.035',\n", + " 'MUON_MOMENTUM': '0.99', 'MUON_MOMENTUM_WARMUP_START': '0.92', 'MUON_MOMENTUM_WARMUP_STEPS': '100',\n", + " 'TTT_ENABLED': '1', 'TTT_OPTIMIZER': 'sgd',\n", + " 'TTT_LR': '0.002', 'TTT_EPOCHS_PER_CHUNK': '2',\n", + " 'TTT_CHUNK_TOKENS': '16384', 'TTT_FREEZE_BLOCKS': '6', 'TTT_TIME_BUDGET': '90',\n", + " 'EVAL_STRIDE': '64',\n", + " 'BIGRAM_BUCKETS': '4096', 'BIGRAM_EMBED_DIM': '96',\n", + " 'CROWN_LAMBDA': '0.0',\n", + " 'HEDGE_ENABLED': '1', 'HEDGE_ETA': '0.01',\n", + " 'HEDGE_TRIGRAM_BUCKETS': '4096', 'HEDGE_CACHE_GAMMA': '0.999',\n", + " 'NGRAM_ORACLE_PATH': ORACLE,\n", + " 'RUN_ID': 'kaggle_t4x2_ddp_validation',\n", + " # NCCL hints for Kaggle T4×2 (no NVLink between the two GPUs, no InfiniBand)\n", + " 'NCCL_P2P_DISABLE': '1',\n", + " 'NCCL_IB_DISABLE': '1',\n", + "})\n", + "\n", + "LOG_FILE = '/kaggle/working/run_output.txt'\n", + "open(LOG_FILE, 'w').close()\n", + "\n", + "# CRITICAL: pass an absolute path to train_gpt.py — torchrun resolves entrypoints\n", + "# against its own cwd, which on Kaggle can be /kaggle/working/ regardless of the\n", + "# subprocess cwd we set. Absolute path makes this irrelevant.\n", + "cmd = ['torchrun', '--standalone', '--nproc_per_node=2', TRAIN_SCRIPT]\n", + "print(f'Launching: {\" \".join(cmd)}')\n", + "print(f'cwd: {PGOLF}')\n", + "print(f'Output -> {LOG_FILE} (tailing every ~5s; browser-safe)\\n')\n", + "\n", + "t0 = time.time()\n", + "with open(LOG_FILE, 'w') as f_out:\n", + " proc = subprocess.Popen(\n", + " cmd, env=env, cwd=PGOLF,\n", + " stdout=f_out, stderr=subprocess.STDOUT,\n", + " )\n", + "\n", + "interesting = (\n", + " 'oracle:', 'model_params', 'step:', 'warmup_step:', 'ema:', 'Serialized',\n", + " 'ttt_score_first:', 'ttt:done', 'final_int6', 'peak memory', 'WARN',\n", + " 'world_size', 'attention_mode', 'WARNING', 'Traceback', 'Error',\n", + " \"can't open\", 'No such file', 'NCCL', 'CUDA',\n", + ")\n", + "seen_chunks = 0\n", + "\n", + "with open(LOG_FILE) as f_in:\n", + " while proc.poll() is None:\n", + " time.sleep(5)\n", + " for line in f_in.readlines():\n", + " line = line.rstrip()\n", + " if 'ttt_chunk:' in line:\n", + " seen_chunks += 1\n", + " if seen_chunks % 50 == 0:\n", + " print(line)\n", + " continue\n", + " if any(k in line for k in interesting):\n", + " print(line)\n", + " elapsed = time.time() - t0\n", + " print(f' [t={elapsed:.0f}s log={os.path.getsize(LOG_FILE)}B chunks={seen_chunks}]', flush=True)\n", + "\n", + " # Drain final lines after process exits\n", + " for line in f_in.readlines():\n", + " line = line.rstrip()\n", + " if any(k in line for k in interesting) and 'ttt_chunk:' not in line:\n", + " print(line)\n", + "\n", + "print(f'\\n=== Exit: {proc.returncode}, wall: {time.time()-t0:.1f}s ===')\n", + "print(f'Full log: {LOG_FILE} ({os.path.getsize(LOG_FILE):,} bytes)')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 8. Validate artifact: magic prefix, version, blob lengths, total size\n", + "import struct, os\n", + "artifact = '/kaggle/working/pgolf/final_model.int6.ptz'\n", + "if not os.path.exists(artifact):\n", + " print('ERROR: artifact not produced — see cell 7 log')\n", + "else:\n", + " size = os.path.getsize(artifact)\n", + " print(f'Artifact: {size:,} bytes ({size/1024/1024:.2f} MB)')\n", + " print(f'Budget remaining: {(16*1024*1024 - size)/1024/1024:.2f} MB')\n", + " with open(artifact, 'rb') as f:\n", + " head = f.read(16)\n", + " magic, version, neural_len, oracle_len = struct.unpack('=2.4.0 +numpy>=1.26.0 +sentencepiece>=0.2.0 +zstandard>=0.22.0 diff --git a/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/run_h100.sh b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/run_h100.sh new file mode 100644 index 0000000000..fb9bac6671 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/run_h100.sh @@ -0,0 +1,91 @@ +#!/bin/bash +# Parameter Golf — H100 Run Script (v4: Oracle + SGD TTT + LeakyReLU 0.75) +# Usage: bash run_h100.sh [seed] +# Requires: 8xH100 SXM, flash_attn_3, zstandard + +set -e + +SEED=${1:-1337} +echo "=== Parameter Golf: VR+GA+Oracle+SGD-TTT on PR #462 base ===" +echo "Seed: $SEED" + +# Install deps +pip install -q zstandard + +# Clone and setup +cd /workspace +git clone https://github.com/openai/parameter-golf.git 2>/dev/null || true +cd parameter-golf + +# Download data (full 80 shards for competition run) +python3 data/cached_challenge_fineweb.py --variant sp1024 + +# Copy our modified files +cp /workspace/train_gpt.py ./train_gpt.py +cp /workspace/build_ngram_oracle.py ./build_ngram_oracle.py + +# === BUILD N-GRAM ORACLE (runs BEFORE the 10-min clock) === +if [ ! -f /workspace/ngram_oracle.bin ]; then + echo "Building n-gram oracle from training data..." + python3 build_ngram_oracle.py \ + "./data/datasets/fineweb10B_sp1024/fineweb_train_*.bin" \ + /workspace/ngram_oracle.bin + echo "Oracle built." +else + echo "Using cached oracle at /workspace/ngram_oracle.bin" +fi + +# Run training (600s) + TTT+eval (600s) +SEED=$SEED \ +NUM_LAYERS=11 \ +MODEL_DIM=512 \ +NUM_HEADS=8 \ +NUM_KV_HEADS=8 \ +MLP_HIDDEN=1792 \ +TRAIN_SEQ_LEN=1024 \ +TRAIN_BATCH_TOKENS=524288 \ +VAL_BATCH_SIZE=524288 \ +VOCAB_SIZE=1024 \ +ITERATIONS=20000 \ +WARMDOWN_ITERS=3500 \ +WARMUP_STEPS=20 \ +MAX_WALLCLOCK_SECONDS=600 \ +VAL_LOSS_EVERY=1000 \ +TRAIN_LOG_EVERY=200 \ +VALUE_RESIDUAL=1 \ +GATED_ATTENTION=1 \ +EMA_ENABLED=1 \ +EMA_DECAY=0.9985 \ +LATE_QAT=1 \ +XSA_LAYERS=4 \ +ROPE_DIMS=16 \ +LN_SCALE=1 \ +LEAKY_RELU_SLOPE=0.75 \ +MUON_WD=0.04 \ +ADAM_WD=0.04 \ +MATRIX_LR=0.025 \ +SCALAR_LR=0.025 \ +TIED_EMBED_LR=0.035 \ +MUON_MOMENTUM=0.99 \ +MUON_MOMENTUM_WARMUP_START=0.92 \ +MUON_MOMENTUM_WARMUP_STEPS=1500 \ +TTT_ENABLED=1 \ +TTT_OPTIMIZER=sgd \ +TTT_LR=0.002 \ +TTT_EPOCHS_PER_CHUNK=20 \ +TTT_CHUNK_TOKENS=131072 \ +TTT_FREEZE_BLOCKS=9 \ +TTT_TIME_BUDGET=450 \ +EVAL_STRIDE=64 \ +BIGRAM_BUCKETS=8192 \ +BIGRAM_EMBED_DIM=128 \ +CROWN_LAMBDA=0.0001 \ +HEDGE_ENABLED=1 \ +HEDGE_ETA=0.01 \ +HEDGE_TRIGRAM_BUCKETS=8192 \ +HEDGE_CACHE_GAMMA=0.999 \ +NGRAM_ORACLE_PATH=/workspace/ngram_oracle.bin \ +RUN_ID="oracle_sgdttt_hedge_seed${SEED}" \ +torchrun --standalone --nproc_per_node=8 train_gpt.py + +echo "=== Run complete for seed $SEED ===" diff --git a/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/submission.json b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/submission.json new file mode 100644 index 0000000000..b03793c5fc --- /dev/null +++ b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/submission.json @@ -0,0 +1,15 @@ +{ + "track": "non_record_16mb", + "date": "2026-04-30", + "name": "Frozen N-gram Oracle + Hedge Mixer + SGD TTT (Research, Not Validated at Scale)", + "author": "Dhruv Puri", + "github_id": "dhruvpuri", + "blurb": "Hybrid system: a frozen multi-order n-gram oracle (built offline from FineWeb training tokens, ~5MB compressed) is bundled into a single artifact alongside the neural model and used as additional experts in the existing Hedge Mixer at TTT/eval time. Adds SGD TTT (PR #967) and LeakyReLU(0.75)^2 (PR #977). End-to-end pipeline validated locally on RTX 4060 (4L/256d toy, 50 steps, 7.6MB artifact). NOT validated at 8xH100 scale — submitted as research/methodology contribution.", + "val_bpb": null, + "val_loss": null, + "bytes_total": null, + "hardware_validated": "RTX 4060 (toy run only)", + "hardware_intended": "8xH100 SXM (untested)", + "base_pr": "#462", + "references": ["#803", "#834", "#924", "#967", "#977", "#1019"] +} diff --git a/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/train_gpt.py b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/train_gpt.py new file mode 100644 index 0000000000..909c402760 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-30_FrozenOracle_ComplementaryTraining_HedgeMixer/train_gpt.py @@ -0,0 +1,2015 @@ +"""train_gpt.py — SwiGLU + U-Net + BigramHash + EMA + TTT + XSA4 + GPTQ-lite. Max 1500 lines.""" + +from __future__ import annotations + +import copy +import glob +import io +import math +import os +import random +import struct +import subprocess +import sys +import time +import uuid +from pathlib import Path + +import numpy as np +import sentencepiece as spm +import torch +import torch.distributed as dist +import torch.nn.functional as F +from torch import Tensor, nn +from torch.nn.parallel import DistributedDataParallel as DDP + +# zstd-22 compression with zlib fallback. zlib is always imported (stdlib) so the +# fallback path is available without inline imports. +import zlib +try: + import zstandard as zstd + USE_ZSTD = True +except ImportError: + USE_ZSTD = False + +# ----------------------------- +# HYPERPARAMETERS +# ----------------------------- + +class Hyperparameters: + data_path = os.environ.get("DATA_PATH", "./data/datasets/fineweb10B_sp1024") + train_files = os.path.join(data_path, "fineweb_train_*.bin") + val_files = os.path.join(data_path, "fineweb_val_*.bin") + tokenizer_path = os.environ.get("TOKENIZER_PATH", "./data/tokenizers/fineweb_1024_bpe.model") + run_id = os.environ.get("RUN_ID", str(uuid.uuid4())) + seed = int(os.environ.get("SEED", 42)) + + val_batch_size = int(os.environ.get("VAL_BATCH_SIZE", 524_288)) + val_loss_every = int(os.environ.get("VAL_LOSS_EVERY", 1000)) + train_log_every = int(os.environ.get("TRAIN_LOG_EVERY", 200)) + + iterations = int(os.environ.get("ITERATIONS", 20000)) + warmdown_iters = int(os.environ.get("WARMDOWN_ITERS", "6000")) + warmup_steps = int(os.environ.get("WARMUP_STEPS", 20)) + train_batch_tokens = int(os.environ.get("TRAIN_BATCH_TOKENS", 524_288)) + train_seq_len = int(os.environ.get("TRAIN_SEQ_LEN", 1024)) + max_wallclock_seconds = float(os.environ.get("MAX_WALLCLOCK_SECONDS", 600.0)) + qk_gain_init = float(os.environ.get("QK_GAIN_INIT", 1.5)) + + vocab_size = int(os.environ.get("VOCAB_SIZE", 1024)) + num_layers = int(os.environ.get("NUM_LAYERS", "11")) # Up from 9 + num_kv_heads = int(os.environ.get("NUM_KV_HEADS", "8")) + model_dim = int(os.environ.get("MODEL_DIM", 512)) + num_heads = int(os.environ.get("NUM_HEADS", 8)) + mlp_mult = int(os.environ.get("MLP_MULT", 3)) # Unused by Star-ReLU + mlp_hidden = int(os.environ.get("MLP_HIDDEN", "1792")) + tie_embeddings = bool(int(os.environ.get("TIE_EMBEDDINGS", "1"))) + eval_stride = int(os.environ.get("EVAL_STRIDE", 64)) + eval_batch_seqs = int(os.environ.get("EVAL_BATCH_SEQS", 32)) + rope_base = float(os.environ.get("ROPE_BASE", 10000.0)) + logit_softcap = float(os.environ.get("LOGIT_SOFTCAP", 30.0)) + + # BigramHash config + bigram_buckets = int(os.environ.get("BIGRAM_BUCKETS", "8192")) + bigram_embed_dim = int(os.environ.get("BIGRAM_EMBED_DIM", 128)) + + # Partial RoPE: apply rotary to only first ROPE_DIMS of head_dim (0 = full) + rope_dims = int(os.environ.get("ROPE_DIMS", 16)) + + # LN Scale: scale norm input by 1/sqrt(layer_idx+1) per block + ln_scale = bool(int(os.environ.get("LN_SCALE", "1"))) + + # Optimizer hyperparameters (updated to match #1 team) + embed_lr = float(os.environ.get("EMBED_LR", 0.6)) + head_lr = float(os.environ.get("HEAD_LR", 0.008)) + tied_embed_lr = float(os.environ.get("TIED_EMBED_LR", 0.035)) + tied_embed_init_std = float(os.environ.get("TIED_EMBED_INIT_STD", 0.005)) + matrix_lr = float(os.environ.get("MATRIX_LR", 0.025)) + scalar_lr = float(os.environ.get("SCALAR_LR", 0.025)) + decoder_lr_mult = float(os.environ.get("DECODER_LR_MULT", 2.0)) + muon_momentum = float(os.environ.get("MUON_MOMENTUM", 0.99)) + muon_backend_steps = int(os.environ.get("MUON_BACKEND_STEPS", 5)) + muon_momentum_warmup_start = float(os.environ.get("MUON_MOMENTUM_WARMUP_START", 0.92)) + muon_momentum_warmup_steps = int(os.environ.get("MUON_MOMENTUM_WARMUP_STEPS", 1500)) + muon_wd = float(os.environ.get("MUON_WD", 0.04)) + adam_wd = float(os.environ.get("ADAM_WD", 0.04)) + beta1 = float(os.environ.get("BETA1", 0.9)) + beta2 = float(os.environ.get("BETA2", 0.95)) + adam_eps = float(os.environ.get("ADAM_EPS", 1e-8)) + grad_clip_norm = float(os.environ.get("GRAD_CLIP_NORM", 0.0)) + + # EMA: exponential moving average, updates every step (priority over SWA) + ema_enabled = bool(int(os.environ.get("EMA_ENABLED", "1"))) + ema_decay = float(os.environ.get("EMA_DECAY", "0.9985")) + + # SWA config (fallback when EMA disabled) + swa_start_frac = float(os.environ.get("SWA_START_FRAC", 0.5)) + swa_every = int(os.environ.get("SWA_EVERY", 50)) + + # Late QAT: enable fake int6 quantization when LR scale < qat_threshold + late_qat = bool(int(os.environ.get("LATE_QAT", "1"))) + qat_threshold = float(os.environ.get("QAT_THRESHOLD", "0.15")) + crown_lambda = float(os.environ.get("CROWN_LAMBDA", "0.0")) + + # TTT: Legal score-first test-time training + ttt_enabled = bool(int(os.environ.get("TTT_ENABLED", "1"))) + ttt_lr = float(os.environ.get("TTT_LR", "0.0001")) + ttt_batch_seqs = int(os.environ.get("TTT_BATCH_SEQS", 32)) + ttt_freeze_blocks = int(os.environ.get("TTT_FREEZE_BLOCKS", 9)) + ttt_time_budget = float(os.environ.get("TTT_TIME_BUDGET", 375.0)) + ttt_chunk_tokens = int(os.environ.get("TTT_CHUNK_TOKENS", 65536)) + ttt_epochs_per_chunk = int(os.environ.get("TTT_EPOCHS_PER_CHUNK", 8)) + xsa_layers = int(os.environ.get("XSA_LAYERS", "4")) + value_residual = bool(int(os.environ.get("VALUE_RESIDUAL", "1"))) + gated_attention = bool(int(os.environ.get("GATED_ATTENTION", "1"))) + + # TTT optimizer: "adamw" (default) or "sgd" (PR #967: -0.041 BPB) + ttt_optimizer = os.environ.get("TTT_OPTIMIZER", "adamw").lower() + + # MLP activation negative slope (PR #977: 0.75 beats 0.5 by -0.008 BPB) + leaky_relu_slope = float(os.environ.get("LEAKY_RELU_SLOPE", "0.5")) + + # Hedge Mixer: online expert mixing during TTT scoring + hedge_enabled = bool(int(os.environ.get("HEDGE_ENABLED", "0"))) + hedge_eta = float(os.environ.get("HEDGE_ETA", "0.01")) + hedge_trigram_buckets = int(os.environ.get("HEDGE_TRIGRAM_BUCKETS", "8192")) + hedge_cache_gamma = float(os.environ.get("HEDGE_CACHE_GAMMA", "0.999")) + + # Frozen N-gram Oracle: precomputed tables from training data + ngram_oracle_path = os.environ.get("NGRAM_ORACLE_PATH", "") + complementary_alpha = float(os.environ.get("COMPLEMENTARY_ALPHA", "0.0")) + complementary_max_order = int(os.environ.get("COMPLEMENTARY_MAX_ORDER", "6")) + + +# ----------------------------- +# MUON OPTIMIZER WITH WEIGHT DECAY +# ----------------------------- + +def zeropower_via_newtonschulz5(G: Tensor, steps: int = 10, eps: float = 1e-7) -> Tensor: + a, b, c = (3.4445, -4.7750, 2.0315) + X = G.bfloat16() + X /= X.norm() + eps + transposed = G.size(0) > G.size(1) + if transposed: + X = X.T + for _ in range(steps): + A = X @ X.T + B = b * A + c * A @ A + X = a * X + B @ X + return X.T if transposed else X + + +class Muon(torch.optim.Optimizer): + def __init__(self, params, lr: float, momentum: float, backend_steps: int, nesterov: bool = True, weight_decay: float = 0.02): + super().__init__( + params, + dict(lr=lr, momentum=momentum, backend_steps=backend_steps, nesterov=nesterov, weight_decay=weight_decay), + ) + + @torch.no_grad() + def step(self, closure=None): + loss = None + if closure is not None: + with torch.enable_grad(): + loss = closure() + + distributed = dist.is_available() and dist.is_initialized() + world_size = dist.get_world_size() if distributed else 1 + rank = dist.get_rank() if distributed else 0 + + for group in self.param_groups: + params = group["params"] + if not params: + continue + lr = group["lr"] + momentum = group["momentum"] + backend_steps = group["backend_steps"] + nesterov = group["nesterov"] + wd = group["weight_decay"] + + total_params = sum(int(p.numel()) for p in params) + updates_flat = torch.zeros(total_params, device=params[0].device, dtype=torch.bfloat16) + + curr = 0 + for i, p in enumerate(params): + if i % world_size == rank and p.grad is not None: + g = p.grad + state = self.state[p] + if "momentum_buffer" not in state: + state["momentum_buffer"] = torch.zeros_like(g) + buf = state["momentum_buffer"] + buf.mul_(momentum).add_(g) + if nesterov: + g = g.add(buf, alpha=momentum) + g = zeropower_via_newtonschulz5(g, steps=backend_steps) + g *= max(1, g.size(0) / g.size(1)) ** 0.5 + updates_flat[curr : curr + p.numel()] = g.reshape(-1) + curr += p.numel() + + if distributed: + dist.all_reduce(updates_flat, op=dist.ReduceOp.SUM) + + curr = 0 + for p in params: + g = updates_flat[curr : curr + p.numel()].view_as(p).to(dtype=p.dtype) + p.add_(g, alpha=-lr) + # Apply weight decay after update + if wd > 0: + p.mul_(1 - wd * lr) + curr += p.numel() + + return loss + + +# ----------------------------- +# TOKENIZER-AGNOSTIC EVALUATION SETUP +# ----------------------------- + +def build_sentencepiece_luts( + sp: spm.SentencePieceProcessor, vocab_size: int, device: torch.device +) -> tuple[Tensor, Tensor, Tensor]: + sp_vocab_size = int(sp.vocab_size()) + table_size = max(sp_vocab_size, vocab_size) + base_bytes_np = np.zeros((table_size,), dtype=np.int16) + has_leading_space_np = np.zeros((table_size,), dtype=np.bool_) + is_boundary_token_np = np.ones((table_size,), dtype=np.bool_) + for token_id in range(sp_vocab_size): + if sp.is_control(token_id) or sp.is_unknown(token_id) or sp.is_unused(token_id): + continue + is_boundary_token_np[token_id] = False + if sp.is_byte(token_id): + base_bytes_np[token_id] = 1 + continue + piece = sp.id_to_piece(token_id) + if piece.startswith("\u2581"): + has_leading_space_np[token_id] = True + piece = piece[1:] + base_bytes_np[token_id] = len(piece.encode("utf-8")) + return ( + torch.tensor(base_bytes_np, dtype=torch.int16, device=device), + torch.tensor(has_leading_space_np, dtype=torch.bool, device=device), + torch.tensor(is_boundary_token_np, dtype=torch.bool, device=device), + ) + + +def load_validation_tokens(pattern: str, seq_len: int) -> Tensor: + files = [Path(p) for p in sorted(glob.glob(pattern))] + if not files: + raise FileNotFoundError(f"No files found for pattern: {pattern}") + tokens = torch.cat([load_data_shard(file) for file in files]).contiguous() + usable = ((tokens.numel() - 1) // seq_len) * seq_len + if usable <= 0: + raise ValueError(f"Validation split is too short for TRAIN_SEQ_LEN={seq_len}") + return tokens[: usable + 1] + + +def eval_val( + args: Hyperparameters, + model: nn.Module, + rank: int, + world_size: int, + device: torch.device, + grad_accum_steps: int, + val_tokens: Tensor, + base_bytes_lut: Tensor, + has_leading_space_lut: Tensor, + is_boundary_token_lut: Tensor, +) -> tuple[float, float]: + local_batch_tokens = args.val_batch_size // (world_size * grad_accum_steps) + if local_batch_tokens < args.train_seq_len: + raise ValueError( + "VAL_BATCH_SIZE must provide at least one sequence per rank; " + f"got VAL_BATCH_SIZE={args.val_batch_size}, WORLD_SIZE={world_size}, " + f"GRAD_ACCUM_STEPS={grad_accum_steps}, TRAIN_SEQ_LEN={args.train_seq_len}" + ) + local_batch_seqs = local_batch_tokens // args.train_seq_len + total_seqs = (val_tokens.numel() - 1) // args.train_seq_len + seq_start = (total_seqs * rank) // world_size + seq_end = (total_seqs * (rank + 1)) // world_size + val_loss_sum = torch.zeros((), device=device, dtype=torch.float64) + val_token_count = torch.zeros((), device=device, dtype=torch.float64) + val_byte_count = torch.zeros((), device=device, dtype=torch.float64) + + model.eval() + with torch.inference_mode(): + for batch_seq_start in range(seq_start, seq_end, local_batch_seqs): + batch_seq_end = min(batch_seq_start + local_batch_seqs, seq_end) + raw_start = batch_seq_start * args.train_seq_len + raw_end = batch_seq_end * args.train_seq_len + 1 + local = val_tokens[raw_start:raw_end].to(device=device, dtype=torch.int64, non_blocking=True) + x = local[:-1].reshape(-1, args.train_seq_len) + y = local[1:].reshape(-1, args.train_seq_len) + with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True): + batch_loss = model(x, y).detach() + batch_token_count = float(y.numel()) + val_loss_sum += batch_loss.to(torch.float64) * batch_token_count + val_token_count += batch_token_count + prev_ids = x.reshape(-1) + tgt_ids = y.reshape(-1) + token_bytes = base_bytes_lut[tgt_ids].to(dtype=torch.int16) + token_bytes += (has_leading_space_lut[tgt_ids] & ~is_boundary_token_lut[prev_ids]).to(dtype=torch.int16) + val_byte_count += token_bytes.to(torch.float64).sum() + + if dist.is_available() and dist.is_initialized(): + dist.all_reduce(val_loss_sum, op=dist.ReduceOp.SUM) + dist.all_reduce(val_token_count, op=dist.ReduceOp.SUM) + dist.all_reduce(val_byte_count, op=dist.ReduceOp.SUM) + + val_loss = val_loss_sum / val_token_count + bits_per_token = val_loss.item() / math.log(2.0) + tokens_per_byte = val_token_count.item() / val_byte_count.item() + model.train() + return float(val_loss.item()), float(bits_per_token * tokens_per_byte) + + +def eval_val_sliding( + args: Hyperparameters, + base_model: nn.Module, + rank: int, + world_size: int, + device: torch.device, + val_tokens: Tensor, + base_bytes_lut: Tensor, + has_leading_space_lut: Tensor, + is_boundary_token_lut: Tensor, + stride: int, + batch_seqs: int = 32, +) -> tuple[float, float]: + """Sliding window evaluation: each token scored with maximum context.""" + seq_len = args.train_seq_len + total_tokens = val_tokens.numel() - 1 + + window_starts = [ws for ws in range(0, total_tokens, stride) + if min(ws + seq_len, total_tokens) - ws >= stride] + total_windows = len(window_starts) + + my_s = (total_windows * rank) // world_size + my_e = (total_windows * (rank + 1)) // world_size + my_windows = window_starts[my_s:my_e] + + loss_sum = torch.zeros((), device=device, dtype=torch.float64) + token_count = torch.zeros((), device=device, dtype=torch.float64) + byte_count = torch.zeros((), device=device, dtype=torch.float64) + + base_model.eval() + with torch.inference_mode(): + for bi in range(0, len(my_windows), batch_seqs): + batch_ws = my_windows[bi:bi + batch_seqs] + bsz = len(batch_ws) + + x_batch = torch.zeros(bsz, seq_len, dtype=torch.int64, device=device) + y_batch = torch.zeros(bsz, seq_len, dtype=torch.int64, device=device) + wlens: list[int] = [] + + for i, ws in enumerate(batch_ws): + end = min(ws + seq_len, total_tokens) + wlen = end - ws + wlens.append(wlen) + chunk = val_tokens[ws:end + 1].to(dtype=torch.int64, device=device) + x_batch[i, :wlen] = chunk[:-1] + y_batch[i, :wlen] = chunk[1:] + + with torch.autocast(device_type="cuda", dtype=torch.bfloat16): + logits = base_model.forward_logits(x_batch) + + nll = F.cross_entropy( + logits.reshape(-1, logits.size(-1)).float(), + y_batch.reshape(-1), + reduction="none", + ).reshape(bsz, seq_len) + + for i, ws in enumerate(batch_ws): + wlen = wlens[i] + s = 0 if ws == 0 else wlen - stride + scored_nll = nll[i, s:wlen].to(torch.float64) + loss_sum += scored_nll.sum() + + scored_prev = x_batch[i, s:wlen] + scored_tgt = y_batch[i, s:wlen] + tb = base_bytes_lut[scored_tgt].to(torch.int16) + tb += (has_leading_space_lut[scored_tgt] & ~is_boundary_token_lut[scored_prev]).to(torch.int16) + byte_count += tb.to(torch.float64).sum() + token_count += float(wlen - s) + + if dist.is_available() and dist.is_initialized(): + dist.all_reduce(loss_sum, op=dist.ReduceOp.SUM) + dist.all_reduce(token_count, op=dist.ReduceOp.SUM) + dist.all_reduce(byte_count, op=dist.ReduceOp.SUM) + + val_loss = loss_sum / token_count + bits_per_token = val_loss.item() / math.log(2.0) + tokens_per_byte = token_count.item() / byte_count.item() + base_model.train() + return float(val_loss.item()), float(bits_per_token * tokens_per_byte) + + +# ----------------------------- +# POST-TRAINING INT6 QUANTIZATION +# ----------------------------- + +INT6_MIN = -32 +INT6_MAX = 31 +INT6_CLIP_PERCENTILE = 99.99984 +INT6_CLIP_Q = INT6_CLIP_PERCENTILE / 100.0 + +CONTROL_TENSOR_NAME_PATTERNS = tuple( + pattern + for pattern in os.environ.get( + "CONTROL_TENSOR_NAME_PATTERNS", + "attn_scale,attn_scales,mlp_scale,mlp_scales,resid_mix,resid_mixes,q_gain,skip_weight,skip_weights,smear_gate,bigram,skip_gates,vr_lambda,attn_gate", + ).split(",") + if pattern +) +INT6_KEEP_FLOAT_FP32_NAME_PATTERNS = tuple( + pattern + for pattern in os.environ.get( + "INT6_KEEP_FLOAT_FP32_NAME_PATTERNS", + ",".join(CONTROL_TENSOR_NAME_PATTERNS), + ).split(",") + if pattern +) +INT6_KEEP_FLOAT_MAX_NUMEL = 65_536 +INT6_KEEP_FLOAT_STORE_DTYPE = torch.float16 +INT6_PER_ROW_SCALE_DTYPE = torch.float16 + +def tensor_nbytes(t: Tensor) -> int: + return int(t.numel()) * int(t.element_size()) + +def keep_float_tensor(name: str, t: Tensor, passthrough_orig_dtypes: dict[str, str]) -> Tensor: + if any(pattern in name for pattern in INT6_KEEP_FLOAT_FP32_NAME_PATTERNS): + return t.float().contiguous() + if t.dtype in {torch.float32, torch.bfloat16}: + passthrough_orig_dtypes[name] = str(t.dtype).removeprefix("torch.") + return t.to(dtype=INT6_KEEP_FLOAT_STORE_DTYPE).contiguous() + return t + +def quantize_float_tensor_int6(t: Tensor) -> tuple[Tensor, Tensor]: + """Quantize to int6 range [-32, 31], stored as int8.""" + t32 = t.float() + if t32.ndim == 2: + clip_abs = ( + torch.quantile(t32.abs(), INT6_CLIP_Q, dim=1) + if t32.numel() + else torch.empty((t32.shape[0],), dtype=torch.float32) + ) + clipped = torch.maximum(torch.minimum(t32, clip_abs[:, None]), -clip_abs[:, None]) + scale = (clip_abs / float(INT6_MAX)).clamp_min(1.0 / float(INT6_MAX)) + q = torch.clamp(torch.round(clipped / scale[:, None]), INT6_MIN, INT6_MAX).to(torch.int8).contiguous() + return q, scale.to(dtype=INT6_PER_ROW_SCALE_DTYPE).contiguous() + + clip_abs = float(torch.quantile(t32.abs().flatten(), INT6_CLIP_Q).item()) if t32.numel() else 0.0 + scale = torch.tensor(clip_abs / float(INT6_MAX) if clip_abs > 0 else 1.0, dtype=torch.float32) + q = torch.clamp(torch.round(torch.clamp(t32, -clip_abs, clip_abs) / scale), INT6_MIN, INT6_MAX).to(torch.int8).contiguous() + return q, scale + +def quantize_state_dict_int6(state_dict: dict[str, Tensor]): + quantized: dict[str, Tensor] = {} + scales: dict[str, Tensor] = {} + dtypes: dict[str, str] = {} + passthrough: dict[str, Tensor] = {} + passthrough_orig_dtypes: dict[str, str] = {} + qmeta: dict[str, dict[str, object]] = {} + stats = dict.fromkeys( + ("param_count", "num_tensors", "num_float_tensors", "num_nonfloat_tensors", "baseline_tensor_bytes", "int6_payload_bytes"), + 0, + ) + + for name, tensor in state_dict.items(): + t = tensor.detach().to("cpu").contiguous() + stats["param_count"] += int(t.numel()) + stats["num_tensors"] += 1 + stats["baseline_tensor_bytes"] += tensor_nbytes(t) + + if not t.is_floating_point(): + stats["num_nonfloat_tensors"] += 1 + passthrough[name] = t + stats["int6_payload_bytes"] += tensor_nbytes(t) + continue + + # Keep small float tensors and tok_emb.weight in fp16 + if t.numel() <= INT6_KEEP_FLOAT_MAX_NUMEL or name == "tok_emb.weight": + kept = keep_float_tensor(name, t, passthrough_orig_dtypes) + passthrough[name] = kept + stats["int6_payload_bytes"] += tensor_nbytes(kept) + continue + + stats["num_float_tensors"] += 1 + q, s = quantize_float_tensor_int6(t) + if s.ndim > 0: + qmeta[name] = {"scheme": "per_row", "axis": 0} + quantized[name] = q + scales[name] = s + dtypes[name] = str(t.dtype).removeprefix("torch.") + stats["int6_payload_bytes"] += tensor_nbytes(q) + tensor_nbytes(s) + + obj: dict[str, object] = { + "__quant_format__": "int6_clean_per_row_v1", + "quantized": quantized, + "scales": scales, + "dtypes": dtypes, + "passthrough": passthrough, + } + if qmeta: + obj["qmeta"] = qmeta + if passthrough_orig_dtypes: + obj["passthrough_orig_dtypes"] = passthrough_orig_dtypes + return obj, stats + +def dequantize_state_dict_int6(obj: dict[str, object]) -> dict[str, Tensor]: + out: dict[str, Tensor] = {} + qmeta = obj.get("qmeta", {}) + passthrough_orig_dtypes = obj.get("passthrough_orig_dtypes", {}) + for name, q in obj["quantized"].items(): + dtype = getattr(torch, obj["dtypes"][name]) + s = obj["scales"][name] + if qmeta.get(name, {}).get("scheme") == "per_row" or s.ndim > 0: + s = s.to(dtype=torch.float32) + out[name] = (q.float() * s.view(q.shape[0], *([1] * (q.ndim - 1)))).to(dtype=dtype).contiguous() + else: + scale = float(s.item()) + out[name] = (q.float() * scale).to(dtype=dtype).contiguous() + for name, t in obj["passthrough"].items(): + out_t = t.detach().to("cpu").contiguous() + orig_dtype = passthrough_orig_dtypes.get(name) + if isinstance(orig_dtype, str): + out_t = out_t.to(dtype=getattr(torch, orig_dtype)).contiguous() + out[name] = out_t + return out + + +# ----------------------------- +# DATA LOADING +# ----------------------------- + +def load_data_shard(file: Path) -> Tensor: + header_bytes = 256 * np.dtype(" None: + self.file_idx = (self.file_idx + 1) % len(self.files) + self.tokens = load_data_shard(self.files[self.file_idx]) + self.pos = 0 + + def take(self, n: int) -> Tensor: + chunks: list[Tensor] = [] + remaining = n + while remaining > 0: + avail = self.tokens.numel() - self.pos + if avail <= 0: + self._advance_file() + continue + k = min(remaining, avail) + chunks.append(self.tokens[self.pos : self.pos + k]) + self.pos += k + remaining -= k + return chunks[0] if len(chunks) == 1 else torch.cat(chunks) + + +class DistributedTokenLoader: + def __init__(self, pattern: str, rank: int, world_size: int, device: torch.device): + self.rank = rank + self.world_size = world_size + self.device = device + self.stream = TokenStream(pattern) + + def next_batch(self, global_tokens: int, seq_len: int, grad_accum_steps: int) -> tuple[Tensor, Tensor]: + local_tokens = global_tokens // (self.world_size * grad_accum_steps) + per_rank_span = local_tokens + 1 + chunk = self.stream.take(per_rank_span * self.world_size) + start = self.rank * per_rank_span + local = chunk[start : start + per_rank_span].to(dtype=torch.int64) + x = local[:-1].reshape(-1, seq_len) + y = local[1:].reshape(-1, seq_len) + return x.to(self.device, non_blocking=True), y.to(self.device, non_blocking=True) + + +# ----------------------------- +# TRANSFORMER MODULES +# ----------------------------- + +class RMSNorm(nn.Module): + def __init__(self, eps: float | None = None): + super().__init__() + self.eps = eps + + def forward(self, x: Tensor) -> Tensor: + return F.rms_norm(x, (x.size(-1),), eps=self.eps) + + +class CastedLinear(nn.Linear): + # Class-level flag: set True during late-QAT phase to enable fake int6 STE + _qat_enabled: bool = False + + def forward(self, x: Tensor) -> Tensor: + w = self.weight.to(x.dtype) + if CastedLinear._qat_enabled and self.training and w.ndim == 2: + # Fake int6 quantization via straight-through estimator + with torch.no_grad(): + w32 = self.weight.float() + row_max = torch.quantile(w32.abs(), INT6_CLIP_Q, dim=1) + scale = (row_max / 31.0).clamp_min(1.0 / 31.0) + w_q = (torch.clamp(torch.round(w32 / scale[:, None]), -32, 31) * scale[:, None]).to(x.dtype) + w = w + (w_q - w).detach() + bias = self.bias.to(x.dtype) if self.bias is not None else None + return F.linear(x, w, bias) + + +def crown_q_penalty(model: nn.Module) -> Tensor: + """CROWN-Q: penalize weight configurations expensive to INT6 quantize.""" + if not CastedLinear._qat_enabled: + return torch.zeros((), device=next(model.parameters()).device) + total = torch.zeros((), device=next(model.parameters()).device) + for m in model.modules(): + if isinstance(m, CastedLinear) and m.weight.ndim == 2: + w = m.weight.float() + row_clip = torch.quantile(w.abs(), INT6_CLIP_Q, dim=1) + delta = row_clip / 31.0 + total = total + (w.pow(2).sum(dim=1) * delta.pow(2) / 12.0).sum() + return total + + +def restore_low_dim_params_to_fp32(module: nn.Module) -> None: + with torch.no_grad(): + for name, param in module.named_parameters(): + if (param.ndim < 2 or any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS)) and param.dtype != torch.float32: + param.data = param.data.float() + + +class Rotary(nn.Module): + """RoPE with optional partial application (first rope_dims of head_dim).""" + def __init__(self, dim: int, base: float = 10000.0, rope_dims: int = 0): + super().__init__() + # rope_dims=0 means full head_dim; otherwise rotate only first rope_dims dims + rope_d = rope_dims if rope_dims > 0 else dim + self.rope_d = rope_d + inv_freq = 1.0 / (base ** (torch.arange(0, rope_d, 2, dtype=torch.float32) / rope_d)) + self.register_buffer("inv_freq", inv_freq, persistent=False) + self._seq_len_cached = 0 + self._cos_cached: Tensor | None = None + self._sin_cached: Tensor | None = None + + def forward(self, seq_len: int, device: torch.device, dtype: torch.dtype) -> tuple[Tensor, Tensor]: + if ( + self._cos_cached is None + or self._sin_cached is None + or self._seq_len_cached != seq_len + or self._cos_cached.device != device + ): + t = torch.arange(seq_len, device=device, dtype=self.inv_freq.dtype) + freqs = torch.outer(t, self.inv_freq.to(device)) + self._cos_cached = freqs.cos()[None, None, :, :] + self._sin_cached = freqs.sin()[None, None, :, :] + self._seq_len_cached = seq_len + return self._cos_cached.to(dtype=dtype), self._sin_cached.to(dtype=dtype) + + +def apply_rotary_emb(x: Tensor, cos: Tensor, sin: Tensor) -> Tensor: + """Apply RoPE; if cos covers fewer dims than x, rotate only those dims.""" + rd = cos.size(-1) * 2 + if rd < x.size(-1): + x_rope = x[..., :rd] + x_pass = x[..., rd:] + half = rd // 2 + x1 = x_rope[..., :half] + x2 = x_rope[..., half:] + x_rot = torch.cat((x1 * cos + x2 * sin, x1 * (-sin) + x2 * cos), dim=-1) + return torch.cat((x_rot, x_pass), dim=-1) + half = x.size(-1) // 2 + x1, x2 = x[..., :half], x[..., half:] + return torch.cat((x1 * cos + x2 * sin, x1 * (-sin) + x2 * cos), dim=-1) + + +class CausalSelfAttention(nn.Module): + def __init__(self, dim: int, num_heads: int, num_kv_heads: int, rope_base: float, qk_gain_init: float, + rope_dims: int = 0, value_residual: bool = False, gated_attention: bool = False): + super().__init__() + if dim % num_heads != 0: + raise ValueError("model_dim must be divisible by num_heads") + if num_heads % num_kv_heads != 0: + raise ValueError("num_heads must be divisible by num_kv_heads") + self.num_heads = num_heads + self.num_kv_heads = num_kv_heads + self.head_dim = dim // num_heads + if self.head_dim % 2 != 0: + raise ValueError("head_dim must be even for RoPE") + kv_dim = self.num_kv_heads * self.head_dim + self.c_q = CastedLinear(dim, dim, bias=False) + self.c_k = CastedLinear(dim, kv_dim, bias=False) + self.c_v = CastedLinear(dim, kv_dim, bias=False) + self.proj = CastedLinear(dim, dim, bias=False) + self.proj._zero_init = True + self.q_gain = nn.Parameter(torch.full((num_heads,), qk_gain_init, dtype=torch.float32)) + self.rotary = Rotary(self.head_dim, base=rope_base, rope_dims=rope_dims) + self.use_xsa = False + self.value_residual = value_residual + if value_residual: + self.vr_lambda = nn.Parameter(torch.tensor([0.5, 0.5], dtype=torch.float32)) + self.gated_attention = gated_attention + if gated_attention: + self.attn_gate = CastedLinear(dim, num_heads, bias=True) + self.attn_gate._zero_init = True + nn.init.constant_(self.attn_gate.bias, 4.0) + + def _xsa_efficient(self, y: Tensor, v: Tensor) -> Tensor: + """Subtract self-value projection via GQA-aware reshape (no repeat_interleave).""" + B, T, H, D = y.shape + Hkv = v.size(-2) + group = H // Hkv + y_g = y.reshape(B, T, Hkv, group, D) + vn = F.normalize(v, dim=-1).unsqueeze(-2) + proj = (y_g * vn).sum(dim=-1, keepdim=True) * vn + return (y_g - proj).reshape(B, T, H, D) + + def forward(self, x: Tensor, v0: Tensor | None = None) -> tuple[Tensor, Tensor | None]: + bsz, seqlen, dim = x.shape + q = self.c_q(x).reshape(bsz, seqlen, self.num_heads, self.head_dim).transpose(1, 2) # (B, H, T, D) + k = self.c_k(x).reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2) # (B, Hkv, T, D) + v = self.c_v(x).reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2) # (B, Hkv, T, D) + raw_v = v if self.value_residual else None + if self.value_residual: + lam = self.vr_lambda.to(dtype=v.dtype) + v = lam[0] * v0 + lam[1] * v # v0 is (B, Hkv, T, D), same layout + q = F.rms_norm(q, (q.size(-1),)) + k = F.rms_norm(k, (k.size(-1),)) + cos, sin = self.rotary(seqlen, x.device, q.dtype) + q = apply_rotary_emb(q, cos, sin) + k = apply_rotary_emb(k, cos, sin) + q = q * self.q_gain.to(dtype=q.dtype)[None, :, None, None] + if self.num_kv_heads != self.num_heads: + k = k.repeat_interleave(self.num_heads // self.num_kv_heads, dim=1) + v_sdpa = v.repeat_interleave(self.num_heads // self.num_kv_heads, dim=1) + else: + v_sdpa = v + y = F.scaled_dot_product_attention(q, k, v_sdpa, attn_mask=None, is_causal=True) + # y is (B, H, T, D) + if self.use_xsa: + y_xsa = y.transpose(1, 2) # (B, T, H, D) + v_xsa = v.transpose(1, 2) # (B, T, Hkv, D) + y_xsa = self._xsa_efficient(y_xsa, v_xsa) + y = y_xsa.transpose(1, 2) # back to (B, H, T, D) + if self.gated_attention: + gate = torch.sigmoid(self.attn_gate(x)).transpose(1, 2).unsqueeze(-1) # (B,T,H) → (B,H,T,1) + y = y * gate + y = y.transpose(1, 2).contiguous().reshape(bsz, seqlen, dim) + return self.proj(y), raw_v + + +class MLP(nn.Module): + def __init__(self, dim: int, mlp_mult: int, mlp_hidden: int = 0, + leaky_relu_slope: float = 0.5): + super().__init__() + hidden = mlp_hidden if mlp_hidden > 0 else int(dim * 3) + self.leaky_relu_slope = leaky_relu_slope + self.up_proj = CastedLinear(dim, hidden, bias=False) + self.down_proj = CastedLinear(hidden, dim, bias=False) + self.down_proj._zero_init = True + self.scale = nn.Parameter(torch.ones(hidden, dtype=torch.float32)) + self.bias = nn.Parameter(torch.zeros(hidden, dtype=torch.float32)) + + def forward(self, x: Tensor) -> Tensor: + x_up = self.up_proj(x) + activated = F.leaky_relu(x_up, self.leaky_relu_slope).pow(2) + activated = activated * self.scale.to(dtype=activated.dtype) + self.bias.to(dtype=activated.dtype) + return self.down_proj(activated) + + +class Block(nn.Module): + def __init__( + self, + dim: int, + num_heads: int, + num_kv_heads: int, + mlp_mult: int, + rope_base: float, + qk_gain_init: float, + mlp_hidden: int = 0, + rope_dims: int = 0, + layer_idx: int = 0, + ln_scale: bool = False, + value_residual: bool = False, + gated_attention: bool = False, + leaky_relu_slope: float = 0.5, + ): + super().__init__() + self.attn_norm = RMSNorm() + self.mlp_norm = RMSNorm() + self.attn = CausalSelfAttention(dim, num_heads, num_kv_heads, rope_base, qk_gain_init, + rope_dims=rope_dims, value_residual=value_residual, + gated_attention=gated_attention) + self.mlp = MLP(dim, mlp_mult, mlp_hidden=mlp_hidden, leaky_relu_slope=leaky_relu_slope) + self.attn_scale = nn.Parameter(torch.ones(dim, dtype=torch.float32)) + self.mlp_scale = nn.Parameter(torch.ones(dim, dtype=torch.float32)) + self.resid_mix = nn.Parameter(torch.stack((torch.ones(dim), torch.zeros(dim))).float()) + self.ln_scale_factor = 1.0 / math.sqrt(layer_idx + 1) if ln_scale else 1.0 + + def forward(self, x: Tensor, x0: Tensor, v0: Tensor | None = None) -> tuple[Tensor, Tensor | None]: + mix = self.resid_mix.to(dtype=x.dtype) + x = mix[0][None, None, :] * x + mix[1][None, None, :] * x0 + s = self.ln_scale_factor + attn_out, raw_v = self.attn(self.attn_norm(x) * s, v0=v0) + x = x + self.attn_scale.to(dtype=x.dtype)[None, None, :] * attn_out + x = x + self.mlp_scale.to(dtype=x.dtype)[None, None, :] * self.mlp(self.mlp_norm(x) * s) + return x, raw_v + + +# ----------------------------- +# BIGRAM HASH EMBEDDING +# ----------------------------- + +class BigramHashEmbedding(nn.Module): + """Hash-based bigram embedding that adds context from previous token.""" + def __init__(self, num_buckets: int, embed_dim: int, model_dim: int): + super().__init__() + self.num_buckets = num_buckets + self.embed = nn.Embedding(num_buckets, embed_dim) + self.proj = CastedLinear(embed_dim, model_dim, bias=False) + nn.init.normal_(self.embed.weight, std=0.01) + nn.init.zeros_(self.proj.weight) + + def forward(self, input_ids: Tensor) -> Tensor: + # input_ids: (bsz, seq_len) + bsz, seq_len = input_ids.shape + # Shift input_ids to get prev_ids, pad with 0 + prev_ids = F.pad(input_ids[:, :-1], (1, 0), value=0) + # Hash: (prev_id * 1009 + curr_id) % buckets + bigram_hash = (prev_ids * 1009 + input_ids) % self.num_buckets + bigram_emb = self.embed(bigram_hash) + return self.proj(bigram_emb) + + +# ----------------------------- +# SMEAR GATE +# ----------------------------- + +class SmearGate(nn.Module): + """Learned blending of current position with previous position.""" + def __init__(self, dim: int): + super().__init__() + self.gate = nn.Parameter(torch.zeros(dim, dtype=torch.float32)) + + def forward(self, x: Tensor) -> Tensor: + # x: (bsz, seq_len, dim) + gate = torch.sigmoid(self.gate.to(dtype=x.dtype)) + # Shift x to get previous position, pad with zeros + x_prev = F.pad(x[:, :-1], (0, 0, 1, 0)) + return (1 - gate) * x + gate * x_prev + + +class GPT(nn.Module): + def __init__( + self, + vocab_size: int, + num_layers: int, + model_dim: int, + num_heads: int, + num_kv_heads: int, + mlp_mult: int, + tie_embeddings: bool, + tied_embed_init_std: float, + logit_softcap: float, + rope_base: float, + qk_gain_init: float, + mlp_hidden: int = 0, + bigram_buckets: int = 4096, + bigram_embed_dim: int = 128, + rope_dims: int = 0, + ln_scale: bool = False, + xsa_last_n: int = 0, + value_residual: bool = False, + gated_attention: bool = False, + leaky_relu_slope: float = 0.5, + ): + super().__init__() + if logit_softcap <= 0.0: + raise ValueError(f"logit_softcap must be positive, got {logit_softcap}") + self.tie_embeddings = tie_embeddings + self.tied_embed_init_std = tied_embed_init_std + self.logit_softcap = logit_softcap + self.value_residual = value_residual + self.tok_emb = nn.Embedding(vocab_size, model_dim) + self.bigram_emb = BigramHashEmbedding(bigram_buckets, bigram_embed_dim, model_dim) + self.smear_gate = SmearGate(model_dim) + self.num_encoder_layers = num_layers // 2 + self.num_decoder_layers = num_layers - self.num_encoder_layers + self.num_skip_weights = min(self.num_encoder_layers, self.num_decoder_layers) + self.skip_weights = nn.Parameter(torch.ones(self.num_skip_weights, model_dim, dtype=torch.float32)) + self.skip_gates = nn.Parameter(torch.zeros(self.num_skip_weights, model_dim, dtype=torch.float32)) + self.blocks = nn.ModuleList([ + Block( + model_dim, num_heads, num_kv_heads, mlp_mult, rope_base, qk_gain_init, + mlp_hidden=mlp_hidden, rope_dims=rope_dims, layer_idx=i, ln_scale=ln_scale, + value_residual=value_residual, gated_attention=gated_attention, + leaky_relu_slope=leaky_relu_slope, + ) + for i in range(num_layers) + ]) + if xsa_last_n > 0: + for i in range(max(0, num_layers - xsa_last_n), num_layers): + self.blocks[i].attn.use_xsa = True + self.final_norm = RMSNorm() + self.lm_head = None if tie_embeddings else CastedLinear(model_dim, vocab_size, bias=False) + if self.lm_head is not None: + self.lm_head._zero_init = True + self._init_weights() + + def _init_weights(self) -> None: + if self.tie_embeddings: + nn.init.normal_(self.tok_emb.weight, mean=0.0, std=self.tied_embed_init_std) + for module in self.modules(): + if isinstance(module, nn.Linear) and getattr(module, "_zero_init", False): + nn.init.zeros_(module.weight) + if self.value_residual and len(self.blocks) > 0: + with torch.no_grad(): + self.blocks[0].attn.vr_lambda.copy_(torch.tensor([0.0, 1.0])) + + def forward(self, input_ids: Tensor, target_ids: Tensor) -> Tensor: + x = self.tok_emb(input_ids) + x = x + self.bigram_emb(input_ids) + x = F.rms_norm(x, (x.size(-1),)) + x = self.smear_gate(x) + x0 = x + skips: list[Tensor] = [] + v0 = None + if self.value_residual: + kv_h = self.blocks[0].attn.num_kv_heads + h_d = self.blocks[0].attn.head_dim + v0 = torch.zeros(x.size(0), kv_h, x.size(1), h_d, dtype=x.dtype, device=x.device) + + for i in range(self.num_encoder_layers): + x, raw_v = self.blocks[i](x, x0, v0=v0) + if i == 0 and raw_v is not None: + v0 = raw_v + skips.append(x) + for i in range(self.num_decoder_layers): + if skips: + skip = skips.pop() + gate = torch.sigmoid(self.skip_gates[i].to(dtype=x.dtype)) + scaled_skip = self.skip_weights[i].to(dtype=x.dtype)[None, None, :] * skip + x = gate[None, None, :] * x + (1.0 - gate[None, None, :]) * scaled_skip + x, raw_v = self.blocks[self.num_encoder_layers + i](x, x0, v0=v0) + + x = self.final_norm(x).reshape(-1, x.size(-1)) + targets = target_ids.reshape(-1) + if self.tie_embeddings: + logits_proj = F.linear(x, self.tok_emb.weight) + else: + if self.lm_head is None: + raise RuntimeError("lm_head is required when tie_embeddings=False") + logits_proj = self.lm_head(x) + logits = self.logit_softcap * torch.tanh(logits_proj / self.logit_softcap) + return F.cross_entropy(logits.float(), targets, reduction="mean") + + def forward_logits(self, input_ids: Tensor) -> Tensor: + """Return logits (bsz, seq_len, vocab) without computing loss.""" + x = self.tok_emb(input_ids) + x = x + self.bigram_emb(input_ids) + x = F.rms_norm(x, (x.size(-1),)) + x = self.smear_gate(x) + x0 = x + skips: list[Tensor] = [] + v0 = None + if self.value_residual: + kv_h = self.blocks[0].attn.num_kv_heads + h_d = self.blocks[0].attn.head_dim + v0 = torch.zeros(x.size(0), kv_h, x.size(1), h_d, dtype=x.dtype, device=x.device) + for i in range(self.num_encoder_layers): + x, raw_v = self.blocks[i](x, x0, v0=v0) + if i == 0 and raw_v is not None: + v0 = raw_v + skips.append(x) + for i in range(self.num_decoder_layers): + if skips: + skip = skips.pop() + gate = torch.sigmoid(self.skip_gates[i].to(dtype=x.dtype)) + scaled_skip = self.skip_weights[i].to(dtype=x.dtype)[None, None, :] * skip + x = gate[None, None, :] * x + (1.0 - gate[None, None, :]) * scaled_skip + x, raw_v = self.blocks[self.num_encoder_layers + i](x, x0, v0=v0) + x = self.final_norm(x) + if self.tie_embeddings: + logits_proj = F.linear(x, self.tok_emb.weight) + else: + logits_proj = self.lm_head(x) + return self.logit_softcap * torch.tanh(logits_proj / self.logit_softcap) + + +# ----------------------------- +# FROZEN N-GRAM ORACLE +# ----------------------------- + +class FrozenNgramOracle: + """Loads precomputed int8 log-prob tables from build_ngram_oracle.py into GPU memory. + + Two construction paths: + - FrozenNgramOracle(path, device): read compressed file from disk + - FrozenNgramOracle.from_bytes(raw_compressed, device): parse from in-memory blob + (used when reloading from a packed artifact — avoids per-rank temp files). + """ + + SCALE: float = 10.0 / 127.0 # int8 -> log-nats + MAGIC: int = 0x4E474F52 # 'NGOR' + + def __init__(self, path: str | None, device: torch.device): + self.tables: dict[int, Tensor] = {} + self.device = device + if not path or not Path(path).exists(): + return + with open(path, "rb") as fh: + compressed = fh.read() + self._parse(self._decompress(compressed)) + + @classmethod + def from_bytes(cls, raw_compressed: bytes, device: torch.device) -> "FrozenNgramOracle": + """Construct from an already-compressed in-memory blob. + + Used during artifact reload so we don't need to write a temp file on every + DDP rank (was a real hazard on shared filesystems). + """ + obj = cls.__new__(cls) + obj.tables = {} + obj.device = device + obj._parse(cls._decompress(raw_compressed)) + return obj + + @staticmethod + def _decompress(compressed: bytes) -> bytes: + if USE_ZSTD: + return zstd.ZstdDecompressor().decompress(compressed) + return zlib.decompress(compressed) + + def _parse(self, raw: bytes) -> None: + buf = io.BytesIO(raw) + magic, num_tables = struct.unpack(" bool: + return bool(self.tables) + + def orders(self) -> list[int]: + return sorted(self.tables.keys()) + + @staticmethod + def _fnv1a_hash(ctx_tokens: Tensor, buckets: int) -> Tensor: + """ctx_tokens: (..., ctx_len) int64. Returns (...) int64 bucket ids.""" + h = torch.full(ctx_tokens.shape[:-1], 2166136261, + dtype=torch.int64, device=ctx_tokens.device) + for i in range(ctx_tokens.shape[-1]): + h = h ^ ctx_tokens[..., i] + h = (h * 16777619) & 0xFFFFFFFF + return h % buckets + + def lookup(self, input_ids: Tensor, order: int) -> Tensor: + """Look up n-gram log-probs for each position. + + Args: + input_ids: (B, T) int64 + Returns: + (B, T, vocab) float16 log-probs. + Position t uses the (order-1) tokens ending at t as context. + """ + if order not in self.tables: + return self._uniform(input_ids) + B, T = input_ids.shape + table = self.tables[order] + rows, vocab = table.shape + + if order == 1: + return table[0].view(1, 1, vocab).expand(B, T, vocab) + + ctx_len = order - 1 + # Pad left for positions without full context + padded = torch.cat([ + torch.zeros(B, ctx_len, dtype=torch.int64, device=self.device), + input_ids, + ], dim=1) + + if order == 2 and rows == vocab: + # Exact bigram: use prev token directly as index + prev = padded[:, ctx_len - 1:ctx_len - 1 + T] + return table[prev.reshape(-1)].reshape(B, T, vocab) + + # Higher orders: hash the context window + ctx_windows = torch.stack([ + padded[:, i:i + T] for i in range(ctx_len) + ], dim=-1) # (B, T, ctx_len) + bucket_ids = self._fnv1a_hash(ctx_windows, rows) + return table[bucket_ids.reshape(-1)].reshape(B, T, vocab) + + def _uniform(self, input_ids: Tensor) -> Tensor: + B, T = input_ids.shape + return torch.full((B, T, 1024), + -math.log(1024), dtype=torch.float16, device=self.device) + + +# ----------------------------- +# COMPLEMENTARY TRAINING LOSS +# ----------------------------- + +def complementary_training_loss( + logits: Tensor, + targets: Tensor, + input_ids: Tensor, + oracle: FrozenNgramOracle, + alpha: float, + max_order: int = 6, +) -> Tensor: + """Down-weight tokens where the oracle is confident. + + Weight = 1 - alpha * max_over_orders(P_oracle(target | context)). + Forces the neural model to learn residual signal n-grams can't predict. + + NOTE: Currently UNUSED at training time — see README §9. Kept here for the + planned inside-graph integration (requires threading `oracle` through + `GPT.forward` so logits are accessible without recompiling the graph). + The earlier inline approach (scaling scalar mean CE by mean weight) was + mathematically incorrect and was removed honestly rather than left active. + + Note on gradient norm: this is a weighted mean (sum/weight.sum). When most + weights are < 1 (high oracle confidence), the effective LR drops vs standard + CE. If wired in, either rescale by len(weight)/weight.sum() or lower the + base LR commensurately to preserve gradient magnitude. + """ + B, T = input_ids.shape + tgt_2d = targets.reshape(B, T) + + best_confidence = torch.zeros(B, T, dtype=torch.float32, device=logits.device) + for order in oracle.orders(): + if order > max_order: + continue + lp = oracle.lookup(input_ids, order) # (B, T, vocab) float16 + tgt_lp = lp.float().gather(-1, tgt_2d.unsqueeze(-1)).squeeze(-1) + confidence = tgt_lp.exp() + best_confidence = torch.maximum(best_confidence, confidence) + + weight = (1.0 - alpha * best_confidence).reshape(-1).detach().clamp_min(0.1) + ce = F.cross_entropy(logits, targets, reduction="none") + return (ce * weight).sum() / weight.sum() + + +# ----------------------------- +# HEDGE MIXER (online expert mixing for TTT scoring) +# ----------------------------- + +def _trigram_hash(prev2: Tensor, prev1: Tensor, buckets: int) -> Tensor: + return ((prev2.long() * 1000003) ^ (prev1.long() * 1009)) % buckets + + +class HedgeMixer: + """Hedge algorithm: Neural + Unigram + Bigram + Trigram + Cache + Oracle experts.""" + + def __init__(self, eta: float, vocab_size: int, trigram_buckets: int, + cache_gamma: float, device: torch.device, + oracle: FrozenNgramOracle | None = None): + self.eta = eta + self.vocab_size = vocab_size + self.trigram_buckets = trigram_buckets + self.cache_gamma = cache_gamma + self.device = device + self.oracle = oracle + # tuple() to make immutability explicit — prevents accidental mutation + self.oracle_orders = tuple(oracle.orders()) if oracle is not None and oracle.is_loaded() else () + self.num_experts = 5 + len(self.oracle_orders) + # Warm prior: favor the neural expert at init so Hedge starts sensibly on short + # eval streams. With K=13 experts (5 base + 8 oracle orders), uniform init can + # mix poorly before convergence. log_w[0]=2 -> ~88% initial weight on neural. + self.log_w = torch.zeros(self.num_experts, dtype=torch.float64, device=device) + self.log_w[0] = 2.0 + # N-gram tables with Laplace smoothing (add-1). + # NOTE: bi_counts is dense (vocab x vocab). At vocab=1024 this is 4 MB on GPU per + # rank — fine. At vocab=4096+ this would be 64 MB+; use a hashed table instead. + assert vocab_size <= 2048, f"bi_counts is dense; vocab_size={vocab_size} too large" + self.uni_counts = torch.ones(vocab_size, device=device) + self.uni_total = float(vocab_size) + self.bi_counts = torch.ones(vocab_size, vocab_size, device=device) + self.bi_totals = torch.full((vocab_size,), float(vocab_size), device=device) + self.tri_counts = torch.ones(trigram_buckets, vocab_size, device=device) + self.tri_totals = torch.full((trigram_buckets,), float(vocab_size), device=device) + self.cache = torch.ones(vocab_size, device=device) # Laplace init + + def recenter_weights(self): + self.log_w -= self.log_w.max() + + def compute_mixed_nll(self, log_probs: Tensor, prev: Tensor, prev2: Tensor, + targets: Tensor, + x_context: Tensor | None = None) -> Tensor: + """Compute Hedge-mixed NLL for scored tokens. + + Args: + log_probs: (N, vocab) log-softmax of neural logits + prev: (N,) previous token IDs + prev2: (N,) two-back token IDs + targets: (N,) target token IDs + x_context: (1, N) input IDs for oracle lookup (optional) + Returns: + mixed_nll: (N,) per-token mixed NLL + """ + n = targets.shape[0] + arange_n = torch.arange(n, device=self.device) + expert_nlls = torch.zeros(self.num_experts, n, device=self.device) + + # Expert 0: Neural + expert_nlls[0] = -log_probs[arange_n, targets] + + # Expert 1: Unigram + uni_lp = torch.log(self.uni_counts / self.uni_total) + expert_nlls[1] = -uni_lp[targets] + + # Expert 2: Bigram + bi_probs = self.bi_counts[prev] / self.bi_totals[prev].unsqueeze(-1) + expert_nlls[2] = -torch.log(bi_probs[arange_n, targets]) + + # Expert 3: Trigram + tri_h = _trigram_hash(prev2, prev, self.trigram_buckets) + tri_probs = self.tri_counts[tri_h] / self.tri_totals[tri_h].unsqueeze(-1) + expert_nlls[3] = -torch.log(tri_probs[arange_n, targets]) + + # Expert 4: Cache (exponentially decaying recent frequency) + cache_lp = torch.log(self.cache / self.cache.sum()) + expert_nlls[4] = -cache_lp[targets] + + # Experts 5+: Frozen oracle (one per n-gram order) + if self.oracle is not None and x_context is not None: + for idx, order in enumerate(self.oracle_orders): + lp = self.oracle.lookup(x_context, order) # (1, N, vocab) + tgt_lp = lp[0, arange_n, targets.long()].float() + expert_nlls[5 + idx] = -tgt_lp + + # Hedge mixing in log-space: -logsumexp(log(p_k) + log(w_k)) + weights = torch.softmax(self.log_w, dim=0).float() + log_weighted = -expert_nlls + torch.log(weights + 1e-10).unsqueeze(1) + mixed_nll = -torch.logsumexp(log_weighted, dim=0) + + # Update Hedge weights + self.log_w -= self.eta * expert_nlls.to(torch.float64).mean(dim=1) + + return mixed_nll + + def update_tables(self, prev: Tensor, prev2: Tensor, targets: Tensor): + """Update n-gram + cache tables with scored tokens (call AFTER scoring). + + Uses index_put_ with accumulate=True for 2-D writes. Naive `t[prev, targets] += 1` + is non-deterministic when prev/targets contain duplicates (common bigrams) — only + one of the duplicate writes lands. This bug existed in the base PR #462 stack. + """ + ones_t = torch.ones_like(targets, dtype=torch.float32) + self.uni_counts.scatter_add_(0, targets, ones_t) + self.uni_total += float(targets.shape[0]) + self.bi_counts.index_put_((prev, targets), ones_t, accumulate=True) + self.bi_totals.scatter_add_(0, prev, ones_t) + tri_h = _trigram_hash(prev2, prev, self.trigram_buckets) + self.tri_counts.index_put_((tri_h, targets), ones_t, accumulate=True) + self.tri_totals.scatter_add_(0, tri_h, ones_t) + # Cache: exponential decay then add + self.cache *= self.cache_gamma + self.cache.scatter_add_(0, targets, ones_t) + + +# ----------------------------- +# TEST-TIME TRAINING (TTT) +# ----------------------------- + +def ttt_score_first( + args: Hyperparameters, + base_model: nn.Module, + device: torch.device, + val_tokens: Tensor, + base_bytes_lut: Tensor, + has_leading_space_lut: Tensor, + is_boundary_token_lut: Tensor, + oracle: FrozenNgramOracle | None = None, + rank: int = 0, + world_size: int = 1, + log_fn=None, +) -> tuple[float, float]: + """Legal score-first TTT: score each chunk, then train on it. Returns (val_loss, val_bpb).""" + seq_len = args.train_seq_len + stride = args.eval_stride if args.eval_stride > 0 else seq_len + chunk_size = args.ttt_chunk_tokens + total_tokens = val_tokens.numel() - 1 + num_chunks = (total_tokens + chunk_size - 1) // chunk_size + + # Freeze early blocks + if args.ttt_freeze_blocks > 0: + for i, block in enumerate(base_model.blocks): + if i < args.ttt_freeze_blocks: + for p in block.parameters(): + p.requires_grad_(False) + + # Per-layer LR groups + output_params, input_params, other_params = [], [], [] + for name, p in base_model.named_parameters(): + if not p.requires_grad: + continue + if '.proj.' in name or name.endswith('.proj.weight') or 'down_proj' in name: + output_params.append(p) + elif '.c_q.' in name or '.c_k.' in name or '.c_v.' in name or 'up_proj' in name: + input_params.append(p) + else: + other_params.append(p) + ttt_params = output_params + input_params + other_params + param_groups = [ + {"params": output_params, "lr": args.ttt_lr * 3.0}, + {"params": input_params, "lr": args.ttt_lr * 0.5}, + {"params": other_params, "lr": args.ttt_lr}, + ] + if args.ttt_optimizer == "sgd": + optimizer = torch.optim.SGD(param_groups, momentum=0.9) + scheduler = None # SGD TTT uses constant LR (PR #967) + else: + optimizer = torch.optim.AdamW(param_groups, weight_decay=0.0) + scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts( + optimizer, T_0=max(args.ttt_epochs_per_chunk, 1), eta_min=args.ttt_lr * 0.1 + ) + + # Hedge Mixer with optional oracle experts + if args.hedge_enabled: + hedge = HedgeMixer( + eta=args.hedge_eta, vocab_size=args.vocab_size, + trigram_buckets=args.hedge_trigram_buckets, + cache_gamma=args.hedge_cache_gamma, device=device, + oracle=oracle, + ) + else: + hedge = None + + # Accumulators + total_loss = torch.zeros((), device=device, dtype=torch.float64) + total_tokens_scored = torch.zeros((), device=device, dtype=torch.float64) + total_bytes = torch.zeros((), device=device, dtype=torch.float64) + t0 = time.perf_counter() + + for chunk_idx in range(num_chunks): + c_start = chunk_idx * chunk_size + c_end = min(c_start + chunk_size, total_tokens) + chunk_toks = val_tokens[c_start:c_end + 1] # +1 for targets + + # === SCORE: sliding window eval on this chunk (inference_mode) === + base_model.eval() + chunk_len = c_end - c_start + window_starts = [ws for ws in range(0, chunk_len, stride) if min(ws + seq_len, chunk_len) - ws >= stride] + my_s = (len(window_starts) * rank) // world_size + my_e = (len(window_starts) * (rank + 1)) // world_size + my_windows = window_starts[my_s:my_e] + + with torch.inference_mode(): + for bi in range(0, len(my_windows), args.ttt_batch_seqs): + batch_ws = my_windows[bi:bi + args.ttt_batch_seqs] + bsz = len(batch_ws) + x_b = torch.zeros(bsz, seq_len, dtype=torch.int64, device=device) + y_b = torch.zeros(bsz, seq_len, dtype=torch.int64, device=device) + wlens = [] + for i, ws in enumerate(batch_ws): + end = min(ws + seq_len, chunk_len) + wlen = end - ws + wlens.append(wlen) + chunk_slice = chunk_toks[ws:end + 1].to(dtype=torch.int64, device=device) + x_b[i, :wlen] = chunk_slice[:-1] + y_b[i, :wlen] = chunk_slice[1:] + with torch.autocast(device_type="cuda", dtype=torch.bfloat16): + logits = base_model.forward_logits(x_b) + logits_f = logits.float() + if hedge is not None: + log_probs_full = F.log_softmax(logits_f, dim=-1) + nll = F.cross_entropy(logits_f.reshape(-1, logits_f.size(-1)), y_b.reshape(-1), reduction="none").reshape(bsz, seq_len) + for i, ws in enumerate(batch_ws): + wlen = wlens[i] + s = 0 if ws == 0 else wlen - stride + scored_prev = x_b[i, s:wlen] + scored_tgt = y_b[i, s:wlen] + if hedge is not None: + scored_lp = log_probs_full[i, s:wlen] + scored_len = wlen - s + scored_prev2 = torch.zeros(scored_len, dtype=torch.int64, device=device) + if s > 0: + scored_prev2 = x_b[i, s - 1:wlen - 1] + else: + if scored_len > 1: + scored_prev2[1:] = x_b[i, 0:wlen - 1] + x_ctx = x_b[i:i+1, s:wlen] if hedge.oracle is not None else None + mixed = hedge.compute_mixed_nll(scored_lp, scored_prev, scored_prev2, scored_tgt, x_context=x_ctx) + total_loss += mixed.to(torch.float64).sum() + hedge.update_tables(scored_prev, scored_prev2, scored_tgt) + else: + total_loss += nll[i, s:wlen].to(torch.float64).sum() + tb = base_bytes_lut[scored_tgt].to(torch.int16) + tb += (has_leading_space_lut[scored_tgt] & ~is_boundary_token_lut[scored_prev]).to(torch.int16) + total_bytes += tb.to(torch.float64).sum() + total_tokens_scored += float(wlen - s) + + # === TRAIN on scored chunk (unless last chunk) === + if chunk_idx < num_chunks - 1: + base_model.train() + chunk_seqs = (chunk_toks.numel() - 1) // seq_len + if chunk_seqs > 0: + my_cs = (chunk_seqs * rank) // world_size + my_ce = (chunk_seqs * (rank + 1)) // world_size + for _epoch in range(args.ttt_epochs_per_chunk): + if time.perf_counter() - t0 > args.ttt_time_budget: + break + for bs in range(my_cs, my_ce, args.ttt_batch_seqs): + be = min(bs + args.ttt_batch_seqs, my_ce) + local = chunk_toks[bs * seq_len:(be * seq_len) + 1].to(device=device, dtype=torch.int64) + x = local[:-1].reshape(-1, seq_len) + y = local[1:].reshape(-1, seq_len) + optimizer.zero_grad(set_to_none=True) + with torch.autocast(device_type="cuda", dtype=torch.bfloat16): + loss = base_model(x, y) + loss.backward() + if world_size > 1: + # Bucketed all_reduce: flatten gradients into one tensor to avoid + # 100s of separate all_reduce calls (was a major TTT bottleneck). + grads = [p.grad for p in ttt_params if p.grad is not None] + if grads: + flat = torch.cat([g.reshape(-1) for g in grads]) + dist.all_reduce(flat, op=dist.ReduceOp.AVG) + offset = 0 + for g in grads: + n = g.numel() + g.copy_(flat[offset:offset + n].reshape(g.shape)) + offset += n + torch.nn.utils.clip_grad_norm_(ttt_params, 1.0) + optimizer.step() + if scheduler is not None: + scheduler.step() + + # Re-center Hedge weights per chunk (prevents float64 overflow) + if hedge is not None: + hedge.recenter_weights() + + if log_fn and (chunk_idx % 10 == 0 or chunk_idx == num_chunks - 1): + log_fn(f"ttt_chunk:{chunk_idx + 1}/{num_chunks} elapsed:{time.perf_counter() - t0:.1f}s") + + # Cleanup + for p in base_model.parameters(): + p.requires_grad_(True) + + if dist.is_available() and dist.is_initialized(): + dist.all_reduce(total_loss, op=dist.ReduceOp.SUM) + dist.all_reduce(total_tokens_scored, op=dist.ReduceOp.SUM) + dist.all_reduce(total_bytes, op=dist.ReduceOp.SUM) + + val_loss = (total_loss / total_tokens_scored).item() + val_bpb = (val_loss / math.log(2.0)) * (total_tokens_scored.item() / total_bytes.item()) + if log_fn: + log_fn(f"ttt:done loss:{val_loss:.4f} bpb:{val_bpb:.4f} elapsed:{time.perf_counter() - t0:.1f}s") + return val_loss, val_bpb + + +# ----------------------------- +# TRAINING +# ----------------------------- + +def main() -> None: + global zeropower_via_newtonschulz5 + + code = Path(__file__).read_text(encoding="utf-8") + args = Hyperparameters() + zeropower_via_newtonschulz5 = torch.compile(zeropower_via_newtonschulz5) + + # ----------------------------- + # DISTRIBUTED + CUDA SETUP + # ----------------------------- + + distributed = "RANK" in os.environ and "WORLD_SIZE" in os.environ + rank = int(os.environ.get("RANK", "0")) + world_size = int(os.environ.get("WORLD_SIZE", "1")) + local_rank = int(os.environ.get("LOCAL_RANK", "0")) + if world_size <= 0: + raise ValueError(f"WORLD_SIZE must be positive, got {world_size}") + if 8 % world_size != 0: + raise ValueError(f"WORLD_SIZE={world_size} must divide 8 so grad_accum_steps stays integral") + grad_accum_steps = 8 // world_size + grad_scale = 1.0 / grad_accum_steps + if not torch.cuda.is_available(): + raise RuntimeError("CUDA is required") + device = torch.device("cuda", local_rank) + torch.cuda.set_device(device) + if distributed: + dist.init_process_group(backend="nccl", device_id=device) + dist.barrier() + master_process = rank == 0 + + torch.backends.cuda.matmul.allow_tf32 = True + torch.backends.cudnn.allow_tf32 = True + from torch.backends.cuda import enable_cudnn_sdp, enable_flash_sdp, enable_math_sdp, enable_mem_efficient_sdp + enable_cudnn_sdp(False) + enable_flash_sdp(True) + enable_mem_efficient_sdp(True) + enable_math_sdp(True) + + logfile = None + if master_process: + os.makedirs("logs", exist_ok=True) + logfile = f"logs/{args.run_id}.txt" + print(logfile) + + def log0(msg: str, console: bool = True) -> None: + if not master_process: + return + if console: + print(msg) + if logfile is not None: + with open(logfile, "a", encoding="utf-8") as f: + print(msg, file=f) + + log0(code, console=False) + log0("=" * 100, console=False) + log0(f"Running Python {sys.version}", console=False) + log0(f"Running PyTorch {torch.__version__}", console=False) + log0(subprocess.run(["nvidia-smi"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=False).stdout, console=False) + log0("=" * 100, console=False) + + # ----------------------------- + # TOKENIZER + VALIDATION METRIC SETUP + # ----------------------------- + + random.seed(args.seed) + np.random.seed(args.seed) + torch.manual_seed(args.seed) + torch.cuda.manual_seed_all(args.seed) + + if not args.tokenizer_path.endswith(".model"): + raise ValueError(f"Script only setup for SentencePiece .model file: {args.tokenizer_path}") + sp = spm.SentencePieceProcessor(model_file=args.tokenizer_path) + if int(sp.vocab_size()) != args.vocab_size: + raise ValueError(f"VOCAB_SIZE={args.vocab_size} does not match tokenizer vocab_size={int(sp.vocab_size())}") + dataset_dir = Path(args.data_path).resolve() + actual_train_files = len(list(dataset_dir.glob("fineweb_train_*.bin"))) + val_tokens = load_validation_tokens(args.val_files, args.train_seq_len) + base_bytes_lut, has_leading_space_lut, is_boundary_token_lut = build_sentencepiece_luts(sp, args.vocab_size, device) + log0(f"val_bpb:enabled tokenizer_kind=sentencepiece tokenizer_path={args.tokenizer_path}") + log0(f"train_loader:dataset:{dataset_dir.name} train_shards:{actual_train_files}") + log0(f"val_loader:shards pattern={args.val_files} tokens:{val_tokens.numel() - 1}") + + # ----------------------------- + # MODEL + OPTIMIZER SETUP + # ----------------------------- + + CastedLinear._qat_enabled = False # start with QAT off; late_qat enables it mid-run + + base_model = GPT( + vocab_size=args.vocab_size, + num_layers=args.num_layers, + model_dim=args.model_dim, + num_heads=args.num_heads, + num_kv_heads=args.num_kv_heads, + mlp_mult=args.mlp_mult, + tie_embeddings=args.tie_embeddings, + tied_embed_init_std=args.tied_embed_init_std, + logit_softcap=args.logit_softcap, + rope_base=args.rope_base, + qk_gain_init=args.qk_gain_init, + mlp_hidden=args.mlp_hidden, + bigram_buckets=args.bigram_buckets, + bigram_embed_dim=args.bigram_embed_dim, + rope_dims=args.rope_dims, + ln_scale=args.ln_scale, + xsa_last_n=args.xsa_layers, + value_residual=args.value_residual, + gated_attention=args.gated_attention, + leaky_relu_slope=args.leaky_relu_slope, + ).to(device).bfloat16() + for module in base_model.modules(): + if isinstance(module, CastedLinear): + module.float() + restore_low_dim_params_to_fp32(base_model) + compiled_model = torch.compile(base_model, dynamic=False, fullgraph=True) + model: nn.Module = DDP(compiled_model, device_ids=[local_rank], broadcast_buffers=False) if distributed else compiled_model + + # Load frozen n-gram oracle for complementary training + train_oracle = None + if args.ngram_oracle_path and Path(args.ngram_oracle_path).exists(): + train_oracle = FrozenNgramOracle(args.ngram_oracle_path, device) + log0(f"oracle:loaded for training orders={train_oracle.orders()} alpha={args.complementary_alpha}") + elif args.ngram_oracle_path: + # User asked for oracle but file is missing — make this loud, not silent. + log0(f"oracle:WARN NGRAM_ORACLE_PATH={args.ngram_oracle_path!r} set but file does not exist; oracle disabled") + elif args.complementary_alpha > 0.0: + log0("oracle:WARN complementary_alpha>0 but no oracle path set; complementary loss is inert (see README §9)") + + # Differential LR setup + matrix_params_enc, scalar_params_enc = [], [] + matrix_params_dec, scalar_params_dec = [], [] + num_encoder_layers = base_model.num_encoder_layers + for i, block in enumerate(base_model.blocks): + is_decoder = i >= num_encoder_layers + for name, p in block.named_parameters(): + if p.ndim == 2 and not any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS): + (matrix_params_dec if is_decoder else matrix_params_enc).append(p) + else: + (scalar_params_dec if is_decoder else scalar_params_enc).append(p) + + # Non-block scalar parameters + other_scalar_params = [base_model.smear_gate.gate, base_model.bigram_emb.embed.weight] + if base_model.skip_weights.numel() > 0: + other_scalar_params.append(base_model.skip_weights) + if hasattr(base_model, 'skip_gates') and base_model.skip_gates.numel() > 0: + other_scalar_params.append(base_model.skip_gates) + + token_lr = args.tied_embed_lr if args.tie_embeddings else args.embed_lr + optimizer_tok = torch.optim.AdamW( + [{"params": [base_model.tok_emb.weight], "lr": token_lr, "base_lr": token_lr}], + betas=(args.beta1, args.beta2), + eps=args.adam_eps, + weight_decay=args.adam_wd, + fused=True, + ) + + matrix_lr_dec = args.matrix_lr * args.decoder_lr_mult + optimizer_muon = Muon( + [ + {'params': matrix_params_enc, 'lr': args.matrix_lr, 'base_lr': args.matrix_lr}, + {'params': matrix_params_dec, 'lr': matrix_lr_dec, 'base_lr': matrix_lr_dec}, + ], + lr=args.matrix_lr, + momentum=args.muon_momentum, + backend_steps=args.muon_backend_steps, + weight_decay=args.muon_wd, + ) + + scalar_lr_dec = args.scalar_lr * args.decoder_lr_mult + optimizer_scalar = torch.optim.AdamW( + [ + {'params': scalar_params_enc, 'lr': args.scalar_lr, 'base_lr': args.scalar_lr}, + {'params': scalar_params_dec, 'lr': scalar_lr_dec, 'base_lr': scalar_lr_dec}, + {'params': other_scalar_params, 'lr': args.scalar_lr, 'base_lr': args.scalar_lr}, + ], + betas=(args.beta1, args.beta2), + eps=args.adam_eps, + weight_decay=args.adam_wd, + fused=True, + ) + + optimizer_bigram_proj = Muon( + [base_model.bigram_emb.proj.weight], + lr=args.matrix_lr, + momentum=args.muon_momentum, + backend_steps=args.muon_backend_steps, + weight_decay=args.muon_wd, + ) + for group in optimizer_bigram_proj.param_groups: + group["base_lr"] = args.matrix_lr + + optimizers: list[torch.optim.Optimizer] = [optimizer_tok, optimizer_muon, optimizer_scalar, optimizer_bigram_proj] + if base_model.lm_head is not None: + optimizer_head = torch.optim.AdamW( + [{"params": [base_model.lm_head.weight], "lr": args.head_lr, "base_lr": args.head_lr}], + betas=(args.beta1, args.beta2), + eps=args.adam_eps, + weight_decay=args.adam_wd, + fused=True, + ) + optimizers.insert(1, optimizer_head) + + n_params = sum(p.numel() for p in base_model.parameters()) + log0(f"model_params:{n_params}") + log0(f"world_size:{world_size} grad_accum_steps:{grad_accum_steps}") + log0("sdp_backends:cudnn=False flash=True mem_efficient=False math=False") + log0(f"attention_mode:gqa num_heads:{args.num_heads} num_kv_heads:{args.num_kv_heads}") + log0(f"tie_embeddings:{args.tie_embeddings} embed_lr:{token_lr} head_lr:{args.head_lr if base_model.lm_head is not None else 0.0} matrix_lr:{args.matrix_lr} scalar_lr:{args.scalar_lr} decoder_lr_mult:{args.decoder_lr_mult}") + log0(f"train_batch_tokens:{args.train_batch_tokens} train_seq_len:{args.train_seq_len} iterations:{args.iterations} warmup_steps:{args.warmup_steps} max_wallclock_seconds:{args.max_wallclock_seconds:.3f}") + log0(f"rope_dims:{args.rope_dims} ln_scale:{args.ln_scale}") + log0(f"muon_wd:{args.muon_wd} adam_wd:{args.adam_wd} ema_enabled:{args.ema_enabled} late_qat:{args.late_qat} ttt_enabled:{args.ttt_enabled}") + log0(f"bigram_buckets:{args.bigram_buckets} bigram_embed_dim:{args.bigram_embed_dim} seed:{args.seed}") + + # ----------------------------- + # DATA LOADER & MODEL WARMUP + # ----------------------------- + + train_loader = DistributedTokenLoader(args.train_files, rank, world_size, device) + + def zero_grad_all() -> None: + for opt in optimizers: + opt.zero_grad(set_to_none=True) + + max_wallclock_ms = 1000.0 * args.max_wallclock_seconds if args.max_wallclock_seconds > 0 else None + + def lr_mul(step: int, elapsed_ms: float) -> float: + if args.warmdown_iters <= 0: + return 1.0 + if max_wallclock_ms is None: + warmdown_start = max(args.iterations - args.warmdown_iters, 0) + return max((args.iterations - step) / max(args.warmdown_iters, 1), 0.0) if warmdown_start <= step < args.iterations else 1.0 + step_ms = elapsed_ms / max(step, 1) + warmdown_ms = args.warmdown_iters * step_ms + remaining_ms = max(max_wallclock_ms - elapsed_ms, 0.0) + return remaining_ms / max(warmdown_ms, 1e-9) if remaining_ms <= warmdown_ms else 1.0 + + if args.warmup_steps > 0: + initial_model_state = {name: tensor.detach().cpu().clone() for name, tensor in base_model.state_dict().items()} + initial_optimizer_states = [copy.deepcopy(opt.state_dict()) for opt in optimizers] + model.train() + for warmup_step in range(args.warmup_steps): + zero_grad_all() + for micro_step in range(grad_accum_steps): + if distributed: + model.require_backward_grad_sync = micro_step == grad_accum_steps - 1 + x, y = train_loader.next_batch(args.train_batch_tokens, args.train_seq_len, grad_accum_steps) + with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True): + warmup_loss = model(x, y) + (warmup_loss * grad_scale).backward() + for opt in optimizers: + opt.step() + zero_grad_all() + if args.warmup_steps <= 20 or (warmup_step + 1) % 10 == 0 or warmup_step + 1 == args.warmup_steps: + log0(f"warmup_step:{warmup_step + 1}/{args.warmup_steps}") + base_model.load_state_dict(initial_model_state, strict=True) + for opt, state in zip(optimizers, initial_optimizer_states, strict=True): + opt.load_state_dict(state) + zero_grad_all() + if distributed: + model.require_backward_grad_sync = True + train_loader = DistributedTokenLoader(args.train_files, rank, world_size, device) + + # ----------------------------- + # EMA / SWA STATE + # ----------------------------- + + # EMA takes priority; SWA is fallback (mutually exclusive) + ema_state: dict[str, Tensor] | None = None + if args.ema_enabled: + ema_state = {name: t.detach().float().clone() for name, t in base_model.state_dict().items()} + log0(f"ema:init decay={args.ema_decay}") + + swa_state: dict[str, Tensor] = {} + swa_count = 0 + + def update_swa(): + nonlocal swa_count + with torch.no_grad(): + for name, param in base_model.state_dict().items(): + if name not in swa_state: + swa_state[name] = param.detach().cpu().clone().float() + else: + swa_state[name].add_(param.detach().cpu().float()) + swa_count += 1 + + def get_swa_state() -> dict[str, Tensor]: + return {name: (t / swa_count).to(dtype=base_model.state_dict()[name].dtype) for name, t in swa_state.items()} + + # ----------------------------- + # MAIN TRAINING LOOP + # ----------------------------- + + training_time_ms = 0.0 + stop_after_step: int | None = None + torch.cuda.synchronize() + t0 = time.perf_counter() + + # Estimate total steps for SWA start + estimated_total_steps = args.iterations + if max_wallclock_ms is not None: + estimated_total_steps = min(args.iterations, int(max_wallclock_ms / 30)) # rough estimate + + step = 0 + while True: + last_step = step == args.iterations or (stop_after_step is not None and step >= stop_after_step) + + should_validate = last_step or (args.val_loss_every > 0 and step % args.val_loss_every == 0) + if should_validate: + torch.cuda.synchronize() + training_time_ms += 1000.0 * (time.perf_counter() - t0) + val_loss, val_bpb = eval_val( + args, model, rank, world_size, device, grad_accum_steps, + val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, + ) + log0(f"step:{step}/{args.iterations} val_loss:{val_loss:.4f} val_bpb:{val_bpb:.4f} train_time:{training_time_ms:.0f}ms step_avg:{training_time_ms / max(step, 1):.2f}ms") + torch.cuda.synchronize() + t0 = time.perf_counter() + + if last_step: + if stop_after_step is not None and step < args.iterations: + log0(f"stopping_early: wallclock_cap train_time:{training_time_ms:.0f}ms step:{step}/{args.iterations}") + break + + elapsed_ms = training_time_ms + 1000.0 * (time.perf_counter() - t0) + scale = lr_mul(step, elapsed_ms) + + # Late QAT: enable fake int6 quantization once LR scale drops below threshold + if args.late_qat and not CastedLinear._qat_enabled and scale < args.qat_threshold: + CastedLinear._qat_enabled = True + log0(f"late_qat:enabled step:{step} scale:{scale:.4f}") + + zero_grad_all() + train_loss = torch.zeros((), device=device) + for micro_step in range(grad_accum_steps): + if distributed: + model.require_backward_grad_sync = micro_step == grad_accum_steps - 1 + x, y = train_loader.next_batch(args.train_batch_tokens, args.train_seq_len, grad_accum_steps) + with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True): + loss = model(x, y) + # Note: per-token complementary reweighting requires logits access from inside + # the compiled model. The standalone `complementary_training_loss` function is + # kept for reference and future integration; for now the oracle only contributes + # at eval/TTT time via HedgeMixer. + if args.crown_lambda > 0.0 and CastedLinear._qat_enabled: + loss = loss + args.crown_lambda * crown_q_penalty(base_model) + train_loss += loss.detach() + (loss * grad_scale).backward() + train_loss /= grad_accum_steps + + frac = min(step / args.muon_momentum_warmup_steps, 1.0) if args.muon_momentum_warmup_steps > 0 else 1.0 + muon_momentum = (1 - frac) * args.muon_momentum_warmup_start + frac * args.muon_momentum + for group in optimizer_muon.param_groups: + group["momentum"] = muon_momentum + for group in optimizer_bigram_proj.param_groups: + group["momentum"] = muon_momentum + + for opt in optimizers: + for group in opt.param_groups: + group["lr"] = group["base_lr"] * scale + + if args.grad_clip_norm > 0: + torch.nn.utils.clip_grad_norm_(base_model.parameters(), args.grad_clip_norm) + for opt in optimizers: + opt.step() + zero_grad_all() + + step += 1 + + # EMA update every step (takes priority over SWA) + if ema_state is not None: + d = args.ema_decay + with torch.no_grad(): + for name, t in base_model.state_dict().items(): + ema_state[name].mul_(d).add_(t.detach().float(), alpha=1.0 - d) + + # SWA update (only when EMA disabled) + swa_start_step = int(estimated_total_steps * args.swa_start_frac) + if ema_state is None and step >= swa_start_step and step % args.swa_every == 0: + update_swa() + + approx_training_time_ms = training_time_ms + 1000.0 * (time.perf_counter() - t0) + should_log_train = args.train_log_every > 0 and (step <= 10 or step % args.train_log_every == 0 or stop_after_step is not None) + if should_log_train: + log0(f"step:{step}/{args.iterations} train_loss:{train_loss.item():.4f} train_time:{approx_training_time_ms:.0f}ms step_avg:{approx_training_time_ms / step:.2f}ms") + + reached_cap = max_wallclock_ms is not None and approx_training_time_ms >= max_wallclock_ms + if distributed and max_wallclock_ms is not None: + reached_cap_tensor = torch.tensor(int(reached_cap), device=device) + dist.all_reduce(reached_cap_tensor, op=dist.ReduceOp.MAX) + reached_cap = bool(reached_cap_tensor.item()) + if stop_after_step is None and reached_cap: + stop_after_step = step + + # Final SWA update (only if EMA disabled and no SWA yet) + if ema_state is None and swa_count == 0: + update_swa() + + log0(f"peak memory allocated: {torch.cuda.max_memory_allocated() // 1024 // 1024} MiB reserved: {torch.cuda.max_memory_reserved() // 1024 // 1024} MiB") + + # Apply EMA or SWA weights (EMA takes priority) + if ema_state is not None: + log0("ema:applying EMA weights") + avg_state = {name: t.to(dtype=base_model.state_dict()[name].dtype) for name, t in ema_state.items()} + del ema_state + base_model.load_state_dict(avg_state, strict=True) + del avg_state + elif swa_count > 0: + log0(f"swa:applying averaged {swa_count} checkpoints") + base_model.load_state_dict(get_swa_state(), strict=True) + else: + log0("weight_avg:skipped (no EMA or SWA state)") + + # ----------------------------- + # SERIALIZATION + ROUNDTRIP VALIDATION + # ----------------------------- + + if master_process: + torch.save(base_model.state_dict(), "final_model.pt") + model_bytes = os.path.getsize("final_model.pt") + code_bytes = len(code.encode("utf-8")) + log0(f"Serialized model: {model_bytes} bytes") + log0(f"Code size: {code_bytes} bytes") + log0(f"Total submission size: {model_bytes + code_bytes} bytes") + + quant_obj, quant_stats = quantize_state_dict_int6(base_model.state_dict()) + quant_buf = io.BytesIO() + torch.save(quant_obj, quant_buf) + quant_raw = quant_buf.getvalue() + + # Use zstd-22 for compression (or zlib fallback) + if USE_ZSTD: + quant_blob = zstd.ZstdCompressor(level=22).compress(quant_raw) + compression_method = "zstd-22" + else: + quant_blob = zlib.compress(quant_raw, level=9) + compression_method = "zlib-9" + + quant_raw_bytes = len(quant_raw) + + # Pack oracle blob into artifact if available + oracle_blob = b"" + if args.ngram_oracle_path and Path(args.ngram_oracle_path).exists(): + with open(args.ngram_oracle_path, "rb") as f: + oracle_blob = f.read() + log0(f"oracle:packing {len(oracle_blob):,} bytes into artifact") + + # Artifact wrapper format: + # + # + # + # Total header: 16 bytes. Magic + version let future versions detect format + # mismatch instead of silently mis-slicing a corrupt or version-skewed file. + PGAR_MAGIC = 0x50474152 # 'PGAR' (Parameter Golf ARtifact) + PGAR_VERSION = 1 + header = struct.pack(" 16 * 1024 * 1024: + log0("WARNING: artifact exceeds 16MB limit!") + + if distributed: + dist.barrier() + with open("final_model.int6.ptz", "rb") as f: + artifact_data = f.read() + + # Validate magic + version, then unpack + magic, version, neural_len, oracle_len = struct.unpack(" 0: + oracle = FrozenNgramOracle.from_bytes(oracle_blob_disk, device) + log0(f"oracle:loaded from artifact orders={oracle.orders()}") + torch.cuda.synchronize() + t_qeval = time.perf_counter() + + if args.ttt_enabled: + log0(f"ttt_score_first:start lr={args.ttt_lr} optimizer={args.ttt_optimizer} " + f"epochs_per_chunk={args.ttt_epochs_per_chunk} " + f"chunk_tokens={args.ttt_chunk_tokens} freeze_blocks={args.ttt_freeze_blocks}" + f" oracle={'yes' if oracle is not None and oracle.is_loaded() else 'no'}") + q_val_loss, q_val_bpb = ttt_score_first( + args, base_model, device, val_tokens, + base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, + oracle=oracle, rank=rank, world_size=world_size, log_fn=log0, + ) + elif args.eval_stride > 0: + q_val_loss, q_val_bpb = eval_val_sliding( + args, base_model, rank, world_size, device, val_tokens, + base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, + stride=args.eval_stride, batch_seqs=args.eval_batch_seqs, + ) + else: + q_val_loss, q_val_bpb = 0.0, 0.0 + log0("eval:skipped (EVAL_STRIDE=0 and TTT_ENABLED=0)") + + torch.cuda.synchronize() + log0(f"final_int6_{compression_method}_roundtrip val_loss:{q_val_loss:.4f} val_bpb:{q_val_bpb:.4f} eval:{1000.0*(time.perf_counter()-t_qeval):.0f}ms") + log0(f"final_int6_{compression_method}_roundtrip_exact val_bpb:{q_val_bpb:.8f}") + + if distributed: + dist.destroy_process_group() + +if __name__ == "__main__": + main() \ No newline at end of file