1pub const U8_MAX: u8 = u8::MAX;
24pub const S8_MAX_ABS: i8 = 127;
26
27#[derive(Clone, Copy, Debug, Eq, PartialEq)]
33pub enum KernelTier {
34 Scalar,
36 Autovec,
38 NeonSdot,
40}
41
42impl KernelTier {
43 #[must_use]
45 pub const fn as_str(self) -> &'static str {
46 match self {
47 Self::Scalar => "scalar",
48 Self::Autovec => "autovec",
49 Self::NeonSdot => "neon-sdot",
50 }
51 }
52
53 const fn from_int8(tier: crate::int8::Int8Tier) -> Self {
54 match tier {
55 crate::int8::Int8Tier::Scalar => Self::Scalar,
56 crate::int8::Int8Tier::Autovec => Self::Autovec,
57 crate::int8::Int8Tier::NeonSdot => Self::NeonSdot,
58 }
59 }
60}
61
62#[derive(Clone, Copy, Debug, Eq, PartialEq)]
64pub enum DotContract {
65 U8S8Envelope,
68 S8S8Kernel,
70}
71
72impl DotContract {
73 #[must_use]
75 pub const fn as_str(self) -> &'static str {
76 match self {
77 Self::U8S8Envelope => "u8s8-envelope",
78 Self::S8S8Kernel => "s8s8-kernel",
79 }
80 }
81}
82
83#[derive(Clone, Copy, Debug, Eq, PartialEq)]
85pub enum ExecutionScope {
86 Enrollment,
88 Decode,
90 Prefill,
92 Microdecoder,
94 MicrodecoderVerify,
96 Talker,
98}
99
100impl ExecutionScope {
101 #[must_use]
103 pub const fn as_str(self) -> &'static str {
104 match self {
105 Self::Enrollment => "enrollment",
106 Self::Decode => "decode",
107 Self::Prefill => "prefill",
108 Self::Microdecoder => "microdecoder",
109 Self::MicrodecoderVerify => "microdecoder_verify_seq16",
110 Self::Talker => "talker",
111 }
112 }
113}
114
115#[derive(Clone, Copy, Debug, Eq, PartialEq)]
117pub struct OverflowProofRow {
118 pub id: &'static str,
120 pub scope: ExecutionScope,
122 pub census_tensor: &'static str,
124 pub reduction_k: u32,
126}
127
128pub const OVERFLOW_PROOF_ROWS: &[OverflowProofRow] = &[
134 OverflowProofRow {
135 id: "codec_encoder_global_k8192",
136 scope: ExecutionScope::Enrollment,
137 census_tensor: "encoder.encoder.layers.12.conv.weight",
138 reduction_k: 8192,
139 },
140 OverflowProofRow {
141 id: "codec_decoder_decode_k7168",
142 scope: ExecutionScope::Decode,
143 census_tensor: "decoder.decoder.0.conv.weight",
144 reduction_k: 7168,
145 },
146 OverflowProofRow {
147 id: "speaker_encoder_k4608",
148 scope: ExecutionScope::Enrollment,
149 census_tensor: "speaker_encoder.asp.tdnn.conv.weight",
150 reduction_k: 4608,
151 },
152 OverflowProofRow {
153 id: "microdecoder_step_k3072",
154 scope: ExecutionScope::Microdecoder,
155 census_tensor: "talker.code_predictor.model.layers.0.mlp.down_proj.weight",
156 reduction_k: 3072,
157 },
158 OverflowProofRow {
159 id: "microdecoder_verify_seq16_k3072",
160 scope: ExecutionScope::MicrodecoderVerify,
161 census_tensor: "talker.code_predictor.model.layers.0.mlp.down_proj.weight",
162 reduction_k: 3072,
163 },
164 OverflowProofRow {
165 id: "talker_down_proj_k3072",
166 scope: ExecutionScope::Talker,
167 census_tensor: "talker.model.layers.0.mlp.down_proj.weight",
168 reduction_k: 3072,
169 },
170 OverflowProofRow {
171 id: "text_projection_k2048",
172 scope: ExecutionScope::Prefill,
173 census_tensor: "talker.model.text_embedding.weight",
174 reduction_k: 2048,
175 },
176];
177
178#[derive(Clone, Copy, Debug, Eq, PartialEq)]
180pub struct SelftestCheck {
181 pub row: OverflowProofRow,
183 pub tier: KernelTier,
185 pub contract: DotContract,
187 pub accumulator_i32: Option<i32>,
192 pub reference_i64: i64,
194 pub passed: bool,
196}
197
198#[derive(Clone, Debug, Eq, PartialEq)]
200pub struct SelftestReport {
201 pub dispatched: KernelTier,
203 pub checks: Vec<SelftestCheck>,
205}
206
207impl SelftestReport {
208 #[must_use]
210 pub fn passed(&self) -> bool {
211 self.checks.iter().all(|check| check.passed)
212 }
213}
214
215#[must_use]
221pub fn run_selftest() -> SelftestReport {
222 run_selftest_inner(None)
223}
224
225fn run_selftest_inner(fault_row: Option<&str>) -> SelftestReport {
226 let dispatched = KernelTier::from_int8(crate::int8::autotuned_plan().decode_gemv);
230 let mut checks = Vec::new();
231 for row in OVERFLOW_PROOF_ROWS.iter().copied() {
232 let accumulator_i32 = scalar_all_extreme_dot_i32(row.reduction_k);
234 let reference_i64 = all_extreme_dot_i64(row.reduction_k);
235 let accumulator_i32 = if fault_row == Some(row.id) {
236 accumulator_i32.map(|accumulator| accumulator.saturating_sub(1))
237 } else {
238 accumulator_i32
239 };
240 checks.push(SelftestCheck {
241 row,
242 tier: KernelTier::Scalar,
243 contract: DotContract::U8S8Envelope,
244 accumulator_i32,
245 reference_i64,
246 passed: accumulator_i32
247 .is_some_and(|accumulator| i64::from(accumulator) == reference_i64),
248 });
249
250 let k = row.reduction_k as usize;
253 let positive = vec![crate::int8::Q8_MAX_ABS; k];
254 let negative = vec![-crate::int8::Q8_MAX_ABS; k];
255 let s8_reference_i64 =
256 i64::from(S8_MAX_ABS) * i64::from(S8_MAX_ABS) * i64::from(row.reduction_k);
257 let scalar_positive =
258 crate::int8::dot_i32(&positive, &positive, crate::int8::Int8Tier::Scalar);
259 for tier in crate::int8::Int8Tier::available() {
260 let up = crate::int8::dot_i32(&positive, &positive, tier);
261 let down = crate::int8::dot_i32(&positive, &negative, tier);
262 let up = if fault_row == Some(row.id) {
263 up.saturating_sub(1)
264 } else {
265 up
266 };
267 let passed = i64::from(up) == s8_reference_i64
268 && i64::from(down) == -s8_reference_i64
269 && up == scalar_positive;
270 checks.push(SelftestCheck {
271 row,
272 tier: KernelTier::from_int8(tier),
273 contract: DotContract::S8S8Kernel,
274 accumulator_i32: Some(up),
275 reference_i64: s8_reference_i64,
276 passed,
277 });
278 }
279 }
280 SelftestReport { dispatched, checks }
281}
282
283fn scalar_all_extreme_dot_i32(reduction_k: u32) -> Option<i32> {
284 let term = i32::from(U8_MAX) * i32::from(S8_MAX_ABS);
285 (0..reduction_k).try_fold(0_i32, |accumulator, _| accumulator.checked_add(term))
286}
287
288fn all_extreme_dot_i64(reduction_k: u32) -> i64 {
289 i64::from(U8_MAX) * i64::from(S8_MAX_ABS) * i64::from(reduction_k)
290}
291
292#[cfg(test)]
293mod tests {
294 use super::*;
295
296 const CENSUS: &str = include_str!("../pinned/EXECUTION_CENSUS.json");
299
300 #[test]
301 fn every_deployed_row_equals_its_i64_reference_on_every_tier() {
302 let report = run_selftest();
303 assert!(report.passed(), "{report:#?}");
304
305 let envelope: Vec<_> = report
306 .checks
307 .iter()
308 .filter(|check| check.contract == DotContract::U8S8Envelope)
309 .collect();
310 assert_eq!(envelope.len(), OVERFLOW_PROOF_ROWS.len());
311 for check in &envelope {
312 assert_eq!(check.tier, KernelTier::Scalar, "{}", check.row.id);
313 assert_eq!(
314 check.accumulator_i32.map(i64::from),
315 Some(check.reference_i64),
316 "{}",
317 check.row.id
318 );
319 }
320
321 let tiers = crate::int8::Int8Tier::available();
322 let s8s8: Vec<_> = report
323 .checks
324 .iter()
325 .filter(|check| check.contract == DotContract::S8S8Kernel)
326 .collect();
327 assert_eq!(s8s8.len(), OVERFLOW_PROOF_ROWS.len() * tiers.len());
328 for check in &s8s8 {
329 assert_eq!(
330 check.accumulator_i32.map(i64::from),
331 Some(check.reference_i64),
332 "{} on {}",
333 check.row.id,
334 check.tier.as_str()
335 );
336 }
337
338 assert!(
339 tiers
340 .iter()
341 .any(|tier| KernelTier::from_int8(*tier) == report.dispatched),
342 "dispatched route {:?} is not among the available tiers",
343 report.dispatched
344 );
345 }
346
347 #[test]
348 fn the_sdot_island_is_proven_on_this_silicon_when_present() {
349 if cfg!(all(target_arch = "aarch64", feature = "neon-dotprod"))
352 && crate::int8::neon_sdot_available()
353 {
354 let report = run_selftest();
355 assert!(
356 report.checks.iter().any(|check| {
357 check.tier == KernelTier::NeonSdot
358 && check.contract == DotContract::S8S8Kernel
359 && check.passed
360 }),
361 "FEAT_DotProd reported but no SDOT proof row executed"
362 );
363 }
364 }
365
366 #[test]
367 fn census_binding_rows_are_not_replaced_by_a_stale_talker_only_bound() {
368 for (tensor, reduction_k) in [
369 ("encoder.encoder.layers.12.conv.weight", 8192),
370 ("decoder.decoder.0.conv.weight", 7168),
371 ("speaker_encoder.asp.tdnn.conv.weight", 4608),
372 (
373 "talker.code_predictor.model.layers.0.mlp.down_proj.weight",
374 3072,
375 ),
376 ("talker.model.layers.0.mlp.down_proj.weight", 3072),
377 ("talker.model.text_embedding.weight", 2048),
378 ] {
379 assert!(
380 OVERFLOW_PROOF_ROWS
381 .iter()
382 .any(|row| { row.census_tensor == tensor && row.reduction_k == reduction_k }),
383 "proof row missing for {tensor} K={reduction_k}"
384 );
385 assert!(
386 CENSUS.contains(&format!("\"tensor\": \"{tensor}\"")),
387 "pinned census no longer contains {tensor}; regenerate the proof table"
388 );
389 assert!(
390 CENSUS.split('{').any(|object| {
391 object.contains(&format!("\"tensor\": \"{tensor}\""))
392 && object.contains(&format!("\"k\": {reduction_k}"))
393 }),
394 "pinned census no longer gives {tensor} reduction K={reduction_k}; regenerate the proof table"
395 );
396 }
397 assert!(
398 CENSUS.contains("\"decode_path_binding_row\""),
399 "proof table requires a separately named decode binding"
400 );
401 }
402
403 #[test]
404 fn a_corrupted_route_fails_the_selftest_instead_of_reporting_green() {
405 let report = run_selftest_inner(Some("codec_decoder_decode_k7168"));
406 assert!(
407 !report.passed(),
408 "fault injection must fail the aggregate verdict"
409 );
410 assert!(
411 report
412 .checks
413 .iter()
414 .any(|check| { check.row.id == "codec_decoder_decode_k7168" && !check.passed })
415 );
416 }
417
418 #[test]
419 fn i32_bound_remains_strictly_below_the_widened_limit() {
420 for row in OVERFLOW_PROOF_ROWS {
421 let reference = all_extreme_dot_i64(row.reduction_k);
422 assert!(
423 reference < i64::from(i32::MAX),
424 "{} no longer fits i32: {reference}",
425 row.id
426 );
427 }
428 }
429
430 #[test]
431 fn pinned_census_copy_matches_the_truth_pack_canonical() {
432 let canonical = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
436 .join("../../docs/truth-pack/EXECUTION_CENSUS.json");
437 match std::fs::read_to_string(&canonical) {
438 Ok(bytes) => assert_eq!(
439 bytes, CENSUS,
440 "pinned/EXECUTION_CENSUS.json drifted from the truth-pack canonical; re-copy it"
441 ),
442 Err(_) => eprintln!(
443 "SKIP pinned_census_copy_matches_the_truth_pack_canonical: no repo checkout at {}",
444 canonical.display()
445 ),
446 }
447 }
448}