mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[GH-ISSUE #268] [tokenizer] O(n^2) BPE merge loop: an unbroken CJK run is ~10 s per 8K chars (1266x slower than needed) #140
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#140
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @cha0yang on GitHub (Sep 30, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/268
tools/strata_tokenizer.pyis a pure-Python BPE whose merge loop is O(n^2) in the length of apre-token, and the pre-tokenizer hands it a very long pre-token whenever a stretch of text has
no punctuation or whitespace. For CJK that stretch is also byte-mapped first, so the length it
scans is 3x the number of characters. A run of Han characters without punctuation therefore costs
seconds, and the cost grows quadratically.
I hit this as "the engine hangs on long prompts":
server.py:736encodes the whole prompt beforethe engine is told anything, so the GPU sits idle, nothing appears in the engine log, and the
request just never returns. It is not the engine — it is the tokenizer.
Root cause
tools/strata_tokenizer.py:140(_bpe):Every merge rescans the entire list, so a word of length n costs O(n^2).
_encode_plainthen makesthe input 3x longer for non-ASCII, because it byte-maps each character to 1-3 byte-chars before
calling
_bpe:So 8000 Han characters (no punctuation -> one pre-token -> 24000 byte-chars) is 256,007,999 dict
lookups, measured with cProfile.
Measurements
Engine 0.1.29, this box, pack
swift-iq3_xxs,Tokenizer(tokens, merges, types)built exactly asserver.pybuilds it,encode(..., parse_special=True)._bpeseesQuadratic in the length of the unbroken run:
_bpeExtrapolating: 32K Han chars ~160 s, 53K ~7 min.
Note the asymmetry, which is probably why nobody has reported it: English is fine (3x slower than
it could be) and normal Chinese prose is fine (12x slower, but 0.024 s at 8K). Only a long
unbroken CJK run blows up. Pasting a long Chinese document that lost its line breaks, a
comma-free log line, or a long Chinese string inside tool output all trigger it.
A drop-in fix, no new dependencies
Replace the rescan with a min-heap over adjacent pairs plus a doubly-linked list of live positions,
and version-stamp heap entries so stale ones can be dropped. Same output, O(n log n):
Verified against the current implementation on 1500 random strings over
"中国古代四大发明abc,。\n"(including the byte-mapped form, which is what actually reaches_bpe): 0 differences. On the worst case above:It is slower than the current code on already-short pre-tokens (the heap setup costs more than a
15-element scan), so if that matters the two can be dispatched on
len(word)— but the shorter pathonly gets used in the region that is already fast.
For reference
I worked around it on my side with a thin wrapper over the Rust
tokenizerscrate loaded from themodel's official
tokenizer.json, which is also bit-identical on a corpus of mixed text and takes8K CJK from 9.95 s to 0.003 s. That adds a dependency, which is why I would prefer the heap version
upstream. Happy to send either as a PR — thanks, the rest of the engine has been excellent.
@cha0yang commented on GitHub (Sep 30, 2026):
Correction to the "For reference" paragraph, now that I have actually switched to the heap version
above: the Rust workaround was not bit-identical after all. The official
tokenizer.jsonapplies NFC, so it folds a decomposed Hangul jamo sequence into a composed syllable, while the GGUF
merges do not — and the round trip through it loses characters:
My first check was 9 strings of plain text, code and emoji, and it passed. A wider corpus (Hangul,
combining marks) is what caught it. So the dependency-free heap version is strictly better here, not
merely lighter: identical to the current implementation on 4000 random strings (both
parse_specialmodes) and on a mixed corpus of prose, code, Hangul, emoji and combining marks, andit has no NFC divergence.
It is also fast enough that the Rust path is not worth keeping:
The "space-free Latin" row is worth a look on its own, by the way — it is not only CJK that trips
the rescan. Anything that arrives as one long pre-token does, and a pasted URL or a minified JSON
blob is exactly that.
I have the heap version in production locally now and the long-prompt symptom is gone: an 8K
unbroken-CJK prompt goes from ~10 s of tokenization before the engine is even told anything to
nothing measurable.
@Niko1221 commented on GitHub (Sep 30, 2026):
Confirmed, thanks for the analysis. 0.1.31 uses your heap + linked-list merge for long words (same merge order, checked token for token against the current code), and short words keep the existing loop.
@Niko1221 commented on GitHub (Oct 1, 2026):
Fixed in 0.1.31 with your heap + linked-list merge for long words (same merge order, checked id for id against the old code): 2,000 CJK characters 1.6 s -> 5 ms here. Thanks!