use crate::algorithms::Unigram;
use crate::error::{Result, TokenizerError};
use crate::pre_tokenizers::{PreTokenizer, Metaspace};
use crate::normalizers::{Normalizer, NFKCNormalizer};
use std::collections::HashMap;
pub struct LlamaTokenizer {
unigram: Unigram,
normalizer: Box<dyn Normalizer>,
pre_tokenizer: Metaspace,
vocab: HashMap<String, u32>,
id_to_token: HashMap<u32, String>,
scores: HashMap<String, f32>,
bos_token: String,
eos_token: String,
unk_token: String,
pad_token: String,
bos_token_id: u32,
eos_token_id: u32,
unk_token_id: u32,
pad_token_id: u32,
add_bos_token: bool,
add_eos_token: bool,
}
impl LlamaTokenizer {
pub fn from_pretrained(model: &str) -> Result<Self> {
match model {
"llama-2-7b" | "llama-2-13b" | "llama-2-70b" => Self::load_llama2(),
"llama-3-8b" | "llama-3-70b" => Self::load_llama3(),
"mistral-7b" => Self::load_mistral(),
"code-llama" => Self::load_code_llama(),
_ => Err(TokenizerError::UnknownModel(model.to_string())),
}
}
pub fn new(
vocab: HashMap<String, u32>,
scores: HashMap<String, f32>,
add_bos_token: bool,
add_eos_token: bool,
) -> Self {
let id_to_token: HashMap<u32, String> = vocab.iter()
.map(|(k, &v)| (v, k.clone()))
.collect();
let pieces: Vec<(String, f64)> = vocab.keys().map(|token| {
let score = *scores.get(token).unwrap_or(&0.0) as f64;
(token.clone(), score)
})
.collect();
let unigram = Unigram::new(pieces);
Self {
unigram,
normalizer: Box::new(NFKCNormalizer),
pre_tokenizer: Metaspace::new()
.with_replacement('▁')
.with_prefix_space(true),
vocab,
id_to_token,
scores,
bos_token: "<s>".to_string(),
eos_token: "</s>".to_string(),
unk_token: "<unk>".to_string(),
pad_token: "<pad>".to_string(),
bos_token_id: 1,
eos_token_id: 2,
unk_token_id: 0,
pad_token_id: 0,
add_bos_token,
add_eos_token,
}
}
fn load_llama2() -> Result<Self> {
let (vocab, scores) = Self::create_llama2_vocab();
Ok(Self::new(vocab, scores, true, false))
}
fn load_llama3() -> Result<Self> {
let (vocab, scores) = Self::create_llama3_vocab();
Ok(Self::new(vocab, scores, true, false))
}
fn load_mistral() -> Result<Self> {
let (vocab, scores) = Self::create_llama2_vocab();
Ok(Self::new(vocab, scores, true, false))
}
fn load_code_llama() -> Result<Self> {
let (vocab, scores) = Self::create_code_llama_vocab();
Ok(Self::new(vocab, scores, true, false))
}
fn create_llama2_vocab() -> (HashMap<String, u32>, HashMap<String, f32>) {
let mut vocab = HashMap::new();
let mut scores = HashMap::new();
vocab.insert("<unk>".to_string(), 0);
vocab.insert("<s>".to_string(), 1);
vocab.insert("</s>".to_string(), 2);
scores.insert("<unk>".to_string(), 0.0);
scores.insert("<s>".to_string(), 0.0);
scores.insert("</s>".to_string(), 0.0);
let common_tokens = vec![
("▁the", 278, -3.5), ("▁of", 310, -4.0), ("▁and", 322, -3.8),
("▁to", 304, -3.2), ("▁a", 263, -3.0), ("▁in", 297, -3.6),
("▁is", 338, -4.2), ("▁for", 363, -4.5), ("▁on", 373, -4.8),
("▁that", 393, -5.0), ("▁with", 411, -5.2), ("▁as", 408, -5.5),
("▁it", 372, -4.9), ("▁be", 367, -5.1), ("▁by", 491, -5.8),
("▁at", 472, -5.6), ("▁Hello", 15043, -8.2), ("▁world", 3186, -7.5),
(",", 29892, -2.0), (".", 29889, -1.8), ("!", 29991, -6.0),
("?", 29973, -6.2), (":", 29901, -5.3), (";", 29936, -7.0),
];
for (token, id, score) in common_tokens {
vocab.insert(token.to_string(), id);
scores.insert(token.to_string(), score);
}
for byte in 0..=255 {
let token = format!("<0x{:02X}>", byte);
vocab.insert(token.clone(), 3 + byte as u32);
scores.insert(token, -10.0); }
let subwords = [("ing", -6.5), ("ed", -6.8), ("s", -5.9), ("ly", -7.2),
("er", -7.0), ("ion", -7.5), ("tion", -7.8), ("al", -7.3)];
for (i, (subword, score)) in subwords.iter().enumerate() {
vocab.insert(subword.to_string(), 500 + i as u32);
scores.insert(subword.to_string(), *score);
}
(vocab, scores)
}
fn create_llama3_vocab() -> (HashMap<String, u32>, HashMap<String, f32>) {
let (mut vocab, mut scores) = Self::create_llama2_vocab();
let multilingual = vec![
("▁português", 50000, -8.0),
("▁español", 50001, -8.1),
("▁français", 50002, -8.2),
("▁日本語", 50003, -9.0),
("▁中文", 50004, -9.1),
];
for (token, id, score) in multilingual {
vocab.insert(token.to_string(), id);
scores.insert(token.to_string(), score);
}
(vocab, scores)
}
fn create_code_llama_vocab() -> (HashMap<String, u32>, HashMap<String, f32>) {
let (mut vocab, mut scores) = Self::create_llama2_vocab();
let code_tokens = vec![
("▁def", 32000, -6.0), ("▁class", 32001, -6.2),
("▁import", 32002, -6.5), ("▁from", 32003, -6.3),
("▁return", 32004, -6.1), ("▁if", 32005, -5.8),
("▁else", 32006, -6.4), ("▁for", 32007, -5.9),
("▁while", 32008, -6.7), ("▁try", 32009, -7.0),
];
for (token, id, score) in code_tokens {
vocab.insert(token.to_string(), id);
scores.insert(token.to_string(), score);
}
(vocab, scores)
}
pub fn encode(&self, text: &str) -> Vec<u32> {
if text.is_empty() {
return Vec::new();
}
let normalized = self.normalizer.normalize(text).unwrap_or_else(|_| text.to_string());
let pre_tokens = self.pre_tokenizer.pre_tokenize(&normalized).unwrap_or_else(|_| vec![normalized.clone()]);
let mut token_ids = Vec::new();
for word in pre_tokens {
let tokens = self.unigram.tokenize(&word);
for token in tokens {
if let Some(&id) = self.vocab.get(&token) {
token_ids.push(id);
} else {
token_ids.push(self.unk_token_id);
}
}
}
token_ids
}
pub fn encode_with_special(&self, text: &str) -> Vec<u32> {
let mut ids = Vec::new();
if self.add_bos_token {
ids.push(self.bos_token_id);
}
ids.extend(self.encode(text));
if self.add_eos_token {
ids.push(self.eos_token_id);
}
ids
}
pub fn encode_pair(&self, text_a: &str, text_b: &str) -> Vec<u32> {
let mut ids = Vec::new();
if self.add_bos_token {
ids.push(self.bos_token_id);
}
ids.extend(self.encode(text_a));
ids.extend(self.encode(text_b));
if self.add_eos_token {
ids.push(self.eos_token_id);
}
ids
}
pub fn decode(&self, ids: &[u32]) -> Result<String> {
let tokens: Vec<String> = ids
.iter()
.filter_map(|&id| self.id_to_token.get(&id).cloned())
.filter(|token| !self.is_special_token(token))
.collect();
let text = tokens.join("").replace('▁', " ").trim().to_string();
Ok(text)
}
pub fn encode_batch(&self, texts: &[&str]) -> Vec<Vec<u32>> {
texts.iter().map(|text| self.encode(text)).collect()
}
pub fn encode_batch_with_special(&self, texts: &[&str]) -> Vec<Vec<u32>> {
texts.iter().map(|text| self.encode_with_special(text)).collect()
}
pub fn decode_batch(&self, ids_batch: &[Vec<u32>]) -> Result<Vec<String>> {
ids_batch.iter().map(|ids| self.decode(ids)).collect()
}
pub fn tokenize(&self, text: &str) -> Vec<String> {
let ids = self.encode(text);
ids.iter()
.filter_map(|&id| self.id_to_token.get(&id).cloned())
.collect()
}
pub fn is_special_token(&self, token: &str) -> bool {
matches!(token, "<s>" | "</s>" | "<unk>" | "<pad>")
}
pub fn vocab_size(&self) -> usize {
self.vocab.len()
}
pub fn id_to_token(&self, id: u32) -> Option<&str> {
self.id_to_token.get(&id).map(|s| s.as_str())
}
pub fn token_to_id(&self, token: &str) -> Option<u32> {
self.vocab.get(token).copied()
}
pub fn get_vocab(&self) -> &HashMap<String, u32> {
&self.vocab
}
pub fn get_score(&self, token: &str) -> Option<f32> {
self.scores.get(token).copied()
}
pub fn truncate(&self, ids: Vec<u32>, max_length: usize) -> Vec<u32> {
if ids.len() <= max_length {
ids
} else {
ids[..max_length].to_vec()
}
}
pub fn pad(&self, mut ids: Vec<u32>, max_length: usize) -> Vec<u32> {
while ids.len() < max_length {
ids.push(self.pad_token_id);
}
ids
}
pub fn apply_chat_template(&self, messages: &[(&str, &str)]) -> String {
let mut formatted = String::new();
for (role, content) in messages {
match *role {
"system" => {
formatted.push_str(&format!("<<SYS>>\n{}\n<</SYS>>\n\n", content));
}
"user" => {
formatted.push_str(&format!("[INST] {} [/INST] ", content));
}
"assistant" => {
formatted.push_str(&format!("{} ", content));
}
_ => {}
}
}
formatted
}
pub fn apply_chat_template_llama3(&self, messages: &[(&str, &str)]) -> String {
let mut formatted = String::new();
for (role, content) in messages {
formatted.push_str(&format!("<|start_header_id|>{}<|end_header_id|>\n\n{}<|eot_id|>", role, content));
}
formatted
}
pub fn save_vocabulary(&self, vocab_path: &str, _scores_path: &str) -> Result<()> {
use std::fs::File;
use std::io::Write;
let mut items: Vec<_> = self.vocab.iter().collect();
items.sort_by_key(|(_, &id)| id);
let mut file = File::create(vocab_path)?;
for (token, &id) in items {
let score = self.scores.get(token).unwrap_or(&0.0);
writeln!(file, "{}\t{}\t{}", token, id, score)?;
}
Ok(())
}
pub fn get_special_tokens(&self) -> HashMap<String, u32> {
let mut tokens = HashMap::new();
tokens.insert(self.bos_token.clone(), self.bos_token_id);
tokens.insert(self.eos_token.clone(), self.eos_token_id);
tokens.insert(self.unk_token.clone(), self.unk_token_id);
tokens.insert(self.pad_token.clone(), self.pad_token_id);
tokens
}
}
impl std::fmt::Debug for LlamaTokenizer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LlamaTokenizer")
.field("vocab_size", &self.vocab_size())
.field("add_bos_token", &self.add_bos_token)
.field("add_eos_token", &self.add_eos_token)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_llama_tokenizer_basic() {
let tokenizer = LlamaTokenizer::from_pretrained("llama-2-7b").unwrap();
let text = "Hello world";
let ids = tokenizer.encode(text);
assert!(!ids.is_empty());
}
#[test]
fn test_llama_encode_decode() {
let tokenizer = LlamaTokenizer::from_pretrained("llama-2-7b").unwrap();
let text = "Hello";
let ids = tokenizer.encode(text);
let decoded = tokenizer.decode(&ids).unwrap();
assert!(!decoded.is_empty());
}
#[test]
fn test_llama_special_tokens() {
let tokenizer = LlamaTokenizer::from_pretrained("llama-2-7b").unwrap();
let text = "Test";
let ids = tokenizer.encode_with_special(text);
assert_eq!(ids[0], 1); }
#[test]
fn test_llama_chat_template() {
let tokenizer = LlamaTokenizer::from_pretrained("llama-2-7b").unwrap();
let messages = vec![
("system", "You are a helpful assistant"),
("user", "Hello"),
];
let formatted = tokenizer.apply_chat_template(&messages);
assert!(formatted.contains("<<SYS>>"));
assert!(formatted.contains("[INST]"));
}
#[test]
fn test_llama_vocab_size() {
let tokenizer = LlamaTokenizer::from_pretrained("llama-2-7b").unwrap();
assert!(tokenizer.vocab_size() > 250);
}
#[test]
fn test_llama3_tokenizer() {
let tokenizer = LlamaTokenizer::from_pretrained("llama-3-8b").unwrap();
assert!(tokenizer.vocab_size() > 250);
}
}