kute-bpe 0.9.1

Byte Pair Encoding for KUTE: native byte-level BPE and BPE over symbol sequences
Documentation
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
//! BPE over sequences of named symbols instead of bytes: events, opcodes,
//! gates. The base alphabet is whatever symbols the corpus contains; merges
//! become composite tokens such as `[MOTOR_START, CURRENT_HIGH, MOTOR_TRIP]`.
//!
//! This is KUTE's own capability, not a rustbpe feature.
//!
//! A vocabulary is frozen, but the world is not: new event names appear. A
//! symbol outside the alphabet is never approximated. Under the strict
//! policy it is refused; under the escape policy it travels literally in
//! the sequence's escape table, the vocabulary stays as it was, and decoding
//! returns the input exactly. Merges never span an escaped symbol.

use crate::merge::{apply_merges, resumable_merges, train_merges, LearnedMerge, Pair};
use kute_core::{check_sequence, sequence_for, CorpusFingerprint, CorpusSource, Tokenizer};
use kute_types::{
    CanonicalInput, KuteError, Manifest, MergeRule, Modality, Payload, PayloadKind, Provenance, Result,
    TestVector, TokenEntry, TokenKind, TokenPayload, TokenSequence, TokenizerCapabilities, TrainingConfig,
    UnknownPolicy, Vocabulary, KUTE_VERSION, KVF_VERSION, MAX_ESCAPES,
};
use rustc_hash::FxHashMap;
use std::collections::{BTreeMap, BTreeSet};

pub const ALGORITHM: &str = "sequence-bpe";

struct Loaded {
    vocabulary: Vocabulary,
    alphabet: FxHashMap<String, u32>,
    ranks: FxHashMap<Pair, u32>,
}

#[derive(Default)]
pub struct SequenceBpeTokenizer {
    loaded: Option<Loaded>,
}

impl SequenceBpeTokenizer {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn boxed() -> Box<dyn Tokenizer> {
        Box::new(Self::new())
    }

    fn loaded(&self) -> Result<&Loaded> {
        self.loaded.as_ref().ok_or_else(|| KuteError::NotTrained("train or load a vocabulary first".into()))
    }
}

fn encode_symbols(loaded: &Loaded, symbols: &[String]) -> Result<Vec<u32>> {
    let mut ids = Vec::with_capacity(symbols.len());
    for s in symbols {
        ids.push(*loaded.alphabet.get(s).ok_or_else(|| KuteError::UnknownSymbol(s.clone()))?);
    }
    apply_merges(&mut ids, &loaded.ranks);
    Ok(ids)
}

/// Encode with unknown symbols escaped. Returns the ids and the escape
/// table; an escaped symbol gets the id `table length + its index`, and the
/// same symbol always reuses one entry.
fn encode_symbols_open(loaded: &Loaded, symbols: &[String]) -> Result<(Vec<u32>, Vec<String>)> {
    let table = loaded.vocabulary.tokens.len() as u32;
    let mut out = Vec::with_capacity(symbols.len());
    let mut run: Vec<u32> = Vec::new();
    let mut escapes: Vec<String> = Vec::new();
    let mut escape_index: FxHashMap<&str, u32> = FxHashMap::default();
    for s in symbols {
        match loaded.alphabet.get(s) {
            Some(&id) => run.push(id),
            None => {
                if s.is_empty() || s.len() > kute_types::vocab::MAX_SYMBOL_BYTES {
                    return Err(KuteError::InvalidInput("symbols must be 1 to 1024 bytes".into()));
                }
                apply_merges(&mut run, &loaded.ranks);
                out.append(&mut run);
                let index = match escape_index.get(s.as_str()) {
                    Some(&i) => i,
                    None => {
                        if escapes.len() >= MAX_ESCAPES {
                            return Err(KuteError::LimitExceeded(format!("more than {MAX_ESCAPES} distinct unknown symbols in one sequence")));
                        }
                        escapes.push(s.clone());
                        escape_index.insert(s.as_str(), escapes.len() as u32 - 1);
                        escapes.len() as u32 - 1
                    }
                };
                out.push(table + index);
            }
        }
    }
    apply_merges(&mut run, &loaded.ranks);
    out.append(&mut run);
    Ok((out, escapes))
}

impl Tokenizer for SequenceBpeTokenizer {
    fn train(&mut self, corpus: &mut dyn CorpusSource, config: &TrainingConfig) -> Result<Vocabulary> {
        config.validate()?;
        // One pass. Only the window counts, the alphabet and a few sample
        // records are kept, so memory follows the number of distinct
        // windows, not the size of the corpus. Symbols get provisional ids
        // in order of first appearance and are renumbered in sorted order
        // afterwards, which keeps the result independent of that order.
        let mut fingerprint = CorpusFingerprint::new();
        let mut modality = Modality::Events;
        let mut seen: FxHashMap<String, u32> = FxHashMap::default();
        let mut provisional: FxHashMap<Vec<u32>, u64> = FxHashMap::default();
        let mut samples: Vec<Vec<String>> = Vec::new();
        let mut ids: Vec<u32> = Vec::new();
        while let Some(record) = corpus.next_record()? {
            fingerprint.update(&record);
            if let Some(limit) = config.max_corpus_units {
                if fingerprint.units() > limit {
                    return Err(KuteError::LimitExceeded(format!("corpus is larger than the max_corpus_units limit of {limit}")));
                }
            }
            modality = record.modality;
            let Payload::Symbols(symbols) = record.payload else {
                return Err(KuteError::InvalidInput("sequence-bpe takes symbol payloads; use bpe for bytes".into()));
            };
            if samples.len() < 4 && !symbols.is_empty() {
                samples.push(symbols.iter().take(32).cloned().collect());
            }
            ids.clear();
            for s in symbols {
                if s.is_empty() || s.len() > kute_types::vocab::MAX_SYMBOL_BYTES {
                    return Err(KuteError::InvalidInput("symbols must be 1 to 1024 bytes".into()));
                }
                let next = seen.len() as u32;
                ids.push(*seen.entry(s).or_insert(next));
            }
            for window in ids.chunks(config.train_window) {
                match provisional.get_mut(window) {
                    Some(count) => *count += 1,
                    None => {
                        provisional.insert(window.to_vec(), 1);
                    }
                }
            }
        }
        let alphabet_set: BTreeSet<&str> = seen.keys().map(String::as_str).collect();
        if alphabet_set.is_empty() {
            return Err(KuteError::InvalidInput("the corpus contains no symbols".into()));
        }
        let base = alphabet_set.len() as u32;
        let reserved = config.reserved_tokens.len() as u32;
        if config.vocab_size < base + reserved {
            return Err(KuteError::InvalidConfig(format!(
                "the corpus has {base} distinct symbols; vocab_size must be at least {}",
                base + reserved
            )));
        }
        let index: FxHashMap<&str, u32> = alphabet_set.iter().enumerate().map(|(i, s)| (*s, i as u32)).collect();
        let mut renumber = vec![0u32; seen.len()];
        for (symbol, &first) in &seen {
            renumber[first as usize] = index[symbol.as_str()];
        }
        let mut windows: Vec<(Vec<u32>, u64)> =
            provisional.into_iter().map(|(w, c)| (w.into_iter().map(|id| renumber[id as usize]).collect(), c)).collect();
        windows.sort_unstable();
        let counts: Vec<u64> = windows.iter().map(|(_, c)| *c).collect();
        let mut words: Vec<Vec<u32>> = windows.into_iter().map(|(w, _)| w).collect();
        let unique_windows = words.len() as u64;

        let mut lengths: Vec<usize> = vec![1; base as usize];
        let limit = config.max_token_length;
        let max_merges = config.vocab_size - base - reserved;
        let corpus_records = fingerprint.records();
        let corpus_units = fingerprint.units();
        let corpus_fingerprint = fingerprint.finish();

        // Resuming, as in byte BPE: replay the earlier merges, then go on.
        let mut kept: Vec<LearnedMerge> = Vec::new();
        if let Some(prior) = &config.resume_from {
            // An evolved vocabulary keeps its parent's fingerprint but may
            // have symbols after its merges; its ids would not line up.
            let same_alphabet = prior.tokens.len() >= base as usize
                && alphabet_set.iter().zip(&prior.tokens).all(|(s, t)| t.kind == TokenKind::Symbol && t.payload == TokenPayload::Symbols(vec![s.to_string()]));
            if !same_alphabet {
                return Err(KuteError::VocabularyMismatch(format!(
                    "cannot resume from {} {}: its base symbols are not this corpus's",
                    prior.id(),
                    prior.version()
                )));
            }
            kept = resumable_merges(prior, ALGORITHM, config, &corpus_fingerprint, base, max_merges)?;
            let mut ranks: FxHashMap<Pair, u32> = FxHashMap::default();
            for m in &kept {
                ranks.insert((m.left, m.right), m.id);
                lengths.push(lengths[m.left as usize] + lengths[m.right as usize]);
            }
            for w in &mut words {
                apply_merges(w, &ranks);
            }
        }
        let resumed = kept.len() as u32;
        let more = train_merges(words, &counts, base + resumed, max_merges - resumed, config.min_frequency, |l, r| {
            let len = lengths[l as usize] + lengths[r as usize];
            if limit.is_none_or(|m| len <= m) && len <= kute_types::vocab::MAX_TOKEN_SYMBOLS {
                lengths.push(len);
                true
            } else {
                false
            }
        });

        let mut learned = kept;
        learned.extend(more);

        let mut tokens: Vec<TokenEntry> = alphabet_set
            .iter()
            .enumerate()
            .map(|(i, s)| TokenEntry { id: i as u32, kind: TokenKind::Symbol, payload: TokenPayload::Symbols(vec![s.to_string()]), frequency: None })
            .collect();
        let mut rules = Vec::with_capacity(learned.len());
        for m in &learned {
            let mut seq = match &tokens[m.left as usize].payload {
                TokenPayload::Symbols(s) => s.clone(),
                TokenPayload::Bytes(_) => unreachable!("sequence vocabularies hold symbols only"),
            };
            if let TokenPayload::Symbols(s) = &tokens[m.right as usize].payload {
                seq.extend(s.iter().cloned());
            }
            tokens.push(TokenEntry { id: m.id, kind: TokenKind::Merge, payload: TokenPayload::Symbols(seq), frequency: Some(m.count) });
            rules.push(MergeRule { left: m.left, right: m.right, id: m.id });
        }
        for name in &config.reserved_tokens {
            let id = tokens.len() as u32;
            tokens.push(TokenEntry { id, kind: TokenKind::Special, payload: TokenPayload::Symbols(vec![name.clone()]), frequency: None });
        }

        let mut statistics = BTreeMap::new();
        statistics.insert("corpus_records".to_string(), corpus_records);
        statistics.insert("corpus_symbols".to_string(), corpus_units);
        statistics.insert("alphabet_size".to_string(), base as u64);
        statistics.insert("unique_windows".to_string(), unique_windows);
        statistics.insert("merges_learned".to_string(), learned.len() as u64);
        statistics.insert("requested_vocab_size".to_string(), config.vocab_size as u64);

        let vocabulary = Vocabulary {
            manifest: Manifest {
                kvf_version: KVF_VERSION.to_string(),
                id: config.vocabulary_id.clone(),
                version: config.vocabulary_version.clone(),
                algorithm: ALGORITHM.to_string(),
                modality,
                payload_kind: PayloadKind::Symbols,
                lossless: true,
                description: config.description.clone(),
                created: None,
                generator: format!("kute {KUTE_VERSION}"),
            },
            pretokenizer: None,
            adapter: None,
            tokens,
            merges: rules,
            statistics,
            provenance: Provenance {
                corpus_fingerprints: vec![corpus_fingerprint],
                license: config.license.clone(),
                training: Some(serde_json::to_value(config)?),
            },
            test_vectors: Vec::new(),
            representations: Vec::new(),
            integrity: None,
        };
        self.load(vocabulary)?;
        let loaded = self.loaded.as_mut().ok_or_else(|| KuteError::NotTrained("load failed".into()))?;
        let mut vectors = Vec::new();
        for sample in samples.into_iter().filter(|_| config.embed_test_vectors) {
            let ids = encode_symbols(loaded, &sample)?;
            vectors.push(TestVector { input: TokenPayload::Symbols(sample), ids });
        }
        loaded.vocabulary.test_vectors = vectors;
        loaded.vocabulary.seal()?;
        Ok(loaded.vocabulary.clone())
    }

    fn load(&mut self, mut vocabulary: Vocabulary) -> Result<()> {
        vocabulary.validate()?;
        if vocabulary.manifest.algorithm != ALGORITHM {
            return Err(KuteError::VocabularyMismatch(format!(
                "the sequence-bpe tokenizer cannot interpret a {:?} vocabulary",
                vocabulary.manifest.algorithm
            )));
        }
        let mut alphabet: FxHashMap<String, u32> = FxHashMap::default();
        for t in &vocabulary.tokens {
            if t.kind == TokenKind::Symbol {
                let TokenPayload::Symbols(s) = &t.payload else {
                    return Err(KuteError::InvalidVocabulary(format!("symbol token {} has a byte payload", t.id)));
                };
                if s.len() != 1 || alphabet.insert(s[0].clone(), t.id).is_some() {
                    return Err(KuteError::InvalidVocabulary(format!("symbol token {} is not a single, unique symbol", t.id)));
                }
            }
        }
        let mut ranks: FxHashMap<Pair, u32> = FxHashMap::default();
        for m in &vocabulary.merges {
            if ranks.insert((m.left, m.right), m.id).is_some() {
                return Err(KuteError::InvalidVocabulary(format!("pair ({}, {}) has two merge rules", m.left, m.right)));
            }
        }
        if vocabulary.integrity.is_none() {
            vocabulary.seal()?;
        }
        self.loaded = Some(Loaded { vocabulary, alphabet, ranks });
        Ok(())
    }

    fn encode(&self, input: &CanonicalInput) -> Result<TokenSequence> {
        let loaded = self.loaded()?;
        let Payload::Symbols(symbols) = &input.payload else {
            return Err(KuteError::InvalidInput("sequence-bpe takes symbol payloads".into()));
        };
        sequence_for(&loaded.vocabulary, input.modality, encode_symbols(loaded, symbols)?)
    }

    fn encode_open(&self, input: &CanonicalInput, policy: UnknownPolicy) -> Result<TokenSequence> {
        if policy == UnknownPolicy::Strict {
            return self.encode(input);
        }
        let loaded = self.loaded()?;
        let Payload::Symbols(symbols) = &input.payload else {
            return Err(KuteError::InvalidInput("sequence-bpe takes symbol payloads".into()));
        };
        let (ids, escapes) = encode_symbols_open(loaded, symbols)?;
        let mut sequence = sequence_for(&loaded.vocabulary, input.modality, ids)?;
        sequence.escapes = escapes;
        Ok(sequence)
    }

    fn decode(&self, tokens: &TokenSequence) -> Result<CanonicalInput> {
        let loaded = self.loaded()?;
        check_sequence(&loaded.vocabulary, tokens)?;
        let mut out = Vec::with_capacity(tokens.ids.len() * 2);
        let table = loaded.vocabulary.tokens.len();
        for &id in &tokens.ids {
            if id as usize >= table {
                let escaped = tokens.escapes.get(id as usize - table).ok_or(KuteError::UnknownToken(id))?;
                out.push(escaped.clone());
                continue;
            }
            match &loaded.vocabulary.token(id)?.payload {
                TokenPayload::Symbols(s) => out.extend(s.iter().cloned()),
                TokenPayload::Bytes(_) => return Err(KuteError::InvalidVocabulary("byte token in a symbol vocabulary".into())),
            }
        }
        Ok(CanonicalInput { modality: tokens.modality, payload: Payload::Symbols(out) })
    }

    fn vocabulary(&self) -> Option<&Vocabulary> {
        self.loaded.as_ref().map(|l| &l.vocabulary)
    }

    fn capabilities(&self) -> TokenizerCapabilities {
        TokenizerCapabilities {
            algorithm: ALGORITHM.to_string(),
            lossless: true,
            trainable: true,
            deterministic: true,
            accepts: vec![PayloadKind::Symbols],
            rejects_unknown: false,
            notes: "BPE over named symbols. Lossless for the trained alphabet. A symbol outside it is refused under the strict policy and carried literally, without changing the vocabulary, under the escape policy.".to_string(),
        }
    }
}

/// The `generator` an evolved vocabulary carries. The generator is part of
/// the digest, so it names the rule of the evolution step and not the
/// release that ran it: two releases that implement the same rule give the
/// same child and the same digest. It changes only when the rule does.
/// Rule 1 (v0.3.0 to v0.5.0) wrote `kute <release>` here. Rule 2 (v0.6.0)
/// derived the same tokens as rule 3 but copied the parent's whole training
/// record into the child, so each generation carried all earlier ones; rule
/// 3 records the parent by digest only. Provenance is part of the digest,
/// so the two rules give different children and have different names.
pub const EVOLVE_GENERATOR: &str = "kute evolve-3";

/// Most additions one evolution step accepts.
pub const MAX_ADDITIONS: usize = 65_536;

/// Derive the next version of a sequence vocabulary by adding symbols and
/// symbol sequences (development directive v0.2, vocabulary evolution).
///
/// The parent is not modified. Every token of the parent keeps its id and
/// payload, so the child's table extends the parent's. New single symbols
/// are appended as symbol tokens; a sequence is first tokenized by the merges
/// already present and the tokens that remain are joined by new merges, each
/// applied to the whole sequence before the next is chosen, so the sequence
/// on its own encodes as one token under the child. The result is a pure
/// function of the parent, `additions` and `version`: two parties that
/// apply the same step hold the same digest.
///
/// Encoding can differ between parent and child, since the child knows more
/// merges. A sequence encoded under the parent still names the parent's
/// digest; re-encode it to move it to the child.
pub fn evolve(parent: &Vocabulary, additions: &[Vec<String>], version: kute_types::Version) -> Result<Vocabulary> {
    parent.validate()?;
    if parent.manifest.algorithm != ALGORITHM {
        return Err(KuteError::Unsupported(format!("evolution is defined for {ALGORITHM} vocabularies, not {:?}", parent.manifest.algorithm)));
    }
    if version <= parent.manifest.version {
        return Err(KuteError::InvalidConfig(format!("the new version {version} must be greater than {}", parent.manifest.version)));
    }
    if additions.is_empty() || additions.len() > MAX_ADDITIONS {
        return Err(KuteError::InvalidConfig(format!("an evolution step takes 1 to {MAX_ADDITIONS} additions")));
    }
    let mut child = parent.clone();
    child.integrity = None;
    child.manifest.version = version;
    child.manifest.generator = EVOLVE_GENERATOR.to_string();
    let mut by_symbol: FxHashMap<String, u32> = FxHashMap::default();
    let mut by_pair: FxHashMap<Pair, u32> = FxHashMap::default();
    for t in &child.tokens {
        if let (TokenKind::Symbol, TokenPayload::Symbols(s)) = (t.kind, &t.payload) {
            by_symbol.insert(s[0].clone(), t.id);
        }
    }
    for m in &child.merges {
        by_pair.insert((m.left, m.right), m.id);
    }
    // Reserved tokens are not symbols of the alphabet; a symbol of the same
    // name would be ambiguous to a reader.
    let reserved: BTreeSet<&str> = parent
        .tokens
        .iter()
        .filter(|t| t.kind == TokenKind::Special)
        .filter_map(|t| match &t.payload {
            TokenPayload::Symbols(s) => Some(s[0].as_str()),
            TokenPayload::Bytes(_) => None,
        })
        .collect();
    let before = child.tokens.len();
    for addition in additions {
        if addition.is_empty() || addition.len() > kute_types::vocab::MAX_TOKEN_SYMBOLS {
            return Err(KuteError::InvalidInput(format!("an addition must hold 1 to {} symbols", kute_types::vocab::MAX_TOKEN_SYMBOLS)));
        }
        let mut ids = Vec::with_capacity(addition.len());
        for symbol in addition {
            if symbol.is_empty() || symbol.len() > kute_types::vocab::MAX_SYMBOL_BYTES {
                return Err(KuteError::InvalidInput("symbols must be 1 to 1024 bytes".into()));
            }
            if reserved.contains(symbol.as_str()) {
                return Err(KuteError::InvalidInput(format!("{symbol:?} is a reserved token of this vocabulary")));
            }
            let id = match by_symbol.get(symbol) {
                Some(&id) => id,
                None => {
                    let id = child.tokens.len() as u32;
                    child.tokens.push(TokenEntry { id, kind: TokenKind::Symbol, payload: TokenPayload::Symbols(vec![symbol.clone()]), frequency: None });
                    by_symbol.insert(symbol.clone(), id);
                    id
                }
            };
            ids.push(id);
        }
        // Tokenize the addition as the vocabulary so far would, then join
        // what is left, one new merge at a time. A chain built over the bare
        // symbols would seldom apply: the encoder runs the earlier merges
        // first, and they have already joined those symbols into other
        // tokens. Each new merge is applied to the whole addition before the
        // next is chosen, because the encoder will apply it that way too:
        // in A A A A the merge of A A leaves two tokens, not three.
        apply_merges(&mut ids, &by_pair);
        let symbols_of = |tokens: &[TokenEntry], id: u32| match &tokens[id as usize].payload {
            TokenPayload::Symbols(s) => s.clone(),
            TokenPayload::Bytes(_) => Vec::new(),
        };
        while ids.len() > 1 {
            // No pair left in `ids` is a known merge, so this one is new.
            let (left, right) = (ids[0], ids[1]);
            let mut payload = symbols_of(&child.tokens, left);
            payload.extend(symbols_of(&child.tokens, right));
            let id = child.tokens.len() as u32;
            child.tokens.push(TokenEntry { id, kind: TokenKind::Merge, payload: TokenPayload::Symbols(payload), frequency: None });
            child.merges.push(MergeRule { left, right, id });
            by_pair.insert((left, right), id);
            apply_merges(&mut ids, &by_pair);
        }
    }
    let added = child.tokens.len() - before;
    if added == 0 {
        return Err(KuteError::InvalidInput("every addition is already in the vocabulary".into()));
    }
    if child.tokens.len() > kute_types::vocab::MAX_TOKENS {
        return Err(KuteError::LimitExceeded("the evolved vocabulary would exceed the token limit".into()));
    }
    child.statistics.insert("evolved_tokens".to_string(), added as u64);
    child.provenance.training = Some(serde_json::json!({
        // Computed from the parent's content, not read from its file, so
        // the link is true whatever the parent claims about itself.
        "evolved_from": { "version": parent.manifest.version.to_string(), "digest": parent.compute_digest()? },
        "additions": additions,
    }));
    // Expected ids of the parent's test vectors may change under the new
    // merges; recompute them against the child.
    let inputs: Vec<TokenPayload> = std::mem::take(&mut child.test_vectors).into_iter().map(|v| v.input).collect();
    let mut tokenizer = SequenceBpeTokenizer::new();
    tokenizer.load(child)?;
    let loaded = tokenizer.loaded.as_mut().ok_or_else(|| KuteError::NotTrained("load failed".into()))?;
    let mut vectors = Vec::with_capacity(inputs.len());
    for input in inputs {
        if let TokenPayload::Symbols(symbols) = &input {
            let ids = encode_symbols(loaded, symbols)?;
            vectors.push(TestVector { input, ids });
        }
    }
    loaded.vocabulary.test_vectors = vectors;
    loaded.vocabulary.seal()?;
    Ok(loaded.vocabulary.clone())
}