feat(gate-arch-370m-011): pre-flight tokenizer↔model vocab parity gate (task #128)#961
Merged
Merged
Conversation
Task #128. First real MODEL-2 from-scratch compute dispatch (post PR #949 canonical rebuild at commit 29607ed, 2026-04-21) reached drive_real and immediately fired `Embedding::forward token_id 50241 >= vocab_size 50000. N-09 OOB escape.` — provably garbage gradients on 257 out-of-range tokens. Root cause decomposes 3 ways: D1: tokenizer artifact at vocab=50_257 (GPT-2 standard: 50_000 merges + 256 bytes + 1 <|endoftext|>) violates BOTH tokenizer-bpe-v1 and llama-370m-sovereign-v1 contracts (both pin 50_000 exact) D2: Llama370MConfig::VOCAB_SIZE is a hardcoded const — `--vocab-size` CLI flag is silently ignored by the model-side config path D3: no pre-flight gate caught the mismatch → N-09 OOB escape guard masked garbage tokens as escape id, producing silent data corruption This PR ships the orthogonal D3 fix only. D1/D2 require an A/B decision (bump both contracts to 50_257, OR retrain tokenizer to 50_000) that is deferred to a follow-up. The pre-flight gate falsifies any future mismatch regardless of which number wins. Contract-first sequence: 1. contracts/model-families/llama-370m-sovereign-v1.yaml v1.3.0 → v1.4.0: new GATE-ARCH-370M-011 + FALSIFY-ARCH-370M-011, ship_blocking, binds_to AC-SHIP2-003. `pv validate` → 0 errors. 2. aprender-train: pure `assert_tokenizer_vocab_matches_model` helper (algorithm-level proof, no I/O) + `falsify_gate_arch_370m_011_*` test covering match, mismatch (50_257 vs 50_000), and edge cases. 3. apr-cli: `preflight_tokenizer_vocab_matches_model` wrapper reads <tokenizer_dir>/vocab.json, counts entries, delegates to pure helper. Called at top of `drive_real` before ShardBatchIter::new — refuses to dispatch a real training step on mismatch. Synthetic drive is intentionally exempt (never touches the real model). 4. Tests: 3 new CLI tests (accept match, reject 50257 vs 50000, reject missing vocab.json) + fix to existing real_mode_empty_dataset_dir test that now stages a valid vocab.json so the shard-iterator failure path is still reachable. Verification: cargo test -p aprender-train --lib llama_370m → 10 passed cargo test -p apr-cli --lib commands::pretrain → 11 passed pv validate contracts/model-families/llama-370m-sovereign-v1.yaml → valid Unblocks task #126 (re-dispatch) pending D1/D2 A/B decision. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
5 tasks
noahgift
added a commit
that referenced
this pull request
Apr 21, 2026
#131) (#984) Closes the 3-way parity defect surfaced by task #126 at commit 29607ed. The MODEL-2 tokenizer artifact (trained in task #118) has 50_257 entries per GPT-2 industry convention (50_000 BPE merges + 256 byte-level fallback tokens + 1 sentinel). Both pre-existing contracts pinned 50_000 exactly, and Llama370MConfig::VOCAB_SIZE was hardcoded 50_000 — a mismatch that GATE-ARCH-370M-011 (PR #961) now catches pre-flight but previously let through silently via the N-09 OOB escape guard in Embedding::forward. Option A (this PR) honors the real-world artifact and burns no tokenizer re-training compute. Option B (retrain tokenizer at 50_000) was rejected as muda. Changes are byte-in-lockstep across three artifacts: - contracts/tokenizer-bpe-v1.yaml 1.1.0 → 1.2.0: vocab_size 50000 → 50257 merge_rules_count_expected 49996 → 49997 INV-BPE-004 / GATE-BPE-004 window [49992, 50000] → [49993, 50001] - contracts/model-families/llama-370m-sovereign-v1.yaml 1.4.0 → 1.5.0: architecture.vocab_size 50000 → 50257 INV-ARCH-370M-006 desc + falsifier updated INV-ARCH-370M-009 shape example [50000, 1024] → [50257, 1024] - crates/aprender-train/src/models/llama_370m.rs: pub const VOCAB_SIZE: usize = 50_000 → 50_257 validate() const-fn assert updated falsify_gate_arch_370m_011_helper_rejects_mismatch counter-example switched to VOCAB_SIZE - 1 so the test exercises a real mismatch - crates/apr-cli/src/commands/pretrain.rs: preflight_rejects_tokenizer_vocab_mismatch counter-example updated in lockstep; uses Llama370MConfig::VOCAB_SIZE.to_string() assertions so the test no longer rots on future vocab changes - crates/apr-cli/src/tokenize_commands.rs: `apr tokenize train --vocab-size` default 50000 → 50257 (GPT-2 canon) Param count: INV-ARCH-370M-001 band [366M, 374M] still holds — delta is (257 × 1024) = 263_168 = 0.07%. Validation: - `pv validate` both contracts: 0 errors - `cargo test -p aprender-train --lib models::llama_370m`: 9/9 pass - `cargo test -p apr-cli --lib commands::pretrain`: 11/11 pass - `cargo fmt --check` (touched files): clean Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
GATE-ARCH-370M-011that refusesapr pretrainreal-compute dispatch whentokenizer.vocab_size != Llama370MConfig::VOCAB_SIZE.Embedding::forward token_id 50241 >= vocab_size 50000. N-09 OOB escape.on step 1 — silently corrupts gradients via the N-09 escape path.VOCAB_SIZEconst, no pre-flight gate). This PR fixes only the gate (D3). D1/D2 need an A/B decision and will follow.Changes
contracts/model-families/llama-370m-sovereign-v1.yamlv1.3.0 → v1.4.0: newGATE-ARCH-370M-011+FALSIFY-ARCH-370M-011,ship_blocking: true,binds_to: AC-SHIP2-003.pv validate→ 0 errors.crates/aprender-train/src/models/llama_370m.rs: pure algorithm-level helperassert_tokenizer_vocab_matches_model+falsify_gate_arch_370m_011_helper_rejects_mismatchtest covering match, 50_257 vs 50_000 mismatch, and edge cases.crates/apr-cli/src/commands/pretrain.rs: CLI wrapperpreflight_tokenizer_vocab_matches_modelreads<tokenizer_dir>/vocab.json, delegates to pure helper. Called at top ofdrive_realbeforeShardBatchIter::new. Synthetic drive is intentionally exempt (never touches the real model). 3 new tests + 1 fixed test (staged valid vocab.json so the intended shard-iterator failure path is still reachable).Verification
cargo test -p aprender-train --lib llama_370m→ 10 passedcargo test -p apr-cli --lib commands::pretrain→ 11 passed (4 new)cargo run -p aprender-contracts-cli --bin pv -- validate contracts/model-families/llama-370m-sovereign-v1.yaml→ validcargo clippy -p aprender-train -p apr-cli --lib -- -D warnings→ cleancargo fmt -- --checkon affected files → cleanTest plan
ci / gatepassesworkspace-testpassesapr pretrain --mode from-scratchwith mismatched tokenizer → verify refusal message cites GATE-ARCH-370M-011 and both vocab sizesUnblocks
🤖 Generated with Claude Code