Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion tokenizers/src/tokenizer/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl Encoding {

/// Returns the index of the sequence containing the given token
pub fn token_to_sequence(&self, token: usize) -> Option<usize> {
if token > self.len() {
if token >= self.len() {
None
} else if self.sequence_ranges.is_empty() {
Some(0)
Expand Down Expand Up @@ -566,6 +566,20 @@ mod tests {
use super::*;
use std::iter::FromIterator;

#[test]
fn token_to_sequence_out_of_bounds() {
// An unprocessed encoding has no `sequence_ranges`, so every in-range token
// maps to sequence 0. Out-of-range indices (>= len) must return `None`, like
// the sibling accessors `token_to_chars`/`token_to_word` do via `.get(token)`.
let encoding = Encoding {
ids: vec![1, 2, 3],
..Default::default()
};
assert_eq!(encoding.token_to_sequence(2), Some(0)); // last valid index
assert_eq!(encoding.token_to_sequence(3), None); // == len, out of bounds
assert_eq!(encoding.token_to_sequence(4), None); // > len, out of bounds
}

#[test]
fn merge_encodings() {
let mut a = Encoding {
Expand Down