perf(fast): batch ingestion in Rust with the HF tokenizers crate#65
Merged
Conversation
One boundary crossing: trainer_from_texts (fast/src/ingest.rs) takes the raw text list and does pretokenization, unique-word dedup, graph building, and Trainer construction in Rust with the GIL released. Pretokenization uses the tokenizers crate (onig feature — the same engine the Python package wraps) with GPT_PATTERN mirrored verbatim, so splits are identical by construction: every digest at every scale is unchanged. No vendored code. The shim gates the fast path on the default configuration (utf8_clusters, default GPT pretokenizer, no registered script handlers); anything else takes the unchanged per-document path. Pre-existing merge replay composes with apply_merges (#61) and the lossless codec (#63) unchanged. 10x benchmark (wikitext-103 slice: 237,721 docs / 108.9M chars, 500 merges, cold subprocess per case): - BPE: 16.5s -> 5.2s (3.2x) — at parity with HuggingFace (5.6s) - BNE n=4: 17.8s -> 6.5s (2.7x) - Boundless: 46.4s -> 31.2s (1.5x, now loop-dominated) - SuperBPE: 52.7s -> 22.2s (2.4x) Full-scale 1x also collapses: BPE 2.0->0.85s, BNE 2.4->1.2s, Boundless 5.0->3.5s, SuperBPE 5.7->2.8s. Build +4.9s (oniguruma C build), .so +0.65MB. Co-Authored-By: Claude Fable 5 <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.
Option 3 from the 10× analysis: #55's one-boundary-crossing ingestion, re-fit to current main (post #57–#63), with pretokenization done by the HF
tokenizerscrate — the exact library the Python side wraps (onigfeature, matching the PyPI wheel's engine),GPT_PATTERNmirrored verbatim. Splits are identical by construction, and every digest at every scale confirms it: zero divergence, no vendored code.Why
At 10× scale, fast BPE was 17.9s vs HF's ~6s — but 13.1s of it was Python-side pretokenization + object crossing, with the actual Rust training loop at 3.6s (already faster than HF end-to-end).
trainer_from_texts(fast/src/ingest.rs, 160 lines) takes the raw text list once: pretokenize (rayon over docs) → unique-word dedup → graph build →Trainer, GIL released. The shim gates it on the default configuration; custom pretokenizers/units/script handlers take the unchanged fallback (covered by existing tests).10× benchmark (wikitext-103 slice: 237,721 docs / 108.9M chars, 500 merges; cold subprocess per case; independently re-verified)
Setup collapsed 13.1s → 1.7s (remaining: the 109MB corpus copy across the boundary + in-Rust pretokenize/build). 1×-scale full benchmark also collapses: BPE 2.0→0.85s, SuperBPE 5.7→2.8s. Default 2000-row: BPE 0.094s / SuperBPE 0.202s.
Verification
20d930a435/6598b73bb3/9999e69912×2), full-scale/500 post-fix: lossless merge strings via UTF-8 with byte escapes #63 canon (f8daaf401b/bd30dd6919/22d80e8f61/15916b03c7), full/5000 BPE spot-check, and all four 10× digests unchanged from the pre-change baselines.-p conftest+ root); fallback tests pass; clippy unchanged.tokenizers 0.22(onig) — build +4.9s,.so+0.65MB.Relation to #55
Supersedes the parked #55 for the ingestion structure. #55's only remaining delta is swapping the pretokenizer for gigatoken's SIMD one (72ms → 5ms at 1× — worth ~1.5s at 10×) once upstream (marcelroed/gigatoken#34) makes it consumable as a dependency; that becomes a small isolated follow-up.
🤖 Generated with Claude Code