aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-inference-abi / src / strict.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Strict real-model execution (AIEN 0.1 commissioning gate GB10-4). |
| 17 | //! |
| 18 | //! A production build (the default: cargo feature `dev-fallback` off) fails |
| 19 | //! closed: no reference weights stand in for a missing checkpoint, no silent |
| 20 | //! CPU execution stands in for a GPU backend that was asked for, and a GPU |
| 21 | //! call that falls back to CPU is a hard failure, not a counter tick. A |
| 22 | //! dev/test build (`--features dev-fallback`) keeps the old permissive |
| 23 | //! behaviour and says so in every receipt. |
| 24 | |
| 25 | /// True when this build allows reference weights and software fallbacks. |
| 26 | pub const DEV_FALLBACK: bool = cfg!(feature = "dev-fallback"); |
| 27 | |
| 28 | /// Environment opt-in for dev and CI runs on machines without the GPU: |
| 29 | /// `AIEN_DEV_FALLBACK=1`. Read once per process, recorded in every receipt, |
| 30 | /// and rejected by the acceptance gate, so a permissive run can never pass |
| 31 | /// as a strict one. |
| 32 | pub const DEV_FALLBACK_ENV: &str = "AIEN_DEV_FALLBACK"; |
| 33 | |
| 34 | fn env_opt_in() -> bool { |
| 35 | static OPT_IN: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); |
| 36 | *OPT_IN.get_or_init(|| { |
| 37 | std::env::var(DEV_FALLBACK_ENV) |
| 38 | .map(|v| v == "1" || v.eq_ignore_ascii_case("true")) |
| 39 | .unwrap_or(false) |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | /// True when fallbacks are allowed in this process: the `dev-fallback` |
| 44 | /// feature or the `AIEN_DEV_FALLBACK=1` environment opt-in. |
| 45 | pub fn dev_fallback_active() -> bool { |
| 46 | DEV_FALLBACK || env_opt_in() |
| 47 | } |
| 48 | |
| 49 | /// True when the process must fail closed (the production default). |
| 50 | pub fn production_strict() -> bool { |
| 51 | !dev_fallback_active() |
| 52 | } |
| 53 | |
| 54 | /// Tag every strict refusal carries, so logs and receipts can be grepped. |
| 55 | pub const STRICT_VIOLATION_PREFIX: &str = "STRICT_REAL_MODEL_VIOLATION"; |
| 56 | |
| 57 | /// Build the refusal message for a strict violation. |
| 58 | pub fn violation(what: &str) -> String { |
| 59 | format!("{STRICT_VIOLATION_PREFIX}: {what}; this production build refuses to fall back (a dev/test run sets AIEN_DEV_FALLBACK=1 or builds with --features dev-fallback)") |
| 60 | } |
| 61 | |
| 62 | /// Called by a backend at the moment it would silently run an operation on |
| 63 | /// CPU instead of the GPU it was bound to. In a production build this is |
| 64 | /// fatal: a wrong-or-slow result is worse than no result for the gate. |
| 65 | pub fn fallback_taken(backend: &str, op: &str) { |
| 66 | if production_strict() { |
| 67 | panic!( |
| 68 | "{}", |
| 69 | violation(&format!("{backend} fell back to CPU in {op}")) |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// What a strict acceptance receipt must record (GB10-4). |
| 75 | #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] |
| 76 | pub struct StrictModelReceipt { |
| 77 | pub checkpoint_path: String, |
| 78 | pub checkpoint_sha256: String, |
| 79 | pub tokenizer_path: String, |
| 80 | pub tokenizer_sha256: String, |
| 81 | pub backend_identity: String, |
| 82 | pub model_id: String, |
| 83 | pub model_config: serde_json::Value, |
| 84 | pub fallback_count: u64, |
| 85 | /// Native-versus-reference op listing for the run (None for receipts that predate cut 3a). |
| 86 | #[serde(default)] |
| 87 | pub op_report: Option<crate::native_ops::OpReport>, |
| 88 | pub dev_fallback_build: bool, |
| 89 | pub verdict: String, |
| 90 | } |
| 91 | |
| 92 | impl StrictModelReceipt { |
| 93 | /// The gate: a production build, a native GPU backend and zero fallbacks. |
| 94 | pub fn verify(&mut self) -> Result<(), String> { |
| 95 | let mut problems = Vec::new(); |
| 96 | if self.dev_fallback_build { |
| 97 | problems.push("built with dev-fallback".to_string()); |
| 98 | } |
| 99 | if self.fallback_count != 0 { |
| 100 | problems.push(format!("fallback_count = {}", self.fallback_count)); |
| 101 | } |
| 102 | if !self.backend_identity.contains("sm_121") { |
| 103 | problems.push(format!( |
| 104 | "backend is not native GB10 sm_121: {}", |
| 105 | self.backend_identity |
| 106 | )); |
| 107 | } |
| 108 | if self.checkpoint_sha256.len() != 64 || self.tokenizer_sha256.len() != 64 { |
| 109 | problems.push("checkpoint or tokenizer digest missing".to_string()); |
| 110 | } |
| 111 | if problems.is_empty() { |
| 112 | self.verdict = "PASS".to_string(); |
| 113 | Ok(()) |
| 114 | } else { |
| 115 | self.verdict = format!("FAIL: {}", problems.join("; ")); |
| 116 | Err(violation(&problems.join("; "))) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | #[cfg(test)] |
| 122 | mod tests { |
| 123 | use super::*; |
| 124 | use crate::native_ops::{NativeOpMask, OpAccounting, TensorOp}; |
| 125 | |
| 126 | fn receipt(fallback_count: u64, report: crate::native_ops::OpReport) -> StrictModelReceipt { |
| 127 | StrictModelReceipt { |
| 128 | checkpoint_path: "x".into(), |
| 129 | checkpoint_sha256: "0".repeat(64), |
| 130 | tokenizer_path: "y".into(), |
| 131 | tokenizer_sha256: "0".repeat(64), |
| 132 | backend_identity: "OmegaGb10Backend (sm_121)".into(), |
| 133 | model_id: "m".into(), |
| 134 | model_config: serde_json::json!({}), |
| 135 | fallback_count, |
| 136 | op_report: Some(report), |
| 137 | dev_fallback_build: false, |
| 138 | verdict: String::new(), |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn reference_ops_by_design_pass_the_receipt_gate() { |
| 144 | let acct = OpAccounting::new(NativeOpMask::from_ops(&[TensorOp::MatmulVec])); |
| 145 | acct.record_reference(TensorOp::Rmsnorm); |
| 146 | let mut r = receipt(acct.fallback_count(), acct.report()); |
| 147 | assert!(r.verify().is_ok(), "{}", r.verdict); |
| 148 | assert!(r.op_report.as_ref().unwrap().line().contains("rmsnorm:1")); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn claimed_native_fallback_fails_the_receipt_gate() { |
| 153 | let acct = OpAccounting::new(NativeOpMask::from_ops(&[TensorOp::MatmulVec])); |
| 154 | acct.record_reference(TensorOp::MatmulVec); |
| 155 | let mut r = receipt(acct.fallback_count(), acct.report()); |
| 156 | assert!(r.verify().is_err()); |
| 157 | } |
| 158 | } |