From abea25b944ceb1b0d6c4b947ba2f97cfc0983030 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sat, 31 Oct 2026 09:10:36 -0500 Subject: [PATCH] q36: set_lm_head_fp8 FFI + AIEN_Q36_LM_HEAD_FP8 test/bench knob (default off) for DRA-69 (refs aien-dev/CUDApp#217) --- .../src/qwen38_omega/moe.rs | 11 ++++++++ crates/aien-omega-q38/src/q36.rs | 25 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/crates/aien-inference-abi/src/qwen38_omega/moe.rs b/crates/aien-inference-abi/src/qwen38_omega/moe.rs index 932b197e..0ded44fe 100644 --- a/crates/aien-inference-abi/src/qwen38_omega/moe.rs +++ b/crates/aien-inference-abi/src/qwen38_omega/moe.rs @@ -458,6 +458,17 @@ impl Qwen38Omega { ); (Some(screen), Some(coef)) }; + // DRA-69 (TEST/BENCH knob, default off): AIEN_Q36_LM_HEAD_FP8=1 makes the fp8b screen the graph's lm_head (FP8 logits for + // every token; the exact head then stands aside). Pair with AIEN_OMEGA_Q36_FP8_HEAD=1 for one head launch per stroke. + if std::env::var("AIEN_Q36_LM_HEAD_FP8").as_deref() == Ok("1") { + let screen = head_screen + .as_ref() + .ok_or("AIEN_Q36_LM_HEAD_FP8=1 needs the fp8b screen (leave AIEN_Q36_EXACT_HEAD unset)")?; + // SAFETY: the screen is owned by the returned value and dropped after the graph (field order). + unsafe { graph.set_lm_head_fp8(screen.as_raw()) } + .map_err(gpu("q36 graph set_lm_head_fp8"))?; + eprintln!("qwen36 omega: FP8 lm_head ON (AIEN_Q36_LM_HEAD_FP8=1, the fp8b screen; TEST/BENCH, not for serving)"); + } drop(head); // Negative controls for the gates (TEST ONLY): AIEN_Q36_TEST_MUTANT=4 drops the shared expert // from COMBINE, 1 shifts the SWIGLU up half, 2 the attention GATHER, 3 drops the attention gate, diff --git a/crates/aien-omega-q38/src/q36.rs b/crates/aien-omega-q38/src/q36.rs index b7b281b0..0d0d741a 100644 --- a/crates/aien-omega-q38/src/q36.rs +++ b/crates/aien-omega-q38/src/q36.rs @@ -147,6 +147,7 @@ mod ffi { ) -> i32; pub fn omega_gpu_q36_graph_kind_name(kind: u8) -> *const std::ffi::c_char; pub fn omega_gpu_q36_graph_test_set_mutant(g: *mut OmegaGpuQ36Graph, mutant: i32); + pub fn omega_gpu_q36_graph_set_lm_head_fp8(g: *mut OmegaGpuQ36Graph, head: *const c_void) -> i32; pub fn omega_gpu_q36_graph_set_exact_head( g: *mut OmegaGpuQ36Graph, screen: *const c_void, @@ -668,6 +669,30 @@ impl Q36Graph { } } + /// FP8 lm_head (`omega_gpu_q36_graph_set_lm_head_fp8`): `[vocab][2048]` with 128 x 128 block scales, e.g. the exact + /// head's fp8b screen. The hidden is then written x 2^64 and every token's logits come from this head (FP8 values, + /// not the bf16 head's). A batched stroke runs it per head unless the process has `AIEN_OMEGA_Q36_FP8_HEAD=1` + /// (CUDApp DRA-69: one launch for every head). Null goes back to bf16. + /// + /// # Safety + /// `head` is null or a live `OmegaGpuFp8Tensor` that outlives every later token of this graph. + pub unsafe fn set_lm_head_fp8(&mut self, head: *mut c_void) -> Result<(), OmegaGpuError> { + #[cfg(has_omega_q36)] + { + // SAFETY: ptr is live; head per the caller's contract. + let rc = unsafe { ffi::omega_gpu_q36_graph_set_lm_head_fp8(self.ptr, head) }; + if rc != 0 { + return Err(session_err("q36 graph set_lm_head_fp8", rc)); + } + Ok(()) + } + #[cfg(not(has_omega_q36))] + { + let _ = head; + Err(OmegaGpuError::Unavailable) + } + } + /// Use the exact two-pass head for the next tokens (greedy) or the plain head (sampled). pub fn set_exact_head_on(&mut self, on: bool) { #[cfg(has_omega_q36)]