fix: prevent IndexError in Whisper timestamp decode on trailing replacement char#45006
Open
Krishnachaitanyakc wants to merge 2 commits intohuggingface:mainfrom
Open
Conversation
…cement char When the decoded token stream ends with a dangling Unicode replacement character (U+FFFD), the computed index in _split_tokens_on_unicode() could equal len(decoded_full), causing an IndexError. Add a bounds check so that an out-of-range index is treated as a word boundary instead of crashing. Fixes huggingface#44869
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: whisper |
Member
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
Fixes #44869
Adds a bounds check in
_split_tokens_on_unicode()intokenization_whisper.pyto handle trailing Unicode replacement characters (U+FFFD) at the end of decoded token streams without crashing withIndexError.Problem
When the decoded token stream ends with a dangling replacement character, the computed index
unicode_offset + decoded.index(replacement_char)can equallen(decoded_full), causing an out-of-bounds string access.Fix
Pre-compute
target_indexand add atarget_index >= len(decoded_full)guard that short-circuits before the out-of-bounds access. When triggered, the trailing fragment is treated as a word boundary.AI Assistance Disclosure
This PR was developed with AI assistance. The fix has been manually reviewed, verified for correctness, and tested against the reported edge case.
Test Plan
unicode_offset=298, len(decoded_full)=298edge casetarget_indexcomputation