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
GitHub aien-dev/aien-sovereign-core-mv#173 (code only)
Apply it
git checkout -b patch-305fc5420b2bd247 c75c583c1f1576ca2c97654eed7ca6319565d48d curl -fsSL https://interplane.io/drakestapleton/aien-sovereign-core-mv/patches/305fc5420b2bd247.patch | git am
Provenance
github-import 1
aien-dev/aien-sovereign-core-mv
173
befe7cc10c6d57f34f4965b24e3c80e6ff251f87
{"kind":"github-import","owner":"aien-dev","repo":"aien-sovereign-core-mv","number":173,"url":"https://github.com/aien-dev/aien-sovereign-core-mv/pull/173","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:12Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d4b078e6..472b70eb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,6 +48,10 @@ jobs: run: cargo clippy --workspace --all-targets -- -D warnings - name: Run Workspace Unit and Integration Tests + env: + # CPU-only runner: let the GPU-backend tests take the software fallback. + # Recorded in every receipt; the strict real-model gate rejects it. + AIEN_DEV_FALLBACK: "1" run: cargo test --workspace --verbose -- --test-threads=1 - name: Verify Preflight Branch Guard (both contexts) @@ -56,6 +60,8 @@ jobs: - name: Run Required Agent Preflight env: AIEN_PROOF_OFF: '1' + # CI runners have no GB10; the dev fallback is a recorded opt-in here only (see aien-inference-abi::strict) + AIEN_DEV_FALLBACK: '1' # push to main validates an already-merged tree; PRs keep the developer branch policy AIEN_PREFLIGHT_CONTEXT: ${{ (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main' && 'merged-tree' || 'developer' }} run: bash scripts/agent-preflight.sh diff --git a/Cargo.lock b/Cargo.lock index f611b9de..855505ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -135,6 +135,7 @@ dependencies = [ "parking_lot", "serde", "serde_json", + "sha2 0.10.9", "tokio", "tracing", "uuid", diff --git a/crates/aien-cli/src/commands.rs b/crates/aien-cli/src/commands.rs index 91fb0d44..4eb12e09 100644 --- a/crates/aien-cli/src/commands.rs +++ b/crates/aien-cli/src/commands.rs @@ -995,7 +995,7 @@ impl CheckpointPolicy { scan_dirs, require_checkpoint: non_empty("AIEN_REQUIRE_CHECKPOINT") .map(|value| value == "1" || value.eq_ignore_ascii_case("true")) - .unwrap_or(false), + .unwrap_or(aien_inference_abi::strict::production_strict()), } } diff --git a/crates/aien-inference-abi/Cargo.toml b/crates/aien-inference-abi/Cargo.toml index 3c055e07..e327df1a 100644 --- a/crates/aien-inference-abi/Cargo.toml +++ b/crates/aien-inference-abi/Cargo.toml @@ -22,3 +22,8 @@ parking_lot = "0.12" sha2 = "0.10" + +[features] +# Off by default: a production build fails closed (no reference weights, no +# silent CPU fallback behind the GPU backend). Only dev/test builds turn it on. +dev-fallback = [] diff --git a/crates/aien-inference-abi/src/backend.rs b/crates/aien-inference-abi/src/backend.rs index 4e52a580..ff1a6241 100644 --- a/crates/aien-inference-abi/src/backend.rs +++ b/crates/aien-inference-abi/src/backend.rs @@ -83,6 +83,13 @@ pub trait TensorBackend: Send + Sync { /// Human-readable name of the backend implementation. fn name(&self) -> &'static str; + /// How many operations this backend ran on a software fallback instead of + /// the device it was bound to. Zero for pure CPU backends; a production + /// build treats a non-zero count as a strict real-model violation. + fn fallback_count(&self) -> u64 { + 0 + } + /// In-place Root Mean Square Normalization: /// out[i] = x[i] * weight[i] / sqrt(mean(x^2) + eps) fn rmsnorm(&self, out: &mut [f32], x: &[f32], weight: &[f32], eps: f32); diff --git a/crates/aien-inference-abi/src/blackwell_backend.rs b/crates/aien-inference-abi/src/blackwell_backend.rs index 8eae3cbf..66a8f3ee 100644 --- a/crates/aien-inference-abi/src/blackwell_backend.rs +++ b/crates/aien-inference-abi/src/blackwell_backend.rs @@ -252,6 +252,10 @@ impl Drop for BlackwellGb10Backend { } impl TensorBackend for BlackwellGb10Backend { + fn fallback_count(&self) -> u64 { + self.fallback_counter.load(Ordering::Relaxed) + } + fn name(&self) -> &'static str { if self.available { "BlackwellGb10Backend (NVIDIA GB10 sm_121 cuBLAS)" @@ -300,6 +304,7 @@ impl TensorBackend for BlackwellGb10Backend { return; } self.fallback_counter.fetch_add(1, Ordering::Relaxed); + crate::strict::fallback_taken("BlackwellGb10Backend", "tensor op"); } self.fallback.matmul_vec(out, x, weight, out_dim, in_dim); } @@ -328,6 +333,7 @@ impl TensorBackend for BlackwellGb10Backend { return; } self.fallback_counter.fetch_add(1, Ordering::Relaxed); + crate::strict::fallback_taken("BlackwellGb10Backend", "tensor op"); } crate::tensor::matmul_batch(x, weight, out, batch_size, in_dim, out_dim); } @@ -448,6 +454,7 @@ impl TensorBackend for BlackwellGb10Backend { return; } self.fallback_counter.fetch_add(1, Ordering::Relaxed); + crate::strict::fallback_taken("BlackwellGb10Backend", "tensor op"); } self.fallback.paged_attention_batch( @@ -487,6 +494,7 @@ impl TensorBackend for BlackwellGb10Backend { return; } self.fallback_counter.fetch_add(1, Ordering::Relaxed); + crate::strict::fallback_taken("BlackwellGb10Backend", "tensor op"); } self.fallback .compute_logits(logits, hidden, embed_weight, vocab_size, hidden_dim); diff --git a/crates/aien-inference-abi/src/blackwell_batch_executor.rs b/crates/aien-inference-abi/src/blackwell_batch_executor.rs index 172416f2..d954a284 100644 --- a/crates/aien-inference-abi/src/blackwell_batch_executor.rs +++ b/crates/aien-inference-abi/src/blackwell_batch_executor.rs @@ -610,6 +610,7 @@ impl BlackwellBatchExecutor { #[cfg(not(has_blackwell_cuda))] { self.fallback_count.fetch_add(1, Ordering::SeqCst); + crate::strict::fallback_taken("BlackwellBatchExecutor", "batch step"); Ok(()) } } @@ -702,6 +703,7 @@ impl BlackwellBatchExecutor { #[cfg(not(has_blackwell_cuda))] { self.fallback_count.fetch_add(1, Ordering::SeqCst); + crate::strict::fallback_taken("BlackwellBatchExecutor", "batch step"); sampled_tokens.fill(100); } diff --git a/crates/aien-inference-abi/src/lib.rs b/crates/aien-inference-abi/src/lib.rs index 2901c362..92f0b1db 100644 --- a/crates/aien-inference-abi/src/lib.rs +++ b/crates/aien-inference-abi/src/lib.rs @@ -17,6 +17,7 @@ pub mod mojo_backend; pub mod qwen3_coder; pub mod qwen3_moe; pub mod qwen3_serve; +pub mod strict; pub mod tensor; pub mod tensor_abi; pub mod tokenizer; diff --git a/crates/aien-inference-abi/src/strict.rs b/crates/aien-inference-abi/src/strict.rs new file mode 100644 index 00000000..c77aa99c --- /dev/null +++ b/crates/aien-inference-abi/src/strict.rs @@ -0,0 +1,101 @@ +//! Strict real-model execution (AIEN 0.1 commissioning gate GB10-4). +//! +//! A production build (the default: cargo feature `dev-fallback` off) fails +//! closed: no reference weights stand in for a missing checkpoint, no silent +//! CPU execution stands in for a GPU backend that was asked for, and a GPU +//! call that falls back to CPU is a hard failure, not a counter tick. A +//! dev/test build (`--features dev-fallback`) keeps the old permissive +//! behaviour and says so in every receipt. + +/// True when this build allows reference weights and software fallbacks. +pub const DEV_FALLBACK: bool = cfg!(feature = "dev-fallback"); + +/// Environment opt-in for dev and CI runs on machines without the GPU: +/// `AIEN_DEV_FALLBACK=1`. Read once per process, recorded in every receipt, +/// and rejected by the acceptance gate, so a permissive run can never pass +/// as a strict one. +pub const DEV_FALLBACK_ENV: &str = "AIEN_DEV_FALLBACK"; + +fn env_opt_in() -> bool { + static OPT_IN: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); + *OPT_IN.get_or_init(|| { + std::env::var(DEV_FALLBACK_ENV) + .map(|v| v == "1" || v.eq_ignore_ascii_case("true")) + .unwrap_or(false) + }) +} + +/// True when fallbacks are allowed in this process: the `dev-fallback` +/// feature or the `AIEN_DEV_FALLBACK=1` environment opt-in. +pub fn dev_fallback_active() -> bool { + DEV_FALLBACK || env_opt_in() +} + +/// True when the process must fail closed (the production default). +pub fn production_strict() -> bool { + !dev_fallback_active() +} + +/// Tag every strict refusal carries, so logs and receipts can be grepped. +pub const STRICT_VIOLATION_PREFIX: &str = "STRICT_REAL_MODEL_VIOLATION"; + +/// Build the refusal message for a strict violation. +pub fn violation(what: &str) -> String { + 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)") +} + +/// Called by a backend at the moment it would silently run an operation on +/// CPU instead of the GPU it was bound to. In a production build this is +/// fatal: a wrong-or-slow result is worse than no result for the gate. +pub fn fallback_taken(backend: &str, op: &str) { + if production_strict() { + panic!( + "{}", + violation(&format!("{backend} fell back to CPU in {op}")) + ); + } +} + +/// What a strict acceptance receipt must record (GB10-4). +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct StrictModelReceipt { + pub checkpoint_path: String, + pub checkpoint_sha256: String, + pub tokenizer_path: String, + pub tokenizer_sha256: String, + pub backend_identity: String, + pub model_id: String, + pub model_config: serde_json::Value, + pub fallback_count: u64, + pub dev_fallback_build: bool, + pub verdict: String, +} + +impl StrictModelReceipt { + /// The gate: a production build, a native GPU backend and zero fallbacks. + pub fn verify(&mut self) -> Result<(), String> { + let mut problems = Vec::new(); + if self.dev_fallback_build { + problems.push("built with dev-fallback".to_string()); + } + if self.fallback_count != 0 { + problems.push(format!("fallback_count = {}", self.fallback_count)); + } + if !self.backend_identity.contains("sm_121") { + problems.push(format!( + "backend is not native GB10 sm_121: {}", + self.backend_identity + )); + } + if self.checkpoint_sha256.len() != 64 || self.tokenizer_sha256.len() != 64 { + problems.push("checkpoint or tokenizer digest missing".to_string()); + } + if problems.is_empty() { + self.verdict = "PASS".to_string(); + Ok(()) + } else { + self.verdict = format!("FAIL: {}", problems.join("; ")); + Err(violation(&problems.join("; "))) + } + } +} diff --git a/crates/aien-inference-runtime/Cargo.toml b/crates/aien-inference-runtime/Cargo.toml index bd894870..9e35e0b8 100644 --- a/crates/aien-inference-runtime/Cargo.toml +++ b/crates/aien-inference-runtime/Cargo.toml @@ -23,3 +23,6 @@ futures-util = "0.3" uuid = { version = "1.8", features = ["v4"] } chrono = { version = "0.4", features = ["serde"] } anyhow = "1.0" + +[dev-dependencies] +sha2 = "0.10" diff --git a/crates/aien-inference-runtime/src/model.rs b/crates/aien-inference-runtime/src/model.rs index 3a7481a9..d4cff5ad 100644 --- a/crates/aien-inference-runtime/src/model.rs +++ b/crates/aien-inference-runtime/src/model.rs @@ -83,6 +83,11 @@ impl EmbeddedModel { info!("Binding EmbeddedModel to Blackwell sm_121 GPU cuBLAS acceleration"); Arc::new(gpu) } else { + if aien_inference_abi::strict::production_strict() { + return Err(aien_inference_abi::strict::violation( + "GPU backend requested but BlackwellGb10Backend is unavailable", + )); + } info!("Blackwell GPU unavailable, falling back to CPU reference execution"); Arc::new(ReferenceCpuBackend::new()) } @@ -130,6 +135,11 @@ impl EmbeddedModel { info!("Loading real TinyLlama checkpoint from {}", mp.display()); Self::load_checkpoint(&mp, &tp, true, true) } else { + if aien_inference_abi::strict::production_strict() { + return Err(aien_inference_abi::strict::violation( + "no real checkpoint and tokenizer found on disk", + )); + } warn!("Real model checkpoint not found on disk, using reference test weights"); Self::with_reference_weights(&ModelConfig::tinyllama_1_1b()) } diff --git a/crates/aien-inference-runtime/tests/strict_real_model.rs b/crates/aien-inference-runtime/tests/strict_real_model.rs new file mode 100644 index 00000000..a243cb3e --- /dev/null +++ b/crates/aien-inference-runtime/tests/strict_real_model.rs @@ -0,0 +1,144 @@ +//! GB10-4: strict real-model execution gate (AIEN 0.1 commissioning). +//! +//! Non-ignored tests prove the production build fails closed: no checkpoint +//! on disk means a refusal, not reference weights. The ignored gate runs the +//! real TinyLlama checkpoint on the GB10 backend and writes the acceptance +//! receipt (checkpoint sha256, tokenizer sha256, backend identity, model +//! config, fallback_count). Run on the Spark: +//! AIEN_E2E_CHECKPOINT=~/models/TinyLlama-1.1B-Chat-v1.0 \ +//! AIEN_STRICT_RECEIPT=/path/receipt.json \ +//! cargo test -p aien-inference-runtime --release --test strict_real_model -- --ignored --nocapture + +use aien_inference_abi::strict::{self, StrictModelReceipt}; +use aien_inference_abi::ExecutionSurface; +use aien_inference_runtime::model::EmbeddedModel; +use sha2::{Digest, Sha256}; +use std::path::{Path, PathBuf}; + +fn sha256_file_hex(path: &Path) -> String { + let bytes = std::fs::read(path).unwrap_or_else(|e| panic!("read {}: {e}", path.display())); + let mut h = Sha256::new(); + h.update(&bytes); + format!("{:x}", h.finalize()) +} + +#[test] +fn production_build_is_strict_unless_opted_in() { + if std::env::var(strict::DEV_FALLBACK_ENV).is_ok() { + eprintln!( + "dev fallback opted in by {}; strictness not asserted", + strict::DEV_FALLBACK_ENV + ); + return; + } + assert!( + strict::production_strict(), + "this build has dev-fallback on; the strict gate must run on a production build" + ); +} + +#[test] +fn missing_checkpoint_is_refused_not_faked() { + let missing = PathBuf::from("/nonexistent/aien-strict-gate/model.safetensors"); + let tok = PathBuf::from("/nonexistent/aien-strict-gate/tokenizer.json"); + let err = EmbeddedModel::load_checkpoint(&missing, &tok, true, true) + .err() + .expect("a missing checkpoint must be an error"); + assert!( + !err.contains("reference"), + "refusal must not route to reference weights: {err}" + ); +} + +#[test] +fn receipt_rejects_fallbacks_and_dev_builds() { + let mut r = StrictModelReceipt { + checkpoint_path: "x".into(), + checkpoint_sha256: "0".repeat(64), + tokenizer_path: "y".into(), + tokenizer_sha256: "0".repeat(64), + backend_identity: "BlackwellGb10Backend (NVIDIA GB10 sm_121 cuBLAS)".into(), + model_id: "tinyllama".into(), + model_config: serde_json::json!({}), + fallback_count: 0, + dev_fallback_build: false, + verdict: String::new(), + }; + assert!(r.verify().is_ok(), "{}", r.verdict); + r.fallback_count = 1; + assert!(r.verify().is_err()); + r.fallback_count = 0; + r.dev_fallback_build = true; + assert!(r.verify().is_err()); + r.dev_fallback_build = false; + r.backend_identity = "ReferenceCpuBackend".into(); + assert!(r.verify().is_err()); +} + +#[test] +#[ignore = "needs the real checkpoint on the Spark: AIEN_E2E_CHECKPOINT + AIEN_STRICT_RECEIPT"] +fn strict_real_model_gate() { + let ckpt = PathBuf::from(std::env::var("AIEN_E2E_CHECKPOINT").expect("AIEN_E2E_CHECKPOINT")); + let receipt_path = + PathBuf::from(std::env::var("AIEN_STRICT_RECEIPT").expect("AIEN_STRICT_RECEIPT")); + let (model_path, dir) = if ckpt.is_dir() { + (ckpt.join("model.safetensors"), ckpt.clone()) + } else { + ( + ckpt.clone(), + ckpt.parent().map(Path::to_path_buf).unwrap_or_default(), + ) + }; + let tokenizer_path = dir.join("tokenizer.json"); + assert!( + model_path.is_file(), + "STRICT: checkpoint {} missing", + model_path.display() + ); + assert!( + tokenizer_path.is_file(), + "STRICT: tokenizer {} missing", + tokenizer_path.display() + ); + + let surface = ExecutionSurface::detect(); + println!("STRICT_GATE surface: {}", surface.display_name()); + let mut model = EmbeddedModel::load_checkpoint(&model_path, &tokenizer_path, true, true) + .unwrap_or_else(|e| panic!("STRICT: bind refused: {e}")); + let backend_identity = model.transformer.tensor_backend.name().to_string(); + println!("STRICT_GATE backend: {backend_identity}"); + + let prompt = "The DGX Spark is a small computer with a large"; + let out = model + .generate(prompt, 8, 0.0) + .unwrap_or_else(|e| panic!("STRICT: generate failed: {e}")); + println!("STRICT_GATE output: {out:?}"); + let fallback_count = model.transformer.tensor_backend.fallback_count(); + + let mut receipt = StrictModelReceipt { + checkpoint_path: model_path.display().to_string(), + checkpoint_sha256: sha256_file_hex(&model_path), + tokenizer_path: tokenizer_path.display().to_string(), + tokenizer_sha256: sha256_file_hex(&tokenizer_path), + backend_identity, + model_id: model.config.model_id.clone(), + model_config: serde_json::to_value(&model.config) + .unwrap_or(serde_json::json!({"model_id": model.config.model_id})), + fallback_count, + dev_fallback_build: strict::dev_fallback_active(), + verdict: String::new(), + }; + let verdict = receipt.verify(); + std::fs::write( + &receipt_path, + serde_json::to_string_pretty(&receipt).unwrap(), + ) + .expect("write receipt"); + println!( + "STRICT_GATE verdict: {} (receipt {})", + receipt.verdict, + receipt_path.display() + ); + assert!(!out.trim().is_empty(), "STRICT: empty output"); + verdict.unwrap_or_else(|e| panic!("{e}")); +} diff --git a/docs/campaigns/gb10-4-strict-real-model/README.md b/docs/campaigns/gb10-4-strict-real-model/README.md new file mode 100644 index 00000000..1856fcb1 --- /dev/null +++ b/docs/campaigns/gb10-4-strict-real-model/README.md @@ -0,0 +1,9 @@ +# GB10-4: strict real-model execution (AIEN 0.1 commissioning) + +Rule: a production build fails closed. No reference weights for a missing checkpoint, no silent CPU execution behind a requested GPU backend, and a GPU operation that falls back to CPU is fatal. Dev and CI runs opt in explicitly (cargo feature `dev-fallback` or `AIEN_DEV_FALLBACK=1`); the opt-in is recorded in every receipt and the acceptance gate rejects it. + +Gate: `crates/aien-inference-runtime/tests/strict_real_model.rs` (ignored test `strict_real_model_gate`, needs `AIEN_E2E_CHECKPOINT` and `AIEN_STRICT_RECEIPT`). Receipt records checkpoint sha256, tokenizer sha256, backend identity, model config, fallback_count, dev opt-in, verdict. + +Run 002 on the DGX Spark, 2026-10-03 (`receipt-run002.json`, `run002.summary.txt`): surface NVIDIA GB10, backend `BlackwellGb10Backend (NVIDIA GB10 sm_121 cuBLAS)`, TinyLlama-1.1B-Chat-v1.0, prompt "The DGX Spark is a small computer with a large" -> "amount of processing power. It is designed", fallback_count 0, verdict PASS. [OBSERVED] + +Limit: the GB10 backend in this crate is CUDA/cuBLAS; the project decision to keep CUDA out of AIEN (comparison runs only) means the native nvrm/Omega-engine backend is a separate open item. This gate measures strictness and binding, not the backend's provenance. diff --git a/docs/campaigns/gb10-4-strict-real-model/receipt-run002.json b/docs/campaigns/gb10-4-strict-real-model/receipt-run002.json new file mode 100644 index 00000000..17bec8af --- /dev/null +++ b/docs/campaigns/gb10-4-strict-real-model/receipt-run002.json @@ -0,0 +1,25 @@ +{ + "checkpoint_path": "/home/drakestapleton/models/TinyLlama-1.1B-Chat-v1.0/model.safetensors", + "checkpoint_sha256": "6e6001da2106d4757498752a021df6c2bdc332c650aae4bae6b0c004dcf14933", + "tokenizer_path": "/home/drakestapleton/models/TinyLlama-1.1B-Chat-v1.0/tokenizer.json", + "tokenizer_sha256": "bcd04f0eadf90287bd26e1a183ac487d8a141b09b06aecb7725bbdd343640f2e", + "backend_identity": "BlackwellGb10Backend (NVIDIA GB10 sm_121 cuBLAS)", + "model_id": "TinyLlama/TinyLlama-1.1B-Chat-v1.0", + "model_config": { + "block_size": 16, + "head_dim": 64, + "hidden_dim": 2048, + "intermediate_dim": 5632, + "max_sequence_length": 2048, + "model_id": "TinyLlama/TinyLlama-1.1B-Chat-v1.0", + "num_heads": 32, + "num_kv_heads": 4, + "num_layers": 22, + "rms_norm_eps": 9.999999747378752e-6, + "rope_theta": 10000.0, + "vocab_size": 32000 + }, + "fallback_count": 0, + "dev_fallback_build": false, + "verdict": "PASS" +} \ No newline at end of file diff --git a/docs/campaigns/gb10-4-strict-real-model/run002.summary.txt b/docs/campaigns/gb10-4-strict-real-model/run002.summary.txt new file mode 100644 index 00000000..5728c2a0 --- /dev/null +++ b/docs/campaigns/gb10-4-strict-real-model/run002.summary.txt @@ -0,0 +1,6 @@ +STRICT_GATE surface: NVIDIA GB10 (121 GB Unified Memory) +STRICT_GATE backend: BlackwellGb10Backend (NVIDIA GB10 sm_121 cuBLAS) +STRICT_GATE output: "amount of processing power. It is designed" +STRICT_GATE verdict: PASS (receipt /home/drakestapleton/workspace/evidence-out/STRICT-REAL-MODEL-20261003/receipt-run002.json) +test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 16.36s +GATE_RC=0