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
6 changes: 5 additions & 1 deletion tokenizers/src/models/bpe/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,12 @@ impl BPE {

/// Resize the cache
pub fn resize_cache(&mut self, capacity: usize) {
if let Some(ref mut cache) = self.cache {
if capacity == 0 {
self.cache = None;
} else if let Some(cache) = self.cache.as_mut() {
cache.resize(capacity);
} else {
self.cache = Some(Cache::new(capacity));
}
}

Expand Down
41 changes: 25 additions & 16 deletions tokenizers/src/models/unigram/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Vocab = Vec<(String, f64)>;
pub struct Unigram {
token_to_ids: TokenMap,
pub(crate) vocab: Vocab,
cache: Cache<String, Vec<String>>,
cache: Option<Cache<String, Vec<String>>>,
trie: Trie<u8>,
pub min_score: f64,
pub(super) unk_id: Option<usize>,
Expand All @@ -40,7 +40,7 @@ impl Clone for Unigram {
// `Clone` can't be derive because it's not implemented for `Cache`.
// To keep things simple when we clone, the new Unigram will start with a fresh cache.
fn clone(&self) -> Self {
let fresh_cache = self.cache.fresh();
let fresh_cache = self.cache.as_ref().map(|cache| cache.fresh());
Self {
vocab: self.vocab.clone(),
cache: fresh_cache,
Expand Down Expand Up @@ -134,7 +134,7 @@ impl Unigram {
eos_id,
unk_id,
fuse_unk,
cache: Cache::default(),
cache: Some(Cache::default()),
is_optimized,
byte_fallback,
})
Expand All @@ -143,7 +143,7 @@ impl Unigram {
#[cfg(test)]
pub(super) fn set_fuse_unk(&mut self, fuse_unk: bool) {
self.fuse_unk = fuse_unk;
self.cache = self.cache.fresh();
self.cache = self.cache.as_ref().map(|cache| cache.fresh());
}

#[cfg(test)]
Expand Down Expand Up @@ -222,19 +222,20 @@ impl Unigram {
if sentence.is_empty() {
return Ok(vec![]);
}
if let Some(result) = self.cache.get(sentence) {
Ok(result.to_vec())
if let Some(result) = self.cache.as_ref().and_then(|cache| cache.get(sentence)) {
return Ok(result.to_vec());
}
let result = if self.is_optimized {
self.encode_optimized(sentence)?
} else {
let result = if self.is_optimized {
self.encode_optimized(sentence)?
} else {
self.encode_unoptimized(sentence)?
};
if sentence.len() < MAX_LENGTH {
self.cache.set(sentence.to_owned(), result.clone());
self.encode_unoptimized(sentence)?
};
if sentence.len() < MAX_LENGTH {
if let Some(cache) = self.cache.as_ref() {
cache.set(sentence.to_owned(), result.clone());
}
Ok(result)
}
Ok(result)
}

fn encode_optimized(&self, sentence: &str) -> Result<Vec<String>> {
Expand Down Expand Up @@ -375,12 +376,20 @@ impl Unigram {

/// Clears the internal cache
pub fn clear_cache(&mut self) {
self.cache.clear();
if let Some(cache) = self.cache.as_ref() {
cache.clear();
}
}

/// Resize the cache
pub fn resize_cache(&mut self, capacity: usize) {
self.cache.resize(capacity);
if capacity == 0 {
self.cache = None;
} else if let Some(cache) = self.cache.as_mut() {
cache.resize(capacity);
} else {
self.cache = Some(Cache::new(capacity));
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions tokenizers/src/tokenizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,11 @@ where
&self.model
}

/// Get a mutable reference to the model
pub fn get_model_mut(&mut self) -> &mut M {
&mut self.model
}

/// Set the added vocabulary.
Comment on lines +607 to 612
Copy link
Collaborator

Choose a reason for hiding this comment

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

as this is unused here I would not put it in this PR!

pub fn with_added_vocabulary(&mut self, added_vocabulary: AddedVocabulary) -> &mut Self {
self.added_vocabulary = added_vocabulary;
Expand Down