#tokenizer #nlp #huggingface #bpe #word-piece

bin+lib tokenizers

Provides an implementation of today's most used tokenizers, with a focus on performances and versatility

22 releases (11 breaking)

0.15.2 Feb 12, 2024
0.15.0 Nov 14, 2023
0.13.3 Apr 5, 2023
0.13.2 Nov 7, 2022
0.5.0 Oct 9, 2019

#17 in Text processing

Download history 12194/week @ 2023-11-07 8631/week @ 2023-11-14 8733/week @ 2023-11-21 9593/week @ 2023-11-28 7945/week @ 2023-12-05 8959/week @ 2023-12-12 7357/week @ 2023-12-19 6068/week @ 2023-12-26 7743/week @ 2024-01-02 10117/week @ 2024-01-09 14140/week @ 2024-01-16 13726/week @ 2024-01-23 15816/week @ 2024-01-30 16313/week @ 2024-02-06 19290/week @ 2024-02-13 17172/week @ 2024-02-20

71,342 downloads per month
Used in 53 crates (44 directly)


18K SLoC

Build GitHub Doc

The core of tokenizers, written in Rust. Provides an implementation of today's most used tokenizers, with a focus on performance and versatility.

What is a Tokenizer

A Tokenizer works as a pipeline, it processes some raw text as input and outputs an Encoding. The various steps of the pipeline are:

  1. The Normalizer: in charge of normalizing the text. Common examples of normalization are the unicode normalization standards, such as NFD or NFKC. More details about how to use the Normalizers are available on the Hugging Face blog
  2. The PreTokenizer: in charge of creating initial words splits in the text. The most common way of splitting text is simply on whitespace.
  3. The Model: in charge of doing the actual tokenization. An example of a Model would be BPE or WordPiece.
  4. The PostProcessor: in charge of post-processing the Encoding to add anything relevant that, for example, a language model would need, such as special tokens.

Loading a pretrained tokenizer from the Hub

use tokenizers::tokenizer::{Result, Tokenizer};

fn main() -> Result<()> {
    # #[cfg(feature = "http")]
    # {
        let tokenizer = Tokenizer::from_pretrained("bert-base-cased", None)?;

        let encoding = tokenizer.encode("Hey there!", false)?;
        println!("{:?}", encoding.get_tokens());
    # }

Deserialization and tokenization example

use tokenizers::tokenizer::{Result, Tokenizer, EncodeInput};
use tokenizers::models::bpe::BPE;

fn main() -> Result<()> {
    let bpe_builder = BPE::from_file("./path/to/vocab.json", "./path/to/merges.txt");
    let bpe = bpe_builder

    let mut tokenizer = Tokenizer::new(bpe);

    let encoding = tokenizer.encode("Hey there!", false)?;
    println!("{:?}", encoding.get_tokens());


Training and serialization example

use tokenizers::decoders::DecoderWrapper;
use tokenizers::models::bpe::{BpeTrainerBuilder, BPE};
use tokenizers::normalizers::{strip::Strip, unicode::NFC, utils::Sequence, NormalizerWrapper};
use tokenizers::pre_tokenizers::byte_level::ByteLevel;
use tokenizers::pre_tokenizers::PreTokenizerWrapper;
use tokenizers::processors::PostProcessorWrapper;
use tokenizers::{AddedToken, Model, Result, TokenizerBuilder};

use std::path::Path;

fn main() -> Result<()> {
    let vocab_size: usize = 100;

    let mut trainer = BpeTrainerBuilder::new()
            AddedToken::from(String::from("<s>"), true),
            AddedToken::from(String::from("<pad>"), true),
            AddedToken::from(String::from("</s>"), true),
            AddedToken::from(String::from("<unk>"), true),
            AddedToken::from(String::from("<mask>"), true),

    let mut tokenizer = TokenizerBuilder::new()
            Strip::new(true, true).into(),

    let pretty = false;
            &mut trainer,
        .save("tokenizer.json", pretty)?;


Additional information

  • tokenizers is designed to leverage CPU parallelism when possible. The level of parallelism is determined by the total number of core/threads your CPU provides but this can be tuned by setting the RAYON_RS_NUM_THREADS environment variable. As an example setting RAYON_RS_NUM_THREADS=4 will allocate a maximum of 4 threads. Please note this behavior may evolve in the future


progressbar: The progress bar visualization is enabled by default. It might be disabled if compilation for certain targets is not supported by the termios dependency of the indicatif progress bar.


~382K SLoC