Fix token_to_sequence returning Some(0) for out-of-bounds index#2214
Open
Osamaali313 wants to merge 1 commit into
Open
Fix token_to_sequence returning Some(0) for out-of-bounds index#2214Osamaali313 wants to merge 1 commit into
Osamaali313 wants to merge 1 commit into
Conversation
`Encoding::token_to_sequence` guarded with `if token > self.len()`, so the index `token == len` (one past the last valid token) fell through. For an unprocessed encoding (empty `sequence_ranges`) it then returned `Some(0)` instead of `None`. Valid token indices are `0..len`, and the sibling accessors treat `index >= len` as out of bounds: `token_to_chars` uses `self.offsets.get(token)` and `token_to_word` uses `self.words.get(token)`, both of which yield `None` at `token == len`. Use `>=` so `token_to_sequence` agrees with them. Processed encodings are unaffected (their populated `sequence_ranges` never contain `len`), so existing tests still pass. Adds a regression test for the unprocessed-encoding boundary.
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.
Problem
Encoding::token_to_sequenceguards the index with>instead of>=:Valid token indices are
0..len, sotoken == lenis out of bounds and should returnNone. Buttoken > self.len()lets it through, and for an unprocessed encoding (emptysequence_ranges, e.g. fromEncoding::from_tokensor a raw model output) it falls toSome(0).Evidence (sibling accessors prove intent)
Every other index-based accessor in the same file treats
index >= lenas out of bounds:token_to_chars→self.offsets.get(token)→Noneattoken == lentoken_to_word→self.words.get(token)→Noneattoken == lenOnly
token_to_sequenceadmitstoken == len.token_to_sequenceis public API (also exposed through the Python and Node bindings), so a caller doingencoding.token_to_sequence(len)on a single-sequence/unprocessed encoding gets0rather thanNone.Fix
Processed encodings are unaffected — their
sequence_rangesare populated and never containlen, so the boundary already resolved toNoneviafind_map; existing tests (processors::bert,processors::roberta, Pythontest_encoding.py) still pass. A regression test is added for the unprocessed-encoding boundary.