Skip to content

Commit

Permalink
potential solution?
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurZucker committed Sep 30, 2024
1 parent 14a07b0 commit ac4e717
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tokenizers/src/models/unigram/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ use super::{
use crate::tokenizer::{Model, Result, Token};
use crate::utils::cache::Cache;

use std::collections::HashMap;
use std::convert::TryInto;
use std::fs::read_to_string;
use std::path::{Path, PathBuf};
use std::{
collections::{HashMap, HashSet},
sync::Arc,
};

type TokenMap = HashMap<String, u32>;
type Vocab = Vec<(String, f64)>;
Expand All @@ -28,6 +31,7 @@ pub struct Unigram {
fuse_unk: bool,
is_optimized: bool,
byte_fallback: bool,
pub special_tokens: Option<Arc<HashSet<String>>>,
}
impl PartialEq for Unigram {
fn eq(&self, other: &Self) -> bool {
Expand All @@ -52,6 +56,7 @@ impl Clone for Unigram {
fuse_unk: self.fuse_unk,
is_optimized: self.is_optimized,
byte_fallback: self.byte_fallback,
special_tokens: self.special_tokens.as_ref().map(Arc::clone),
}
}
}
Expand Down Expand Up @@ -114,6 +119,9 @@ impl Unigram {

let mut min_score = f64::INFINITY;
for (id, (token, score)) in vocab.iter().enumerate() {
if &token.to_string() == "<s>" {
continue;
}
token_to_ids.insert(token.to_string(), id as u32);
let bytes: Vec<u8> = token.bytes().collect();
builder.push(&bytes);
Expand All @@ -137,6 +145,7 @@ impl Unigram {
cache: Cache::default(),
is_optimized,
byte_fallback,
special_tokens: None,
})
}

Expand Down
4 changes: 4 additions & 0 deletions tokenizers/src/tokenizer/added_vocabulary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ impl AddedVocabulary {

if self.encode_special_tokens && self.special_tokens_set.contains(&added_token.content)
{
println!(
"finding matched, but encode special tokens is true. skipping: {:?}",
added_token
);
continue;
}

Expand Down

0 comments on commit ac4e717

Please sign in to comment.