kute-types 0.2.0

Shared data model for KUTE: identifiers, canonical inputs, token sequences and the vocabulary model
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
//! The in-memory vocabulary model. KVF (see `specifications/kvf`) is the
//! serialization of this structure; `kute-vocab` does the file handling.

use crate::{sha256_tagged, KuteError, Modality, PayloadKind, Result, Version, VocabularyId};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// The KVF format version this build reads and writes.
pub const KVF_VERSION: &str = "0.1";

pub const MAX_TOKENS: usize = 4_000_000;
pub const MAX_TOKEN_BYTES: usize = 64 * 1024;
pub const MAX_TOKEN_SYMBOLS: usize = 4096;
pub const MAX_SYMBOL_BYTES: usize = 1024;
pub const MAX_TEST_VECTORS: usize = 1024;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Manifest {
    pub kvf_version: String,
    pub id: VocabularyId,
    pub version: Version,
    /// Name of the algorithm that interprets this vocabulary, e.g. `bpe`.
    pub algorithm: String,
    pub modality: Modality,
    pub payload_kind: PayloadKind,
    /// True when every input the tokenizer accepts decodes back exactly.
    pub lossless: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Optional creation time. Left out by default so training is reproducible
    /// byte for byte.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub created: Option<String>,
    /// Software that wrote the file, e.g. `kute 0.1.0`.
    pub generator: String,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TokenKind {
    /// One of the 256 byte values.
    Byte,
    /// A single Unicode character.
    Char,
    /// A single named symbol from a symbol alphabet.
    Symbol,
    /// A fixed dictionary entry.
    Entry,
    /// A learned token: the concatenation of two earlier tokens.
    Merge,
    /// A reserved token. Never produced by encoding content.
    Special,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TokenPayload {
    /// Raw bytes, base64 in JSON.
    Bytes(#[serde(with = "crate::b64")] Vec<u8>),
    /// An ordered sequence of symbol names.
    Symbols(Vec<String>),
}

impl TokenPayload {
    pub fn kind(&self) -> PayloadKind {
        match self {
            TokenPayload::Bytes(_) => PayloadKind::Bytes,
            TokenPayload::Symbols(_) => PayloadKind::Symbols,
        }
    }

    pub fn len(&self) -> usize {
        match self {
            TokenPayload::Bytes(b) => b.len(),
            TokenPayload::Symbols(s) => s.len(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn concat(&self, other: &TokenPayload) -> Option<TokenPayload> {
        match (self, other) {
            (TokenPayload::Bytes(a), TokenPayload::Bytes(b)) => {
                let mut v = Vec::with_capacity(a.len() + b.len());
                v.extend_from_slice(a);
                v.extend_from_slice(b);
                Some(TokenPayload::Bytes(v))
            }
            (TokenPayload::Symbols(a), TokenPayload::Symbols(b)) => {
                let mut v = Vec::with_capacity(a.len() + b.len());
                v.extend(a.iter().cloned());
                v.extend(b.iter().cloned());
                Some(TokenPayload::Symbols(v))
            }
            _ => None,
        }
    }

    /// Stable content hash of the payload, independent of any token id.
    pub fn content_hash(&self) -> String {
        match self {
            TokenPayload::Bytes(b) => {
                let mut buf = Vec::with_capacity(b.len() + 1);
                buf.push(b'b');
                buf.extend_from_slice(b);
                sha256_tagged(&buf)
            }
            TokenPayload::Symbols(s) => {
                let mut buf = vec![b's'];
                for sym in s {
                    buf.extend_from_slice(&(sym.len() as u64).to_le_bytes());
                    buf.extend_from_slice(sym.as_bytes());
                }
                sha256_tagged(&buf)
            }
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TokenEntry {
    pub id: u32,
    pub kind: TokenKind,
    #[serde(flatten)]
    pub payload: TokenPayload,
    /// Occurrences observed during training, when known.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub frequency: Option<u64>,
}

/// A learned transformation: tokens `left` and `right`, adjacent, become `id`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct MergeRule {
    pub left: u32,
    pub right: u32,
    pub id: u32,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AdapterSpec {
    pub name: String,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub options: BTreeMap<String, String>,
    /// Whether the adapter reconstructs the original bytes exactly.
    pub lossless: bool,
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Provenance {
    /// `sha256:` fingerprints of the training corpus as the tokenizer saw it.
    #[serde(default)]
    pub corpus_fingerprints: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub license: Option<String>,
    /// The training configuration, kept so a run can be repeated.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub training: Option<serde_json::Value>,
}

/// A known input and the ids it must encode to. Shipped inside the vocabulary
/// so any implementation can check itself against the file.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TestVector {
    #[serde(flatten)]
    pub input: TokenPayload,
    pub ids: Vec<u32>,
}

/// Pointer to an independently versioned mapping from token ids to another
/// representation (FR-015). KVF only references mappings; it never embeds them.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RepresentationRef {
    pub mapping_id: String,
    pub mapping_version: String,
    pub kind: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Signature {
    pub key_id: String,
    pub algorithm: String,
    pub signature: String,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Integrity {
    pub algorithm: String,
    pub digest: String,
    /// Reserved. KVF 0.1 defines the field but no signature scheme.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub signatures: Vec<Signature>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Vocabulary {
    pub manifest: Manifest,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pretokenizer: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub adapter: Option<AdapterSpec>,
    pub tokens: Vec<TokenEntry>,
    #[serde(default)]
    pub merges: Vec<MergeRule>,
    #[serde(default)]
    pub statistics: BTreeMap<String, u64>,
    #[serde(default)]
    pub provenance: Provenance,
    #[serde(default)]
    pub test_vectors: Vec<TestVector>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub representations: Vec<RepresentationRef>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub integrity: Option<Integrity>,
}

/// The token envelope from FR-005: one token with everything needed to
/// interpret it outside its vocabulary file.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TokenRecord {
    pub token_id: u32,
    pub vocabulary_id: VocabularyId,
    pub vocabulary_version: Version,
    pub content_hash: String,
    pub symbol_type: TokenKind,
    pub modality: Modality,
    pub payload_encoding: String,
    pub payload: serde_json::Value,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub frequency: Option<u64>,
    pub metadata: BTreeMap<String, serde_json::Value>,
}

impl Vocabulary {
    pub fn id(&self) -> &VocabularyId {
        &self.manifest.id
    }

    pub fn version(&self) -> &Version {
        &self.manifest.version
    }

    pub fn len(&self) -> usize {
        self.tokens.len()
    }

    pub fn is_empty(&self) -> bool {
        self.tokens.is_empty()
    }

    pub fn token(&self, id: u32) -> Result<&TokenEntry> {
        self.tokens.get(id as usize).ok_or(KuteError::UnknownToken(id))
    }

    /// SHA-256 over the canonical JSON of everything except `integrity`.
    pub fn compute_digest(&self) -> Result<String> {
        let mut body = self.clone();
        body.integrity = None;
        Ok(sha256_tagged(&serde_json::to_vec(&body)?))
    }

    /// The digest recorded in the file, or a freshly computed one.
    pub fn digest(&self) -> Result<String> {
        match &self.integrity {
            Some(i) => Ok(i.digest.clone()),
            None => self.compute_digest(),
        }
    }

    /// Record the content digest. Call after the last modification.
    pub fn seal(&mut self) -> Result<()> {
        let digest = self.compute_digest()?;
        self.integrity = Some(Integrity { algorithm: "sha256".into(), digest, signatures: Vec::new() });
        Ok(())
    }

    /// The FR-005 envelope for one token.
    pub fn record(&self, id: u32) -> Result<TokenRecord> {
        let t = self.token(id)?;
        let (encoding, payload) = match &t.payload {
            TokenPayload::Bytes(b) => match std::str::from_utf8(b) {
                Ok(s) => ("utf8", serde_json::Value::String(s.to_string())),
                Err(_) => ("base64", serde_json::Value::String(crate::b64::encode(b))),
            },
            TokenPayload::Symbols(s) => ("json", serde_json::json!(s)),
        };
        let mut metadata = BTreeMap::new();
        metadata.insert("reversible".to_string(), serde_json::json!(self.manifest.lossless));
        metadata.insert("algorithm".to_string(), serde_json::json!(self.manifest.algorithm));
        metadata.insert("length".to_string(), serde_json::json!(t.payload.len()));
        if let Some(rule) = self.merges.iter().find(|m| m.id == id) {
            metadata.insert("merge".to_string(), serde_json::json!([rule.left, rule.right]));
        }
        Ok(TokenRecord {
            token_id: id,
            vocabulary_id: self.manifest.id.clone(),
            vocabulary_version: self.manifest.version.clone(),
            content_hash: t.payload.content_hash(),
            symbol_type: t.kind,
            modality: self.manifest.modality,
            payload_encoding: encoding.to_string(),
            payload,
            frequency: t.frequency,
            metadata,
        })
    }

    /// Structural validation. Everything that loads a vocabulary from outside
    /// the process must call this before using it.
    pub fn validate(&self) -> Result<()> {
        let bad = |why: String| Err(KuteError::InvalidVocabulary(why));
        if self.manifest.kvf_version != KVF_VERSION {
            return Err(KuteError::Unsupported(format!(
                "KVF version {:?}; this build reads {KVF_VERSION:?}",
                self.manifest.kvf_version
            )));
        }
        if self.manifest.algorithm.is_empty() || self.manifest.algorithm.len() > 64 {
            return bad("algorithm name must be 1 to 64 bytes".into());
        }
        if self.tokens.is_empty() {
            return bad("no tokens".into());
        }
        if self.tokens.len() > MAX_TOKENS {
            return bad(format!("more than {MAX_TOKENS} tokens"));
        }
        if let Some(p) = &self.pretokenizer {
            if p.is_empty() || p.len() > 4096 {
                return bad("pretokenizer pattern must be 1 to 4096 bytes".into());
            }
        }
        for (i, t) in self.tokens.iter().enumerate() {
            if t.id as usize != i {
                return bad(format!("token at position {i} has id {}; ids must be dense and ordered", t.id));
            }
            if t.payload.kind() != self.manifest.payload_kind && t.kind != TokenKind::Special {
                return bad(format!("token {i} payload kind differs from the manifest"));
            }
            match &t.payload {
                TokenPayload::Bytes(b) => {
                    if b.is_empty() || b.len() > MAX_TOKEN_BYTES {
                        return bad(format!("token {i} payload must be 1 to {MAX_TOKEN_BYTES} bytes"));
                    }
                }
                TokenPayload::Symbols(s) => {
                    if s.is_empty() || s.len() > MAX_TOKEN_SYMBOLS {
                        return bad(format!("token {i} must hold 1 to {MAX_TOKEN_SYMBOLS} symbols"));
                    }
                    if s.iter().any(|x| x.is_empty() || x.len() > MAX_SYMBOL_BYTES) {
                        return bad(format!("token {i} has an empty or oversized symbol"));
                    }
                }
            }
        }
        let mut has_rule = vec![false; self.tokens.len()];
        for m in &self.merges {
            let n = self.tokens.len() as u32;
            if m.id >= n || m.left >= m.id || m.right >= m.id {
                return bad(format!("merge rule ({}, {}) -> {} refers to a token that is not earlier in the table", m.left, m.right, m.id));
            }
            let target = &self.tokens[m.id as usize];
            if target.kind != TokenKind::Merge {
                return bad(format!("merge rule targets token {} which is not a merge token", m.id));
            }
            if has_rule[m.id as usize] {
                return bad(format!("token {} has more than one merge rule", m.id));
            }
            has_rule[m.id as usize] = true;
            let expected = self.tokens[m.left as usize].payload.concat(&self.tokens[m.right as usize].payload);
            if expected.as_ref() != Some(&target.payload) {
                return bad(format!("token {} payload is not the concatenation of tokens {} and {}", m.id, m.left, m.right));
            }
        }
        for (i, t) in self.tokens.iter().enumerate() {
            if t.kind == TokenKind::Merge && !has_rule[i] {
                return bad(format!("merge token {i} has no merge rule"));
            }
        }
        if self.test_vectors.len() > MAX_TEST_VECTORS {
            return bad(format!("more than {MAX_TEST_VECTORS} test vectors"));
        }
        for (i, v) in self.test_vectors.iter().enumerate() {
            if v.ids.iter().any(|&id| id as usize >= self.tokens.len()) {
                return bad(format!("test vector {i} uses a token id outside the table"));
            }
        }
        if let Some(integrity) = &self.integrity {
            if integrity.algorithm != "sha256" {
                return Err(KuteError::Unsupported(format!("integrity algorithm {:?}", integrity.algorithm)));
            }
            let actual = self.compute_digest()?;
            if actual != integrity.digest {
                return Err(KuteError::Integrity(format!(
                    "recorded digest {} does not match content digest {actual}",
                    integrity.digest
                )));
            }
        }
        Ok(())
    }
}