traits.rs 2.65 KB
Newer Older
1
2
3
4
5
use std::{
    collections::hash_map::DefaultHasher,
    hash::{Hash, Hasher},
};

6
use anyhow::Result;
7
8
9

/// Type alias for token IDs
pub type TokenIdType = u32;
10
11
12
13
14
15
16
17
18

/// Core encoding trait - separate from decoding for modularity
pub trait Encoder: Send + Sync {
    fn encode(&self, input: &str) -> Result<Encoding>;
    fn encode_batch(&self, inputs: &[&str]) -> Result<Vec<Encoding>>;
}

/// Core decoding trait - can be implemented independently
pub trait Decoder: Send + Sync {
19
    fn decode(&self, token_ids: &[TokenIdType], skip_special_tokens: bool) -> Result<String>;
20
21
22
23
24
25
}

/// Combined tokenizer trait
pub trait Tokenizer: Encoder + Decoder {
    fn vocab_size(&self) -> usize;
    fn get_special_tokens(&self) -> &SpecialTokens;
26
27
    fn token_to_id(&self, token: &str) -> Option<TokenIdType>;
    fn id_to_token(&self, id: TokenIdType) -> Option<String>;
28
29
30

    /// Enable downcasting to concrete types
    fn as_any(&self) -> &dyn std::any::Any;
31
32
33
34
35
36
37
38
}

/// Contains the results of tokenizing text: token IDs, string tokens, and their spans
#[derive(Debug, Clone)]
pub enum Encoding {
    /// Hugging Face
    Hf(Box<tokenizers::tokenizer::Encoding>),
    /// Sentence Piece
39
40
41
    Sp(Vec<TokenIdType>),
    /// Tiktoken (for GPT models) - now uses u32 in tiktoken-rs 0.7.0
    Tiktoken(Vec<TokenIdType>),
42
43
44
}

impl Encoding {
45
46
    /// Returns a reference to token IDs - zero-copy operation
    pub fn token_ids(&self) -> &[TokenIdType] {
47
        match self {
48
49
50
            Encoding::Hf(inner) => inner.get_ids(),
            Encoding::Sp(inner) => inner,
            Encoding::Tiktoken(inner) => inner,
51
52
53
        }
    }

54
55
    /// Deprecated: Use token_ids() instead (kept for compatibility)
    #[deprecated(since = "0.1.0", note = "Use token_ids() instead")]
56
    pub fn token_ids_ref(&self) -> &[TokenIdType] {
57
        self.token_ids()
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    }

    /// Get a hash of the token IDs for caching purposes
    pub fn get_hash(&self) -> u64 {
        let mut hasher = DefaultHasher::new();
        self.hash(&mut hasher);
        hasher.finish()
    }
}

/// Hash implementation for Encoding
impl Hash for Encoding {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            Encoding::Hf(inner) => inner.get_ids().hash(state),
            Encoding::Sp(inner) => inner.hash(state),
            Encoding::Tiktoken(inner) => inner.hash(state),
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        }
    }
}

#[derive(Debug, Clone)]
pub struct SpecialTokens {
    pub bos_token: Option<String>,
    pub eos_token: Option<String>,
    pub unk_token: Option<String>,
    pub sep_token: Option<String>,
    pub pad_token: Option<String>,
    pub cls_token: Option<String>,
    pub mask_token: Option<String>,
    pub additional_special_tokens: Vec<String>,
}