Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/patches/ad48ccdd0b2d0cb6 # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/aien-sovereign-core-mv#893 (code only) closed derived `ad48ccdd0b2d0cb6` #893 Imported from GitHub aien-dev/aien-sovereign-core-mv#893 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `d38411c38f94b02d9e87beea440d01b42cfc5bd8` · Head `1b49eb5b2815f85ee2101f6ddc7d53fce9abb0fd` ### Fetch it A git bundle of the pull request's head, 4761 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/aien-sovereign-core-mv/patches/ad48ccdd0b2d0cb6.bundle -o ad48ccdd0b2d0cb6.bundle git fetch ad48ccdd0b2d0cb6.bundle refs/pull/893/head ``` Raw bundle · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/aien-sovereign-core-mv 893 1b49eb5b2815f85ee2101f6ddc7d53fce9abb0fd ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aien-sovereign-core-mv","number":893,"url":"https://github.com/aien-dev/aien-sovereign-core-mv/pull/893","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:38:13Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/crates/aien-inference-abi/src/qwen3_moe_omega_dattn.rs b/crates/aien-inference-abi/src/qwen3_moe_omega_dattn.rs index fdf90840..8e4917ab 100644 --- a/crates/aien-inference-abi/src/qwen3_moe_omega_dattn.rs +++ b/crates/aien-inference-abi/src/qwen3_moe_omega_dattn.rs @@ -134,7 +134,15 @@ impl KvStore for OmegaFusedKv { "fused" } fn truncate(&mut self, len: usize) { - // The cache keeps its bytes; positions past `len` are rewritten by later steps. + // The cache keeps its bytes; positions past `len` are rewritten by later steps. Those + // steps write the cache only, so omega's prefill-attention mirror must drop its copies + // of the forgotten rows first, or a later whole-prompt pass reads the old sequence's + // K/V there (mirror contract, omega_gpu_prefill_attn_api.h). Every cache write is at a + // layer's `filled` position, so this one call covers decode, the step_batch fallback + // and imports after a rewind. + if let Some(from) = rewritten_from(&self.filled, len) { + aien_omega_pf::kv_mirror_invalidate(&mut self.cache, from); + } for f in self.filled.iter_mut() { *f = (*f).min(len); } @@ -258,3 +266,38 @@ impl KvAttention for OmegaFusedKv { self.filled.iter_mut().for_each(|f| *f = pos + 1); } } + +impl Drop for OmegaFusedKv { + fn drop(&mut self) { + // Mirrors are keyed by the layer's K plane address: a later cache allocated there would + // inherit stale rows. Runs before the cache field frees the planes. + aien_omega_pf::kv_mirror_forget(&mut self.cache); + } +} + +/// The first position a truncate to `len` forgets in any layer, if it forgets one: rows from +/// there on will be rewritten, so mirrored copies of them go stale. +fn rewritten_from(filled: &[usize], len: usize) -> Option { + filled.iter().any(|&f| f > len).then_some(len) +} + +#[cfg(test)] +mod tests { + use super::rewritten_from; + + #[test] + fn a_rewind_below_the_stored_rows_invalidates_from_the_kept_prefix() { + // A: whole-prompt prefill of 40 tokens + 8 decoded; B keeps a 12-token prefix. + assert_eq!(rewritten_from(&[48; 4], 12), Some(12)); + // One layer ahead of the others (mid-step) still forgets rows. + assert_eq!(rewritten_from(&[30, 30, 31, 30], 30), Some(30)); + assert_eq!(rewritten_from(&[48; 4], 0), Some(0)); + } + + #[test] + fn keeping_every_stored_row_invalidates_nothing() { + assert_eq!(rewritten_from(&[48; 4], 48), None); + assert_eq!(rewritten_from(&[48; 4], 100), None); + assert_eq!(rewritten_from(&[], 0), None); + } +} diff --git a/crates/aien-omega-pf/build.rs b/crates/aien-omega-pf/build.rs index 02dd3cf8..92873039 100644 --- a/crates/aien-omega-pf/build.rs +++ b/crates/aien-omega-pf/build.rs @@ -25,6 +25,8 @@ //! `cfg(has_omega_pf)`: `omega_gpu_pf_qgemm` defined (quantiser, QMMA grouped GEMM, pf buffers). //! `cfg(has_omega_pattn)`: `omega_gpu_prefill_attn` defined (host-pointer prefill attention). //! `cfg(has_omega_pattn_dev)`: `omega_gpu_prefill_attn_dev` defined (device-pointer entry). +//! `cfg(has_omega_pattn_mirror)`: `omega_gpu_prefill_attn_kv_mirror_invalidate` and `_forget` +//! defined (the persistent KV mirror's coherence calls). //! Otherwise every call returns `Unavailable`; a stub never fails the build. use std::path::Path; use std::process::Command; @@ -36,6 +38,7 @@ fn main() { "has_omega_pattn_dev", "has_omega_pf_scoped", "has_omega_pf_reading", + "has_omega_pattn_mirror", ] { println!("cargo:rustc-check-cfg=cfg({c})"); } @@ -72,6 +75,13 @@ fn main() { if has("omega_gpu_prefill_attn_dev") { println!("cargo:rustc-cfg=has_omega_pattn_dev"); } + if has("omega_gpu_prefill_attn_kv_mirror_invalidate") + && has("omega_gpu_prefill_attn_kv_mirror_forget") + { + println!("cargo:rustc-cfg=has_omega_pattn_mirror"); + } else { + println!("cargo:warning=aien-omega-pf: {lib} has prefill attention but no kv_mirror_invalidate/forget; a rewound cache cannot drop stale mirror rows"); + } } else { println!("cargo:warning=aien-omega-pf: {lib} does not define omega_gpu_prefill_attn; prefill attention is stubbed"); } @@ -122,6 +132,11 @@ fn main() { if t.contains("omega_gpu_prefill_attn_dev(") { println!("cargo:rustc-cfg=has_omega_pattn_dev"); } + if t.contains("omega_gpu_prefill_attn_kv_mirror_invalidate(") + && t.contains("omega_gpu_prefill_attn_kv_mirror_forget(") + { + println!("cargo:rustc-cfg=has_omega_pattn_mirror"); + } } } } diff --git a/crates/aien-omega-pf/src/lib.rs b/crates/aien-omega-pf/src/lib.rs index 1d104a92..5c49a99b 100644 --- a/crates/aien-omega-pf/src/lib.rs +++ b/crates/aien-omega-pf/src/lib.rs @@ -599,6 +599,58 @@ mod ffi_attn { } } +#[cfg(has_omega_pattn_mirror)] +mod ffi_mirror { + use std::os::raw::c_void; + extern "C" { + pub fn omega_gpu_prefill_attn_kv_mirror_invalidate( + c: *const c_void, + layer: u32, + from_pos: u32, + ); + pub fn omega_gpu_prefill_attn_kv_mirror_forget(c: *const c_void); + } +} + +/// True when omega's KV mirror coherence calls are linked ([`kv_mirror_invalidate`], +/// [`kv_mirror_forget`]); otherwise both are no-ops. +pub const fn has_kv_mirror_coherence() -> bool { + cfg!(has_omega_pattn_mirror) +} + +/// Drops every layer's mirrored rows of `cache` at and past `from_pos` +/// (`omega_gpu_prefill_attn_kv_mirror_invalidate(c, UINT32_MAX, from_pos)`). Contract +/// (omega_gpu_prefill_attn_api.h): call before cache rows below a mirror's `valid` are rewritten +/// other than through prefill attention, e.g. after the sequence is rewound and decoded again. +pub fn kv_mirror_invalidate(cache: &mut aien_omega_dattn::DecodeAttnCache, from_pos: usize) { + #[cfg(has_omega_pattn_mirror)] + { + let c = cache.raw_handle(); + if !c.is_null() { + let from = u32::try_from(from_pos).unwrap_or(u32::MAX); + // SAFETY: a live cache handle, exclusively borrowed; omega only reads its views. + unsafe { ffi_mirror::omega_gpu_prefill_attn_kv_mirror_invalidate(c, u32::MAX, from) }; + } + } + #[cfg(not(has_omega_pattn_mirror))] + let _ = (cache, from_pos); +} + +/// Frees the mirrors of `cache` (`omega_gpu_prefill_attn_kv_mirror_forget`). Contract: call before +/// the cache is freed, or a later cache at the same K plane address inherits stale rows. +pub fn kv_mirror_forget(cache: &mut aien_omega_dattn::DecodeAttnCache) { + #[cfg(has_omega_pattn_mirror)] + { + let c = cache.raw_handle(); + if !c.is_null() { + // SAFETY: as kv_mirror_invalidate. + unsafe { ffi_mirror::omega_gpu_prefill_attn_kv_mirror_forget(c) }; + } + } + #[cfg(not(has_omega_pattn_mirror))] + let _ = cache; +} + fn bad(name: String) -> OmegaGpuError { OmegaGpuError::Rc { rc: -1, name } } ```