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 } }