Use cached added-token dicts in per-token decode loops#46535
Open
ishan-1010 wants to merge 1 commit into
Open
Conversation
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: byt5, dia, myt5, perceiver |
Member
|
cc @itazap |
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.
What this fixes
Fixes #46396 (follow-up to #46323). Four slow tokenizers read the
added_tokens_encoder/added_tokens_decoderproperties inside the per-token loop ofconvert_tokens_to_string. Both properties rebuild and re-sort the full added-token map on every access, so decoding T tokens with N added tokens costs O(T * N * logN).The fix
Point those loops at the maintained
_added_tokens_encoder/_added_tokens_decoderdicts instead, same as #46323 did forconvert_tokens_to_ids. Membership and lookup don't depend on the property's sort order, so behavior is unchanged. Files:byt5,myt5,dia,perceivertokenizers; 10 lines changed, attribute swaps only.This is option (B) from #46396. The broader option (memoizing the property itself) is left for that issue since it has the cpmant-mutation and sort-order questions.
Numbers
Decoding 2,840 mixed tokens with 4,000 added tokens (M1, CPU):
Output verified byte-identical (md5) before/after for both.
Tests
Notes
Not a duplicate: no open PR touches these call sites (checked per the contribution policy). AI-assisted; I reviewed every line and ran the tests and benchmark above.