hf.rs 2.63 KB
Newer Older
Biswa Panda's avatar
Biswa Panda committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use tokenizers::tokenizer::Tokenizer as HfTokenizer;

use super::{
    Encoding, Error, Result, TokenIdType,
20
    traits::{Decoder, Encoder, Tokenizer},
Biswa Panda's avatar
Biswa Panda committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
};

pub struct HuggingFaceTokenizer {
    tokenizer: HfTokenizer,
}

impl HuggingFaceTokenizer {
    pub fn from_file(model_name: &str) -> Result<Self> {
        let tokenizer = HfTokenizer::from_file(model_name)
            .map_err(|err| Error::msg(format!("Error loading tokenizer: {}", err)))?;

        Ok(HuggingFaceTokenizer { tokenizer })
    }

    pub fn from_tokenizer(tokenizer: HfTokenizer) -> Self {
        HuggingFaceTokenizer { tokenizer }
    }
}

impl Encoder for HuggingFaceTokenizer {
    fn encode(&self, input: &str) -> Result<Encoding> {
42
        // This self.tokenizer is the library
Biswa Panda's avatar
Biswa Panda committed
43
44
45
        let encoding = self
            .tokenizer
            .encode(input, false)
46
            .map_err(|err| Error::msg(format!("Error tokenizing input: {err}")))?;
Biswa Panda's avatar
Biswa Panda committed
47

48
        Ok(Encoding::Hf(Box::new(encoding)))
Biswa Panda's avatar
Biswa Panda committed
49
    }
50
51
52
53
54

    fn encode_batch(&self, inputs: &[&str]) -> Result<Vec<Encoding>> {
        let hf_encodings = self
            .tokenizer
            .encode_batch(inputs.to_vec(), false)
55
            .map_err(|err| Error::msg(format!("Error batch tokenizing input: {err}")))?;
56
57
58

        let encodings = hf_encodings
            .into_iter()
59
            .map(|enc| Encoding::Hf(Box::new(enc)))
60
61
62
63
            .collect();

        Ok(encodings)
    }
Biswa Panda's avatar
Biswa Panda committed
64
65
66
67
}

impl Decoder for HuggingFaceTokenizer {
    fn decode(&self, token_ids: &[TokenIdType], skip_special_tokens: bool) -> Result<String> {
68
        // This calls into the library
Biswa Panda's avatar
Biswa Panda committed
69
70
71
        let text = self
            .tokenizer
            .decode(token_ids, skip_special_tokens)
72
            .map_err(|err| Error::msg(format!("Error de-tokenizing input: {err}")))?;
Biswa Panda's avatar
Biswa Panda committed
73
74
75
76
77
78
79
80
81
82
83
84

        Ok(text)
    }
}

impl Tokenizer for HuggingFaceTokenizer {}

impl From<HfTokenizer> for HuggingFaceTokenizer {
    fn from(tokenizer: HfTokenizer) -> Self {
        HuggingFaceTokenizer { tokenizer }
    }
}