toktier 0.2.0

Rust API preview for TokTier's exact, stateful tokenization runtime
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
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
use std::collections::{BTreeMap, HashMap};
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::{Arc, OnceLock};

use flate2::read::ZlibDecoder;
use serde::Deserialize;
use sha2::{Digest, Sha256};

use crate::{Error, ErrorCode, Result};

pub(crate) const ARTIFACT_MANIFEST_BYTES: &[u8] = include_bytes!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/data/src/toktier/artifacts/tables/artifact_manifest.v1.json"
));
pub(crate) const SUPPORT_REGISTRY_BYTES: &[u8] = include_bytes!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/data/src/toktier/routing/tables/support_registry.v1.json"
));
pub(crate) const SIBLING_ALIASES_BYTES: &[u8] = include_bytes!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/data/src/toktier/artifacts/tables/sibling_aliases.v1.json"
));
pub(crate) const REPAIR_MANIFEST_BYTES: &[u8] = include_bytes!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/data/src/toktier/repair/tables/fast_repair_families.v1.json"
));
pub(crate) const REPAIR_PCLASS_COMPRESSED: &[u8] = include_bytes!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/data/src/toktier/repair/tables/repair_pclass.v1.zlib"
));
pub(crate) const KERNEL_FAMILIES_BYTES: &[u8] = include_bytes!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/data/src/toktier/kernels/tables/kernel_families.v1.json"
));
pub(crate) const PREBUILT_MANIFEST_BYTES: &[u8] = include_bytes!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/data/src/toktier/kernels/prebuilt/build_manifest.json"
));
#[cfg(feature = "prebuilt-gpu")]
pub(crate) const PREBUILT_FATBIN: &[u8] = include_bytes!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/data/src/toktier/kernels/prebuilt/pretok_kernel.fatbin"
));

const ARTIFACT_MANIFEST_SHA256: &str =
    "aae335b469142ea935b0573ed8ce0f2770a512daa6f70e65580875609c6fa417";
const SUPPORT_REGISTRY_SHA256: &str = env!("TOKTIER_SUPPORT_REGISTRY_SHA256");
const SIBLING_ALIASES_SHA256: &str =
    "db979dca87435879c78fdd31fb2eb914e262416d6c2a6cfce0399263de813aac";
const REPAIR_MANIFEST_SHA256: &str =
    "8801781427f98456a7dfce6d9cc8f8ddd8dd4ec31c1ff37b71913d83182f3853";
const KERNEL_FAMILIES_SHA256: &str =
    "40834dc72faf3d5437fe5b60f2d2752b71728ad149327b5658268221aa6f0b9c";
const PREBUILT_MANIFEST_SHA256: &str =
    "9f193e6b42408a09ee0796cbff525e283836389c40a91d67aa4ec1271db8acbe";

/// Content and provenance identity of a verified tokenizer artifact.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ArtifactIdentity {
    pub family: String,
    pub repo_id: String,
    pub revision: String,
    pub tokenizer_sha256: String,
    pub tokenizer_size: u64,
}

/// A local tokenizer whose required bytes were checked against the shipped
/// manifest during this process.
#[derive(Debug, Clone)]
pub struct LocalArtifact {
    identity: ArtifactIdentity,
    directory: PathBuf,
    tokenizer_json: PathBuf,
    bytes: Arc<[u8]>,
}

impl LocalArtifact {
    pub fn identity(&self) -> &ArtifactIdentity {
        &self.identity
    }

    pub fn directory(&self) -> &Path {
        &self.directory
    }

    pub fn tokenizer_json(&self) -> &Path {
        &self.tokenizer_json
    }

    pub(crate) fn bytes(&self) -> Arc<[u8]> {
        Arc::clone(&self.bytes)
    }
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct ArtifactFileRow {
    pub(crate) sha256: String,
    pub(crate) size: u64,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct ArtifactRow {
    pub(crate) repo_id: String,
    pub(crate) revision: String,
    pub(crate) files: BTreeMap<String, ArtifactFileRow>,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct RepairRow {
    pub(crate) family: String,
    pub(crate) artifact_sha256: String,
    pub(crate) margin: usize,
    pub(crate) effective_l_max: usize,
    pub(crate) has_normalizer: bool,
}

#[derive(Debug, Deserialize)]
struct RepairManifest {
    schema: String,
    pclass: PclassRow,
    families: Vec<RepairRow>,
}

#[derive(Debug, Deserialize)]
struct PclassRow {
    raw_sha256: String,
    compressed_sha256: String,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct SupportArtifact {
    pub(crate) family: String,
    pub(crate) artifact_sha256: String,
    #[cfg_attr(not(feature = "jit"), allow(dead_code))]
    pub(crate) oracle_id: String,
    #[cfg_attr(not(feature = "jit"), allow(dead_code))]
    pub(crate) evidence_id: String,
    pub(crate) backends: BTreeMap<String, serde_json::Value>,
}

#[derive(Debug, Deserialize)]
struct SupportRegistry {
    schema_version: u64,
    #[serde(default)]
    runtime_builds: Vec<RuntimeBuildRow>,
    artifacts: Vec<SupportArtifact>,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct RuntimeBuildRow {
    runtime: String,
    source_digest: String,
    build_flags: Vec<String>,
    toolchain: String,
    fast_cpu_source_digest: String,
    native_host_source_digest: String,
    evidence_id: String,
}

#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(not(feature = "prebuilt-gpu"), allow(dead_code))]
pub(crate) struct KernelFamily {
    pub(crate) band: String,
    pub(crate) ruleset: String,
    pub(crate) digits_max: Option<i32>,
    pub(crate) class_table: String,
    #[serde(default)]
    pub(crate) contractions: bool,
}

#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(not(feature = "prebuilt-gpu"), allow(dead_code))]
pub(crate) struct ClassTableRow {
    pub(crate) file: String,
    pub(crate) shape: Vec<usize>,
    pub(crate) dtype: String,
    pub(crate) sha256: String,
    pub(crate) meta_file: Option<String>,
    pub(crate) meta_sha256: Option<String>,
}

#[derive(Debug, Deserialize)]
struct KernelManifest {
    schema: String,
    class_tables: BTreeMap<String, ClassTableRow>,
    bands: BTreeMap<String, KernelBand>,
    families: BTreeMap<String, KernelFamily>,
}

#[derive(Debug, Deserialize)]
struct KernelBand {
    #[serde(default)]
    e2e: bool,
}

#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(not(feature = "prebuilt-gpu"), allow(dead_code))]
pub(crate) struct PrebuiltManifest {
    pub(crate) toolchain: String,
    pub(crate) architectures: BTreeMap<String, serde_json::Value>,
    pub(crate) fatbin: PrebuiltFatbin,
    pub(crate) kernels: BTreeMap<String, String>,
}

#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(not(feature = "prebuilt-gpu"), allow(dead_code))]
pub(crate) struct PrebuiltFatbin {
    pub(crate) size: u64,
    pub(crate) digest: String,
}

#[derive(Debug, Deserialize)]
struct AliasManifest {
    schema_version: u64,
    aliases: Vec<AliasRow>,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct AliasRow {
    pub(crate) repo_id: String,
    pub(crate) revision: String,
    pub(crate) source_sha256: String,
    pub(crate) source_size: u64,
    pub(crate) canonical_family: String,
    pub(crate) canonical_anchor_sha256: String,
    pub(crate) basis: String,
    pub(crate) canonical_packaged: bool,
}

#[derive(Debug)]
#[cfg_attr(not(feature = "prebuilt-gpu"), allow(dead_code))]
pub(crate) struct Registry {
    artifacts: BTreeMap<String, ArtifactRow>,
    repairs: HashMap<String, RepairRow>,
    support: HashMap<String, SupportArtifact>,
    runtime_builds: Vec<RuntimeBuildRow>,
    pub(crate) kernel_families: BTreeMap<String, KernelFamily>,
    pub(crate) class_tables: BTreeMap<String, ClassTableRow>,
    pub(crate) prebuilt: PrebuiltManifest,
    pub(crate) aliases: Vec<AliasRow>,
    pclass: Vec<u8>,
}

impl Registry {
    /// Verify and parse the embedded tables once per process, then share
    /// the result. The embedded bytes are compile-time constants, so the
    /// digest checks, JSON parses, and pclass decompression in
    /// [`Registry::load_embedded`] cannot change between calls; a failure
    /// is replayed with its recorded code and message, matching the
    /// per-call verification behavior this replaces.
    pub(crate) fn load() -> Result<Arc<Self>> {
        static REGISTRY: OnceLock<std::result::Result<Arc<Registry>, (ErrorCode, String)>> =
            OnceLock::new();
        REGISTRY
            .get_or_init(|| {
                Self::load_embedded()
                    .map(Arc::new)
                    .map_err(|error| (error.code(), error.message().to_owned()))
            })
            .clone()
            .map_err(|(code, message)| Error::new(code, message))
    }

    fn load_embedded() -> Result<Self> {
        verify_embedded(
            "artifact manifest",
            ARTIFACT_MANIFEST_BYTES,
            ARTIFACT_MANIFEST_SHA256,
        )?;
        verify_embedded(
            "support registry",
            SUPPORT_REGISTRY_BYTES,
            SUPPORT_REGISTRY_SHA256,
        )?;
        verify_embedded(
            "sibling aliases",
            SIBLING_ALIASES_BYTES,
            SIBLING_ALIASES_SHA256,
        )?;
        verify_embedded(
            "repair manifest",
            REPAIR_MANIFEST_BYTES,
            REPAIR_MANIFEST_SHA256,
        )?;
        verify_embedded(
            "kernel families",
            KERNEL_FAMILIES_BYTES,
            KERNEL_FAMILIES_SHA256,
        )?;
        verify_embedded(
            "prebuilt manifest",
            PREBUILT_MANIFEST_BYTES,
            PREBUILT_MANIFEST_SHA256,
        )?;

        let artifacts: BTreeMap<String, ArtifactRow> =
            parse_json("artifact manifest", ARTIFACT_MANIFEST_BYTES)?;
        let repair: RepairManifest = parse_json("repair manifest", REPAIR_MANIFEST_BYTES)?;
        if repair.schema != "toktier.fast_repair_families.v1" {
            return Err(registry_error("unexpected repair manifest schema"));
        }
        verify_embedded(
            "compressed repair property table",
            REPAIR_PCLASS_COMPRESSED,
            &repair.pclass.compressed_sha256,
        )?;
        let mut decoder = ZlibDecoder::new(REPAIR_PCLASS_COMPRESSED);
        let mut pclass = Vec::with_capacity(0x11_0000);
        decoder.read_to_end(&mut pclass).map_err(|error| {
            Error::new(
                ErrorCode::RegistryInvalid,
                format!("cannot decompress repair property table: {error}"),
            )
        })?;
        if pclass.len() != 0x11_0000 || sha256_hex(&pclass) != repair.pclass.raw_sha256 {
            return Err(registry_error(
                "decompressed repair property table has the wrong identity",
            ));
        }
        let support: SupportRegistry = parse_json("support registry", SUPPORT_REGISTRY_BYTES)?;
        if support.schema_version != 1 {
            return Err(registry_error("unexpected support registry schema"));
        }
        let kernel: KernelManifest = parse_json("kernel families", KERNEL_FAMILIES_BYTES)?;
        if kernel.schema != "toktier.kernel_families.v1" {
            return Err(registry_error("unexpected kernel-family schema"));
        }
        for (family, row) in &kernel.families {
            let band = kernel.bands.get(&row.band).ok_or_else(|| {
                registry_error(format!("kernel family {family} names an unknown band"))
            })?;
            if !kernel.class_tables.contains_key(&row.class_table) {
                return Err(registry_error(format!(
                    "kernel family {family} names an unknown class table"
                )));
            }
            if !band.e2e && family != "kimi_k3" {
                return Err(registry_error(format!(
                    "unexpected non-e2e kernel family {family}"
                )));
            }
        }
        let aliases: AliasManifest = parse_json("sibling aliases", SIBLING_ALIASES_BYTES)?;
        if aliases.schema_version != 1 {
            return Err(registry_error("unexpected sibling-alias schema"));
        }
        let prebuilt: PrebuiltManifest = parse_json("prebuilt manifest", PREBUILT_MANIFEST_BYTES)?;

        let repairs = repair
            .families
            .into_iter()
            .map(|row| (row.family.clone(), row))
            .collect();
        let runtime_builds = support.runtime_builds;
        let support = support
            .artifacts
            .into_iter()
            .map(|row| (row.family.clone(), row))
            .collect();
        Ok(Self {
            artifacts,
            repairs,
            support,
            runtime_builds,
            kernel_families: kernel.families,
            class_tables: kernel.class_tables,
            prebuilt,
            aliases: aliases.aliases,
            pclass,
        })
    }

    pub(crate) fn artifact(&self, family: &str) -> Result<&ArtifactRow> {
        self.artifacts.get(family).ok_or_else(|| {
            Error::new(
                ErrorCode::ArtifactNotFound,
                format!("unknown tokenizer family {family:?}"),
            )
            .with_family(family)
        })
    }

    pub(crate) fn artifacts(&self) -> impl Iterator<Item = (&str, &ArtifactRow)> {
        self.artifacts
            .iter()
            .map(|(family, row)| (family.as_str(), row))
    }

    pub(crate) fn repair(&self, family: &str) -> Option<&RepairRow> {
        self.repairs.get(family)
    }

    pub(crate) fn support(&self, family: &str) -> Option<&SupportArtifact> {
        self.support.get(family)
    }

    pub(crate) fn rust_api_build_certified(&self) -> bool {
        let observed_flags = env!("TOKTIER_RUST_API_BUILD_FLAGS")
            .split('\x1f')
            .map(str::to_owned)
            .collect::<Vec<_>>();
        self.runtime_builds.iter().any(|row| {
            row.runtime == "rust_api"
                && row.source_digest == env!("TOKTIER_RUST_API_SOURCE_SHA256")
                && row.build_flags == observed_flags
                && row.toolchain == env!("TOKTIER_RUST_API_TOOLCHAIN")
                && row.fast_cpu_source_digest == env!("TOKTIER_RUST_API_FAST_CPU_SOURCE_SHA256")
                && row.native_host_source_digest
                    == env!("TOKTIER_RUST_API_NATIVE_HOST_SOURCE_SHA256")
                && !row.evidence_id.is_empty()
        })
    }

    pub(crate) fn pclass(&self) -> Vec<u8> {
        self.pclass.clone()
    }

    pub(crate) fn resolve_repo(&self, repo_id: &str, revision: &str) -> Result<String> {
        if let Some((family, _)) = self
            .artifacts
            .iter()
            .find(|(_, row)| row.repo_id == repo_id && row.revision == revision)
        {
            return Ok(family.clone());
        }
        let alias = self
            .aliases
            .iter()
            .find(|row| row.repo_id == repo_id && row.revision == revision)
            .ok_or_else(|| {
                Error::new(
                    ErrorCode::UncertifiedTokenizer,
                    format!("{repo_id}@{revision} is not in the shipped verified-repository table"),
                )
            })?;
        if !alias.canonical_packaged
            || alias.source_sha256.len() != 64
            || alias.source_size == 0
            || !matches!(
                alias.basis.as_str(),
                "identical"
                    | "identical_source"
                    | "equivalent_canonicalisation"
                    | "equivalent_serialisation"
            )
        {
            return Err(Error::new(
                ErrorCode::UncertifiedTokenizer,
                format!("{repo_id}@{revision} has no admitted canonical execution artifact"),
            ));
        }
        let canonical = self.artifact(&alias.canonical_family)?;
        let file = canonical
            .files
            .get("tokenizer.json")
            .ok_or_else(|| registry_error("canonical artifact does not bind tokenizer.json"))?;
        if file.sha256 != alias.canonical_anchor_sha256 {
            return Err(registry_error("sibling alias canonical anchor drifted"));
        }
        Ok(alias.canonical_family.clone())
    }

    pub(crate) fn verify_local(
        &self,
        family: &str,
        cache_root: &Path,
        explicit: Option<&Path>,
    ) -> Result<LocalArtifact> {
        let row = self.artifact(family)?;
        let file = row.files.get("tokenizer.json").ok_or_else(|| {
            registry_error(format!("artifact {family} does not bind tokenizer.json"))
        })?;
        let directory = explicit.map_or_else(
            || cache_root.join(format!("{}-{}", family, &row.revision[..12])),
            Path::to_path_buf,
        );
        let tokenizer_json = if directory.is_file() {
            directory.clone()
        } else {
            directory.join("tokenizer.json")
        };
        let metadata = fs::symlink_metadata(&tokenizer_json).map_err(|error| {
            Error::new(
                ErrorCode::ArtifactNotFound,
                format!("verified tokenizer artifact is unavailable: {error}"),
            )
            .with_path(&tokenizer_json)
            .with_family(family)
        })?;
        if metadata.file_type().is_symlink() || !metadata.file_type().is_file() {
            return Err(Error::new(
                ErrorCode::ArtifactNotFound,
                "tokenizer artifact must be a regular, non-symlink file",
            )
            .with_path(&tokenizer_json)
            .with_family(family));
        }
        if metadata.len() != file.size {
            return Err(Error::new(
                ErrorCode::ArtifactSizeMismatch,
                format!(
                    "tokenizer.json has {} bytes; manifest requires {}",
                    metadata.len(),
                    file.size
                ),
            )
            .with_path(&tokenizer_json)
            .with_family(family));
        }
        let bytes = fs::read(&tokenizer_json).map_err(|error| {
            Error::new(ErrorCode::Io, error.to_string())
                .with_path(&tokenizer_json)
                .with_family(family)
        })?;
        let observed = sha256_hex(&bytes);
        if observed != file.sha256 {
            return Err(Error::new(
                ErrorCode::ArtifactHashMismatch,
                format!(
                    "tokenizer.json digest mismatch: expected {}, observed {observed}",
                    file.sha256
                ),
            )
            .with_path(&tokenizer_json)
            .with_family(family));
        }
        let artifact_directory = tokenizer_json
            .parent()
            .unwrap_or_else(|| Path::new("."))
            .to_path_buf();
        Ok(LocalArtifact {
            identity: ArtifactIdentity {
                family: family.to_owned(),
                repo_id: row.repo_id.clone(),
                revision: row.revision.clone(),
                tokenizer_sha256: file.sha256.clone(),
                tokenizer_size: file.size,
            },
            directory: artifact_directory,
            tokenizer_json,
            bytes: bytes.into(),
        })
    }
}

fn parse_json<T: for<'de> Deserialize<'de>>(name: &str, bytes: &[u8]) -> Result<T> {
    serde_json::from_slice(bytes).map_err(|error| {
        Error::new(
            ErrorCode::RegistryInvalid,
            format!("cannot parse shipped {name}: {error}"),
        )
    })
}

fn verify_embedded(name: &str, bytes: &[u8], expected: &str) -> Result<()> {
    let observed = sha256_hex(bytes);
    if observed == expected.trim_start_matches("sha256:") {
        Ok(())
    } else {
        Err(registry_error(format!(
            "shipped {name} digest mismatch: expected {expected}, observed {observed}"
        )))
    }
}

pub(crate) fn sha256_hex(bytes: &[u8]) -> String {
    crate::fsutil::hex(&Sha256::digest(bytes))
}

#[cfg_attr(not(feature = "prebuilt-gpu"), allow(dead_code))]
pub(crate) fn domain_sha256_hex(domain: &[u8], bytes: &[u8]) -> String {
    let mut digest = Sha256::new();
    digest.update(domain);
    digest.update(bytes);
    crate::fsutil::hex(&digest.finalize())
}

fn registry_error(message: impl Into<String>) -> Error {
    Error::new(ErrorCode::RegistryInvalid, message)
}