Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 2 additions & 12 deletions tokenizers/src/tokenizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ impl DerefMut for Tokenizer {

#[derive(thiserror::Error, Debug)]
#[error("{0}")]
pub struct TruncationParamError(String);
pub struct TruncationParamError(pub String);
Copy link
Collaborator

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.


/// A `Tokenizer` is capable of encoding/decoding any text.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel great about modifying this sanitation.
It seems to me that one should be aware of the added tokens as it's the standard way to tokenizer defined by the tokenizer's creator. So preventing chunking below those is kind of important.

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
}
Expand Down Expand Up @@ -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},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
max_length: if n_added_tokens > trunc.max_length {0} else {trunc.max_length - n_added_tokens},
max_length: trunc.max_length.checked_sub(n_added_tokens).unwrap_or(0)

NIT: I feel like this is more readable

..*trunc
};
truncate_encodings(encoding, pair_encoding, &params)?
Expand Down
9 changes: 8 additions & 1 deletion tokenizers/src/utils/truncation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::tokenizer::{Encoding, Result};
use crate::tokenizer::{Encoding, Result, TruncationParamError};
use serde::{Deserialize, Serialize};
use std::cmp;
use std::mem;
Expand Down Expand Up @@ -96,6 +96,13 @@ pub fn truncate_encodings(
return Ok((encoding, pair_encoding));
};

if params.stride > params.max_length {
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), ",
params.stride, params.max_length
))));
}

match params.strategy {
TruncationStrategy::LongestFirst => {
if let Some(other_encoding) = pair_encoding.as_mut() {
Expand Down