tokenizers.rs 17.7 KB
Newer Older
1
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Biswa Panda's avatar
Biswa Panda committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// SPDX-License-Identifier: Apache-2.0

//! Tokenizer Tests
//!
//! This module contains tests for the Tokenizer.
//!
//! For each tokenizer we use in production, we should have either a url to or a local copy
//! of either the tokenizer.json or the .model file.
//!
//! For a small set of common prompts, we need to have a hashable representation of the the encoding
//! object. We will precompute the hashes for each of these prompts for each tokenizer and store them
//! in a hashmap. We will then use these hashes to test that the tokenizer is working correctly. This
//! will detect if upstream dependency changes result in different/new behavior.

16
use dynamo_llm::tokenizers::traits::{Decoder, Encoder, Tokenizer};
Neelay Shah's avatar
Neelay Shah committed
17
use dynamo_llm::tokenizers::*;
18
use rstest::rstest;
Biswa Panda's avatar
Biswa Panda committed
19
use std::collections::HashMap;
Nikita's avatar
Nikita committed
20
use std::path::Path;
Biswa Panda's avatar
Biswa Panda committed
21
22
use std::sync::Arc;

23
24
25
26
// ---------------------------------------------------------------------------
// Test data
// ---------------------------------------------------------------------------

Biswa Panda's avatar
Biswa Panda committed
27
28
29
30
31
32
33
const TEST_PROMPTS: [&str; 4] = [
    "deep learning is",
    "Deep learning is",
    "has anyone seen nemo lately",
    "another prompt",
];

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const LONG_TEST_PROMPTS: [(&str, &str); 6] = [
    ("Tell me about the following text.", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."),
    ("Tell me about the following text.", "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."),
    ("Tell me about the following text.", "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt."),
    ("Tell me about the following text.", "Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem."),
    // Note(jthomson04): Ishan asked me to add this one.
    ("Tell me about the following text.", "In the ancient realm of Tennisia, the very magic of the land is drawn from the sport itself. Forehands light the skies, backhands carve the earth, and serves rumble like thunder across kingdoms. At the center of this balance lie four sacred Grand Slam relics: the Sapphire Trophy of Melbourne, the Emerald Chalice of Paris, the Ruby Crown of London, and the Diamond Orb of New York. Together, they keep the game's spirit alive.
    But the relics are scattered, guarded by champions of legendary skill. The first is the Fire King of Clay, ruler of the crimson courts, whose topspin arcs blaze high and heavy, scorching all who dare stand across from him. The second is the Tempest Trickster, master of the baseline fortress, whose footwork and precision can turn back any storm, and whose returns arrive as if pulled by invisible strings. The third is the Shadow-Dancer of the Highlands, a tactician who thrives in the long rallies of twilight, changing pace and spin until opponents lose their rhythm. The fourth and final guardian is a towering Diamond Titan, a net-charging colossus whose volleys shatter the air itself.
    Into this arena of gods steps the Silver-Wristed Knight — a player of impossible grace, whose game is an art form. His quest: to claim each relic not for glory, but to restore harmony to the rankings of the realm.
    He travels across the Kingdom of Clay, where the points stretch like marathons and the air tastes of iron; through the Grasslands of London, where the ball skids low and the margins are razor-thin; over the Hard Courts of the East, where rallies turn into duels of endurance; and finally to the Cathedral of Lights in New York, where night matches burn with fevered energy.
    Each battle is played under enchanted floodlights, the lines patrolled by spectral line judges whose calls are final. The crowd's roar swells with every break point, and the Silver-Wristed Knight's racket glows brightest when the match teeters at deuce. There are moments when doubt grips him — when his serve falters or his touch deserts him — but each challenge teaches a new stroke, culminating in the legendary Forehand of Dawn.
    When the last relic is claimed, he stands not as a conqueror but as a custodian of the game, knowing that rivalries forge the very magic he protects. The balance is restored — until the next season begins."),
    // Emoji stress test
    ("Tell me about the following text.", "😀😃😄😁😆🥹😅😂🤣🥲☺️😊😇🙂🙃😉🤩😎 🤪🥳🤓🙄🤪😵👻")
];

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const MULTIBYTE_TEST_CASES: [&str; 14] = [
    "hello world",
    "deep learning is awesome",
    "The quick brown fox jumps over the lazy dog.",
    "line1\nline2\nline3",
    "你好世界",            // CJK: 3-byte UTF-8 chars
    "😀😃😄😁",            // Emoji: 4-byte UTF-8 chars
    "hello 你好 world 🌍", // Mixed ASCII + CJK + emoji
    "café résumé naïve",   // Latin with diacritics (2-byte UTF-8)
    "こんにちは",          // Japanese hiragana
    "Привет мир",          // Cyrillic
    "مرحبا",               // Arabic (RTL)
    "🧑‍💻👨‍👩‍👧‍👦",                // Emoji ZWJ sequences (complex multi-codepoint)
    "a你b😀c",             // Interleaved single-byte and multi-byte
    "",                    // Empty string
];
Biswa Panda's avatar
Biswa Panda committed
66

67
68
69
70
71
72
73
74
75
76
const STREAM_TEST_CASES: [(&str, &str); 8] = [
    ("hello world", "deep learning is great"),
    ("summarize:", "The quick brown fox jumps over the lazy dog."),
    ("hello world", "你好世界"),
    ("prompt:", "😀😃😄😁"),
    ("translate this:", "hello 你好 world 🌍"),
    ("text:", "café résumé naïve"),
    ("say:", "こんにちは"),
    ("input:", "🧑‍💻👨‍👩‍👧‍👦"),
];
Biswa Panda's avatar
Biswa Panda committed
77

78
79
80
// ---------------------------------------------------------------------------
// Tokenizer paths
// ---------------------------------------------------------------------------
Biswa Panda's avatar
Biswa Panda committed
81

82
83
84
85
86
87
88
89
const TINYLLAMA_TOKENIZER_PATH: &str = "tests/data/sample-models/TinyLlama_v1.1/tokenizer.json";
const MOCK_TIKTOKEN_DIR: &str = "tests/data/sample-models/mock-tiktoken";

fn tinyllama_tokenizer() -> Arc<dyn Tokenizer> {
    Arc::new(
        HuggingFaceTokenizer::from_file(TINYLLAMA_TOKENIZER_PATH)
            .expect("Failed to load HuggingFace tokenizer"),
    )
Biswa Panda's avatar
Biswa Panda committed
90
91
}

92
93
94
95
96
97
98
99
100
fn mock_tiktoken_tokenizer() -> Arc<dyn Tokenizer> {
    let path = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join(MOCK_TIKTOKEN_DIR)
        .join("tiktoken.model");
    Arc::new(
        TikTokenTokenizer::from_file_auto(path.to_str().unwrap())
            .expect("Failed to load tiktoken tokenizer"),
    )
}
Biswa Panda's avatar
Biswa Panda committed
101

102
103
104
// ---------------------------------------------------------------------------
// Parameterized scenario tests — every tokenizer must pass all of these
// ---------------------------------------------------------------------------
Biswa Panda's avatar
Biswa Panda committed
105

106
107
108
109
110
111
112
113
114
#[rstest]
#[case::huggingface(tinyllama_tokenizer())]
#[case::tiktoken(mock_tiktoken_tokenizer())]
fn test_encode_decode_roundtrip(#[case] tokenizer: Arc<dyn Tokenizer>) {
    for &text in TEST_PROMPTS.iter() {
        let encoding = tokenizer
            .encode(text)
            .unwrap_or_else(|e| panic!("Failed to encode '{text}': {e}"));
        assert!(!encoding.token_ids().is_empty());
Biswa Panda's avatar
Biswa Panda committed
115

116
        let decoded: String = tokenizer
117
            .decode(encoding.token_ids(), false)
118
119
            .unwrap_or_else(|e| panic!("Failed to decode '{text}': {e}"))
            .into();
120
        assert_eq!(decoded, text, "Roundtrip failed for: '{text}'");
Biswa Panda's avatar
Biswa Panda committed
121
122
123
    }
}

124
125
126
127
128
129
130
131
#[rstest]
#[case::huggingface(tinyllama_tokenizer())]
#[case::tiktoken(mock_tiktoken_tokenizer())]
fn test_encode_decode_roundtrip_multibyte(#[case] tokenizer: Arc<dyn Tokenizer>) {
    for &text in MULTIBYTE_TEST_CASES.iter() {
        let encoding = tokenizer
            .encode(text)
            .unwrap_or_else(|e| panic!("Failed to encode '{text}': {e}"));
Biswa Panda's avatar
Biswa Panda committed
132

133
        let decoded: String = tokenizer
134
            .decode(encoding.token_ids(), false)
135
136
            .unwrap_or_else(|e| panic!("Failed to decode '{text}': {e}"))
            .into();
137
138
        assert_eq!(decoded, text, "Roundtrip failed for: '{text}'");
    }
Biswa Panda's avatar
Biswa Panda committed
139
140
}

141
142
143
144
145
146
147
148
149
#[rstest]
#[case::huggingface(tinyllama_tokenizer())]
#[case::tiktoken(mock_tiktoken_tokenizer())]
fn test_batch_encode_roundtrip(#[case] tokenizer: Arc<dyn Tokenizer>) {
    let inputs = &["hello", "world", "deep learning"];
    let encodings = tokenizer
        .encode_batch(inputs)
        .expect("Failed to batch encode");
    assert_eq!(encodings.len(), inputs.len());
Biswa Panda's avatar
Biswa Panda committed
150

151
    for (encoding, &input) in encodings.iter().zip(inputs.iter()) {
152
        let decoded: String = tokenizer
153
            .decode(encoding.token_ids(), false)
154
155
            .expect("Failed to decode")
            .into();
156
157
158
        assert_eq!(decoded, input);
    }
}
Biswa Panda's avatar
Biswa Panda committed
159

160
161
162
163
164
165
#[rstest]
#[case::huggingface(tinyllama_tokenizer())]
#[case::tiktoken(mock_tiktoken_tokenizer())]
fn test_sequence_append_and_decode(#[case] tokenizer: Arc<dyn Tokenizer>) {
    let text = TEST_PROMPTS[0];
    let encoding = tokenizer.encode(text).expect("Failed to encode prompt");
Biswa Panda's avatar
Biswa Panda committed
166

167
168
169
    // Append text and verify token count matches
    let mut sequence = Sequence::new(tokenizer.clone().into());
    sequence.append_text(text).expect("Failed to append prompt");
170
    assert_eq!(sequence.len(), encoding.token_ids().len());
Biswa Panda's avatar
Biswa Panda committed
171

172
173
    // Incremental token-by-token decode via Sequence::append_token_id
    let mut decoder = Sequence::new(tokenizer.clone().into());
Biswa Panda's avatar
Biswa Panda committed
174
    let mut output = String::new();
175
176
177
    for &token_id in encoding.token_ids() {
        let chunk = decoder
            .append_token_id(token_id)
Biswa Panda's avatar
Biswa Panda committed
178
            .expect("Failed to decode token_id");
179
        output.push_str(&chunk);
Biswa Panda's avatar
Biswa Panda committed
180
181
182
183
    }

    assert_eq!(decoder.len(), sequence.len());
    assert_eq!(decoder.token_ids(), sequence.token_ids());
184
185
    assert_eq!(output, text);
}
Biswa Panda's avatar
Biswa Panda committed
186

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#[rstest]
#[case::huggingface(tinyllama_tokenizer())]
#[case::tiktoken(mock_tiktoken_tokenizer())]
fn test_sequence_roundtrip_multibyte(#[case] tokenizer: Arc<dyn Tokenizer>) {
    // Skip empty string — Sequence doesn't produce output for zero tokens
    for &text in MULTIBYTE_TEST_CASES.iter().filter(|t| !t.is_empty()) {
        let encoding = tokenizer
            .encode(text)
            .unwrap_or_else(|e| panic!("Failed to encode '{text}': {e}"));

        let mut sequence = Sequence::new(tokenizer.clone().into());
        let mut output = String::new();
        for &token_id in encoding.token_ids() {
            let chunk = sequence
                .append_token_id(token_id)
                .unwrap_or_else(|e| panic!("append_token_id failed for '{text}': {e}"));
            output.push_str(&chunk);
Biswa Panda's avatar
Biswa Panda committed
204
        }
205
        assert_eq!(output, text, "Sequence roundtrip failed for: '{text}'");
Biswa Panda's avatar
Biswa Panda committed
206
207
    }
}
208

209
210
211
212
213
214
#[rstest]
#[case::huggingface(tinyllama_tokenizer())]
#[case::tiktoken(mock_tiktoken_tokenizer())]
fn test_decode_stream_basic(#[case] tokenizer: Arc<dyn Tokenizer>) {
    let text = TEST_PROMPTS[0];
    let encoding = tokenizer.encode(text).expect("Failed to encode prompt");
215

216
217
218
219
220
221
222
223
224
    let mut stream = DecodeStream::new(tokenizer.clone(), &[], false);
    let mut output = String::new();
    for &token_id in encoding.token_ids() {
        if let Some(chunk) = stream.step(token_id).expect("Failed to decode token_id") {
            output.push_str(&chunk);
        }
    }
    assert_eq!(output, text);
}
225

226
227
228
229
230
231
#[rstest]
#[case::huggingface(tinyllama_tokenizer())]
#[case::tiktoken(mock_tiktoken_tokenizer())]
fn test_decode_stream_with_prefill(#[case] tokenizer: Arc<dyn Tokenizer>) {
    for &(input_text, output_text) in LONG_TEST_PROMPTS.iter() {
        let input_encoding = tokenizer
232
            .encode(input_text)
233
            .unwrap_or_else(|e| panic!("Failed to encode prompt '{input_text}': {e}"));
234

235
        let output_encoding = tokenizer
236
            .encode(output_text)
237
            .unwrap_or_else(|e| panic!("Failed to encode output '{output_text}': {e}"));
238

239
        let mut stream = DecodeStream::new(tokenizer.clone(), input_encoding.token_ids(), false);
240
241

        let mut output = String::new();
242
243
244
245
246
247
        for &token_id in output_encoding.token_ids() {
            if let Some(chunk) = stream
                .step(token_id)
                .unwrap_or_else(|e| panic!("DecodeStream::step failed for '{output_text}': {e}"))
            {
                output.push_str(&chunk);
248
249
250
251
252
253
            }
        }

        assert_eq!(output.trim(), output_text.to_string());
    }
}
254

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#[rstest]
#[case::huggingface(tinyllama_tokenizer())]
#[case::tiktoken(mock_tiktoken_tokenizer())]
fn test_decode_stream_multibyte(#[case] tokenizer: Arc<dyn Tokenizer>) {
    for &(prompt, output_text) in STREAM_TEST_CASES.iter() {
        let prompt_encoding = tokenizer
            .encode(prompt)
            .unwrap_or_else(|e| panic!("Failed to encode prompt '{prompt}': {e}"));

        let output_encoding = tokenizer
            .encode(output_text)
            .unwrap_or_else(|e| panic!("Failed to encode output '{output_text}': {e}"));

        let mut stream = DecodeStream::new(tokenizer.clone(), prompt_encoding.token_ids(), false);

        let mut reassembled = String::new();
        for &token_id in output_encoding.token_ids() {
            if let Some(chunk) = stream
                .step(token_id)
                .unwrap_or_else(|e| panic!("DecodeStream::step failed for '{output_text}': {e}"))
            {
                reassembled.push_str(&chunk);
            }
        }

        assert_eq!(
            reassembled.trim(),
            output_text,
            "DecodeStream roundtrip failed for: '{output_text}'"
        );
    }
}

#[rstest]
#[case::huggingface(tinyllama_tokenizer())]
#[case::tiktoken(mock_tiktoken_tokenizer())]
fn test_hash_determinism(#[case] tokenizer: Arc<dyn Tokenizer>) {
    let prompts = &["hello world", "deep learning", "another prompt"];
    let hashes1 = compute_hashes_for_tokenizer(tokenizer.as_ref(), prompts);
    let hashes2 = compute_hashes_for_tokenizer(tokenizer.as_ref(), prompts);
    assert_eq!(hashes1, hashes2, "Hashes should be deterministic");
    assert!(hashes1.iter().all(|&h| h != 0), "Hashes should be non-zero");
}

// ---------------------------------------------------------------------------
// Tokenizer-specific tests (not parameterized)
// ---------------------------------------------------------------------------

fn compute_hashes_for_tokenizer<E: Encoder + ?Sized>(tokenizer: &E, prompts: &[&str]) -> Vec<u64> {
    prompts
        .iter()
        .map(|&prompt| {
            tokenizer
                .encode(prompt)
                .expect("Failed to encode prompt")
                .get_hash()
        })
        .collect()
}

const HF_TOKENIZERS_LOCAL: [&str; 1] = [TINYLLAMA_TOKENIZER_PATH];

const HASHES: [(&str, [u64; 4]); 1] = [(
    TINYLLAMA_TOKENIZER_PATH,
    [
        1209591529327510910,
        4181375434596349981,
        6245658446118930933,
        5097285695902185237,
    ],
)];

#[test]
fn compute_hashes_hf() {
    let hash_map: HashMap<&str, [u64; 4]> = HASHES.iter().cloned().collect();

    for &tokenizer_name in HF_TOKENIZERS_LOCAL.iter() {
        let tokenizer = HuggingFaceTokenizer::from_file(tokenizer_name)
            .expect("Failed to load HuggingFace tokenizer");

        let prompt_hashes = compute_hashes_for_tokenizer(&tokenizer, &TEST_PROMPTS);

        println!(
            "HF Tokenizer: {:?} Hashes: {:?}",
            tokenizer_name, prompt_hashes
        );

        assert_eq!(prompt_hashes, hash_map[tokenizer_name]);
    }
}

346
347
348
349
350
351
352
353
354
355
356
357
358
359
#[test]
fn test_decode_with_skip_special_tokens() {
    let tokenizer = HuggingFaceTokenizer::from_file(TINYLLAMA_TOKENIZER_PATH)
        .expect("Failed to load remote HuggingFace tokenizer");

    // Create a sequence with special tokens:
    // <s> (token_id: 1) + "Hello world" + </s> (token_id: 2)
    let text = "Hello world";
    let encoding = tokenizer.encode(text).expect("Failed to encode text");
    let mut token_ids = vec![1]; // <s>
    token_ids.extend(encoding.token_ids());
    token_ids.push(2); // </s>

    // Decode with skip_special_tokens = false (should keep special tokens)
360
    let decoded_with_special: String = tokenizer
361
        .decode(&token_ids, false)
362
363
        .expect("Failed to decode with skip_special_tokens=false")
        .into();
364
365

    // Decode with skip_special_tokens = true (should remove special tokens)
366
    let decoded_without_special: String = tokenizer
367
        .decode(&token_ids, true)
368
369
        .expect("Failed to decode with skip_special_tokens=true")
        .into();
370
371
372
373
374

    // Validate exact matches on the entire decoded strings
    assert_eq!(decoded_with_special, "<s> Hello world</s>");
    assert_eq!(decoded_without_special, "Hello world");
}
Nikita's avatar
Nikita committed
375
376
377

#[test]
fn test_tiktoken_create_from_file() {
378
379
380
381
382
    let path = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join(MOCK_TIKTOKEN_DIR)
        .join("tiktoken.model");
    let tokenizer =
        create_tokenizer_from_file(path.to_str().unwrap()).expect("Failed to create tokenizer");
Nikita's avatar
Nikita committed
383
384
385
386
387
388
389
390

    let encoding = tokenizer
        .encode("hello")
        .expect("Failed to encode with factory-created tokenizer");
    assert!(!encoding.token_ids().is_empty());
}

#[test]
391
392
393
394
395
396
fn test_tiktoken_encoding_variant_is_sp() {
    let tokenizer = mock_tiktoken_tokenizer();
    let encoding = tokenizer.encode("hello world").expect("Failed to encode");
    match &encoding {
        Encoding::Sp(_) => {}
        other => panic!("Expected Encoding::Sp, got {:?}", other),
Nikita's avatar
Nikita committed
397
398
    }
}