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-omega-q38 / build.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 | //! Decides whether omega's Qwen3.8-27B decode pieces (`src/omega_gpu_q38_graph.h`, sc #642 |
| 17 | //! on the omega side) are linked. |
| 18 | //! |
| 19 | //! The archive itself is linked by `aien-omega-gpu`'s build script (this crate only declares |
| 20 | //! the extra symbols), so the decision mirrors its inputs: |
| 21 | //! * `AIEN_FORCE_CPU_STUB=1`: stub. |
| 22 | //! * `AIEN_OMEGA_GPU_LIB=<archive>`: native when `nm` finds `omega_gpu_q38_graph_create` |
| 23 | //! defined in that archive. |
| 24 | //! * `AIEN_OMEGA_DIR=<checkout>`: native when the checkout has `src/omega_gpu_q38_graph.h`. |
| 25 | //! * otherwise: stub (every call returns `Unavailable`). |
| 26 | //! |
| 27 | //! Native linkage also requires the shape ABI this crate calls: |
| 28 | //! `int omega_q38_gather_table(uint32_t q_heads, uint32_t kv_heads, uint32_t *tab)`. |
| 29 | //! Older omega (including the previous pin a647b6a9) declares |
| 30 | //! `void omega_q38_gather_table(uint32_t *tab)`. The names match, so the 3-arg call would link |
| 31 | //! and then treat `q_heads` as the table pointer. A header is checked for the 3-arg declaration. |
| 32 | //! An archive cannot show C argument counts, so the witness is |
| 33 | //! `omega_gpu_q38_graph_set_layer_bf16`, which shipped in the same omega change as that |
| 34 | //! signature (omega-mv#905, on main at f9a9a40). A q38 omega without it fails the build. |
| 35 | //! A checkout or archive with no q38 graph at all still stubs. |
| 36 | //! |
| 37 | //! `cfg(has_omega_q38_prefill)` is set when, in addition, the prefill entries are linked |
| 38 | //! (`omega_gpu_gdn_prefill` and `omega_gpu_prefill_attn256`, omega#543 / #545; with |
| 39 | //! `AIEN_OMEGA_DIR`, the header `src/omega_gpu_prefill_attn256_api.h`). |
| 40 | //! |
| 41 | //! `cfg(has_omega_q38)` is set only when native. A stub never fails the build: the Qwen3.8 |
| 42 | //! engine reports itself unavailable, and `is_native()` says which one a binary carries. |
| 43 | use std::path::Path; |
| 44 | use std::process::Command; |
| 45 | |
| 46 | fn main() { |
| 47 | println!("cargo:rustc-check-cfg=cfg(has_omega_q38_exact)"); |
| 48 | println!("cargo:rustc-check-cfg=cfg(has_omega_q38)"); |
| 49 | println!("cargo:rustc-check-cfg=cfg(has_omega_q38_prefill)"); |
| 50 | println!("cargo:rustc-check-cfg=cfg(has_omega_q38_attention_mirror)"); |
| 51 | println!("cargo:rustc-check-cfg=cfg(has_omega_q36)"); |
| 52 | for v in [ |
| 53 | "AIEN_FORCE_CPU_STUB", |
| 54 | "AIEN_OMEGA_GPU_LIB", |
| 55 | "AIEN_OMEGA_DIR", |
| 56 | ] { |
| 57 | println!("cargo:rerun-if-env-changed={v}"); |
| 58 | } |
| 59 | if std::env::var("AIEN_FORCE_CPU_STUB").as_deref() == Ok("1") { |
| 60 | return; |
| 61 | } |
| 62 | if let Ok(lib) = std::env::var("AIEN_OMEGA_GPU_LIB") { |
| 63 | println!("cargo:rerun-if-changed={lib}"); |
| 64 | if archive_defines(Path::new(&lib), "omega_gpu_q38_graph_create") { |
| 65 | if !archive_defines(Path::new(&lib), "omega_gpu_q38_graph_set_layer_bf16") { |
| 66 | refuse_shape_abi( |
| 67 | &lib, |
| 68 | "nm did not find T omega_gpu_q38_graph_set_layer_bf16. That symbol shipped with the 3-arg gather; nm cannot see C argument counts, and omega_q38_gather_table keeps its name.", |
| 69 | ); |
| 70 | } |
| 71 | if archive_defines(Path::new(&lib), "omega_gpu_q38_exact_extra_bytes") { |
| 72 | println!("cargo:rustc-cfg=has_omega_q38_exact"); |
| 73 | } |
| 74 | println!("cargo:rustc-cfg=has_omega_q38"); |
| 75 | build_prefill_policy(); |
| 76 | if archive_defines(Path::new(&lib), "omega_gpu_q36_graph_create") { |
| 77 | println!("cargo:rustc-cfg=has_omega_q36"); |
| 78 | } |
| 79 | if archive_defines(Path::new(&lib), "omega_gpu_gdn_prefill") |
| 80 | && archive_defines(Path::new(&lib), "omega_gpu_prefill_attn256") |
| 81 | { |
| 82 | println!("cargo:rustc-cfg=has_omega_q38_prefill"); |
| 83 | if archive_defines(Path::new(&lib), "omega_gpu_prefill_attn256_scoped") { |
| 84 | println!("cargo:rustc-cfg=has_omega_q38_attention_mirror"); |
| 85 | } |
| 86 | } |
| 87 | } else { |
| 88 | println!("cargo:warning=aien-omega-q38: {lib} does not define the q38 graph API; the Qwen3.8 decode is stubbed"); |
| 89 | } |
| 90 | return; |
| 91 | } |
| 92 | if let Ok(dir) = std::env::var("AIEN_OMEGA_DIR") { |
| 93 | let header = Path::new(&dir).join("src/omega_gpu_q38_graph.h"); |
| 94 | println!("cargo:rerun-if-changed={}", header.display()); |
| 95 | if header.is_file() { |
| 96 | let text = std::fs::read_to_string(&header).unwrap_or_default(); |
| 97 | if !shape_gather_declared(&text) { |
| 98 | refuse_shape_abi( |
| 99 | &header.display().to_string(), |
| 100 | "the header does not declare int omega_q38_gather_table(uint32_t q_heads, uint32_t kv_heads, uint32_t *tab).", |
| 101 | ); |
| 102 | } |
| 103 | if text.contains("omega_gpu_q38_exact_extra_bytes(") { |
| 104 | println!("cargo:rustc-cfg=has_omega_q38_exact"); |
| 105 | } |
| 106 | println!("cargo:rustc-cfg=has_omega_q38"); |
| 107 | build_prefill_policy(); |
| 108 | if Path::new(&dir).join("src/omega_gpu_q36_graph.h").is_file() { |
| 109 | println!("cargo:rustc-cfg=has_omega_q36"); |
| 110 | } |
| 111 | if Path::new(&dir) |
| 112 | .join("src/omega_gpu_prefill_attn256_api.h") |
| 113 | .is_file() |
| 114 | { |
| 115 | println!("cargo:rustc-cfg=has_omega_q38_prefill"); |
| 116 | let mirror = Path::new(&dir).join("src/omega_gpu_attention_mirror.h"); |
| 117 | println!("cargo:rerun-if-changed={}", mirror.display()); |
| 118 | if std::fs::read_to_string(mirror) |
| 119 | .unwrap_or_default() |
| 120 | .contains("omega_gpu_prefill_attn256_scoped(") |
| 121 | { |
| 122 | println!("cargo:rustc-cfg=has_omega_q38_attention_mirror"); |
| 123 | } |
| 124 | } |
| 125 | } else { |
| 126 | println!("cargo:warning=aien-omega-q38: {dir} has no src/omega_gpu_q38_graph.h (omega before sc #642); the Qwen3.8 decode is stubbed"); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /// True when `header` declares the 3-arg gather this crate calls. Whitespace is ignored so |
| 132 | /// `uint32_t *tab` and `uint32_t* tab` match. The 1-arg `void omega_q38_gather_table(uint32_t *tab)` |
| 133 | /// does not. |
| 134 | fn shape_gather_declared(header: &str) -> bool { |
| 135 | let flat: String = header.chars().filter(|c| !c.is_whitespace()).collect(); |
| 136 | flat.contains("intomega_q38_gather_table(uint32_tq_heads,uint32_tkv_heads,uint32_t*tab)") |
| 137 | } |
| 138 | |
| 139 | fn omega_pin() -> String { |
| 140 | let root = std::path::PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()); |
| 141 | std::fs::read_to_string(root.join("../../omega.lock")) |
| 142 | .unwrap_or_default() |
| 143 | .trim() |
| 144 | .to_string() |
| 145 | } |
| 146 | |
| 147 | fn refuse_shape_abi(where_: &str, detail: &str) -> ! { |
| 148 | let pin = omega_pin(); |
| 149 | panic!( |
| 150 | "aien-omega-q38: {where_} has the Qwen3.8 graph but not the shape ABI this crate calls.\n\ |
| 151 | required: int omega_q38_gather_table(uint32_t q_heads, uint32_t kv_heads, uint32_t *tab)\n\ |
| 152 | omega.lock pins {pin}, which is an omega-mv main commit that exports that signature.\n\ |
| 153 | {detail}\n\ |
| 154 | The 1-arg form is `void omega_q38_gather_table(uint32_t *tab)`. The names match, so the 3-arg call would link and then write through q_heads as a pointer.\n\ |
| 155 | Point AIEN_OMEGA_DIR or AIEN_OMEGA_GPU_LIB at the pinned commit, or unset them for the CPU stub." |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | fn archive_defines(lib: &Path, symbol: &str) -> bool { |
| 160 | let Ok(out) = Command::new("nm").arg(lib).output() else { |
| 161 | println!("cargo:warning=aien-omega-q38: nm not available, assuming the archive lacks the q38 graph API"); |
| 162 | return false; |
| 163 | }; |
| 164 | String::from_utf8_lossy(&out.stdout) |
| 165 | .lines() |
| 166 | .any(|l| l.ends_with(&format!(" T {symbol}"))) |
| 167 | } |
| 168 | |
| 169 | // Compatibility build for checked Core-owned Omega. No policy is implemented here. |
| 170 | fn build_prefill_policy() { |
| 171 | let root = std::path::PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()); |
| 172 | let policy = root.join("om"); |
| 173 | for file in ["plan.om", "plan.c", "plan.h", "identity.sha256"] { |
| 174 | println!("cargo:rerun-if-changed={}", policy.join(file).display()); |
| 175 | } |
| 176 | let pins = |
| 177 | std::fs::read_to_string(policy.join("identity.sha256")).expect("Omega policy identity"); |
| 178 | for file in ["plan.om", "plan.c", "plan.h"] { |
| 179 | let rows: Vec<_> = pins |
| 180 | .lines() |
| 181 | .filter_map(|line| { |
| 182 | let fields: Vec<_> = line.split_whitespace().collect(); |
| 183 | if fields.len() == 2 && fields[1] == file { |
| 184 | Some(fields[0]) |
| 185 | } else { |
| 186 | None |
| 187 | } |
| 188 | }) |
| 189 | .collect(); |
| 190 | assert_eq!(rows.len(), 1, "exactly one identity required for {file}"); |
| 191 | let actual = aien_abi_conformance::identity::sha256_file(&policy.join(file)) |
| 192 | .expect("hash checked Omega policy"); |
| 193 | assert_eq!( |
| 194 | actual, rows[0], |
| 195 | "Omega policy source/binding identity mismatch: {file}" |
| 196 | ); |
| 197 | } |
| 198 | let out = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); |
| 199 | let target = std::env::var("TARGET").unwrap().replace('-', "_"); |
| 200 | let tool = |name: &str| { |
| 201 | for key in [ |
| 202 | format!("{name}_{target}"), |
| 203 | format!("TARGET_{name}"), |
| 204 | name.to_owned(), |
| 205 | ] { |
| 206 | println!("cargo:rerun-if-env-changed={key}"); |
| 207 | if let Some(value) = std::env::var_os(&key) { |
| 208 | return value; |
| 209 | } |
| 210 | } |
| 211 | std::ffi::OsString::from(name.to_ascii_lowercase()) |
| 212 | }; |
| 213 | let cc = tool("CC"); |
| 214 | let ar = tool("AR"); |
| 215 | let object = out.join("q38_prefill_plan.o"); |
| 216 | let status = Command::new(&cc) |
| 217 | .args(["-std=gnu11", "-O2", "-fPIC", "-ffp-contract=off", "-c"]) |
| 218 | .arg(policy.join("plan.c")) |
| 219 | .arg("-o") |
| 220 | .arg(&object) |
| 221 | .status() |
| 222 | .expect("compile checked Omega prefill policy"); |
| 223 | assert!( |
| 224 | status.success(), |
| 225 | "checked Omega prefill policy compilation failed" |
| 226 | ); |
| 227 | let status = Command::new(ar) |
| 228 | .arg("crs") |
| 229 | .arg(out.join("libaien_q38_prefill_plan.a")) |
| 230 | .arg(object) |
| 231 | .status() |
| 232 | .expect("archive checked Omega prefill policy"); |
| 233 | assert!( |
| 234 | status.success(), |
| 235 | "checked Omega prefill policy archive failed" |
| 236 | ); |
| 237 | let version = Command::new(cc) |
| 238 | .arg("--version") |
| 239 | .output() |
| 240 | .expect("record policy compiler"); |
| 241 | assert!( |
| 242 | version.status.success(), |
| 243 | "policy compiler identity unavailable" |
| 244 | ); |
| 245 | let text = String::from_utf8_lossy(&version.stdout); |
| 246 | println!( |
| 247 | "cargo:rustc-env=AIEN_Q38_PREFILL_CC={}", |
| 248 | text.lines().next().unwrap_or("unknown") |
| 249 | ); |
| 250 | println!("cargo:rustc-link-search=native={}", out.display()); |
| 251 | println!("cargo:rustc-link-lib=static=aien_q38_prefill_plan"); |
| 252 | } |