Skip to content

feat(bpe): ScratchPool pattern + BPE word cache#2223

Open
SBrandeis wants to merge 11 commits into
feat/train_encode_splitfrom
feat/bpe-cache
Open

feat(bpe): ScratchPool pattern + BPE word cache#2223
SBrandeis wants to merge 11 commits into
feat/train_encode_splitfrom
feat/bpe-cache

Conversation

@SBrandeis

@SBrandeis SBrandeis commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

PipelineTokenizer benchmark

7 / 8 models supported — PipelineTokenizer vs tokenizers v0.23.1 (latest release) · ~10 kB inputs · single thread + 1/2/4/8/max-thread sweep

51d2ab06f · 2026-07-22 08:29 UTC · Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz · 48 cores

Per-model encode throughput vs latest release

vs base branch (575582072) — per-model geomean ×speedup of this PR's PipelineTokenizer against the base branch's; regressions in red.

Per-model encode throughput vs base branch

Per-model memory footprint

Minimal encode binary size

bert-base-uncased — normalizer-heavy WordPiece · ×4.95 vs v0.23.1 · ×1.01 vs base bert-base-uncased speedup bert-base-uncased stage decomposition bert-base-uncased thread scaling

Memory (RSS MB, load+encode): v0.23.1 12+0 (peak 12) · Pipeline 8+0 (peak 17)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model Ids
amh_Ethi lang 7.8 27.6 ×3.56 ×1.01 3% (1.2) 76% (27.3) 13% (4.7) 7% (2.5) match
arb_Arab lang 4.2 25.1 ×5.92 ×1.01 3% (1.2) 69% (27.4) 8% (3.3) 19% (7.7) match
ben_Beng lang 6.0 35.0 ×5.82 ×1.01 4% (1.2) 67% (19.0) 10% (2.9) 18% (5.1) match
cmn_Hani lang 3.9 19.0 ×4.91 ×1.01 2% (1.2) 71% (37.3) 11% (5.7) 16% (8.2) match
ell_Grek lang 3.8 23.8 ×6.30 ×1.01 3% (1.2) 69% (28.5) 8% (3.4) 20% (8.4) match
eng_Latn lang 4.5 18.4 ×4.10 ×1.02 5% (2.5) 71% (38.4) 8% (4.5) 16% (8.5) match
heb_Hebr lang 4.2 20.0 ×4.73 ×1.01 2% (1.2) 75% (37.4) 7% (3.6) 15% (7.4) match
hin_Deva lang 6.5 27.6 ×4.23 ×1.01 3% (1.2) 75% (26.9) 9% (3.2) 13% (4.7) match
jpn_Jpan lang 4.3 27.6 ×6.44 ×1.01 3% (1.2) 65% (23.2) 13% (4.6) 19% (7.0) match
kat_Geor lang 6.2 28.3 ×4.57 ×1.00 3% (1.2) 75% (26.4) 9% (3.1) 13% (4.4) match
kor_Hang lang 2.4 17.8 ×7.30 ×1.01 2% (1.2) 63% (35.0) 13% (7.3) 22% (12.5) match
rus_Cyrl lang 3.7 23.9 ×6.48 ×1.01 3% (1.2) 66% (27.3) 8% (3.2) 24% (9.9) match
tam_Taml lang 7.0 38.7 ×5.49 ×1.01 5% (1.2) 72% (18.5) 10% (2.4) 14% (3.5) match
tha_Thai lang 8.4 33.1 ×3.96 ×0.99 4% (1.2) 84% (24.9) 7% (2.2) 5% (1.5) match
added_normalized_dense modalities 6.8 19.7 ×2.91 ×1.00 3% (1.3) 81% (40.5) 15% (7.3) 1% (0.7) match
added_normalized_sparse modalities 5.3 18.5 ×3.49 ×1.02 4% (1.9) 74% (39.7) 13% (7.2) 9% (4.6) match
added_special_dense modalities 5.3 38.3 ×7.20 ×0.98 21% (5.3) 36% (9.0) 36% (9.0) 7% (1.6) match
added_special_sparse modalities 3.9 21.5 ×5.46 ×1.01 8% (3.6) 61% (28.0) 18% (8.5) 13% (5.9) match
agentic-traces modalities 3.8 18.2 ×4.77 ×1.02 4% (2.3) 70% (38.3) 9% (4.8) 16% (8.9) match
agentic_swe modalities 4.1 19.5 ×4.75 ×1.01 4% (1.9) 76% (38.5) 7% (3.6) 13% (6.8) match
code_mixed modalities 3.9 19.0 ×4.87 ×1.01 4% (2.1) 74% (38.3) 8% (4.2) 14% (7.3) match
math_latex modalities 4.0 18.3 ×4.57 ×1.02 5% (2.5) 71% (38.3) 9% (4.7) 16% (8.7) match
deepseek-v4 — deepseek 3-regex split-heavy byte-level BPE · ×22.28 vs v0.23.1 · ×5.45 vs base deepseek-v4 speedup deepseek-v4 stage decomposition deepseek-v4 thread scaling

Memory (RSS MB, load+encode): v0.23.1 62+0 (peak 68) · Pipeline 96+0 (peak 96)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model Ids
amh_Ethi lang 4.4 91.4 ×20.78 ×2.73 7% (0.7) 0% (0.0) 50% (4.8) 43% (4.2) match
arb_Arab lang 4.4 98.6 ×22.59 ×5.85 7% (0.6) 0% (0.0) 39% (3.5) 55% (4.9) match
ben_Beng lang 6.4 138.6 ×21.74 ×7.58 10% (0.6) 0% (0.0) 51% (3.2) 40% (2.5) match
cmn_Hani lang 3.7 81.5 ×22.21 ×5.18 8% (0.8) 0% (0.0) 31% (3.2) 62% (6.3) match
ell_Grek lang 4.6 109.1 ×23.78 ×6.22 8% (0.6) 0% (0.0) 45% (3.5) 47% (3.7) match
eng_Latn lang 3.1 72.1 ×23.51 ×5.85 16% (2.0) 0% (0.0) 40% (5.1) 44% (5.6) match
heb_Hebr lang 3.8 92.9 ×24.51 ×7.30 6% (0.6) 0% (0.0) 39% (3.6) 55% (5.1) match
hin_Deva lang 6.0 131.4 ×21.87 ×6.10 9% (0.6) 0% (0.0) 50% (3.4) 42% (2.8) match
jpn_Jpan lang 4.2 111.8 ×26.70 ×6.17 9% (0.7) 0% (0.0) 42% (3.0) 48% (3.4) match
kat_Geor lang 6.1 134.7 ×22.26 ×7.55 10% (0.6) 0% (0.0) 48% (3.0) 42% (2.6) match
kor_Hang lang 3.4 71.0 ×20.67 ×3.63 5% (0.6) 0% (0.0) 30% (3.7) 65% (8.0) match
rus_Cyrl lang 4.3 100.3 ×23.40 ×7.03 7% (0.6) 0% (0.0) 40% (3.4) 53% (4.4) match
tam_Taml lang 6.4 147.0 ×23.05 ×8.29 10% (0.6) 0% (0.0) 48% (2.8) 42% (2.4) match
tha_Thai lang 6.9 91.2 ×13.19 ×6.19 6% (0.6) 0% (0.0) 24% (2.3) 70% (6.9) match
added_normalized_dense modalities 6.2 162.2 ×26.29 ×7.00 13% (0.7) 0% (0.0) 50% (2.8) 37% (2.1) match
added_normalized_sparse modalities 5.5 118.0 ×21.41 ×6.16 16% (1.3) 0% (0.0) 46% (3.7) 38% (3.1) match
added_special_dense modalities 3.7 62.8 ×16.84 ×1.79 42% (6.5) 3% (0.5) 37% (5.8) 18% (2.7) match
added_special_sparse modalities 4.1 67.3 ×16.43 ×3.45 28% (3.9) 1% (0.1) 49% (6.7) 22% (3.1) match
agentic-traces modalities 2.8 69.4 ×24.68 ×5.03 13% (1.8) 0% (0.0) 40% (5.4) 47% (6.2) match
agentic_swe modalities 2.9 94.8 ×32.26 ×6.47 13% (1.3) 0% (0.0) 40% (3.8) 47% (4.5) match
code_mixed modalities 3.4 84.9 ×24.92 ×5.61 14% (1.6) 0% (0.0) 42% (4.6) 44% (4.8) match
math_latex modalities 2.9 71.2 ×24.66 ×5.37 15% (1.9) 0% (0.0) 41% (5.3) 44% (5.7) match

Pre-tokenize: classify + fsm vs regex engines — ns/byte, lower better. The fsm is the scalar jump-table in both pipe columns; SIMD / scalar is the classify pass (regex pre-tokenizers have no SIMD fsm). ×vs = engine ÷ our pipeline (SIMD / scalar classify); onig & pcre2 (JIT) are C, fancy is pure-Rust fancy-regex, logos is a compile-time DFA lexer (approximate grammar; n/a for deepseek).

Fixture classify SIMD classify scalar pipe (SIMD cls + fsm) pipe (scalar cls + fsm) onig fancy pcre2 logos ×vs onig ×vs fancy ×vs pcre2 ×vs logos
amh_Ethi 2.63 3.40 4.76 5.53 42.0 19.9 9.1 8.8× / 7.6× 4.2× / 3.6× 1.9× / 1.6×
arb_Arab 1.00 3.11 3.46 5.57 50.3 22.6 10.2 14.5× / 9.0× 6.5× / 4.1× 2.9× / 1.8×
ben_Beng 1.46 2.91 3.17 4.62 36.2 16.0 7.5 11.4× / 7.8× 5.0× / 3.5× 2.4× / 1.6×
cmn_Hani 1.13 2.27 3.16 4.30 60.3 34.6 14.5 19.1× / 14.0× 10.9× / 8.0× 4.6× / 3.4×
ell_Grek 0.58 3.05 3.49 5.96 48.0 20.8 9.6 13.8× / 8.1× 6.0× / 3.5× 2.7× / 1.6×
eng_Latn 0.11 1.45 5.07 6.41 65.7 39.4 16.2 13.0× / 10.3× 7.8× / 6.1× 3.2× / 2.5×
heb_Hebr 1.00 3.23 3.63 5.86 51.8 23.8 10.7 14.3× / 8.8× 6.6× / 4.1× 2.9× / 1.8×
hin_Deva 1.36 3.34 3.37 5.35 38.5 18.3 8.5 11.4× / 7.2× 5.4× / 3.4× 2.5× / 1.6×
jpn_Jpan 1.56 3.35 2.98 4.77 52.8 27.4 11.8 17.7× / 11.1× 9.2× / 5.8× 4.0× / 2.5×
kat_Geor 1.39 2.55 2.99 4.15 32.6 15.1 7.1 10.9× / 7.9× 5.1× / 3.6× 2.4× / 1.7×
kor_Hang 1.08 2.75 3.73 5.41 50.3 27.0 11.8 13.5× / 9.3× 7.2× / 5.0× 3.2× / 2.2×
rus_Cyrl 1.02 2.96 3.35 5.29 48.2 21.3 9.5 14.4× / 9.1× 6.3× / 4.0× 2.8× / 1.8×
tam_Taml 0.92 2.94 2.76 4.77 32.1 13.5 6.6 11.6× / 6.7× 4.9× / 2.8× 2.4× / 1.4×
tha_Thai 1.36 2.57 2.32 3.53 27.5 10.0 5.2 11.9× / 7.8× 4.3× / 2.8× 2.2× / 1.5×
added_normalized_dense 0.06 1.47 2.81 4.22 43.2 19.9 9.0 15.4× / 10.2× 7.1× / 4.7× 3.2× / 2.1×
added_normalized_sparse 0.06 1.48 3.70 5.12 49.2 26.1 11.2 13.3× / 9.6× 7.0× / 5.1× 3.0× / 2.2×
added_special_dense 0.06 1.47 5.79 7.21 170.9 96.3 37.6 29.5× / 23.7× 16.6× / 13.4× 6.5× / 5.2×
added_special_sparse 0.06 1.47 6.71 8.13 101.0 57.4 23.9 15.0× / 12.4× 8.5× / 7.1× 3.6× / 2.9×
agentic-traces 0.58 1.47 5.38 6.27 80.5 53.1 20.1 15.0× / 12.8× 9.9× / 8.5× 3.7× / 3.2×
agentic_swe 0.53 1.45 3.83 4.75 95.3 64.6 23.6 24.9× / 20.1× 16.9× / 13.6× 6.2× / 5.0×
code_mixed 0.08 1.44 4.62 5.98 74.6 53.6 18.7 16.1× / 12.5× 11.6× / 9.0× 4.0× / 3.1×
math_latex 0.58 1.48 5.32 6.23 78.3 48.1 19.3 14.7× / 12.6× 9.0× / 7.7× 3.6× / 3.1×
gpt2 — gpt2 ByteLevel regex · ×26.95 vs v0.23.1 · ×3.67 vs base gpt2 speedup gpt2 stage decomposition gpt2 thread scaling

Memory (RSS MB, load+encode): v0.23.1 25+2 (peak 27) · Pipeline 30+0 (peak 29)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model Ids
amh_Ethi lang 4.4 104.2 ×23.73 ×2.21 8% (0.7) 0% (0.0) 44% (3.6) 48% (3.9) match
arb_Arab lang 4.3 110.6 ×26.02 ×4.22 8% (0.6) 0% (0.0) 29% (2.2) 63% (4.9) match
ben_Beng lang 3.4 102.7 ×30.59 ×2.24 7% (0.6) 0% (0.0) 33% (3.0) 61% (5.5) match
cmn_Hani lang 4.0 104.9 ×26.12 ×3.62 8% (0.6) 0% (0.0) 30% (2.3) 63% (4.9) match
ell_Grek lang 4.6 124.7 ×27.23 ×4.24 9% (0.6) 0% (0.0) 31% (2.1) 60% (4.1) match
eng_Latn lang 3.7 86.1 ×22.97 ×6.27 19% (2.0) 0% (0.0) 29% (3.0) 52% (5.5) match
heb_Hebr lang 4.1 110.3 ×26.99 ×3.76 8% (0.6) 0% (0.0) 29% (2.3) 63% (5.0) match
hin_Deva lang 3.3 105.8 ×31.77 ×2.71 7% (0.6) 1% (0.1) 33% (3.0) 59% (5.3) match
jpn_Jpan lang 4.6 133.7 ×29.00 ×5.95 10% (0.6) 0% (0.0) 35% (2.1) 55% (3.4) match
kat_Geor lang 4.8 156.0 ×32.23 ×2.26 12% (0.6) 0% (0.0) 40% (2.1) 48% (2.5) match
kor_Hang lang 3.5 90.3 ×26.12 ×2.05 6% (0.6) 0% (0.0) 25% (2.4) 68% (6.5) match
rus_Cyrl lang 4.4 119.1 ×27.36 ×4.29 8% (0.6) 0% (0.0) 29% (2.1) 62% (4.5) match
tam_Taml lang 2.9 114.5 ×39.73 ×1.75 7% (0.6) 0% (0.0) 33% (2.7) 60% (4.9) match
tha_Thai lang 3.7 115.2 ×31.02 ×3.19 8% (0.6) 0% (0.0) 34% (2.5) 58% (4.2) match
added_normalized_dense modalities 6.2 207.0 ×33.32 ×8.04 17% (0.7) 0% (0.0) 25% (1.1) 58% (2.5) match
added_normalized_sparse modalities 5.5 146.8 ×26.92 ×6.79 20% (1.2) 0% (0.0) 34% (2.1) 47% (2.9) match
added_special_dense modalities 4.4 82.2 ×18.79 ×1.79 46% (4.9) 3% (0.3) 35% (3.7) 16% (1.8) match
added_special_sparse modalities 4.6 83.5 ×18.31 ×3.60 30% (3.1) 0% (0.0) 41% (4.4) 29% (3.1) match
agentic-traces modalities 3.2 77.9 ×24.06 ×4.88 15% (1.8) 0% (0.0) 29% (3.5) 56% (6.7) match
agentic_swe modalities 3.5 102.3 ×29.04 ×4.25 14% (1.3) 0% (0.0) 27% (2.4) 59% (5.2) match
code_mixed modalities 3.7 95.2 ×25.75 ×4.75 16% (1.5) 0% (0.0) 29% (2.9) 55% (5.5) match
math_latex modalities 3.4 82.6 ×24.54 ×5.52 17% (1.9) 0% (0.0) 30% (3.3) 54% (6.0) match

Pre-tokenize: classify + fsm vs regex engines — ns/byte, lower better. The fsm is the scalar jump-table in both pipe columns; SIMD / scalar is the classify pass (regex pre-tokenizers have no SIMD fsm). ×vs = engine ÷ our pipeline (SIMD / scalar classify); onig & pcre2 (JIT) are C, fancy is pure-Rust fancy-regex, logos is a compile-time DFA lexer (approximate grammar; n/a for deepseek).

Fixture classify SIMD classify scalar pipe (SIMD cls + fsm) pipe (scalar cls + fsm) onig fancy pcre2 logos ×vs onig ×vs fancy ×vs pcre2 ×vs logos
amh_Ethi 2.62 3.43 3.63 4.44 24.8 21.1 5.8 4.7 6.8× / 5.6× 5.8× / 4.8× 1.6× / 1.3× 1.3× / 1.1×
arb_Arab 1.00 2.98 2.24 4.22 29.1 26.4 6.8 5.1 13.0× / 6.9× 11.8× / 6.2× 3.0× / 1.6× 2.3× / 1.2×
ben_Beng 1.47 2.97 3.01 4.51 59.7 56.6 14.0 4.1 19.9× / 13.2× 18.8× / 12.6× 4.7× / 3.1× 1.4× / 0.9×
cmn_Hani 1.13 2.34 2.32 3.53 24.2 21.4 5.9 2.4 10.5× / 6.9× 9.2× / 6.1× 2.6× / 1.7× 1.0× / 0.7×
ell_Grek 0.58 3.05 2.14 4.61 25.9 22.1 6.0 4.7 12.1× / 5.6× 10.3× / 4.8× 2.8× / 1.3× 2.2× / 1.0×
eng_Latn 0.11 1.45 3.02 4.36 39.6 43.0 12.1 3.7 13.1× / 9.1× 14.2× / 9.8× 4.0× / 2.8× 1.2× / 0.9×
heb_Hebr 1.00 3.08 2.26 4.34 28.7 26.3 6.9 2.9 12.7× / 6.6× 11.7× / 6.1× 3.1× / 1.6× 1.3× / 0.7×
hin_Deva 1.37 3.12 2.98 4.74 55.0 56.0 13.5 4.2 18.4× / 11.6× 18.8× / 11.8× 4.5× / 2.8× 1.4× / 0.9×
jpn_Jpan 1.56 3.33 2.13 3.90 21.8 17.9 5.1 3.8 10.3× / 5.6× 8.4× / 4.6× 2.4× / 1.3× 1.8× / 1.0×
kat_Geor 1.39 2.54 2.06 3.20 15.9 13.9 4.1 2.0 7.7× / 5.0× 6.8× / 4.4× 2.0× / 1.3× 1.0× / 0.6×
kor_Hang 1.08 2.77 2.43 4.11 28.4 28.4 7.4 3.7 11.7× / 6.9× 11.7× / 6.9× 3.1× / 1.8× 1.5× / 0.9×
rus_Cyrl 1.02 2.95 2.11 4.04 24.6 21.6 5.8 2.5 11.7× / 6.1× 10.2× / 5.3× 2.8× / 1.4× 1.2× / 0.6×
tam_Taml 0.92 2.94 2.68 4.70 62.9 60.1 14.2 3.8 23.5× / 13.4× 22.4× / 12.8× 5.3× / 3.0× 1.4× / 0.8×
tha_Thai 1.36 2.52 2.49 3.65 36.7 32.5 8.8 3.2 14.8× / 10.1× 13.1× / 8.9× 3.5× / 2.4× 1.3× / 0.9×
added_normalized_dense 0.06 1.47 1.05 2.47 21.2 23.2 6.6 1.9 20.1× / 8.6× 22.1× / 9.4× 6.2× / 2.7× 1.8× / 0.8×
added_normalized_sparse 0.06 1.48 2.06 3.47 27.8 31.3 8.7 2.6 13.5× / 8.0× 15.2× / 9.0× 4.3× / 2.5× 1.3× / 0.8×
added_special_dense 0.06 1.48 3.74 5.15 85.1 98.9 20.2 2.9 22.8× / 16.5× 26.5× / 19.2× 5.4× / 3.9× 0.8× / 0.6×
added_special_sparse 0.07 1.47 4.35 5.75 55.0 63.4 14.8 3.4 12.6× / 9.6× 14.6× / 11.0× 3.4× / 2.6× 0.8× / 0.6×
agentic-traces 0.59 1.48 3.50 4.39 52.4 62.2 15.7 4.7 15.0× / 11.9× 17.8× / 14.2× 4.5× / 3.6× 1.3× / 1.1×
agentic_swe 0.51 1.46 2.40 3.35 53.7 69.7 14.8 3.6 22.4× / 16.0× 29.1× / 20.8× 6.2× / 4.4× 1.5× / 1.1×
code_mixed 0.07 1.45 2.91 4.30 52.7 67.9 15.5 4.0 18.1× / 12.3× 23.3× / 15.8× 5.3× / 3.6× 1.4× / 0.9×
math_latex 0.69 1.48 3.28 4.08 46.9 52.1 14.4 4.1 14.3× / 11.5× 15.9× / 12.8× 4.4× / 3.5× 1.3× / 1.0×
gpt-oss — o200k-regex byte-level BPE (gpt-oss) · ×24.52 vs v0.23.1 · ×2.97 vs base gpt-oss speedup gpt-oss stage decomposition gpt-oss thread scaling

Memory (RSS MB, load+encode): v0.23.1 4+11 (peak 16) · Pipeline 7+2 (peak 7)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model Ids
amh_Ethi lang 3.1 84.1 ×26.94 ×1.35 7% (0.7) 0% (0.0) 48% (4.8) 46% (4.6) match
arb_Arab lang 3.4 92.2 ×26.96 ×3.61 6% (0.6) 0% (0.0) 35% (3.3) 59% (5.6) match
ben_Beng lang 4.7 114.3 ×24.14 ×4.23 8% (0.6) 0% (0.0) 49% (3.7) 43% (3.3) match
cmn_Hani lang 3.8 96.1 ×25.33 ×2.49 7% (0.6) 0% (0.0) 41% (3.5) 52% (4.5) match
ell_Grek lang 3.4 99.2 ×29.20 ×3.07 7% (0.6) 0% (0.0) 37% (3.2) 56% (4.7) match
eng_Latn lang 2.6 64.8 ×25.31 ×3.07 14% (2.0) 0% (0.0) 33% (4.6) 53% (7.2) match
heb_Hebr lang 3.3 90.0 ×27.17 ×3.20 6% (0.6) 0% (0.0) 35% (3.4) 59% (5.7) match
hin_Deva lang 4.7 110.1 ×23.46 ×4.68 8% (0.6) 0% (0.0) 47% (3.8) 45% (3.6) match
jpn_Jpan lang 4.5 115.5 ×25.83 ×3.06 9% (0.6) 0% (0.0) 46% (3.3) 45% (3.2) match
kat_Geor lang 5.2 128.3 ×24.81 ×5.08 9% (0.6) 0% (0.0) 43% (2.8) 48% (3.1) match
kor_Hang lang 3.2 75.6 ×23.45 ×1.66 5% (0.6) 0% (0.0) 33% (3.7) 62% (7.2) match
rus_Cyrl lang 4.1 100.3 ×24.44 ×4.45 7% (0.6) 0% (0.0) 35% (3.1) 58% (5.1) match
tam_Taml lang 5.2 125.6 ×24.14 ×4.34 9% (0.6) 0% (0.0) 48% (3.2) 43% (2.9) match
tha_Thai lang 5.8 105.7 ×18.38 ×3.74 7% (0.6) 0% (0.0) 39% (3.2) 54% (4.5) match
added_normalized_dense modalities 4.1 143.6 ×35.00 ×3.11 12% (0.7) 0% (0.0) 35% (2.2) 54% (3.3) match
added_normalized_sparse modalities 3.7 101.8 ×27.28 ×2.79 15% (1.3) 0% (0.0) 35% (3.1) 51% (4.5) match
added_special_dense modalities 3.2 74.6 ×23.25 ×1.36 41% (5.1) 0% (0.0) 40% (5.0) 18% (2.3) match
added_special_sparse modalities 3.3 66.3 ×19.99 ×1.94 22% (3.1) 0% (0.0) 43% (5.9) 35% (4.8) match
agentic-traces modalities 3.0 63.4 ×21.45 ×2.83 12% (1.8) 0% (0.0) 34% (5.0) 54% (8.0) match
agentic_swe modalities 3.4 82.6 ×24.29 ×3.84 12% (1.3) 0% (0.0) 34% (3.7) 55% (6.0) match
code_mixed modalities 3.4 76.2 ×22.70 ×2.58 13% (1.5) 0% (0.0) 36% (4.4) 51% (6.3) match
math_latex modalities 3.1 63.7 ×20.76 ×2.87 13% (1.9) 0% (0.0) 34% (4.9) 53% (7.6) match

Pre-tokenize: classify + fsm vs regex engines — ns/byte, lower better. The fsm is the scalar jump-table in both pipe columns; SIMD / scalar is the classify pass (regex pre-tokenizers have no SIMD fsm). ×vs = engine ÷ our pipeline (SIMD / scalar classify); onig & pcre2 (JIT) are C, fancy is pure-Rust fancy-regex, logos is a compile-time DFA lexer (approximate grammar; n/a for deepseek).

Fixture classify SIMD classify scalar pipe (SIMD cls + fsm) pipe (scalar cls + fsm) onig fancy pcre2 logos ×vs onig ×vs fancy ×vs pcre2 ×vs logos
amh_Ethi 2.68 3.54 4.78 5.64 30.2 14.1 7.0 4.7 6.3× / 5.4× 2.9× / 2.5× 1.5× / 1.2× 1.0× / 0.8×
arb_Arab 1.00 3.03 3.29 5.33 34.6 16.2 7.6 5.0 10.5× / 6.5× 4.9× / 3.0× 2.3× / 1.4× 1.5× / 0.9×
ben_Beng 1.47 2.92 3.67 5.12 24.2 11.0 5.4 2.8 6.6× / 4.7× 3.0× / 2.1× 1.5× / 1.1× 0.8× / 0.5×
cmn_Hani 1.12 2.29 3.52 4.68 21.8 10.9 5.5 2.5 6.2× / 4.7× 3.1× / 2.3× 1.6× / 1.2× 0.7× / 0.5×
ell_Grek 0.58 3.04 3.16 5.62 30.6 15.2 6.9 5.0 9.7× / 5.5× 4.8× / 2.7× 2.2× / 1.2× 1.6× / 0.9×
eng_Latn 0.10 1.46 4.57 5.93 44.6 29.2 13.5 4.0 9.8× / 7.5× 6.4× / 4.9× 3.0× / 2.3× 0.9× / 0.7×
heb_Hebr 1.00 3.18 3.37 5.56 34.0 17.3 8.0 2.8 10.1× / 6.1× 5.1× / 3.1× 2.4× / 1.4× 0.8× / 0.5×
hin_Deva 1.37 3.16 3.79 5.58 26.3 12.9 6.4 3.1 6.9× / 4.7× 3.4× / 2.3× 1.7× / 1.1× 0.8× / 0.6×
jpn_Jpan 1.56 3.51 3.29 5.24 20.3 9.3 4.8 3.8 6.2× / 3.9× 2.8× / 1.8× 1.5× / 0.9× 1.2× / 0.7×
kat_Geor 1.38 2.58 2.81 4.01 19.0 10.1 4.7 2.2 6.8× / 4.7× 3.6× / 2.5× 1.7× / 1.2× 0.8× / 0.5×
kor_Hang 1.07 2.77 3.74 5.44 33.7 18.6 8.8 3.6 9.0× / 6.2× 5.0× / 3.4× 2.3× / 1.6× 1.0× / 0.7×
rus_Cyrl 1.02 3.10 3.09 5.18 29.4 15.1 6.7 4.8 9.5× / 5.7× 4.9× / 2.9× 2.2× / 1.3× 1.5× / 0.9×
tam_Taml 0.93 3.01 3.21 5.30 19.2 8.8 4.3 2.9 6.0× / 3.6× 2.7× / 1.7× 1.3× / 0.8× 0.9× / 0.6×
tha_Thai 1.36 2.53 3.23 4.40 13.3 5.7 2.8 2.3 4.1× / 3.0× 1.8× / 1.3× 0.9× / 0.6× 0.7× / 0.5×
added_normalized_dense 0.06 1.48 2.16 3.58 34.3 18.0 11.2 2.2 15.9× / 9.6× 8.3× / 5.0× 5.2× / 3.1× 1.0× / 0.6×
added_normalized_sparse 0.06 1.48 3.07 4.49 36.7 21.4 11.3 2.9 11.9× / 8.2× 7.0× / 4.8× 3.7× / 2.5× 0.9× / 0.6×
added_special_dense 0.06 1.47 5.04 6.46 88.2 68.9 23.6 3.1 17.5× / 13.7× 13.7× / 10.7× 4.7× / 3.6× 0.6× / 0.5×
added_special_sparse 0.06 1.48 5.92 7.34 58.8 40.9 16.5 3.6 9.9× / 8.0× 6.9× / 5.6× 2.8× / 2.2× 0.6× / 0.5×
agentic-traces 0.58 1.50 5.01 5.94 53.8 41.0 17.1 4.8 10.7× / 9.1× 8.2× / 6.9× 3.4× / 2.9× 1.0× / 0.8×
agentic_swe 0.52 1.46 3.72 4.66 54.7 48.0 17.1 3.5 14.7× / 11.7× 12.9× / 10.3× 4.6× / 3.7× 0.9× / 0.8×
code_mixed 0.07 1.47 4.41 5.82 54.2 46.8 16.9 4.2 12.3× / 9.3× 10.6× / 8.0× 3.8× / 2.9× 1.0× / 0.7×
math_latex 0.69 1.53 4.85 5.69 51.5 35.5 15.7 4.4 10.6× / 9.1× 7.3× / 6.2× 3.2× / 2.8× 0.9× / 0.8×
glm-5.2 — cl100k-variant regex byte-level BPE (glm-5.2) · ×23.89 vs v0.23.1 · ×1.91 vs base glm-5.2 speedup glm-5.2 stage decomposition glm-5.2 thread scaling

Memory (RSS MB, load+encode): v0.23.1 4+11 (peak 15) · Pipeline 6+2 (peak 7)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model Ids
amh_Ethi lang 4.6 89.4 ×19.43 ×1.44 13% (1.2) 0% (0.0) 39% (3.7) 48% (4.6) match
arb_Arab lang 4.1 94.2 ×23.03 ×1.33 13% (1.2) 0% (0.0) 26% (2.3) 61% (5.5) match
ben_Beng lang 3.6 95.5 ×26.83 ×1.38 12% (1.2) 0% (0.0) 32% (3.1) 56% (5.4) match
cmn_Hani lang 4.8 110.6 ×23.03 ×1.61 16% (1.2) 0% (0.0) 32% (2.4) 52% (3.8) match
ell_Grek lang 4.2 106.3 ×25.32 ×1.45 14% (1.2) 0% (0.0) 27% (2.2) 58% (4.7) match
eng_Latn lang 3.6 71.4 ×19.77 ×3.15 20% (2.5) 0% (0.0) 24% (3.0) 56% (7.0) match
heb_Hebr lang 3.8 96.7 ×25.29 ×1.47 13% (1.2) 0% (0.0) 26% (2.3) 61% (5.5) match
hin_Deva lang 3.2 92.8 ×29.14 ×1.48 11% (1.2) 0% (0.0) 30% (3.2) 59% (6.2) match
jpn_Jpan lang 4.8 128.3 ×26.92 ×1.73 19% (1.2) 0% (0.0) 34% (2.2) 47% (2.9) match
kat_Geor lang 4.5 131.3 ×29.19 ×1.63 18% (1.2) 0% (0.0) 33% (2.1) 48% (3.0) match
kor_Hang lang 3.5 85.7 ×24.35 ×1.30 12% (1.2) 0% (0.0) 25% (2.5) 63% (6.4) match
rus_Cyrl lang 4.1 102.5 ×25.29 ×2.55 14% (1.2) 0% (0.0) 26% (2.2) 60% (4.9) match
tam_Taml lang 3.3 102.3 ×31.02 ×1.44 13% (1.2) 0% (0.0) 30% (2.7) 57% (5.0) match
tha_Thai lang 3.9 103.9 ×26.42 ×1.42 14% (1.2) 0% (0.0) 30% (2.5) 56% (4.6) match
added_normalized_dense modalities 4.7 160.2 ×34.34 ×3.50 23% (1.3) 0% (0.0) 19% (1.1) 58% (3.3) match
added_normalized_sparse modalities 4.2 113.3 ×26.87 ×2.96 24% (1.9) 0% (0.0) 24% (1.9) 54% (4.3) match
added_special_dense modalities 3.4 50.5 ×14.67 ×1.24 63% (11.9) 1% (0.1) 25% (4.6) 11% (2.1) match
added_special_sparse modalities 3.6 57.7 ×16.19 ×1.71 41% (6.6) 1% (0.1) 30% (4.8) 29% (4.7) match
agentic-traces modalities 3.2 67.7 ×20.90 ×2.87 18% (2.4) 0% (0.0) 27% (3.7) 55% (7.5) match
agentic_swe modalities 3.6 86.0 ×23.67 ×3.90 18% (1.9) 0% (0.0) 27% (2.8) 55% (5.9) match
code_mixed modalities 3.6 81.0 ×22.34 ×2.63 19% (2.2) 0% (0.0) 29% (3.3) 52% (5.9) match
math_latex modalities 3.2 69.1 ×21.38 ×2.89 19% (2.5) 0% (0.0) 26% (3.5) 55% (7.2) match

Pre-tokenize: classify + fsm vs regex engines — ns/byte, lower better. The fsm is the scalar jump-table in both pipe columns; SIMD / scalar is the classify pass (regex pre-tokenizers have no SIMD fsm). ×vs = engine ÷ our pipeline (SIMD / scalar classify); onig & pcre2 (JIT) are C, fancy is pure-Rust fancy-regex, logos is a compile-time DFA lexer (approximate grammar; n/a for deepseek).

Fixture classify SIMD classify scalar pipe (SIMD cls + fsm) pipe (scalar cls + fsm) onig fancy pcre2 logos ×vs onig ×vs fancy ×vs pcre2 ×vs logos
amh_Ethi 2.63 3.41 3.69 4.47 27.4 16.3 5.8 4.8 7.4× / 6.1× 4.4× / 3.6× 1.6× / 1.3× 1.3× / 1.1×
arb_Arab 1.01 3.02 2.33 4.34 31.4 19.6 6.8 5.2 13.5× / 7.2× 8.4× / 4.5× 2.9× / 1.6× 2.2× / 1.2×
ben_Beng 1.46 2.98 3.05 4.57 45.4 28.2 10.1 3.7 14.9× / 9.9× 9.2× / 6.2× 3.3× / 2.2× 1.2× / 0.8×
cmn_Hani 1.13 2.34 2.35 3.56 19.7 11.8 4.7 2.4 8.4× / 5.5× 5.0× / 3.3× 2.0× / 1.3× 1.0× / 0.7×
ell_Grek 0.58 3.00 2.20 4.62 28.8 17.5 6.1 4.8 13.1× / 6.2× 7.9× / 3.8× 2.8× / 1.3× 2.2× / 1.0×
eng_Latn 0.10 1.50 3.05 4.45 44.5 33.1 12.1 3.8 14.6× / 10.0× 10.9× / 7.4× 4.0× / 2.7× 1.2× / 0.8×
heb_Hebr 1.00 3.12 2.30 4.43 31.5 19.7 6.8 3.0 13.7× / 7.1× 8.6× / 4.5× 3.0× / 1.5× 1.3× / 0.7×
hin_Deva 1.36 3.12 3.18 4.93 48.2 30.2 11.1 4.1 15.2× / 9.8× 9.5× / 6.1× 3.5× / 2.2× 1.3× / 0.8×
jpn_Jpan 1.57 3.33 2.17 3.93 18.3 10.0 4.0 3.8 8.4× / 4.6× 4.6× / 2.5× 1.9× / 1.0× 1.8× / 1.0×
kat_Geor 1.39 2.57 2.09 3.27 17.2 11.3 4.1 2.0 8.2× / 5.3× 5.4× / 3.5× 2.0× / 1.3× 1.0× / 0.6×
kor_Hang 1.08 2.86 2.50 4.28 31.2 22.2 7.3 3.7 12.5× / 7.3× 8.9× / 5.2× 2.9× / 1.7× 1.5× / 0.9×
rus_Cyrl 1.02 2.93 2.15 4.06 27.2 16.7 5.9 2.4 12.6× / 6.7× 7.7× / 4.1× 2.8× / 1.5× 1.1× / 0.6×
tam_Taml 0.92 2.92 2.65 4.65 44.5 26.4 9.7 3.4 16.8× / 9.6× 9.9× / 5.7× 3.6× / 2.1× 1.3× / 0.7×
tha_Thai 1.37 2.64 2.53 3.80 28.4 16.5 6.7 3.0 11.2× / 7.5× 6.5× / 4.3× 2.7× / 1.8× 1.2× / 0.8×
added_normalized_dense 0.06 1.47 1.07 2.48 24.0 16.7 6.5 1.9 22.5× / 9.7× 15.7× / 6.7× 6.1× / 2.6× 1.8× / 0.8×
added_normalized_sparse 0.06 1.47 1.93 3.35 31.7 22.5 8.9 2.7 16.4× / 9.4× 11.6× / 6.7× 4.6× / 2.6× 1.4× / 0.8×
added_special_dense 0.06 1.47 4.64 6.05 96.3 74.0 20.8 3.2 20.8× / 15.9× 16.0× / 12.2× 4.5× / 3.4× 0.7× / 0.5×
added_special_sparse 0.06 1.48 4.84 6.26 61.7 45.3 15.5 3.7 12.7× / 9.8× 9.4× / 7.2× 3.2× / 2.5× 0.8× / 0.6×
agentic-traces 0.56 1.47 3.69 4.59 53.6 45.3 15.3 4.6 14.5× / 11.7× 12.3× / 9.9× 4.1× / 3.3× 1.2× / 1.0×
agentic_swe 0.54 1.45 2.83 3.73 56.1 50.2 15.2 3.4 19.8× / 15.0× 17.7× / 13.4× 5.4× / 4.1× 1.2× / 0.9×
code_mixed 0.10 1.44 3.25 4.60 53.7 49.8 14.9 4.0 16.5× / 11.7× 15.3× / 10.8× 4.6× / 3.2× 1.2× / 0.9×
math_latex 0.57 1.48 3.45 4.36 51.6 38.8 14.0 4.2 15.0× / 11.8× 11.2× / 8.9× 4.1× / 3.2× 1.2× / 1.0×
llama-2 — model-bounded BPE, no pre-tokenizer · ×3.72 vs v0.23.1 · ×1.16 vs base llama-2 speedup llama-2 stage decomposition llama-2 thread scaling

Memory (RSS MB, load+encode): v0.23.1 19+0 (peak 23) · Pipeline 22+0 (peak 21)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model Ids
amh_Ethi lang 5.6 47.3 ×8.42 ×1.04 0% (0.0) 13% (2.7) 0% (0.0) 87% (18.0) match
arb_Arab lang 12.3 54.4 ×4.42 ×1.04 0% (0.0) 17% (3.2) 0% (0.0) 82% (15.0) match
ben_Beng lang 13.6 95.8 ×7.07 ×1.09 0% (0.0) 21% (2.1) 0% (0.0) 79% (8.0) match
cmn_Hani lang 11.3 74.4 ×6.59 ×1.10 0% (0.1) 3% (0.4) 0% (0.0) 96% (12.1) match
ell_Grek lang 12.8 63.5 ×4.97 ×1.06 0% (0.0) 20% (3.1) 0% (0.0) 80% (12.1) match
eng_Latn lang 4.4 6.4 ×1.46 ×1.04 0% (0.1) 4% (6.8) 0% (0.0) 96% (144.8) match
heb_Hebr lang 12.5 68.0 ×5.44 ×1.06 0% (0.0) 23% (3.3) 0% (0.0) 77% (11.0) match
hin_Deva lang 14.9 85.1 ×5.72 ×1.01 0% (0.0) 25% (2.8) 0% (0.0) 75% (8.5) match
jpn_Jpan lang 16.1 96.5 ×6.01 ×1.11 1% (0.1) 4% (0.4) 0% (0.0) 96% (9.4) match
kat_Geor lang 17.9 105.5 ×5.89 ×1.12 0% (0.0) 20% (1.9) 0% (0.0) 79% (7.4) match
kor_Hang lang 8.6 57.2 ×6.63 ×1.06 0% (0.1) 19% (3.2) 0% (0.0) 81% (13.7) match
rus_Cyrl lang 9.1 16.9 ×1.85 ×1.03 0% (0.0) 5% (2.8) 0% (0.0) 95% (55.6) match
tam_Taml lang 15.6 103.7 ×6.65 ×1.10 0% (0.0) 18% (1.7) 0% (0.0) 81% (7.7) match
tha_Thai lang 19.6 97.6 ×4.97 ×1.07 0% (0.0) 10% (0.9) 0% (0.0) 90% (8.9) match
added_normalized_dense modalities 6.0 8.8 ×1.46 ×1.01 0% (0.1) 3% (3.8) 0% (0.0) 97% (110.7) match
added_normalized_sparse modalities 5.2 7.5 ×1.43 ×1.00 0% (0.0) 4% (5.9) 0% (0.0) 96% (126.7) match
added_special_dense modalities 5.3 38.8 ×7.26 ×1.96 21% (5.0) 65% (15.5) 7% (1.7) 7% (1.6) match
added_special_sparse modalities 7.6 49.7 ×6.52 ×4.83 12% (2.1) 77% (13.7) 3% (0.5) 9% (1.5) match
agentic-traces modalities 4.8 7.2 ×1.50 ×1.01 0% (0.1) 4% (6.1) 0% (0.0) 95% (131.4) match
agentic_swe modalities 4.3 7.5 ×1.74 ×1.00 0% (0.0) 7% (9.7) 0% (0.0) 93% (124.7) match
code_mixed modalities 4.5 7.2 ×1.62 ×1.00 0% (0.0) 6% (7.9) 0% (0.0) 94% (131.8) match
math_latex modalities 4.6 6.8 ×1.48 ×1.02 0% (0.1) 4% (6.3) 0% (0.0) 96% (140.8) match
llama-3 — cl100k-regex byte-level BPE (llama-3), single regex · ×21.20 vs v0.23.1 · ×3.20 vs base llama-3 speedup llama-3 stage decomposition llama-3 thread scaling

Memory (RSS MB, load+encode): v0.23.1 143+0 (peak 199) · Pipeline 144+0 (peak 200)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model Ids
amh_Ethi lang 4.4 89.2 ×20.06 ×1.74 7% (0.7) 0% (0.0) 39% (3.7) 54% (5.1) match
arb_Arab lang 4.2 97.1 ×22.87 ×5.56 7% (0.6) 0% (0.0) 27% (2.3) 66% (5.6) match
ben_Beng lang 4.1 103.8 ×25.41 ×3.28 7% (0.6) 0% (0.0) 35% (3.1) 59% (5.2) match
cmn_Hani lang 4.8 84.4 ×17.69 ×5.40 7% (0.6) 0% (0.0) 27% (2.3) 67% (5.9) match
ell_Grek lang 5.0 112.8 ×22.76 ×5.74 9% (0.6) 0% (0.0) 32% (2.2) 60% (4.1) match
eng_Latn lang 4.0 70.6 ×17.65 ×2.09 18% (2.0) 0% (0.0) 28% (3.0) 54% (5.8) match
heb_Hebr lang 4.2 96.8 ×22.97 ×3.57 7% (0.6) 0% (0.0) 27% (2.3) 67% (5.7) match
hin_Deva lang 4.4 118.7 ×26.86 ×1.57 8% (0.6) 0% (0.0) 43% (3.2) 49% (3.7) match
jpn_Jpan lang 5.8 119.3 ×20.67 ×7.54 9% (0.6) 0% (0.0) 31% (2.1) 60% (4.1) match
kat_Geor lang 5.6 134.1 ×24.01 ×3.37 10% (0.6) 0% (0.0) 35% (2.1) 55% (3.3) match
kor_Hang lang 4.0 68.6 ×17.16 ×3.82 5% (0.6) 0% (0.0) 21% (2.5) 74% (9.0) match
rus_Cyrl lang 4.9 102.7 ×20.76 ×6.46 8% (0.6) 0% (0.0) 28% (2.2) 64% (4.8) match
tam_Taml lang 4.2 108.4 ×25.84 ×3.07 7% (0.6) 0% (0.0) 32% (2.7) 61% (5.2) match
tha_Thai lang 5.2 103.6 ×19.74 ×5.10 8% (0.6) 0% (0.0) 33% (2.5) 59% (4.6) match
added_normalized_dense modalities 6.4 195.0 ×30.60 ×7.06 16% (0.7) 0% (0.0) 22% (1.1) 63% (3.0) match
added_normalized_sparse modalities 5.7 141.5 ×24.76 ×3.27 19% (1.3) 0% (0.0) 30% (2.0) 51% (3.3) match
added_special_dense modalities 4.5 78.1 ×17.49 ×1.12 41% (4.9) 0% (0.0) 42% (5.0) 16% (1.9) match
added_special_sparse modalities 4.6 83.4 ×18.19 ×1.20 29% (3.2) 0% (0.0) 43% (4.8) 29% (3.2) match
agentic-traces modalities 3.9 68.8 ×17.62 ×2.29 14% (1.8) 0% (0.0) 29% (3.7) 57% (7.3) match
agentic_swe modalities 4.2 88.9 ×21.16 ×3.50 13% (1.3) 0% (0.0) 29% (2.8) 57% (5.5) match
code_mixed modalities 4.3 85.9 ×19.88 ×2.12 15% (1.5) 0% (0.0) 32% (3.3) 53% (5.5) match
math_latex modalities 3.8 70.5 ×18.31 ×2.20 15% (1.9) 0% (0.0) 28% (3.4) 57% (7.1) match

Pre-tokenize: classify + fsm vs regex engines — ns/byte, lower better. The fsm is the scalar jump-table in both pipe columns; SIMD / scalar is the classify pass (regex pre-tokenizers have no SIMD fsm). ×vs = engine ÷ our pipeline (SIMD / scalar classify); onig & pcre2 (JIT) are C, fancy is pure-Rust fancy-regex, logos is a compile-time DFA lexer (approximate grammar; n/a for deepseek).

Fixture classify SIMD classify scalar pipe (SIMD cls + fsm) pipe (scalar cls + fsm) onig fancy pcre2 logos ×vs onig ×vs fancy ×vs pcre2 ×vs logos
amh_Ethi 2.64 3.41 3.70 4.46 26.6 16.4 6.0 4.8 7.2× / 5.9× 4.4× / 3.7× 1.6× / 1.3× 1.3× / 1.1×
arb_Arab 1.00 3.12 2.30 4.41 30.5 20.5 6.8 5.2 13.3× / 6.9× 8.9× / 4.6× 3.0× / 1.5× 2.3× / 1.2×
ben_Beng 1.47 2.93 3.08 4.54 44.2 29.1 10.4 3.8 14.4× / 9.7× 9.5× / 6.4× 3.4× / 2.3× 1.2× / 0.8×
cmn_Hani 1.12 2.31 2.35 3.53 19.1 12.1 4.6 2.4 8.1× / 5.4× 5.1× / 3.4× 2.0× / 1.3× 1.0× / 0.7×
ell_Grek 0.58 3.00 2.18 4.60 28.0 17.9 6.1 4.8 12.8× / 6.1× 8.2× / 3.9× 2.8× / 1.3× 2.2× / 1.1×
eng_Latn 0.11 1.48 3.02 4.39 43.0 35.1 12.4 3.8 14.2× / 9.8× 11.6× / 8.0× 4.1× / 2.8× 1.2× / 0.9×
heb_Hebr 1.01 3.15 2.28 4.43 30.5 20.0 7.1 2.9 13.4× / 6.9× 8.7× / 4.5× 3.1× / 1.6× 1.3× / 0.7×
hin_Deva 1.37 3.19 3.20 5.02 47.0 32.5 11.2 4.0 14.7× / 9.4× 10.2× / 6.5× 3.5× / 2.2× 1.3× / 0.8×
jpn_Jpan 1.56 3.47 2.14 4.05 17.9 10.2 4.1 3.8 8.3× / 4.4× 4.7× / 2.5× 1.9× / 1.0× 1.8× / 0.9×
kat_Geor 1.39 2.57 2.06 3.25 16.8 11.7 4.2 2.0 8.1× / 5.2× 5.7× / 3.6× 2.0× / 1.3× 1.0× / 0.6×
kor_Hang 1.10 2.79 2.51 4.21 30.2 21.7 7.5 3.7 12.0× / 7.2× 8.6× / 5.1× 3.0× / 1.8× 1.5× / 0.9×
rus_Cyrl 1.01 2.96 2.15 4.10 26.5 16.5 6.0 2.5 12.3× / 6.5× 7.7× / 4.0× 2.8× / 1.5× 1.2× / 0.6×
tam_Taml 0.92 2.94 2.69 4.70 43.5 27.6 10.1 3.4 16.2× / 9.3× 10.3× / 5.9× 3.8× / 2.2× 1.3× / 0.7×
tha_Thai 1.36 2.56 2.51 3.71 27.6 16.7 6.9 3.0 11.0× / 7.4× 6.7× / 4.5× 2.7× / 1.9× 1.2× / 0.8×
added_normalized_dense 0.06 1.48 1.05 2.47 22.9 16.9 6.6 1.9 21.7× / 9.3× 16.0× / 6.8× 6.3× / 2.7× 1.8× / 0.8×
added_normalized_sparse 0.06 1.47 1.96 3.38 30.2 22.7 8.8 2.7 15.4× / 9.0× 11.6× / 6.7× 4.5× / 2.6× 1.4× / 0.8×
added_special_dense 0.06 1.47 4.98 6.40 91.5 74.5 20.8 3.2 18.4× / 14.3× 15.0× / 11.6× 4.2× / 3.2× 0.6× / 0.5×
added_special_sparse 0.06 1.47 4.79 6.21 59.3 46.7 15.1 3.5 12.4× / 9.5× 9.8× / 7.5× 3.2× / 2.4× 0.7× / 0.6×
agentic-traces 0.64 1.48 3.71 4.55 51.9 47.6 15.4 4.6 14.0× / 11.4× 12.8× / 10.5× 4.2× / 3.4× 1.2× / 1.0×
agentic_swe 0.54 1.46 2.83 3.75 54.3 54.9 15.8 3.4 19.2× / 14.5× 19.4× / 14.6× 5.6× / 4.2× 1.2× / 0.9×
code_mixed 0.09 1.45 3.26 4.62 52.2 52.6 15.5 4.0 16.0× / 11.3× 16.1× / 11.4× 4.8× / 3.4× 1.2× / 0.9×
math_latex 0.63 1.48 3.42 4.28 49.7 42.2 14.3 4.2 14.5× / 11.6× 12.3× / 9.9× 4.2× / 3.4× 1.2× / 1.0×
Not yet supported: t5-base
t5-base not supported

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@SBrandeis SBrandeis changed the title wip / ai: scratch pool feta(bpe): ScratchPool pattern + BPE word cache Jul 21, 2026
Comment on lines +4 to +8
/// naive implem of word -> IDs cache
pub struct WordCache {
capacity: usize,
lookup: AHashMap<String, Box<[u32]>>,
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we ever want to use this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YES (look at the perf improvements)

let maybe_scratch = self
.pool
.lock()
.unwrap_or_else(PoisonError::into_inner)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this do practically? does it return Vec<PipelineModelScratch> in its last known state?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PoisonError::into_inner ignores the poisoning and hands the lock regardless of the error, which is safe here because what we do is push/pop on a Vec

Worst case, a scratch buffer is lost or leaked, which is fine because we clear it at L303

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From Claude:

1. The only code that can panic while holding this lock is Vec::push's capacity overflow — practically unreachable here (pop is panic-free, OOM aborts).
2. Even in that unreachable case, the Vec is left valid and merely unchanged, and get already tolerates a smaller-than-expected pool via model.init_scratch() at line 306.

.pool
.lock()
.unwrap_or_else(PoisonError::into_inner)
.pop(); // Release the lock

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we sure lock is released here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used a {} block to make it more obvious

impl ScratchPool {
fn new() -> Self {
Self {
pool: Mutex::new(Vec::new()),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be Arc<Mutex<...>> instead since we'll be sharing this across threads?

// Option so we can use `.take()` to reclaim ownership of the scratch
// to push it back to the pool
scratch: Option<PipelineModelScratch>,
pool: &'a ScratchPool,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pool: &'a ScratchPool,
scratch_pool: &'a ScratchPool,

self.pool
.pool
.lock()
.unwrap_or_else(PoisonError::into_inner)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that this allows some form of recovery. If a thread panics, do we want to abort the program or recover the last known state of the pool?

impl std::ops::Deref for ScratchGuard<'_> {
type Target = PipelineModelScratch;
fn deref(&self) -> &PipelineModelScratch {
self.scratch.as_ref().unwrap()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.unwrap?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed the type to remove the necessity of unwrap


impl std::ops::DerefMut for ScratchGuard<'_> {
fn deref_mut(&mut self) -> &mut PipelineModelScratch {
self.scratch.as_mut().unwrap()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too, .unwrap not great

Comment thread tokenizers/tk-encode/src/utils/cache.rs Outdated

/// The default capacity for a `BPE`'s internal cache.
pub static DEFAULT_CACHE_CAPACITY: usize = 10_000;
pub static DEFAULT_CACHE_CAPACITY: usize = 1 << 16;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bitshift + usize seems error prone, although shifting by 16 should be safe for most platforms

@SBrandeis SBrandeis changed the title feta(bpe): ScratchPool pattern + BPE word cache feat(bpe): ScratchPool pattern + BPE word cache Jul 21, 2026
@SBrandeis
SBrandeis marked this pull request as ready for review July 22, 2026 09:12
@SBrandeis SBrandeis mentioned this pull request Jul 22, 2026
self.scratch_pool
.pool
.lock()
.unwrap_or_else(PoisonError::into_inner)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does acquiring the lock + calling poisonerror::into_inner "unpoison" the lock?

Just found: mutex.clear_poison(); might be worth calling, wonder if we can do it in the unwrap_or_else closure

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does acquiring the lock + calling poisonerror::into_inner "unpoison" the lock?

It does not

Just found: mutex.clear_poison(); might be worth calling, wonder if we can do it in the unwrap_or_else closure

Wouldn't do much except an unecessary flag flip, no?

@McPatate McPatate Jul 22, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels weird to continue using a poisoned mutex, but from what I can gather it's not much of an issue indeed. If a thread were to fail a write to the inner pool object, could we leave things in a state that would cause downstream errors (e.g. partial writes) or is it simply a matter of "has been added/removed to the pool" and since it's a cache we don't really care?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is mostly safe to fail at that specific boundary for a few reasons:

  • the only thing that can panic while holding the lock is the Vec::push (out of capacity)
  • even if something else would panic, the lock is only held to push or pop a scratch buffer into or from the pool
  • not managing to re-pool a scratch buffer is safe (we lost the cache but not critical)
  • not managing to pop an existing scratch buffer is safe, the code defaults to ::init()ing a fresh one

With the current state of the code, the Mutex being poisonned is both unlikely and not recoverable

@SBrandeis
SBrandeis requested a review from McPatate July 22, 2026 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants