perf: skip alignment tracking in encode_fast normalization#2022
Open
ArthurZucker wants to merge 3 commits intomainfrom
Open
perf: skip alignment tracking in encode_fast normalization#2022ArthurZucker wants to merge 3 commits intomainfrom
ArthurZucker wants to merge 3 commits intomainfrom
Conversation
…_fast When encode_fast is called (OffsetType::None), the normalization step now uses normalize_str + set_normalized instead of the full NormalizedString::normalize which builds per-byte alignment vectors. Changes: - NormalizedString::set_normalized(): replace normalized content with trivial 1:1 alignments (enough for splitting, no real offset mapping) - AddedVocabulary::extract_and_normalize_fast(): uses normalize_str for the normalization step, avoiding O(n) alignment allocations - encode_single_sequence: automatically picks the fast path when offsets_type is None (i.e. encode_fast) - Normalizer::normalize_str trait method added (default falls back to NormalizedString)
|
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. |
Collaborator
Author
|
/benchmark |
Stresses the normalize path during deserialization: 100k added tokens with NFKC normalizer, saved to a temp file and loaded back. This reflects real-world tokenizers with large added vocabularies.
Collaborator
Author
|
/benchmark |
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
When
encode_fastis called, the normalization step now skips per-byte alignment tracking — the main allocation overhead in the normalize path.Problem
extract_and_normalizecallsnormalizer.normalize(&mut NormalizedString)which rebuilds alignment vectors (one(usize, usize)per byte) on every normalization. Forencode_fastwhere offsets are explicitly not needed (OffsetType::None), this is wasted work.Solution
NormalizedString::set_normalized(String)— replaces normalized content with trivial 1:1 alignments. Enough for the split/slice machinery to work, but no real offset mapping is preserved.AddedVocabulary::extract_and_normalize_fast()— usesNormalizer::normalize_str()(noNormalizedStringallocation in the normalizer) +set_normalized()to avoid alignment tracking.encode_single_sequence— automatically picks the fast path whenoffsets_type == OffsetType::None.What this skips per normalization call
For a normalizer like
Lowercase,normalize_stris justs.to_lowercase()— one allocation, zero alignment work.Depends on
normalize_strimplementations (this PR includes the trait method with default fallback, so it works standalone too)