Repository navigation
fix(tk-encode): take &str in PatternMatcher::extract_next - #2712
Merged
Merged
Conversation
|
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
marked this pull request as ready for review
November 18, 2026 12:25
&str in PatternMatcher::extract_next
AddedVocabulary's single_word, lstrip and rstrip checks reinterpreted the input with from_utf8_unchecked, but the public trait method took &[u8]: a single_word token and the bytes b"\x80[T]" walked off the slice decoding an invalid str (miri: entering unreachable code). The only caller, SpecialSegmentIterator, already holds a &str, so the type now says it and the three unsafe blocks are gone.
SBrandeis
force-pushed
the
fix/extract-next-str
branch
from
November 19, 2026 10:05
fc09919 to
2b36ca3
Compare
The lstrip check sliced the input at match_end before a helper that only reads s[..match_start]. As a str slice that cut costs a char-boundary check per lstrip match and nothing uses it. Without it, SpecialSegmentIterator::next is 3 instructions smaller than before the &str change (x86-64, rustc 1.93).
SBrandeis
marked this pull request as draft
November 19, 2026 14:45
SBrandeis
marked this pull request as ready for review
November 19, 2026 15:39
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.
TL;DR
extract_nextuses&strinstead of&[u8]to remove potential UB surfacePerformance
Result: no regression. This PR is as fast as its parent.
102 MiB/s; on bert, within 0.5 MiB/s at about 22 MiB/s.
no code change at all, already gives results up to 7.3% apart. This PR is at most 4.2% from the
parent, and its median is never below the slower of the two parent builds.
So where a table below says "slower" for this PR, compare it with the "parent, rebuilt" row of the
same table: that row is the parent's own code, and it says "slower" by a similar or larger amount. Those
percentages are noise between builds, not a cost of this change.
Why there is nothing to pay for: the three
from_utf8_uncheckedare removed and no runtime checkreplaces them. The helpers already worked on a
str.Full tables: tokbench on HF Jobs, this PR vs its parent vs the parent rebuilt
tokbench,
pipelineengine, single thread, HF Jobscpu-performance(EPYC 7R13, rustc 1.93.0). Three builds: this PR (0490595), its parent(1a9ec50), and the parent built a second time from another directory. 20 rounds, builds rotated
within a round, pinned to one CPU. Cells are the median over rounds ± half of its 95% bootstrap
interval; the "vs parent" rows are the median per-round change. Ids are identical across builds and
rounds.
No tokbench model has an
lstrip,rstriporsingle_wordadded token, so the first five modelsare llama-3 plus the five
<|xs0..4|>tokens of theadded-special-*corpora, flagged as named.lstriptokensrstriptokenssingle_wordtokensall three flags
no flag
stock llama-3 (the corpora's tokens are not in its vocabulary)
stock bert-base-uncased
Reproduce: one HF Job, about 20 minutes
Save the script as
hf-job.sh, then:The tables above are printed at the end of the job log.