-
Notifications
You must be signed in to change notification settings - Fork 965
Fix unsigned integer underflow issue with truncation #1859
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||
---|---|---|---|---|---|---|
|
@@ -506,7 +506,7 @@ impl DerefMut for Tokenizer { | |||||
|
||||||
#[derive(thiserror::Error, Debug)] | ||||||
#[error("{0}")] | ||||||
pub struct TruncationParamError(String); | ||||||
pub struct TruncationParamError(pub String); | ||||||
|
||||||
/// A `Tokenizer` is capable of encoding/decoding any text. | ||||||
#[derive(Clone, Debug)] | ||||||
|
@@ -619,16 +619,6 @@ where | |||||
/// | ||||||
/// 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 { | ||||||
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 don't feel great about modifying this sanitation. If someone REALLY wants super low chunking and wants to ignore the added tokens. It seems specific enough that simply changing the post_processor for None would be much simpler at this points. So we can keep this footgun check alive, and power users can still modify the tokenizers' behavior. |
||||||
let n_added_tokens = self.get_n_added_tokens(false); | ||||||
let effective_max_length = trunc_params.max_length - n_added_tokens; | ||||||
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. This should probably be checked_sub so we don't panic either. |
||||||
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; | ||||||
Ok(self) | ||||||
} | ||||||
|
@@ -1216,7 +1206,7 @@ where | |||||
|
||||||
if add_special_tokens && n_added_tokens > 0 { | ||||||
let params = TruncationParams { | ||||||
max_length: trunc.max_length - n_added_tokens, | ||||||
max_length: if n_added_tokens > trunc.max_length {0} else {trunc.max_length - n_added_tokens}, | ||||||
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.
Suggested change
NIT: I feel like this is more readable |
||||||
..*trunc | ||||||
}; | ||||||
truncate_encodings(encoding, pair_encoding, ¶ms)? | ||||||
|
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.
No pub here please. Create a construction if needed.