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-campaign-verifier / src / placement.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 | //! Device placement: AIEN's per-op dispatch record must agree with the external device witness |
| 17 | //! and with `placement.toml` (SPEC.md Section 6). |
| 18 | |
| 19 | use serde::Deserialize; |
| 20 | use std::collections::{BTreeMap, BTreeSet}; |
| 21 | |
| 22 | /// Allowlist entries with this prefix are not yet frozen and never match. |
| 23 | pub const PENDING_PREFIX: &str = "PENDING"; |
| 24 | pub const CPU: &str = "cpu"; |
| 25 | |
| 26 | #[derive(Debug, Clone, Deserialize)] |
| 27 | pub struct OpPolicy { |
| 28 | pub allowed: Vec<String>, |
| 29 | #[serde(default)] |
| 30 | pub kernels: Vec<String>, |
| 31 | } |
| 32 | |
| 33 | #[derive(Debug, Clone, Deserialize)] |
| 34 | pub struct PlacementSpec { |
| 35 | pub ops: BTreeMap<String, OpPolicy>, |
| 36 | } |
| 37 | |
| 38 | /// One line of `op_records.jsonl`, written by AIEN. |
| 39 | #[derive(Debug, Clone, Deserialize)] |
| 40 | pub struct OpRecord { |
| 41 | pub op_call_id: u64, |
| 42 | pub op: String, |
| 43 | pub reported_device: String, |
| 44 | #[serde(default)] |
| 45 | pub fallback: bool, |
| 46 | } |
| 47 | |
| 48 | /// One line of `witness.jsonl`, written by the external device witness. |
| 49 | #[derive(Debug, Clone, Deserialize)] |
| 50 | pub struct WitnessEvent { |
| 51 | pub op_call_id: u64, |
| 52 | pub kernel: String, |
| 53 | pub completed: bool, |
| 54 | } |
| 55 | |
| 56 | /// Glob match supporting `*` only. |
| 57 | pub fn glob_match(pattern: &str, text: &str) -> bool { |
| 58 | let parts: Vec<&str> = pattern.split('*').collect(); |
| 59 | if parts.len() == 1 { |
| 60 | return pattern == text; |
| 61 | } |
| 62 | let first = parts[0]; |
| 63 | let last = parts[parts.len() - 1]; |
| 64 | if text.len() < first.len() + last.len() || !text.starts_with(first) || !text.ends_with(last) { |
| 65 | return false; |
| 66 | } |
| 67 | let mut rest = &text[first.len()..text.len() - last.len()]; |
| 68 | for part in &parts[1..parts.len() - 1] { |
| 69 | match rest.find(part) { |
| 70 | Some(i) => rest = &rest[i + part.len()..], |
| 71 | None => return false, |
| 72 | } |
| 73 | } |
| 74 | true |
| 75 | } |
| 76 | |
| 77 | pub fn check_placement( |
| 78 | spec: &PlacementSpec, |
| 79 | records: &[OpRecord], |
| 80 | witness: &[WitnessEvent], |
| 81 | ) -> Vec<String> { |
| 82 | let mut v = Vec::new(); |
| 83 | |
| 84 | let mut kernels_by_call: BTreeMap<u64, Vec<&WitnessEvent>> = BTreeMap::new(); |
| 85 | for w in witness { |
| 86 | kernels_by_call.entry(w.op_call_id).or_default().push(w); |
| 87 | } |
| 88 | |
| 89 | let mut seen = BTreeSet::new(); |
| 90 | for r in records { |
| 91 | let id = r.op_call_id; |
| 92 | if !seen.insert(id) { |
| 93 | v.push(format!("op_call {id}: duplicate op_call_id")); |
| 94 | } |
| 95 | let Some(policy) = spec.ops.get(&r.op) else { |
| 96 | v.push(format!("op_call {id}: op {} not in placement spec", r.op)); |
| 97 | continue; |
| 98 | }; |
| 99 | if !policy.allowed.iter().any(|d| d == &r.reported_device) { |
| 100 | v.push(format!( |
| 101 | "op_call {id}: {} ran on {}, allowed {:?}", |
| 102 | r.op, r.reported_device, policy.allowed |
| 103 | )); |
| 104 | } |
| 105 | if r.fallback { |
| 106 | v.push(format!("op_call {id}: {} fell back", r.op)); |
| 107 | } |
| 108 | |
| 109 | let kernels = kernels_by_call.get(&id).map(Vec::as_slice).unwrap_or(&[]); |
| 110 | if r.reported_device == CPU { |
| 111 | if !kernels.is_empty() { |
| 112 | v.push(format!( |
| 113 | "op_call {id}: {} reported cpu but witness saw {} kernel(s)", |
| 114 | r.op, |
| 115 | kernels.len() |
| 116 | )); |
| 117 | } |
| 118 | continue; |
| 119 | } |
| 120 | if kernels.is_empty() { |
| 121 | v.push(format!( |
| 122 | "op_call {id}: {} reported {} but witness saw no kernel", |
| 123 | r.op, r.reported_device |
| 124 | )); |
| 125 | } |
| 126 | for k in kernels { |
| 127 | if !k.completed { |
| 128 | v.push(format!( |
| 129 | "op_call {id}: kernel {} did not complete", |
| 130 | k.kernel |
| 131 | )); |
| 132 | } |
| 133 | let allowed = policy |
| 134 | .kernels |
| 135 | .iter() |
| 136 | .any(|p| !p.starts_with(PENDING_PREFIX) && glob_match(p, &k.kernel)); |
| 137 | if !allowed { |
| 138 | v.push(format!( |
| 139 | "op_call {id}: kernel {} not in the frozen allowlist for {}", |
| 140 | k.kernel, r.op |
| 141 | )); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | for id in kernels_by_call.keys() { |
| 147 | if !seen.contains(id) { |
| 148 | v.push(format!( |
| 149 | "witness saw kernel(s) for op_call {id} with no dispatch record" |
| 150 | )); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | v |
| 155 | } |
| 156 | |
| 157 | #[cfg(test)] |
| 158 | mod tests { |
| 159 | use super::*; |
| 160 | |
| 161 | fn spec() -> PlacementSpec { |
| 162 | toml::from_str( |
| 163 | r#" |
| 164 | [ops.rmsnorm] |
| 165 | allowed = ["cpu"] |
| 166 | |
| 167 | [ops.paged_attention_batch] |
| 168 | allowed = ["gb10"] |
| 169 | kernels = ["*paged_attention_bf16*"] |
| 170 | |
| 171 | [ops.compute_logits] |
| 172 | allowed = ["gb10"] |
| 173 | kernels = ["PENDING_GATE0_CUBLAS_GEMV"] |
| 174 | "#, |
| 175 | ) |
| 176 | .unwrap() |
| 177 | } |
| 178 | |
| 179 | fn rec(id: u64, op: &str, device: &str) -> OpRecord { |
| 180 | OpRecord { |
| 181 | op_call_id: id, |
| 182 | op: op.into(), |
| 183 | reported_device: device.into(), |
| 184 | fallback: false, |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | fn kernel(id: u64, name: &str) -> WitnessEvent { |
| 189 | WitnessEvent { |
| 190 | op_call_id: id, |
| 191 | kernel: name.into(), |
| 192 | completed: true, |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn glob() { |
| 198 | assert!(glob_match( |
| 199 | "*paged_attention_bf16*", |
| 200 | "void paged_attention_bf16_kernel<128>" |
| 201 | )); |
| 202 | assert!(glob_match("exact", "exact")); |
| 203 | assert!(!glob_match("exact", "exactly")); |
| 204 | assert!(glob_match("a*c", "abc")); |
| 205 | assert!(!glob_match("ab*bc", "abc")); |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn honest_hybrid_run_passes() { |
| 210 | let records = [ |
| 211 | rec(1, "rmsnorm", "cpu"), |
| 212 | rec(2, "paged_attention_batch", "gb10"), |
| 213 | ]; |
| 214 | let witness = [kernel(2, "paged_attention_bf16_kernel")]; |
| 215 | assert!(check_placement(&spec(), &records, &witness).is_empty()); |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn required_gpu_op_on_cpu_fails() { |
| 220 | let records = [rec(1, "paged_attention_batch", "cpu")]; |
| 221 | assert!(!check_placement(&spec(), &records, &[]).is_empty()); |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn backend_claiming_gpu_without_kernel_fails() { |
| 226 | let records = [rec(1, "paged_attention_batch", "gb10")]; |
| 227 | let v = check_placement(&spec(), &records, &[]); |
| 228 | assert!(v.iter().any(|m| m.contains("witness saw no kernel"))); |
| 229 | } |
| 230 | |
| 231 | #[test] |
| 232 | fn kernel_under_cpu_report_fails() { |
| 233 | let records = [rec(1, "rmsnorm", "cpu")]; |
| 234 | let witness = [kernel(1, "rmsnorm_kernel")]; |
| 235 | let v = check_placement(&spec(), &records, &witness); |
| 236 | assert!(v.iter().any(|m| m.contains("reported cpu"))); |
| 237 | } |
| 238 | |
| 239 | #[test] |
| 240 | fn wrong_kernel_symbol_fails() { |
| 241 | let records = [rec(1, "paged_attention_batch", "gb10")]; |
| 242 | let witness = [kernel(1, "some_other_kernel")]; |
| 243 | let v = check_placement(&spec(), &records, &witness); |
| 244 | assert!(v.iter().any(|m| m.contains("not in the frozen allowlist"))); |
| 245 | } |
| 246 | |
| 247 | #[test] |
| 248 | fn pending_allowlist_never_passes() { |
| 249 | let records = [rec(1, "compute_logits", "gb10")]; |
| 250 | let witness = [kernel(1, "PENDING_GATE0_CUBLAS_GEMV")]; |
| 251 | assert!(!check_placement(&spec(), &records, &witness).is_empty()); |
| 252 | } |
| 253 | |
| 254 | #[test] |
| 255 | fn fallback_fails() { |
| 256 | let mut r = rec(1, "rmsnorm", "cpu"); |
| 257 | r.fallback = true; |
| 258 | assert!(!check_placement(&spec(), &[r], &[]).is_empty()); |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn orphan_witness_kernel_fails() { |
| 263 | let witness = [kernel(9, "paged_attention_bf16_kernel")]; |
| 264 | let v = check_placement(&spec(), &[], &witness); |
| 265 | assert!(v.iter().any(|m| m.contains("no dispatch record"))); |
| 266 | } |
| 267 | } |