README.md 1.74 KB
Newer Older
Biswa Panda's avatar
Biswa Panda committed
1
2
3
# Tokenizers

## Introduction
4
`dynamo-tokenizers` provides efficient, versatile tokenization for NLP workloads. It supports HuggingFace and TikToken tokenizers (plus a FastTokenizer hybrid mode) through a streamlined encoding/decoding API.
Biswa Panda's avatar
Biswa Panda committed
5
6
7
8
9
10
11
12
13
14

## Features
- **Hash Verification**: Ensures tokenization consistency and accuracy across different models.
- **Simple Encoding and Decoding**: Facilitates the conversion of text to token IDs and back.
- **Sequence Management**: Manage sequences of tokens for complex NLP tasks effectively.

## Quick Start

#### HuggingFace Tokenizer
```rust
15
use dynamo_tokenizers::hf::HuggingFaceTokenizer;
Biswa Panda's avatar
Biswa Panda committed
16
17
18
19
20
21
22
23

let hf_tokenizer = HuggingFaceTokenizer::from_file("tests/data/sample-models/TinyLlama_v1.1/tokenizer.json")
    .expect("Failed to load HuggingFace tokenizer");
```

### Encoding and Decoding Text

```rust
24
use dynamo_tokenizers::{HuggingFaceTokenizer, traits::{Encoder, Decoder}};
Biswa Panda's avatar
Biswa Panda committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

let tokenizer = HuggingFaceTokenizer::from_file("tests/data/sample-models/TinyLlama_v1.1/tokenizer.json")
    .expect("Failed to load HuggingFace tokenizer");

let text = "Your sample text here";
let encoding = tokenizer.encode(text)
    .expect("Failed to encode text");

println!("Encoding: {:?}", encoding);

let decoded_text = tokenizer.decode(&encoding.token_ids, false)
    .expect("Failed to decode token IDs");

assert_eq!(text, decoded_text);

// Using the Sequence object for encoding and decoding

42
use dynamo_tokenizers::{Sequence, Tokenizer};
Biswa Panda's avatar
Biswa Panda committed
43
44
45
46
47
48
49
50
51
52
use std::sync::{Arc, RwLock};

let tokenizer = Tokenizer::from(Arc::new(tokenizer));
let mut sequence = Sequence::new(tokenizer.clone());

sequence.append_text("Your sample text here")
    .expect("Failed to append text");

let delta = sequence.append_token_id(1337)
    .expect("Failed to append token_id");
53
```