Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that processors add the number of tokens they say they will #1312

Closed
wants to merge 3 commits into from
Closed
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
17 changes: 16 additions & 1 deletion tokenizers/src/tokenizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,9 +916,10 @@ where
add_special_tokens: bool,
) -> Result<Encoding> {
// 1. First we truncate if needed
let is_pair = pair_encoding.is_some();
let (encoding, pair_encoding) = {
if let Some(trunc) = &self.truncation {
let n_added_tokens = self.get_n_added_tokens(pair_encoding.is_some());
let n_added_tokens = self.get_n_added_tokens(is_pair);

if add_special_tokens && n_added_tokens > 0 {
let params = TruncationParams {
Expand All @@ -933,6 +934,11 @@ where
(encoding, pair_encoding)
}
};
let original_length = if let Some(second_encoding) = &pair_encoding {
encoding.len() + second_encoding.len()
} else {
encoding.len()
};

// 2. Then We post process
let final_encoding = if let Some(processor) = &self.post_processor {
Expand All @@ -950,6 +956,15 @@ where
}
encodings.pop().unwrap()
};
if add_special_tokens {
assert_eq!(
final_encoding.len() - self.get_n_added_tokens(is_pair),
original_length,
"Processor should add {} tokens but instead added {}!",
self.get_n_added_tokens(is_pair),
final_encoding.len() - original_length
)
};

// 3. Then we pad if needed
let [final_encoding] = if let Some(params) = &self.padding {
Expand Down
Loading