-
Notifications
You must be signed in to change notification settings - Fork 782
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
Give error when initializing tokenizer with too high stride #1306
Changes from all commits
ec31bd2
ffce311
e812b42
06261db
16af73a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -497,6 +497,10 @@ impl DerefMut for Tokenizer { | |
} | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
#[error("{0}")] | ||
pub struct TruncationParamError(String); | ||
|
||
/// A `Tokenizer` is capable of encoding/decoding any text. | ||
#[derive(Clone, Debug)] | ||
pub struct TokenizerImpl<M, N, PT, PP, D> { | ||
|
@@ -595,9 +599,21 @@ where | |
} | ||
|
||
/// Set the truncation parameters | ||
pub fn with_truncation(&mut self, trunc: Option<TruncationParams>) -> &mut Self { | ||
/// | ||
/// Fails if `stride` is too high relative to `max_length` and `post_processor.added_tokens()` | ||
pub fn with_truncation(&mut self, trunc: Option<TruncationParams>) -> Result<&mut Self> { | ||
if let Some(trunc_params) = &trunc { | ||
let n_added_tokens = self.get_n_added_tokens(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assumed that fewer tokens were added when Another reason why I kept the original There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Narsil can you confirm whether my assumption in the above comment is a good one (i.e. is |
||
let effective_max_length = trunc_params.max_length - n_added_tokens; | ||
if effective_max_length <= trunc_params.stride { | ||
return Err(Box::new(TruncationParamError(format!( | ||
"tokenizer stride set to {}, which is greater than or equal to its effective max length of {} (= {} original max length - {} added special tokens), ", | ||
trunc_params.stride, effective_max_length, trunc_params.max_length, n_added_tokens | ||
)))); | ||
} | ||
} | ||
self.truncation = trunc; | ||
self | ||
Ok(self) | ||
} | ||
|
||
/// Get the currently set truncation parameters | ||
|
@@ -902,11 +918,7 @@ where | |
// 1. First we truncate if needed | ||
let (encoding, pair_encoding) = { | ||
if let Some(trunc) = &self.truncation { | ||
let n_added_tokens = if let Some(processor) = &self.post_processor { | ||
processor.added_tokens(pair_encoding.is_some()) | ||
} else { | ||
0 | ||
}; | ||
let n_added_tokens = self.get_n_added_tokens(pair_encoding.is_some()); | ||
|
||
if add_special_tokens && n_added_tokens > 0 { | ||
let params = TruncationParams { | ||
|
@@ -950,6 +962,14 @@ where | |
|
||
Ok(final_encoding) | ||
} | ||
|
||
fn get_n_added_tokens(&self, is_pair: bool) -> usize { | ||
if let Some(processor) = &self.post_processor { | ||
processor.added_tokens(is_pair) | ||
} else { | ||
0 | ||
} | ||
} | ||
} | ||
|
||
impl<M, N, PT, PP, D> TokenizerImpl<M, N, PT, PP, D> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you
expect
here ? no_truncation should never fail, but it's better to actually fail than be in the dark.