aprender-core 0.70.2

Next-generation machine learning library in pure Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
#[allow(clippy::wildcard_imports)]
use super::*;
use crate::error::{AprenderError, Result};
use serde::Deserialize;
use std::collections::HashMap;

// ============================================================================
// Merge Format Compatibility (Qwen2 string vs Qwen3 array)
// ============================================================================

/// Deserialize BPE merges from either string or array format.
///
/// HuggingFace tokenizer.json uses two merge formats across model generations:
/// - **String format** (Qwen2, GPT-2, LLaMA): `"Ġ Ġ"` — space-separated pair
/// - **Array format** (Qwen3, newer models): `["Ġ", "Ġ"]` — 2-element array
///
/// This deserializer normalizes both to `"first second"` string format.
fn deserialize_merges<'de, D>(deserializer: D) -> std::result::Result<Vec<String>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde::de::{self, SeqAccess, Visitor};
    use std::fmt;

    struct MergesVisitor;

    impl<'de> Visitor<'de> for MergesVisitor {
        type Value = Vec<String>;

        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str("a list of merge rules (strings or 2-element arrays)")
        }

        fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Vec<String>, A::Error>
        where
            A: SeqAccess<'de>,
        {
            let mut merges = Vec::with_capacity(seq.size_hint().unwrap_or(0));

            while let Some(element) = seq.next_element::<MergeEntry>()? {
                match element {
                    MergeEntry::String(s) => merges.push(s),
                    MergeEntry::Array(pair) => {
                        if pair.len() == 2 {
                            merges.push(format!("{} {}", pair[0], pair[1]));
                        } else {
                            return Err(de::Error::custom(format!(
                                "merge array must have exactly 2 elements, got {}",
                                pair.len()
                            )));
                        }
                    }
                }
            }

            Ok(merges)
        }
    }

    /// A single merge entry: either `"a b"` or `["a", "b"]`.
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum MergeEntry {
        String(String),
        Array(Vec<String>),
    }

    deserializer.deserialize_seq(MergesVisitor)
}

impl Qwen2BpeTokenizer {
    // Source of truth: SpecialTokens::qwen2() from special-tokens-registry-v1.yaml
    /// Special token: <|`im_start`|>
    pub const IM_START_ID: u32 = crate::demo::SpecialTokens::qwen2().im_start_id;
    /// Special token: <|`im_end`|>
    pub const IM_END_ID: u32 = crate::demo::SpecialTokens::qwen2().im_end_id;
    /// Special token: <|endoftext|>
    pub const ENDOFTEXT_ID: u32 = crate::demo::SpecialTokens::qwen2().bos_id;

    /// Create a new Qwen2 tokenizer.
    #[must_use]
    pub fn new() -> Self {
        let config = BpeConfig::qwen2();
        let mut base = BpeTokenizer::new(config);

        // Add Qwen2 special tokens
        base.add_special_token("<|endoftext|>", Self::ENDOFTEXT_ID);
        base.add_special_token("<|im_start|>", Self::IM_START_ID);
        base.add_special_token("<|im_end|>", Self::IM_END_ID);

        // Add basic byte vocabulary (will be replaced when loading from file)
        for i in 0..=255u8 {
            if let Some(&c) = base.byte_encoder.get(&i) {
                let token = c.to_string();
                let id = u32::from(i);
                base.vocab.insert(token.clone(), id);
                base.id_to_token.insert(id, token);
            }
        }

        Self {
            base,
            im_start_id: Self::IM_START_ID,
            im_end_id: Self::IM_END_ID,
            endoftext_id: Self::ENDOFTEXT_ID,
        }
    }

    /// Check if token is EOS (end of sequence).
    #[must_use]
    pub fn is_eos(&self, token_id: u32) -> bool {
        token_id == self.im_end_id || token_id == self.endoftext_id
    }

    /// Check if token is BOS (beginning of sequence).
    #[must_use]
    pub fn is_bos(&self, token_id: u32) -> bool {
        token_id == self.im_start_id
    }

    /// Get vocabulary size.
    #[must_use]
    pub fn vocab_size(&self) -> usize {
        crate::demo::Qwen2Config::VOCAB_SIZE
    }

    /// Encode text to token IDs.
    #[must_use]
    pub fn encode(&self, text: &str) -> Vec<u32> {
        contract_pre_encode!();
        contract_pre_roundtrip_encoding!();
        contract_pre_tokenizer_consistency!();
        let result = self.base.encode(text);
        contract_post_tokenizer_consistency!(&result);
        result
    }

    /// Decode token IDs to text.
    #[must_use]
    pub fn decode(&self, ids: &[u32]) -> String {
        self.base.decode(ids)
    }

    /// Format a chat message with Qwen2 template.
    ///
    /// Format: `<|im_start|>role\nmessage<|im_end|>\n`
    #[must_use]
    pub fn format_chat(&self, role: &str, content: &str) -> String {
        format!("<|im_start|>{role}\n{content}<|im_end|>\n")
    }

    /// Format a complete chat conversation.
    ///
    /// # Arguments
    /// * `messages` - List of (role, content) pairs
    ///
    /// # Returns
    /// Formatted conversation string with chat template applied.
    #[must_use]
    pub fn format_conversation(&self, messages: &[(&str, &str)]) -> String {
        let mut result = String::new();
        for (role, content) in messages {
            result.push_str(&self.format_chat(role, content));
        }
        // Add assistant prefix for generation
        result.push_str("<|im_start|>assistant\n");
        result
    }

    /// Get the `im_start` token ID.
    #[must_use]
    pub fn im_start_id(&self) -> u32 {
        self.im_start_id
    }

    /// Get the `im_end` token ID.
    #[must_use]
    pub fn im_end_id(&self) -> u32 {
        self.im_end_id
    }

    /// Load tokenizer from tokenizer.json file path.
    ///
    /// # Arguments
    /// * `path` - Path to tokenizer.json file
    ///
    /// # Returns
    /// Loaded Qwen2 tokenizer with full vocabulary
    ///
    /// # Errors
    /// Returns error if file cannot be read or parsed.
    pub fn from_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
        let json =
            std::fs::read_to_string(path.as_ref()).map_err(|e| AprenderError::FormatError {
                message: format!("Failed to read tokenizer file: {e}"),
            })?;
        Self::from_json(&json)
    }

    /// Load tokenizer from JSON string.
    ///
    /// # Arguments
    /// * `json` - JSON string containing `HuggingFace` tokenizer format
    ///
    /// # Returns
    /// Loaded Qwen2 tokenizer with full vocabulary
    ///
    /// # Errors
    /// Returns error if JSON parsing fails.
    /// Build from vocabulary and merges already in memory — an `.apr`'s embedded
    /// tokenizer, without a sibling `tokenizer.json` on disk.
    ///
    /// The special-token ids are derived from the vocabulary exactly as
    /// [`Self::from_json`] does, so an embedded tokenizer and a sibling one that
    /// carry the same vocabulary produce the same ids.
    ///
    /// # Errors
    /// Returns an error when the vocabulary is empty.
    pub fn from_vocab_merges_data(vocab: &[String], merges: &[(String, String)]) -> Result<Self> {
        let base = load_from_vocab_merges(vocab, merges)?;
        Ok(Self::with_special_ids_from(base))
    }

    /// The special-token derivation shared by every constructor: read the three
    /// chat ids out of the vocabulary, falling back to the compiled-in defaults.
    /// Factored so a second constructor cannot drift from the first.
    fn with_special_ids_from(base: BpeTokenizer) -> Self {
        let im_start_id = base
            .vocab
            .get("<|im_start|>")
            .copied()
            .unwrap_or(Self::IM_START_ID);
        let im_end_id = base
            .vocab
            .get("<|im_end|>")
            .copied()
            .unwrap_or(Self::IM_END_ID);
        let endoftext_id = base
            .vocab
            .get("<|endoftext|>")
            .copied()
            .unwrap_or(Self::ENDOFTEXT_ID);
        Self {
            base,
            im_start_id,
            im_end_id,
            endoftext_id,
        }
    }

    pub fn from_json(json: &str) -> Result<Self> {
        let base = load_from_json(json)?;

        // Find special token IDs from the loaded vocabulary
        let im_start_id = base
            .vocab
            .get("<|im_start|>")
            .copied()
            .unwrap_or(Self::IM_START_ID);
        let im_end_id = base
            .vocab
            .get("<|im_end|>")
            .copied()
            .unwrap_or(Self::IM_END_ID);
        let endoftext_id = base
            .vocab
            .get("<|endoftext|>")
            .copied()
            .unwrap_or(Self::ENDOFTEXT_ID);

        Ok(Self {
            base,
            im_start_id,
            im_end_id,
            endoftext_id,
        })
    }
}

impl Default for Qwen2BpeTokenizer {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Utility Functions
// ============================================================================

/// Create byte to Unicode character mapping.
///
/// GPT-2 uses a specific mapping to avoid issues with certain bytes.
#[must_use]
pub fn bytes_to_unicode() -> (HashMap<u8, char>, HashMap<char, u8>) {
    let mut encoder = HashMap::new();
    let mut decoder = HashMap::new();

    // Printable ASCII + Latin-1 supplement
    let mut n = 0u32;
    for b in 0..=255u8 {
        // Printable characters map to themselves
        let c = if (b'!'..=b'~').contains(&b)
            || (b'\xa1'..=b'\xac').contains(&b)
            || (b'\xae'..=b'\xff').contains(&b)
        {
            char::from(b)
        } else {
            // Non-printable map to Unicode offset
            let c = char::from_u32(256 + n).unwrap_or('?');
            n += 1;
            c
        };

        encoder.insert(b, c);
        decoder.insert(c, b);
    }

    (encoder, decoder)
}

// ============================================================================
// HuggingFace tokenizer.json Parsing Structures
// ============================================================================

/// `HuggingFace` tokenizer.json root structure.
#[derive(Debug, Deserialize)]
struct HfTokenizerJson {
    model: HfModel,
    #[serde(default)]
    added_tokens: Vec<HfAddedToken>,
}

/// BPE model section in tokenizer.json.
///
/// Supports both string-format merges (`"Ġ Ġ"`, Qwen2/GPT-2/LLaMA) and
/// array-format merges (`["Ġ", "Ġ"]`, Qwen3/newer HF models).
#[derive(Debug, Deserialize)]
struct HfModel {
    vocab: HashMap<String, u32>,
    #[serde(deserialize_with = "deserialize_merges")]
    merges: Vec<String>,
}

/// Added token entry.
#[derive(Debug, Deserialize)]
struct HfAddedToken {
    id: u32,
    content: String,
    #[serde(default)]
    special: bool,
}

/// Load tokenizer from `HuggingFace` tokenizer.json format.
///
/// Uses the optimized loading path (GH-378): pre-sized HashMaps, owned-string
/// moves, and fast merge loading. Applies to all vocab sizes via
/// `config_from_vocab_size` dispatch (Qwen2, Whisper, GPT-2, LLaMA).
///
/// # Arguments
/// * `json` - JSON string of tokenizer configuration
///
/// # Returns
/// Loaded tokenizer with vocabulary and merge rules
///
/// # Errors
/// Returns error if parsing fails.
pub fn load_from_json(json: &str) -> Result<BpeTokenizer> {
    if json.is_empty() {
        return Err(AprenderError::FormatError {
            message: "Empty tokenizer JSON".to_string(),
        });
    }

    let hf_tokenizer: HfTokenizerJson =
        serde_json::from_str(json).map_err(|e| AprenderError::FormatError {
            message: format!("Failed to parse tokenizer JSON: {e}"),
        })?;

    let vocab_size = hf_tokenizer.model.vocab.len();
    let merge_count = hf_tokenizer.model.merges.len();
    let config = config_from_vocab_size(vocab_size);

    // Pre-size all HashMaps to avoid rehashing (151K entries = ~15 rehash cycles avoided)
    let mut tokenizer = BpeTokenizer::with_capacity(config, vocab_size, merge_count);

    // Move vocab strings (saves 150K String clones vs borrow+clone)
    load_vocab_owned(&mut tokenizer, hf_tokenizer.model.vocab);

    // Process merges with split_once + owned strings (saves 150K Vec + String allocations)
    load_merges_fast(&mut tokenizer, hf_tokenizer.model.merges);

    load_added_tokens(&mut tokenizer, &hf_tokenizer.added_tokens);

    Ok(tokenizer)
}

/// Determine BPE config based on vocabulary size.
fn config_from_vocab_size(vocab_size: usize) -> BpeConfig {
    if vocab_size > 150_000 {
        BpeConfig::qwen2()
    } else if vocab_size > 50_000 {
        BpeConfig::whisper()
    } else if vocab_size > 40_000 {
        BpeConfig::gpt2()
    } else {
        BpeConfig::llama()
    }
}

/// Load vocabulary entries into a tokenizer (borrow path, used by `load_from_files`).
fn load_vocab_into(tokenizer: &mut BpeTokenizer, vocab: &HashMap<String, u32>) {
    for (token, id) in vocab {
        tokenizer.vocab.insert(token.clone(), *id);
        tokenizer.id_to_token.insert(*id, token.clone());
    }
}

/// Load vocabulary by consuming the deserialized HashMap.
///
/// Moves each String into `vocab` (zero-copy) and clones once for `id_to_token`.
/// Saves 150K String allocations vs the borrow path on Qwen2-scale vocabs.
fn load_vocab_owned(tokenizer: &mut BpeTokenizer, vocab: HashMap<String, u32>) {
    for (token, id) in vocab {
        tokenizer.id_to_token.insert(id, token.clone());
        tokenizer.vocab.insert(token, id); // move, not clone
    }
}

/// Load merge rules by consuming the deserialized Vec.
///
/// Uses `split_once` (no Vec allocation) and `add_merge_owned` (moves strings
/// instead of cloning). Saves ~300K String + 150K Vec allocations on Qwen2.
fn load_merges_fast(tokenizer: &mut BpeTokenizer, merges: Vec<String>) {
    for merge_str in merges {
        if let Some((first, second)) = merge_str.split_once(' ') {
            tokenizer.add_merge_owned(first.to_string(), second.to_string());
        }
    }
}

/// Load added tokens (special and regular) into a tokenizer.
fn load_added_tokens(tokenizer: &mut BpeTokenizer, added_tokens: &[HfAddedToken]) {
    for added in added_tokens {
        if added.special {
            tokenizer.add_special_token(&added.content, added.id);
        } else {
            tokenizer.vocab.insert(added.content.clone(), added.id);
            tokenizer
                .id_to_token
                .insert(added.id, added.content.clone());
        }
    }
}

/// Load tokenizer from vocab.json and merges.txt files.
///
/// # Arguments
/// * `vocab_json` - JSON string of vocabulary mapping (token -> id)
/// * `merges_txt` - Text file with merge rules (one "pair1 pair2" per line)
///
/// # Returns
/// Loaded tokenizer
///
/// # Errors
/// Returns error if parsing fails.
pub fn load_from_files(vocab_json: &str, merges_txt: &str) -> Result<BpeTokenizer> {
    if vocab_json.is_empty() {
        return Err(AprenderError::FormatError {
            message: "Empty vocabulary JSON".to_string(),
        });
    }

    let vocab: HashMap<String, u32> =
        serde_json::from_str(vocab_json).map_err(|e| AprenderError::FormatError {
            message: format!("Failed to parse vocabulary JSON: {e}"),
        })?;

    let config = config_from_vocab_size(vocab.len());
    let mut tokenizer = BpeTokenizer::new(config);

    load_vocab_into(&mut tokenizer, &vocab);
    load_merges_from_text(&mut tokenizer, merges_txt);

    Ok(tokenizer)
}

/// Build a tokenizer from vocabulary and merges ALREADY IN MEMORY.
///
/// `vocab[i]` is the token with id `i` — the layout an `.apr` embeds under
/// `tokenizer.vocabulary`, and the layout GGUF uses. `merges` is the ordered
/// merge table from `tokenizer.merges`.
///
/// WHY THIS EXISTS. `load_from_files` takes the two TEXT forms (`vocab.json`,
/// `merges.txt`), so every caller holding structured data had to serialise it back
/// to text to use the parser — or, as `apr chat` did, give up and report "no
/// tokenizer found" for a model that carries one. An `.apr` with vocab and merges
/// IS a complete tokenizer, and this is where the codebase says so once.
///
/// It reuses `load_from_files`'s own primitives — `config_from_vocab_size`,
/// `load_vocab_into`, `add_merge` — rather than round-tripping through JSON, so
/// there is one parser and not two.
///
/// # Errors
/// Returns an error when `vocab` is empty: a tokenizer with no vocabulary would
/// encode every input to the unknown token, which is worse than refusing.
pub fn load_from_vocab_merges(
    vocab: &[String],
    merges: &[(String, String)],
) -> Result<BpeTokenizer> {
    if vocab.is_empty() {
        return Err(AprenderError::FormatError {
            message: "Empty embedded vocabulary".to_string(),
        });
    }
    let map: HashMap<String, u32> = vocab
        .iter()
        .enumerate()
        .map(|(id, token)| (token.clone(), u32::try_from(id).unwrap_or(u32::MAX)))
        .collect();

    let config = config_from_vocab_size(map.len());
    let mut tokenizer = BpeTokenizer::new(config);
    load_vocab_into(&mut tokenizer, &map);
    for (left, right) in merges {
        tokenizer.add_merge(left, right);
    }

    // CONTROL TOKENS MUST MATCH ATOMICALLY (#3920).
    //
    // `load_from_json` finishes with `load_added_tokens` -> `add_special_token`,
    // which registers a token so the BPE matches it WHOLE. The first version of
    // this function loaded the vocabulary and the merges and stopped there — the
    // control tokens were present in the vocab and nothing told the tokenizer they
    // were atomic, so BPE split them into pieces:
    //
    //     sibling    <|im_start|>user\n…   ->  [151644, 872, 198, …]   18 tokens
    //     embedded   same text             ->  [27, 91, 318, 4906, 91, 29, …]  32
    //                                          "<" "|" "im" "start" "|" ">"
    //
    // A model prompted that way receives no chat-turn structure at all, and on gx10
    // the longer prefill also took `chat` into a CUDA illegal address.
    //
    // THE RULE IS THE SHAPE, not a hardcoded list. A vocabulary entry delimited by
    // `<|` and `|>` is a ChatML control token; enumerating `im_start`/`im_end`/
    // `endoftext` by name would work today and silently miss whatever the next
    // vocabulary adds — which is exactly how the substring dispatch in #3918 came to
    // have three defects. The embedded metadata carries no `added_tokens` list, so
    // the shape is the only signal available.
    for (token, id) in &map {
        if token.starts_with("<|") && token.ends_with("|>") && token.len() > 4 {
            tokenizer.add_special_token(token, *id);
        }
    }

    Ok(tokenizer)
}

/// Parse merge rules from merges.txt format (one "pair1 pair2" per line, skipping comments).
fn load_merges_from_text(tokenizer: &mut BpeTokenizer, merges_txt: &str) {
    for line in merges_txt.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() >= 2 {
            tokenizer.add_merge(parts[0], parts[1]);
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
#[path = "tests.rs"]
mod tests;

#[cfg(test)]
mod embedded_vocab_merges_tests {
    use super::*;

    /// The constructor must produce a tokenizer that ENCODES — which is the whole
    /// point: a vocabulary alone gives a decode-only tokenizer, and `chat` encodes.
    #[test]
    fn vocab_and_merges_build_an_encoding_tokenizer() {
        let vocab: Vec<String> = vec![
            "a".into(),
            "b".into(),
            "ab".into(),
            "<|im_start|>".into(),
            "<|im_end|>".into(),
            "<|endoftext|>".into(),
        ];
        let merges = vec![("a".to_string(), "b".to_string())];
        let tok = Qwen2BpeTokenizer::from_vocab_merges_data(&vocab, &merges)
            .expect("vocab+merges is a complete tokenizer");
        // NOT `vocab_size()`: that reports the CONFIG's field (151936 by default),
        // not the number of tokens loaded, so it is the same for this 6-token
        // fixture and for a full Qwen vocabulary. Assert CONTENT instead.
        assert_eq!(
            tok.decode(&[0]),
            "a",
            "id 0 must be the embedded vocab's first token"
        );
        assert_eq!(
            tok.decode(&[2]),
            "ab",
            "id 2 must be the embedded vocab's third token"
        );
    }

    /// The special ids come from the VOCABULARY, exactly as `from_json` derives
    /// them — so an embedded tokenizer and a sibling one carrying the same
    /// vocabulary cannot disagree about `<|im_start|>`.
    #[test]
    fn special_ids_are_read_from_the_vocabulary_not_defaulted() {
        let mut vocab: Vec<String> = (0..40).map(|i| format!("t{i}")).collect();
        vocab[7] = "<|im_start|>".into();
        vocab[9] = "<|im_end|>".into();
        let tok = Qwen2BpeTokenizer::from_vocab_merges_data(&vocab, &[]).expect("builds");
        assert_eq!(
            tok.im_start_id(),
            7,
            "im_start must come from the vocab, not the default"
        );
        assert_eq!(
            tok.im_end_id(),
            9,
            "im_end must come from the vocab, not the default"
        );
    }

    /// THE TEST THAT WOULD HAVE CAUGHT #3920, and did not exist.
    ///
    /// The original tests asserted the tokenizer LOADS and that `decode` returns the
    /// right strings. Nothing asserted what it ENCODES. So a constructor that never
    /// registered the control tokens passed every one of them while splitting
    /// `<|im_start|>` into `"<" "|" "im" "start" "|" ">"` — measured on a real model
    /// as 32 tokens where the reference tokenizer produced 18.
    ///
    /// The fixture deliberately CONTAINS the pieces, so the BPE genuinely could
    /// split the control token. Without that this assertion would hold for the wrong
    /// reason: a vocabulary with no `<` or `|` cannot split it however the
    /// constructor behaves.
    #[test]
    fn a_control_token_encodes_to_exactly_one_id() {
        let vocab: Vec<String> = vec![
            "<".into(),
            "|".into(),
            "im".into(),
            "start".into(),
            ">".into(),
            "a".into(),
            "b".into(),
            "<|im_start|>".into(),
        ];
        let merges = vec![("a".to_string(), "b".to_string())];
        let tok = Qwen2BpeTokenizer::from_vocab_merges_data(&vocab, &merges).expect("builds");

        let ids = tok.encode("<|im_start|>");
        assert_eq!(
            ids.len(),
            1,
            "a control token must match ATOMICALLY; got {} ids ({ids:?}), which is the \
             BPE splitting it into the pieces this fixture also contains",
            ids.len()
        );
        assert_eq!(ids[0], 7, "and it must be the id the vocabulary gives it");
    }

    /// The OTHER two control tokens, because a fix that special-cased one string
    /// would pass the test above and still break `<|im_end|>`.
    #[test]
    fn every_chatml_control_token_encodes_to_exactly_one_id() {
        let vocab: Vec<String> = vec![
            "<".into(),
            "|".into(),
            "im".into(),
            "start".into(),
            "end".into(),
            "endoftext".into(),
            ">".into(),
            "a".into(),
            "<|im_start|>".into(),
            "<|im_end|>".into(),
            "<|endoftext|>".into(),
        ];
        let tok = Qwen2BpeTokenizer::from_vocab_merges_data(&vocab, &[]).expect("builds");
        for ctl in ["<|im_start|>", "<|im_end|>", "<|endoftext|>"] {
            let ids = tok.encode(ctl);
            assert_eq!(
                ids.len(),
                1,
                "{ctl} must match atomically; got {ids:?} — the fixture contains its \
                 pieces, so this is the BPE splitting it"
            );
        }
    }

    /// THE DIFFERENTIAL, and the assertion whose absence caused #3920.
    ///
    /// Per-token round-trips prove each control token is atomic. They do NOT prove
    /// the embedded tokenizer agrees with the REFERENCE one on ordinary text. This
    /// builds both from the same vocabulary and merges — one through
    /// `from_vocab_merges_data`, one through `from_json` with the control tokens
    /// declared in `added_tokens` as a real `tokenizer.json` does — and requires
    /// IDENTICAL id sequences.
    ///
    /// The real defect was found exactly this way, on a real model: 32 ids against
    /// the reference's 18.
    #[test]
    fn embedded_and_reference_tokenizers_produce_identical_ids() {
        let vocab: Vec<&str> = vec![
            "<",
            "|",
            "im",
            "start",
            "end",
            ">",
            "a",
            "b",
            "ab",
            " ",
            "user",
            "<|im_start|>",
            "<|im_end|>",
        ];
        let owned: Vec<String> = vocab.iter().map(|s| (*s).to_string()).collect();
        let merges = vec![("a".to_string(), "b".to_string())];

        let embedded =
            Qwen2BpeTokenizer::from_vocab_merges_data(&owned, &merges).expect("embedded builds");

        // The same data in HuggingFace `tokenizer.json` shape, with the control
        // tokens declared special — which is what a sibling file carries and what
        // the embedded metadata does NOT.
        let vocab_obj: String = owned
            .iter()
            .enumerate()
            .map(|(i, t)| format!("{}:{i}", serde_json::to_string(t).expect("json")))
            .collect::<Vec<_>>()
            .join(",");
        let json = format!(
            r#"{{"model":{{"vocab":{{{vocab_obj}}},"merges":["a b"]}},"added_tokens":[
                 {{"id":11,"content":"<|im_start|>","special":true}},
                 {{"id":12,"content":"<|im_end|>","special":true}}]}}"#
        );
        let reference = Qwen2BpeTokenizer::from_json(&json).expect("reference builds");

        for text in [
            "<|im_start|>user",
            "ab",
            "<|im_start|>user<|im_end|>",
            "a b ab",
        ] {
            assert_eq!(
                embedded.encode(text),
                reference.encode(text),
                "embedded and reference must agree on {text:?}"
            );
        }
    }

    /// The pieces are still encodable on their own — the registration must not
    /// swallow ordinary text that merely contains the delimiters.
    #[test]
    fn registering_control_tokens_does_not_break_ordinary_text() {
        let vocab: Vec<String> = vec![
            "<".into(),
            "|".into(),
            "im".into(),
            "start".into(),
            ">".into(),
            "a".into(),
            "b".into(),
            "<|im_start|>".into(),
        ];
        let tok = Qwen2BpeTokenizer::from_vocab_merges_data(&vocab, &[]).expect("builds");
        assert!(!tok.encode("ab").is_empty(), "ordinary text still encodes");
        assert!(
            !tok.encode("<").is_empty(),
            "a bare delimiter still encodes"
        );
    }

    /// An empty vocabulary is refused rather than silently producing a tokenizer
    /// that encodes everything to the unknown token.
    #[test]
    fn an_empty_vocabulary_is_refused() {
        assert!(Qwen2BpeTokenizer::from_vocab_merges_data(&[], &[]).is_err());
    }
}