Skip to main content

memra_engine/
hybrid_forward.rs

1//! Hybrid forward pass (Stage-1, f32, prefill, single sequence). Per layer dispatches to a
2//! linear-attention (Gated DeltaNet) or full-attention mixer, then SwiGLU FFN. Matches
3//! llama.cpp src/models/qwen35.cpp node-for-node.
4
5use cudarc::driver::CudaSlice;
6use memra_gguf::config::ModelConfig;
7use crate::Engine;
8use crate::cache::Cache;
9
10/// Resident trunk transients for the eager prime (piecewise-graph foundation; see
11/// HybridModel::prime_slabs). Every buffer is fully overwritten before use per prime.
12pub struct PrimeSlabs {
13    pub t_cap: usize,
14    pub h: CudaSlice<f32>,
15    pub x1: CudaSlice<f32>,
16    pub z: CudaSlice<f32>,
17    pub act: CudaSlice<f32>,
18    pub xa: CudaSlice<f32>,
19    pub xb: CudaSlice<f32>,
20    pub h16: CudaSlice<u8>,
21    pub z16: CudaSlice<u8>,
22    /// piecewise boundary slabs (increment 2): GEMM outputs land here so the
23    /// downstream captured segments see fixed addresses.
24    pub gate: CudaSlice<f32>,     // t * n_ff_max
25    pub up: CudaSlice<f32>,       // t * n_ff_max
26    pub ffn_out: CudaSlice<f32>,  // t * n_embd
27    /// piecewise increment 3: per-layer S-glue segment graphs (down-add + next
28    /// attn-norm, ALL-slab IO, zero in-graph allocations -> keeperless capture is
29    /// clean). Baked at this t_cap; replay only when t == t_cap. seg_glue[il] fires
30    /// between layer il and il+1 (ping-pong parity is deterministic per il).
31    pub seg_glue: Vec<Option<cudarc::driver::CudaGraph>>,
32    /// increment 5 (core-split edition): the mixer out-GEMM writes _into_ `mixed`
33    /// directly (no staging copy — the increment-4 copy route was refuted), making
34    /// S-mid [add + post-norm] all-slab and capturable.
35    pub mixed: CudaSlice<f32>,
36    pub seg_mid: Vec<Option<cudarc::driver::CudaGraph>>,
37    pub seg_t: usize,
38}
39
40
41/// task #18 (attn side): one sequence's pre-attention outputs (post-rope q/k, v, out-gate).
42pub(crate) struct AttnPre {
43    pub q: cudarc::driver::CudaSlice<f32>,
44    pub k: cudarc::driver::CudaSlice<f32>,
45    pub v: cudarc::driver::CudaSlice<f32>,
46    pub gate: Option<cudarc::driver::CudaSlice<f32>>,
47}
48
49/// task #18: one sequence's GDN prep outputs (the scan inputs).
50pub(crate) struct GdnPrep {
51    pub hk: usize,
52    pub q_l2: cudarc::driver::CudaSlice<f32>,
53    pub k_l2: cudarc::driver::CudaSlice<f32>,
54    pub v_g: cudarc::driver::CudaSlice<f32>,
55    pub beta: cudarc::driver::CudaSlice<f32>,
56    pub g_log: cudarc::driver::CudaSlice<f32>,
57    pub kb16: Option<cudarc::driver::CudaSlice<u8>>,
58    pub qb16: Option<cudarc::driver::CudaSlice<u8>>,
59}
60
61/// Device scratch for the burst verify stream (see `verify_stream_scratch`).
62pub(crate) struct VerifyStreamScratch {
63    pub pos_d: CudaSlice<i32>,
64    pub row_ctrs: Vec<CudaSlice<i32>>,
65}
66use crate::hybrid::{HybridModel, Mixer, FullAttnLayer, LinearAttnLayer, MoeWeights};
67
68struct MoeInputTraceWriter {
69    dir: std::path::PathBuf,
70    index: std::fs::File,
71    payloads: std::collections::HashMap<u16, (std::fs::File, u64)>,
72}
73
74static MOE_INPUT_TRACE_WRITER: std::sync::OnceLock<
75    std::sync::Mutex<Option<MoeInputTraceWriter>>,
76> = std::sync::OnceLock::new();
77
78/// STAGE-2 GROUPED DECODE gate (MEMRA_MOE_GDEC, default ON; `=0` restores the sequential
79/// per-expert launch chain). See `moe_gdec_token`.
80fn gdec_enabled() -> bool {
81    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
82    *E.get_or_init(|| std::env::var("MEMRA_MOE_GDEC").map(|v| v != "0").unwrap_or(true))
83}
84
85/// Deterministic in-token expert prefetch. `MEMRA_MOE_PREFETCH=1` overlaps memory-source H2D on the
86/// copy stream; selecting the opt-in worker spill backend enables the same known-next hook for disk.
87fn moe_prefetch_enabled() -> bool {
88    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
89    *E.get_or_init(|| std::env::var("MEMRA_MOE_PREFETCH").as_deref() == Ok("1")
90        || crate::spill_pread::worker_enabled())
91}
92
93/// Best-effort OS page-cache prefetch distance for mmap-backed expert ranges. Independent of the
94/// H2D copy-stream experiment so storage->RAM and RAM->HBM overlap can be measured separately.
95/// The opt-in default stays one expert to preserve the original experiment; spill rigs can widen
96/// it with `MEMRA_MOE_PAGE_PREFETCH_WINDOW` to cover NVMe latency.
97fn moe_page_prefetch_window() -> usize {
98    static W: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
99    *W.get_or_init(|| page_prefetch_window_from_values(
100        std::env::var("MEMRA_MOE_PAGE_PREFETCH").as_deref() == Ok("1"),
101        std::env::var("MEMRA_MOE_PAGE_PREFETCH_WINDOW").ok().as_deref(),
102    ))
103}
104
105fn page_prefetch_window_from_values(enabled: bool, raw_window: Option<&str>) -> usize {
106    if !enabled {
107        return 0;
108    }
109    raw_window
110        .and_then(|value| value.parse().ok())
111        .unwrap_or(1)
112}
113
114/// Return only the newly exposed positions in a rolling lookahead window. Position zero seeds the
115/// full window; each later position adds one expert at the far edge. Thus widening the window does
116/// not repeatedly issue `MADV_WILLNEED` for the same range.
117fn page_prefetch_positions(
118    position: usize,
119    len: usize,
120    window: usize,
121) -> std::ops::Range<usize> {
122    if window == 0 || position >= len {
123        return len..len;
124    }
125    let (start, count) = if position == 0 {
126        (1, window)
127    } else {
128        (position.saturating_add(window), 1)
129    };
130    let start = start.min(len);
131    start..start.saturating_add(count).min(len)
132}
133
134/// Grouped worker-I/O schedule: prime the first active expert before the loop, then queue exactly
135/// one known-next expert at each iteration. Returning positions keeps expert ordering authoritative.
136fn grouped_worker_prefetch_position(order_len: usize, current: Option<usize>) -> Option<usize> {
137    let position = current.map_or(0, |position| position.saturating_add(1));
138    (position < order_len).then_some(position)
139}
140
141/// Fill the worker ring with complete experts, retaining one pinned buffer for an unexpected
142/// demand miss. Each expert has gate/up/down extents, so depth 16 admits a rolling five-expert
143/// window. Position zero primes the current expert too: its three independent reads can run in
144/// parallel instead of demand-serializing gate, up, and down before any useful GPU work exists.
145fn worker_prefetch_window() -> usize {
146    static WINDOW: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
147    *WINDOW.get_or_init(|| {
148        let automatic = crate::spill_pread::configured_depth().saturating_sub(1) / 3;
149        std::env::var("MEMRA_SPILL_WORKER_EXPERT_WINDOW")
150            .ok()
151            .and_then(|value| value.parse::<usize>().ok())
152            .unwrap_or(automatic.max(1))
153    })
154}
155
156/// Return only positions newly exposed by a rolling worker-I/O window. Unlike mmap page advice,
157/// this includes the current expert when the window is seeded so all three current projections
158/// enter the CPU pool together.
159fn worker_prefetch_positions(position: usize, len: usize, window: usize) -> std::ops::Range<usize> {
160    if window == 0 || position >= len {
161        return len..len;
162    }
163    let (start, count) = if position == 0 {
164        (0, window)
165    } else {
166        (position.saturating_add(window).saturating_sub(1), 1)
167    };
168    let start = start.min(len);
169    start..start.saturating_add(count).min(len)
170}
171
172/// LAUNCH-STRUCTURE STAGE 3 gate (MEMRA_MOE_DEV, default ON; `=0` restores host routing). The
173/// zero-DtoH device-dispatch path for fully-resident layers: router top-k output stays on device,
174/// expert weight pointers come from the per-layer device table. Requires the fused router (the
175/// dev path consumes the device sel/w directly), so MEMRA_FUSED_ROUTER=0 also disables it.
176fn moe_dev_enabled() -> bool {
177    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
178    *E.get_or_init(|| std::env::var("MEMRA_MOE_DEV").map(|v| v != "0").unwrap_or(true)
179        && !matches!(std::env::var("MEMRA_FUSED_ROUTER").as_deref(), Ok("0")))
180}
181
182/// MoE EXPERT dp4a gate (MEMRA_MOE_Q8, default ON; `=0` restores the Stage-A f32-dequant expert
183/// kernels). Applies when gate/up/down expert qtypes are all in the dp4a body set (IQ3_S/IQ4_XS).
184/// FP-order differs from Stage-A (int dp4a + warp tree) — argmax/run-gen/stream-identity gates
185/// arbitrate; the sequential and fused q8 paths ship as a matched pair (MEMRA_MOE_GATE contract).
186fn moe_q8_enabled() -> bool {
187    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
188    *E.get_or_init(|| std::env::var("MEMRA_MOE_Q8").map(|v| v != "0").unwrap_or(true))
189}
190
191/// gemma4 fast-arm gate: qtypes with an `expert_dot_g` dp4a body (superset used by the gelu
192/// dev arm; the qwen q8 arms keep their own battery-gated q8_expert_supported policy).
193fn expert_dp4a_supported(qt: i32) -> bool {
194    qt == crate::QT_Q4_0 || qt == crate::QT_IQ3_S || qt == crate::QT_IQ4_XS
195        || qt == crate::QT_Q3_K || qt == crate::QT_Q4_K || qt == crate::QT_Q6_K
196}
197
198fn q8_expert_supported(qt: i32) -> bool {
199    // k-quant arms added 2026-07-06 (Q3_K/Q4_K/Q6_K bodies for the UD tail layers). Briefly
200    // default-excluded the same day when they appeared to break 35B real-prompt spec — the
201    // ACTUAL culprit was the MoE router's cuBLASLt n-dependence (d994271); with the router
202    // decode-exact at verify t, the k-quant arms pass the full spec battery (p1/p2/p3 + raw
203    // K=1..8) and are DEFAULT ON again (+9 tok/s: 148.9 -> 157.9). MEMRA_MOE_Q8_KQ=0 excludes.
204    static KQ: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
205    let kq = *KQ.get_or_init(|| {
206        std::env::var("MEMRA_MOE_Q8_KQ").map(|v| v != "0").unwrap_or(true)
207    });
208    // NVFP4 experts: DEFAULT ON (2026-07-17). The M3-era "decode-vs-verify MISMATCH 3.4e1"
209    // that had this excluded was the missing per-expert macro-scale fold, fixed in the
210    // dev-kernel epilogues + moe_w_scale_by_expert; the 35B ct-NVFP4 artifact now runs the
211    // q8 arm at parity with the IQ4_XS daily (174-178 tok/s, spec K=1..8 exact). M3/Hy3
212    // never reach the q8 arms regardless (sigmoid-router cfg gates on pairs/dev/gdec).
213    // MEMRA_MOE_Q8_NVFP4=0 restores the f32 arm.
214    let nvfp4_q8 = std::env::var("MEMRA_MOE_Q8_NVFP4").map(|v| v != "0").unwrap_or(true);
215    qt == crate::QT_IQ3_S || qt == crate::QT_IQ4_XS || (nvfp4_q8 && qt == crate::QT_NVFP4)
216        || (kq && (qt == crate::QT_Q3_K || qt == crate::QT_Q4_K || qt == crate::QT_Q6_K))
217}
218
219/// The decode-once (_dec) and IQ-MMA expert kernels dequant via IQ-specific extractors —
220/// k-quant tensors must fall to the _em dot path instead.
221fn q8_expert_dec_supported(qt: i32) -> bool {
222    qt == crate::QT_IQ3_S || qt == crate::QT_IQ4_XS || qt == crate::QT_Q4_0
223}
224
225/// Grouped-f16 door (MEMRA_MOE_F16G) per-projection admission: the qtype has a dequant-to-f16
226/// kernel in cu/moe_f16_grouped.cu AND the projection's k dimension tiles its block size.
227/// Round 49 widened coverage to q35's UD mix (gate/up IQ3_S x39 + Q3_K x1 + IQ4_XS x1; down
228/// IQ4_XS x37 + Q6_K x3 + Q4_K x1) — the round-47 IQ4_XS/Q4_0-only table admitted ~1 of 41
229/// q35 layers, which is why that cell measured FLAT.
230fn f16g_proj_ok(qt: i32, in_f: usize) -> bool {
231    match qt {
232        crate::QT_Q4_0 => in_f % 32 == 0,
233        crate::QT_IQ4_XS | crate::QT_IQ3_S | crate::QT_Q3_K | crate::QT_Q4_K
234        | crate::QT_Q6_K => in_f % 256 == 0,
235        _ => false,
236    }
237}
238
239/// STAGE 3 prewarm gate (MEMRA_MOE_PREWARM, default ON; `=0` leaves residency organic). One-shot
240/// per layer: force-admit every block while FREE slots cover the whole layer (never evicts).
241fn moe_prewarm_enabled() -> bool {
242    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
243    *E.get_or_init(|| std::env::var("MEMRA_MOE_PREWARM").map(|v| v != "0").unwrap_or(true))
244}
245
246/// During a discarded fixed-residency profile, admit CPU-routed misses after their current-token
247/// CPU result is complete. The current result and numeric path are unchanged; later warmup tokens
248/// can then vote for and exercise those experts on GPU before the cache is frozen.
249fn cpu_expert_profile_admit_enabled() -> bool {
250    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
251    *E.get_or_init(|| std::env::var("MEMRA_CPU_EXPERT_FREEZE_PROFILE_ADMIT").as_deref() == Ok("1"))
252}
253
254/// Minimum prompt length for the BATCHED cache prime (`prime_cache`). Below this the tokenwise
255/// decode loop wins anyway (the batched path's GEMM dispatch needs m>=16, and the stateful conv
256/// kernel needs T >= d_conv-1). Callers: generate / generate_spec.
257pub const PRIME_MIN_T: usize = 16;
258
259impl HybridModel {
260    /// Prefill forward over `tokens`; returns logits [T, n_vocab] (host f32).
261    pub fn forward(&self, e: &Engine, tokens: &[u32]) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
262        if self.is_gemma4_e4b() { return self.gemma4_e4b_forward(e, tokens, false); }
263        if self.cfg.gemma4.is_some() { return self.gemma4_forward(e, tokens, false); }
264        let cfg = &self.cfg;
265        let n_embd = cfg.n_embd as usize;
266        let t = tokens.len();
267        let eps = cfg.rms_eps;
268        let pos: Vec<i32> = (0..t as i32).collect();
269        let pos_d = e.htod_i32(&pos)?;
270
271        let mut x = self.embed(e, tokens)?;   // [T, n_embd]
272
273        for (il, layer) in self.layers.iter().enumerate() {
274            // attn_norm
275            let mut h = e.uninit(t * n_embd)?;
276            e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
277
278            let mixed = match &layer.mixer {
279                Mixer::Full(fa) => self.full_attn(e, fa, &h, &pos_d, t)?,
280                Mixer::Linear(la) => self.linear_attn(e, la, &h, t)?,
281                Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
282            };
283
284            // residual 1
285            let mut x1 = e.uninit(t * n_embd)?;
286            e.add(&x, &mixed, &mut x1, t * n_embd)?;
287
288            // pre-FFN norm (post_attention_norm), FFN (Dense or MoE), residual 2
289            let mut z = e.uninit(t * n_embd)?;
290            e.rms_norm(&x1, layer.post_attn_norm.float_data(), &mut z, n_embd, t, eps)?;
291            let ffn_out = match &layer.ffn {
292                crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } => {
293                    let n_ff = ffn_gate.out_features();
294                    let mut g2 = e.matmul_group(&[ffn_gate, ffn_up], &z, t)?;
295                    let up = g2.pop().unwrap();
296                    let gate = g2.pop().unwrap();
297                    let mut act = e.uninit(t * n_ff)?;
298                    Self::ffn_act(e, &self.cfg, &gate, &up, &mut act, t * n_ff)?;
299                    e.matmul(ffn_down, &act, t)?
300                }
301                crate::hybrid::Ffn::Moe(m) => self.moe_ffn_il(e, m, &z, t, il as u16)?,
302            };
303            let mut x2 = e.uninit(t * n_embd)?;
304            e.add(&x1, &ffn_out, &mut x2, t * n_embd)?;
305            x = x2;
306        }
307
308        let mut hn = e.uninit(t * n_embd)?;
309        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
310        let logits = e.matmul(&self.output, &hn, t)?;
311        Ok(e.dtoh(&logits)?)
312    }
313
314    /// Prefill that returns ONLY the last token's logits — the common case (greedy/sample needs
315    /// just the final position to start decode). Runs the trunk over all T, then the lm_head
316    /// (output.weight, the largest matrix — 248320 rows) on the LAST hidden row ONLY, not all T.
317    /// On a 512-token prompt this turns a [512,248320] GEMM into [1,248320] — the dominant prefill
318    /// cost (nsys: ~99ms when done for all T). Bit-identical last-row logits to forward()[last].
319    pub fn forward_last(&self, e: &Engine, tokens: &[u32]) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
320        if self.cfg.gemma4.is_some() { return self.gemma4_forward(e, tokens, true); }
321        let cfg = &self.cfg;
322        let n_embd = cfg.n_embd as usize;
323        let t = tokens.len();
324        let eps = cfg.rms_eps;
325        let pos: Vec<i32> = (0..t as i32).collect();
326        let pos_d = e.htod_i32(&pos)?;
327
328        let mut x = self.embed(e, tokens)?;   // [T, n_embd]
329        // MEMRA_LAYER_PROBE=1: synchronize + print after every stage — bisects an in-graph
330        // ILLEGAL_ADDRESS to (layer, stage) at ~1 line of output per layer (M3 bring-up tool).
331        let probe = std::env::var("MEMRA_LAYER_PROBE").is_ok();
332        for (il, layer) in self.layers.iter().enumerate() {
333            let mut h = e.uninit(t * n_embd)?;
334            e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
335            if probe { e.stream().synchronize()?; eprintln!("[probe] L{il} norm ok"); }
336            let mixed = match &layer.mixer {
337                Mixer::Full(fa) => self.full_attn(e, fa, &h, &pos_d, t)?,
338                Mixer::Linear(la) => self.linear_attn(e, la, &h, t)?,
339                Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
340            };
341            if probe { e.stream().synchronize()?; eprintln!("[probe] L{il} mixer ok"); }
342            let mut x1 = e.uninit(t * n_embd)?;
343            e.add(&x, &mixed, &mut x1, t * n_embd)?;
344            let mut z = e.uninit(t * n_embd)?;
345            e.rms_norm(&x1, layer.post_attn_norm.float_data(), &mut z, n_embd, t, eps)?;
346            let ffn_out = match &layer.ffn {
347                crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } => {
348                    let n_ff = ffn_gate.out_features();
349                    let mut g2 = e.matmul_group(&[ffn_gate, ffn_up], &z, t)?;
350                    let up = g2.pop().unwrap();
351                    let gate = g2.pop().unwrap();
352                    let mut act = e.uninit(t * n_ff)?;
353                    Self::ffn_act(e, &self.cfg, &gate, &up, &mut act, t * n_ff)?;
354                    e.matmul(ffn_down, &act, t)?
355                }
356                crate::hybrid::Ffn::Moe(m) => self.moe_ffn_il(e, m, &z, t, il as u16)?,
357            };
358            if probe { e.stream().synchronize()?; eprintln!("[probe] L{il} ffn ok"); }
359            let mut x2 = e.uninit(t * n_embd)?;
360            e.add(&x1, &ffn_out, &mut x2, t * n_embd)?;
361            x = x2;
362        }
363        // norm over all T, then slice the LAST row and run lm_head on that single row.
364        let mut hn = e.uninit(t * n_embd)?;
365        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
366        let last = e.view(&hn, t * n_embd);            // [T, n_embd]
367        let last_row = last.slice((t - 1) * n_embd..t * n_embd);  // [1, n_embd]
368        let mut hlast = e.uninit(n_embd)?;
369        e.copy_view_into(&mut hlast, 0, &last_row, n_embd)?;
370        let logits = e.matmul(&self.output, &hlast, 1)?;   // [1, n_vocab] — lm_head on ONE row
371        Ok(e.dtoh(&logits)?)
372    }
373
374    /// BATCHED PROMPT PRIME (the measured #1 e2e gap, e2e-image-1): `forward_last`'s batched
375    /// prefill body EXTENDED to leave a DECODE-READY cache behind — vs the tokenwise prime's
376    /// ~102/38 tok/s (9B/27B) decode_step loop, this runs the whole prompt at prefill throughput.
377    ///   (a) full-attn layers append their T post-RoPE K/V rows into `cache.kv[il]` via the SAME
378    ///       per-row quantize kernel as the decode append (bit-identical cache bytes per row);
379    ///   (b) linear layers run STATEFULLY from the cache's current recurrent state (zero at a
380    ///       fresh prime): carried-ring conv (ssm_conv1d_tm_state) + ONE gdn_scan(state_in,
381    ///       state_out) whose internal sequential t-loop equals T chained T=1 steps — but with
382    ///       the NORMAL prefill matmul dispatch (GEMM at m>=16), NOT the decode-exact MMVQ the
383    ///       spec verify uses (prime is a prefill-regime pass; the run-gen prefill==decode
384    ///       argmax gate is the accuracy authority, exactly as for forward_last);
385    ///   (c) `cache.pos`/KV len/len_d advance by T.
386    /// Returns (last-row logits host, h_seed = last-row PRE-output_norm hidden [n_embd],
387    /// hiddens = the full pre-output_norm hidden stack [T, n_embd] — generate_spec's prompt_h).
388    /// FRESH-PROMPT ONLY (cache.pos == 0): the fa_prefill tiles attend within `tokens` alone.
389    /// forward_last itself stays untouched (kernel-check / run-gen gate on it).
390    pub fn prime_cache(&self, e: &Engine, tokens: &[u32], cache: &mut Cache)
391                       -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
392        let n_embd = self.cfg.n_embd as usize;
393        let t = tokens.len();
394        // SESSION CONTINUATION (2026-07-05): cache.pos > 0 = priming a NEW SUFFIX onto a live
395        // session cache — every chunk (including the first) takes the continuation arm
396        // (fa_prefill_view over the quantized past + this chunk). Fresh prime (pos==0) unchanged.
397        assert!(t >= PRIME_MIN_T, "prime_cache needs T >= {PRIME_MIN_T} (caller gates)");
398        assert!(cache.pos + t <= cache.max_ctx, "prime_cache: prompt exceeds cache max_ctx");
399
400        // CHUNKED PRIME (2026-07-05, the long-ctx OOM fix): the monolithic prime allocates
401        // per-layer transients proportional to T (gate/up/act = T*n_ff*4B EACH — 1.5GB apiece at
402        // 16k on the 27B), which OOMs a 24GB card around 16k prompt tokens. Chunk the prompt:
403        // each chunk runs the full layer stack with transients sized to the chunk, appending its
404        // K/V to the resident quantized cache and carrying the GDN conv-ring + recurrent state
405        // through `cache.recur` (linear_attn_prime is already stateful — a chunk boundary is
406        // exactly the state carry it was built for). Full-attn chunks after the first attend to
407        // the QUANTIZED past KV via fa_prefill_view (the spec-verify pattern) — same numeric
408        // class as decode reading the cache. Prompts <= one chunk take the ORIGINAL monolithic
409        // body byte-for-byte (chunk 0 short-circuits to the f32 fa_prefill path).
410        // MEMRA_PRIME_CHUNK sets the chunk size (tokens); 0 disables chunking (monolithic).
411        if self.is_gemma4_e4b() {
412            return self.gemma4_e4b_prime(e, tokens, cache);
413        }
414        if self.cfg.gemma4.is_some() {
415            // gemma4 v0: monolithic fresh-prompt prime (chunked/continuation arms later).
416            return self.gemma4_prime(e, tokens, cache);
417        }
418        let chunk: usize = std::env::var("MEMRA_PRIME_CHUNK").ok()
419            .and_then(|v| v.parse().ok()).unwrap_or(4096);
420        if chunk == 0 || t <= chunk {
421            return self.prime_chunk(e, tokens, cache);
422        }
423        let mut hiddens = e.uninit(t * n_embd)?;
424        let mut last: Option<(Vec<f32>, CudaSlice<f32>)> = None;
425        let mut start = 0usize;
426        while start < t {
427            // keep the tail chunk >= PRIME_MIN_T (the stateful conv needs T >= d_conv-1).
428            let mut end = (start + chunk).min(t);
429            if t - end > 0 && t - end < PRIME_MIN_T { end = t; }
430            let (l, hs, x) = self.prime_chunk(e, &tokens[start..end], cache)?;
431            e.copy_into(&mut hiddens, start * n_embd, &x, (end - start) * n_embd)?;
432            last = Some((l, hs));
433            start = end;
434        }
435        let (logits, h_seed) = last.unwrap();
436        Ok((logits, h_seed, hiddens))
437    }
438
439    /// One prime chunk: the full layer stack over `tokens`, continuing from the cache's current
440    /// state (`cache.pos` = tokens already primed; 0 = fresh). Positions/RoPE are absolute
441    /// task #21 de-broadcast: the q/k head count PREP must emit so the scan's consumers
442    /// agree. Compact (num_k) ONLY when the scan will take the chunked+mma route AND
443    /// num_k == num_v/2 (the engine-side hint mirrors this exact formula); everything
444    /// else (s128 verify tier, chunked-off, mma-off) keeps the broadcast layout.
445    fn gdn_hk(e: &Engine, t: usize, num_v: usize, num_k: usize) -> usize {
446        if Engine::gdn_db_on()
447            && Engine::gdn_chunked_enabled() && t >= 16
448            && e.gdn_mma_enabled(Engine::gdn_chunk_size())
449            && num_k * 2 == num_v
450        {
451            num_k
452        } else {
453            num_v
454        }
455    }
456
457    /// task #17: gate for the fused fp16-operand epilogues (silu_mul/gated_rmsnorm/sig_mul
458    /// `_f16out` twins). Bit-identical class (the twins emit the cvt kernel's exact halves),
459    /// but seam-gated (MEMRA_F16OUT=0) so a bit-check can arbitrate, and OFF under verify-exact
460    /// (matmul_group skips the f16 lane there; the twins must not resurrect it).
461    fn f16out_on(e: &Engine, t: usize) -> bool {
462        crate::f16_ffi::pp_f16_enabled() && t >= 16 && !e.verify_exact_on()
463            && std::env::var("MEMRA_F16OUT").as_deref() != Ok("0")
464    }
465
466    /// (cache.pos + i). Returns (last-row logits, h_seed, this chunk's hidden stack [T, n_embd]).
467    /// See HybridModel::prime_slabs — the eager prime's resident trunk transients.
468    pub fn prime_slabs_get(&self, e: &Engine, t: usize, n_embd: usize, n_ff_max: usize)
469                           -> Result<std::sync::MutexGuard<'_, Option<PrimeSlabs>>, Box<dyn std::error::Error>> {
470        let mut g = self.prime_slabs.lock().unwrap();
471        let need_new = match g.as_ref() { None => true, Some(sl) => sl.t_cap < t };
472        if need_new {
473            *g = Some(PrimeSlabs {
474                t_cap: t,
475                h: e.uninit(t * n_embd)?,
476                x1: e.uninit(t * n_embd)?,
477                z: e.uninit(t * n_embd)?,
478                act: e.uninit(t * n_ff_max)?,
479                xa: e.uninit(t * n_embd)?,
480                xb: e.uninit(t * n_embd)?,
481                h16: e.alloc_u8_uninit(t * n_embd * 2)?,
482                z16: e.alloc_u8_uninit(t * n_embd * 2)?,
483                gate: e.uninit(t * n_ff_max)?,
484                up: e.uninit(t * n_ff_max)?,
485                ffn_out: e.uninit(t * n_embd)?,
486                seg_glue: Vec::new(),
487                mixed: e.uninit(t * n_embd)?,
488                seg_mid: Vec::new(),
489                seg_t: 0,
490            });
491        }
492        Ok(g)
493    }
494
495    fn prime_chunk(&self, e: &Engine, tokens: &[u32], cache: &mut Cache)
496                       -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
497        let cfg = &self.cfg;
498        let n_embd = cfg.n_embd as usize;
499        let t = tokens.len();
500        let eps = cfg.rms_eps;
501        let base = cache.pos;
502        let pos: Vec<i32> = (base as i32..(base + t) as i32).collect();
503        let pos_d = e.htod_i32(&pos)?;
504
505        let x_embed = self.embed(e, tokens)?;   // [T, n_embd]
506        // task #14: fuse the fp16 GEMM-operand emission into the trunk norms (kills the
507        // standalone convert launches). Only when the f16 lane serves and T reaches the
508        // GEMM tier; bit-identical either way.
509        let f16fuse = crate::f16_ffi::pp_f16_enabled() && t >= 16;
510        // PRIME SLABS (piecewise foundation): trunk transients in resident buffers —
511        // ~224 fewer alloc/free calls per prime and FROZEN Lt operand addresses
512        // (nvjet's alignment-variant selection becomes run-to-run stable). Every slab is
513        // fully overwritten before use; x ping-pongs xa<->xb; the hidden-stack return
514        // clones the final x (the slab cannot leave). Non-slab fallback: MEMRA_PRIME_SLABS=0.
515        let n_ff_max = self.layers.iter().map(|l| match &l.ffn {
516            crate::hybrid::Ffn::Dense { ffn_gate, .. } => ffn_gate.out_features(),
517            _ => n_embd,
518        }).max().unwrap_or(n_embd).max(n_embd);
519        let use_slabs = std::env::var("MEMRA_PRIME_SLABS").as_deref() != Ok("0");
520        let mut slab_guard = if use_slabs {
521            Some(self.prime_slabs_get(e, t, n_embd, n_ff_max)?)
522        } else {
523            None
524        };
525        let mut x_own;   // fallback storage when slabs are off
526        type SlabRefs<'a> = (&'a mut CudaSlice<f32>, &'a mut CudaSlice<f32>, &'a mut CudaSlice<f32>, &'a mut CudaSlice<f32>, &'a mut CudaSlice<u8>, &'a mut CudaSlice<u8>, &'a mut CudaSlice<f32>, &'a mut CudaSlice<f32>, &'a mut CudaSlice<f32>);
527        let (mut x_cur, mut x_nxt, sl): (&mut CudaSlice<f32>, &mut CudaSlice<f32>, Option<SlabRefs>);
528        let mut seg: Option<(&mut Vec<Option<cudarc::driver::CudaGraph>>, &mut Vec<Option<cudarc::driver::CudaGraph>>, &mut CudaSlice<f32>, &mut usize)> = None;
529        let mut x_own2;
530        match slab_guard.as_mut() {
531            Some(g) => {
532                let slabs = g.as_mut().unwrap();
533                e.copy_into(&mut slabs.xa, 0, &x_embed, t * n_embd)?;
534                let PrimeSlabs { xa, xb, h, x1, z, act, h16, z16, gate, up, ffn_out, seg_glue, mixed, seg_mid, seg_t, .. } = slabs;
535                x_cur = xa;
536                x_nxt = xb;
537                seg = Some((seg_glue, seg_mid, mixed, seg_t));
538                sl = Some((h, x1, z, act, h16, z16, gate, up, ffn_out));
539            }
540            None => {
541                x_own = x_embed;
542                x_own2 = e.uninit(t * n_embd)?;
543                x_cur = &mut x_own;
544                x_nxt = &mut x_own2;
545                sl = None;
546            }
547        }
548        let mut alloc_h; let mut alloc_x1; let mut alloc_z; let mut alloc_act;
549        let mut alloc_h16; let mut alloc_z16;
550        let mut alloc_gate; let mut alloc_up; let mut alloc_fo;
551        let (h, x1, z, act): (&mut CudaSlice<f32>, &mut CudaSlice<f32>, &mut CudaSlice<f32>, &mut CudaSlice<f32>);
552        let (h16, z16): (&mut CudaSlice<u8>, &mut CudaSlice<u8>);
553        let (sl_gate, sl_up, sl_fo): (&mut CudaSlice<f32>, &mut CudaSlice<f32>, &mut CudaSlice<f32>);
554        match sl {
555            Some((a, b, c, d, e16, f16b, g, u, fo)) => {
556                h = a; x1 = b; z = c; act = d; h16 = e16; z16 = f16b;
557                sl_gate = g; sl_up = u; sl_fo = fo;
558            }
559            None => {
560                alloc_h = e.uninit(t * n_embd)?;
561                alloc_x1 = e.uninit(t * n_embd)?;
562                alloc_z = e.uninit(t * n_embd)?;
563                alloc_act = e.uninit(t * n_ff_max)?;
564                alloc_h16 = e.alloc_u8_uninit(t * n_embd * 2)?;
565                alloc_z16 = e.alloc_u8_uninit(t * n_embd * 2)?;
566                alloc_gate = e.uninit(t * n_ff_max)?;
567                alloc_up = e.uninit(t * n_ff_max)?;
568                alloc_fo = e.uninit(t * n_embd)?;
569                h = &mut alloc_h; x1 = &mut alloc_x1; z = &mut alloc_z; act = &mut alloc_act;
570                h16 = &mut alloc_h16; z16 = &mut alloc_z16;
571                sl_gate = &mut alloc_gate; sl_up = &mut alloc_up; sl_fo = &mut alloc_fo;
572            }
573        }
574        // piecewise increment 3: layer-0 norm hoisted; the per-layer tail fuses
575        // [down-add + NEXT layer's attn-norm] into one captured S-glue segment
576        // (all-slab IO, zero in-graph allocations). Capture happens lazily on the
577        // first prime at this t (capture does not execute -> launch right after).
578        let n_layers = self.layers.len();
579        // OPT-IN (2026-07-26 interleaved verdict): 2-kernel segments measured NET
580        // -0.7%-to-neutral under the interleaved A/B protocol — one cuGraphLaunch costs
581        // about what two kernel submissions do. The earlier "+0.9%" was cross-run clock
582        // drift (the repo's interleaved-A/B law exists for exactly this). Larger segments
583        // (S-prep/S-attn, 7-9 kernels) remain the open hypothesis; the core-split
584        // machinery stays (byte-identical) as their foundation.
585        let use_seg = f16fuse && seg.is_some()
586            && std::env::var("MEMRA_PRIME_SEG").as_deref() == Ok("1");
587        if let Some((sg, sm, _, st)) = seg.as_mut() {
588            if **st != t {
589                sg.clear();
590                sg.extend((0..n_layers).map(|_| None));
591                sm.clear();
592                sm.extend((0..n_layers).map(|_| None));
593                **st = t;
594            }
595        }
596        {
597            let layer0 = &self.layers[0];
598            if f16fuse {
599                e.rms_norm_f16out(x_cur, layer0.attn_norm.float_data(), h, h16, n_embd, t, eps)?;
600            } else {
601                e.rms_norm(x_cur, layer0.attn_norm.float_data(), h, n_embd, t, eps)?;
602            }
603        }
604        for (il, layer) in self.layers.iter().enumerate() {
605            let hx16 = if f16fuse { Some(&*h16) } else { None };
606            if use_seg {
607                // core-split path: projections -> _inner core -> out-GEMM INTO the mixed
608                // slab (no copies) -> S-mid segment [add + post-norm] as one graph launch.
609                let (pre, pre16, w_out) = match &layer.mixer {
610                    Mixer::Full(fa) => {
611                        let g3 = match hx16 {
612                            Some(xh) => e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv], h, xh, t)?,
613                            None => e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?,
614                        };
615                        let (pre, pre16) = self.full_attn_prime_core_inner(e, fa, g3, &pos_d, t, cache, il)?;
616                        (pre, pre16, &fa.wo)
617                    }
618                    Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
619                    Mixer::Linear(la) => {
620                        let ws = [&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha];
621                        let g4 = match hx16 {
622                            Some(xh) => e.matmul_group_xh(&ws, h, xh, t)?,
623                            None => e.matmul_group(&ws, h, t)?,
624                        };
625                        let (pre, pre16) = self.linear_attn_prime_core_pad_inner(e, la, g4, t, cache, il, None)?;
626                        (pre, pre16, &la.ssm_out)
627                    }
628                };
629                {
630                    let (_, sm, mslab, _) = seg.as_mut().unwrap();
631                    let pre_n = pre.len() / t;
632                    let xh_pre = match pre16 {
633                        Some(x) => x,
634                        None => e.f16_act(&pre, t * pre_n, pre_n)?,
635                    };
636                    if !e.try_f16_gemm_pre_into(w_out, &xh_pre, t, mslab)? {
637                        let y = e.matmul(w_out, &pre, t)?;
638                        e.copy_into(mslab, 0, &y, t * n_embd)?;
639                    }
640                    if sm[il].is_none() {
641                        use cudarc::driver::sys::{CUgraphInstantiate_flags, CUstreamCaptureMode};
642                        let w_post = layer.post_attn_norm.float_data();
643                        e.stream().synchronize()?;
644                        e.stream().begin_capture(CUstreamCaptureMode::CU_STREAM_CAPTURE_MODE_RELAXED)?;
645                        let r = (|| -> Result<(), Box<dyn std::error::Error>> {
646                            e.add(x_cur, mslab, x1, t * n_embd)?;
647                            e.rms_norm_f16out(x1, w_post, z, z16, n_embd, t, eps)?;
648                            Ok(())
649                        })();
650                        let g = e.stream().end_capture(
651                            CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH);
652                        r?;
653                        sm[il] = Some(g?.ok_or("S-mid capture produced no graph")?);
654                    }
655                    sm[il].as_ref().unwrap().launch()?;
656                }
657            } else {
658                let mixed = match &layer.mixer {
659                    Mixer::Full(fa) => self.full_attn_prime(e, fa, h, hx16, &pos_d, t, cache, il)?,
660                    Mixer::Linear(la) => self.linear_attn_prime(e, la, h, hx16, t, cache, il)?,
661                    Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
662                };
663                if f16fuse {
664                    // round 28: residual+norm in ONE kernel (add_rms_norm precedent,
665                    // bit-identical) — the standalone add pass disappears.
666                    e.add_rms_norm_f16out(x_cur, &mixed, layer.post_attn_norm.float_data(),
667                                          x1, z, z16, n_embd, t, eps)?;
668                } else {
669                    e.add(x_cur, &mixed, x1, t * n_embd)?;
670                    e.rms_norm(x1, layer.post_attn_norm.float_data(), z, n_embd, t, eps)?;
671                }
672            }
673            let zx16 = if f16fuse { Some(&*z16) } else { None };
674            match &layer.ffn {
675                crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } => {
676                    let n_ff = ffn_gate.out_features();
677                    // gate/up INTO boundary slabs (piecewise increment 2); fall back to
678                    // the allocating group + copy when a mirror is missing.
679                    let mut into_ok = false;
680                    if let Some(xh) = zx16 {
681                        into_ok = e.try_f16_gemm_pre_into(ffn_gate, xh, t, sl_gate)?
682                            && e.try_f16_gemm_pre_into(ffn_up, xh, t, sl_up)?;
683                    }
684                    if !into_ok {
685                        let mut g2 = match zx16 {
686                            Some(xh) => e.matmul_group_xh(&[ffn_gate, ffn_up], z, xh, t)?,
687                            None => e.matmul_group(&[ffn_gate, ffn_up], z, t)?,
688                        };
689                        let up_y = g2.pop().unwrap();
690                        let gate_y = g2.pop().unwrap();
691                        e.copy_into(sl_gate, 0, &gate_y, t * n_ff)?;
692                        e.copy_into(sl_up, 0, &up_y, t * n_ff)?;
693                    }
694                    // task #17: the silu arm's f16out twin emits the down GEMM's fp16
695                    // operand in-epilogue; non-silu activations keep the standalone convert.
696                    let act16 = if Self::f16out_on(e, t) && self.cfg.m3.is_none() {
697                        let mut a16 = e.alloc_u8_uninit(t * n_ff * 2)?;
698                        e.silu_mul_f16out(sl_gate, sl_up, act, &mut a16, t * n_ff)?;
699                        Some(a16)
700                    } else {
701                        Self::ffn_act(e, &self.cfg, sl_gate, sl_up, act, t * n_ff)?;
702                        None
703                    };
704                    // down GEMM into the ffn_out slab (f16 arm; fallback copies)
705                    let xh_act = match act16 {
706                        Some(x) => x,
707                        None => e.f16_act(act, t * n_ff, n_ff)?,
708                    };
709                    if !e.try_f16_gemm_pre_into(ffn_down, &xh_act, t, sl_fo)? {
710                        let y = e.matmul(ffn_down, &*act, t)?;
711                        e.copy_into(sl_fo, 0, &y, t * n_embd)?;
712                    }
713                }
714                crate::hybrid::Ffn::Moe(m) => {
715                    let y = self.moe_ffn_il(e, m, z, t, il as u16)?;
716                    e.copy_into(sl_fo, 0, &y, t * n_embd)?;
717                }
718            }
719            if use_seg && il + 1 < n_layers {
720                // S-glue segment: [add + next attn-norm(+f16out)] — one cuGraphLaunch
721                let w_next = self.layers[il + 1].attn_norm.float_data();
722                let (sg, _, _, _) = seg.as_mut().unwrap();
723                if sg[il].is_none() {
724                    use cudarc::driver::sys::{CUgraphInstantiate_flags, CUstreamCaptureMode};
725                    e.stream().synchronize()?;
726                    e.stream().begin_capture(CUstreamCaptureMode::CU_STREAM_CAPTURE_MODE_RELAXED)?;
727                    let r = (|| -> Result<(), Box<dyn std::error::Error>> {
728                        e.add(x1, sl_fo, x_nxt, t * n_embd)?;
729                        e.rms_norm_f16out(x_nxt, w_next, h, h16, n_embd, t, eps)?;
730                        Ok(())
731                    })();
732                    let g = e.stream().end_capture(
733                        CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH);
734                    r?;
735                    sg[il] = Some(g?.ok_or("S-glue capture produced no graph")?);
736                }
737                sg[il].as_ref().unwrap().launch()?;
738            } else {
739                if il + 1 < n_layers {
740                    let w_next = self.layers[il + 1].attn_norm.float_data();
741                    if f16fuse {
742                        e.add_rms_norm_f16out(x1, sl_fo, w_next, x_nxt, h, h16, n_embd, t, eps)?;
743                    } else {
744                        e.add(x1, sl_fo, x_nxt, t * n_embd)?;
745                        e.rms_norm(x_nxt, w_next, h, n_embd, t, eps)?;
746                    }
747                } else {
748                    e.add(x1, sl_fo, x_nxt, t * n_embd)?;
749                }
750            }
751            std::mem::swap(&mut x_cur, &mut x_nxt);
752        }
753        // hidden-stack return: clone the final x out of the slab
754        let mut x = e.uninit(t * n_embd)?;
755        e.copy_into(&mut x, 0, x_cur, t * n_embd)?;
756        drop(slab_guard);
757
758        // h_seed = LAST row of x BEFORE output_norm (MTP-PLAN §A default) or AFTER it
759        // (MEMRA_SPEC_HPOST — reference convention; hn is computed just below either way, so
760        // the post-norm copy happens after hn exists).
761        let mut h_seed = e.uninit(n_embd)?;
762        if !crate::spec::spec_hpost() {
763            e.copy_view_into(&mut h_seed, 0, &x.slice((t - 1) * n_embd..t * n_embd), n_embd)?;
764        }
765        // last-row logits, exactly like forward_last (norm all T — per-row op — then lm_head on 1 row).
766        let mut hn = e.uninit(t * n_embd)?;
767        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
768        if crate::spec::spec_hpost() {
769            e.copy_view_into(&mut h_seed, 0, &hn.slice((t - 1) * n_embd..t * n_embd), n_embd)?;
770        }
771        let last = e.view(&hn, t * n_embd);
772        let last_row = last.slice((t - 1) * n_embd..t * n_embd);
773        let mut hlast = e.uninit(n_embd)?;
774        e.copy_view_into(&mut hlast, 0, &last_row, n_embd)?;
775        let logits = e.matmul(&self.output, &hlast, 1)?;
776        cache.pos += t;
777        // Hidden stack handed to generate_spec as prompt_h: pre-norm x (default) or the full
778        // post-norm stack hn (MEMRA_SPEC_HPOST).
779        Ok((e.dtoh(&logits)?, h_seed, if crate::spec::spec_hpost() { hn } else { x }))
780    }
781
782    /// CAPTURE-SAFE prime trunk (task #14 increment 1, ARCHITECTURE-H100.md design v2):
783    /// prime_chunk's layer stack with every capture hazard hoisted — `x` is the
784    /// PRE-EMBEDDED input in a STABLE graph-input buffer (embed + its 8MB htod stay
785    /// eager, one launch), `pos_d` is a baked device param (fresh prime = 0..T, constant
786    /// per bucket), logits/h_seed/hidden stay DEVICE-resident (no dtoh), and cache.pos is
787    /// NOT advanced (host state — the replay wrapper owns it). Body mirrors prime_chunk
788    /// (the prime-graph-gate pins them together). KNOWN smoke-scope gap: append host-len
789    /// bookkeeping still runs on the host per call — the real replay path moves the write
790    /// slot to the len_d device counter (increment 3).
791    /// GRAPH-OUTPUT CONTRACT: results are COPIED into caller-provided stable buffers
792    /// (`logits_out` [n_vocab], `h_seed_out` [n_embd]) — every internal allocation drops
793    /// INSIDE the capture region (alloc+free node pairs). Retaining an in-capture
794    /// allocation across end_capture makes instantiate throw INVALID_VALUE (smoke finding
795    /// 2026-07-26), and under AUTO_FREE_ON_LAUNCH its address wouldn't survive a launch
796    /// anyway — the decode GraphSession's pre-allocated-output pattern is the law here.
797    pub fn prime_chunk_captured(&self, e: &Engine, x_in: &CudaSlice<f32>, pos_d: &CudaSlice<i32>,
798                                t: usize, cache: &mut Cache,
799                                len_d: &CudaSlice<i32>,
800                                logits_out: &mut CudaSlice<f32>, h_seed_out: &mut CudaSlice<f32>)
801                                -> Result<(), Box<dyn std::error::Error>> {
802        let cfg = &self.cfg;
803        let n_embd = cfg.n_embd as usize;
804        let eps = cfg.rms_eps;
805        let f16fuse = crate::f16_ffi::pp_f16_enabled() && t >= 16;
806        let mut x = e.uninit(t * n_embd)?;
807        e.copy_into(&mut x, 0, x_in, t * n_embd)?;
808        for (il, layer) in self.layers.iter().enumerate() {
809            let mut h = e.uninit(t * n_embd)?;
810            let mut hx16: Option<CudaSlice<u8>> = None;
811            if f16fuse {
812                let mut b16 = e.alloc_u8_uninit(t * n_embd * 2)?;
813                e.rms_norm_f16out(&x, layer.attn_norm.float_data(), &mut h, &mut b16, n_embd, t, eps)?;
814                hx16 = Some(b16);
815            } else {
816                e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
817            }
818            let mixed = match &layer.mixer {
819                Mixer::Full(fa) => self.full_attn_prime(e, fa, &h, hx16.as_ref(), pos_d, t, cache, il)?,
820                Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
821                Mixer::Linear(la) => {
822                    let ws = [&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha];
823                    let g4 = match hx16.as_ref() {
824                        Some(xh) => e.matmul_group_xh(&ws, &h, xh, t)?,
825                        None => e.matmul_group(&ws, &h, t)?,
826                    };
827                    self.linear_attn_prime_core_pad(e, la, g4, t, cache, il, Some(len_d))?
828                }
829            };
830            let mut x1 = e.uninit(t * n_embd)?;
831            e.add(&x, &mixed, &mut x1, t * n_embd)?;
832            let mut z = e.uninit(t * n_embd)?;
833            let mut zx16: Option<CudaSlice<u8>> = None;
834            if f16fuse {
835                let mut b16 = e.alloc_u8_uninit(t * n_embd * 2)?;
836                e.rms_norm_f16out(&x1, layer.post_attn_norm.float_data(), &mut z, &mut b16, n_embd, t, eps)?;
837                zx16 = Some(b16);
838            } else {
839                e.rms_norm(&x1, layer.post_attn_norm.float_data(), &mut z, n_embd, t, eps)?;
840            }
841            let ffn_out = match &layer.ffn {
842                crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } => {
843                    let n_ff = ffn_gate.out_features();
844                    let mut g2 = match &zx16 {
845                        Some(xh) => e.matmul_group_xh(&[ffn_gate, ffn_up], &z, xh, t)?,
846                        None => e.matmul_group(&[ffn_gate, ffn_up], &z, t)?,
847                    };
848                    let up = g2.pop().unwrap();
849                    let gate = g2.pop().unwrap();
850                    let mut act = e.uninit(t * n_ff)?;
851                    Self::ffn_act(e, &self.cfg, &gate, &up, &mut act, t * n_ff)?;
852                    e.matmul(ffn_down, &act, t)?
853                }
854                crate::hybrid::Ffn::Moe(m) => self.moe_ffn_il(e, m, &z, t, il as u16)?,
855            };
856            let mut x2 = e.uninit(t * n_embd)?;
857            e.add(&x1, &ffn_out, &mut x2, t * n_embd)?;
858            x = x2;
859        }
860        // device-indexed TRUE last row (pads sit past it in a bucketed graph)
861        if !crate::spec::spec_hpost() {
862            e.row_gather_dev(&x, h_seed_out, len_d, n_embd)?;
863        }
864        let mut hn = e.uninit(t * n_embd)?;
865        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
866        if crate::spec::spec_hpost() {
867            e.row_gather_dev(&hn, h_seed_out, len_d, n_embd)?;
868        }
869        let mut hlast = e.uninit(n_embd)?;
870        e.row_gather_dev(&hn, &mut hlast, len_d, n_embd)?;
871        let logits = e.matmul(&self.output, &hlast, 1)?;
872        let nv = logits.len();
873        e.copy_into(logits_out, 0, &logits, nv)?;
874        Ok(())
875    }
876
877    /// Cross-request BATCHED fresh prime (task #13, design in ARCHITECTURE-H100.md): the
878    /// trunk's token-parallel ops (embed, norms, adds, ffn, projection GROUPS) run once on
879    /// the CONCATENATION of B sequences — GEMMs at m = sum_T, the continuous-batching win
880    /// the serving-lane bench attributed the remaining vLLM gap to. The stateful mixer
881    /// CORES (QK-norm/RoPE/FA/append, conv/GDN scans) run per sequence on split projection
882    /// buffers (D2D row copies; the mixers' own out-projections stay per-seq this
883    /// increment). CONTINUATION primes (increment (b), 2026-07-30): cache.pos > 0 seqs
884    /// batch the projections/FFN/lm_head exactly like fresh; the mixer cores take the
885    /// per-seq CONTINUATION arms (Full: core_inner with carried pos_d + fa_prefill_view
886    /// over the quantized past; Linear: the stateful pad_view twin — the same state
887    /// carry the chunked single-seq prime rides). The fresh-only favl/gdn-vl fast paths
888    /// stay byte-identical (gated on !carried). gemma4 models have no continuation
889    /// prime (v0 monolithic fresh) — carried gemma4 batches return Err (caller falls
890    /// back to single-chunk serving).
891    /// NUMERIC CONFIG: a concat GEMM tiles K differently than per-seq GEMMs — same class
892    /// as every prefill GEMM change; prime_batch_gate arbitrates (argmax + stream battery).
893    pub fn prime_cache_batch(&self, e: &Engine, prompts: &[&[u32]], caches: &mut [&mut Cache])
894                             -> Result<Vec<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
895        let cfg = &self.cfg;
896        let n_embd = cfg.n_embd as usize;
897        let eps = cfg.rms_eps;
898        let b = prompts.len();
899        assert!(b >= 1 && b == caches.len());
900        let pos0s: Vec<usize> = caches.iter().map(|c| c.pos).collect();
901        let carried = pos0s.iter().any(|&p| p > 0);
902        if carried && cfg.gemma4.is_some() {
903            return Err("prime_cache_batch: gemma4 has no continuation prime (v0 fresh-only)".into());
904        }
905        let ts: Vec<usize> = prompts.iter().map(|p| p.len()).collect();
906        for &t in &ts { assert!(t >= PRIME_MIN_T, "prime_cache_batch needs T >= {PRIME_MIN_T}"); }
907        for (s, c) in caches.iter().enumerate() {
908            assert!(c.pos + ts[s] <= c.max_ctx, "prime_cache_batch: prompt exceeds cache max_ctx");
909        }
910        let total: usize = ts.iter().sum();
911        let offs: Vec<usize> = ts.iter().scan(0usize, |a, &t| { let o = *a; *a += t; Some(o) }).collect();
912        // per-seq positions (fresh: 0..T_s; continuation: pos0..pos0+T_s)
913        let pos_ds: Vec<CudaSlice<i32>> = ts.iter().zip(&pos0s)
914            .map(|(&t, &p0)| e.htod_i32(&(p0 as i32..(p0 + t) as i32).collect::<Vec<_>>()))
915            .collect::<Result<_, _>>()?;
916        // split a concat [total, dim] buffer into per-seq copies
917        let split = |e: &Engine, y: &CudaSlice<f32>, dim: usize|
918                     -> Result<Vec<CudaSlice<f32>>, Box<dyn std::error::Error>> {
919            let mut out = Vec::with_capacity(b);
920            for s in 0..b {
921                let mut ys = e.uninit(ts[s] * dim)?;
922                e.copy_view_into(&mut ys, 0, &y.slice(offs[s] * dim..(offs[s] + ts[s]) * dim), ts[s] * dim)?;
923                out.push(ys);
924            }
925            Ok(out)
926        };
927
928        let cat_tokens: Vec<u32> = prompts.iter().flat_map(|p| p.iter().copied()).collect();
929        let mut x = self.embed(e, &cat_tokens)?;   // [total, n_embd]
930        for (il, layer) in self.layers.iter().enumerate() {
931            let mut h = e.uninit(total * n_embd)?;
932            let mut hx16 = e.alloc_u8_uninit(total * n_embd * 2)?;
933            e.rms_norm_f16out(&x, layer.attn_norm.float_data(), &mut h, &mut hx16, n_embd, total, eps)?;
934            // mixer: projection GROUP on the concat (m = total), stateful core per seq
935            let mut mixed = e.uninit(total * n_embd)?;
936            match &layer.mixer {
937                Mixer::Full(fa) => {
938                    let g3 = e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv], &h, &hx16, total)?;
939                    // task #18 (attn side): the WHOLE attn core is varlen for fresh gated
940                    // batches — split/QK-norm/RoPE/append (attn_pre_vl8, view inputs: the
941                    // q/k/v split copies vanish) + ONE varlen FA. Per-block math identical
942                    // everywhere (bit-gateable); MEMRA_FA_VL=0 or a non-bf16kv config falls
943                    // back to the per-seq dispatch.
944                    let (n_head, n_head_kv, head_dim) =
945                        (self.cfg.n_head as usize, self.cfg.n_head_kv as usize, self.cfg.head_dim_k as usize);
946                    let fa_scale = 1.0 / (head_dim as f32).sqrt();
947                    let use_favl = !carried
948                        && (2..=8).contains(&b)
949                        && (head_dim == 256 || head_dim == 128)
950                        && self.cfg.attn_out_gate()
951                        && std::env::var("MEMRA_NOFA").is_err()
952                        && std::env::var("MEMRA_FA_FLOOR").is_err()
953                        && std::env::var("MEMRA_FA_PP_W2").as_deref() != Ok("1")
954                        && std::env::var("MEMRA_FA_BF16KV").as_deref() != Ok("0")
955                        && std::env::var("MEMRA_FA_VL").as_deref() != Ok("0");
956                    if use_favl {
957                        let (qf_w, kf_w, vf_w) =
958                            (fa.wq.out_features(), fa.wk.out_features(), fa.wv.out_features());
959                        struct APre {
960                            q: CudaSlice<f32>, gate: Option<CudaSlice<f32>>,
961                            qn: CudaSlice<f32>, kn: CudaSlice<f32>,
962                        }
963                        let mut aps = Vec::with_capacity(b);
964                        for &t in ts.iter().take(b) {
965                            aps.push(APre {
966                                q: e.uninit(t * n_head * head_dim)?,
967                                gate: Some(e.uninit(t * n_head * head_dim)?),
968                                qn: e.uninit(t * n_head * head_dim)?,
969                                kn: e.uninit(t * n_head_kv * head_dim)?,
970                            });
971                        }
972                        let (kv_dim_k, kv_dim_v, ktb, vtb) = {
973                            let kvl = caches[0].kv[il].as_ref().unwrap();
974                            (kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes)
975                        };
976                        let pargs: Vec<crate::AttnPreVl> = (0..b).map(|s| {
977                            let (o, t) = (offs[s], ts[s]);
978                            let kvl = caches[s].kv[il].as_ref().unwrap();
979                            assert!(kvl.len == 0 && kvl.len + t <= caches[s].max_ctx,
980                                    "prime_cache_batch attn vl: fresh + capacity");
981                            crate::AttnPreVl {
982                                qf: e.addr_f32v(&g3[0].slice(o * qf_w..(o + t) * qf_w)),
983                                kf: e.addr_f32v(&g3[1].slice(o * kf_w..(o + t) * kf_w)),
984                                vf: e.addr_f32v(&g3[2].slice(o * vf_w..(o + t) * vf_w)),
985                                q: e.addr_f32(&aps[s].q),
986                                gate: e.addr_f32(aps[s].gate.as_ref().unwrap()),
987                                qn: e.addr_f32(&aps[s].qn), kn: e.addr_f32(&aps[s].kn),
988                                kc: e.addr_u8(&kvl.k), vc: e.addr_u8(&kvl.v),
989                                t: t as i32, pad: 0,
990                            }
991                        }).collect();
992                        e.attn_pre_vl8(&pargs, fa.q_norm.float_data(), fa.k_norm.float_data(),
993                                       head_dim, self.cfg.rope_dim_count as usize, n_head, n_head_kv,
994                                       self.cfg.rms_eps, self.cfg.rope_freq_base, 1.0,
995                                       kv_dim_k, kv_dim_v, ktb, vtb)?;
996                        for s in 0..b {
997                            let kvl = caches[s].kv[il].as_mut().unwrap();
998                            kvl.len += ts[s];
999                            let new_len = kvl.len as i32;
1000                            e.set_i32_one(&mut kvl.len_d, new_len)?;
1001                        }
1002                        let mut attns = Vec::with_capacity(b);
1003                        let mut mirrors = Vec::with_capacity(b);
1004                        for &t in ts.iter().take(b) {
1005                            attns.push(e.uninit(t * n_head * head_dim)?);
1006                            let n = t * n_head_kv * head_dim;
1007                            mirrors.push((e.alloc_u8_uninit(n * 2)?, e.alloc_u8_uninit(n * 2)?));
1008                        }
1009                        // FA3 batched twin (round 31): TMA-swizzled wgmma vl when the
1010                        // promoted single-seq config is on; else the mma favl.
1011                        let fa3_on = match std::env::var("MEMRA_FA3").as_deref() {
1012                            Ok("0") => false,
1013                            Ok("1") => true,
1014                            _ => cfg!(memra_hopper_mma),
1015                        };
1016                        if fa3_on {
1017                            let mut q16s = Vec::with_capacity(b);
1018                            let mut v16s = Vec::with_capacity(b);
1019                            for s in 0..b {
1020                                let t = ts[s];
1021                                let mut q16 = e.alloc_u8_uninit(t * n_head * head_dim * 2)?;
1022                                e.f32_to_bf16_into(&aps[s].qn, &mut q16, t * n_head * head_dim)?;
1023                                let mut k16 = e.alloc_u8_uninit(t * n_head_kv * head_dim * 2)?;
1024                                e.f32_to_bf16_into(&aps[s].kn, &mut k16, t * n_head_kv * head_dim)?;
1025                                let mut v16 = e.alloc_u8_uninit(t * n_head_kv * head_dim * 2)?;
1026                                e.f32_to_bf16_v(&g3[2].slice(offs[s] * vf_w..(offs[s] + t) * vf_w),
1027                                                &mut v16, t * n_head_kv * head_dim)?;
1028                                q16s.push(q16);
1029                                v16s.push((k16, v16));
1030                            }
1031                            let mut qp = [core::ptr::null::<core::ffi::c_void>(); 8];
1032                            let mut kp = qp;
1033                            let mut vp = qp;
1034                            let mut op = [core::ptr::null_mut::<f32>(); 8];
1035                            let mut tsv = [0i32; 8];
1036                            for s in 0..b {
1037                                qp[s] = e.addr_u8(&q16s[s]) as *const core::ffi::c_void;
1038                                kp[s] = e.addr_u8(&v16s[s].0) as *const core::ffi::c_void;
1039                                vp[s] = e.addr_u8(&v16s[s].1) as *const core::ffi::c_void;
1040                                op[s] = e.addr_f32(&attns[s]) as *mut f32;
1041                                tsv[s] = ts[s] as i32;
1042                            }
1043                            let rc = unsafe {
1044                                crate::fa3_vl_raw(qp.as_ptr(), kp.as_ptr(), vp.as_ptr(), op.as_ptr(),
1045                                                  tsv.as_ptr(), b as i32, n_head as i32,
1046                                                  n_head_kv as i32, head_dim as i32, fa_scale,
1047                                                  e.stream().cu_stream() as *mut core::ffi::c_void)
1048                            };
1049                            if rc != 0 {
1050                                return Err(format!("memra_fa3_vl rc={rc}").into());
1051                            }
1052                        } else {
1053                            let fargs: Vec<crate::FaSeqVl> = (0..b).map(|s| crate::FaSeqVl {
1054                                q: e.addr_f32(&aps[s].qn), k16: e.addr_u8(&mirrors[s].0),
1055                                v16: e.addr_u8(&mirrors[s].1), o: e.addr_f32(&attns[s]),
1056                                kf: e.addr_f32(&aps[s].kn),
1057                                vf: e.addr_f32v(&g3[2].slice(offs[s] * vf_w..(offs[s] + ts[s]) * vf_w)),
1058                                t: ts[s] as i32, pad: 0,
1059                            }).collect();
1060                            e.fa_prefill_vl8(&fargs, head_dim, n_head, n_head_kv, fa_scale)?;
1061                        }
1062                        for (s, attn) in attns.into_iter().enumerate() {
1063                            let (attn_g, ag16) = self.full_attn_prime_post_fa(
1064                                e, attn, &aps[s].gate, ts[s], n_head, head_dim)?;
1065                            let mut done = false;
1066                            if let Some(xh) = &ag16 {
1067                                done = e.try_f16_gemm_pre_into_off(&fa.wo, xh, ts[s], &mut mixed, offs[s] * n_embd)?;
1068                            }
1069                            if !done {
1070                                let m = e.matmul(&fa.wo, &attn_g, ts[s])?;
1071                                e.copy_into(&mut mixed, offs[s] * n_embd, &m, ts[s] * n_embd)?;
1072                            }
1073                        }
1074                    } else {
1075                        let mut parts: Vec<Vec<CudaSlice<f32>>> = (0..b).map(|_| Vec::new()).collect();
1076                        for (w, y) in [&fa.wq, &fa.wk, &fa.wv].iter().zip(g3) {
1077                            for (s, ys) in split(e, &y, w.out_features())?.into_iter().enumerate() {
1078                                parts[s].push(ys);
1079                            }
1080                        }
1081                        for (s, g3s) in parts.into_iter().enumerate() {
1082                            // task #16 gather removal: wo writes into `mixed` at offs[s] directly.
1083                            let (attn_g, ag16) = self.full_attn_prime_core_inner(
1084                                e, fa, g3s, &pos_ds[s], ts[s], caches[s], il)?;
1085                            let mut done = false;
1086                            if let Some(xh) = &ag16 {
1087                                done = e.try_f16_gemm_pre_into_off(&fa.wo, xh, ts[s], &mut mixed, offs[s] * n_embd)?;
1088                            }
1089                            if !done {
1090                                let m = e.matmul(&fa.wo, &attn_g, ts[s])?;
1091                                e.copy_into(&mut mixed, offs[s] * n_embd, &m, ts[s] * n_embd)?;
1092                            }
1093                        }
1094                    }
1095                }
1096                Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
1097                Mixer::Linear(la) => {
1098                    // task #16: NO split copies (cores read row-offset views of the concat
1099                    // outputs; out-GEMMs write into `mixed` at offs[s]). task #18: the core
1100                    // itself is BATCHED — per-seq prep/K1-K3, then ONE varlen K4 + ONE
1101                    // varlen K5 launch for all sequences.
1102                    let ws = [&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha];
1103                    let g4 = e.matmul_group_xh(&ws, &h, &hx16, total)?;
1104                    let outs = self.linear_attn_prime_core_batch(e, la, &g4, &offs, &ts, caches, il)?;
1105                    for (s, (gn, gn16)) in outs.into_iter().enumerate() {
1106                        let (o, t) = (offs[s], ts[s]);
1107                        let mut done = false;
1108                        if let Some(xh) = &gn16 {
1109                            done = e.try_f16_gemm_pre_into_off(&la.ssm_out, xh, t, &mut mixed, o * n_embd)?;
1110                        }
1111                        if !done {
1112                            let m = e.matmul(&la.ssm_out, &gn, t)?;
1113                            e.copy_into(&mut mixed, o * n_embd, &m, t * n_embd)?;
1114                        }
1115                    }
1116                }
1117            }
1118            let mut x1 = e.uninit(total * n_embd)?;
1119            let mut z = e.uninit(total * n_embd)?;
1120            let mut zx16 = e.alloc_u8_uninit(total * n_embd * 2)?;
1121            e.add_rms_norm_f16out(&x, &mixed, layer.post_attn_norm.float_data(),
1122                                  &mut x1, &mut z, &mut zx16, n_embd, total, eps)?;
1123            let ffn_out = match &layer.ffn {
1124                crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } => {
1125                    let n_ff = ffn_gate.out_features();
1126                    let mut g2 = e.matmul_group_xh(&[ffn_gate, ffn_up], &z, &zx16, total)?;
1127                    let up = g2.pop().unwrap();
1128                    let gate = g2.pop().unwrap();
1129                    let mut act = e.uninit(total * n_ff)?;
1130                    // task #17 (batch trunk): silu twin emits the down GEMM's fp16 operand
1131                    // in-epilogue (nsys round-26: this trunk still paid 32 cvt passes).
1132                    if Self::f16out_on(e, total) && self.cfg.m3.is_none() {
1133                        let mut a16 = e.alloc_u8_uninit(total * n_ff * 2)?;
1134                        e.silu_mul_f16out(&gate, &up, &mut act, &mut a16, total * n_ff)?;
1135                        match e.try_f16_gemm_pre(ffn_down, &a16, total)? {
1136                            Some(y) => y,
1137                            None => e.matmul(ffn_down, &act, total)?,
1138                        }
1139                    } else {
1140                        Self::ffn_act(e, &self.cfg, &gate, &up, &mut act, total * n_ff)?;
1141                        e.matmul(ffn_down, &act, total)?
1142                    }
1143                }
1144                crate::hybrid::Ffn::Moe(m) => self.moe_ffn_il(e, m, &z, total, il as u16)?,
1145            };
1146            let mut x2 = e.uninit(total * n_embd)?;
1147            e.add(&x1, &ffn_out, &mut x2, total * n_embd)?;
1148            x = x2;
1149        }
1150        // epilogue per seq (identical math to prime_chunk: norm all rows, lm_head on last row)
1151        let mut hn = e.uninit(total * n_embd)?;
1152        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, total, eps)?;
1153        // batched lm_head (nsys round-26: B sequential m=1 matvecs re-read the 600MB
1154        // lm_head weight B times — 2.2ms at B=6): gather the B last rows and run ONE
1155        // m=B GEMM (f16 lane; falls back to the per-seq matvec when no mirror). The
1156        // f16-vs-mmvq logits delta is the prefill GEMM numeric class — prime_batch_gate's
1157        // argmax battery arbitrates, same as every other prefill GEMM change.
1158        let mut hcat = e.uninit(b * n_embd)?;
1159        for s in 0..b {
1160            let last0 = (offs[s] + ts[s] - 1) * n_embd;
1161            e.copy_view_into(&mut hcat, s * n_embd, &hn.slice(last0..last0 + n_embd), n_embd)?;
1162        }
1163        let logits_cat = if b >= 2 { e.try_f16_gemm(&self.output, &hcat, b)? } else { None };
1164        let logits_host: Option<Vec<f32>> = match &logits_cat {
1165            Some(lc) => Some(e.dtoh(lc)?),
1166            None => None,
1167        };
1168        let n_vocab = self.output.out_features();
1169        let mut hidden_all = if crate::spec::spec_hpost() {
1170            split(e, &hn, n_embd)?
1171        } else {
1172            split(e, &x, n_embd)?
1173        };
1174        let mut out = Vec::with_capacity(b);
1175        for s in 0..b {
1176            let last0 = (offs[s] + ts[s] - 1) * n_embd;
1177            let mut h_seed = e.uninit(n_embd)?;
1178            if !crate::spec::spec_hpost() {
1179                e.copy_view_into(&mut h_seed, 0, &x.slice(last0..last0 + n_embd), n_embd)?;
1180            } else {
1181                e.copy_view_into(&mut h_seed, 0, &hn.slice(last0..last0 + n_embd), n_embd)?;
1182            }
1183            let logits = match &logits_host {
1184                Some(lh) => lh[s * n_vocab..(s + 1) * n_vocab].to_vec(),
1185                None => {
1186                    let mut hlast = e.uninit(n_embd)?;
1187                    e.copy_view_into(&mut hlast, 0, &hn.slice(last0..last0 + n_embd), n_embd)?;
1188                    e.dtoh(&e.matmul(&self.output, &hlast, 1)?)?
1189                }
1190            };
1191            caches[s].pos += ts[s];
1192            out.push((logits, h_seed, hidden_all.remove(0)));
1193        }
1194        Ok(out)
1195    }
1196
1197    /// `full_attn` (batched prefill mixer) + the cache side-effect: append the T post-RoPE K/V
1198    /// rows into the resident quantized KV cache (q8_0 K / q5_1 V) and advance len/len_d. Row
1199    /// bytes are BIT-IDENTICAL to the decode append (same per-warp quant kernel per row; the
1200    /// batched `append_kv_quantized_rows` runs that exact warp math on a (block, token) grid).
1201    /// The attention itself is unchanged prefill math (fa_prefill over the f32 K/V).
1202    fn full_attn_prime(&self, e: &Engine, fa: &FullAttnLayer, h: &CudaSlice<f32>,
1203                       hx: Option<&CudaSlice<u8>>,
1204                       pos_d: &CudaSlice<i32>, t: usize, cache: &mut Cache, il: usize)
1205                       -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1206        // PROJ/CORE SPLIT (task #13, 2026-07-26): the q/k/v group projection is hoisted so
1207        // the cross-request batch driver can run it at m = sum_T over concatenated tokens;
1208        // this single-seq path composes proj+core identically (byte-for-byte the old body).
1209        // task #14: `hx` = the norm-fused fp16 twin of `h` (skips the convert launch).
1210        let g3 = match hx {
1211            Some(xh) => e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv], h, xh, t)?,
1212            None => e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?,
1213        };
1214        self.full_attn_prime_core(e, fa, g3, pos_d, t, cache, il)
1215    }
1216
1217    /// Everything after the q/k/v projections (split/QK-norm/RoPE/FA/gate/append + wo).
1218    /// `g3` = matmul_group([wq, wk, wv]) output rows for THIS sequence.
1219    /// Composes inner + wo (== the pre-split core; every existing caller unchanged).
1220    fn full_attn_prime_core(&self, e: &Engine, fa: &FullAttnLayer, g3: Vec<CudaSlice<f32>>,
1221                            pos_d: &CudaSlice<i32>, t: usize, cache: &mut Cache, il: usize)
1222                            -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1223        let (attn_g, ag16) = self.full_attn_prime_core_inner(e, fa, g3, pos_d, t, cache, il)?;
1224        if let Some(xh) = &ag16 {
1225            if let Some(y) = e.try_f16_gemm_pre(&fa.wo, xh, t)? {
1226                return Ok(y);
1227            }
1228        }
1229        Ok(e.matmul(&fa.wo, &attn_g, t)?)
1230    }
1231
1232    fn full_attn_prime_core_inner(&self, e: &Engine, fa: &FullAttnLayer, g3: Vec<CudaSlice<f32>>,
1233                            pos_d: &CudaSlice<i32>, t: usize, cache: &mut Cache, il: usize)
1234                            -> Result<(CudaSlice<f32>, Option<CudaSlice<u8>>), Box<dyn std::error::Error>> {
1235        let cfg = &self.cfg;
1236        let n_head = cfg.n_head as usize;
1237        let n_head_kv = cfg.n_head_kv as usize;
1238        let head_dim = cfg.head_dim_k as usize;
1239        let scale = 1.0 / (head_dim as f32).sqrt();
1240        let (pre, base_len) = self.full_attn_prime_pre_fa(e, fa, g3, pos_d, t, cache, il)?;
1241        let AttnPre { q, k, v, gate } = pre;
1242        let mut attn = e.uninit(t * n_head * head_dim)?;
1243        self.full_attn_prime_fa_dispatch(e, &q, &k, &v, &mut attn, base_len, t, cache, il,
1244                                         head_dim, n_head, n_head_kv, scale)?;
1245        self.full_attn_prime_post_fa(e, attn, &gate, t, n_head, head_dim)
1246    }
1247
1248    /// task #18 (attn side): projections tail through KV append — everything before the
1249    /// attention kernel. Returns the post-rope q/k, v, optional out-gate, and the KV rows
1250    /// present BEFORE this chunk's append (base_len; 0 == fresh).
1251    #[allow(clippy::type_complexity)]
1252    fn full_attn_prime_pre_fa(&self, e: &Engine, fa: &FullAttnLayer, mut g3: Vec<CudaSlice<f32>>,
1253                            pos_d: &CudaSlice<i32>, t: usize, cache: &mut Cache, il: usize)
1254                            -> Result<(AttnPre, usize), Box<dyn std::error::Error>> {
1255        let cfg = &self.cfg;
1256        let n_head = cfg.n_head as usize;
1257        let n_head_kv = cfg.n_head_kv as usize;
1258        let head_dim = cfg.head_dim_k as usize;
1259        let eps = cfg.rms_eps;
1260
1261        // qwen35 fuses [q|gate] per head in wq (2*head_dim stride); M3/Hy3 have NO output gate
1262        // (attention_output_gate=false) — wq out = n_head*head_dim exactly, and q_gate_split
1263        // would read 2x out of bounds. `gated` keys both the split and the sigmoid epilogue.
1264        let gated = cfg.attn_out_gate();
1265        let v = g3.pop().unwrap();
1266        let mut k = g3.pop().unwrap();
1267        let qf = g3.pop().unwrap();
1268        let (mut q, gate) = if gated {
1269            let mut q = e.uninit(t * n_head * head_dim)?;
1270            let mut gate = e.uninit(t * n_head * head_dim)?;
1271            e.q_gate_split(&qf, &mut q, &mut gate, head_dim, n_head, t)?;
1272            (q, Some(gate))
1273        } else {
1274            (qf, None)
1275        };
1276
1277        let mut qn = e.uninit(t * n_head * head_dim)?;
1278        e.rms_norm(&q, fa.q_norm.float_data(), &mut qn, head_dim, n_head * t, eps)?;
1279        q = qn;
1280        let mut kn = e.uninit(t * n_head_kv * head_dim)?;
1281        e.rms_norm(&k, fa.k_norm.float_data(), &mut kn, head_dim, n_head_kv * t, eps)?;
1282        k = kn;
1283        let rope_dims = cfg.rope_dim_count as usize;
1284        e.rope_neox(&mut q, pos_d, head_dim, rope_dims, n_head, t, cfg.rope_freq_base, 1.0)?;
1285        e.rope_neox(&mut k, pos_d, head_dim, rope_dims, n_head_kv, t, cfg.rope_freq_base, 1.0)?;
1286
1287        // CACHE SIDE-EFFECT: append the T post-rope K/V token rows (token-major [T, kv_dim] ==
1288        // the cache row layout) quantized into cache.kv[il], then advance len + device len_d.
1289        {
1290            let kvl = cache.kv[il].as_mut().unwrap();
1291            assert!(kvl.len + t <= cache.max_ctx, "prime_cache: KV overflow");
1292            e.append_kv_quantized_rows(&k, &v, &mut kvl.k, &mut kvl.v, kvl.len, t,
1293                                       kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes,
1294                                       crate::Engine::kv_fp8_on())?;
1295            kvl.len += t;
1296            let new_len = kvl.len as i32;
1297            e.set_i32_one(&mut kvl.len_d, new_len)?;
1298        }
1299
1300        let base_len = {
1301            let kvl = cache.kv[il].as_ref().unwrap();
1302            kvl.len - t   // KV rows present BEFORE this chunk's append above
1303        };
1304        Ok((AttnPre { q, k, v, gate }, base_len))
1305    }
1306
1307    /// batched prefill attention. FRESH prime (no past KV): unchanged forward_last math over
1308    /// the f32 K/V of this batch. CONTINUATION chunk (past KV present): the chunk's queries
1309    /// must attend to [0 .. base+t) — run fa_prefill_view over the resident QUANTIZED cache
1310    /// (the spec-verify pattern; kernel's causal mask offsets by T_kv-T). Numerically this
1311    /// reads q8_0/q5_1-dequantized K/V for the past AND the current chunk — the same class as
1312    /// decode reading the cache; the run-gen/first-16 battery is the accuracy authority.
1313    #[allow(clippy::too_many_arguments)]
1314    fn full_attn_prime_fa_dispatch(&self, e: &Engine, q: &CudaSlice<f32>, k: &CudaSlice<f32>,
1315                            v: &CudaSlice<f32>, attn: &mut CudaSlice<f32>, base_len: usize,
1316                            t: usize, cache: &mut Cache, il: usize,
1317                            head_dim: usize, n_head: usize, n_head_kv: usize, scale: f32)
1318                            -> Result<(), Box<dyn std::error::Error>> {
1319        if base_len == 0 {
1320            // fa_prefill's smem layout is compile-time HEAD_DIM: stamped twins exist for 256
1321            // (qwen35) and 128 (M3, `_hd128` — 2026-07-07). Other dims would overrun the
1322            // runtime-sized allocation -> ILLEGAL_ADDRESS; fall to naive SDPA there.
1323            if std::env::var("MEMRA_NOFA").is_ok() || !(head_dim == 256 || head_dim == 128) {
1324                e.sdpa_naive(q, k, v, attn, head_dim, n_head, n_head_kv, t, t, scale, true)?;
1325            } else {
1326                e.fa_prefill(q, k, v, attn, head_dim, n_head, n_head_kv, t, t, scale, true)?;
1327            }
1328        } else {
1329            let kvl = cache.kv[il].as_ref().unwrap();
1330            let t_kv = base_len + t;
1331            let k_view = e.view_u8(&kvl.k, t_kv * kvl.k_tok_bytes);
1332            let v_view = e.view_u8(&kvl.v, t_kv * kvl.v_tok_bytes);
1333            // ARC B (2026-07-05): dequant-once workspace, DEFAULT ON. fa_prefill_q's inline
1334            // dequant re-reads+re-dequants the whole quantized KV stream from every one of the
1335            // T/64 x n_head CTAs (64x+ redundant at chunk=4096; 30.5% of the 32k prime wall).
1336            // fa_prefill_view_ws dequants K/V ONCE into a resident bf16 workspace then runs the
1337            // bit-identical bf16 twin (fa_prefill_qw) — same staged values, same FP order, token-
1338            // identical output (gate: MEMRA_PRIME_CHUNK=4096 ws-on vs ws-off vs monolithic).
1339            // MEMRA_PRIME_DEQW=0 reverts to the inline-dequant kernel.
1340            let deqw = std::env::var("MEMRA_PRIME_DEQW").map(|v| v != "0").unwrap_or(true);
1341            if deqw {
1342                e.fa_prefill_view_ws(q, &k_view, &v_view, attn, head_dim, n_head, n_head_kv,
1343                                     t, t_kv, scale, true, kvl.k_tok_bytes, kvl.v_tok_bytes,
1344                                     crate::Engine::kv_fp8_on())?;
1345            } else {
1346                e.fa_prefill_view(q, &k_view, &v_view, attn, head_dim, n_head, n_head_kv,
1347                                  t, t_kv, scale, true, kvl.k_tok_bytes, kvl.v_tok_bytes,
1348                                  crate::Engine::kv_fp8_on())?;
1349            }
1350        }
1351        Ok(())
1352    }
1353
1354    /// task #17: sig_mul_f16out fuses [sigmoid + mul + f16 convert] into one launch
1355    /// (bit-identical composition) and hands wo its fp16 operand directly.
1356    fn full_attn_prime_post_fa(&self, e: &Engine, attn: CudaSlice<f32>,
1357                            gate: &Option<CudaSlice<f32>>, t: usize,
1358                            n_head: usize, head_dim: usize)
1359                            -> Result<(CudaSlice<f32>, Option<CudaSlice<u8>>), Box<dyn std::error::Error>> {
1360        let (attn_g, ag16) = match gate {
1361            Some(gate) => {
1362                let n = t * n_head * head_dim;
1363                let mut ag = e.uninit(n)?;
1364                if Self::f16out_on(e, t) {
1365                    let mut a16 = e.alloc_u8_uninit(n * 2)?;
1366                    e.sig_mul_f16out(&attn, gate, &mut ag, &mut a16, n)?;
1367                    (ag, Some(a16))
1368                } else {
1369                    let mut gsig = e.uninit(n)?;
1370                    e.sigmoid(gate, &mut gsig, n)?;
1371                    e.mul(&attn, &gsig, &mut ag, n)?;
1372                    (ag, None)
1373                }
1374            }
1375            None => (attn, None),
1376        };
1377        Ok((attn_g, ag16))
1378    }
1379
1380    /// STATEFUL batched linear-attention prime: `linear_attn`'s prefill-dispatch pass (normal
1381    /// `e.matmul` — GEMM at m>=16 — plus the prefill repack/L2/glog kernels) but with the state
1382    /// carried THROUGH the cache like the spec verify does: carried-ring conv
1383    /// (ssm_conv1d_tm_state writes the final ring back) + ONE gdn_scan from cache.recur[il]'s
1384    /// current state (zero at a fresh prime) whose final state ping-pongs back into the cache.
1385    /// Wiring mirrors `linear_attn_verify_t` (spec.rs); dispatch mirrors `linear_attn` (prefill).
1386    fn linear_attn_prime(&self, e: &Engine, la: &LinearAttnLayer, h: &CudaSlice<f32>,
1387                         hx: Option<&CudaSlice<u8>>, t: usize,
1388                         cache: &mut Cache, il: usize)
1389                         -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1390        // PROJ/CORE SPLIT (task #13): see full_attn_prime — same hoist for the GDN 4-tuple.
1391        let ws = [&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha];
1392        let g4 = match hx {
1393            Some(xh) => e.matmul_group_xh(&ws, h, xh, t)?,
1394            None => e.matmul_group(&ws, h, t)?,
1395        };
1396        self.linear_attn_prime_core(e, la, g4, t, cache, il)
1397    }
1398
1399    /// Everything after the GDN 4-tuple projections (conv/chunk stack/gated norm + ssm_out).
1400    fn linear_attn_prime_core(&self, e: &Engine, la: &LinearAttnLayer, mut g4: Vec<CudaSlice<f32>>,
1401                              t: usize, cache: &mut Cache, il: usize)
1402                              -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1403        self.linear_attn_prime_core_pad(e, la, g4.drain(..).collect(), t, cache, il, None)
1404    }
1405
1406    /// task #14: `pad_len` = device true length for PADDED prime graphs. Pads become
1407    /// identity GDN steps (gdn_pad_mask zeroes beta/g_log past the true length) and the
1408    /// conv ring writes back from the true tail. None = classic path, byte-identical.
1409    #[allow(clippy::too_many_arguments)]
1410    fn linear_attn_prime_core_pad_inner(&self, e: &Engine, la: &LinearAttnLayer, mut g4: Vec<CudaSlice<f32>>,
1411                              t: usize, cache: &mut Cache, il: usize,
1412                              pad_len: Option<&CudaSlice<i32>>)
1413                              -> Result<(CudaSlice<f32>, Option<CudaSlice<u8>>), Box<dyn std::error::Error>> {
1414        // shim over the view twin (task #16): full-range views of the owned buffers.
1415        let ssm = self.cfg.ssm.as_ref().unwrap();
1416        let d_state = ssm.state_size as usize;
1417        let num_k = ssm.group_count as usize;
1418        let num_v = ssm.time_step_rank as usize;
1419        let key_dim = d_state * num_k;
1420        let value_dim = d_state * num_v;
1421        let conv_dim = key_dim * 2 + value_dim;
1422        let alpha = g4.pop().unwrap();                   // [T, num_v]
1423        let beta_raw = g4.pop().unwrap();                // [T, num_v]
1424        let z = g4.pop().unwrap();                       // [T, value_dim]
1425        let qkv_mixed = g4.pop().unwrap();               // [T, conv_dim] token-major
1426        self.linear_attn_prime_core_pad_view(
1427            e, la,
1428            &qkv_mixed.slice(0..t * conv_dim), &z.slice(0..t * value_dim),
1429            &beta_raw.slice(0..t * num_v), &alpha.slice(0..t * num_v),
1430            t, cache, il, pad_len)
1431    }
1432
1433    /// task #18: the GDN prep stage (conv + repack + l2 x2 + sigmoid + glog + pad-mask) —
1434    /// shared verbatim by the per-seq scan path and the varlen batched path.
1435    #[allow(clippy::too_many_arguments)]
1436    fn linear_attn_gdn_prep(&self, e: &Engine, la: &LinearAttnLayer,
1437                            qkv_mixed: &cudarc::driver::CudaView<f32>,
1438                            beta_raw: &cudarc::driver::CudaView<f32>,
1439                            alpha: &cudarc::driver::CudaView<f32>,
1440                            t: usize, cache: &mut Cache, il: usize,
1441                            pad_len: Option<&CudaSlice<i32>>)
1442                            -> Result<GdnPrep, Box<dyn std::error::Error>> {
1443        let cfg = &self.cfg;
1444        let ssm = cfg.ssm.as_ref().unwrap();
1445        let d_state = ssm.state_size as usize;       // 128
1446        let num_k = ssm.group_count as usize;        // 16
1447        let num_v = ssm.time_step_rank as usize;     // 32
1448        let d_conv = ssm.conv_kernel as usize;       // 4
1449        let key_dim = d_state * num_k;               // 2048
1450        let value_dim = d_state * num_v;             // 4096
1451        let conv_dim = key_dim * 2 + value_dim;      // 8192
1452        let eps = cfg.rms_eps;
1453        debug_assert!(t >= d_conv - 1, "stateful conv needs T >= pad (PRIME_MIN_T gates)");
1454
1455        // conv with CARRIED ring state + ring roll (state read + final-window write-back).
1456        // task #18 conv-fuse (default ON): conv + SiLU + repack in one pass — the 67MB
1457        // conv_out intermediate and its transposed re-read disappear (values bit-identical).
1458        // MEMRA_CONV_FUSE=0 reverts to the two-kernel chain.
1459        let rl = cache.recur[il].as_mut().unwrap();
1460        let hk = Self::gdn_hk(e, t, num_v, num_k);
1461        let conv_fuse = std::env::var("MEMRA_CONV_FUSE").as_deref() != Ok("0");
1462        let hk = if conv_fuse { hk } else { num_v };   // de-broadcast rides the fused conv
1463        let mut q_g = e.uninit(d_state * hk * t)?;
1464        let mut k_g = e.uninit(d_state * hk * t)?;
1465        let mut v_g = e.uninit(d_state * num_v * t)?;
1466        if conv_fuse {
1467            e.ssm_conv1d_gdn_state_pad(qkv_mixed, &mut rl.conv_state, la.ssm_conv1d.float_data(),
1468                                  &mut q_g, &mut k_g, &mut v_g,
1469                                  conv_dim, t, d_conv, d_state, num_v, num_k, key_dim, hk, pad_len)?;
1470        } else {
1471            let mut conv_out = e.uninit(conv_dim * t)?;      // [conv_dim, T] channel-major, SiLU
1472            e.ssm_conv1d_tm_state_pad_v(qkv_mixed, &mut rl.conv_state, la.ssm_conv1d.float_data(),
1473                                  &mut conv_out, conv_dim, t, d_conv, pad_len)?;
1474            e.qkv_to_gdn_repack(&conv_out, &mut q_g, &mut k_g, &mut v_g, d_state, num_v, num_k, key_dim, t)?;
1475        }
1476        let mut q_l2 = e.uninit(d_state * hk * t)?;
1477        // mirror-fold (round 35): q's bf16 twin (wgmma K45/K2 A-operand) in-epilogue too.
1478        // Emitted only where a consumer exists (the wgmma config) — on other arches the
1479        // alloc + epilogue stores would be pure waste.
1480        let qb16 = if Engine::l2_v2_on(d_state) && e.gdn_wgmma_on(32) {
1481            let mut qb = e.alloc_u8_uninit(d_state * hk * t * 2)?;
1482            e.l2_norm_pp(&q_g, &mut q_l2, Some(&mut qb), d_state, hk * t, eps)?;
1483            Some(qb)
1484        } else {
1485            e.l2_norm_pp(&q_g, &mut q_l2, None, d_state, hk * t, eps)?;
1486            None
1487        };
1488        let mut k_l2 = e.uninit(d_state * hk * t)?;
1489        // mirror-fold: emit k's bf16 twin (the chunked-scan kb16 mirror) in-epilogue
1490        let kb16 = if Engine::l2_v2_on(d_state) {
1491            let mut kb = e.alloc_u8_uninit(d_state * hk * t * 2)?;
1492            e.l2_norm_pp(&k_g, &mut k_l2, Some(&mut kb), d_state, hk * t, eps)?;
1493            Some(kb)
1494        } else {
1495            e.l2_norm_pp(&k_g, &mut k_l2, None, d_state, hk * t, eps)?;
1496            None
1497        };
1498        let mut beta = e.uninit(t * num_v)?;
1499        e.sigmoid_v(beta_raw, &mut beta, t * num_v)?;
1500        let mut g_log = e.uninit(t * num_v)?;
1501        e.gdn_glog_v(alpha, la.ssm_dt.float_data(), la.ssm_a.float_data(), &mut g_log, num_v, t)?;
1502        if let Some(len_d) = pad_len {
1503            e.gdn_pad_mask(&mut beta, &mut g_log, len_d, num_v, t)?;
1504        }
1505        Ok(GdnPrep { hk, q_l2, k_l2, v_g, beta, g_log, kb16, qb16 })
1506    }
1507
1508    /// task #18: batched GDN mixer core — per-seq prep + K1-K3, then ONE varlen K4 and
1509    /// ONE varlen K5 launch for all B sequences (full-machine occupancy vs B underfilled
1510    /// per-seq trains). Per-block math identical to the per-seq path (bit-gateable);
1511    /// MEMRA_GDN_VL=0 or a non-mma/non-C32 config falls back to the per-seq loop.
1512    #[allow(clippy::too_many_arguments)]
1513    fn linear_attn_prime_core_batch(&self, e: &Engine, la: &LinearAttnLayer,
1514                                    g4: &[CudaSlice<f32>], offs: &[usize], ts: &[usize],
1515                                    caches: &mut [&mut Cache], il: usize)
1516                                    -> Result<Vec<(CudaSlice<f32>, Option<CudaSlice<u8>>)>, Box<dyn std::error::Error>> {
1517        let ssm = self.cfg.ssm.as_ref().unwrap();
1518        let d_state = ssm.state_size as usize;
1519        let num_k = ssm.group_count as usize;
1520        let num_v = ssm.time_step_rank as usize;
1521        let key_dim = d_state * num_k;
1522        let value_dim = d_state * num_v;
1523        let conv_dim = key_dim * 2 + value_dim;
1524        let eps = self.cfg.rms_eps;
1525        let scale = 1.0 / (d_state as f32).sqrt();
1526        let b = ts.len();
1527        let c = Engine::gdn_chunk_size();
1528        // CONTINUATION batches (increment (b)) take the per-seq stateful pad_view twin
1529        // below — the varlen K4/K5 chain is fresh-only (zero initial state assumed).
1530        let carried = caches.iter().any(|c| c.pos > 0);
1531        let use_vl = !carried
1532            && (2..=8).contains(&b)
1533            && Engine::gdn_chunked_enabled() && ts.iter().all(|&t| t >= 16)
1534            && e.gdn_mma_enabled(c)
1535            && std::env::var("MEMRA_GDN_VL").as_deref() != Ok("0");
1536        if !use_vl {
1537            return (0..b).map(|s| {
1538                let (o, t) = (offs[s], ts[s]);
1539                self.linear_attn_prime_core_pad_view(
1540                    e, la,
1541                    &g4[0].slice(o * conv_dim..(o + t) * conv_dim),
1542                    &g4[1].slice(o * value_dim..(o + t) * value_dim),
1543                    &g4[2].slice(o * num_v..(o + t) * num_v),
1544                    &g4[3].slice(o * num_v..(o + t) * num_v),
1545                    t, caches[s], il, None)
1546            }).collect();
1547        }
1548        // increment 3: the ENTIRE core is varlen — allocs only per seq, then 13 launches
1549        // total for the whole batch: [conv, ring, repack, l2(q+k), gate-prep] +
1550        // [k-mirror, K1, K2, K3, w-mirror, K4, K5] + [gated-norm tail].
1551        struct SeqBufs {
1552            conv_out: CudaSlice<f32>, q_g: CudaSlice<f32>, k_g: CudaSlice<f32>, v_g: CudaSlice<f32>,
1553            q_l2: CudaSlice<f32>, k_l2: CudaSlice<f32>, beta: CudaSlice<f32>, g_log: CudaSlice<f32>,
1554            gn: CudaSlice<f32>, gn16: CudaSlice<u8>,
1555        }
1556        let d_conv = ssm.conv_kernel as usize;
1557        let f16o = Self::f16out_on(e, 16);
1558        let hk = Self::gdn_hk(e, 16, num_v, num_k);   // vl path is always chunked+mma
1559        let mut sb = Vec::with_capacity(b);
1560        let mut pres = Vec::with_capacity(b);
1561        for &t in ts.iter().take(b) {
1562            sb.push(SeqBufs {
1563                conv_out: e.uninit(conv_dim * t)?,
1564                q_g: e.uninit(d_state * hk * t)?,
1565                k_g: e.uninit(d_state * hk * t)?,
1566                v_g: e.uninit(d_state * num_v * t)?,
1567                q_l2: e.uninit(d_state * hk * t)?,
1568                k_l2: e.uninit(d_state * hk * t)?,
1569                beta: e.uninit(t * num_v)?,
1570                g_log: e.uninit(t * num_v)?,
1571                gn: e.uninit(d_state * num_v * t)?,
1572                gn16: e.alloc_u8_uninit(d_state * num_v * t * 2)?,
1573            });
1574            pres.push(e.gdn_chunk_alloc(num_v, t, c, hk)?);
1575        }
1576        let prep_args: Vec<crate::GdnPrepVl> = (0..b).map(|s| {
1577            let (o, t) = (offs[s], ts[s]);
1578            let rl = caches[s].recur[il].as_ref().unwrap();
1579            crate::GdnPrepVl {
1580                qkv: e.addr_f32v(&g4[0].slice(o * conv_dim..(o + t) * conv_dim)),
1581                conv_state: e.addr_f32(&rl.conv_state),
1582                conv_out: e.addr_f32(&sb[s].conv_out),
1583                q_g: e.addr_f32(&sb[s].q_g), k_g: e.addr_f32(&sb[s].k_g), v_g: e.addr_f32(&sb[s].v_g),
1584                q_l2: e.addr_f32(&sb[s].q_l2), k_l2: e.addr_f32(&sb[s].k_l2),
1585                beta_raw: e.addr_f32v(&g4[2].slice(o * num_v..(o + t) * num_v)),
1586                alpha: e.addr_f32v(&g4[3].slice(o * num_v..(o + t) * num_v)),
1587                beta: e.addr_f32(&sb[s].beta), g_log: e.addr_f32(&sb[s].g_log),
1588                o: e.addr_f32(&pres[s].o),
1589                z: e.addr_f32v(&g4[1].slice(o * value_dim..(o + t) * value_dim)),
1590                gn: e.addr_f32(&sb[s].gn), gn16: e.addr_u8(&sb[s].gn16),
1591                kb16: if Engine::l2_v2_on(d_state) { e.addr_u8(&pres[s].kb16) } else { 0 },
1592                qb16: if Engine::l2_v2_on(d_state) && e.gdn_wgmma_on(c) { e.addr_u8(&pres[s].qb16) } else { 0 },
1593                t: t as i32, pad: 0,
1594            }
1595        }).collect();
1596        let args: Vec<crate::GdnSeqVl> = (0..b).map(|s| {
1597            let rl = caches[s].recur[il].as_ref().unwrap();
1598            crate::GdnSeqVl {
1599                kb16: e.addr_u8(&pres[s].kb16), gcum: e.addr_f32(&pres[s].gcum),
1600                beta: e.addr_f32(&sb[s].beta), u: e.addr_f32(&pres[s].u),
1601                wb16: e.addr_u8(&pres[s].wb16), y: e.addr_u8(&pres[s].y16),
1602                ssnap: e.addr_u8(&pres[s].ssnap16),
1603                state_in: e.addr_f32(&rl.ssm_state), state_out: e.addr_f32(&rl.ssm_state_alt),
1604                q: e.addr_f32(&sb[s].q_l2), p: e.addr_f32(&pres[s].p),
1605                o: e.addr_f32(&pres[s].o),
1606                k: e.addr_f32(&sb[s].k_l2), v: e.addr_f32(&sb[s].v_g),
1607                g: e.addr_f32(&sb[s].g_log), a: e.addr_f32(&pres[s].a),
1608                w: e.addr_f32(&pres[s].w),
1609                t: ts[s] as i32, nc: pres[s].nc as i32,
1610            }
1611        }).collect();
1612        e.gdn_prep_vl8(&prep_args, la.ssm_conv1d.float_data(), la.ssm_dt.float_data(),
1613                       la.ssm_a.float_data(), conv_dim, d_conv, d_state, num_v, num_k, key_dim, hk, eps)?;
1614        // mirror-fold (round 27): l2 v2 emits kb16 in-epilogue; K3's store emits wb16 —
1615        // both standalone mirror launches vanish on the default config.
1616        if !Engine::l2_v2_on(d_state) {
1617            e.gdn_mirror_vl8(&args, num_v, 0, hk)?;
1618        }
1619        // task #22: wgmma-fused vl twins — qb16 mirrors + GdnWVl8 side-struct.
1620        let wq8: Option<crate::GdnWVl8> = if e.gdn_wgmma_on(c) {
1621            // qb16 emitted by the vl l2 mirror-fold when l2-v2 serves; bulk cvt otherwise
1622            if !Engine::l2_v2_on(d_state) {
1623                for s in 0..b {
1624                    e.f32_to_bf16_into(&sb[s].q_l2, &mut pres[s].qb16, d_state * hk * ts[s])?;
1625                }
1626            }
1627            let mut wa = [crate::GdnWVl::default(); 8];
1628            for s in 0..b {
1629                wa[s] = crate::GdnWVl { qb16: e.addr_u8(&pres[s].qb16), pb16: e.addr_u8(&pres[s].pb16) };
1630            }
1631            Some(crate::GdnWVl8(wa))
1632        } else { None };
1633        e.gdn_chunk_k123_vl8(&args, num_v, hk, wq8.as_ref())?;
1634        e.gdn_chunk_vl8(&args, num_v, scale, hk, wq8.as_ref())?;
1635        if f16o {
1636            e.gdn_tail_vl8(&prep_args, la.ssm_norm.float_data(), d_state, num_v, eps)?;
1637        }
1638        // per-seq state swap (+ non-f16out tail fallback)
1639        let mut out = Vec::with_capacity(b);
1640        for (s, bufs) in sb.into_iter().enumerate() {
1641            let rl = caches[s].recur[il].as_mut().unwrap();
1642            std::mem::swap(&mut rl.ssm_state, &mut rl.ssm_state_alt);
1643            let (o, t) = (offs[s], ts[s]);
1644            let SeqBufs { mut gn, gn16, .. } = bufs;
1645            if f16o {
1646                out.push((gn, Some(gn16)));
1647            } else {
1648                let z_v = g4[1].slice(o * value_dim..(o + t) * value_dim);
1649                e.gated_rmsnorm_zv(&pres[s].o, la.ssm_norm.float_data(), &z_v, &mut gn,
1650                                   d_state, num_v * t, eps)?;
1651                out.push((gn, None));
1652            }
1653        }
1654        Ok(out)
1655    }
1656
1657    /// task #16: view-consuming GDN prime core — the batched prime hands row-offset
1658    /// views of the CONCAT projection outputs directly (no per-seq split copies).
1659    /// Same kernels, same values, byte-identical to the Vec shim above.
1660    #[allow(clippy::too_many_arguments)]
1661    fn linear_attn_prime_core_pad_view(&self, e: &Engine, la: &LinearAttnLayer,
1662                              qkv_mixed: &cudarc::driver::CudaView<f32>,
1663                              z: &cudarc::driver::CudaView<f32>,
1664                              beta_raw: &cudarc::driver::CudaView<f32>,
1665                              alpha: &cudarc::driver::CudaView<f32>,
1666                              t: usize, cache: &mut Cache, il: usize,
1667                              pad_len: Option<&CudaSlice<i32>>)
1668                              -> Result<(CudaSlice<f32>, Option<CudaSlice<u8>>), Box<dyn std::error::Error>> {
1669        let cfg = &self.cfg;
1670        let ssm = cfg.ssm.as_ref().unwrap();
1671        let d_state = ssm.state_size as usize;       // 128
1672        let num_v = ssm.time_step_rank as usize;     // 32
1673        let eps = cfg.rms_eps;
1674        let scale = 1.0 / (d_state as f32).sqrt();
1675
1676        let prep = self.linear_attn_gdn_prep(e, la, qkv_mixed, beta_raw, alpha, t, cache, il, pad_len)?;
1677
1678        // ONE gdn_scan over T from the cache's CURRENT state (zero at fresh prime); the final
1679        // state lands in the spare buffer and ping-pongs back (stable resident pointers, the
1680        // decode-determinism discipline from linear_attn_decode_inner). A4: `gdn_scan_prefill`
1681        // dispatches the chunked WY form under MEMRA_GDN_CHUNKED (prefill-only seam; decode +
1682        // verify keep the sequential kernel).
1683        let mut o = e.uninit(d_state * num_v * t)?;
1684        let rl = cache.recur[il].as_mut().unwrap();
1685        {
1686            let crate::cache::RecurLayer { ssm_state, ssm_state_alt, .. } = rl;
1687            e.gdn_scan_prefill(&prep.q_l2, &prep.k_l2, &prep.v_g, &prep.g_log, &prep.beta,
1688                               prep.kb16.as_ref(), prep.qb16.as_ref(), ssm_state, ssm_state_alt, &mut o, num_v, t, scale,
1689                               prep.hk)?;
1690        }
1691        std::mem::swap(&mut rl.ssm_state, &mut rl.ssm_state_alt);
1692
1693        // gated RMSNorm + out projection (prefill dispatch). task #17: the f16out twin also
1694        // emits the ssm_out GEMM's fp16 operand in-epilogue (kills the standalone convert).
1695        let mut gn = e.uninit(d_state * num_v * t)?;
1696        let gn16 = if Self::f16out_on(e, t) {
1697            let mut g16 = e.alloc_u8_uninit(d_state * num_v * t * 2)?;
1698            e.gated_rmsnorm_f16out_zv(&o, la.ssm_norm.float_data(), z, &mut gn, &mut g16,
1699                                      d_state, num_v * t, eps)?;
1700            Some(g16)
1701        } else {
1702            e.gated_rmsnorm_zv(&o, la.ssm_norm.float_data(), z, &mut gn, d_state, num_v * t, eps)?;
1703            None
1704        };
1705        Ok((gn, gn16))
1706    }
1707
1708    /// Task #15 core-split wrapper: composes inner + ssm_out (== the old core).
1709    #[allow(clippy::too_many_arguments)]
1710    fn linear_attn_prime_core_pad(&self, e: &Engine, la: &LinearAttnLayer, g4: Vec<CudaSlice<f32>>,
1711                              t: usize, cache: &mut Cache, il: usize,
1712                              pad_len: Option<&CudaSlice<i32>>)
1713                              -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1714        let (gn, gn16) = self.linear_attn_prime_core_pad_inner(e, la, g4, t, cache, il, pad_len)?;
1715        if let Some(xh) = &gn16 {
1716            if let Some(y) = e.try_f16_gemm_pre(&la.ssm_out, xh, t)? {
1717                return Ok(y);
1718            }
1719        }
1720        Ok(e.matmul(&la.ssm_out, &gn, t)?)
1721    }
1722
1723    /// Full-attention mixer with QK-norm, partial RoPE, sigmoid output gate (qwen35 :257-336).
1724    pub fn full_attn(&self, e: &Engine, fa: &FullAttnLayer, h: &CudaSlice<f32>, pos_d: &CudaSlice<i32>, t: usize)
1725                 -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1726        let cfg = &self.cfg;
1727        let _n_embd = cfg.n_embd as usize;
1728        let n_head = cfg.n_head as usize;
1729        let n_head_kv = cfg.n_head_kv as usize;
1730        let head_dim = cfg.head_dim_k as usize;
1731        let eps = cfg.rms_eps;
1732        let scale = 1.0 / (head_dim as f32).sqrt();
1733
1734        // qwen35: wq output = head_dim*2*n_head (fused [q|gate] per head). M3/Hy3: NO output
1735        // gate — wq out = n_head*head_dim, no split (see prime-path note).
1736        let gated = cfg.attn_out_gate();
1737        // grouped: one f16 activation convert feeds q/k/v (matmul_group)
1738        let mut g3 = e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?;
1739        let v = g3.pop().unwrap();
1740        let mut k = g3.pop().unwrap();
1741        let qf = g3.pop().unwrap();
1742        let (mut q, gate) = if gated {
1743            let mut q = e.uninit(t * n_head * head_dim)?;
1744            let mut gate = e.uninit(t * n_head * head_dim)?;
1745            e.q_gate_split(&qf, &mut q, &mut gate, head_dim, n_head, t)?;
1746            (q, Some(gate))
1747        } else {
1748            (qf, None)
1749        };
1750
1751        // QK-norm (per head_dim row), then partial RoPE.
1752        let mut qn = e.uninit(t * n_head * head_dim)?;
1753        e.rms_norm(&q, fa.q_norm.float_data(), &mut qn, head_dim, n_head * t, eps)?;
1754        q = qn;
1755        let mut kn = e.uninit(t * n_head_kv * head_dim)?;
1756        e.rms_norm(&k, fa.k_norm.float_data(), &mut kn, head_dim, n_head_kv * t, eps)?;
1757        k = kn;
1758        let rope_dims = cfg.rope_dim_count as usize;
1759        e.rope_neox(&mut q, pos_d, head_dim, rope_dims, n_head, t, cfg.rope_freq_base, 1.0)?;
1760        e.rope_neox(&mut k, pos_d, head_dim, rope_dims, n_head_kv, t, cfg.rope_freq_base, 1.0)?;
1761
1762        // SDPA
1763        let mut attn = e.uninit(t * n_head * head_dim)?;
1764        // hand-written FlashAttention prefill (head_dim 256/128 stamped twins). MEMRA_NOFA
1765        // falls back to naive sdpa.
1766        if std::env::var("MEMRA_NOFA").is_ok() || !(head_dim == 256 || head_dim == 128) {
1767            // head_dim gate: see prime-path note (fa_prefill is stamped at 256 and 128 only).
1768            e.sdpa_naive(&q, &k, &v, &mut attn, head_dim, n_head, n_head_kv, t, t, scale, true)?;
1769        } else {
1770            e.fa_prefill(&q, &k, &v, &mut attn, head_dim, n_head, n_head_kv, t, t, scale, true)?;
1771        }
1772
1773        // output gate: attn * sigmoid(gate) — qwen35 only (M3 has no gate).
1774        let attn_g = match &gate {
1775            Some(gate) => {
1776                let mut gsig = e.uninit(t * n_head * head_dim)?;
1777                e.sigmoid(gate, &mut gsig, t * n_head * head_dim)?;
1778                let mut ag = e.uninit(t * n_head * head_dim)?;
1779                e.mul(&attn, &gsig, &mut ag, t * n_head * head_dim)?;
1780                ag
1781            }
1782            None => attn,
1783        };
1784
1785        // o projection
1786        let o = e.matmul(&fa.wo, &attn_g, t)?;
1787        Ok(o)
1788    }
1789
1790    /// Linear-attention (Gated DeltaNet) mixer (qwen35 :338-470).
1791    pub fn linear_attn(&self, e: &Engine, la: &LinearAttnLayer, h: &CudaSlice<f32>, t: usize)
1792                   -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1793        let cfg = &self.cfg;
1794        let _n_embd = cfg.n_embd as usize;
1795        let ssm = cfg.ssm.as_ref().unwrap();
1796        let d_state = ssm.state_size as usize;       // 128
1797        let num_k = ssm.group_count as usize;        // 16
1798        let num_v = ssm.time_step_rank as usize;     // 32
1799        let d_conv = ssm.conv_kernel as usize;       // 4
1800        let head_k = d_state; let head_v = d_state;
1801        let key_dim = head_k * num_k;                // 2048
1802        let value_dim = head_v * num_v;              // 4096
1803        let conv_dim = key_dim * 2 + value_dim;      // 8192
1804        let eps = cfg.rms_eps;
1805        let scale = 1.0 / (d_state as f32).sqrt();
1806
1807        // projections
1808        // grouped: one f16 activation convert feeds all four projections (matmul_group)
1809        let mut g4 = e.matmul_group(&[&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha], h, t)?;
1810        let alpha = g4.pop().unwrap();                   // [T, num_v]
1811        let beta_raw = g4.pop().unwrap();                // [T, num_v]
1812        let z = g4.pop().unwrap();                       // [T, value_dim]
1813        let qkv_mixed = g4.pop().unwrap();               // [T, conv_dim] token-major
1814
1815        // conv + GDN repack, FUSED (2026-07-03): ssm_conv1d_gdn reads qkv_mixed [T, conv_dim]
1816        // token-major DIRECTLY (causal window rows t-pad..t, rows<0 = zero prefill state), applies
1817        // the 8-tap conv + SiLU, and scatters straight into the GDN [d_state, num_v, T] q/k/v
1818        // layout with the modulo head-repeat. Replaces transpose + zeros + conv_left_pad +
1819        // ssm_conv1d + qkv_to_gdn_repack (5 launches, conv_in/conv_out scratch + a 16MB@T=512
1820        // round-trip). BIT-IDENTICAL accumulation and scatter mapping.
1821        let _ = (head_k, head_v);
1822        let mut q_g = e.uninit(d_state * num_v * t)?;
1823        let mut k_g = e.uninit(d_state * num_v * t)?;
1824        let mut v_g = e.uninit(d_state * num_v * t)?;
1825        e.ssm_conv1d_gdn(&qkv_mixed, la.ssm_conv1d.float_data(), &mut q_g, &mut k_g, &mut v_g,
1826                         conv_dim, t, d_conv, d_state, num_v, num_k, key_dim)?;
1827        // L2-norm q,k per (head_dim) row — rows are contiguous d_state in q_g.
1828        let mut q_l2 = e.uninit(d_state * num_v * t)?;
1829        e.l2_norm(&q_g, &mut q_l2, d_state, num_v * t, eps)?;
1830        let mut k_l2 = e.uninit(d_state * num_v * t)?;
1831        e.l2_norm(&k_g, &mut k_l2, d_state, num_v * t, eps)?;
1832        let v_gd = v_g;
1833
1834        // beta = sigmoid(beta_raw) ; g_log = a * softplus(alpha + dt). Both need [num_v, T] layout
1835        // (g[t*num_v + h]). beta_raw/alpha are [T, num_v] token-major == that layout already.
1836        let mut beta = e.uninit(t * num_v)?;
1837        e.sigmoid(&beta_raw, &mut beta, t * num_v)?;
1838        // gdn_glog expects alpha [H,T] with alpha[t*H+h] and dt_bias/a [H] — matches token-major [T,num_v].
1839        let mut g_log = e.uninit(t * num_v)?;
1840        e.gdn_glog(&alpha, la.ssm_dt.float_data(), la.ssm_a.float_data(), &mut g_log, num_v, t)?;
1841
1842        // GDN scan (A4: gdn_scan_prefill dispatches chunked WY under MEMRA_GDN_CHUNKED)
1843        let state_in = e.zeros(d_state * d_state * num_v)?;  // zero state (prefill)
1844        let mut state_out = e.zeros(d_state * d_state * num_v)?;
1845        let mut o = e.uninit(d_state * num_v * t)?;
1846        e.gdn_scan_prefill(&q_l2, &k_l2, &v_gd, &g_log, &beta, None, None, &state_in, &mut state_out, &mut o, num_v, t, scale, num_v)?;
1847
1848        // gated RMSNorm: dst = RMSNorm(o, ssm_norm[head_v]) * silu(z). o is [d_state, num_v, T];
1849        // rows of head_v=d_state, nrows = num_v*T. z must match row layout: z is [T, value_dim] token-major
1850        // = [T, num_v*head_v]; per (t, vh) the head_v slice is contiguous -> rows align as (t*num_v+vh).
1851        // o rows are (t*num_v+vh) too. Good.
1852        let mut gn = e.uninit(d_state * num_v * t)?;
1853        e.gated_rmsnorm(&o, la.ssm_norm.float_data(), &z, &mut gn, d_state, num_v * t, eps)?;
1854
1855        // ssm_out projection: gn is [d_state, num_v, T] = [value_dim, T] viewed token-major as [T, value_dim]?
1856        // gn layout: (t*num_v+vh)*d_state + i  == token t, then (vh,i) = channel vh*d_state+i. That's
1857        // token-major [T, value_dim]. linear wants [T, in=value_dim]. Good.
1858        let out = e.matmul(&la.ssm_out, &gn, t)?;
1859        Ok(out)
1860    }
1861}
1862
1863impl HybridModel {
1864    /// MoE FFN (EDGE-1). z: [T, n_embd] (already post-attention-normed). Returns moe_out [T, n_embd].
1865    /// Node-for-node vs llama.cpp build_moe_ffn + qwen35moe::build_layer_ffn.
1866    ///
1867    /// `il` is the trunk layer index — the residency-cache key prefix (a gate-expert of layer 3 is a
1868    /// different 860160-byte block than the same expert of layer 7).
1869    ///
1870    /// Routing: host softmax+sort (default) OR the fused router kernel (MEMRA_FUSED_ROUTER).
1871    /// Dispatch: stage-every-token into 3 scratch slots (default) OR the SLRU residency cache
1872    /// (MEMRA_MOE_CACHE). The cache-HIT weight path is bit-identical to stage-every-token (§B.3).
1873    /// Convenience wrapper used by the hybrid trunk/MTP loops: pulls dims + max-block from `self`.
1874    pub fn moe_ffn_il(&self, e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>, t: usize, il: u16)
1875               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1876        Self::moe_ffn(e, m, z, t, &self.cfg, il, self.max_moe_block())
1877    }
1878
1879    /// Decode-path twin with a PRE-QUANTIZED z (from add_rms_norm_zq8): threads (zq, zd) into the
1880    /// t=1 dev arm so the per-layer standalone quantize_q8_1 launch folds away. Identical bytes
1881    /// (the fused kernel reproduces quantize_q8_1 exactly); every other path ignores the pair.
1882    pub fn moe_ffn_il_zq8(&self, e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>,
1883                          zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>, t: usize, il: u16)
1884               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1885        Self::moe_ffn_inner(e, m, z, zq8, t, &self.cfg, il, self.max_moe_block())
1886    }
1887
1888    /// MoE FFN (EDGE-1), source-/model-agnostic. z: [T, n_embd] (already post-attention-normed).
1889    /// Returns moe_out [T, n_embd]. Node-for-node vs llama.cpp build_moe_ffn. Shared by the hybrid
1890    /// (qwen35moe, shared expert present) and the dense-attention MoE (OLMoE, no shared expert) paths;
1891    /// `cfg.moe` supplies the dims and the optional shexp fields decide whether step 3 runs.
1892    ///
1893    /// `il` is the layer index — the residency-cache key prefix. `max_block` is the global max expert
1894    /// stride (fixed cache-slot size); pass `self.max_moe_block()`.
1895    pub(crate) fn moe_ffn(e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>, t: usize,
1896                          cfg: &ModelConfig, il: u16, max_block: usize)
1897               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1898        Self::moe_ffn_inner(e, m, z, None, t, cfg, il, max_block)
1899    }
1900
1901    #[allow(clippy::too_many_arguments)]
1902    pub(crate) fn moe_ffn_inner(
1903        e: &Engine,
1904        m: &MoeWeights,
1905        z: &CudaSlice<f32>,
1906        zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>,
1907        t: usize,
1908        cfg: &ModelConfig,
1909        il: u16,
1910        max_block: usize,
1911    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1912        let worker_io = crate::spill_pread::worker_enabled();
1913        let epoch_lfu = std::env::var_os("MEMRA_MOE_LFU_DECAY").is_some();
1914        if Engine::moe_cache_enabled() && (worker_io || epoch_lfu) {
1915            e.with_moe_cache(max_block, |cache, _| {
1916                cache.begin_forward_epoch(il, t);
1917                if worker_io {
1918                    cache.begin_worker_scope();
1919                }
1920                Ok(())
1921            })?;
1922        }
1923        // A2: Expert-grouped dispatch for prefill (T>1). MEMRA_MOE_GROUPED=1 routes here.
1924        if t > 1 && std::env::var("MEMRA_MOE_GROUPED").is_ok() {
1925            let grouped_out = Self::moe_ffn_grouped(e, m, z, t, cfg, il, max_block)?;
1926            // MEMRA_MOE_GATE: byte-identity comparison vs sequential path.
1927            // KNOWN t>1 MISMATCH maxdiff ~3.4e-4 (deterministic, 5x bit-identical 2026-07-05): the
1928            // sequential arm routes resident experts through the dev_q8 dp4a path (q8_1-quantized z
1929            // and act rows) while grouped stays f32-dequant qmatvec — a quantize-path difference,
1930            // not a bug (per-stage: act q8-vs-f32 ~4-9e-3 abs on |act|<=3, down-only ~1-3e-4; the
1931            // q8_1 activation-quantize error class). MEMRA_MOE_Q8=0 restores BYTE-IDENTICAL.
1932            if std::env::var("MEMRA_MOE_GATE").is_ok() {
1933                let seq_out = Self::moe_ffn_sequential(e, m, z, t, cfg, il, max_block)?;
1934                let g_host = e.dtoh(&grouped_out)?;
1935                let s_host = e.dtoh(&seq_out)?;
1936                let g_bytes: &[u8] = unsafe { std::slice::from_raw_parts(g_host.as_ptr() as *const u8, g_host.len() * 4) };
1937                let s_bytes: &[u8] = unsafe { std::slice::from_raw_parts(s_host.as_ptr() as *const u8, s_host.len() * 4) };
1938                if g_bytes == s_bytes {
1939                    if il == 0 { println!("moe-gate il={il} t={t} BYTE-IDENTICAL (first layer only printed)"); }
1940                } else {
1941                    let diffs = g_host.iter().zip(s_host.iter()).enumerate()
1942                        .filter(|(_, (a, b))| a != b).count();
1943                    let maxdiff = g_host.iter().zip(s_host.iter())
1944                        .map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max);
1945                    panic!("moe-gate il={il} t={t} MISMATCH: {diffs}/{} elems differ, maxdiff={maxdiff:.6e}", g_host.len());
1946                }
1947            }
1948            return Ok(grouped_out);
1949        }
1950        Self::moe_ffn_sequential_zq8(e, m, z, zq8, t, cfg, il, max_block)
1951    }
1952
1953    /// Sequential (per-token) MoE FFN -- the original path. Factored out for the gate comparison.
1954    pub(crate) fn moe_ffn_sequential(e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>, t: usize,
1955                          cfg: &ModelConfig, il: u16, max_block: usize)
1956               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1957        Self::moe_ffn_sequential_zq8(e, m, z, None, t, cfg, il, max_block)
1958    }
1959
1960    /// Append the host-visible router selection for one layer/forward when calibration tracing is
1961    /// enabled. Both sequential and expert-grouped prefill must call this after routing so the
1962    /// trace is independent of the dispatch optimization selected for the forward.
1963    fn trace_moe_routes(il: u16, t: usize, sel_all: &[u32], weights: &[f32])
1964                        -> Result<(), Box<dyn std::error::Error>> {
1965        use std::io::Write as _;
1966        if let Ok(path) = std::env::var("MEMRA_MOE_TRACE") {
1967            let mut f = std::fs::OpenOptions::new().create(true).append(true).open(path)?;
1968            let ids: Vec<String> = sel_all.iter().map(|s| s.to_string()).collect();
1969            writeln!(f, "{} {} {}", il, t, ids.join(","))?;
1970        }
1971        if let Ok(path) = std::env::var("MEMRA_MOE_WEIGHT_TRACE") {
1972            let mut f = std::fs::OpenOptions::new().create(true).append(true).open(path)?;
1973            let pairs: Vec<String> = sel_all.iter().zip(weights)
1974                .map(|(expert, weight)| format!("{expert}:{weight:.9}"))
1975                .collect();
1976            writeln!(f, "{} {} {}", il, t, pairs.join(","))?;
1977        }
1978        Ok(())
1979    }
1980
1981    /// Append the f32 input to one MoE layer for offline, layerwise calibration. This diagnostic
1982    /// intentionally performs a DtoH copy and is therefore disabled unless an explicit fresh trace
1983    /// directory is supplied. Each layer owns one payload file; index.jsonl records byte offsets so
1984    /// a validator can prove request/layer coverage before the trace is used for pruning or healing.
1985    fn trace_moe_input(e: &Engine, il: u16, t: usize, n_embd: usize, z: &CudaSlice<f32>)
1986                       -> Result<(), Box<dyn std::error::Error>> {
1987        use std::io::Write as _;
1988        let Ok(dir) = std::env::var("MEMRA_MOE_INPUT_TRACE_DIR") else { return Ok(()) };
1989        let host = e.dtoh(z)?;
1990        if host.len() != t * n_embd {
1991            return Err(format!(
1992                "MoE input trace shape mismatch at layer {il}: got {} values, expected {}x{}",
1993                host.len(), t, n_embd
1994            ).into());
1995        }
1996        let bytes = unsafe {
1997            std::slice::from_raw_parts(
1998                host.as_ptr().cast::<u8>(), host.len() * std::mem::size_of::<f32>()
1999            )
2000        };
2001        let state = MOE_INPUT_TRACE_WRITER.get_or_init(|| std::sync::Mutex::new(None));
2002        let mut state = state.lock().map_err(|_| "MoE input trace writer lock is poisoned")?;
2003        if state.is_none() {
2004            let dir = std::path::PathBuf::from(&dir);
2005            std::fs::create_dir_all(&dir)?;
2006            let index = std::fs::OpenOptions::new().create(true).append(true)
2007                .open(dir.join("index.jsonl"))?;
2008            *state = Some(MoeInputTraceWriter {
2009                dir,
2010                index,
2011                payloads: std::collections::HashMap::new(),
2012            });
2013        }
2014        let writer = state.as_mut().unwrap();
2015        if writer.dir != std::path::Path::new(&dir) {
2016            return Err("MEMRA_MOE_INPUT_TRACE_DIR changed after capture started".into());
2017        }
2018        let file_name = format!("layer-{il:03}.f32");
2019        if !writer.payloads.contains_key(&il) {
2020            let payload = std::fs::OpenOptions::new().create(true).append(true)
2021                .open(writer.dir.join(&file_name))?;
2022            let offset = payload.metadata()?.len();
2023            writer.payloads.insert(il, (payload, offset));
2024        }
2025        let (payload, offset) = writer.payloads.get_mut(&il).unwrap();
2026        let row_offset = *offset;
2027        payload.write_all(bytes)?;
2028        *offset += bytes.len() as u64;
2029        writeln!(
2030            writer.index,
2031            "{{\"format\":\"memra-moe-input-trace-v1\",\"layer\":{il},\"tokens\":{t},\
2032             \"hidden_size\":{n_embd},\"file\":\"{file_name}\",\"offset\":{row_offset},\
2033             \"payload_bytes\":{}}}",
2034            bytes.len()
2035        )?;
2036        Ok(())
2037    }
2038
2039    #[allow(clippy::too_many_arguments)]
2040    pub(crate) fn moe_ffn_sequential_zq8(
2041        e: &Engine,
2042        m: &MoeWeights,
2043        z: &CudaSlice<f32>,
2044        zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>,
2045        t: usize,
2046        cfg: &ModelConfig,
2047        il: u16,
2048        max_block: usize,
2049    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2050        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
2051        let moe = cfg.moe.as_ref().unwrap();
2052        let n_embd = cfg.n_embd as usize;          // 2048 (gate/up in_f, down out_f)
2053        let n_expert = moe.expert_count as usize;  // 256
2054        let n_used = moe.expert_used_count as usize; // 8
2055        let n_ff_exp = moe.expert_ff_length as usize; // 512 (gate/up out_f, down in_f)
2056
2057        // verify the HostExps dims match cfg (catches a wrong-file / transpose mixup)
2058        debug_assert_eq!(m.gate_exps.in_f, n_embd);
2059        debug_assert_eq!(m.gate_exps.out_f, n_ff_exp);
2060        debug_assert_eq!(m.down_exps.in_f, n_ff_exp);  // down is TRANSPOSED: in=512
2061        debug_assert_eq!(m.down_exps.out_f, n_embd);   //                     out=2048
2062        debug_assert_eq!(m.gate_exps.n_expert, n_expert);
2063
2064        let use_cache = Engine::moe_cache_enabled();
2065        let uniform_experts = m.has_uniform_expert_layout();
2066        let moe_q8 = uniform_experts && moe_q8_enabled()
2067            && q8_expert_supported(m.gate_exps.qtype) && q8_expert_supported(m.up_exps.qtype)
2068            && q8_expert_supported(m.down_exps.qtype);
2069        // Experimental secondary backend: complete experts already resident in the SLRU stay on
2070        // CUDA; any expert missing one or more projections runs from the original host GGUF bytes
2071        // through llama.cpp CPU quant dots. Small-t only keeps decode and speculative verification
2072        // in the same numeric/dispatch class; real prefill remains on the established GPU path and
2073        // seeds the residency cache. An explicit library path is the build/runtime gate, so naked
2074        // commands and CI have no llama.cpp or OpenMP dependency.
2075        let cpu_expert_requested = crate::cpu_experts::configured();
2076        if cpu_expert_requested && (cfg.hy3.is_none() || cfg.m3.is_some()) {
2077            return Err(std::io::Error::other(
2078                "MEMRA_CPU_EXPERT_LIB is experimental and currently gated to Hy3",
2079            )
2080            .into());
2081        }
2082        let cpu_hybrid = cpu_expert_requested && t < PRIME_MIN_T && m.dev_exps.is_none();
2083        // A dynamic SLRU changes which complete experts run on CUDA versus llama.cpp CPU dots.
2084        // Those backends are each deterministic but are different numeric configurations, so a
2085        // later prefill eviction can change greedy output. Freeze after the first real prefill;
2086        // decode/spec reads the fixed resident set, while later prefill misses use transient GPU
2087        // staging below and cannot change backend assignment.
2088        let freeze_cpu_residency = cpu_expert_requested
2089            && std::env::var("MEMRA_CPU_EXPERT_FREEZE_CACHE").as_deref() == Ok("1");
2090        let caller_warms_before_freeze = std::env::var("MEMRA_CPU_EXPERT_FREEZE_WARMUP_TOKENS")
2091            .ok()
2092            .and_then(|value| value.parse::<usize>().ok())
2093            .is_some_and(|tokens| tokens > 0);
2094        if cpu_hybrid && freeze_cpu_residency && !caller_warms_before_freeze {
2095            e.freeze_moe_cache();
2096        }
2097        let cache_frozen = use_cache && e.moe_cache_frozen();
2098        let cache_dispatch = use_cache && (!cache_frozen || cpu_hybrid);
2099
2100        // 1. ROUTER: logits = ffn_gate_inp @ z  -> [T, 256]. gate_inp is F32 -> cuBLASLt, whose
2101        // reductions are n-DEPENDENT (lt_ndep probe: m=1 vs m=2 col0 differs every bit). At
2102        // small t (spec verify, 2..15) that shifts router logits vs the T=1 decode chain ->
2103        // top-k WEIGHTS (and at tie margins the SELECTION) differ -> verify != decode. Route
2104        // small-t through per-column m=1 calls (decode-exact contract); real prefill keeps the
2105        // batched GEMM.
2106        let logits = if t < PRIME_MIN_T {
2107            // t == 1 included since 2026-07-10 (was cuBLAS gemvx, 3.1% + adjacent of the depth
2108            // decode map): decode and verify now route through the SAME kernel — the
2109            // verify==decode router parity holds by construction instead of by FP-order luck.
2110            if crate::router_kernel_on() {
2111                // MEMRA_ROUTER_KERNEL=1: in-house router GEMV (battery-gated numeric config —
2112                // top-k discontinuity means FP-order changes can flip routing; oracle arbitrates).
2113                e.router_gemv(m.gate_inp.float_data(), z, cfg.n_embd as usize,
2114                              m.gate_exps.n_expert, t)?
2115            } else {
2116                e.matmul_decode_exact(&m.gate_inp, z, t)?
2117            }
2118        } else if crate::router_prefill_exact_on() && crate::router_kernel_on() {
2119            // SERVE ISOLATION (lane/concat-prime-exact, 2026-08-02): the cuBLASLt router GEMM
2120            // is m-DEPENDENT — probed on the Ornith-35B router weight, rows [0,19) of an m=65
2121            // call differ from the m=19 call by 3.9e-3 while the same probe on the lm_head /
2122            // wq MMQ+f16 weights is BIT-IDENTICAL across m (research/concat-prime-exact-20260802,
2123            // gemm-razor-router-o35b.log vs gemm-razor-o35b.log). Because the router feeds a
2124            // top-k DISCONTINUITY, that perturbation reorders ties and at ~16% of (layer,token)
2125            // pairs changes the selected expert SET — so a request's own prefill routing depended
2126            // on how many OTHER requests' tokens shared its concat batch (cross-request prime
2127            // batching, worker.rs task #13). The in-house router GEMV computes one row per
2128            // (expert, token) block with a fixed per-row reduction order and is m-INVARIANT
2129            // (same probe: BIT-IDENTICAL, gemm-razor-router-gemv-o35b.log), so routing prefill
2130            // through it makes a session's routing a function of its OWN tokens alone — the
2131            // serving isolation contract at the prime level. MEMRA_ROUTER_PREFILL_EXACT=0 reverts
2132            // to the batched cuBLASLt GEMM (numeric-config rollback seam).
2133            e.router_gemv(m.gate_inp.float_data(), z, cfg.n_embd as usize,
2134                          m.gate_exps.n_expert, t)?
2135        } else {
2136            e.matmul(&m.gate_inp, z, t)?
2137        };
2138
2139        // LAUNCH-STRUCTURE STAGE 3 (2026-07-05, MEMRA_MOE_DEV default ON, =0 rollback): ZERO-DtoH
2140        // device-dispatch when this layer's expert blocks are ALL cache-resident. The fused
2141        // router's sel/w stay ON DEVICE; the expert weight pointers come from the per-layer
2142        // device table of fixed slot addresses; gate/up/silu + down/fma run as the same TWO
2143        // launches per token as gdec. Removes the per-layer router DtoH + stream sync — the
2144        // per-token host stall that dominated the 35B decode wall after stages 1+2.
2145        // BIT-IDENTITY: the router kernel is selection-exact vs the host oracle (kernel-check
2146        // tie gate) and the _dev matvec twins reproduce the gdec kernels' exact FP chains; the
2147        // only difference is where sel/w/pointers are READ from (device instead of params).
2148        // Residency: one-shot PREWARM force-admits the layer while free slots cover it
2149        // (MEMRA_MOE_PREWARM=0 -> organic residency, dev path fires when the SLRU fills).
2150        // Any non-resident layer falls through to host routing + the gdec/sequential path.
2151        // FITS-VRAM RESIDENT EXPERTS (2026-07-06): the layer's expert slabs are device-resident
2152        // (load-time decision) — fire the zero-DtoH dev path unconditionally with the prebuilt
2153        // pointer row. No cache, no dispatch, no residency check: the llama full-offload regime
2154        // (it measured 169.55 vs the cache path's 28.5 on the local 35B — the residency-gate
2155        // all-or-nothing fallback was the 6x). BIT-IDENTITY: same _dev kernels, same math; only
2156        // the pointer table's provenance differs (slab base+stride vs SLRU slot addresses).
2157        // MoE PREFILL PAIR-BATCH (2026-07-06, the 16x pp hole): t>1 on resident experts — ONE
2158        // launch per proj covers ALL (token,expert) pairs (grid.y=pair, warp-per-row), replacing
2159        // the per-expert loop (256 experts x 3-4 launches x tiny m_e). Scatter is slot-ordered
2160        // per token (the sequential-axpy bit-identity class). Requires q8-supported qtypes +
2161        // resident slabs. MEMRA_MOE_PAIRS=0 rollback.
2162        // t >= PRIME_MIN_T only (2026-07-06 exactness fix, verify-probe proof): spec VERIFY
2163        // batches (t = 2..K+2) previously rode these pairs kernels while T=1 decode rode the
2164        // dev_q8 loop — different FP chains, verify-T2 logit maxdiff 2.6e-1 vs eager -> greedy
2165        // flips at tight margins -> 35B real-prompt spec self-consistency FAIL (the 27B
2166        // "verify must be kernel-DISPATCH-identical to decode" lesson, MoE edition). Small-t
2167        // now rides the dev loop below (same kernels per token as decode); pairs serves real
2168        // prefill (t >= 16, where spec never verifies).
2169        // sigmoid-router archs (M3, Hy3) must NOT enter the pairs/dev arms: those route via the
2170        // fused SOFTMAX device router (moe_router_topk) — silently wrong experts (the M3
2171        // gate-MISMATCH 74602-vs-92 lesson, 2026-07-07). Host sigmoid routing below is correct.
2172        // Per-expert macro-scales (compressed-tensors NVFP4 ST class, e.g. unsloth 35B-A3B):
2173        // the device-dispatch kernels (pairs/dev) do NOT fold them — those checkpoints must
2174        // ride the macro-aware sequential/staged paths below or every expert output is off by
2175        // its global scale (~3e4x, measured garbage 2026-07-16). GGUF experts: macros None.
2176        let no_exp_macros = m.gate_exps.macros.is_none() && m.up_exps.macros.is_none()
2177            && m.down_exps.macros.is_none();
2178        if cfg.sigmoid_router().is_none() && cfg.m3.is_none() && cfg.hy3.is_none()
2179            && no_exp_macros
2180            && t >= PRIME_MIN_T && m.dev_exps.is_some() && moe_q8_enabled()
2181            && q8_expert_supported(m.gate_exps.qtype) && q8_expert_supported(m.up_exps.qtype)
2182            && q8_expert_supported(m.down_exps.qtype)
2183            && std::env::var("MEMRA_MOE_PAIRS").map(|v| v != "0").unwrap_or(true)
2184            && std::env::var("MEMRA_MOE_STATS").is_err() {
2185            return Self::moe_ffn_pairs(e, m, z, &logits, t, cfg);
2186        }
2187
2188        // t < PRIME_MIN_T: moe_ffn_dev loops tokens serially (1 launch-pair per token) — the
2189        // decode path AND the spec-verify path (dispatch parity = exactness; see pairs gate
2190        // above). Serial launches are fine at t<=10 (K+2); real prefill never lands here.
2191        // moe_ffn_dev routes via the FUSED SOFTMAX device router (moe_router_topk) — sigmoid
2192        // routing (M3, Hy3: +expert bias) has no device kernel yet, so those arches must NOT
2193        // enter the dev arms: with MOE_CACHE=1 M3 silently routed softmax = wrong experts
2194        // (gate MISMATCH 74602 vs 92, caught 2026-07-07). Host sigmoid path below is correct.
2195        // macro-carrying experts are handled inside the dev path now (epilogue fold + w-scale);
2196        // pairs/gdec/csr keep their macro gates until their kernels grow the fold.
2197        let dev_ok = uniform_experts && cfg.m3.is_none() && cfg.hy3.is_none();
2198        // Observation modes must route through the host-visible selection below. Otherwise a fully
2199        // resident layer returns through device dispatch before its trace/stats row is recorded,
2200        // silently biasing calibration toward only non-resident layers on large-VRAM machines.
2201        let observe_routes = std::env::var("MEMRA_MOE_STATS").is_ok()
2202            || std::env::var("MEMRA_MOE_TRACE").is_ok()
2203            || std::env::var("MEMRA_MOE_WEIGHT_TRACE").is_ok()
2204            || std::env::var("MEMRA_MOE_INPUT_TRACE_DIR").is_ok();
2205        if dev_ok && t < PRIME_MIN_T && m.dev_exps.is_some() && n_used <= 8 && moe_dev_enabled()
2206            && !observe_routes {
2207            return Self::moe_ffn_dev(e, m, z, zq8, &logits, t, cfg, il, max_block);
2208        }
2209        if dev_ok && use_cache && n_used <= 8 && moe_dev_enabled()
2210            && !observe_routes {
2211            let row_ok = e.with_moe_cache(max_block, |c, eng| {
2212                if moe_prewarm_enabled() { c.prewarm_layer(il, m, eng)?; }
2213                Ok(c.layer_dev_row(il, n_expert, eng)?.is_some())
2214            })?;
2215            if row_ok {
2216                return Self::moe_ffn_dev(e, m, z, zq8, &logits, t, cfg, il, max_block);
2217            }
2218        }
2219
2220        // Per-token (sel[8], w[8]) — sigmoid host oracle (M3/Hy3), else fused-router/softmax.
2221        let (sel_all, w_all, routed_cpu_input) = if let Some(sig) = cfg.sigmoid_router() {
2222            if cpu_hybrid {
2223                let (sel, w, input) = Self::moe_route_sigmoid_with_input(
2224                    e,
2225                    &logits,
2226                    z,
2227                    t,
2228                    n_expert,
2229                    n_used,
2230                    m.exp_probs_b.as_deref(),
2231                    sig,
2232                    m.active_experts.as_deref(),
2233                )?;
2234                (sel, w, Some(input))
2235            } else {
2236                let (sel, w) = Self::moe_route_cfg(
2237                    e,
2238                    &logits,
2239                    t,
2240                    n_expert,
2241                    n_used,
2242                    m.exp_probs_b.as_deref(),
2243                    Some(sig),
2244                    m.active_experts.as_deref(),
2245                )?;
2246                (sel, w, None)
2247            }
2248        } else {
2249            let (sel, w) = Self::moe_route_cfg(
2250                e,
2251                &logits,
2252                t,
2253                n_expert,
2254                n_used,
2255                None,
2256                None,
2257                m.active_experts.as_deref(),
2258            )?;
2259            (sel, w, None)
2260        };
2261
2262        // MEMRA_MOE_TRACE=<path>: append one line per (layer, step) with the selected expert ids —
2263        // offline analysis derives the decode working set + step-to-step reuse (the go/no-go
2264        // measurement for resident-expert tiering; see rig5090.jsonl 2026-07-07 pinned-tier row).
2265        Self::trace_moe_routes(il, t, &sel_all, &w_all)?;
2266        Self::trace_moe_input(e, il, t, n_embd, z)?;
2267
2268        // Worker reads ordinarily overlap NVMe with the current expert but leave their H2D copies
2269        // demand-serialized on the compute stream. Hy3 decode already has a safe owner-thread
2270        // boundary here: sigmoid routing read `logits` back to the host, which synchronized all
2271        // earlier-layer compute. Submit the complete selected set first, then reserve only victims
2272        // outside that set and queue its H2D copies on the copy stream. Dispatch inserts an event
2273        // wait for each pending block, so later copies can overlap the earlier expert kernels while
2274        // the selected weights and numerical kernels remain unchanged. Restrict this experiment to
2275        // T=1; batched forwards can have token-local consumers still in flight between selections.
2276        // The CPU backend reads mmap-backed model bytes directly. Positioned-read worker buffers
2277        // are CUDA write-combined staging allocations and are intentionally not CPU-compute inputs;
2278        // do not spend NVMe bandwidth filling them for a layer whose misses go to CPU.
2279        let worker_disk_prefetch =
2280            cache_dispatch && crate::spill_pread::worker_enabled() && !cpu_hybrid;
2281        let promote_worker_h2d =
2282            t == 1 && worker_disk_prefetch && crate::spill_pread::copy_h2d_enabled();
2283        if promote_worker_h2d {
2284            let mut selected_blocks = Vec::with_capacity(n_used * 3);
2285            for &ex in sel_all.iter().take(n_used) {
2286                let ex = ex as u16;
2287                selected_blocks.extend([
2288                    BlockId::new(il, PROJ_GATE, ex),
2289                    BlockId::new(il, PROJ_UP, ex),
2290                    BlockId::new(il, PROJ_DOWN, ex),
2291                ]);
2292            }
2293            for &ex in sel_all.iter().take(n_used) {
2294                Self::moe_prefetch_disk_expert(e, il, ex as usize, m, max_block, &selected_blocks)?;
2295            }
2296            e.with_moe_cache(max_block, |cache, eng| {
2297                cache.promote_worker_reads_at_safe_boundary(
2298                    &selected_blocks,
2299                    &selected_blocks,
2300                    eng,
2301                )?;
2302                Ok(())
2303            })?;
2304        }
2305
2306        // MEMRA_MOE_STATS: per-layer routing stats for the A2 (expert-grouped prefill) baseline —
2307        // per-token expert-id entropy, active-expert coverage, tokens-per-expert group sizes.
2308        if t > 1 && std::env::var("MEMRA_MOE_STATS").is_ok() {
2309            let mut cnt = vec![0u32; n_expert];
2310            for &s in sel_all.iter() { cnt[s as usize] += 1; }
2311            let total = sel_all.len() as f64;
2312            let mut h = 0.0f64;
2313            let mut active = 0usize;
2314            for &c in &cnt { if c > 0 { active += 1; let p = c as f64 / total; h -= p * p.log2(); } }
2315            let maxc = cnt.iter().copied().max().unwrap_or(0);
2316            println!("moe-stats il={} t={} assignments={} active={}/{} entropy={:.3}b (max {:.3}b) mean_tok_per_active={:.2} max_tok_per_expert={}",
2317                     il, t, sel_all.len(), active, n_expert, h, (n_expert as f64).log2(), total / active.max(1) as f64, maxc);
2318        }
2319
2320        // LAUNCH-STRUCTURE STAGE 2 (2026-07-05): moe_out memset elision on the gdec path.
2321        // moe_down8_fma_f32 FULLY overwrites its token row (dst[o] = the in-kernel FMA chain that
2322        // starts at 0.0f — numerically the axpy-into-zeroed-row chain), so when the grouped-decode
2323        // path fires the upfront `e.zeros(t*n_embd)` memset is pure launch churn. Allocate uninit
2324        // when gdec CAN fire (any t — decode t=1 AND the spec verify t=K+1 route here per token)
2325        // and lazily zero ONLY the row of a token that falls through to the sequential axpy loop.
2326        // BIT-IDENTITY: unchanged — every row is either fully overwritten (gdec) or
2327        // zeroed-then-accumulated exactly as before (fallback).
2328        let gdec_may_fire = uniform_experts && use_cache && n_used <= 8 && gdec_enabled();
2329        let mut moe_out = if gdec_may_fire {
2330            e.uninit(t * n_embd)?
2331        } else {
2332            e.zeros(t * n_embd)?
2333        };
2334        // The router readback above already established a host boundary. Copy each small-t hidden
2335        // row once so CPU miss experts can start while the owner thread queues resident GPU work.
2336        let cpu_input = if cpu_hybrid {
2337            Some(routed_cpu_input.ok_or("CPU expert routing did not return the MoE input")?)
2338        } else {
2339            None
2340        };
2341
2342        // GPU scratch: one slot per proj, big enough for ONE expert (default stage-every-token path).
2343        // STAGE 2: LAZY — allocated only if the no-cache staging path actually runs (under
2344        // MEMRA_MOE_CACHE they were 3 dead ~1MB alloc_zeros + memset + free per layer per token,
2345        // measured ~123 memsets/token of the decode wall).
2346        let g_len = m.gate_exps.max_expert_bytes();  // 860160 for the uniform 35B gate
2347        let u_len = m.up_exps.max_expert_bytes();    // 860160 for the uniform 35B up
2348        let d_len = m.down_exps.max_expert_bytes();  // 1114112 for the uniform 35B down
2349        let mut scratch_g: Option<CudaSlice<u8>> = None;
2350        let mut scratch_u: Option<CudaSlice<u8>> = None;
2351        let mut scratch_d: Option<CudaSlice<u8>> = None;
2352        // `max_block` (the GLOBAL max expert stride across all layers) is passed in — the cache slots
2353        // are FIXED-ADDRESS and must fit any layer's block (UD/dynamic GGUFs vary quant per layer).
2354
2355        // EDGE-1 §C.2/C.3: the optional pipeline queues the next selected expert's MISS blocks on
2356        // the copy stream before launching the current expert's compute. Pending slots stay invisible
2357        // to cache hits until dispatch inserts the completion-event wait; current gate/up/down ids are
2358        // protected from eviction. This changes scheduling only, never the GGUF bytes or GEMM path.
2359        let page_window = moe_page_prefetch_window();
2360
2361        // 2. PER TOKEN: routed-expert loop. The ONE dispatch change vs Stage-1: a resident slot
2362        //    (cache HIT, no H2D) OR a staged slot (MISS) feeds the SAME unchanged qmatvec_view.
2363        for tok in 0..t {
2364            let sel = &sel_all[tok * n_used..(tok + 1) * n_used];
2365            let w = &w_all[tok * n_used..(tok + 1) * n_used];
2366            let zt = z.slice(tok * n_embd..(tok + 1) * n_embd);  // CudaView<f32>
2367            let mut tok_q8: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
2368
2369            // STAGE-2 GROUPED DECODE (2026-07-04, MEMRA_MOE_GDEC default ON, =0 rollback): fold
2370            // this token's whole routed-expert FFN (8x gate/up/silu + 8x down/axpy = 40 launches)
2371            // into TWO launches via expert-pointer indirection over the fixed-address cache slots.
2372            // Fires only when ALL 3*n_used blocks are ALREADY cache-resident (pure-HIT: zero
2373            // memcpy, zero admission, so no slot can move under the collected pointers) — any
2374            // miss falls through to the sequential loop below, which admits as before. In steady
2375            // state on a fully-resident rig every token-layer takes the grouped path.
2376            // BIT-IDENTITY: each in-kernel dot reproduces qmatvec_f32's exact reduction; SiLU is
2377            // silu_mul_f32's exact expression; the down accumulation is a slot-ordered
2378            // __fmaf_rn chain == the sequential axpy_f32 chain (MEMRA_MOE_GDEC_GATE compares).
2379            // cfg.m3: the grouped kernels' fused epilogues are plain SiLU — M3's swigluoai must
2380            // NOT take them until the kernels grow the clamped variant. NVFP4 experts carry
2381            // per-expert macro-scales the fused kernels don't fold — those fall through too.
2382            let no_macros = m.gate_exps.macros.is_none() && m.up_exps.macros.is_none()
2383                && m.down_exps.macros.is_none();
2384            if gdec_may_fire && moe_q8 && cfg.m3.is_none() && no_macros {
2385                if tok_q8.is_none() {
2386                    tok_q8 = Some(e.quantize_q8_1_view(&zt, 1, n_embd)?);
2387                }
2388                let (zq, zd) = tok_q8.as_ref().unwrap();
2389                if Self::moe_gdec_token_q8(e, m, il, max_block, zq, zd, sel, w,
2390                                           &mut moe_out, tok, n_embd, n_ff_exp, n_used)? {
2391                    continue;
2392                }
2393            } else if gdec_may_fire && cfg.m3.is_none() && no_macros
2394                && Self::moe_gdec_token(e, m, il, max_block, &zt, sel, w,
2395                                        &mut moe_out, tok, n_embd, n_ff_exp, n_used)? {
2396                continue;
2397            }
2398
2399            // STAGE 2 memset-elision invariant: moe_out was allocated UNINIT when gdec could fire.
2400            // This token fell through to the sequential axpy loop, which ACCUMULATES — zero its row
2401            // first (row-sized memset, replaces the old full-buffer zeros; other rows are gdec-owned).
2402            if gdec_may_fire {
2403                let mut row = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
2404                e.memset_zeros_view(&mut row)?;
2405            }
2406
2407            // Fiddler-style split at whole-expert granularity. Partial residency is deliberately a
2408            // CPU assignment: transferring the missing projection would reintroduce the PCIe/NVMe
2409            // stall this path exists to remove, while mixing projections would require another
2410            // activation round-trip. Weight addresses remain valid until this worker is joined at
2411            // the bottom of the token scope.
2412            let mut cpu_mask = vec![false; sel.len()];
2413            let cpu_worker = if let Some(host_input) = cpu_input.as_ref() {
2414                let gpu_resident = if use_cache {
2415                    e.with_moe_cache(max_block, |cache, _| {
2416                        Ok(sel
2417                            .iter()
2418                            .map(|&expert| {
2419                                let expert = expert as u16;
2420                                [PROJ_GATE, PROJ_UP, PROJ_DOWN]
2421                                    .into_iter()
2422                                    .filter(|&projection| {
2423                                        cache
2424                                            .resident(BlockId::new(il, projection, expert))
2425                                            .is_some()
2426                                    })
2427                                    .count()
2428                            })
2429                            .collect::<Vec<_>>())
2430                    })?
2431                } else {
2432                    vec![0; sel.len()]
2433                };
2434                let mut cpu_selected = Vec::new();
2435                for (index, (&expert, &route_weight)) in sel.iter().zip(w).enumerate() {
2436                    if gpu_resident[index] != 3 {
2437                        cpu_mask[index] = true;
2438                        crate::cpu_experts::record_incomplete_gpu_residency(gpu_resident[index]);
2439                        let expert = expert as usize;
2440                        cpu_selected.push((expert, route_weight));
2441                    }
2442                }
2443                if crate::cpu_experts::predictor_enabled() {
2444                    // Fire-and-forget lookahead: the predictor worker scores layers il+1..
2445                    // from this layer's MoE input and prefetches predicted-and-missing
2446                    // experts into the companion RAM cache. Never blocks this thread.
2447                    let row = &host_input[tok * n_embd..(tok + 1) * n_embd];
2448                    crate::cpu_experts::predictor_submit(il, row);
2449                }
2450                if cpu_selected.is_empty() {
2451                    None
2452                } else {
2453                    let row = &host_input[tok * n_embd..(tok + 1) * n_embd];
2454                    let job = crate::cpu_experts::prepare_job(m, il, &cpu_selected, row)
2455                        .map_err(std::io::Error::other)?;
2456                    Some(crate::cpu_experts::submit(job).map_err(std::io::Error::other)?)
2457                }
2458            } else {
2459                None
2460            };
2461
2462            let worker_window = worker_disk_prefetch
2463                .then(worker_prefetch_window)
2464                .unwrap_or(0);
2465            for (j, &ex) in sel.iter().enumerate() {
2466                if cpu_mask[j] {
2467                    continue;
2468                }
2469                let ex = ex as usize;
2470                for next in page_prefetch_positions(j, sel.len(), page_window) {
2471                    Self::moe_prefetch_host_expert(sel[next] as usize, m);
2472                }
2473                let keep = [
2474                    crate::moe_cache::BlockId::new(il, crate::moe_cache::PROJ_GATE, ex as u16),
2475                    crate::moe_cache::BlockId::new(il, crate::moe_cache::PROJ_UP, ex as u16),
2476                    crate::moe_cache::BlockId::new(il, crate::moe_cache::PROJ_DOWN, ex as u16),
2477                ];
2478                if worker_disk_prefetch && worker_window > 0 {
2479                    for next in worker_prefetch_positions(j, sel.len(), worker_window) {
2480                        Self::moe_prefetch_disk_expert(
2481                            e,
2482                            il,
2483                            sel[next] as usize,
2484                            m,
2485                            max_block,
2486                            &keep,
2487                        )?;
2488                    }
2489                } else if cache_dispatch
2490                    && !cpu_hybrid
2491                    && moe_prefetch_enabled()
2492                    && j + 1 < sel.len()
2493                {
2494                    let next = sel[j + 1] as usize;
2495                    Self::moe_prefetch_expert(e, il, next, m, max_block, &keep)?;
2496                }
2497                let [gate_q8, up_q8, down_q8] = [moe_q8; 3];
2498                if cache_dispatch && (gate_q8 || up_q8 || down_q8) {
2499                    // dp4a EXPERT PATH (MEMRA_MOE_Q8): quantize z-row once per token. Mixed expert
2500                    // layouts stay on the metadata-aware f32 path.
2501                    if (gate_q8 || up_q8) && tok_q8.is_none() {
2502                        tok_q8 = Some(e.quantize_q8_1_view(&zt, 1, n_embd)?);
2503                    }
2504                    let gate = if gate_q8 {
2505                        let (zq, zd) = tok_q8.as_ref().unwrap();
2506                        Self::moe_cached_gemm_q8(e, il, PROJ_GATE, ex, m, max_block, zq, zd)?
2507                    } else {
2508                        Self::moe_cached_gemm(e, il, PROJ_GATE, ex, m, max_block, &zt)?
2509                    };
2510                    let up = if up_q8 {
2511                        let (zq, zd) = tok_q8.as_ref().unwrap();
2512                        Self::moe_cached_gemm_q8(e, il, PROJ_UP, ex, m, max_block, zq, zd)?
2513                    } else {
2514                        Self::moe_cached_gemm(e, il, PROJ_UP, ex, m, max_block, &zt)?
2515                    };
2516                    let mut act = e.uninit(n_ff_exp)?;
2517                    Self::ffn_act_scaled(
2518                        e,
2519                        cfg,
2520                        &gate,
2521                        &up,
2522                        m.gate_exps.macro_scale(ex),
2523                        m.up_exps.macro_scale(ex),
2524                        &mut act,
2525                        n_ff_exp,
2526                    )?;
2527                    let y = if down_q8 {
2528                        let (aq2, ad2) = e.quantize_q8_1(&act, 1, n_ff_exp)?;
2529                        Self::moe_cached_gemm_q8(e, il, PROJ_DOWN, ex, m, max_block, &aq2, &ad2)?
2530                    } else {
2531                        let actv = act.slice(0..n_ff_exp);
2532                        Self::moe_cached_gemm(e, il, PROJ_DOWN, ex, m, max_block, &actv)?
2533                    };
2534                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
2535                    // down-proj macro folds into the accumulate weight (1.0 for non-macro archs).
2536                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
2537                } else if cache_dispatch {
2538                    // SLRU residency cache: per-projection, dispatch the block (HIT => resident slot,
2539                    // MISS => staged slot) then run the SAME unchanged qmatvec_view from that slot.
2540                    // The bytes the kernel reads are byte-for-byte the same GGUF block (§B.3); the
2541                    // only difference between HIT and MISS is whether the memcpy_htod ran.
2542                    let gate = Self::moe_cached_gemm(e, il, PROJ_GATE, ex, m, max_block, &zt)?;
2543                    let up   = Self::moe_cached_gemm(e, il, PROJ_UP,   ex, m, max_block, &zt)?;
2544                    let mut act = e.uninit(n_ff_exp)?;  // activation fully overwrites
2545                    Self::ffn_act_scaled(e, cfg, &gate, &up,
2546                        m.gate_exps.macro_scale(ex), m.up_exps.macro_scale(ex), &mut act, n_ff_exp)?;
2547                    let actv = act.slice(0..n_ff_exp);
2548                    let y = Self::moe_cached_gemm(e, il, PROJ_DOWN, ex, m, max_block, &actv)?;
2549                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
2550                    // down-proj macro folds into the accumulate weight (post-matmul linear scale).
2551                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
2552                } else if cache_frozen {
2553                    // A later prompt prime must not change the CPU/GPU assignment frozen after the
2554                    // first prime. Reuse every fixed resident projection directly and stage only a
2555                    // true miss through the ordinary scratch slot. This preserves the established
2556                    // f32-dequant numeric path while avoiding a full-bank reread on every prime.
2557                    let gate = Self::moe_frozen_gemm(
2558                        e,
2559                        il,
2560                        PROJ_GATE,
2561                        ex,
2562                        m,
2563                        max_block,
2564                        &zt,
2565                        &mut scratch_g,
2566                        g_len,
2567                    )?;
2568                    let up = Self::moe_frozen_gemm(
2569                        e,
2570                        il,
2571                        PROJ_UP,
2572                        ex,
2573                        m,
2574                        max_block,
2575                        &zt,
2576                        &mut scratch_u,
2577                        u_len,
2578                    )?;
2579                    let mut act = e.uninit(n_ff_exp)?;
2580                    Self::ffn_act_scaled(
2581                        e,
2582                        cfg,
2583                        &gate,
2584                        &up,
2585                        m.gate_exps.macro_scale(ex),
2586                        m.up_exps.macro_scale(ex),
2587                        &mut act,
2588                        n_ff_exp,
2589                    )?;
2590                    let actv = act.slice(0..n_ff_exp);
2591                    let y = Self::moe_frozen_gemm(
2592                        e,
2593                        il,
2594                        PROJ_DOWN,
2595                        ex,
2596                        m,
2597                        max_block,
2598                        &actv,
2599                        &mut scratch_d,
2600                        d_len,
2601                    )?;
2602                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
2603                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
2604                } else {
2605                    // Stage-1: stage gate/up/down for expert `ex` into the scratch slots, then GEMM.
2606                    // Lazy scratch: first no-cache expert allocates the 3 slots (uninit — stage_expert
2607                    // fully overwrites the byte range the GEMM reads).
2608                    if scratch_g.is_none() {
2609                        scratch_g = Some(e.alloc_u8_uninit(g_len)?);
2610                        scratch_u = Some(e.alloc_u8_uninit(u_len)?);
2611                        scratch_d = Some(e.alloc_u8_uninit(d_len)?);
2612                    }
2613                    let (sg, su, sd) = (scratch_g.as_mut().unwrap(), scratch_u.as_mut().unwrap(),
2614                                        scratch_d.as_mut().unwrap());
2615                    let gl = m.gate_exps.expert_layout(ex);
2616                    let ul = m.up_exps.expert_layout(ex);
2617                    let dl = m.down_exps.expert_layout(ex);
2618                    e.stage_expert(m.gate_exps.expert_bytes(ex), sg, 0)?;
2619                    let gate = e.qmatvec_view(sg, 0..gl.len, &zt, 1,
2620                        m.gate_exps.in_f, m.gate_exps.out_f, gl.qtype, gl.row_bytes)?;
2621
2622                    e.stage_expert(m.up_exps.expert_bytes(ex), su, 0)?;
2623                    let up = e.qmatvec_view(su, 0..ul.len, &zt, 1,
2624                        m.up_exps.in_f, m.up_exps.out_f, ul.qtype, ul.row_bytes)?;
2625
2626                    let mut act = e.uninit(n_ff_exp)?;  // activation fully overwrites
2627                    Self::ffn_act_scaled(e, cfg, &gate, &up,
2628                        m.gate_exps.macro_scale(ex), m.up_exps.macro_scale(ex), &mut act, n_ff_exp)?;
2629
2630                    e.stage_expert(m.down_exps.expert_bytes(ex), sd, 0)?;
2631                    let actv = act.slice(0..n_ff_exp);
2632                    let y = e.qmatvec_view(sd, 0..dl.len, &actv, 1,
2633                        m.down_exps.in_f, m.down_exps.out_f, dl.qtype, dl.row_bytes)?;
2634
2635                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
2636                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
2637                }
2638            }
2639            if let Some(worker) = cpu_worker {
2640                let cpu_output = worker.wait().map_err(std::io::Error::other)?;
2641                let cpu_output = e.htod(&cpu_output)?;
2642                let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
2643                e.axpy_into(&cpu_output, 1.0, &mut dst, n_embd)?;
2644            }
2645            if cpu_hybrid && !cache_frozen && cpu_expert_profile_admit_enabled() {
2646                for (j, &ex) in sel.iter().enumerate() {
2647                    if cpu_mask[j] {
2648                        Self::moe_profile_admit_expert(e, il, ex as usize, m, max_block)?;
2649                    }
2650                }
2651            }
2652        }
2653
2654        // 3. SHARED EXPERT (ALWAYS-ON, no routing) on the SAME z — qwen35moe only. OLMoE and most
2655        //    vanilla MoE have NO shared expert (the shexp tensors are absent / `None`); skip it then.
2656        // gate_inp_shexp is OPTIONAL: qwen35moe gates the shared expert (sigmoid(gate_inp) x sh);
2657        // MiniMax-M3 (DeepSeek-V3 class) has NO shexp gate — the shared expert adds directly.
2658        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
2659            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
2660        {
2661            let n_ff_sh = gate_shexp.out_features();  // 512
2662            // Q8 TRUNK-FUSION (decode t=1): gate_shexp+up_shexp are Q8_0 same-shape on the 35B —
2663            // ONE fused2 launch (also folds the two per-matmul re-quantizes of z into one).
2664            // Bit-identical per (tensor,row); falls back to the two matmul calls when ineligible.
2665            // Small-t (spec verify 2..15) rides matmul_decode_exact so shexp FP chains match the
2666            // t==1 decode chain per column (cuBLASLt n-dependence + dp4a-vs-mmvq class); real
2667            // prefill keeps the batched matmul. Activation routes through ffn_act (SiLU for
2668            // softmax archs, clamped swigluoai for M3 — identical to silu_mul when cfg.m3 is None).
2669            let verify_t = t > 1 && t < PRIME_MIN_T;
2670            let (sg_gate, sg_up) = if t == 1 {
2671                match e.matmul_q8_fused2_x(gate_shexp, up_shexp, z)? {
2672                    Some(pair) => pair,
2673                    None => (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?),
2674                }
2675            } else if verify_t {
2676                (e.matmul_decode_exact(gate_shexp, z, t)?, e.matmul_decode_exact(up_shexp, z, t)?)
2677            } else {
2678                (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?)   // [T, 512] each
2679            };
2680            let mut sa = e.uninit(t * n_ff_sh)?;  // activation fully overwrites
2681            Self::ffn_act(e, cfg, &sg_gate, &sg_up, &mut sa, t * n_ff_sh)?;
2682            let sh = if verify_t { e.matmul_decode_exact(down_shexp, &sa, t)? }
2683                     else { e.matmul(down_shexp, &sa, t)? };     // [T, n_embd]
2684
2685            // shexp gate: qwen35moe sigmoid-gates via ffn_gate_inp_shexp (1-D ne=[n_embd] ->
2686            // out_f=1); M3 has no gate tensor -> weight 1.0. Decode + verify ride the fused
2687            // sigmoid-dot kernel (one fold order for both chains; kills the per-layer
2688            // cuBLASLt m=1 splitK GEMM — 40x/step, ~10% of the H100 q35 decode step).
2689            // SERVE ISOLATION (lane/concat-prime-exact, 2026-08-02): PREFILL rides it too.
2690            // This out_f=1 cuBLASLt GEMV is the SECOND m-dependent op in the trunk (probed:
2691            // rows [0,19) move by 1.07e-4 between m=74 and m=75 while sigmoid_dot_rows is
2692            // BIT-IDENTICAL — allw-shexpgate-o35b.log). The gate multiplies the shared
2693            // expert's contribution into every token's residual, so under cross-request
2694            // concat prefill a session's hidden state depended on its co-arrivals' token
2695            // count. MEMRA_ROUTER_PREFILL_EXACT=0 reverts to the batched cuBLASLt linear.
2696            let g = match &m.gate_inp_shexp {
2697                Some(gate_inp_shexp) => {
2698                    if t < PRIME_MIN_T || crate::router_prefill_exact_on() {
2699                        e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
2700                    } else {
2701                        let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
2702                        let mut g = e.uninit(t)?;  // sigmoid fully overwrites
2703                        e.sigmoid(&gs, &mut g, t)?;
2704                        g
2705                    }
2706                }
2707                None => e.htod(&vec![1.0f32; t])?,
2708            };
2709            // moe_out[r, :] += sh[r, :] * g[r]   (per-token scalar gate; g=1 ungated)
2710            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
2711        }
2712
2713        Ok(moe_out)
2714    }
2715
2716    /// Stage-1 (no-cache) per-DECODE-TOKEN H2D bytes: every routed block re-staged every layer every
2717    /// token = sum over MoE layers of n_used * (gate+up+down expert_stride). The §D.4 PCIe baseline.
2718    pub fn stage1_h2d_per_token(&self) -> u64 {
2719        use crate::hybrid::Ffn;
2720        let n_used = self.cfg.moe.as_ref().map(|m| m.expert_used_count as u64).unwrap_or(0);
2721        let mut bytes = 0u64;
2722        for l in self.layers.iter() {
2723            if let Ffn::Moe(m) = &l.ffn {
2724                bytes += n_used * (m.gate_exps.max_expert_bytes() + m.up_exps.max_expert_bytes()
2725                                   + m.down_exps.max_expert_bytes()) as u64;
2726            }
2727        }
2728        bytes
2729    }
2730
2731    /// Largest expert block (bytes) across ALL MoE layers + the MTP head — the fixed cache slot size.
2732    /// UD/dynamic GGUFs quant different layers differently, so `expert_stride` varies per layer; the
2733    /// residency cache slots are fixed-address and must fit any block, so size to this global max.
2734    pub(crate) fn max_moe_block(&self) -> usize {
2735        use crate::hybrid::Ffn;
2736        let mut mx = 0usize;
2737        let mut scan = |ffn: &Ffn| {
2738            if let Ffn::Moe(m) = ffn {
2739                mx = mx.max(m.gate_exps.max_expert_bytes())
2740                       .max(m.up_exps.max_expert_bytes())
2741                       .max(m.down_exps.max_expert_bytes());
2742            }
2743        };
2744        for l in self.layers.iter() { scan(&l.ffn); }
2745        if let Some(mtp) = self.mtp.as_ref() { scan(&mtp.ffn); }
2746        mx
2747    }
2748
2749    /// Exact retained projection lengths for cache class sizing. Pruned ids keep router positions
2750    /// but have no bytes and therefore consume no residency slot.
2751    pub(crate) fn moe_cache_block_sizes(&self) -> Vec<usize> {
2752        use crate::hybrid::Ffn;
2753        let mut sizes = Vec::new();
2754        let mut scan = |ffn: &Ffn| {
2755            let Ffn::Moe(m) = ffn else { return };
2756            for ex in 0..m.gate_exps.n_expert {
2757                if m.active_experts.as_ref().is_some_and(|active| !active[ex]) {
2758                    continue;
2759                }
2760                for exps in [&m.gate_exps, &m.up_exps, &m.down_exps] {
2761                    let len = exps.expert_layout(ex).len;
2762                    if len > 0 {
2763                        sizes.push(len);
2764                    }
2765                }
2766            }
2767        };
2768        for layer in &self.layers {
2769            scan(&layer.ffn);
2770        }
2771        if let Some(mtp) = &self.mtp {
2772            scan(&mtp.ffn);
2773        }
2774        sizes
2775    }
2776
2777    /// Persist the frozen residency set so a later process can restage it directly and skip
2778    /// the profiling warmup. Plain text: a versioned header binding slot geometry, then one
2779    /// `layer proj ex` triple per line. A mismatched or stale profile is rejected at load
2780    /// (header check) or degrades to fewer restaged blocks (per-id checks); either way the
2781    /// post-freeze argmax gate still validates the serving assignment.
2782    pub fn save_cpu_expert_residency_profile(
2783        &self,
2784        e: &Engine,
2785        path: &std::path::Path,
2786    ) -> Result<(), Box<dyn std::error::Error>> {
2787        let Some(ids) = e.export_moe_residency() else {
2788            return Err("no MoE residency cache to persist".into());
2789        };
2790        let mut body = format!(
2791            "memra-freeze-profile v1 max_block={} blocks={}\n",
2792            self.max_moe_block(),
2793            ids.len()
2794        );
2795        for (layer, proj, ex) in &ids {
2796            body.push_str(&format!("{layer} {proj} {ex}\n"));
2797        }
2798        let tmp = path.with_extension("tmp");
2799        std::fs::write(&tmp, body)?;
2800        std::fs::rename(&tmp, path)?;
2801        println!(
2802            "[moe-cache] freeze profile saved: {} blocks -> {}",
2803            ids.len(),
2804            path.display()
2805        );
2806        Ok(())
2807    }
2808
2809    /// Restage a saved freeze profile and freeze immediately, skipping the profiling warmup.
2810    /// Returns false (leaving the cache untouched for a normal warmup) when the profile is
2811    /// missing or its header does not match this model's slot geometry.
2812    pub fn restore_cpu_expert_residency_profile(
2813        &self,
2814        e: &Engine,
2815        path: &std::path::Path,
2816    ) -> Result<bool, Box<dyn std::error::Error>> {
2817        use crate::hybrid::Ffn;
2818        use crate::moe_cache::BlockId;
2819        let Ok(content) = std::fs::read_to_string(path) else {
2820            return Ok(false);
2821        };
2822        let mut lines = content.lines();
2823        let Some(header) = lines.next() else { return Ok(false) };
2824        let expected = format!("memra-freeze-profile v1 max_block={}", self.max_moe_block());
2825        if !header.starts_with(&expected) {
2826            println!(
2827                "[moe-cache] freeze profile ignored (geometry mismatch): {}",
2828                path.display()
2829            );
2830            return Ok(false);
2831        }
2832        let mut by_layer: std::collections::HashMap<u16, Vec<BlockId>> =
2833            std::collections::HashMap::new();
2834        for line in lines {
2835            let mut fields = line.split_whitespace();
2836            let (Some(layer), Some(proj), Some(ex)) =
2837                (fields.next(), fields.next(), fields.next())
2838            else {
2839                continue;
2840            };
2841            let (Ok(layer), Ok(proj), Ok(ex)) =
2842                (layer.parse::<u16>(), proj.parse::<u8>(), ex.parse::<u16>())
2843            else {
2844                continue;
2845            };
2846            by_layer
2847                .entry(layer)
2848                .or_default()
2849                .push(BlockId::new(layer, proj, ex));
2850        }
2851        let requested: usize = by_layer.values().map(Vec::len).sum();
2852        if requested == 0 {
2853            return Ok(false);
2854        }
2855        let max_block = self.max_moe_block();
2856        let mut restaged = 0usize;
2857        let mut stage_layer = |layer_index: u16,
2858                               ffn: &Ffn|
2859         -> Result<(), Box<dyn std::error::Error>> {
2860            let Ffn::Moe(m) = ffn else { return Ok(()) };
2861            let Some(ids) = by_layer.get(&layer_index) else {
2862                return Ok(());
2863            };
2864            e.with_moe_cache(max_block, |cache, eng| {
2865                for id in ids {
2866                    if cache.restage_block(*id, m, eng)? {
2867                        restaged += 1;
2868                    }
2869                }
2870                Ok(())
2871            })
2872        };
2873        for (index, layer) in self.layers.iter().enumerate() {
2874            stage_layer(index as u16, &layer.ffn)?;
2875        }
2876        if let Some(mtp) = self.mtp.as_ref() {
2877            stage_layer(u16::MAX, &mtp.ffn)?;
2878        }
2879        e.freeze_moe_cache();
2880        println!(
2881            "[moe-cache] freeze profile restored: {restaged}/{requested} blocks restaged from {}",
2882            path.display()
2883        );
2884        Ok(true)
2885    }
2886
2887    /// Freeze the heterogeneous CPU/GPU split after the caller's discarded profile warmup.
2888    pub fn freeze_cpu_expert_residency(
2889        &self,
2890        e: &Engine,
2891    ) -> Result<(), Box<dyn std::error::Error>> {
2892        e.freeze_moe_cache();
2893        Ok(())
2894    }
2895
2896    /// FFN activation dispatch: swigluoai (clamped, alpha/limit) when cfg.m3 says so, else the
2897    /// standard SiLU*up. One seam so every FFN site (dense, routed expert, shared expert) follows
2898    /// the model's activation exactly.
2899    pub fn ffn_act(e: &Engine, cfg: &ModelConfig, gate: &CudaSlice<f32>, up: &CudaSlice<f32>,
2900               act: &mut CudaSlice<f32>, n: usize) -> Result<(), Box<dyn std::error::Error>> {
2901        Self::ffn_act_scaled(e, cfg, gate, up, 1.0, 1.0, act, n)
2902    }
2903
2904    /// ffn_act with per-tensor post-matmul macro-scales folded in (gs/us == 1.0 -> identical
2905    /// float ops to ffn_act; used by the ModelOpt NVFP4 expert path where each expert tensor
2906    /// carries a `weight_scale_2`).
2907    #[allow(clippy::too_many_arguments)]
2908    pub(crate) fn ffn_act_scaled(e: &Engine, cfg: &ModelConfig, gate: &CudaSlice<f32>, up: &CudaSlice<f32>,
2909               gs: f32, us: f32, act: &mut CudaSlice<f32>, n: usize)
2910               -> Result<(), Box<dyn std::error::Error>> {
2911        if let Some(m3) = cfg.m3.as_ref() {
2912            return e.swigluoai_mul_scaled(gate, up, gs, us, m3.swiglu_alpha, m3.swiglu_limit, act, n);
2913        }
2914        if gs == 1.0 && us == 1.0 { return e.silu_mul(gate, up, act, n); }
2915        e.silu_mul_scaled(gate, up, gs, us, act, n)
2916    }
2917
2918    /// Routing for the whole batch: returns (sel [T*n_used] expert ids, w [T*n_used] renorm weights),
2919    /// token-major. Default = the Stage-1 host path (dtoh logits, softmax-256, stable DESC top-k,
2920    /// renorm). MEMRA_FUSED_ROUTER = the device kernel (§A) which reproduces the same numerics; we
2921    /// still dtoh the tiny [T,n_used] sel/w buffers (64 B/token vs 1 KB/token) — the host loop
2922    /// indexes HostExps.bytes on the CPU to choose the DMA source (§A.2 output staging).
2923    fn moe_route(e: &Engine, logits: &CudaSlice<f32>, t: usize, n_expert: usize, n_used: usize)
2924                 -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
2925        Self::moe_route_cfg(e, logits, t, n_expert, n_used, None, None, None)
2926    }
2927
2928    /// DeepSeek-V3-class sigmoid routing (MiniMax-M3, Hy3), host oracle. Reference:
2929    /// M3/Hy3 modeling code — scores = sigmoid(logits); selection over scores + expert bias
2930    /// (M3 `e_score_correction_bias` / Hy3 `expert_bias`, both surfaced as `exp_probs_b`);
2931    /// weights = un-biased scores of the selected experts, sum-normalized when `route_norm`,
2932    /// x scaling factor (M3 routed_scaling_factor 2.0 / Hy3 router_scaling_factor 2.826).
2933    /// `sig` = (scaling_factor, route_norm) from `cfg.sigmoid_router()`; softmax archs pass
2934    /// None -> the qwen35moe/OLMoE path below.
2935    fn moe_route_cfg(e: &Engine, logits: &CudaSlice<f32>, t: usize, n_expert: usize, n_used: usize,
2936                     bias: Option<&[f32]>, sig: Option<(f32, bool)>, active: Option<&[bool]>)
2937                 -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
2938        if let Some((sf, route_norm)) = sig {
2939            // sigmoid routing. Host path only for now (fused-router kernel is softmax-top-k).
2940            let lg = e.dtoh(logits)?;
2941            return Self::moe_route_sigmoid_host(
2942                &lg, t, n_expert, n_used, bias, sf, route_norm, active,
2943            );
2944        }
2945        // LAUNCH-STRUCTURE STAGE 1 (2026-07-05): fused router DEFAULT ON (MEMRA_FUSED_ROUTER=0
2946        // rollback) via the single-sync pinned readback — softmax arch only; the M3 sigmoid arm
2947        // above returns before this (host path until a sigmoid fused-router kernel exists).
2948        if active.is_none() && !matches!(std::env::var("MEMRA_FUSED_ROUTER").as_deref(), Ok("0")) {
2949            return e.moe_router_topk_host(logits, t, n_expert, n_used);
2950        }
2951        // Host oracle (the §D bit-identity reference).
2952        let lg = e.dtoh(logits)?;   // [T*n_expert] host
2953        let mut sel = vec![0u32; t * n_used];
2954        let mut w_out = vec![0f32; t * n_used];
2955        for tok in 0..t {
2956            let row = &lg[tok * n_expert..(tok + 1) * n_expert];
2957            // softmax over ALL n_expert (stable: subtract max)
2958            let maxl = row.iter().enumerate()
2959                .filter(|(i, _)| active.is_none_or(|mask| mask[*i]))
2960                .map(|(_, &x)| x).fold(f32::NEG_INFINITY, f32::max);
2961            let mut probs = vec![0f32; n_expert];
2962            let mut den = 0f32;
2963            for i in 0..n_expert {
2964                if active.is_some_and(|mask| !mask[i]) { continue; }
2965                let x = (row[i] - maxl).exp(); probs[i] = x; den += x;
2966            }
2967            for p in probs.iter_mut() { *p /= den; }
2968            // stable DESC sort: prob DESC, ascending-index tiebreak.
2969            let mut idx: Vec<usize> = (0..n_expert)
2970                .filter(|&i| active.is_none_or(|mask| mask[i])).collect();
2971            idx.sort_by(|&a, &b| probs[b].total_cmp(&probs[a]).then(a.cmp(&b)));
2972            let sl = &idx[..n_used];
2973            let mut wv: Vec<f32> = sl.iter().map(|&i| probs[i]).collect();
2974            let mut ws: f32 = wv.iter().sum();
2975            ws = ws.max(6.103515625e-5_f32);  // F16 smallest normal, clamp BEFORE divide
2976            for x in wv.iter_mut() { *x /= ws; }
2977            for j in 0..n_used {
2978                sel[tok * n_used + j] = sl[j] as u32;
2979                w_out[tok * n_used + j] = wv[j];
2980            }
2981        }
2982        Ok((sel, w_out))
2983    }
2984
2985    #[allow(clippy::too_many_arguments)]
2986    fn moe_route_sigmoid_with_input(
2987        e: &Engine,
2988        logits: &CudaSlice<f32>,
2989        input: &CudaSlice<f32>,
2990        t: usize,
2991        n_expert: usize,
2992        n_used: usize,
2993        bias: Option<&[f32]>,
2994        (sf, route_norm): (f32, bool),
2995        active: Option<&[bool]>,
2996    ) -> Result<(Vec<u32>, Vec<f32>, Vec<f32>), Box<dyn std::error::Error>> {
2997        let (lg, input) = e.dtoh_pair(logits, input)?;
2998        let (sel, w) =
2999            Self::moe_route_sigmoid_host(&lg, t, n_expert, n_used, bias, sf, route_norm, active)?;
3000        Ok((sel, w, input))
3001    }
3002
3003    /// Start the prediction-guided prefetch worker (MEMRA_MOE_PREFETCH=depth). Call after
3004    /// residency freeze: the worker filters against a static snapshot of the frozen HBM set.
3005    /// Builds a fully-owned per-layer table (host router copies via one-time DtoH, bias,
3006    /// active mask, prebuilt projection descriptors) so no model reference escapes.
3007    pub fn start_moe_prefetch_predictor(
3008        &self,
3009        e: &Engine,
3010        cfg: &ModelConfig,
3011    ) -> Result<(), Box<dyn std::error::Error>> {
3012        use crate::hybrid::Ffn;
3013        let Some(sig) = cfg.sigmoid_router() else {
3014            return Err("prefetch predictor requires a sigmoid-router arch".into());
3015        };
3016        let resident: std::collections::HashSet<(u16, u8, u16)> = e
3017            .export_moe_residency()
3018            .ok_or("prefetch predictor needs the frozen MoE residency cache")?
3019            .into_iter()
3020            .collect();
3021        let mut layers = Vec::new();
3022        for (index, layer) in self.layers.iter().enumerate() {
3023            let Ffn::Moe(m) = &layer.ffn else { continue };
3024            let crate::model::GpuTensor::Float { data, .. } = &m.gate_inp else { continue };
3025            let router = e.dtoh(data)?;
3026            let n_expert = m.gate_exps.n_expert;
3027            let n_embd = m.gate_exps.in_f;
3028            if router.len() != n_embd * n_expert {
3029                continue;
3030            }
3031            let build = |exps: &crate::model::HostExps| {
3032                (0..n_expert)
3033                    .map(|expert| crate::cpu_experts::predictor_projection(exps, expert))
3034                    .collect::<Vec<_>>()
3035            };
3036            layers.push((index as u16, crate::cpu_experts::PredictLayerInit {
3037                router,
3038                bias: m.exp_probs_b.clone(),
3039                active: m.active_experts.clone(),
3040                n_embd,
3041                n_used: cfg
3042                    .moe
3043                    .as_ref()
3044                    .map(|moe| moe.expert_used_count as usize)
3045                    .ok_or("prefetch predictor requires MoE config")?,
3046                sig,
3047                weights_n_expert: n_expert,
3048                gate: build(&m.gate_exps),
3049                up: build(&m.up_exps),
3050                down: build(&m.down_exps),
3051            }));
3052        }
3053        crate::cpu_experts::start_prefetch_predictor(layers, resident)
3054            .map_err(|error| error.into())
3055    }
3056
3057    /// Crate-visible sigmoid-routing oracle for the prefetch predictor: identical selection
3058    /// math to the runtime router, applied to host-computed lookahead logits.
3059    #[allow(clippy::too_many_arguments)]
3060    pub(crate) fn moe_route_sigmoid_host_public(
3061        logits: &[f32],
3062        t: usize,
3063        n_expert: usize,
3064        n_used: usize,
3065        bias: Option<&[f32]>,
3066        sf: f32,
3067        route_norm: bool,
3068        active: Option<&[bool]>,
3069    ) -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
3070        Self::moe_route_sigmoid_host(logits, t, n_expert, n_used, bias, sf, route_norm, active)
3071    }
3072
3073    #[allow(clippy::too_many_arguments)]
3074    fn moe_route_sigmoid_host(
3075        lg: &[f32],
3076        t: usize,
3077        n_expert: usize,
3078        n_used: usize,
3079        bias: Option<&[f32]>,
3080        sf: f32,
3081        route_norm: bool,
3082        active: Option<&[bool]>,
3083    ) -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
3084        if lg.len() != t * n_expert {
3085            return Err(format!(
3086                "sigmoid router logits length mismatch: got {}, expected {}",
3087                lg.len(),
3088                t * n_expert,
3089            )
3090            .into());
3091        }
3092        let mut sel = vec![0u32; t * n_used];
3093        let mut w_out = vec![0f32; t * n_used];
3094        for tok in 0..t {
3095            let row = &lg[tok * n_expert..(tok + 1) * n_expert];
3096            let scores: Vec<f32> = row.iter().map(|&x| 1.0 / (1.0 + (-x).exp())).collect();
3097            // selection score = sigmoid + bias; weight = plain sigmoid.
3098            let selsc: Vec<f32> = match bias {
3099                Some(b) => scores.iter().zip(b).map(|(s, bb)| s + bb).collect(),
3100                None => scores.clone(),
3101            };
3102            let mut idx: Vec<usize> = (0..n_expert)
3103                .filter(|&i| active.is_none_or(|mask| mask[i]))
3104                .collect();
3105            idx.sort_by(|&a, &b| selsc[b].total_cmp(&selsc[a]).then(a.cmp(&b)));
3106            let sl = &idx[..n_used];
3107            let mut wv: Vec<f32> = sl.iter().map(|&i| scores[i]).collect();
3108            if route_norm {
3109                let ws: f32 = wv.iter().sum::<f32>().max(1e-20);
3110                for x in wv.iter_mut() {
3111                    *x = *x / ws * sf;
3112                }
3113            } else {
3114                for x in wv.iter_mut() {
3115                    *x *= sf;
3116                }
3117            }
3118            for j in 0..n_used {
3119                sel[tok * n_used + j] = sl[j] as u32;
3120                w_out[tok * n_used + j] = wv[j];
3121            }
3122        }
3123        Ok((sel, w_out))
3124    }
3125
3126    /// LAUNCH-STRUCTURE STAGE 3: the ZERO-DtoH fully-resident MoE FFN. Caller guarantees the
3127    /// layer's device pointer row exists (checked under the cache lock). Router top-k runs on
3128    /// device; sel/w are consumed by the `_dev` matvec twins directly; NOTHING crosses PCIe.
3129    /// Same numerics as the fused-router + gdec chain (kernel-level bit-identity, see the
3130    /// MoE PREFILL PAIR-BATCH: host routing (sel/w like the sequential path), then 5 launches
3131    /// TOTAL per layer (quantize z, gate-pairs, up-pairs, silu, act-quantize, down-pairs,
3132    /// scatter) regardless of T or expert count. Bit-identity class: per (pair,row) dot =
3133    /// qmatvec_expert_q8 order; per-token accumulation slot-ordered (scatter kernel).
3134    fn moe_ffn_pairs(e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>, logits: &CudaSlice<f32>,
3135                     t: usize, cfg: &ModelConfig)
3136                     -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3137        let moe = cfg.moe.as_ref().unwrap();
3138        let n_embd = cfg.n_embd as usize;
3139        let n_expert = moe.expert_count as usize;
3140        let n_used = moe.expert_used_count as usize;
3141        let n_ff_exp = moe.expert_ff_length as usize;
3142        let dev = m.dev_exps.as_ref().unwrap();
3143        // WALL-GAP ARC: interleaved gate/up slab strides (see moe_ffn_dev).
3144        let (rbg_d, rbu_d) = if dev.gu_il {
3145            let sxx = m.gate_exps.row_bytes + m.up_exps.row_bytes; (sxx, sxx)
3146        } else { (m.gate_exps.row_bytes, m.up_exps.row_bytes) };
3147
3148        let (sel_all, w_all) = Self::moe_route(e, logits, t, n_expert, n_used)?;
3149        let n_pairs = t * n_used;
3150        // pair arrays: pair p = (token p/n_used, slot p%n_used) — ALREADY slot-ordered per token,
3151        // so the CSR is trivial: tok_pair_off[tok] = tok*n_used, ids identity.
3152        let pair_tok: Vec<i32> = (0..n_pairs).map(|p| (p / n_used) as i32).collect();
3153        let pair_ex:  Vec<i32> = sel_all.iter().map(|&x| x as i32).collect();
3154        let pair_w:   Vec<f32> = w_all.clone();
3155        let tok_off:  Vec<i32> = (0..=t).map(|tok| (tok * n_used) as i32).collect();
3156        let tok_ids:  Vec<i32> = (0..n_pairs as i32).collect();
3157        let pt = e.htod_i32(&pair_tok)?;
3158        let px = e.htod_i32(&pair_ex)?;
3159        let pw = e.htod(&pair_w)?;
3160        let toff = e.htod_i32(&tok_off)?;
3161        let tids = e.htod_i32(&tok_ids)?;
3162
3163        // z quantized ONCE for all tokens; gate/up pair matvecs; silu; act quantize; down; scatter.
3164        // EXPERT-MAJOR CSR (rung 2): pairs grouped by expert -> the kernel reuses each weight
3165        // row across the expert's token group (llama-MMQ's core win). Host grouping is O(pairs).
3166        let mut by_ex: Vec<Vec<i32>> = vec![Vec::new(); n_expert];
3167        for p in 0..n_pairs { by_ex[pair_ex[p] as usize].push(p as i32); }
3168        let mut ex_ids: Vec<i32> = Vec::new();
3169        let mut ex_off: Vec<i32> = vec![0];
3170        let mut ex_pairs: Vec<i32> = Vec::with_capacity(n_pairs);
3171        for (ex, list) in by_ex.iter().enumerate() {
3172            if list.is_empty() { continue; }
3173            ex_ids.push(ex as i32);
3174            ex_pairs.extend_from_slice(list);
3175            ex_off.push(ex_pairs.len() as i32);
3176        }
3177        let n_active = ex_ids.len();
3178        let exi = e.htod_i32(&ex_ids)?;
3179        let exo = e.htod_i32(&ex_off)?;
3180        let exp_d = e.htod_i32(&ex_pairs)?;
3181        let _ = &px;   // pair-major twin keeps it; em path uses CSR
3182
3183        // INT8-MMA EXPERT MMQ (MEMRA_MOE_MMA=1, opt-in): the m16n8k16.s8 tensor-core analog of the
3184        // _dec dp4a kernel (cu/mmq_iq_experts.cu). Same CSR grouping; per-expert matvec runs as a
3185        // 128x128-tile int8 MMA GEMM over the expert's token group. Weight IQ nibbles decode to int8
3186        // at tile-load + per-32 float scale; activation is q8_1_mmq (D4, same quant class as dp4a).
3187        // FP-ORDER differs from dp4a (MMA reduction) — logits SHIFT, gated on argmax/spec/closeness,
3188        // NOT byte-identity (like the W4A8 path). Requires IQ3_S/IQ4_XS + in_f % 256 == 0.
3189        // t >= 16 (GEMM_M-class rule): the MMA tile needs token volume (crossover ~200 tok/expert;
3190        // microbench: dp4a wins at tiny groups). ALSO an exactness requirement — spec verify
3191        // batches (t=2..K+2) must ride the dp4a path whose FP order matches the T=1 decode chain,
3192        // else K=1 self-consistency FAILs (caught 2026-07-06: MMA at T=2 flipped a verify argmax).
3193        // DEFAULT ON (2026-07-06, third flip — this time with the real culprit fixed): the
3194        // "MMA prime breaks spec" failure was the ROUTER's cuBLASLt n-dependence (d994271),
3195        // not MMA's own FP order — both this and the k-quant arms were innocent suspects whose
3196        // margin shifts surfaced the router bug. With the router decode-exact at verify t, the
3197        // full battery is green with MMA on (spec p1/p2/p3 PASS, raw K=1..8 PASS, argmax MATCH,
3198        // pp6257 2862 = 2.1x dec). t>=16 floor still required: verify batches must ride dp4a
3199        // (dispatch parity with the T=1 decode chain). MEMRA_MOE_MMA=0 rollback;
3200        // MEMRA_MOE_MMA_T overrides the floor (bisect seam).
3201        static MMA_T: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
3202        let mma_t = *MMA_T.get_or_init(|| {
3203            std::env::var("MEMRA_MOE_MMA_T").ok().and_then(|v| v.parse().ok()).unwrap_or(16)
3204        });
3205        let use_mma = std::env::var("MEMRA_MOE_MMA").map(|v| v != "0").unwrap_or(true)
3206            && t >= mma_t
3207            && q8_expert_dec_supported(m.gate_exps.qtype) && q8_expert_dec_supported(m.up_exps.qtype)
3208            && q8_expert_dec_supported(m.down_exps.qtype)
3209            && n_embd % 256 == 0 && n_ff_exp % 256 == 0;
3210        // GROUPED f16 LANE admission (MEMRA_MOE_F16G, rounds 46-49): its own door, no longer a
3211        // subset of the MMQ arm — q35's k-quant stragglers (Q6_K/Q4_K down, one Q3_K gate/up
3212        // layer) fail q8_expert_dec_supported but dequant fine to f16, so f16g must be able to
3213        // take a layer the MMQ arm would reject. Same t >= mma_t floor as MMA: decode and
3214        // spec-verify batches must ride the dp4a path whose FP order matches the T=1 chain.
3215        // MODE 2 (sm_120a naked default, lane/f16g-default-rearb 2026-08-02): every layer
3216        // whose three projections pass f16g_proj_ok rides the sk visitor with direct tile
3217        // loaders — with IQ4_XS/IQ3_S direct coverage the sk arm beats the int8-MMA MMQ
3218        // tiles on the IQ-bank models too (q35 board-2048 +33.9%, KAT pp512 +46.7%).
3219        // AUTO-KQUANT (mode 3, MEMRA_MOE_F16G=3, lane/q4k-expert-prefill): admit f16g ONLY
3220        // where the MMA arm can't take the layer's QTYPES — the k-quant expert class whose
3221        // baseline is the per-pair _em fallback (Ornith-35B Q4_K: board-2048 3.14x). Its
3222        // MMA-capable carve-out (IQ3_S/IQ4_XS/Q4_0 banks on MMQ) was priced pre-IQ-direct;
3223        // it survives as the rollback seam. Keyed on qtype capability, NOT use_mma, so
3224        // MEMRA_MOE_MMA=0 stays a pure dp4a rollback seam.
3225        let mma_capable = q8_expert_dec_supported(m.gate_exps.qtype)
3226            && q8_expert_dec_supported(m.up_exps.qtype)
3227            && q8_expert_dec_supported(m.down_exps.qtype)
3228            && n_embd % 256 == 0 && n_ff_exp % 256 == 0;
3229        let f16g_mode = crate::moe_f16g_mode();
3230        let f16g = f16g_mode != 0 && t >= mma_t
3231            && (f16g_mode != 3 || !mma_capable)
3232            && f16g_proj_ok(m.gate_exps.qtype, n_embd)
3233            && f16g_proj_ok(m.up_exps.qtype, n_embd)
3234            && f16g_proj_ok(m.down_exps.qtype, n_ff_exp);
3235        if use_mma || f16g {
3236            // GROUPED f16 LANE (MEMRA_MOE_F16G, rounds 46-49, experimental door): dequant
3237            // the active experts ONCE per projection to f16 and run one grouped f16 GEMM over
3238            // the CSR groups. f16-mirror numeric class — argmax/spec gated before promotion.
3239            // NOTE: the pair-gather kernel needs pair p ordered EXPERT-MAJOR (ex_pairs order);
3240            // the y rows come back in that same CSR order, so gate/up/down all stay pair-major
3241            // in ex_pairs order — but moe_pairs_silu_mul and the scatter consume PAIR-ID order.
3242            // We therefore gather activations per ex_pairs and scatter y back through ex_pairs.
3243            let y_down = if f16g {
3244                // CSR order end-to-end: gather z rows by the pair's TOKEN (pair p's token is
3245                // p / n_used — the trivial CSR above), silu in CSR order (elementwise), one
3246                // permute at the very end back to pair-id order for the scatter.
3247                let csr_tok: Vec<i32> = ex_pairs.iter().map(|&p| p / n_used as i32).collect();
3248                let csr_tok_d = e.htod_i32(&csr_tok)?;
3249                let (z_f16, z_s) = e.moe_f16g_act(z, Some(&csr_tok_d), n_embd, n_pairs)?;
3250                let g_csr = e.moe_f16_grouped(&dev.ptr_row, 0, n_expert, &exi, &ex_off, &exo,
3251                                              &z_f16, &z_s, n_embd, n_ff_exp, n_active, n_pairs,
3252                                              m.gate_exps.qtype, rbg_d)?;
3253                let u_csr = e.moe_f16_grouped(&dev.ptr_row, 1, n_expert, &exi, &ex_off, &exo,
3254                                              &z_f16, &z_s, n_embd, n_ff_exp, n_active, n_pairs,
3255                                              m.up_exps.qtype, rbu_d)?;
3256                let act_csr = e.moe_pairs_silu_mul(&g_csr, &u_csr, n_pairs * n_ff_exp)?;
3257                let (a_f16, a_s) = e.moe_f16g_act(&act_csr, None, n_ff_exp, n_pairs)?;
3258                let d_csr = e.moe_f16_grouped(&dev.ptr_row, 2, n_expert, &exi, &ex_off, &exo,
3259                                              &a_f16, &a_s, n_ff_exp, n_embd, n_active, n_pairs,
3260                                              m.down_exps.qtype, m.down_exps.row_bytes)?;
3261                e.rows_permute(&d_csr, &exp_d, n_pairs, n_embd)?
3262            } else {
3263            // gate/up: activation = z, token-major over t tokens; pair_tok gathers the routed row.
3264            let z_scr = e.mmq_iq_quantize_act(z, n_embd, t)?;
3265            let gate = e.mmq_iq_experts(&dev.ptr_row, 0, n_expert, &exi, &exo, &exp_d, &pt, &z_scr,
3266                                        n_embd, n_ff_exp, n_active, n_pairs, t,
3267                                        m.gate_exps.qtype, rbg_d)?;
3268            let up = e.mmq_iq_experts(&dev.ptr_row, 1, n_expert, &exi, &exo, &exp_d, &pt, &z_scr,
3269                                      n_embd, n_ff_exp, n_active, n_pairs, t,
3270                                      m.up_exps.qtype, rbu_d)?;
3271            // down: activation = silu(gate)*up, pair-major [n_pairs, n_ff_exp]; pair_tok =
3272            // identity. FUSED ACT-EPILOGUE (default on): one launch computes the activation in
3273            // registers and writes ONLY the quantized scratch — the two-pass chain
3274            // (moe_pairs_silu_mul writes act f32, mmq_iq_quantize_act re-reads it) is the
3275            // MEMRA_MOE_FUSE_ACTQ=0 rollback. Scratch bytes are BYTE-IDENTICAL (kernel-check).
3276            let a_scr = if crate::moe_fuse_actq_on() {
3277                e.mmq_iq_fused_act_quant(&gate, &up, n_ff_exp, n_pairs, 0)?
3278            } else {
3279                let act = e.moe_pairs_silu_mul(&gate, &up, n_pairs * n_ff_exp)?;
3280                e.mmq_iq_quantize_act(&act, n_ff_exp, n_pairs)?
3281            };
3282            let pair_self: Vec<i32> = (0..n_pairs as i32).collect();
3283            let pself = e.htod_i32(&pair_self)?;
3284            e.mmq_iq_experts(&dev.ptr_row, 2, n_expert, &exi, &exo, &exp_d, &pself, &a_scr,
3285                             n_ff_exp, n_embd, n_active, n_pairs, n_pairs,
3286                             m.down_exps.qtype, m.down_exps.row_bytes)?
3287            };
3288            let mut moe_out = e.uninit(t * n_embd)?;
3289            e.moe_pairs_scatter(&y_down, &pw, &toff, &tids, &mut moe_out, t, n_embd)?;
3290            if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
3291                (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
3292            {
3293                let n_ff_sh = gate_shexp.out_features();
3294                let sg_gate = e.matmul(gate_shexp, z, t)?;
3295                let sg_up = e.matmul(up_shexp, z, t)?;
3296                let mut sa = e.uninit(t * n_ff_sh)?;
3297                Self::ffn_act(e, cfg, &sg_gate, &sg_up, &mut sa, t * n_ff_sh)?;
3298                let sh = e.matmul(down_shexp, &sa, t)?;
3299                // shexp gate: qwen35moe sigmoid-gates; M3 has no gate tensor -> weight 1.0.
3300                // SERVE ISOLATION (lane/concat-prime-exact): the fused sigmoid-dot is the
3301                // m-INVARIANT form — see the sequential arm's note. This is the PAIRS arm,
3302                // i.e. the one real prefill actually takes on a resident-expert MoE model,
3303                // so the concat-prime isolation fix has to land here as well.
3304                let g = match &m.gate_inp_shexp {
3305                    Some(gate_inp_shexp) if crate::router_prefill_exact_on() => {
3306                        e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
3307                    }
3308                    Some(gate_inp_shexp) => {
3309                        let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
3310                        let mut g = e.uninit(t)?;
3311                        e.sigmoid(&gs, &mut g, t)?;
3312                        g
3313                    }
3314                    None => e.htod(&vec![1.0f32; t])?,
3315                };
3316                e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
3317            }
3318            return Ok(moe_out);
3319        }
3320
3321        // DECODE-ONCE MMQ (rung 3, MEMRA_MOE_DEC=1 default-on): dequant each weight group once per
3322        // (row,group) then dp4a across the expert's tokens. _em re-decoded per token (NEUTRAL).
3323        let dec = std::env::var("MEMRA_MOE_DEC").map(|v| v != "0").unwrap_or(true);
3324        let matvec = |proj, exi: &_, exo: &_, exp_d: &_, pt: &_, aq: &_, ad: &_,
3325                      inf, outf, qtype, rb| -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3326            // _dec's decode-once extractors are IQ-only; k-quant expert layers take the _em dot path.
3327            let dec = dec && q8_expert_dec_supported(qtype);
3328            if dec { e.moe_pairs_matvec_q8_dec(&dev.ptr_row, proj, exi, exo, exp_d, pt, aq, ad,
3329                                               inf, outf, n_expert, n_active, n_pairs, qtype, rb) }
3330            else   { e.moe_pairs_matvec_q8_em (&dev.ptr_row, proj, exi, exo, exp_d, pt, aq, ad,
3331                                               inf, outf, n_expert, n_active, n_pairs, qtype, rb) }
3332        };
3333        let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
3334        let gate = matvec(0, &exi, &exo, &exp_d, &pt, &zq, &zd,
3335                          n_embd, n_ff_exp, m.gate_exps.qtype, rbg_d)?;
3336        let up = matvec(1, &exi, &exo, &exp_d, &pt, &zq, &zd,
3337                        n_embd, n_ff_exp, m.up_exps.qtype, rbu_d)?;
3338        let act = e.moe_pairs_silu_mul(&gate, &up, n_pairs * n_ff_exp)?;
3339        let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
3340        // down consumes PAIR-major activation rows: pair_tok = identity.
3341        let pair_self: Vec<i32> = (0..n_pairs as i32).collect();
3342        let pself = e.htod_i32(&pair_self)?;
3343        let y_down = matvec(2, &exi, &exo, &exp_d, &pself, &aq2, &ad2,
3344                            n_ff_exp, n_embd, m.down_exps.qtype, m.down_exps.row_bytes)?;
3345        let mut moe_out = e.uninit(t * n_embd)?;   // scatter fully overwrites per (token,col)
3346        e.moe_pairs_scatter(&y_down, &pw, &toff, &tids, &mut moe_out, t, n_embd)?;
3347
3348        // SHARED EXPERT epilogue — same as the other paths.
3349        // gate_inp_shexp is OPTIONAL: qwen35moe gates the shared expert (sigmoid(gate_inp) x sh);
3350        // MiniMax-M3 (DeepSeek-V3 class) has NO shexp gate — the shared expert adds directly.
3351        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
3352            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
3353        {
3354            let n_ff_sh = gate_shexp.out_features();
3355            let sg_gate = e.matmul(gate_shexp, z, t)?;
3356            let sg_up = e.matmul(up_shexp, z, t)?;
3357            let mut sa = e.uninit(t * n_ff_sh)?;
3358            e.silu_mul(&sg_gate, &sg_up, &mut sa, t * n_ff_sh)?;
3359            let sh = e.matmul(down_shexp, &sa, t)?;
3360            // shexp gate: qwen35moe sigmoid-gates; M3 has no gate tensor -> weight 1.0.
3361            // m-INVARIANT fused sigmoid-dot under router_prefill_exact (serve isolation,
3362            // lane/concat-prime-exact) — every shexp-gate arm shares the same form so a
3363            // dispatch choice cannot change bits.
3364            let g = match &m.gate_inp_shexp {
3365                Some(gate_inp_shexp) if crate::router_prefill_exact_on() => {
3366                    e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
3367                }
3368                Some(gate_inp_shexp) => {
3369                    let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
3370                    let mut g = e.uninit(t)?;
3371                    e.sigmoid(&gs, &mut g, t)?;
3372                    g
3373                }
3374                None => e.htod(&vec![1.0f32; t])?,
3375            };
3376            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
3377        }
3378        Ok(moe_out)
3379    }
3380
3381    /// kernel headers); the shared-expert epilogue is byte-identical to moe_ffn_sequential's.
3382    #[allow(clippy::too_many_arguments)]
3383    #[allow(clippy::too_many_arguments)]
3384    fn moe_ffn_dev(e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>,
3385                   zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>, logits: &CudaSlice<f32>,
3386                   t: usize, cfg: &ModelConfig, il: u16, max_block: usize)
3387                   -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3388        let moe = cfg.moe.as_ref().unwrap();
3389        let n_embd = cfg.n_embd as usize;
3390        let n_expert = moe.expert_count as usize;
3391        let n_used = moe.expert_used_count as usize;
3392        let n_ff_exp = moe.expert_ff_length as usize;
3393
3394        // device top-k: sel [t, n_used] i32, w [t, n_used] f32 — stays on device.
3395        let (sel_d, mut w_d) = e.moe_router_topk(logits, t, n_expert, n_used)?;
3396        // Down-projection macro fold (compressed-tensors NVFP4 artifacts): one tiny launch,
3397        // skipped entirely for macro-free experts (every k-quant GGUF).
3398        if m.has_macros {
3399            e.moe_w_scale_by_expert(&mut w_d, &sel_d, &m.dev_macros, n_expert, t * n_used)?;
3400        }
3401
3402        // moe_out rows are FULLY overwritten by moe_down8_fma_dev — uninit (stage-2 rule).
3403        let mut moe_out = e.uninit(t * n_embd)?;
3404
3405        // RESIDENT-EXPERTS arm: the pointer row comes from the load-time slab (no cache, no
3406        // lock). Same kernels/loop as the SLRU arm below — only the row's provenance differs.
3407        if let Some(dev) = m.dev_exps.as_ref() {
3408            // WALL-GAP ARC: interleaved gate/up slab (MEMRA_MOE_GU_IL) -> both projections use
3409            // the combined stride; up's base is offset in the ptr table. Down unchanged.
3410            let (rbg_d, rbu_d) = if dev.gu_il {
3411                let sxx = m.gate_exps.row_bytes + m.up_exps.row_bytes; (sxx, sxx)
3412            } else { (m.gate_exps.row_bytes, m.up_exps.row_bytes) };
3413            let q8 = moe_q8_enabled()
3414                && q8_expert_supported(m.gate_exps.qtype) && q8_expert_supported(m.up_exps.qtype)
3415                && q8_expert_supported(m.down_exps.qtype);
3416            // SMALL-M ROWS ARM (MEMRA_SPEC_M2, lane/spec-m2): batch the verify token loop —
3417            // ONE batched z-quantize + ONE gate_up rows launch + ONE act quantize + ONE down
3418            // rows launch (4 launches/layer, was 4t). BIT-IDENTICAL per token to the serial
3419            // loop below (rows twins = the _v/w8h2v per-token programs on a grid.z token axis;
3420            // quantize_q8_1 is per-32-block row-independent). Gated to the AUTO kernel modes —
3421            // a custom MEMRA_MOE_DEVQ8_GU/DOWN diagnostic run keeps the serial loop so the
3422            // dispatched kernel stays exactly the env-selected one — and to the w8h2v shape
3423            // (n_ff_exp==512, n_used<=8), the same contract the AUTO down dispatch keys on.
3424            let rows_arm = q8 && t > 1 && crate::spec::spec_m2()
3425                && n_ff_exp == 512 && n_used <= 8
3426                && std::env::var("MEMRA_MOE_DEVQ8_GU").map(|v| v.is_empty() || v == "v").unwrap_or(true)
3427                && std::env::var("MEMRA_MOE_DEVQ8_DOWN").map(|v| v.is_empty() || v == "w8h2v").unwrap_or(true);
3428            // CSR EXPERT-DEDUP gate_up (verify-cost target #1, DEFAULT ON 2026-07-10): the
3429            // owner-scan kernel serves every (token, slot) pair of an expert from ONE block,
3430            // deduping the 38-40% duplicated weight-stream+decode the overlap probe measured
3431            // (gate_up 55.0 -> 39.7us/launch; +1.3-2.1% spec e2e p2, +0.6-1.7% p3, all K).
3432            // Bit-identical to the _rows twins (explicit-intrinsic accumulate — the ULP/fmad
3433            // lesson; =2 byte-compare verified zero diffs). down stays on _rows (CSR down
3434            // measured 23.5 -> 37.5us: 16-group rows can't amortize the serial pair loop).
3435            // MEMRA_MOE_CSR=0 rollback; =2 runs BOTH paths and byte-compares (debug).
3436            let csr_mode = std::env::var("MEMRA_MOE_CSR").ok()
3437                .and_then(|v| v.parse::<i32>().ok()).unwrap_or(1);
3438            let csr_qt = |qt: i32| qt == crate::QT_IQ4_XS || qt == crate::QT_IQ3_S;
3439            let csr_arm = rows_arm && csr_mode > 0 && t <= 10
3440                && csr_qt(m.gate_exps.qtype) && csr_qt(m.up_exps.qtype)
3441                && csr_qt(m.down_exps.qtype);
3442            if csr_arm {
3443                if csr_mode == 2 {
3444                    static ENGAGED: std::sync::Once = std::sync::Once::new();
3445                    ENGAGED.call_once(|| eprintln!("[memra] moe CSR byte-compare mode ON (t={t})"));
3446                }
3447                let n_pairs = t * n_used;
3448                let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
3449                let act = e.moe_gate_up_silu8_dev_q8_csr(&dev.ptr_row, &sel_d, &zq, &zd, n_pairs,
3450                                                         n_embd, n_ff_exp, n_used, n_expert,
3451                                                         m.gate_exps.qtype, m.up_exps.qtype,
3452                                                         rbg_d, rbu_d)?;
3453                let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
3454                // down stays on the _rows twin — BOTH CSR down variants measured negative
3455                // (v1 serial pairs 23.5->37.5us; v2 warp-parallel+SMEM -14% e2e, K=8 -37%):
3456                // 16-group rows have too little decode to amortize any dedup structure.
3457                e.moe_down8_fma_dev_q8_rows(&dev.ptr_row, &sel_d, &w_d, &aq2, &ad2, &mut moe_out,
3458                                            t, n_ff_exp, n_embd, n_used, n_expert,
3459                                            m.down_exps.qtype, m.down_exps.row_bytes)?;
3460                if csr_mode == 2 {
3461                    // DEBUG BYTE-COMPARE: run the _rows twins on the same inputs, diff bits.
3462                    let act_r = e.moe_gate_up_silu8_dev_q8_rows(&dev.ptr_row, &sel_d, &zq, &zd, t,
3463                                                                n_embd, n_ff_exp, n_used, n_expert,
3464                                                                m.gate_exps.qtype, m.up_exps.qtype,
3465                                                                rbg_d, rbu_d, &m.dev_macros)?;
3466                    let mut out_r = e.uninit(t * n_embd)?;
3467                    let (aq2r, ad2r) = e.quantize_q8_1(&act_r, n_pairs, n_ff_exp)?;
3468                    e.moe_down8_fma_dev_q8_rows(&dev.ptr_row, &sel_d, &w_d, &aq2r, &ad2r, &mut out_r,
3469                                                t, n_ff_exp, n_embd, n_used, n_expert,
3470                                                m.down_exps.qtype, m.down_exps.row_bytes)?;
3471                    let (a1, a2) = (e.dtoh(&act)?, e.dtoh(&act_r)?);
3472                    let (o1, o2) = (e.dtoh(&moe_out)?, e.dtoh(&out_r)?);
3473                    let ba = a1.iter().zip(&a2).filter(|(x, y)| x.to_bits() != y.to_bits()).count();
3474                    let bo = o1.iter().zip(&o2).filter(|(x, y)| x.to_bits() != y.to_bits()).count();
3475                    if ba + bo > 0 {
3476                        eprintln!("[csr-check] il={il} t={t} ACT diffs={ba}/{} OUT diffs={bo}/{}",
3477                                  a1.len(), o1.len());
3478                        // First 4 differing ACT elements: (pair, o, csr, rows) + that pair's expert
3479                        let sel_h = e.dtoh_i32(&sel_d)?;
3480                        let mut shown = 0;
3481                        for (i, (x, y)) in a1.iter().zip(&a2).enumerate() {
3482                            if x.to_bits() != y.to_bits() && shown < 4 {
3483                                let (p, o) = (i / n_ff_exp, i % n_ff_exp);
3484                                let ex = sel_h[p];
3485                                let npx = sel_h.iter().filter(|&&v| v == ex).count();
3486                                eprintln!("  ACT p={p} ex={ex} np={npx} o={o} csr={x:e} rows={y:e}");
3487                                shown += 1;
3488                            }
3489                        }
3490                        std::process::exit(3);
3491                    }
3492                }
3493            } else if rows_arm {
3494                // TEMP PROBE (MEMRA_MOE_OVERLAP=1): cross-token expert-activation overlap at
3495                // verify — sizes the CSR dedup win (unique experts vs t*n_used pairs).
3496                if std::env::var("MEMRA_MOE_OVERLAP").as_deref() == Ok("1") {
3497                    use std::sync::atomic::{AtomicU64, Ordering};
3498                    static PAIRS: AtomicU64 = AtomicU64::new(0);
3499                    static UNIQ: AtomicU64 = AtomicU64::new(0);
3500                    static CALLS: AtomicU64 = AtomicU64::new(0);
3501                    let sel_h = e.dtoh_i32(&sel_d)?;
3502                    let mut u: Vec<i32> = sel_h.clone(); u.sort_unstable(); u.dedup();
3503                    PAIRS.fetch_add(sel_h.len() as u64, Ordering::Relaxed);
3504                    UNIQ.fetch_add(u.len() as u64, Ordering::Relaxed);
3505                    let c = CALLS.fetch_add(1, Ordering::Relaxed) + 1;
3506                    if c % 480 == 0 {
3507                        let p = PAIRS.load(Ordering::Relaxed); let q = UNIQ.load(Ordering::Relaxed);
3508                        eprintln!("[overlap] calls={c} pairs={p} unique={q} ratio={:.3} (t={t})",
3509                                  q as f64 / p as f64);
3510                    }
3511                }
3512                let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
3513                let act = e.moe_gate_up_silu8_dev_q8_rows(&dev.ptr_row, &sel_d, &zq, &zd, t,
3514                                                          n_embd, n_ff_exp, n_used, n_expert,
3515                                                          m.gate_exps.qtype, m.up_exps.qtype,
3516                                                          rbg_d, rbu_d, &m.dev_macros)?;
3517                let (aq2, ad2) = e.quantize_q8_1(&act, t * n_used, n_ff_exp)?;
3518                e.moe_down8_fma_dev_q8_rows(&dev.ptr_row, &sel_d, &w_d, &aq2, &ad2, &mut moe_out,
3519                                            t, n_ff_exp, n_embd, n_used, n_expert,
3520                                            m.down_exps.qtype, m.down_exps.row_bytes)?;
3521            } else {
3522            for tok in 0..t {
3523                let zt = z.slice(tok * n_embd..(tok + 1) * n_embd);
3524                let selt = sel_d.slice(tok * n_used..(tok + 1) * n_used);
3525                let wt = w_d.slice(tok * n_used..(tok + 1) * n_used);
3526                let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
3527                if q8 {
3528                    let (zq, zd) = match (t, zq8) {
3529                        (1, Some((q, d))) => (q.clone(), d.clone()),
3530                        _ => e.quantize_q8_1_view(&zt, 1, n_embd)?,
3531                    };
3532                    let act = e.moe_gate_up_silu8_dev_q8(&dev.ptr_row, &selt, &zq, &zd,
3533                                                         n_embd, n_ff_exp, n_used, n_expert,
3534                                                         m.gate_exps.qtype, m.up_exps.qtype,
3535                                                         rbg_d, rbu_d, &m.dev_macros)?;
3536                    let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
3537                    e.moe_down8_fma_dev_q8(&dev.ptr_row, &selt, &wt, &aq2, &ad2, &mut dst,
3538                                           n_ff_exp, n_embd, n_used, n_expert,
3539                                           m.down_exps.qtype, m.down_exps.row_bytes)?;
3540                } else {
3541                    let act = e.moe_gate_up_silu8_dev(&dev.ptr_row, &selt, &zt, n_embd, n_ff_exp,
3542                                                      n_used, n_expert,
3543                                                      m.gate_exps.qtype, m.up_exps.qtype,
3544                                                      rbg_d, rbu_d, &m.dev_macros)?;
3545                    e.moe_down8_fma_dev(&dev.ptr_row, &selt, &wt, &act, &mut dst,
3546                                        n_ff_exp, n_embd, n_used, n_expert,
3547                                        m.down_exps.qtype, m.down_exps.row_bytes)?;
3548                }
3549            }
3550            }
3551        } else {
3552        // Launch under the cache lock: the row borrow lives as long as the closure, and the
3553        // lock covers only launch ISSUE (µs), same policy as moe_cached_gemm.
3554        // Q8 ARM PARITY (2026-07-06): the SLRU arm ran the f32-dequant _dev kernels only —
3555        // 80us/launch vs the q8 twins' 15us on the SAME shapes (fixed-build profile: 228
3556        // f32 launches = 36ms of the 64-tok window). Same q8 gate + kernels as the resident
3557        // arm above; MEMRA_MOE_Q8=0 restores the byte-identical f32 path.
3558        let q8 = moe_q8_enabled()
3559            && q8_expert_supported(m.gate_exps.qtype) && q8_expert_supported(m.up_exps.qtype)
3560            && q8_expert_supported(m.down_exps.qtype);
3561        e.with_moe_cache(max_block, |c, eng| {
3562            let row = c.layer_dev_row(il, n_expert, eng)?
3563                .ok_or("moe_ffn_dev: layer row vanished under the lock")?;
3564            for tok in 0..t {
3565                let zt = z.slice(tok * n_embd..(tok + 1) * n_embd);
3566                let selt = sel_d.slice(tok * n_used..(tok + 1) * n_used);
3567                let wt = w_d.slice(tok * n_used..(tok + 1) * n_used);
3568                let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
3569                if q8 {
3570                    let (zq, zd) = match (t, zq8) {
3571                        (1, Some((q, d))) => (q.clone(), d.clone()),
3572                        _ => eng.quantize_q8_1_view(&zt, 1, n_embd)?,
3573                    };
3574                    let act = eng.moe_gate_up_silu8_dev_q8(row, &selt, &zq, &zd,
3575                                                           n_embd, n_ff_exp, n_used, n_expert,
3576                                                           m.gate_exps.qtype, m.up_exps.qtype,
3577                                                           m.gate_exps.row_bytes, m.up_exps.row_bytes,
3578                                                           &m.dev_macros)?;
3579                    let (aq2, ad2) = eng.quantize_q8_1(&act, n_used, n_ff_exp)?;
3580                    eng.moe_down8_fma_dev_q8(row, &selt, &wt, &aq2, &ad2, &mut dst,
3581                                             n_ff_exp, n_embd, n_used, n_expert,
3582                                             m.down_exps.qtype, m.down_exps.row_bytes)?;
3583                } else {
3584                    let act = eng.moe_gate_up_silu8_dev(row, &selt, &zt, n_embd, n_ff_exp,
3585                                                        n_used, n_expert,
3586                                                        m.gate_exps.qtype, m.up_exps.qtype,
3587                                                        m.gate_exps.row_bytes, m.up_exps.row_bytes,
3588                                                        &m.dev_macros)?;
3589                    eng.moe_down8_fma_dev(row, &selt, &wt, &act, &mut dst,
3590                                          n_ff_exp, n_embd, n_used, n_expert,
3591                                          m.down_exps.qtype, m.down_exps.row_bytes)?;
3592                }
3593            }
3594            // instrumentation parity with the host paths (3 blocks/expert-slot, all hits).
3595            c.hits += (t * 3 * n_used) as u64;
3596            Ok(())
3597        })?;
3598        }
3599
3600        // SHARED EXPERT epilogue — byte-identical to moe_ffn_sequential step 3 (incl. its Q8
3601        // TRUNK-FUSION arm: fused2 is bit-identical to the two matmul calls per (tensor,row)).
3602        // gate_inp_shexp is OPTIONAL: qwen35moe gates the shared expert (sigmoid(gate_inp) x sh);
3603        // MiniMax-M3 (DeepSeek-V3 class) has NO shexp gate — the shared expert adds directly.
3604        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
3605            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
3606        {
3607            let n_ff_sh = gate_shexp.out_features();
3608            // verify-t (2..15) decode-exact arm: this fn now serves the spec verify batches
3609            // (pairs gate moved to t>=PRIME_MIN_T), so the shexp chain must match t==1 per col.
3610            let verify_t = t > 1 && t < PRIME_MIN_T;
3611            let (sg_gate, sg_up) = if t == 1 {
3612                match e.matmul_q8_fused2_x(gate_shexp, up_shexp, z)? {
3613                    Some(pair) => pair,
3614                    None => (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?),
3615                }
3616            } else if verify_t {
3617                // VERIFY-TIER TRUNK FUSION (MEMRA_SPEC_FUSED_T, t=2-4): the shexp gate+up pair
3618                // rides one shared quantize + one fused2 batched launch instead of two
3619                // decode-exact calls. Bit-identical per (tensor,token,row) — see spec_fused_t.
3620                let mut fused = None;
3621                if crate::spec::spec_fused_t() && (2..=4).contains(&t)
3622                    && e.uses_q8_1_fast(gate_shexp) && e.uses_q8_1_fast(up_shexp) {
3623                    let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
3624                    fused = e.matmul_q8_fused2_t(gate_shexp, up_shexp, &zq, &zd, t)?;
3625                }
3626                match fused {
3627                    Some(pair) => pair,
3628                    None => (e.matmul_decode_exact(gate_shexp, z, t)?,
3629                             e.matmul_decode_exact(up_shexp, z, t)?),
3630                }
3631            } else {
3632                (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?)
3633            };
3634            let mut sa = e.uninit(t * n_ff_sh)?;  // silu_mul fully overwrites
3635            e.silu_mul(&sg_gate, &sg_up, &mut sa, t * n_ff_sh)?;
3636            let sh = if verify_t { e.matmul_decode_exact(down_shexp, &sa, t)? }
3637                     else { e.matmul(down_shexp, &sa, t)? };
3638            // shexp gate: qwen35moe sigmoid-gates; M3 has no gate tensor -> weight 1.0.
3639            // Same fused sigmoid-dot as moe_ffn_sequential step 3 (byte-identity contract
3640            // between the two arms; prefill keeps the batched cuBLASLt linear).
3641            let g = match &m.gate_inp_shexp {
3642                Some(gate_inp_shexp) => {
3643                    // router_prefill_exact_on(): the fused sigmoid-dot is m-INVARIANT and
3644                    // serves EVERY t (serve isolation, lane/concat-prime-exact).
3645                    if t < PRIME_MIN_T || crate::router_prefill_exact_on() {
3646                        e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
3647                    } else {
3648                        let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
3649                        let mut g = e.uninit(t)?;
3650                        e.sigmoid(&gs, &mut g, t)?;
3651                        g
3652                    }
3653                }
3654                None => e.htod(&vec![1.0f32; t])?,
3655            };
3656            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
3657        }
3658
3659        Ok(moe_out)
3660    }
3661
3662    /// STAGE-2 GROUPED DECODE (2026-07-04): run ONE token's whole routed-expert FFN in TWO
3663    /// launches when every one of its 3*n_used blocks is ALREADY cache-resident. Returns
3664    /// Ok(true) if the grouped path ran (caller skips the sequential loop for this token);
3665    /// Ok(false) on ANY miss (caller falls through — the sequential loop stages/admits as
3666    /// before, so the NEXT occurrence takes the grouped path). Pointer safety: cache slots are
3667    /// fixed-address for the engine's lifetime and the pure-HIT path performs no admission, so
3668    /// the collected raw pointers cannot move between collection and launch (single-threaded
3669    /// decode; the lock is held only for collection, launches are stream-ordered after any
3670    /// prior same-stream staging writes).
3671    #[allow(clippy::too_many_arguments)]
3672    /// q8 twin of moe_gdec_token (dp4a arc): same residency check + 2-launch shape; the fused
3673    /// kernels consume the pre-quantized z-row and re-quantize act per slot batch.
3674    #[allow(clippy::too_many_arguments)]
3675    fn moe_gdec_token_q8(e: &Engine, m: &MoeWeights, il: u16, max_block: usize,
3676                      zq: &CudaSlice<i8>, zd: &CudaSlice<f32>, sel: &[u32], w: &[f32],
3677                      moe_out: &mut CudaSlice<f32>, tok: usize,
3678                      n_embd: usize, n_ff_exp: usize, n_used: usize)
3679                      -> Result<bool, Box<dyn std::error::Error>> {
3680        use crate::moe_cache::{BlockId, PROJ_GATE, PROJ_UP, PROJ_DOWN};
3681        use cudarc::driver::DevicePtr;
3682        let ptrs = e.with_moe_cache(max_block, |c, eng| {
3683            let mut g = [0u64; 8];
3684            let mut u = [0u64; 8];
3685            let mut d = [0u64; 8];
3686            for (j, &ex) in sel.iter().enumerate() {
3687                let ex = ex as u16;
3688                let (Some(sg), Some(su), Some(sd)) = (c.resident(BlockId::new(il, PROJ_GATE, ex)),
3689                                                      c.resident(BlockId::new(il, PROJ_UP,   ex)),
3690                                                      c.resident(BlockId::new(il, PROJ_DOWN, ex)))
3691                else { return Ok(None); };
3692                let __s = eng.stream();
3693                let (pg, _e0) = c.slot(sg).device_ptr(&__s);
3694                let (pu, _e1) = c.slot(su).device_ptr(&__s);
3695                let (pd, _e2) = c.slot(sd).device_ptr(&__s);
3696                g[j] = pg as u64; u[j] = pu as u64; d[j] = pd as u64;
3697            }
3698            if cpu_expert_profile_admit_enabled() && !c.is_frozen() {
3699                for &ex in sel {
3700                    let ex = ex as u16;
3701                    for proj in [PROJ_GATE, PROJ_UP, PROJ_DOWN] {
3702                        c.note_profile_hit(BlockId::new(il, proj, ex));
3703                    }
3704                }
3705            }
3706            c.hits += (3 * n_used) as u64;
3707            Ok(Some((g, u, d)))
3708        })?;
3709        let Some((g, u, d)) = ptrs else { return Ok(false) };
3710        let mut wv = [0f32; 8];
3711        wv[..n_used].copy_from_slice(w);
3712        let act = e.moe_gate_up_silu8_q8(crate::WPtr8(g), crate::WPtr8(u), zq, zd,
3713                                         n_embd, n_ff_exp, n_used,
3714                                         m.gate_exps.qtype, m.up_exps.qtype,
3715                                         m.gate_exps.row_bytes, m.up_exps.row_bytes)?;
3716        // per-slot act quantize: [n_used, n_ff] rows in one quantize launch.
3717        let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
3718        let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
3719        e.moe_down8_fma_q8(crate::WPtr8(d), crate::F32x8(wv), &aq2, &ad2, &mut dst,
3720                           n_ff_exp, n_embd, n_used,
3721                           m.down_exps.qtype, m.down_exps.row_bytes)?;
3722        Ok(true)
3723    }
3724
3725    fn moe_gdec_token(e: &Engine, m: &MoeWeights, il: u16, max_block: usize,
3726                      zt: &cudarc::driver::CudaView<f32>, sel: &[u32], w: &[f32],
3727                      moe_out: &mut CudaSlice<f32>, tok: usize,
3728                      n_embd: usize, n_ff_exp: usize, n_used: usize)
3729                      -> Result<bool, Box<dyn std::error::Error>> {
3730        use crate::moe_cache::{BlockId, PROJ_GATE, PROJ_UP, PROJ_DOWN};
3731        use cudarc::driver::DevicePtr;
3732        // One lock hold: residency-check all 3*n_used blocks, collect raw slot pointers.
3733        let ptrs = e.with_moe_cache(max_block, |c, eng| {
3734            let mut g = [0u64; 8];
3735            let mut u = [0u64; 8];
3736            let mut d = [0u64; 8];
3737            for (j, &ex) in sel.iter().enumerate() {
3738                let ex = ex as u16;
3739                let (Some(sg), Some(su), Some(sd)) = (c.resident(BlockId::new(il, PROJ_GATE, ex)),
3740                                                      c.resident(BlockId::new(il, PROJ_UP,   ex)),
3741                                                      c.resident(BlockId::new(il, PROJ_DOWN, ex)))
3742                else { return Ok(None); };
3743                let __s = eng.stream();
3744                let (pg, _e0) = c.slot(sg).device_ptr(&__s);
3745                let (pu, _e1) = c.slot(su).device_ptr(&__s);
3746                let (pd, _e2) = c.slot(sd).device_ptr(&__s);
3747                g[j] = pg as u64; u[j] = pu as u64; d[j] = pd as u64;
3748            }
3749            if cpu_expert_profile_admit_enabled() && !c.is_frozen() {
3750                for &ex in sel {
3751                    let ex = ex as u16;
3752                    for proj in [PROJ_GATE, PROJ_UP, PROJ_DOWN] {
3753                        c.note_profile_hit(BlockId::new(il, proj, ex));
3754                    }
3755                }
3756            }
3757            c.hits += (3 * n_used) as u64; // instrumentation parity with dispatch()
3758            Ok(Some((g, u, d)))
3759        })?;
3760        let Some((g, u, d)) = ptrs else { return Ok(false) };
3761        let mut wv = [0f32; 8];
3762        wv[..n_used].copy_from_slice(w);
3763        // 2 launches: (gate+up+silu) x8, then (down + slot-ordered FMA accumulate) x8.
3764        let act = e.moe_gate_up_silu8(crate::WPtr8(g), crate::WPtr8(u), zt,
3765                                      n_embd, n_ff_exp, n_used,
3766                                      m.gate_exps.qtype, m.up_exps.qtype,
3767                                      m.gate_exps.row_bytes, m.up_exps.row_bytes)?;
3768        let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
3769        e.moe_down8_fma_into(crate::WPtr8(d), crate::F32x8(wv), &act, &mut dst,
3770                             n_ff_exp, n_embd, n_used,
3771                             m.down_exps.qtype, m.down_exps.row_bytes)?;
3772        Ok(true)
3773    }
3774
3775    /// EDGE-1 §B.3: dispatch one expert projection through the SLRU cache, then run the SAME
3776    /// `qmatvec_view` from whichever slot it landed in (resident HIT or staged MISS). `x` is the
3777    /// sliced activation row. `proj` selects the gate/up/down HostExps tensor. Returns y = W_expert @ x.
3778    /// q8 twin of moe_cached_gemm: same dispatch/slot mechanics, dp4a expert kernel.
3779    fn moe_cached_gemm_q8(e: &Engine, il: u16, proj: u8, ex: usize, m: &MoeWeights,
3780                          max_block: usize, aq: &CudaSlice<i8>, ad: &CudaSlice<f32>)
3781                          -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3782        use crate::moe_cache::{BlockId, DispatchSlot, PROJ_GATE, PROJ_UP};
3783        let exps = match proj { PROJ_GATE => &m.gate_exps, PROJ_UP => &m.up_exps, _ => &m.down_exps };
3784        let layout = exps.expert_layout(ex);
3785        let id = BlockId::new(il, proj, ex as u16);
3786        let source = exps.expert_source(ex);
3787        e.with_moe_cache(max_block, |c, eng| {
3788            let slot = c.dispatch_source(id, source, eng)?;
3789            let DispatchSlot::Resident(sl) = slot;
3790            let buf = c.slot(sl);
3791            eng.qmatvec_expert_q8(buf, 0..layout.len, aq, ad, 1, exps.in_f, exps.out_f,
3792                                  layout.qtype, layout.row_bytes)
3793        })
3794    }
3795
3796    fn moe_cached_gemm(e: &Engine, il: u16, proj: u8, ex: usize, m: &MoeWeights,
3797                       max_block: usize, x: &cudarc::driver::CudaView<f32>)
3798                       -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3799        use crate::moe_cache::{BlockId, DispatchSlot, PROJ_GATE, PROJ_UP};
3800        let exps = match proj { PROJ_GATE => &m.gate_exps, PROJ_UP => &m.up_exps, _ => &m.down_exps };
3801        let layout = exps.expert_layout(ex);
3802        let id = BlockId::new(il, proj, ex as u16);
3803        let source = exps.expert_source(ex);
3804        // dispatch under the lock (lookup/admit/memcpy-issue), then resolve the slot and GEMM.
3805        e.with_moe_cache(max_block, |c, eng| {
3806            let slot = c.dispatch_source(id, source, eng)?;
3807            // resolve the device buffer for this slot; the GEMM is enqueued on the compute stream
3808            // (the same stream the memcpy was issued on, so ordering holds without extra sync).
3809            let DispatchSlot::Resident(sl) = slot;
3810            let buf = c.slot(sl);
3811            eng.qmatvec_view(buf, 0..layout.len, x, 1, exps.in_f, exps.out_f,
3812                             layout.qtype, layout.row_bytes)
3813        })
3814    }
3815
3816    /// Populate the warmup cache for an expert whose current-token output intentionally ran on
3817    /// CPU. No GEMM is launched and callers invoke this only after the CPU result has completed,
3818    /// so the current forward's backend assignment and output remain unchanged.
3819    fn moe_profile_admit_expert(
3820        e: &Engine,
3821        il: u16,
3822        ex: usize,
3823        m: &MoeWeights,
3824        max_block: usize,
3825    ) -> Result<(), Box<dyn std::error::Error>> {
3826        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
3827        e.with_moe_cache(max_block, |cache, eng| {
3828            for (proj, exps) in [
3829                (PROJ_GATE, &m.gate_exps),
3830                (PROJ_UP, &m.up_exps),
3831                (PROJ_DOWN, &m.down_exps),
3832            ] {
3833                let id = BlockId::new(il, proj, ex as u16);
3834                let _ = cache.dispatch_source(id, exps.expert_source(ex), eng)?;
3835            }
3836            Ok(())
3837        })
3838    }
3839
3840    /// Read a projection from the immutable residency set when present; otherwise use one
3841    /// transient slot without admitting or evicting anything. Used only by post-freeze prefill.
3842    #[allow(clippy::too_many_arguments)]
3843    fn moe_frozen_gemm(
3844        e: &Engine,
3845        il: u16,
3846        proj: u8,
3847        ex: usize,
3848        m: &MoeWeights,
3849        max_block: usize,
3850        x: &cudarc::driver::CudaView<f32>,
3851        scratch: &mut Option<CudaSlice<u8>>,
3852        scratch_len: usize,
3853    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3854        use crate::moe_cache::{BlockId, PROJ_GATE, PROJ_UP};
3855        let exps = match proj {
3856            PROJ_GATE => &m.gate_exps,
3857            PROJ_UP => &m.up_exps,
3858            _ => &m.down_exps,
3859        };
3860        let layout = exps.expert_layout(ex);
3861        let id = BlockId::new(il, proj, ex as u16);
3862        if let Some(output) = e.with_moe_cache(max_block, |cache, eng| {
3863            let Some(slot) = cache.resident(id) else {
3864                return Ok(None);
3865            };
3866            let buf = cache.slot(slot);
3867            Ok(Some(eng.qmatvec_view(
3868                buf,
3869                0..layout.len,
3870                x,
3871                1,
3872                exps.in_f,
3873                exps.out_f,
3874                layout.qtype,
3875                layout.row_bytes,
3876            )?))
3877        })? {
3878            return Ok(output);
3879        }
3880        if scratch.is_none() {
3881            *scratch = Some(e.alloc_u8_uninit(scratch_len)?);
3882        }
3883        let scratch = scratch.as_mut().unwrap();
3884        e.stage_expert(exps.expert_bytes(ex), scratch, 0)?;
3885        e.qmatvec_view(
3886            scratch,
3887            0..layout.len,
3888            x,
3889            1,
3890            exps.in_f,
3891            exps.out_f,
3892            layout.qtype,
3893            layout.row_bytes,
3894        )
3895    }
3896
3897    fn moe_prefetch_expert(
3898        e: &Engine,
3899        il: u16,
3900        ex: usize,
3901        m: &MoeWeights,
3902        max_block: usize,
3903        keep: &[crate::moe_cache::BlockId],
3904    ) -> Result<(), Box<dyn std::error::Error>> {
3905        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
3906        e.with_moe_cache(max_block, |c, eng| {
3907            for (proj, exps) in [(PROJ_GATE, &m.gate_exps), (PROJ_UP, &m.up_exps),
3908                                 (PROJ_DOWN, &m.down_exps)] {
3909                let id = BlockId::new(il, proj, ex as u16);
3910                let _ = c.prefetch_source(id, exps.expert_source(ex), keep, eng)?;
3911            }
3912            Ok(())
3913        })
3914    }
3915
3916    /// Worker-mode disk lookahead for grouped prefill. Memory sources are deliberately skipped so
3917    /// selecting `worker` changes only storage scheduling here; all CUDA work stays in dispatch.
3918    fn moe_prefetch_disk_expert(e: &Engine, il: u16, ex: usize, m: &MoeWeights,
3919                                max_block: usize, keep: &[crate::moe_cache::BlockId])
3920                                -> Result<(), Box<dyn std::error::Error>> {
3921        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
3922        e.with_moe_cache(max_block, |c, eng| {
3923            for (proj, exps) in [(PROJ_GATE, &m.gate_exps), (PROJ_UP, &m.up_exps),
3924                                 (PROJ_DOWN, &m.down_exps)] {
3925                let source = exps.expert_source(ex);
3926                if let crate::model::ExpertSource::Disk { .. } = &source {
3927                    let id = BlockId::new(il, proj, ex as u16);
3928                    let _ = c.prefetch_source(id, source, keep, eng)?;
3929                }
3930            }
3931            Ok(())
3932        })
3933    }
3934
3935    #[inline]
3936    fn moe_prefetch_host_expert(ex: usize, m: &MoeWeights) {
3937        let _ = m.gate_exps.prefetch_expert_pages(ex);
3938        let _ = m.up_exps.prefetch_expert_pages(ex);
3939        let _ = m.down_exps.prefetch_expert_pages(ex);
3940    }
3941}
3942
3943// ================================================================================================
3944// A2: EXPERT-GROUPED MoE PREFILL (MEMRA_MOE_GROUPED=1). Resident-case prototype.
3945//
3946// Instead of the per-token loop (T * 8 experts * 3 projections = 12024 individual m=1 matvecs),
3947// this groups tokens by expert and runs ONE matmul per active expert per projection at m=m_e.
3948// On a 501-token prefill with ~170 active experts, that's ~510 matmuls (vs 12024).
3949//
3950// EXACTNESS: per-token accumulation across its 8 experts is reordered (grouped processes experts
3951// in expert-id order, not the router's top-k order). To preserve bit-identity with the sequential
3952// loop, we use an 8-SLOT scheme: expert outputs are scattered into slots keyed by the token's
3953// top-k position (0..7), then reduced in that fixed order. This makes the f32 addition order
3954// identical to the per-token loop regardless of expert processing order.
3955//
3956// Memory: T * 8 * n_embd * 4 = 501 * 8 * 2048 * 4 = ~32 MB (slot buffer). Fine on 96GB.
3957// ================================================================================================
3958
3959impl HybridModel {
3960    /// A2 expert-grouped MoE FFN (prefill path, MEMRA_MOE_GROUPED=1). Same semantics as moe_ffn:
3961    /// z [T, n_embd] -> moe_out [T, n_embd]. BIT-IDENTICAL to moe_ffn when using the slot scheme.
3962    pub(crate) fn moe_ffn_grouped(e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>, t: usize,
3963                                  cfg: &ModelConfig, il: u16, _max_block: usize)
3964                   -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3965        let moe = cfg.moe.as_ref().unwrap();
3966        let n_embd = cfg.n_embd as usize;
3967        let n_expert = moe.expert_count as usize;
3968        let n_used = moe.expert_used_count as usize;
3969        let n_ff_exp = moe.expert_ff_length as usize;
3970
3971        // 1. ROUTER (identical to moe_ffn).
3972        let logits = e.matmul(&m.gate_inp, z, t)?;
3973        let (sel_all, w_all) = if let Some(sig) = cfg.sigmoid_router() {
3974            Self::moe_route_cfg(e, &logits, t, n_expert, n_used,
3975                                m.exp_probs_b.as_deref(), Some(sig), m.active_experts.as_deref())?
3976        } else {
3977            Self::moe_route_cfg(e, &logits, t, n_expert, n_used,
3978                                None, None, m.active_experts.as_deref())?
3979        };
3980        Self::trace_moe_routes(il, t, &sel_all, &w_all)?;
3981
3982        // 2. BUILD PER-EXPERT TOKEN LISTS (host-side grouping).
3983        // For each expert e, we need: which tokens use it, their positions in z, their top-k
3984        // slot index (for bit-identical accumulation), and their weights.
3985        struct ExpertGroup {
3986            tok_indices: Vec<i32>,   // indices into z rows (0..T-1)
3987            slot_indices: Vec<i32>,  // top-k slot (0..n_used-1) for that token-expert pair
3988            weights: Vec<f32>,       // renormalized weight for that token-expert pair
3989        }
3990        let mut groups: Vec<ExpertGroup> = (0..n_expert).map(|_| ExpertGroup {
3991            tok_indices: Vec::new(), slot_indices: Vec::new(), weights: Vec::new(),
3992        }).collect();
3993
3994        for tok in 0..t {
3995            for j in 0..n_used {
3996                let ex = sel_all[tok * n_used + j] as usize;
3997                let w = w_all[tok * n_used + j];
3998                groups[ex].tok_indices.push(tok as i32);
3999                groups[ex].slot_indices.push(j as i32);
4000                groups[ex].weights.push(w);
4001            }
4002        }
4003
4004        // 3. ALLOCATE SLOT BUFFER: [T, n_used, n_embd] f32, zero-initialized.
4005        // Each token's 8 expert contributions land in their respective slots.
4006        let mut slot_buf = e.zeros(t * n_used * n_embd)?;
4007        let mut wbuf = e.zeros(t * n_used)?;  // [T, n_used] weight buffer for FMA reduce
4008
4009        // Expert weight dimensions (used in both cache and staging paths).
4010        let g_len = m.gate_exps.max_expert_bytes();
4011        let u_len = m.up_exps.max_expert_bytes();
4012        let d_len = m.down_exps.max_expert_bytes();
4013        let use_cache = Engine::moe_cache_enabled();
4014        let max_block = _max_block;
4015
4016        // GPU scratch for staging (only allocated when NOT using cache).
4017        let (mut scratch_g, mut scratch_u, mut scratch_d) = if !use_cache {
4018            (Some(e.alloc_u8(g_len)?), Some(e.alloc_u8(u_len)?), Some(e.alloc_u8(d_len)?))
4019        } else {
4020            (None, None, None)
4021        };
4022
4023        // 4. PER ACTIVE EXPERT: gather, compute, scatter.
4024        // Processing ORDER: DESCENDING m_e (biggest token batches first) — the concluded winner
4025        // (rig5090 2026-07-04, the ascending-id arm and its MEMRA_MOE_ORDER seam removed): desc is
4026        // a first-forward win at partial cache capacity — the hot (big-m_e) experts are admitted
4027        // to the SLRU before the small-m_e tail can pollute it, so residency converges in ONE
4028        // forward instead of several: auto-cache T=501 126.9 -> 169.9 tok/s (1.34x), cap512
4029        // 119.6 -> 160.8 (and kills the rep-to-rep bimodal); wash (<2%) at cap64 pure-spill and
4030        // at long prompts where every expert stages regardless. Order is FREE to change without
4031        // breaking the byte-identity gate: the slot scheme pins each token's accumulation order
4032        // regardless of expert processing order (the whole point of the slots).
4033        let mut order: Vec<usize> =
4034            (0..n_expert).filter(|&ex| !groups[ex].tok_indices.is_empty()).collect();
4035        order.sort_by(|&a, &b| groups[b].tok_indices.len()
4036            .cmp(&groups[a].tok_indices.len()).then(a.cmp(&b)));
4037        let mut m_dist: Vec<usize> = Vec::new();  // for stats
4038        let page_window = moe_page_prefetch_window();
4039        let worker_disk_prefetch = use_cache && crate::spill_pread::worker_enabled();
4040        if worker_disk_prefetch {
4041            if let Some(first) = grouped_worker_prefetch_position(order.len(), None) {
4042                Self::moe_prefetch_disk_expert(e, il, order[first], m, max_block, &[])?;
4043            }
4044        }
4045        for (order_pos, &ex) in order.iter().enumerate() {
4046            for next in page_prefetch_positions(order_pos, order.len(), page_window) {
4047                Self::moe_prefetch_host_expert(order[next], m);
4048            }
4049            if worker_disk_prefetch {
4050                if let Some(next) = grouped_worker_prefetch_position(order.len(), Some(order_pos)) {
4051                    use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
4052                    let keep = [
4053                        BlockId::new(il, PROJ_GATE, ex as u16),
4054                        BlockId::new(il, PROJ_UP, ex as u16),
4055                        BlockId::new(il, PROJ_DOWN, ex as u16),
4056                    ];
4057                    Self::moe_prefetch_disk_expert(e, il, order[next], m, max_block, &keep)?;
4058                }
4059            }
4060            let grp = &groups[ex];
4061            let m_e = grp.tok_indices.len();
4062            m_dist.push(m_e);
4063            let gl = m.gate_exps.expert_layout(ex);
4064            let ul = m.up_exps.expert_layout(ex);
4065            let dl = m.down_exps.expert_layout(ex);
4066
4067            // Upload index/weight arrays to device. The down-proj per-expert macro-scale
4068            // (ModelOpt weight_scale_2) folds into the scatter weights — post-matmul linear,
4069            // same fold as the sequential loop's `w[j] * macro_scale(ex)`. 1.0 for GGUF experts.
4070            let tok_idx_d = e.htod_i32(&grp.tok_indices)?;
4071            let slot_idx_d = e.htod_i32(&grp.slot_indices)?;
4072            let dmac = m.down_exps.macro_scale(ex);
4073            let weight_d = if dmac == 1.0 { e.htod(&grp.weights)? } else {
4074                let scaled: Vec<f32> = grp.weights.iter().map(|&w| w * dmac).collect();
4075                e.htod(&scaled)?
4076            };
4077
4078            // GATHER: collect m_e activation rows from z into a contiguous buffer.
4079            let mut gathered = e.zeros(m_e * n_embd)?;
4080            e.gather_rows(z, &tok_idx_d, &mut gathered, n_embd, m_e)?;
4081            let gv = gathered.slice(0..m_e * n_embd);
4082
4083            // Compute gate/up/down matmuls -- two paths: cache-resident or host-staged.
4084            let y = if use_cache {
4085                use crate::moe_cache::{BlockId, PROJ_GATE, PROJ_UP, PROJ_DOWN};
4086                // CACHE PATH: dispatch through MOE cache, get device-resident buffer, GEMM at m=m_e.
4087                let gate = e.with_moe_cache(max_block, |c, eng| {
4088                    let id = BlockId::new(il, PROJ_GATE, ex as u16);
4089                    let slot = c.dispatch_source(id, m.gate_exps.expert_source(ex), eng)?;
4090                    let buf = c.buf(slot);
4091                    eng.qmatvec_view(buf, 0..gl.len, &gv, m_e,
4092                        m.gate_exps.in_f, m.gate_exps.out_f, gl.qtype, gl.row_bytes)
4093                })?;
4094                let up = e.with_moe_cache(max_block, |c, eng| {
4095                    let id = BlockId::new(il, PROJ_UP, ex as u16);
4096                    let slot = c.dispatch_source(id, m.up_exps.expert_source(ex), eng)?;
4097                    let buf = c.buf(slot);
4098                    eng.qmatvec_view(buf, 0..ul.len, &gv, m_e,
4099                        m.up_exps.in_f, m.up_exps.out_f, ul.qtype, ul.row_bytes)
4100                })?;
4101                // SiLU-MUL activation (per-expert macro-scales folded).
4102                let mut act = e.zeros(m_e * n_ff_exp)?;
4103                Self::ffn_act_scaled(e, cfg, &gate, &up,
4104                    m.gate_exps.macro_scale(ex), m.up_exps.macro_scale(ex), &mut act, m_e * n_ff_exp)?;
4105                let actv = act.slice(0..m_e * n_ff_exp);
4106                e.with_moe_cache(max_block, |c, eng| {
4107                    let id = BlockId::new(il, PROJ_DOWN, ex as u16);
4108                    let slot = c.dispatch_source(id, m.down_exps.expert_source(ex), eng)?;
4109                    let buf = c.buf(slot);
4110                    eng.qmatvec_view(buf, 0..dl.len, &actv, m_e,
4111                        m.down_exps.in_f, m.down_exps.out_f, dl.qtype, dl.row_bytes)
4112                })?
4113            } else {
4114                // STAGING PATH: H2D the expert blocks into scratch buffers, then GEMM.
4115                let sg = scratch_g.as_mut().unwrap();
4116                let su = scratch_u.as_mut().unwrap();
4117                let sd = scratch_d.as_mut().unwrap();
4118                e.stage_expert(m.gate_exps.expert_bytes(ex), sg, 0)?;
4119                e.stage_expert(m.up_exps.expert_bytes(ex), su, 0)?;
4120                e.stage_expert(m.down_exps.expert_bytes(ex), sd, 0)?;
4121                let gate = e.qmatvec_view(sg, 0..gl.len, &gv, m_e,
4122                    m.gate_exps.in_f, m.gate_exps.out_f, gl.qtype, gl.row_bytes)?;
4123                let up = e.qmatvec_view(su, 0..ul.len, &gv, m_e,
4124                    m.up_exps.in_f, m.up_exps.out_f, ul.qtype, ul.row_bytes)?;
4125                // SiLU-MUL activation (per-expert macro-scales folded).
4126                let mut act = e.zeros(m_e * n_ff_exp)?;
4127                Self::ffn_act_scaled(e, cfg, &gate, &up,
4128                    m.gate_exps.macro_scale(ex), m.up_exps.macro_scale(ex), &mut act, m_e * n_ff_exp)?;
4129                let actv = act.slice(0..m_e * n_ff_exp);
4130                e.qmatvec_view(sd, 0..dl.len, &actv, m_e,
4131                    m.down_exps.in_f, m.down_exps.out_f, dl.qtype, dl.row_bytes)?
4132            };
4133
4134            // SCATTER into slot buffer: each row goes to slot_buf[tok, slot, :].
4135            e.scatter_slot(&y, &tok_idx_d, &slot_idx_d, &weight_d,
4136                           &mut slot_buf, &mut wbuf, n_embd, n_used, m_e)?;
4137        }
4138
4139        // 5. REDUCE SLOTS: sum the 8 slots per token into the final moe_out.
4140        let mut moe_out = e.zeros(t * n_embd)?;
4141        e.reduce_slots(&slot_buf, &wbuf, &mut moe_out, n_embd, n_used, t)?;
4142
4143        // STATS: print m-distribution when MEMRA_MOE_STATS is set.
4144        if std::env::var("MEMRA_MOE_STATS").is_ok() && !m_dist.is_empty() {
4145            m_dist.sort_unstable();
4146            let active = m_dist.len();
4147            let mean = m_dist.iter().sum::<usize>() as f64 / active as f64;
4148            let median = m_dist[active / 2];
4149            let max_m = *m_dist.last().unwrap();
4150            let min_m = m_dist[0];
4151            let above16 = m_dist.iter().filter(|&&x| x >= 16).count();
4152            println!("moe-grouped il={il} t={t} active={active}/{n_expert} \
4153                      m_e: min={min_m} median={median} mean={mean:.1} max={max_m} \
4154                      above_gemm_threshold(>=16)={above16}/{active}");
4155        }
4156
4157        // 6. SHARED EXPERT (same as moe_ffn — untouched).
4158        // gate_inp_shexp is OPTIONAL: qwen35moe gates the shared expert (sigmoid(gate_inp) x sh);
4159        // MiniMax-M3 (DeepSeek-V3 class) has NO shexp gate — the shared expert adds directly.
4160        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
4161            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
4162        {
4163            let n_ff_sh = gate_shexp.out_features();
4164            let sg_gate = e.matmul(gate_shexp, z, t)?;
4165            let sg_up = e.matmul(up_shexp, z, t)?;
4166            let mut sa = e.zeros(t * n_ff_sh)?;
4167            Self::ffn_act(e, cfg, &sg_gate, &sg_up, &mut sa, t * n_ff_sh)?;
4168            let sh = e.matmul(down_shexp, &sa, t)?;
4169            // shexp gate: qwen35moe sigmoid-gates; M3 has no gate tensor -> weight 1.0.
4170            // Fused sigmoid-dot below PRIME_MIN_T — one fold order with the sequential and
4171            // dev decode arms (dispatch choice must not change bits).
4172            let g = match &m.gate_inp_shexp {
4173                Some(gate_inp_shexp) => {
4174                    // router_prefill_exact_on(): the fused sigmoid-dot is m-INVARIANT and
4175                    // serves EVERY t (serve isolation, lane/concat-prime-exact).
4176                    if t < PRIME_MIN_T || crate::router_prefill_exact_on() {
4177                        e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
4178                    } else {
4179                        let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
4180                        let mut g = e.uninit(t)?;
4181                        e.sigmoid(&gs, &mut g, t)?;
4182                        g
4183                    }
4184                }
4185                None => e.htod(&vec![1.0f32; t])?,
4186            };
4187            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
4188        }
4189
4190        Ok(moe_out)
4191    }
4192
4193    /// Lane-3 M2: cross-stream MoE for lockstep decode. Routes all m stream rows in one
4194    /// batch, executes fully-HBM-resident experts through the grouped gather/GEMM/scatter
4195    /// machinery at m_e>1 (weight reads amortized across streams), and assigns any expert
4196    /// with a missing projection to that row's CPU companion call (whole-expert granularity,
4197    /// same rule as the sequential frozen path). Slot-pinned accumulation keeps each row's
4198    /// expert-sum order identical to the sequential path.
4199    pub(crate) fn moe_ffn_lockstep(
4200        &self,
4201        e: &Engine,
4202        m: &MoeWeights,
4203        zbatch: &CudaSlice<f32>,
4204        mrows: usize,
4205        il: u16,
4206        max_block: usize,
4207    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4208        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
4209        let cfg = &self.cfg;
4210        let moe = cfg.moe.as_ref().unwrap();
4211        let n_embd = cfg.n_embd as usize;
4212        let n_expert = moe.expert_count as usize;
4213        let n_used = moe.expert_used_count as usize;
4214        let n_ff_exp = moe.expert_ff_length as usize;
4215
4216        let logits = e.matmul(&m.gate_inp, zbatch, mrows)?;
4217        let (sel_all, w_all) = if let Some(sig) = cfg.sigmoid_router() {
4218            Self::moe_route_cfg(e, &logits, mrows, n_expert, n_used,
4219                                m.exp_probs_b.as_deref(), Some(sig), m.active_experts.as_deref())?
4220        } else {
4221            Self::moe_route_cfg(e, &logits, mrows, n_expert, n_used,
4222                                None, None, m.active_experts.as_deref())?
4223        };
4224        Self::trace_moe_routes(il, mrows, &sel_all, &w_all)?;
4225
4226        // Residency split at whole-expert granularity against the (frozen) cache.
4227        let resident_expert: Vec<bool> = e.with_moe_cache(max_block, |c, _| {
4228            Ok((0..n_expert)
4229                .map(|ex| {
4230                    [PROJ_GATE, PROJ_UP, PROJ_DOWN].into_iter().all(|p| {
4231                        c.resident(BlockId::new(il, p, ex as u16)).is_some()
4232                    })
4233                })
4234                .collect())
4235        })?;
4236
4237        struct Group {
4238            rows: Vec<i32>,
4239            slots: Vec<i32>,
4240            weights: Vec<f32>,
4241        }
4242        let mut groups: std::collections::HashMap<usize, Group> = Default::default();
4243        let mut cpu_rows: Vec<Vec<(usize, f32)>> = vec![Vec::new(); mrows];
4244        let mut cpu_by_expert: std::collections::HashMap<usize, Vec<(usize, f32)>> =
4245            Default::default();
4246        for row in 0..mrows {
4247            for j in 0..n_used {
4248                let ex = sel_all[row * n_used + j] as usize;
4249                let w = w_all[row * n_used + j];
4250                if resident_expert[ex] {
4251                    let group = groups.entry(ex).or_insert_with(|| Group {
4252                        rows: Vec::new(),
4253                        slots: Vec::new(),
4254                        weights: Vec::new(),
4255                    });
4256                    group.rows.push(row as i32);
4257                    group.slots.push(j as i32);
4258                    group.weights.push(w);
4259                } else {
4260                    crate::cpu_experts::record_incomplete_gpu_residency(0);
4261                    cpu_rows[row].push((ex, w));
4262                    cpu_by_expert.entry(ex).or_default().push((row, w));
4263                }
4264            }
4265        }
4266
4267        // CPU tickets first: reads/compute overlap the GPU grouped work below. Experts routed
4268        // by >=2 streams go through the multi-row ABI (weight decode amortized across rows);
4269        // each row's remaining experts stay one ordinary per-row call. Contribution FP-sum
4270        // order per row differs from the sequential single-call chunk — part of the
4271        // documented lockstep numeric class.
4272        let host_rows = e.dtoh(zbatch)?;
4273        let rows_ok = crate::cpu_experts::rows_supported();
4274        enum CpuPart {
4275            Single { row: usize },
4276            Rows { rows: Vec<usize> },
4277        }
4278        let mut tickets: Vec<(CpuPart, crate::cpu_experts::CpuExpertTicket)> = Vec::new();
4279        let mut rows_served: std::collections::HashSet<(usize, usize)> = Default::default();
4280        if rows_ok {
4281            let mut shared: Vec<(usize, Vec<(usize, f32)>)> = cpu_by_expert
4282                .into_iter()
4283                .filter(|(_, rows)| rows.len() >= 2)
4284                .collect();
4285            shared.sort_by_key(|(ex, _)| *ex);
4286            for (ex, mut row_weights) in shared {
4287                row_weights.sort_by_key(|(row, _)| *row);
4288                let inputs: Vec<(&[f32], f32)> = row_weights
4289                    .iter()
4290                    .map(|&(row, w)| (&host_rows[row * n_embd..(row + 1) * n_embd], w))
4291                    .collect();
4292                let job = crate::cpu_experts::prepare_rows_job(m, ex, &inputs)
4293                    .map_err(std::io::Error::other)?;
4294                for &(row, _) in &row_weights {
4295                    rows_served.insert((row, ex));
4296                }
4297                tickets.push((
4298                    CpuPart::Rows {
4299                        rows: row_weights.iter().map(|&(row, _)| row).collect(),
4300                    },
4301                    crate::cpu_experts::submit_rows(job).map_err(std::io::Error::other)?,
4302                ));
4303            }
4304        }
4305        for (row, selected) in cpu_rows.iter().enumerate() {
4306            let leftover: Vec<(usize, f32)> = selected
4307                .iter()
4308                .copied()
4309                .filter(|&(ex, _)| !rows_served.contains(&(row, ex)))
4310                .collect();
4311            if leftover.is_empty() {
4312                continue;
4313            }
4314            let host_row = &host_rows[row * n_embd..(row + 1) * n_embd];
4315            let job = crate::cpu_experts::prepare_job(m, il, &leftover, host_row)
4316                .map_err(std::io::Error::other)?;
4317            tickets.push((
4318                CpuPart::Single { row },
4319                crate::cpu_experts::submit(job).map_err(std::io::Error::other)?,
4320            ));
4321        }
4322
4323        let mut slot_buf = e.zeros(mrows * n_used * n_embd)?;
4324        let mut wbuf = e.zeros(mrows * n_used)?;
4325        let mut order: Vec<usize> = groups.keys().copied().collect();
4326        order.sort_by(|&a, &b| {
4327            groups[&b].rows.len().cmp(&groups[&a].rows.len()).then(a.cmp(&b))
4328        });
4329        for &ex in &order {
4330            let group = &groups[&ex];
4331            let m_e = group.rows.len();
4332            let gl = m.gate_exps.expert_layout(ex);
4333            let ul = m.up_exps.expert_layout(ex);
4334            let dl = m.down_exps.expert_layout(ex);
4335            let row_idx_d = e.htod_i32(&group.rows)?;
4336            let slot_idx_d = e.htod_i32(&group.slots)?;
4337            let dmac = m.down_exps.macro_scale(ex);
4338            let weight_d = if dmac == 1.0 {
4339                e.htod(&group.weights)?
4340            } else {
4341                let scaled: Vec<f32> = group.weights.iter().map(|&w| w * dmac).collect();
4342                e.htod(&scaled)?
4343            };
4344            let mut gathered = e.zeros(m_e * n_embd)?;
4345            e.gather_rows(zbatch, &row_idx_d, &mut gathered, n_embd, m_e)?;
4346            let gv = gathered.slice(0..m_e * n_embd);
4347            let gate = e.with_moe_cache(max_block, |c, eng| {
4348                let slot = c
4349                    .resident(BlockId::new(il, PROJ_GATE, ex as u16))
4350                    .ok_or("lockstep resident expert vanished (cache not frozen?)")?;
4351                eng.qmatvec_view(c.buf(crate::moe_cache::DispatchSlot::Resident(slot)), 0..gl.len, &gv, m_e,
4352                    m.gate_exps.in_f, m.gate_exps.out_f, gl.qtype, gl.row_bytes)
4353            })?;
4354            let up = e.with_moe_cache(max_block, |c, eng| {
4355                let slot = c
4356                    .resident(BlockId::new(il, PROJ_UP, ex as u16))
4357                    .ok_or("lockstep resident expert vanished (cache not frozen?)")?;
4358                eng.qmatvec_view(c.buf(crate::moe_cache::DispatchSlot::Resident(slot)), 0..ul.len, &gv, m_e,
4359                    m.up_exps.in_f, m.up_exps.out_f, ul.qtype, ul.row_bytes)
4360            })?;
4361            let mut act = e.zeros(m_e * n_ff_exp)?;
4362            Self::ffn_act_scaled(e, cfg, &gate, &up,
4363                m.gate_exps.macro_scale(ex), m.up_exps.macro_scale(ex), &mut act, m_e * n_ff_exp)?;
4364            let actv = act.slice(0..m_e * n_ff_exp);
4365            let y = e.with_moe_cache(max_block, |c, eng| {
4366                let slot = c
4367                    .resident(BlockId::new(il, PROJ_DOWN, ex as u16))
4368                    .ok_or("lockstep resident expert vanished (cache not frozen?)")?;
4369                eng.qmatvec_view(c.buf(crate::moe_cache::DispatchSlot::Resident(slot)), 0..dl.len, &actv, m_e,
4370                    m.down_exps.in_f, m.down_exps.out_f, dl.qtype, dl.row_bytes)
4371            })?;
4372            e.scatter_slot(&y, &row_idx_d, &slot_idx_d, &weight_d,
4373                           &mut slot_buf, &mut wbuf, n_embd, n_used, m_e)?;
4374        }
4375        let mut moe_out = e.zeros(mrows * n_embd)?;
4376        e.reduce_slots(&slot_buf, &wbuf, &mut moe_out, n_embd, n_used, mrows)?;
4377
4378        // CPU contributions join BEFORE the shared expert (the sequential path's placement).
4379        let mut row_sums: Vec<Option<Vec<f32>>> = vec![None; mrows];
4380        for (part, ticket) in tickets {
4381            let cpu_output = ticket.wait().map_err(std::io::Error::other)?;
4382            let mut add_row = |row: usize, chunk: &[f32]| {
4383                let sum = row_sums[row].get_or_insert_with(|| vec![0.0f32; n_embd]);
4384                for (accumulator, value) in sum.iter_mut().zip(chunk) {
4385                    *accumulator += value;
4386                }
4387            };
4388            match part {
4389                CpuPart::Single { row } => add_row(row, &cpu_output),
4390                CpuPart::Rows { rows } => {
4391                    for (slot, row) in rows.into_iter().enumerate() {
4392                        add_row(row, &cpu_output[slot * n_embd..(slot + 1) * n_embd]);
4393                    }
4394                }
4395            }
4396        }
4397        for (row, sum) in row_sums.into_iter().enumerate() {
4398            let Some(sum) = sum else { continue };
4399            let cpu_output = e.htod(&sum)?;
4400            let mut dst = moe_out.slice_mut(row * n_embd..(row + 1) * n_embd);
4401            e.axpy_into(&cpu_output, 1.0, &mut dst, n_embd)?;
4402        }
4403
4404        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
4405            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
4406        {
4407            let n_ff_sh = gate_shexp.out_features();
4408            let sg_gate = e.matmul(gate_shexp, zbatch, mrows)?;
4409            let sg_up = e.matmul(up_shexp, zbatch, mrows)?;
4410            let mut sa = e.zeros(mrows * n_ff_sh)?;
4411            Self::ffn_act(e, cfg, &sg_gate, &sg_up, &mut sa, mrows * n_ff_sh)?;
4412            let sh = e.matmul(down_shexp, &sa, mrows)?;
4413            // lockstep rows ARE decode tokens: fused sigmoid-dot per row so batched serving
4414            // decode matches the single-sequence decode chain bit-for-bit.
4415            let g = match &m.gate_inp_shexp {
4416                Some(gate_inp_shexp) => {
4417                    e.sigmoid_dot_rows(zbatch, gate_inp_shexp.float_data(), n_embd, mrows)?
4418                }
4419                None => e.htod(&vec![1.0f32; mrows])?,
4420            };
4421            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, mrows)?;
4422        }
4423
4424        Ok(moe_out)
4425    }
4426}
4427
4428// ============================ gemma4 (R8 verified wiring) ==================================
4429// Node-for-node vs llama.cpp src/models/gemma4.cpp:180-405 (HANDOVER "R8 VERIFIED WIRING").
4430// v0 bring-up: full attention everywhere (exact for prompts < sliding_window 1024 — R6 masking
4431// later), sdpa_naive (hd 512 has no FA stamp), sequential host-staged MoE (the perf arms grow
4432// gemma variants after the correctness gate).
4433impl HybridModel {
4434    /// Per-layer attention geometry (R5): (head_dim, n_kv, n_head, rope_base, scale, is_swa).
4435    pub(crate) fn gemma4_geom(&self, il: usize) -> (usize, usize, usize, f32, f32, bool) {
4436        let g = self.cfg.gemma4.as_ref().unwrap();
4437        let swa = g.swa_pattern[il];
4438        let hd = if swa { g.key_length_swa } else { g.key_length_global } as usize;
4439        // attention scale = 1.0 (llama gemma4.cpp:11 "Gemma4 uses self.scaling = 1.0" — q/k are
4440        // per-head rms-normed; NOT the 1/sqrt(hd) default. Bring-up bug: 1/sqrt(hd) left token-0
4441        // rows exact (softmax over one element) while every later position drifted).
4442        (hd, g.head_count_kv[il] as usize, self.cfg.n_head as usize,
4443         if swa { g.rope_base_swa } else { g.rope_base_global },
4444         1.0, swa)
4445    }
4446
4447    /// Suppress-token mask over t logits rows (tokenizer.ggml.suppress_tokens; no-op when the
4448    /// model ships none). NOT monotonic like softcap — must run before every argmax/sample, so
4449    /// every gemma4 logits tail (forward/prime/decode/dc/verify/e4b) calls this on device ld.
4450    fn gemma4_suppress(&self, e: &Engine, ld: &mut CudaSlice<f32>, t: usize)
4451                       -> Result<(), Box<dyn std::error::Error>> {
4452        if let Some((ids, n)) = self.gemma4_aux.as_ref().and_then(|a| a.suppress_d.as_ref()) {
4453            e.mask_ids_rows(ld, ids, *n, self.output.out_features(), t)?;
4454        }
4455        Ok(())
4456    }
4457
4458    /// gemma4 attention (R5 geometry, R7 weightless V-norm on the RAW K projection, R9 dual rope).
4459    /// `cache`: Some => PRIME mode — append the T post-rope K / normed V rows into the quantized
4460    /// KV cache (same per-row quantize math as the decode append) and advance len. Fresh-prompt
4461    /// only (v0): attends within `tokens` via the f32 sdpa.
4462    fn gemma4_attn_prime(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer, il: usize,
4463                         h: &CudaSlice<f32>, pos_d: &CudaSlice<i32>, t: usize,
4464                         cache: Option<&mut Cache>)
4465                         -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4466        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
4467        let eps = self.cfg.rms_eps;
4468        let aux = self.gemma4_aux.as_ref().unwrap();
4469
4470        // quantize-once window: q/k/v share `h` — the MMQ D4 activation quantizes once
4471        // (h stays borrowed across the triple, so the cache key can't go stale).
4472        e.mmq_act_begin();
4473        let q0 = e.matmul(&fa.wq, h, t)?;   // [t, nh*hd]
4474        let k0 = e.matmul(&fa.wk, h, t)?;   // [t, nkv*hd]
4475        // globals ship no v_proj (wv := wk at load) — V input is the SAME projection output;
4476        // reuse k0 instead of re-running the identical matmul (K=V dedup, 5 layers).
4477        let v0 = if swa { e.matmul(&fa.wv, h, t)? } else { e.clone_dtod(&k0)? };
4478
4479        let mut q = e.uninit(t * nh * hd)?;
4480        let mut k = e.uninit(t * nkv * hd)?;
4481        // R7: V = weightless rms_norm of the raw projection; NEVER roped.
4482        let mut v = e.uninit(t * nkv * hd)?;
4483        // 31B glue lane: producers emit the bf16 FA operands (norm emits vb; rope emits qb/kb
4484        // post-rope) — kills 3 f32->bf16 converts + re-reads per layer. Bit-identical operands
4485        // (same __float2bfloat16); MEMRA_FA_EMIT=0 reverts to the convert-in-FA path.
4486        static EMIT: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
4487        let emit = t >= 16 && crate::Engine::qkvnorm_w_on_prefill(nh * t + 2 * nkv * t, hd)
4488            && *EMIT.get_or_init(|| std::env::var("MEMRA_FA_EMIT").map(|s| s != "0").unwrap_or(true));
4489        let mut qb = e.alloc_uninit::<u8>(if emit { t * nh * hd * 2 } else { 1 })?;
4490        let mut kb = e.alloc_uninit::<u8>(if emit { t * nkv * hd * 2 } else { 1 })?;
4491        let mut vb = e.alloc_uninit::<u8>(if emit { t * nkv * hd * 2 } else { 1 })?;
4492        // f16-P/V door: emit V as f16 straight from the norm when this layer's FA consumer
4493        // reads f16 — kills the per-layer bf16->f16 re-encode (1.4GB/pass on 31B SWA).
4494        let v_f16 = emit && crate::fa_f16pv_on() && match hd {
4495            512 => true,
4496            256 => swa && crate::faw_hp_on() && nh % 2 == 0 && (nh / nkv) % 2 == 0,
4497            _ => false,
4498        };
4499        if emit {
4500            e.rms_norm_qkv_w4b(&q0, &k0, &v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
4501                               &aux.ones, &mut q, &mut k, &mut v, &mut vb,
4502                               hd, nh * t, nkv * t, eps, v_f16)?;
4503        } else {
4504            e.rms_norm_qkv(&q0, &k0, &v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
4505                           &aux.ones, &mut q, &mut k, &mut v, hd, nh * t, nkv * t, eps)?;
4506        }
4507
4508        let ff = if swa { None } else {
4509            Some(aux.rope_freqs.as_ref().expect("gemma4 global rope needs rope_freqs.weight"))
4510        };
4511        if emit {
4512            e.rope_neox2_bf16e(&mut q, &mut k, &mut qb, &mut kb, pos_d, hd, hd, nh, nkv, t,
4513                               base, 1.0, ff)?;
4514        } else {
4515            e.rope_neox2(&mut q, &mut k, pos_d, hd, hd, nh, nkv, t, base, 1.0, ff)?;
4516        }
4517
4518        if let Some(cache) = cache {
4519            let kvl = cache.kv[il].as_mut().unwrap();
4520            assert_eq!(kvl.len, 0, "gemma4 prime is fresh-prompt only (v0)");
4521            e.append_kv_quantized_rows(&k, &v, &mut kvl.k, &mut kvl.v, kvl.len, t,
4522                                       kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes, (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on()))?;
4523            kvl.len += t;
4524        }
4525        let mut attn = e.zeros(t * nh * hd)?;
4526        // R6: SWA layers mask keys older than sliding_window once the prompt exceeds it
4527        // (windowed naive twin; fa windowed stamps later). Under the window, full attention
4528        // is exact — SWA rides fa_prefill (hd-256 stamp), the hd-512 globals stay naive.
4529        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
4530        if swa && t > win {
4531            if hd == 256 && std::env::var("MEMRA_NOFA").is_err() {
4532                if emit { e.fa_prefill_w_pre(&qb, &kb, &vb, &mut attn, hd, nh, nkv, t, t,
4533                                             scale, true, win, v_f16)?; }
4534                else { e.fa_prefill_w(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true,
4535                                      win)?; }
4536            } else {
4537                e.sdpa_naive_w(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true, win)?;
4538            }
4539        } else if hd == 256 && std::env::var("MEMRA_NOFA").is_err() {
4540            e.fa_prefill(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?;
4541        } else if hd == 512 && std::env::var("MEMRA_NOFA").is_err() {
4542            if emit { e.fa_prefill_hd512_pre(&qb, &kb, &vb, &mut attn, hd, nh, nkv, t, t,
4543                                             scale, true, v_f16)?; }
4544            else { e.fa_prefill_hd512(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?; }
4545        } else {
4546            e.sdpa_naive(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?;
4547        }
4548        Ok(e.matmul(&fa.wo, &attn, t)?)
4549    }
4550
4551    /// Back-compat wrapper (pure prefill, no cache).
4552    fn gemma4_attn(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer, il: usize,
4553                   h: &CudaSlice<f32>, pos_d: &CudaSlice<i32>, t: usize)
4554                   -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4555        self.gemma4_attn_prime(e, fa, il, h, pos_d, t, None)
4556    }
4557
4558    /// gemma4 MoE with the expert input PRE-QUANTIZED (the q8z tail fusion). Caller guarantees
4559    /// the fast-arm conditions (resident dev slabs + dp4a qtypes) — decode t=1 and verify rows
4560    /// arms only, per-token kernel chains identical to the f32-input path (same quantize bytes:
4561    /// the q8z epilogue is quantize_q8_1 verbatim).
4562    fn gemma4_moe_q8(&self, e: &Engine, m: &crate::hybrid::MoeWeights,
4563                     bits: &crate::hybrid::Gemma4MoeBits,
4564                     mq: &(CudaSlice<i8>, CudaSlice<f32>),
4565                     router_in: &CudaSlice<f32>, t: usize)
4566                     -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4567        let cfg = &self.cfg;
4568        let moe = cfg.moe.as_ref().unwrap();
4569        let n_embd = cfg.n_embd as usize;
4570        let n_expert = moe.expert_count as usize;
4571        let n_used = moe.expert_used_count as usize;
4572        let n_ff_exp = moe.expert_ff_length as usize;
4573        // router stays the two-launch pair: THREE fuse variants measured worse (serial-dot
4574        // -50%, 1024-thread warp-parallel -12% on topk sync overhead — jsonl 2026-07-11);
4575        // the pair's 12us is kernel time, not launch gaps.
4576        let logits = if crate::router_kernel_on() {
4577            e.router_gemv(m.gate_inp.float_data(), router_in, n_embd, n_expert, t)?
4578        } else {
4579            e.matmul(&m.gate_inp, router_in, t)?
4580        };
4581        let dev = m.dev_exps.as_ref().unwrap();
4582        let (sel_d, w_d) = e.moe_router_topk_scaled(&logits, t, n_expert, n_used,
4583                                                    &bits.per_expert_scale_d)?;
4584        let (zq, zd) = mq;
4585        if t == 1 {
4586            let selv = sel_d.slice(0..n_used);
4587            let wv = w_d.slice(0..n_used);
4588            let act = e.moe_gate_up_gelu8_dev_q8(&dev.ptr_row, &selv, zq, zd,
4589                                                 n_embd, n_ff_exp, n_used, n_expert,
4590                                                 m.gate_exps.qtype, m.up_exps.qtype,
4591                                                 m.gate_exps.row_bytes, m.up_exps.row_bytes)?;
4592            let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
4593            let mut moe_out = e.uninit(n_embd)?;
4594            e.moe_down8_fma_dev_q8(&dev.ptr_row, &selv, &wv, &aq2, &ad2,
4595                                   &mut moe_out.slice_mut(0..n_embd), n_ff_exp, n_embd,
4596                                   n_used, n_expert, m.down_exps.qtype, m.down_exps.row_bytes)?;
4597            return Ok(moe_out);
4598        }
4599        let csr = t <= 10 && std::env::var("MEMRA_GEMMA_CSR").as_deref() != Ok("0");
4600        let act = if csr {
4601            e.moe_gate_up_gelu8_dev_q8_csr(&dev.ptr_row, &sel_d, zq, zd, t * n_used,
4602                                           n_embd, n_ff_exp, n_used, n_expert,
4603                                           m.gate_exps.qtype, m.up_exps.qtype,
4604                                           m.gate_exps.row_bytes, m.up_exps.row_bytes)?
4605        } else {
4606            e.moe_gate_up_gelu8_dev_q8_rows(&dev.ptr_row, &sel_d, zq, zd, t,
4607                                            n_embd, n_ff_exp, n_used, n_expert,
4608                                            m.gate_exps.qtype, m.up_exps.qtype,
4609                                            m.gate_exps.row_bytes, m.up_exps.row_bytes)?
4610        };
4611        let (aq2, ad2) = e.quantize_q8_1(&act, t * n_used, n_ff_exp)?;
4612        let mut moe_out = e.uninit(t * n_embd)?;
4613        // down stays rows_g: the CSR dedup twin measured NEGATIVE at nsb=22 too (189.4 vs
4614        // 207.0 depth spec, bitwise-exact — jsonl 2026-07-10; qwen nsb=16 same verdict).
4615        e.moe_down8_fma_dev_q8_rows_g(&dev.ptr_row, &sel_d, &w_d, &aq2, &ad2, &mut moe_out, t,
4616                                      n_ff_exp, n_embd, n_used, n_expert,
4617                                      m.down_exps.qtype, m.down_exps.row_bytes)?;
4618        Ok(moe_out)
4619    }
4620
4621    /// gemma4 MoE (R2 router prologue input supplied by caller, R3 per-expert output scale).
4622    /// Sequential host-staged v0 — softmax gating + renorm (moe_route, the qwen recipe), GELU
4623    /// experts, scale folded into the accumulate weight (post-matmul linear scale, exact fold).
4624    fn gemma4_moe(&self, e: &Engine, m: &crate::hybrid::MoeWeights,
4625                  bits: &crate::hybrid::Gemma4MoeBits, moe_in: &CudaSlice<f32>,
4626                  router_in: &CudaSlice<f32>, t: usize)
4627                  -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4628        let cfg = &self.cfg;
4629        let moe = cfg.moe.as_ref().unwrap();
4630        let n_embd = cfg.n_embd as usize;
4631        let n_expert = moe.expert_count as usize;
4632        let n_used = moe.expert_used_count as usize;
4633        let n_ff_exp = moe.expert_ff_length as usize;
4634
4635        // Router: the in-house GEMV for ALL small t (decode AND verify ride the same per-column
4636        // kernel — the cuBLASLt n-dependence flipped top-k at verify t on the 27B, d994271);
4637        // batched matmul only at real prefill.
4638        let logits = if t < PRIME_MIN_T && crate::router_kernel_on() {
4639            e.router_gemv(m.gate_inp.float_data(), router_in, n_embd, n_expert, t)?
4640        } else {
4641            e.matmul(&m.gate_inp, router_in, t)?
4642        };
4643
4644        // FAST SMALL-T ARM (decode t=1 AND spec verify t=2..15): device softmax-topk router,
4645        // then PER TOKEN the same fused gate_up GELU + down8 FMA launch pair over the resident
4646        // dev slabs — verify rides the EXACT decode kernel chain per token (dispatch-parity
4647        // law; the qwen "verify must be kernel-dispatch-identical to decode" lesson).
4648        if t < PRIME_MIN_T && m.dev_exps.as_ref().is_some_and(|d| !d.gu_il)
4649            && expert_dp4a_supported(m.gate_exps.qtype) && expert_dp4a_supported(m.up_exps.qtype)
4650            && expert_dp4a_supported(m.down_exps.qtype)
4651            && std::env::var("MEMRA_GEMMA_MOE_FAST").as_deref() != Ok("0") {
4652            let dev = m.dev_exps.as_ref().unwrap();
4653            let (sel_d, w_d) = e.moe_router_topk_scaled(&logits, t, n_expert, n_used,
4654                                                        &bits.per_expert_scale_d)?;
4655            if t == 1 {
4656                let (zq, zd) = e.quantize_q8_1(moe_in, 1, n_embd)?;
4657                let selv = sel_d.slice(0..n_used);
4658                let wv = w_d.slice(0..n_used);
4659                let act = e.moe_gate_up_gelu8_dev_q8(&dev.ptr_row, &selv, &zq, &zd,
4660                                                     n_embd, n_ff_exp, n_used, n_expert,
4661                                                     m.gate_exps.qtype, m.up_exps.qtype,
4662                                                     m.gate_exps.row_bytes, m.up_exps.row_bytes)?;
4663                let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
4664                let mut moe_out = e.uninit(n_embd)?;
4665                e.moe_down8_fma_dev_q8(&dev.ptr_row, &selv, &wv, &aq2, &ad2,
4666                                       &mut moe_out.slice_mut(0..n_embd), n_ff_exp, n_embd,
4667                                       n_used, n_expert, m.down_exps.qtype, m.down_exps.row_bytes)?;
4668                return Ok(moe_out);
4669            }
4670            // VERIFY ROWS TWINS (t=2..15): ONE launch pair for all tokens; per (token,row,slot)
4671            // bodies are the t=1 kernels VERBATIM (bit-identical to the per-token loop).
4672            // gate_up rides the CSR owner-scan dedup when t <= 10 (each duplicated expert's
4673            // weight stream decoded once; the qwen CSR class). MEMRA_GEMMA_CSR=0 -> rows.
4674            let (zq, zd) = e.quantize_q8_1(moe_in, t, n_embd)?;
4675            let csr = t <= 10 && std::env::var("MEMRA_GEMMA_CSR").as_deref() != Ok("0");
4676            let act = if csr {
4677                e.moe_gate_up_gelu8_dev_q8_csr(&dev.ptr_row, &sel_d, &zq, &zd, t * n_used,
4678                                               n_embd, n_ff_exp, n_used, n_expert,
4679                                               m.gate_exps.qtype, m.up_exps.qtype,
4680                                               m.gate_exps.row_bytes, m.up_exps.row_bytes)?
4681            } else {
4682                e.moe_gate_up_gelu8_dev_q8_rows(&dev.ptr_row, &sel_d, &zq, &zd, t,
4683                                                n_embd, n_ff_exp, n_used, n_expert,
4684                                                m.gate_exps.qtype, m.up_exps.qtype,
4685                                                m.gate_exps.row_bytes, m.up_exps.row_bytes)?
4686            };
4687            let (aq2, ad2) = e.quantize_q8_1(&act, t * n_used, n_ff_exp)?;
4688            let mut moe_out = e.uninit(t * n_embd)?;
4689            e.moe_down8_fma_dev_q8_rows_g(&dev.ptr_row, &sel_d, &w_d, &aq2, &ad2, &mut moe_out, t,
4690                                          n_ff_exp, n_embd, n_used, n_expert,
4691                                          m.down_exps.qtype, m.down_exps.row_bytes)?;
4692            return Ok(moe_out);
4693        }
4694
4695        let (sel_all, mut w_all) = Self::moe_route(e, &logits, t, n_expert, n_used)?;
4696        for (i, &sx) in sel_all.iter().enumerate() {
4697            w_all[i] *= bits.per_expert_scale[sx as usize];
4698        }
4699
4700        // PREFILL PAIRS ARM (t >= 16): expert-major CSR over the resident slabs — ONE launch per
4701        // projection covers ALL (token,expert) pairs (the qwen pairs recipe, _em dot = expert_dot_g
4702        // which has the Q4_0 body; GELU pairs epilogue; R3 scale folded into pair_w).
4703        if t >= PRIME_MIN_T && m.dev_exps.as_ref().is_some_and(|d| !d.gu_il)
4704            && expert_dp4a_supported(m.gate_exps.qtype) && expert_dp4a_supported(m.up_exps.qtype)
4705            && expert_dp4a_supported(m.down_exps.qtype)
4706            && std::env::var("MEMRA_GEMMA_MOE_PAIRS").as_deref() != Ok("0") {
4707            let dev = m.dev_exps.as_ref().unwrap();
4708            let n_pairs = t * n_used;
4709            let pair_ex: Vec<i32> = sel_all.iter().map(|&x| x as i32).collect();
4710            let pair_tok: Vec<i32> = (0..n_pairs).map(|p| (p / n_used) as i32).collect();
4711            let tok_off: Vec<i32> = (0..=t).map(|tok| (tok * n_used) as i32).collect();
4712            let tok_ids: Vec<i32> = (0..n_pairs as i32).collect();
4713            let pt = e.htod_i32(&pair_tok)?;
4714            let pw = e.htod(&w_all)?;
4715            let toff = e.htod_i32(&tok_off)?;
4716            let tids = e.htod_i32(&tok_ids)?;
4717            let mut by_ex: Vec<Vec<i32>> = vec![Vec::new(); n_expert];
4718            for p in 0..n_pairs { by_ex[pair_ex[p] as usize].push(p as i32); }
4719            let mut ex_ids: Vec<i32> = Vec::new();
4720            let mut ex_off: Vec<i32> = vec![0];
4721            let mut ex_pairs: Vec<i32> = Vec::with_capacity(n_pairs);
4722            for (ex, list) in by_ex.iter().enumerate() {
4723                if list.is_empty() { continue; }
4724                ex_ids.push(ex as i32);
4725                ex_pairs.extend_from_slice(list);
4726                ex_off.push(ex_pairs.len() as i32);
4727            }
4728            let n_active = ex_ids.len();
4729            let exi = e.htod_i32(&ex_ids)?;
4730            let exo = e.htod_i32(&ex_off)?;
4731            let exp_d = e.htod_i32(&ex_pairs)?;
4732            // GROUPED f16 LANE (MEMRA_MOE_F16G=1, round 46 arc 2): dequant active experts to
4733            // f16 once per projection + one grouped f16 GEMM over the CSR groups; CSR order
4734            // end-to-end (gelu is elementwise), one row permute before the scatter. The
4735            // ragged down k (704) needs no padding here — cublas takes any k.
4736            // f16-mirror numeric class, argmax/spec gated. PER-MODEL default OFF (round 50):
4737            // the gelu class regressed g26 board-2048 prefill -8.3% under the round-49
4738            // Hopper default — see moe_f16g_gemma_on.
4739            if crate::moe_f16g_gemma_on()
4740                && f16g_proj_ok(m.gate_exps.qtype, n_embd)
4741                && f16g_proj_ok(m.up_exps.qtype, n_embd)
4742                && f16g_proj_ok(m.down_exps.qtype, n_ff_exp) {
4743                let csr_tok: Vec<i32> = ex_pairs.iter().map(|&p| p / n_used as i32).collect();
4744                let csr_tok_d = e.htod_i32(&csr_tok)?;
4745                let (z_f16, z_s) = e.moe_f16g_act(moe_in, Some(&csr_tok_d), n_embd, n_pairs)?;
4746                let g_csr = e.moe_f16_grouped(&dev.ptr_row, 0, n_expert, &exi, &ex_off, &exo,
4747                                              &z_f16, &z_s, n_embd, n_ff_exp, n_active, n_pairs,
4748                                              m.gate_exps.qtype, m.gate_exps.row_bytes)?;
4749                let u_csr = e.moe_f16_grouped(&dev.ptr_row, 1, n_expert, &exi, &ex_off, &exo,
4750                                              &z_f16, &z_s, n_embd, n_ff_exp, n_active, n_pairs,
4751                                              m.up_exps.qtype, m.up_exps.row_bytes)?;
4752                let act_csr = e.moe_pairs_gelu_mul(&g_csr, &u_csr, n_pairs * n_ff_exp)?;
4753                let (a_f16, a_s) = e.moe_f16g_act(&act_csr, None, n_ff_exp, n_pairs)?;
4754                let d_csr = e.moe_f16_grouped(&dev.ptr_row, 2, n_expert, &exi, &ex_off, &exo,
4755                                              &a_f16, &a_s, n_ff_exp, n_embd, n_active, n_pairs,
4756                                              m.down_exps.qtype, m.down_exps.row_bytes)?;
4757                let y_down = e.rows_permute(&d_csr, &exp_d, n_pairs, n_embd)?;
4758                let mut moe_out = e.uninit(t * n_embd)?;
4759                e.moe_pairs_scatter(&y_down, &pw, &toff, &tids, &mut moe_out, t, n_embd)?;
4760                if std::env::var("MEMRA_F16G_DEBUG").is_ok() {
4761                    let scan = |v: &[f32]| v.iter().filter(|x| !x.is_finite()).count();
4762                    let (yd, mo) = (e.dtoh(&y_down)?, e.dtoh(&moe_out)?);
4763                    eprintln!("[f16g-debug] post-permute bad={} post-scatter bad={}",
4764                              scan(&yd), scan(&mo));
4765                }
4766                return Ok(moe_out);
4767            }
4768            // gate/up: int8-MMA expert GEMM (in_f = n_embd 2816 = 11x256 tiles ok); down keeps
4769            // the decode-once dp4a (in_f 704 fails the 256-superblock tiling).
4770            let mma = n_embd % 256 == 0
4771                && std::env::var("MEMRA_GEMMA_MOE_MMA").as_deref() != Ok("0");
4772            let (gate, up) = if mma {
4773                let z_scr = e.mmq_iq_quantize_act(moe_in, n_embd, t)?;
4774                (e.mmq_iq_experts(&dev.ptr_row, 0, n_expert, &exi, &exo, &exp_d, &pt, &z_scr,
4775                                  n_embd, n_ff_exp, n_active, n_pairs, t,
4776                                  m.gate_exps.qtype, m.gate_exps.row_bytes)?,
4777                 e.mmq_iq_experts(&dev.ptr_row, 1, n_expert, &exi, &exo, &exp_d, &pt, &z_scr,
4778                                  n_embd, n_ff_exp, n_active, n_pairs, t,
4779                                  m.up_exps.qtype, m.up_exps.row_bytes)?)
4780            } else {
4781                let (zq, zd) = e.quantize_q8_1(moe_in, t, n_embd)?;
4782                (e.moe_pairs_matvec_q8_dec(&dev.ptr_row, 0, &exi, &exo, &exp_d, &pt, &zq, &zd,
4783                                           n_embd, n_ff_exp, n_expert, n_active, n_pairs,
4784                                           m.gate_exps.qtype, m.gate_exps.row_bytes)?,
4785                 e.moe_pairs_matvec_q8_dec(&dev.ptr_row, 1, &exi, &exo, &exp_d, &pt, &zq, &zd,
4786                                           n_embd, n_ff_exp, n_expert, n_active, n_pairs,
4787                                           m.up_exps.qtype, m.up_exps.row_bytes)?)
4788            };
4789            let pair_self: Vec<i32> = (0..n_pairs as i32).collect();
4790            let pself = e.htod_i32(&pair_self)?;
4791            // DOWN through the int8-MMA expert GEMM (2026-07-31, g26 prefill lever): the
4792            // ragged k (n_ff_exp=704 on the 26B) rides a PADDED k-walk — in_f rounds up
4793            // to the 256-val superblock (768) while the act quantizer's zero padding
4794            // makes every padded-k product exactly zero (weight overread bytes multiply
4795            // zero int8 act values; the dev slab carries 144B tail slack for the OOB).
4796            // The old dp4a matvec was 11.3ms/call at m=T (the 0.07x prefill wall).
4797            // MEMRA_GEMMA_MOE_MMA=0 reverts down together with gate/up.
4798            // FUSED ACT-EPILOGUE (default on): gelu_tanh(gate)*up + D4 quantize in one
4799            // launch — no f32 act buffer (the fused kernel zero-pads the ragged tail
4800            // exactly like the two-pass quantizer). MEMRA_MOE_FUSE_ACTQ=0 rollback.
4801            // Scratch bytes are BYTE-IDENTICAL (kernel-check gated).
4802            let y_down = if mma {
4803                let in_pad = n_ff_exp.div_ceil(256) * 256;
4804                let a_scr = if crate::moe_fuse_actq_on() {
4805                    e.mmq_iq_fused_act_quant(&gate, &up, n_ff_exp, n_pairs, 1)?
4806                } else {
4807                    let act = e.moe_pairs_gelu_mul(&gate, &up, n_pairs * n_ff_exp)?;
4808                    e.mmq_iq_quantize_act(&act, n_ff_exp, n_pairs)?
4809                };
4810                e.mmq_iq_experts(&dev.ptr_row, 2, n_expert, &exi, &exo, &exp_d, &pself, &a_scr,
4811                                 in_pad, n_embd, n_active, n_pairs, n_pairs,
4812                                 m.down_exps.qtype, m.down_exps.row_bytes)?
4813            } else {
4814                let act = e.moe_pairs_gelu_mul(&gate, &up, n_pairs * n_ff_exp)?;
4815                let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
4816                e.moe_pairs_matvec_q8_dec(&dev.ptr_row, 2, &exi, &exo, &exp_d, &pself, &aq2, &ad2,
4817                                          n_ff_exp, n_embd, n_expert, n_active, n_pairs,
4818                                          m.down_exps.qtype, m.down_exps.row_bytes)?
4819            };
4820            let mut moe_out = e.uninit(t * n_embd)?;
4821            e.moe_pairs_scatter(&y_down, &pw, &toff, &tids, &mut moe_out, t, n_embd)?;
4822            return Ok(moe_out);
4823        }
4824
4825        let g_len = m.gate_exps.expert_stride;
4826        let u_len = m.up_exps.expert_stride;
4827        let d_len = m.down_exps.expert_stride;
4828        // Resident dev slabs (fits-VRAM regime): read each expert straight from the device slab
4829        // at ex*stride — zero H2D, SAME qmatvec_view kernel/bytes as the staged path. Staging is
4830        // the spill fallback.
4831        let dev = m.dev_exps.as_ref().filter(|d| !d.gu_il);
4832        let (mut sg, mut su, mut sd) = if dev.is_some() { (None, None, None) } else {
4833            (Some(e.alloc_u8_uninit(g_len)?), Some(e.alloc_u8_uninit(u_len)?), Some(e.alloc_u8_uninit(d_len)?))
4834        };
4835        let mut moe_out = e.zeros(t * n_embd)?;
4836        for tok in 0..t {
4837            let sel = &sel_all[tok * n_used..(tok + 1) * n_used];
4838            let w = &w_all[tok * n_used..(tok + 1) * n_used];
4839            let zt = moe_in.slice(tok * n_embd..(tok + 1) * n_embd);
4840            for (j, &ex) in sel.iter().enumerate() {
4841                let ex = ex as usize;
4842                let gate = match dev {
4843                    Some(d) => e.qmatvec_view(&d.gate, ex * g_len..(ex + 1) * g_len, &zt, 1,
4844                        m.gate_exps.in_f, m.gate_exps.out_f, m.gate_exps.qtype, m.gate_exps.row_bytes)?,
4845                    None => {
4846                        let sg = sg.as_mut().unwrap();
4847                        e.stage_expert(m.gate_exps.expert_bytes(ex), sg, 0)?;
4848                        e.qmatvec_view(sg, 0..g_len, &zt, 1,
4849                            m.gate_exps.in_f, m.gate_exps.out_f, m.gate_exps.qtype, m.gate_exps.row_bytes)?
4850                    }
4851                };
4852                let up = match dev {
4853                    Some(d) => e.qmatvec_view(&d.up, ex * u_len..(ex + 1) * u_len, &zt, 1,
4854                        m.up_exps.in_f, m.up_exps.out_f, m.up_exps.qtype, m.up_exps.row_bytes)?,
4855                    None => {
4856                        let su = su.as_mut().unwrap();
4857                        e.stage_expert(m.up_exps.expert_bytes(ex), su, 0)?;
4858                        e.qmatvec_view(su, 0..u_len, &zt, 1,
4859                            m.up_exps.in_f, m.up_exps.out_f, m.up_exps.qtype, m.up_exps.row_bytes)?
4860                    }
4861                };
4862                let mut act = e.uninit(n_ff_exp)?;
4863                e.gelu_tanh_mul(&gate, &up, &mut act, n_ff_exp)?;
4864                let actv = act.slice(0..n_ff_exp);
4865                let y = match dev {
4866                    Some(d) => e.qmatvec_view(&d.down, ex * d_len..(ex + 1) * d_len, &actv, 1,
4867                        m.down_exps.in_f, m.down_exps.out_f, m.down_exps.qtype, m.down_exps.row_bytes)?,
4868                    None => {
4869                        let sd = sd.as_mut().unwrap();
4870                        e.stage_expert(m.down_exps.expert_bytes(ex), sd, 0)?;
4871                        e.qmatvec_view(sd, 0..d_len, &actv, 1,
4872                            m.down_exps.in_f, m.down_exps.out_f, m.down_exps.qtype, m.down_exps.row_bytes)?
4873                    }
4874                };
4875                let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
4876                e.axpy_into(&y, w[j], &mut dst, n_embd)?;
4877            }
4878        }
4879        Ok(moe_out)
4880    }
4881
4882    /// One gemma4 trunk layer (R8): x -> x_next.
4883    fn gemma4_layer(&self, e: &Engine, il: usize, layer: &crate::hybrid::HybridLayer,
4884                    x: &CudaSlice<f32>, pos_d: &CudaSlice<i32>, t: usize)
4885                    -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4886        let n_embd = self.cfg.n_embd as usize;
4887        let eps = self.cfg.rms_eps;
4888
4889        let mut h = e.zeros(t * n_embd)?;
4890        e.rms_norm(x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
4891        let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
4892        let o = self.gemma4_attn(e, fa, il, &h, pos_d, t)?;
4893        // gemma order: post_attention_norm applies to the ATTENTION OUTPUT, then the residual.
4894        let mut cur = e.zeros(t * n_embd)?;
4895        e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, t, eps)?;
4896        self.gemma4_layer_tail_add(e, layer, &cur, x, t)
4897    }
4898
4899    /// Everything after the attention output in a gemma4 layer: the residual add (cur + x ->
4900    /// attn_out) FUSED with the three attn_out norms, then shared FFN + router + MoE + combine +
4901    /// layer scale — shared verbatim by the prefill, decode and verify paths.
4902    fn gemma4_layer_tail_add(&self, e: &Engine, layer: &crate::hybrid::HybridLayer,
4903                             cur: &CudaSlice<f32>, x: &CudaSlice<f32>, t: usize)
4904                             -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4905        Ok(self.gemma4_layer_tail_add_n(e, layer, cur, x, t, None)?.0)
4906    }
4907
4908    /// tail_add with the NEXT layer's attn_norm fused into the closing add+scale (one launch
4909    /// produces both x_next and h_next — the cross-layer fusion). `next_norm` None = last layer.
4910    fn gemma4_layer_tail_add_n(&self, e: &Engine, layer: &crate::hybrid::HybridLayer,
4911                               cur: &CudaSlice<f32>, x: &CudaSlice<f32>, t: usize,
4912                               next_norm: Option<&CudaSlice<f32>>)
4913                               -> Result<(CudaSlice<f32>, Option<CudaSlice<f32>>), Box<dyn std::error::Error>> {
4914        let n_embd = self.cfg.n_embd as usize;
4915        let bits = layer.gemma4.as_ref().unwrap();
4916        let (sn, attn_out) = self.gemma4_layer_tail_core(e, layer, cur, x, t)?;
4917        let mut xn = e.uninit(t * n_embd)?;
4918        match next_norm {
4919            Some(w) => {
4920                let mut hn = e.uninit(t * n_embd)?;
4921                e.add_scale_rms_norm(&sn, &attn_out, bits.layer_scale, w, &mut xn, &mut hn,
4922                                     n_embd, t, self.cfg.rms_eps)?;
4923                Ok((xn, Some(hn)))
4924            }
4925            None => {
4926                e.add_scale(&sn, &attn_out, bits.layer_scale, &mut xn, t * n_embd)?;
4927                Ok((xn, None))
4928            }
4929        }
4930    }
4931
4932    /// Tail core: attn_out = cur+x (fused with the 3 norms), both FFN branches, the combine
4933    /// norm — returns (sn, attn_out) for the closing add+scale variants.
4934    fn gemma4_layer_tail_core(&self, e: &Engine, layer: &crate::hybrid::HybridLayer,
4935                              cur: &CudaSlice<f32>, x: &CudaSlice<f32>, t: usize)
4936                              -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
4937        self.gemma4_layer_tail_core_pn(e, layer, cur, x, t, None, false)
4938    }
4939
4940    /// tail_core with an optional PRE-NORM fold (glue-fusion lane): `pre_norm = Some(wa)`
4941    /// means `cur` is the RAW attention output and the dense entry runs
4942    /// rms(cur, wa) + residual-add + ffn_norm as ONE launch (E4B's post-attn norm fold).
4943    /// `defer_post_norm`: dense-arm exit returns RAW f0 (ffn_down output) instead of
4944    /// sn = rms(f0, post_ffw) — the caller fuses the post-norm into its residual emit
4945    /// (rms_pre_add_q8_1, E4B glue wave 5). MoE arm ignores it.
4946    fn gemma4_layer_tail_core_pn(&self, e: &Engine, layer: &crate::hybrid::HybridLayer,
4947                                 cur: &CudaSlice<f32>, x: &CudaSlice<f32>, t: usize,
4948                                 pre_norm: Option<&CudaSlice<f32>>, defer_post_norm: bool)
4949                                 -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
4950        let n_embd = self.cfg.n_embd as usize;
4951        let eps = self.cfg.rms_eps;
4952        let bits = layer.gemma4.as_ref().unwrap();
4953
4954        // DENSE gemma4 variants (31B/E4B — no MoE, no parallel branch): attn_out = cur + x
4955        // fused with the single ffn_norm; GELU_PAR ffn; post_ffw_norm.
4956        let Some(mbits) = bits.moe_bits.as_ref() else {
4957            let crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } = &layer.ffn
4958            else { panic!("gemma4 dense layer without Dense ffn") };
4959            let mut attn_out = e.uninit(t * n_embd)?;
4960            let mut zsh = e.uninit(t * n_embd)?;
4961            // wave-2: with the pre-norm fold active the entry ALSO emits zsh q8_1 — the
4962            // t=1 fused2 gate/up consume the pair with no standalone quantize launch.
4963            let mut zpair: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
4964            match pre_norm {
4965                Some(wa) if t == 1 => {
4966                    zpair = Some(e.rms_pre_add_rms_norm_q8z(cur, wa, x,
4967                                                            bits.ffn_norm.float_data(),
4968                                                            &mut attn_out, &mut zsh,
4969                                                            n_embd, t, eps)?);
4970                }
4971                Some(wa) => e.rms_pre_add_rms_norm(cur, wa, x, bits.ffn_norm.float_data(),
4972                                                   &mut attn_out, &mut zsh, n_embd, t, eps)?,
4973                None => e.add_rms_norm(cur, x, bits.ffn_norm.float_data(), &mut attn_out,
4974                                       &mut zsh, n_embd, t, eps)?,
4975            }
4976            let n_ff = ffn_gate.out_features();
4977            // FFN persistent slab (counter-barrier form) FALSIFIED here 2026-07-14
4978            // (falsification #7, jsonl row): PDL glue already hides the launch boundaries
4979            // it fused; barrier + worst-segment occupancy net −0.3% (31B depth) / −2.3%
4980            // (E4B spec). Down's act dependency is all-to-all, so sentinel sync cannot
4981            // rescue segment C — the megakernel front is closed for the dense tail.
4982            let (gate, up) = if t == 1 {
4983                let (zq, zd) = match zpair {
4984                    Some(p) => p,
4985                    None => e.quantize_q8_1(&zsh, 1, n_embd)?,
4986                };
4987                match e.matmul_q4_fused2(ffn_gate, ffn_up, &zq, &zd)? {
4988                    Some(p) => p,
4989                    None => (e.matmul_pre(ffn_gate, &zq, &zd, &zsh, 1)?,
4990                             e.matmul_pre(ffn_up, &zq, &zd, &zsh, 1)?),
4991                }
4992            } else {
4993                // BATCHED FUSED2 (DEFAULT ON 2026-07-13, MEMRA_F2B=0 seam): one segmented
4994                // launch for the verify's gate+up — the up segment's blocks fill SMs as
4995                // the gate segment drains (the launch-tail mechanism behind the b-tier
4996                // plateau; first positive after six falsified in-kernel variants).
4997                static F2B: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
4998                let f2b = *F2B.get_or_init(|| std::env::var("MEMRA_F2B").as_deref() != Ok("0"));
4999                let fused = if f2b {
5000                    let (zq, zd) = e.quantize_q8_1(&zsh, t, n_embd)?;
5001                    e.matmul_q4_fused2_batched(ffn_gate, ffn_up, &zq, &zd, t)?
5002                } else { None };
5003                match fused {
5004                    Some(p) => p,
5005                    None => {
5006                        // quantize-once window: gate/up share `zsh` (borrowed across the pair).
5007                        e.mmq_act_begin();
5008                        (e.matmul(ffn_gate, &zsh, t)?, e.matmul(ffn_up, &zsh, t)?)
5009                    }
5010                }
5011            };
5012            let mut act = e.uninit(t * n_ff)?;
5013            // act quantize folds into the GELU epilogue (bit-identical q8_1 rounding);
5014            // ffn_down rides matmul_pre — one quantize launch fewer per layer.
5015            let f0 = if e.uses_q8_1_fast(ffn_down) {
5016                let upv = e.view(&up, t * n_ff);
5017                let up_all = upv.slice(0..t * n_ff);
5018                let (aq, ad) = e.gelu_tanh_mul_q8_1(&gate, &up_all, &mut act, n_ff, t)?;
5019                e.matmul_pre(ffn_down, &aq, &ad, &act, t)?
5020            } else {
5021                e.gelu_tanh_mul(&gate, &up, &mut act, t * n_ff)?;
5022                e.matmul(ffn_down, &act, t)?
5023            };
5024            if defer_post_norm { return Ok((f0, attn_out)); }
5025            let mut sn = e.uninit(t * n_embd)?;
5026            e.rms_norm(&f0, bits.post_ffw_norm.float_data(), &mut sn, n_embd, t, eps)?;
5027            return Ok((sn, attn_out));
5028        };
5029
5030        assert!(pre_norm.is_none(), "pre-norm fold is dense-entry only");
5031        // MoE variant (26B): attn_out = cur + x fused with the three attn_out norms
5032        // (ffn_norm + router-scale + pre_ffw_norm_2): ONE launch, chains verbatim. At small t
5033        // (decode + verify) the zsh and moe_in outputs are EMITTED q8_1 (both consumers are
5034        // quantized matmuls — two quantize launches + two f32 round-trips fold away).
5035        let mut attn_out = e.uninit(t * n_embd)?;
5036        let mut router_in = e.uninit(t * n_embd)?;
5037        let fast_moe = match &layer.ffn {
5038            crate::hybrid::Ffn::Moe(m) => m.dev_exps.as_ref().is_some_and(|d| !d.gu_il)
5039                && expert_dp4a_supported(m.gate_exps.qtype)
5040                && expert_dp4a_supported(m.up_exps.qtype)
5041                && expert_dp4a_supported(m.down_exps.qtype)
5042                && std::env::var("MEMRA_GEMMA_MOE_FAST").as_deref() != Ok("0"),
5043            _ => false,
5044        };
5045        let q8z = t < PRIME_MIN_T && fast_moe;
5046        let (zsh_f32, zsh_q8, moe_q8) = if q8z {
5047            let (z0, m2) = e.add_rms_norm3_q8z(cur, x, bits.ffn_norm.float_data(),
5048                                               &mbits.router_scale_pre,
5049                                               mbits.pre_ffw_norm_2.float_data(),
5050                                               &mut attn_out, &mut router_in, n_embd, t, eps)?;
5051            (None, Some(z0), Some(m2))
5052        } else {
5053            let mut zsh = e.uninit(t * n_embd)?;
5054            let mut moe_in = e.uninit(t * n_embd)?;
5055            e.add_rms_norm3(cur, x, bits.ffn_norm.float_data(), &mbits.router_scale_pre,
5056                            mbits.pre_ffw_norm_2.float_data(), &mut attn_out, &mut zsh,
5057                            &mut router_in, &mut moe_in, n_embd, t, eps)?;
5058            (Some((zsh, moe_in)), None, None)
5059        };
5060        let attn_out2 = attn_out;
5061        #[allow(unused_variables)]
5062        let attn_out = &attn_out2;
5063        let n_ff = mbits.shared_gate.out_features();
5064        let (gate, up) = if let Some((zq, zd)) = zsh_q8.as_ref() {
5065            if t == 1 {
5066                match e.matmul_q4_fused2(&mbits.shared_gate, &mbits.shared_up, zq, zd)? {
5067                    Some(p) => p,
5068                    None => {
5069                        let h0 = e.zeros(0)?;
5070                        (e.matmul_pre(&mbits.shared_gate, zq, zd, &h0, 1)?,
5071                         e.matmul_pre(&mbits.shared_up, zq, zd, &h0, 1)?)
5072                    }
5073                }
5074            } else {
5075                // verify t 2..15: the batched mmvq twins consume the pre-quantized pair.
5076                let h0 = e.zeros(0)?;
5077                (e.matmul_pre(&mbits.shared_gate, zq, zd, &h0, t)?,
5078                 e.matmul_pre(&mbits.shared_up, zq, zd, &h0, t)?)
5079            }
5080        } else {
5081            let (zsh, _) = zsh_f32.as_ref().unwrap();
5082            (e.matmul(&mbits.shared_gate, zsh, t)?, e.matmul(&mbits.shared_up, zsh, t)?)
5083        };
5084        let mut act = e.uninit(t * n_ff)?;
5085        e.gelu_tanh_mul(&gate, &up, &mut act, t * n_ff)?;
5086        let mlp0 = e.matmul(&mbits.shared_down, &act, t)?;
5087        let crate::hybrid::Ffn::Moe(m) = &layer.ffn else { panic!("gemma4 layer not MoE") };
5088        let moe0 = match (&moe_q8, &zsh_f32) {
5089            (Some(mq), _) => self.gemma4_moe_q8(e, m, mbits, mq, &router_in, t)?,
5090            (None, Some((_, moe_in))) => self.gemma4_moe(e, m, mbits, moe_in, &router_in, t)?,
5091            _ => unreachable!(),
5092        };
5093        // post_ffw_norm_1(mlp0) + post_ffw_norm_2(moe0): one fused launch, per-row verbatim.
5094        let mut mlp = e.uninit(t * n_embd)?;
5095        let mut moe = e.uninit(t * n_embd)?;
5096        e.rms_norm2x(&mlp0, &moe0, mbits.post_ffw_norm_1.float_data(),
5097                     mbits.post_ffw_norm_2.float_data(), &mut mlp, &mut moe, n_embd, t, eps)?;
5098
5099        // combine: rms_norm(mlp + moe, post_ffw_norm) + attn_out, then the layer output scalar.
5100        // add+norm fused (add_rms_norm == add then rms_norm, kernel-check-pinned identity).
5101        let mut sum = e.uninit(t * n_embd)?;
5102        let mut sn = e.uninit(t * n_embd)?;
5103        e.add_rms_norm(&mlp, &moe, bits.post_ffw_norm.float_data(), &mut sum, &mut sn,
5104                       n_embd, t, eps)?;
5105        Ok((sn, attn_out2))
5106    }
5107
5108    /// tail_add with the next attn_norm emitted PRE-QUANTIZED q8_1 (decode/verify loops).
5109    fn gemma4_layer_tail_add_nq(&self, e: &Engine, layer: &crate::hybrid::HybridLayer,
5110                                cur: &CudaSlice<f32>, x: &CudaSlice<f32>, t: usize,
5111                                next_norm: Option<&CudaSlice<f32>>)
5112                                -> Result<(CudaSlice<f32>, Option<(CudaSlice<i8>, CudaSlice<f32>)>), Box<dyn std::error::Error>> {
5113        let n_embd = self.cfg.n_embd as usize;
5114        let bits = layer.gemma4.as_ref().unwrap();
5115        let (sn, attn_out) = self.gemma4_layer_tail_core(e, layer, cur, x, t)?;
5116        let mut xn = e.uninit(t * n_embd)?;
5117        match next_norm {
5118            Some(w) => {
5119                let pair = e.add_scale_rms_norm_q8_1(&sn, &attn_out, bits.layer_scale, w, &mut xn,
5120                                                     n_embd, t, self.cfg.rms_eps)?;
5121                Ok((xn, Some(pair)))
5122            }
5123            None => {
5124                e.add_scale(&sn, &attn_out, bits.layer_scale, &mut xn, t * n_embd)?;
5125                Ok((xn, None))
5126            }
5127        }
5128    }
5129
5130    /// gemma4 prefill: `last_only` = forward_last semantics (lm_head on the final row only).
5131    /// R4: final logits softcapped 30*tanh(l/30) on host (monotonic — argmax unaffected).
5132    fn gemma4_forward(&self, e: &Engine, tokens: &[u32], last_only: bool)
5133                      -> Result<Vec<f32>, Box<dyn std::error::Error>> {
5134        // E4B routes to its own forward regardless of the caller's entry point (forward /
5135        // forward_last / prime paths all funnel here for gemma4).
5136        if self.is_gemma4_e4b() { return self.gemma4_e4b_forward(e, tokens, last_only); }
5137        let n_embd = self.cfg.n_embd as usize;
5138        let t = tokens.len();
5139        let pos: Vec<i32> = (0..t as i32).collect();
5140        let pos_d = e.htod_i32(&pos)?;
5141
5142        let mut x = self.embed(e, tokens)?;
5143        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
5144        // MEMRA_GEMMA_PROBE=1: per-layer trunk stats (host rms + max of the LAST token row) —
5145        // the bring-up bisect vs llama-eval-callback node stats.
5146        let probe = std::env::var("MEMRA_GEMMA_PROBE").is_ok();
5147        let stat = |e: &Engine, x: &CudaSlice<f32>, tag: &str| -> Result<(), Box<dyn std::error::Error>> {
5148            let h = e.dtoh(x)?;
5149            let bad = h.iter().filter(|v| !v.is_finite()).count();
5150            let mx = h.iter().filter(|v| v.is_finite()).fold(0.0f32, |m, v| m.max(v.abs()));
5151            eprintln!("[gemma-probe] {tag}: tok0_first3={:?} bad={bad} max={mx:.3e}", &h[..3]);
5152            Ok(())
5153        };
5154        if probe { stat(e, &x, "embed")?; }
5155        for (il, layer) in self.layers.iter().enumerate() {
5156            x = self.gemma4_layer(e, il, layer, &x, &pos_d, t)?;
5157            if probe { stat(e, &x, &format!("L{il}"))?; }
5158        }
5159        let mut hn = e.zeros(t * n_embd)?;
5160        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, self.cfg.rms_eps)?;
5161        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
5162        let n_vocab = self.output.out_features();
5163        let logits = if last_only {
5164            let hv = e.view(&hn, t * n_embd);
5165            let last_row = hv.slice((t - 1) * n_embd..t * n_embd);
5166            let mut hlast = e.zeros(n_embd)?;
5167            e.copy_view_into(&mut hlast, 0, &last_row, n_embd)?;
5168            let mut ld = e.matmul(&self.output, &hlast, 1)?;
5169            e.softcap(&mut ld, cap, n_vocab)?;
5170            self.gemma4_suppress(e, &mut ld, 1)?;
5171            e.dtoh(&ld)?
5172        } else {
5173            let mut ld = e.matmul(&self.output, &hn, t)?;
5174            e.softcap(&mut ld, cap, t * n_vocab)?;
5175            self.gemma4_suppress(e, &mut ld, t)?;
5176            e.dtoh(&ld)?
5177        };
5178        Ok(logits)
5179    }
5180
5181    /// gemma4 BATCHED PROMPT PRIME v0 (fresh cache only): the prefill graph over the whole
5182    /// prompt with each layer's post-rope K / weightless-normed V appended into the quantized
5183    /// KV cache (decode-append row math). Returns (last-row softcapped logits, h_seed = last
5184    /// pre-output_norm hidden, hiddens = full pre-output_norm stack [T, n_embd]).
5185    pub(crate) fn gemma4_prime(&self, e: &Engine, tokens: &[u32], cache: &mut Cache)
5186                               -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
5187        assert_eq!(cache.pos, 0, "gemma4 prime v0 is fresh-prompt only");
5188        let n_embd = self.cfg.n_embd as usize;
5189        let eps = self.cfg.rms_eps;
5190        let t = tokens.len();
5191        let pos: Vec<i32> = (0..t as i32).collect();
5192        let pos_d = e.htod_i32(&pos)?;
5193        let mut x = self.embed(e, tokens)?;
5194        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
5195        for (il, layer) in self.layers.iter().enumerate() {
5196            let mut h = e.zeros(t * n_embd)?;
5197            e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
5198            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer not full-attn") };
5199            let o = self.gemma4_attn_prime(e, fa, il, &h, &pos_d, t, Some(cache))?;
5200            let mut cur = e.zeros(t * n_embd)?;
5201            e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, t, eps)?;
5202            x = self.gemma4_layer_tail_add(e, layer, &cur, &x, t)?;
5203            self.dflash_tap(e, cache, il, &x, t)?;
5204        }
5205        cache.pos += t;
5206        let hiddens = e.clone_dtod(&x)?;
5207        let xv = e.view(&x, t * n_embd);
5208        let last_row = xv.slice((t - 1) * n_embd..t * n_embd);
5209        let mut h_seed = e.zeros(n_embd)?;
5210        e.copy_view_into(&mut h_seed, 0, &last_row, n_embd)?;
5211        let mut hn = e.uninit(n_embd)?;
5212        e.rms_norm(&h_seed, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
5213        let mut ld = e.matmul(&self.output, &hn, 1)?;
5214        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
5215        e.softcap(&mut ld, cap, self.output.out_features())?;
5216        self.gemma4_suppress(e, &mut ld, 1)?;
5217        let logits = e.dtoh(&ld)?;
5218        Ok((logits, h_seed, hiddens))
5219    }
5220
5221    /// gemma4 T=1 decode attention: per-layer geometry, quantized-KV append + fa_decode
5222    /// (vec kernels at hd 256, generic scalar at the globals' hd 512), weightless V-norm,
5223    /// dual rope, scale 1.0. Takes the attn-normed input PRE-QUANTIZED (the cross-layer
5224    /// fused norm emits q8 directly — the f32 h never materializes).
5225    fn gemma4_decode_attn(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer, il: usize,
5226                          hq: &CudaSlice<i8>, hdq: &CudaSlice<f32>,
5227                          pos_d: &CudaSlice<i32>, cache: &mut Cache)
5228                          -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
5229        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
5230        let eps = self.cfg.rms_eps;
5231        let aux = self.gemma4_aux.as_ref().unwrap();
5232        let (hq, hdq) = (hq, hdq);
5233        let h0 = e.zeros(0)?;
5234        let h = &h0;
5235        let (q0, k0, v0) = if swa {
5236            match e.matmul_q4_fused3(&fa.wq, &fa.wk, &fa.wv, &hq, &hdq)? {
5237                Some(t3) => t3,
5238                None => (e.matmul_pre(&fa.wq, &hq, &hdq, h, 1)?,
5239                         e.matmul_pre(&fa.wk, &hq, &hdq, h, 1)?,
5240                         e.matmul_pre(&fa.wv, &hq, &hdq, h, 1)?),
5241            }
5242        } else {
5243            let (q0, k0) = match e.matmul_q4_fused2(&fa.wq, &fa.wk, &hq, &hdq)? {
5244                Some(p) => p,
5245                None => (e.matmul_pre(&fa.wq, &hq, &hdq, h, 1)?,
5246                         e.matmul_pre(&fa.wk, &hq, &hdq, h, 1)?),
5247            };
5248            let v0 = e.clone_dtod(&k0)?;
5249            (q0, k0, v0)
5250        };
5251        let mut q = e.uninit(nh * hd)?;
5252        let mut k = e.uninit(nkv * hd)?;
5253        let mut v = e.uninit(nkv * hd)?;
5254        // E4B wave-3 fold, m=1 completion (2026-07-23): the rows arms took this fold at
5255        // 550fcfa5; the decode-step trio kept 2 launches/layer it doesn't need.
5256        let ff = if swa { None } else {
5257            Some(aux.rope_freqs.as_ref().expect("gemma4 global rope needs rope_freqs.weight"))
5258        };
5259        e.rms_norm_qkv_rope(&q0, &k0, &v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
5260                            &aux.ones, &mut q, &mut k, &mut v, hd, nh, nkv,
5261                            pos_d, nh, nkv, base, 1.0, ff, eps)?;
5262        let kvl = cache.kv[il].as_mut().unwrap();
5263        e.append_kv_quantized(&k, &v, &mut kvl.k, &mut kvl.v, kvl.len,
5264                              kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes, (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on()))?;
5265        kvl.len += 1;
5266        // R6 decode: SWA layers attend only the last `sliding_window` keys — a token-aligned
5267        // VIEW OFFSET into the quantized cache (keys carry absolute rope; the mask is purely
5268        // positional). Globals attend the full history.
5269        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
5270        let mut attn = e.uninit(nh * hd)?;
5271        // global layers: SAME hd512 rows twin as verify with t=1 (parity law).
5272        if !swa && hd == 512 && kvl.len >= crate::fa512_min_tkv()
5273            && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
5274            let kp = e.view_u8(&kvl.k, kvl.len * kvl.k_tok_bytes);
5275            let vp = e.view_u8(&kvl.v, kvl.len * kvl.v_tok_bytes);
5276            // device-len: eager syncs the counter (async arg-store), dc keeps it live.
5277            let base = kvl.len as i32;
5278            e.i32_set_k(&mut kvl.len_d, base)?;
5279            e.fa_decode_rows(&q, &kp, &vp, &mut attn, hd, nh, nkv, kvl.len - 1, 1, scale,
5280                             kvl.k_tok_bytes, kvl.v_tok_bytes, Some((&kvl.len_d, -1)), false,
5281                             false, None)?;
5282            return Ok(e.matmul(&fa.wo, &attn, 1)?);
5283        }
5284        // windowed regime: SAME rows_w kernel as verify with t=1 (parity law — see verify_attn).
5285        if swa && kvl.len > win && hd == 256
5286            && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
5287            let kp = e.view_u8(&kvl.k, kvl.len * kvl.k_tok_bytes);
5288            let vp = e.view_u8(&kvl.v, kvl.len * kvl.v_tok_bytes);
5289            let base = kvl.len as i32;
5290            e.i32_set_k(&mut kvl.len_d, base)?;
5291            e.fa_decode_rows_w(&q, &kp, &vp, &mut attn, hd, nh, nkv, &kvl.len_d, -1, 1, scale,
5292                               win, kvl.k_tok_bytes, kvl.v_tok_bytes, None)?;
5293            return Ok(e.matmul(&fa.wo, &attn, 1)?);
5294        }
5295        let (off_tok, t_kv) = if swa && kvl.len > win { (kvl.len - win, win) } else { (0, kvl.len) };
5296        let k_view = e.view_u8_range(&kvl.k, off_tok * kvl.k_tok_bytes,
5297                                     (off_tok + t_kv) * kvl.k_tok_bytes);
5298        let v_view = e.view_u8_range(&kvl.v, off_tok * kvl.v_tok_bytes,
5299                                     (off_tok + t_kv) * kvl.v_tok_bytes);
5300        e.fa_decode_kvmod(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, t_kv, scale,
5301                    kvl.k_tok_bytes, kvl.v_tok_bytes, swa && crate::Engine::wkv_on())?;
5302        Ok(e.matmul(&fa.wo, &attn, 1)?)
5303    }
5304
5305    /// gemma4 DEVICE-COUNTER decode step (graph arc): token id + rope pos + KV lengths live in
5306    /// device counters; ZERO varying host kernel args. `cap_bucket_max` = Some(bucket) for graph
5307    /// capture (host mirrors untouched, n_splits from bucket, full-buffer KV views) / None for
5308    /// the eager-dc gate path (host mirrors advanced, live geometry — bit-identical target =
5309    /// gemma4_decode_step_h's token stream). V1 scope: t_kv <= sliding_window (no window views
5310    /// in-graph; the driver gates).
5311    #[allow(clippy::too_many_arguments)]
5312    pub fn gemma4_decode_step_dc(&self, e: &Engine, token_d: &CudaSlice<u32>,
5313                                 pos_d: &mut CudaSlice<i32>, embd_gpu: &CudaSlice<u8>,
5314                                 embd_qt: i32, embd_rb: usize, cache: &mut Cache,
5315                                 n_vocab: usize, cap_bucket_max: Option<(usize, usize)>)
5316                                 -> Result<CudaSlice<u32>, Box<dyn std::error::Error>> {
5317        let mut tok_out = e.stream().alloc_zeros::<u32>(1)?;
5318        self.gemma4_decode_step_dc_into(e, token_d, pos_d, embd_gpu, embd_qt, embd_rb, cache,
5319                                        n_vocab, cap_bucket_max, &mut tok_out)?;
5320        Ok(tok_out)
5321    }
5322
5323    /// CAPTURE body: argmax lands in the PERSISTENT `tok_out` (same buffer = same address on
5324    /// every replay; pass `token_d` itself for the self-feeding graph loop).
5325    #[allow(clippy::too_many_arguments)]
5326    pub fn gemma4_decode_step_dc_into(&self, e: &Engine, token_d: &CudaSlice<u32>,
5327                                      pos_d: &mut CudaSlice<i32>, embd_gpu: &CudaSlice<u8>,
5328                                      embd_qt: i32, embd_rb: usize, cache: &mut Cache,
5329                                      n_vocab: usize, cap_bucket_max: Option<(usize, usize)>,
5330                                      tok_out: &mut CudaSlice<u32>)
5331                                      -> Result<(), Box<dyn std::error::Error>> {
5332        let n_embd = self.cfg.n_embd as usize;
5333        let eps = self.cfg.rms_eps;
5334        let mut x = e.embed_gather_device(embd_gpu, token_d, n_embd, embd_qt, embd_rb)?;
5335        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
5336        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
5337        let n_layers = self.layers.len();
5338        for (il, layer) in self.layers.iter().enumerate() {
5339            let (hq, hdq) = match h_carry.take() {
5340                Some(p) => p,
5341                None => e.rms_norm_q8_1(&x, self.layers[0].attn_norm.float_data(), n_embd, 1, eps)?,
5342            };
5343            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
5344            let o = self.gemma4_decode_attn_dc(e, fa, il, &hq, &hdq, pos_d, cache, cap_bucket_max)?;
5345            let mut cur = e.uninit(n_embd)?;
5346            e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, 1, eps)?;
5347            let next_norm = if il + 1 < n_layers {
5348                Some(self.layers[il + 1].attn_norm.float_data())
5349            } else { None };
5350            let (xn, hn) = self.gemma4_layer_tail_add_nq(e, layer, &cur, &x, 1, next_norm)?;
5351            x = xn;
5352            h_carry = hn;
5353        }
5354        let mut hn = e.uninit(n_embd)?;
5355        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
5356        let mut logits = e.matmul(&self.output, &hn, 1)?;
5357        self.gemma4_suppress(e, &mut logits, 1)?;   // cap skipped (monotonic); the mask is not
5358        e.argmax_token_device_into(&logits, tok_out, n_vocab)?;
5359        e.inc_seqlen(pos_d)?;
5360        if cap_bucket_max.is_none() { cache.pos += 1; }
5361        Ok(())
5362    }
5363
5364    /// Persistent transient slots for the ALLOC-FREE captured dc step (the graph door):
5365    /// every buffer the step produces per token lives here, allocated ONCE pre-capture, so
5366    /// the captured graph carries zero cuMemAllocAsync/Free nodes (the 226us/launch tax,
5367    /// osrt 2026-07-23). Sized for the model's max per-layer shapes.
5368
5369    /// Build the slot set (call OUTSIDE any capture).
5370    pub fn g4_dc_slots(&self, e: &Engine) -> Result<G4DcSlots, Box<dyn std::error::Error>> {
5371        let n_embd = self.cfg.n_embd as usize;
5372        let n_vocab = self.output.out_features();
5373        let n_layers = self.layers.len();
5374        let (mut qmax, mut kvmax, mut ffmax) = (0usize, 0usize, 0usize);
5375        for il in 0..n_layers {
5376            let (hd, nkv, nh, _b, _s, _w) = self.gemma4_geom(il);
5377            qmax = qmax.max(nh * hd);
5378            kvmax = kvmax.max(nkv * hd);
5379            if let crate::hybrid::Ffn::Dense { ffn_gate, .. } = &self.layers[il].ffn {
5380                ffmax = ffmax.max(ffn_gate.out_features());
5381            }
5382        }
5383        Ok(G4DcSlots {
5384            x: e.uninit(n_embd)?, xn: e.uninit(n_embd)?, cur: e.uninit(n_embd)?,
5385            hq: e.alloc_i8_uninit(n_embd)?, hd_: e.uninit(n_embd / 32)?,
5386            q0: e.uninit(qmax)?, k0: e.uninit(kvmax)?, v0: e.uninit(kvmax)?,
5387            q: e.uninit(qmax)?, k: e.uninit(kvmax)?, v: e.uninit(kvmax)?,
5388            attn: e.uninit(qmax)?, o: e.uninit(n_embd)?,
5389            attn_out: e.uninit(n_embd)?, zsh: e.uninit(n_embd)?,
5390            // zq/zd feed the wo matvec (nh*hd rows — 4096 on hd512 globals > n_embd),
5391            // the ffn entry (n_embd) and the lm_head (n_embd): size for the max.
5392            zq: e.alloc_i8_uninit(n_embd.max(qmax))?, zd: e.uninit(n_embd.max(qmax) / 32)?,
5393            gate: e.uninit(ffmax)?, up: e.uninit(ffmax)?,
5394            act: e.uninit(ffmax)?, actq: e.alloc_i8_uninit(ffmax)?, actd: e.uninit(ffmax / 32)?,
5395            f0: e.uninit(n_embd)?, sn: e.uninit(n_embd)?,
5396            hn: e.uninit(n_embd)?, logits: e.uninit(n_vocab)?,
5397        })
5398    }
5399
5400    /// m=1 pre-quantized matvec into a slot — mirrors matmul_pre's m=1 mmvq route exactly
5401    /// (rp4-mirror bytes; mmvq_supports guaranteed for gemma4 q4_0/q6_K).
5402    fn g4_matvec_m1_into(&self, e: &Engine, w: &crate::model::GpuTensor,
5403                         aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, y: &mut CudaSlice<f32>)
5404                         -> Result<(), Box<dyn std::error::Error>> {
5405        use crate::model::GpuTensor;
5406        let (bytes, qtype, row_bytes, scale, rp) = match w {
5407            GpuTensor::Quant { bytes, qtype, row_bytes, scale, rp, .. } =>
5408                (bytes, *qtype, *row_bytes, *scale, *rp),
5409            _ => return Err("g4_matvec_m1_into: non-quant tensor".into()),
5410        };
5411        let (mbytes, mrp) = match w {
5412            GpuTensor::Quant { rp4: Some(m4), .. } => (m4, true),
5413            _ => (bytes, rp),
5414        };
5415        e.qmatvec_mmvq_into(mbytes, aq, ad, 1, w.in_features(), w.out_features(),
5416                            qtype, row_bytes, scale, mrp, y)
5417    }
5418
5419    /// ALLOC-FREE dc step (capture body): kernel-for-kernel mirror of
5420    /// `gemma4_decode_step_dc_into` at t=1 with every transient slot-fed. Dense gemma4 only
5421    /// (12B/31B; uniform q4_0 trunk guarantees the fused2/3 arms).
5422    #[allow(clippy::too_many_arguments)]
5423    pub fn gemma4_decode_step_dc_slotted(&self, e: &Engine, token_d: &CudaSlice<u32>,
5424                                         pos_d: &mut CudaSlice<i32>, embd_gpu: &CudaSlice<u8>,
5425                                         embd_qt: i32, embd_rb: usize, cache: &mut Cache,
5426                                         n_vocab: usize, cap_bucket_max: Option<(usize, usize)>,
5427                                         sl: &mut G4DcSlots, tok_out: &mut CudaSlice<u32>,
5428                                         ring: Option<(&mut CudaSlice<u32>, usize)>)
5429                                         -> Result<(), Box<dyn std::error::Error>> {
5430        let n_embd = self.cfg.n_embd as usize;
5431        let eps = self.cfg.rms_eps;
5432        e.embed_gather_device_into(embd_gpu, token_d, &mut sl.x, n_embd, embd_qt, embd_rb)?;
5433        e.scale_inplace(&mut sl.x, (n_embd as f32).sqrt(), n_embd)?;
5434        let n_layers = self.layers.len();
5435        let mut has_carry = false;
5436        for il in 0..n_layers {
5437            if !has_carry {
5438                e.rms_norm_q8_1_into(&sl.x, self.layers[il].attn_norm.float_data(), n_embd, 1,
5439                                     eps, &mut sl.hq, &mut sl.hd_)?;
5440            }
5441            has_carry = true;
5442            let layer = &self.layers[il];
5443            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
5444            self.gemma4_decode_attn_dc_slotted(e, fa, il, pos_d, cache, cap_bucket_max, sl)?;
5445            e.rms_norm(&sl.o, layer.post_attn_norm.float_data(), &mut sl.cur, n_embd, 1, eps)?;
5446            let next_norm = if il + 1 < n_layers {
5447                Some(self.layers[il + 1].attn_norm.float_data())
5448            } else { None };
5449            self.gemma4_layer_tail_slotted(e, layer, next_norm, sl)?;
5450            std::mem::swap(&mut sl.x, &mut sl.xn);
5451        }
5452        e.rms_norm(&sl.x, self.output_norm.float_data(), &mut sl.hn, n_embd, 1, eps)?;
5453        e.quantize_q8_1_into(&sl.hn, 1, n_embd, &mut sl.zq, &mut sl.zd)?;
5454        // lm_head via the same m=1 mmvq route as matmul (q6_K on the gemma4 family).
5455        {
5456            let (zq, zd) = (&sl.zq, &sl.zd);
5457            let zq = unsafe { &*(zq as *const CudaSlice<i8>) };
5458            let zd = unsafe { &*(zd as *const CudaSlice<f32>) };
5459            self.g4_matvec_m1_into(e, &self.output, zq, zd, &mut sl.logits)?;
5460        }
5461        self.gemma4_suppress(e, &mut sl.logits, 1)?;
5462        e.argmax_token_device_into(&sl.logits, tok_out, n_vocab)?;
5463        if let Some((ring, base)) = ring {
5464            // in-graph token parking: slot = (pos - base) % ring.len(); the door drains
5465            // with ONE sync per chunk instead of a per-token dtoh (the launch-serialization
5466            // fix — llama's 885us/launch overlaps GPU work because nothing waits per token).
5467            e.plain_tok_ring(tok_out, pos_d, base, ring)?;
5468        }
5469        e.inc_seqlen(pos_d)?;
5470        if cap_bucket_max.is_none() { cache.pos += 1; }
5471        Ok(())
5472    }
5473
5474    /// Slot-fed dc attention: mirrors gemma4_decode_attn_dc's CAPTURE arm kernel-for-kernel.
5475    #[allow(clippy::too_many_arguments)]
5476    fn gemma4_decode_attn_dc_slotted(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer,
5477                                     il: usize, pos_d: &CudaSlice<i32>, cache: &mut Cache,
5478                                     cap_bucket_max: Option<(usize, usize)>, sl: &mut G4DcSlots)
5479                                     -> Result<(), Box<dyn std::error::Error>> {
5480        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
5481        let eps = self.cfg.rms_eps;
5482        let aux = self.gemma4_aux.as_ref().unwrap();
5483        {
5484            let hq = unsafe { &*(&sl.hq as *const CudaSlice<i8>) };
5485            let hdq = unsafe { &*(&sl.hd_ as *const CudaSlice<f32>) };
5486            if swa {
5487                if !e.matmul_q4_fused3_into(&fa.wq, &fa.wk, &fa.wv, hq, hdq,
5488                                            &mut sl.q0, &mut sl.k0, &mut sl.v0)? {
5489                    return Err("slotted step: fused3 unavailable (non-uniform trunk)".into());
5490                }
5491            } else {
5492                if !e.matmul_q4_fused2_into(&fa.wq, &fa.wk, hq, hdq, &mut sl.q0, &mut sl.k0)? {
5493                    return Err("slotted step: fused2 unavailable".into());
5494                }
5495                let k0r = unsafe { &*(&sl.k0 as *const CudaSlice<f32>) };
5496                e.copy_into(&mut sl.v0, 0, k0r, nkv * hd)?;
5497            }
5498        }
5499        // E4B wave-3 fold, m=1 completion (2026-07-23) — MUST mirror the dc_into arm
5500        // kernel-for-kernel (graph stream-identity gate).
5501        let ff = if swa { None } else {
5502            Some(aux.rope_freqs.as_ref().expect("gemma4 global rope needs rope_freqs.weight"))
5503        };
5504        let kvl = cache.kv[il].as_mut().unwrap();
5505        let kv_fp8 = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
5506        if crate::Engine::qkv_append_on() {
5507            // append fold (2026-07-23): mirrors dc_into.
5508            e.rms_norm_qkv_rope_append_dc(&sl.q0, &sl.k0, &sl.v0, fa.q_norm.float_data(),
5509                fa.k_norm.float_data(), &aux.ones, &mut sl.q, &mut sl.k, &mut sl.v, hd, nh, nkv,
5510                pos_d, nh, nkv, base, 1.0, ff, eps,
5511                &mut kvl.k, &mut kvl.v, &kvl.len_d, kvl.k_tok_bytes, kvl.v_tok_bytes, kv_fp8)?;
5512        } else {
5513            e.rms_norm_qkv_rope(&sl.q0, &sl.k0, &sl.v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
5514                                &aux.ones, &mut sl.q, &mut sl.k, &mut sl.v, hd, nh, nkv,
5515                                pos_d, nh, nkv, base, 1.0, ff, eps)?;
5516            e.append_kv_quantized_dc(&sl.k, &sl.v, &mut kvl.k, &mut kvl.v, &kvl.len_d,
5517                                     kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes,
5518                                     kv_fp8)?;
5519        }
5520        e.inc_seqlen(&mut kvl.len_d)?;
5521        let (b_swa, b_glob) = cap_bucket_max.expect("slotted step is capture-only");
5522        let k_view = e.view_u8(&kvl.k, kvl.k.len());
5523        let v_view = e.view_u8(&kvl.v, kvl.v.len());
5524        let rows_on = std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0");
5525        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
5526        // combine-q8 emit (wave-5b m=1 port): the rows arms quantize inside the combine —
5527        // the standalone quantize launch runs only on the non-rows fallback. MUST mirror
5528        // the dc_into arm branch-for-branch (stream gate).
5529        let mut fa_q8 = false;
5530        if !swa && hd == 512 && b_glob >= crate::fa512_min_tkv() && rows_on {
5531            e.fa_decode_rows(&sl.q, &k_view, &v_view, &mut sl.attn, hd, nh, nkv, b_glob - 1,
5532                             1, scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
5533                             Some((&kvl.len_d, -1)), false, false,
5534                             Some((&mut sl.zq, &mut sl.zd)))?;
5535            fa_q8 = true;
5536        } else if swa && b_swa > win && hd == 256 && rows_on {
5537            e.fa_decode_rows_w(&sl.q, &k_view, &v_view, &mut sl.attn, hd, nh, nkv,
5538                               &kvl.len_d, -1, 1, scale, win,
5539                               kvl.k_tok_bytes, kvl.v_tok_bytes,
5540                               Some((&mut sl.zq, &mut sl.zd)))?;
5541            fa_q8 = true;
5542        } else {
5543            let b = if swa { b_swa } else { b_glob };
5544            e.fa_decode_dc(&sl.q, &k_view, &v_view, &mut sl.attn, hd, nh, nkv, &kvl.len_d, b,
5545                           scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
5546                           swa && crate::Engine::wkv_on())?;
5547        }
5548        if !fa_q8 {
5549            let aq = unsafe { &*(&sl.attn as *const CudaSlice<f32>) };
5550            e.quantize_q8_1_into(aq, 1, nh * hd, &mut sl.zq, &mut sl.zd)?;
5551        }
5552        {
5553            let zq = unsafe { &*(&sl.zq as *const CudaSlice<i8>) };
5554            let zd = unsafe { &*(&sl.zd as *const CudaSlice<f32>) };
5555            self.g4_matvec_m1_into(e, &fa.wo, zq, zd, &mut sl.o)?;
5556        }
5557        Ok(())
5558    }
5559
5560    /// Slot-fed dense layer tail: mirrors gemma4_layer_tail_core (t=1, no pre-norm fold) +
5561    /// tail_add_nq kernel-for-kernel; the next layer's (hq, hd_) carry lands in the slots.
5562    fn gemma4_layer_tail_slotted(&self, e: &Engine, layer: &crate::hybrid::HybridLayer,
5563                                 next_norm: Option<&CudaSlice<f32>>, sl: &mut G4DcSlots)
5564                                 -> Result<(), Box<dyn std::error::Error>> {
5565        let n_embd = self.cfg.n_embd as usize;
5566        let eps = self.cfg.rms_eps;
5567        let bits = layer.gemma4.as_ref().unwrap();
5568        let crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } = &layer.ffn
5569        else { return Err("slotted tail: dense ffn only".into()) };
5570        e.add_rms_norm(&sl.cur, &sl.x, bits.ffn_norm.float_data(), &mut sl.attn_out,
5571                       &mut sl.zsh, n_embd, 1, eps)?;
5572        let n_ff = ffn_gate.out_features();
5573        {
5574            let zshr = unsafe { &*(&sl.zsh as *const CudaSlice<f32>) };
5575            e.quantize_q8_1_into(zshr, 1, n_embd, &mut sl.zq, &mut sl.zd)?;
5576        }
5577        {
5578            let zq = unsafe { &*(&sl.zq as *const CudaSlice<i8>) };
5579            let zd = unsafe { &*(&sl.zd as *const CudaSlice<f32>) };
5580            if !e.matmul_q4_fused2_into(ffn_gate, ffn_up, zq, zd, &mut sl.gate, &mut sl.up)? {
5581                return Err("slotted tail: ffn fused2 unavailable".into());
5582            }
5583        }
5584        debug_assert!(e.uses_q8_1_fast(ffn_down));
5585        {
5586            let upr = unsafe { &*(&sl.up as *const CudaSlice<f32>) };
5587            let upv = e.view(upr, n_ff);
5588            let up_all = upv.slice(0..n_ff);
5589            let gr = unsafe { &*(&sl.gate as *const CudaSlice<f32>) };
5590            e.gelu_tanh_mul_q8_1_into(gr, &up_all, &mut sl.act, n_ff, 1,
5591                                      &mut sl.actq, &mut sl.actd)?;
5592        }
5593        {
5594            let aq = unsafe { &*(&sl.actq as *const CudaSlice<i8>) };
5595            let ad = unsafe { &*(&sl.actd as *const CudaSlice<f32>) };
5596            self.g4_matvec_m1_into(e, ffn_down, aq, ad, &mut sl.f0)?;
5597        }
5598        e.rms_norm(&sl.f0, bits.post_ffw_norm.float_data(), &mut sl.sn, n_embd, 1, eps)?;
5599        match next_norm {
5600            Some(w) => {
5601                e.add_scale_rms_norm_q8_1_into(&sl.sn, &sl.attn_out, bits.layer_scale, w,
5602                                               &mut sl.xn, n_embd, 1, eps,
5603                                               &mut sl.hq, &mut sl.hd_)?;
5604            }
5605            None => {
5606                e.add_scale(&sl.sn, &sl.attn_out, bits.layer_scale, &mut sl.xn, n_embd)?;
5607            }
5608        }
5609        Ok(())
5610    }
5611
5612    /// dc attention: same math as gemma4_decode_attn, KV slot/lengths from device counters.
5613    #[allow(clippy::too_many_arguments)]
5614    fn gemma4_decode_attn_dc(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer, il: usize,
5615                             hq: &CudaSlice<i8>, hdq: &CudaSlice<f32>,
5616                             pos_d: &CudaSlice<i32>, cache: &mut Cache,
5617                             cap_bucket_max: Option<(usize, usize)>)
5618                             -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
5619        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
5620        let eps = self.cfg.rms_eps;
5621        let aux = self.gemma4_aux.as_ref().unwrap();
5622        let (q0, k0, v0) = if swa {
5623            match e.matmul_q4_fused3(&fa.wq, &fa.wk, &fa.wv, hq, hdq)? {
5624                Some(t3) => t3,
5625                None => {
5626                    let h0 = e.zeros(0)?;
5627                    (e.matmul_pre(&fa.wq, hq, hdq, &h0, 1)?,
5628                     e.matmul_pre(&fa.wk, hq, hdq, &h0, 1)?,
5629                     e.matmul_pre(&fa.wv, hq, hdq, &h0, 1)?)
5630                }
5631            }
5632        } else {
5633            let (q0, k0) = match e.matmul_q4_fused2(&fa.wq, &fa.wk, hq, hdq)? {
5634                Some(p) => p,
5635                None => {
5636                    let h0 = e.zeros(0)?;
5637                    (e.matmul_pre(&fa.wq, hq, hdq, &h0, 1)?,
5638                     e.matmul_pre(&fa.wk, hq, hdq, &h0, 1)?)
5639                }
5640            };
5641            let v0 = e.clone_dtod(&k0)?;
5642            (q0, k0, v0)
5643        };
5644        let mut q = e.uninit(nh * hd)?;
5645        let mut k = e.uninit(nkv * hd)?;
5646        let mut v = e.uninit(nkv * hd)?;
5647        // E4B wave-3 fold, m=1 completion (2026-07-23): mirrored by the slotted arm.
5648        let ff = if swa { None } else {
5649            Some(aux.rope_freqs.as_ref().expect("gemma4 global rope needs rope_freqs.weight"))
5650        };
5651        let kvl = cache.kv[il].as_mut().unwrap();
5652        let kv_fp8 = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
5653        if crate::Engine::qkv_append_on() {
5654            // append fold (2026-07-23): norm+rope+cache-append in ONE launch.
5655            e.rms_norm_qkv_rope_append_dc(&q0, &k0, &v0, fa.q_norm.float_data(),
5656                fa.k_norm.float_data(), &aux.ones, &mut q, &mut k, &mut v, hd, nh, nkv,
5657                pos_d, nh, nkv, base, 1.0, ff, eps,
5658                &mut kvl.k, &mut kvl.v, &kvl.len_d, kvl.k_tok_bytes, kvl.v_tok_bytes, kv_fp8)?;
5659        } else {
5660            e.rms_norm_qkv_rope(&q0, &k0, &v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
5661                                &aux.ones, &mut q, &mut k, &mut v, hd, nh, nkv,
5662                                pos_d, nh, nkv, base, 1.0, ff, eps)?;
5663            e.append_kv_quantized_dc(&k, &v, &mut kvl.k, &mut kvl.v, &kvl.len_d,
5664                                     kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes, kv_fp8)?;
5665        }
5666        e.inc_seqlen(&mut kvl.len_d)?;
5667        let mut attn = e.uninit(nh * hd)?;
5668        // combine-q8 emit carrier (wave-5b m=1 port): rows arms fill this; the tail then
5669        // rides g4_matvec_m1_into instead of matmul's internal quantize.
5670        let mut fa_q8: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
5671        // Weight prefetch NOT wired here (26B/31B/qwen probes 2026-07-13: 26B flat
5672        // 196.6/196.0 vs 196.4/196.1 — MoE ffn dilutes wo; 31B −0.2% — dense decode sits
5673        // at the DRAM wall, no idle window to front-load into). E4B keeps the arm
5674        // (gemma4_e4b_attn, +0.65% valid window).
5675        match cap_bucket_max {
5676            None => {
5677                // dc-EAGER: host knows the length — R6 window views EXACTLY like the eager
5678                // decode (SWA layers attend the last `sliding_window` keys); the device
5679                // counters carry only the append slot + the graph seam.
5680                kvl.len += 1;
5681                let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
5682                if !swa && hd == 512 && kvl.len >= crate::fa512_min_tkv()
5683                    && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
5684                    // global layers: SAME hd512 rows twin as verify, t=1 (parity law).
5685                    // dc: len_d is live (inc_seqlen) — device-len rides it, plus=-1.
5686                    let kp = e.view_u8(&kvl.k, kvl.len * kvl.k_tok_bytes);
5687                    let vp = e.view_u8(&kvl.v, kvl.len * kvl.v_tok_bytes);
5688                    let (mut aq8, mut ad8) = e.uninit_q8_pair(nh * hd)?;
5689                    e.fa_decode_rows(&q, &kp, &vp, &mut attn, hd, nh, nkv, kvl.len - 1, 1,
5690                                     scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
5691                                     Some((&kvl.len_d, -1)), false, false,
5692                                     Some((&mut aq8, &mut ad8)))?;
5693                    fa_q8 = Some((aq8, ad8));
5694                } else if swa && kvl.len > win && hd == 256
5695                    && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
5696                    // windowed regime: SAME rows_w kernel as verify, t=1 (parity law).
5697                    let kp = e.view_u8(&kvl.k, kvl.len * kvl.k_tok_bytes);
5698                    let vp = e.view_u8(&kvl.v, kvl.len * kvl.v_tok_bytes);
5699                    let (mut aq8, mut ad8) = e.uninit_q8_pair(nh * hd)?;
5700                    e.fa_decode_rows_w(&q, &kp, &vp, &mut attn, hd, nh, nkv, &kvl.len_d, -1,
5701                                       1, scale, win, kvl.k_tok_bytes, kvl.v_tok_bytes,
5702                                       Some((&mut aq8, &mut ad8)))?;
5703                    fa_q8 = Some((aq8, ad8));
5704                } else {
5705                    let (off_tok, t_kv) = if swa && kvl.len > win { (kvl.len - win, win) }
5706                                          else { (0, kvl.len) };
5707                    let k_view = e.view_u8_range(&kvl.k, off_tok * kvl.k_tok_bytes,
5708                                                 (off_tok + t_kv) * kvl.k_tok_bytes);
5709                    let v_view = e.view_u8_range(&kvl.v, off_tok * kvl.v_tok_bytes,
5710                                                 (off_tok + t_kv) * kvl.v_tok_bytes);
5711                    e.fa_decode_kvmod(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, t_kv, scale,
5712                                kvl.k_tok_bytes, kvl.v_tok_bytes, swa && crate::Engine::wkv_on())?;
5713                }
5714            }
5715            Some((b_swa, b_glob)) => {
5716                // capture: full-buffer views + device length. PARITY LAW port (graph arc step
5717                // 3): the SAME regime branches as dc-eager, pinned per arm — b_swa is the
5718                // exact-key t_kv for the dc family (n_splits constant per bucket), b_glob is
5719                // the RUNG max for the rows family (kernels derive per-replay splits from
5720                // kvl.len_d; grid/partials sized for the rung end, excess splits exit).
5721                let k_view = e.view_u8(&kvl.k, kvl.k.len());
5722                let v_view = e.view_u8(&kvl.v, kvl.v.len());
5723                let rows_on = std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0");
5724                let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
5725                if !swa && hd == 512 && b_glob >= crate::fa512_min_tkv() && rows_on {
5726                    let (mut aq8, mut ad8) = e.uninit_q8_pair(nh * hd)?;
5727                    e.fa_decode_rows(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, b_glob - 1,
5728                                     1, scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
5729                                     Some((&kvl.len_d, -1)), false, false,
5730                                     Some((&mut aq8, &mut ad8)))?;
5731                    fa_q8 = Some((aq8, ad8));
5732                } else if swa && b_swa > win && hd == 256 && rows_on {
5733                    let (mut aq8, mut ad8) = e.uninit_q8_pair(nh * hd)?;
5734                    e.fa_decode_rows_w(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
5735                                       &kvl.len_d, -1, 1, scale, win,
5736                                       kvl.k_tok_bytes, kvl.v_tok_bytes,
5737                                       Some((&mut aq8, &mut ad8)))?;
5738                    fa_q8 = Some((aq8, ad8));
5739                } else {
5740                    let b = if swa { b_swa } else { b_glob };
5741                    e.fa_decode_dc(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, &kvl.len_d, b,
5742                                   scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
5743                                   swa && crate::Engine::wkv_on())?;
5744                }
5745            }
5746        }
5747        // combine-q8 emit (wave-5b m=1 port): the rows arms produced the wo activation pair
5748        // in-combine — ride the slotted arm's exact matvec route (parity by construction).
5749        if let Some((aq8, ad8)) = fa_q8 {
5750            let mut y = e.uninit(fa.wo.out_features())?;
5751            self.g4_matvec_m1_into(e, &fa.wo, &aq8, &ad8, &mut y)?;
5752            return Ok(y);
5753        }
5754        Ok(e.matmul(&fa.wo, &attn, 1)?)
5755    }
5756
5757    /// gemma4 GRAPH-REPLAY greedy loop: per (swa-key, global-key) fa bucket, capture ONE full
5758    /// dc step (self-feeding: argmax writes token_d in-graph) and replay it — one graph launch
5759    /// per token, one 4B dtoh. V1 scope: whole generation under the sliding window (no window
5760    /// views in-graph); caller gates and falls back to the dc-eager loop.
5761    pub fn gemma4_generate_graph(&self, e: &Engine, prompt_pos: usize, first_token: u32,
5762                                 cache: &mut Cache, max_new: usize, eos: &[u32],
5763                                 mut on_token: impl FnMut(u32) -> bool)
5764                                 -> Result<(Vec<u32>, crate::decode::StopReason), Box<dyn std::error::Error>> {
5765        if self.is_gemma4_e4b() {
5766            return Err("E4B graph serving is unwired (HANDOVER-E4B.md) — dc-eager is the serving arm".into());
5767        }
5768        use crate::decode::StopReason;
5769        let n_vocab = self.output.out_features();
5770        let n_embd = self.cfg.n_embd as usize;
5771        let embd_gpu = self.embd_gpu.get_or_init(|| {
5772            e.upload_u8(&self.embd.raw).expect("embed table upload")
5773        });
5774        let (qt, rb) = self.embd.qt_and_row_bytes(n_embd);
5775        for kvl in cache.kv.iter_mut().flatten() {
5776            e.set_i32_one(&mut kvl.len_d, kvl.len as i32)?;
5777        }
5778        let mut token_d = e.stream().clone_htod(&[first_token])?;
5779        let mut pos_d = e.htod_i32(&[prompt_pos as i32])?;
5780        let g4 = self.cfg.gemma4.as_ref().unwrap();
5781        let (hd_s, hd_g) = (g4.key_length_swa as usize, g4.key_length_global as usize);
5782        // per-layer nkv: swa vs global counts from the pattern (uniform within class).
5783        let nkv_s = g4.head_count_kv.iter().zip(g4.swa_pattern.iter())
5784            .find(|p| *p.1).map(|p| *p.0 as usize).unwrap_or(8);
5785        let nkv_g = g4.head_count_kv.iter().zip(g4.swa_pattern.iter())
5786            .find(|p| !*p.1).map(|p| *p.0 as usize).unwrap_or(2);
5787        let mut graphs: std::collections::HashMap<((bool, usize), (bool, usize), bool, bool),
5788                                                  (cudarc::driver::CudaGraph,
5789                                                   Vec<Box<dyn std::any::Any + Send>>)> = Default::default();
5790        // ALLOC-FREE capture: persistent transient slots — the captured graph carries zero
5791        // mem nodes (the 226us/launch tax). Slots must outlive every cached graph.
5792        let mut slots = self.g4_dc_slots(e)?;
5793        // Chunked replay ring: tokens park on-device; ONE drain sync per chunk. ring_base is
5794        // baked at the door entry (the modulo keeps every capture valid indefinitely).
5795        const RING: usize = 64;
5796        // DRAIN pinned at 1 (2026-07-23): relaunching the SAME graph exec before its prior
5797        // launch completes is ILLEGAL (chunk=4 -> ILLEGAL_ADDRESS; chunk=1 clean). Pipelining
5798        // needs alternating exec instances — and the measured payoff was ~0 (the ~200us
5799        // cuGraphLaunch host cost already overlaps its own launch's GPU work; llama pays
5800        // 885us/launch the same way). MEMRA_GRAPH_DRAIN raises it only for experiments.
5801        const DRAIN: usize = 1;
5802        let mut ring = e.stream().alloc_zeros::<u32>(RING)?;
5803        let ring_base = prompt_pos;
5804        let mut out = Vec::with_capacity(max_new);
5805        let mut reason = StopReason::MaxNew;
5806        let mut next = first_token;
5807        let mut captures = 0usize;
5808        for _ in 0..max_new {
5809            out.push(next);
5810            if eos.contains(&next) { reason = StopReason::Eos; break; }
5811            if !on_token(next) { reason = StopReason::Callback; break; }
5812            let t_kv = cache.pos + 1;
5813            // Bucket key per ARM (graph arc step 3):
5814            //  - swa component: dc family under the window (exact (fa_vec, n_splits) key —
5815            //    n_splits must be constant per bucket); rows_w above it (window-constant, so
5816            //    the component collapses to a single marker).
5817            //  - global component: dc family under the fa512 floor (exact key); rows_dpl16
5818            //    at/above it — the kernel derives splits from len_d per replay, so buckets
5819            //    are power-of-2 RUNGS (one capture per doubling; grid sized for the rung end).
5820            let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
5821            let f512 = crate::fa512_min_tkv();
5822            let key_s = if t_kv > win { (true, usize::MAX) }
5823                        else { e.fa_bucket_key(t_kv, hd_s, nkv_s, crate::Engine::wkv_on()) };
5824            let (key_g, rung_end) = if t_kv >= f512 {
5825                // strict upper bound: the rung must ROLL at exact powers (t_kv==1024 starts
5826                // the [1024,2048) bucket) — sizing covers every replayed T_kv < end.
5827                let end = (t_kv + 1).next_power_of_two().max(f512 * 2);
5828                ((true, end), end)
5829            } else { (e.fa_bucket_key(t_kv, hd_g, nkv_g, false), t_kv) };
5830            let key = (key_s, key_g, t_kv >= f512, t_kv > win);
5831            if !graphs.contains_key(&key) {
5832                let bucket_max = (t_kv, rung_end);
5833                // snapshot device+host state (the 3 capture-warmup runs leave no residue).
5834                let snap = cache.snapshot(e)?;
5835                let pos_save = e.dtoh_i32_one(&pos_d)?;
5836                let len_save: Vec<Option<i32>> = cache.kv.iter()
5837                    .map(|k| k.as_ref().map(|kvl| e.dtoh_i32_one(&kvl.len_d).unwrap())).collect();
5838                let tok_save = e.dtoh_u32_one(&token_d)?;
5839                // RETAINED capture (2026-07-23): the plain capture_graph left pool-transient
5840                // clones as dead COPY NODES replayed every launch — the E4B 0.74ms/token
5841                // regression class, and this door's measured -8.8%. The keeper pins warmup
5842                // transients so the captured graph holds kernel nodes only.
5843                let graph = {
5844                    let tok_ref = &mut token_d;
5845                    let pos_ref = &mut pos_d;
5846                    let cache_ref = &mut *cache;
5847                    let slots_ref = &mut slots;
5848                    let ring_ref = &mut ring;
5849                    e.capture_graph_retained_flags(
5850                        cudarc::driver::sys::CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_USE_NODE_PRIORITY,
5851                        |e| {
5852                        // self-feeding: the argmax writes token_d itself.
5853                        let tok_in = unsafe { &*(tok_ref as *const CudaSlice<u32>) };
5854                        let sl = unsafe { &mut *(slots_ref as *mut G4DcSlots) };
5855                        let rg = unsafe { &mut *(ring_ref as *mut CudaSlice<u32>) };
5856                        self.gemma4_decode_step_dc_slotted(e, tok_in, pos_ref, embd_gpu, qt, rb,
5857                                                           cache_ref, n_vocab, Some(bucket_max),
5858                                                           sl, tok_ref, Some((rg, ring_base)))
5859                    })?
5860                };
5861                cache.rollback(e, &snap, 0)?;
5862                e.set_i32_one(&mut pos_d, pos_save)?;
5863                for (il, ls) in len_save.iter().enumerate() {
5864                    if let (Some(kvl), Some(v)) = (cache.kv[il].as_mut(), ls) {
5865                        e.set_i32_one(&mut kvl.len_d, *v)?;
5866                    }
5867                }
5868                e.set_u32_one(&mut token_d, tok_save)?;
5869                if std::env::var("MEMRA_GRAPH_CENSUS").as_deref() == Ok("1") {
5870                    if let Ok(c) = crate::graph_update::node_census(&graph.0) {
5871                        eprintln!("[graph-census] {c:?}");
5872                    }
5873                }
5874                graphs.insert(key, graph);
5875                captures += 1;
5876            }
5877            // CHUNKED REPLAY: enqueue up to DRAIN launches back-to-back (the ~200us host
5878            // cost of each cuGraphLaunch overlaps the PREVIOUS launch's ~11ms GPU work),
5879            // then ONE sync + ring drain. The chunk must stay inside this bucket key and
5880            // the budget; capture warmups already emitted their tokens through the ring.
5881            let mut chunk = 1usize;
5882            let drain_cap: usize = std::env::var("MEMRA_GRAPH_DRAIN").ok()
5883                .and_then(|v| v.parse().ok()).unwrap_or(DRAIN);
5884            while chunk < drain_cap && out.len() + chunk < max_new {
5885                let t_next = cache.pos + 1 + chunk;
5886                let key_s2 = if t_next > win { (true, usize::MAX) }
5887                             else { e.fa_bucket_key(t_next, hd_s, nkv_s, crate::Engine::wkv_on()) };
5888                let key_g2 = if t_next >= f512 {
5889                    (true, (t_next + 1).next_power_of_two().max(f512 * 2))
5890                } else { e.fa_bucket_key(t_next, hd_g, nkv_g, false) };
5891                if (key_s2, key_g2, t_next >= f512, t_next > win) != key { break; }
5892                chunk += 1;
5893            }
5894            let g = &graphs.get(&key).unwrap().0;
5895            for _ in 0..chunk { g.launch()?; }
5896            e.stream().synchronize()?;
5897            let ringh = e.dtoh_u32(&ring)?;
5898            for j in 0..chunk {
5899                let pos_j = cache.pos + j;
5900                let tok_j = ringh[(pos_j - ring_base) % RING];
5901                cache.pos += 0; // advanced below in one shot
5902                if j + 1 == chunk { next = tok_j; }
5903                else {
5904                    out.push(tok_j);
5905                    if eos.contains(&tok_j) || !on_token(tok_j) {
5906                        reason = if eos.contains(&tok_j) { StopReason::Eos }
5907                                 else { StopReason::Callback };
5908                        // roll device/host state back to the stop point.
5909                        let keep = cache.pos + j + 1;
5910                        e.set_i32_one(&mut pos_d, keep as i32)?;
5911                        for kvl in cache.kv.iter_mut().filter_map(|k| k.as_mut()) {
5912                            e.set_i32_one(&mut kvl.len_d, keep as i32)?;
5913                            kvl.len = keep;
5914                        }
5915                        cache.pos = keep;
5916                        if std::env::var("MEMRA_GRAPH_STATS").is_ok() {
5917                            eprintln!("[gemma-graph] captures={captures} buckets={}", graphs.len());
5918                        }
5919                        return Ok((out, reason));
5920                    }
5921                }
5922            }
5923            cache.pos += chunk;
5924            for kvl in cache.kv.iter_mut().filter_map(|k| k.as_mut()) { kvl.len += chunk; }
5925        }
5926        if std::env::var("MEMRA_GRAPH_STATS").is_ok() {
5927            eprintln!("[gemma-graph] captures={captures} buckets={}", graphs.len());
5928        }
5929        Ok((out, reason))
5930    }
5931
5932    /// gemma4 VERIFY step (spec decode): t tokens batched through the trunk at positions
5933    /// pos0..pos0+t-1, K/V rows appended to the quantized cache (caller rolls back rejected
5934    /// rows via kvl.len), per-token causal windowed attend (fa_decode per token — the same
5935    /// kernel family as T=1 decode at each token's t_kv). Returns [t, n_vocab] softcapped
5936    /// logits (host) + advances cache.pos by t.
5937    pub(crate) fn gemma4_decode_step_t(&self, e: &Engine, tokens: &[u32], pos0: usize,
5938                                       cache: &mut Cache)
5939                                       -> Result<Vec<f32>, Box<dyn std::error::Error>> {
5940        Ok(self.gemma4_decode_step_t_h(e, tokens, pos0, cache)?.0)
5941    }
5942
5943    /// GREEDY verify: per-row DEVICE argmax (t x 4B host traffic instead of the t x 1MB logits
5944    /// stack — softcap skipped: tanh is monotonic, per-row argmax unaffected). Returns
5945    /// (argmax ids [t], post-output_norm hidden stack [t, n_embd]).
5946    pub(crate) fn gemma4_decode_step_t_am(&self, e: &Engine, tokens: &[u32], pos0: usize,
5947                                          cache: &mut Cache)
5948                                          -> Result<(Vec<u32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
5949        let (ld, hn) = self.gemma4_verify_trunk(e, tokens, pos0, cache, None)?;
5950        let t = tokens.len();
5951        let n_vocab = self.output.out_features();
5952        let mut toks = e.stream().alloc_zeros::<u32>(t)?;
5953        for i in 0..t {
5954            e.argmax_token_device_col(&ld, i, n_vocab, &mut toks, i)?;
5955        }
5956        Ok((e.dtoh_u32(&toks)?, hn))
5957    }
5958
5959    /// Device-token verify (async spec round): tokens live in tok_d[0..t]; per-row argmax
5960    /// lands in a DEVICE buffer (no host logits). Returns (vam_d [t] u32 device, hn stack).
5961    pub(crate) fn gemma4_decode_step_t_am_dev(&self, e: &Engine, tok_d: &CudaSlice<u32>, t: usize,
5962                                              pos0: usize, cache: &mut Cache)
5963                                              -> Result<(CudaSlice<u32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
5964        let (ld, hn) = self.gemma4_verify_trunk(e, &vec![0u32; t], pos0, cache, Some(tok_d))?;
5965        let n_vocab = self.output.out_features();
5966        let mut vam = e.stream().alloc_zeros::<u32>(t)?;
5967        for i in 0..t {
5968            e.argmax_token_device_col(&ld, i, n_vocab, &mut vam, i)?;
5969        }
5970        Ok((vam, hn))
5971    }
5972
5973    /// gemma4 verify + the POST-output_norm hidden stack [t, n_embd] (the drafter's h input —
5974    /// llama's h_nextn convention).
5975    pub(crate) fn gemma4_decode_step_t_h(&self, e: &Engine, tokens: &[u32], pos0: usize,
5976                                         cache: &mut Cache)
5977                                         -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
5978        let (mut ld, hn) = self.gemma4_verify_trunk(e, tokens, pos0, cache, None)?;
5979        let t = tokens.len();
5980        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
5981        e.softcap(&mut ld, cap, t * self.output.out_features())?;
5982        Ok((e.dtoh(&ld)?, hn))
5983    }
5984
5985    /// Persistent device scratch for the BURST verify stream (one per generation): rope pos
5986    /// rows [cap] + per-row t_kv counters (device-filled from the round counter, zero H2D).
5987    pub(crate) fn verify_stream_scratch(&self, e: &Engine, cap: usize)
5988                                        -> Result<VerifyStreamScratch, Box<dyn std::error::Error>> {
5989        Ok(VerifyStreamScratch {
5990            pos_d: e.htod_i32(&vec![0i32; cap])?,
5991            row_ctrs: (0..cap).map(|_| e.htod_i32(&[0])).collect::<Result<_, _>>()?,
5992        })
5993    }
5994
5995    /// BURST verify trunk (device-slot twin): tokens from a device buffer, rope positions
5996    /// iota'd from `ctr`, appends/attention at the layers' len_d counters (verify_attn_stream),
5997    /// NO host cache.pos/len advance. Returns (per-row device argmaxes [t], post-norm hidden
5998    /// stack [t, n_embd]) — the burst's accept/seed inputs, zero host readbacks.
5999    /// `scr` = PERSISTENT per-gen scratch (pos rows + per-row t_kv counters): the first cut
6000    /// htod_i32-allocated these per call — 9 pageable H2D copies per round, each a stream
6001    /// sync, exactly the turnaround the burst exists to remove.
6002    pub(crate) fn gemma4_verify_t_am_stream(&self, e: &Engine, tok_d: &CudaSlice<u32>, t: usize,
6003                                            ctr: &CudaSlice<i32>, hint: usize,
6004                                            cache: &mut Cache,
6005                                            scr: &mut VerifyStreamScratch)
6006                                            -> Result<(CudaSlice<u32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6007        let n_embd = self.cfg.n_embd as usize;
6008        let eps = self.cfg.rms_eps;
6009        assert!(t <= scr.row_ctrs.len() && t <= 64);
6010        e.i32_iota_from(ctr, &mut scr.pos_d, t)?;
6011        for i in 0..t {
6012            e.i32_copy_add(ctr, &mut scr.row_ctrs[i], (i + 1) as i32)?;
6013        }
6014        let (pos_d, row_ctrs) = (&scr.pos_d, &scr.row_ctrs);
6015        let embd_gpu = self.embd_gpu.get_or_init(|| {
6016            e.upload_u8(&self.embd.raw).expect("embed table upload")
6017        });
6018        let (qt, rb) = self.embd.qt_and_row_bytes(n_embd);
6019        let mut x = e.embed_gather_device_td(embd_gpu, tok_d, t, n_embd, qt, rb)?;
6020        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
6021        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
6022        let n_layers = self.layers.len();
6023        for (il, layer) in self.layers.iter().enumerate() {
6024            let (hq, hdq) = match h_carry.take() {
6025                Some(p) => p,
6026                None => e.rms_norm_q8_1(&x, self.layers[0].attn_norm.float_data(), n_embd, t, eps)?,
6027            };
6028            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
6029            let o = self.gemma4_verify_attn_stream(e, fa, il, &hq, &hdq, pos_d, t, cache,
6030                                                    hint, row_ctrs)?;
6031            let mut cur = e.uninit(t * n_embd)?;
6032            e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, t, eps)?;
6033            let next_norm = if il + 1 < n_layers {
6034                Some(self.layers[il + 1].attn_norm.float_data())
6035            } else { None };
6036            let (xn, hn) = self.gemma4_layer_tail_add_nq(e, layer, &cur, &x, t, next_norm)?;
6037            x = xn;
6038            h_carry = hn;
6039            self.dflash_tap(e, cache, il, &x, t)?;
6040        }
6041        let mut hn = e.uninit(t * n_embd)?;
6042        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
6043        let ld = e.matmul(&self.output, &hn, t)?;
6044        let n_vocab = self.output.out_features();
6045        let mut vam = e.stream().alloc_zeros::<u32>(t)?;
6046        for i in 0..t {
6047            e.argmax_token_device_col(&ld, i, n_vocab, &mut vam, i)?;
6048        }
6049        Ok((vam, hn))
6050    }
6051
6052    /// Verify trunk core: returns (UN-softcapped logits device [t, n_vocab], post-output_norm
6053    /// hidden stack [t, n_embd]); appends KV rows + advances cache.pos.
6054    /// DFlash tap write (dflash lane): copy the post-layer residual rows of `x` into the
6055    /// armed sink at the tap slot for `il` (row-major [t, n_taps*hidden]). Per-row D2D
6056    /// copies — t <= block_size on verify; prime pays t*n_taps once per prompt (dedicated
6057    /// kernel later if it shows in the profile).
6058    fn dflash_tap(&self, e: &Engine, cache: &mut Cache, il: usize, x: &CudaSlice<f32>, t: usize)
6059                  -> Result<(), Box<dyn std::error::Error>> {
6060        let Some(taps) = cache.dflash_taps.as_mut() else { return Ok(()) };
6061        let Some(slot) = taps.layer_ids.iter().position(|&l| l == il) else { return Ok(()) };
6062        let h = taps.hidden;
6063        let n_taps = taps.layer_ids.len();
6064        debug_assert_eq!(taps.t, t);
6065        let xv = e.view(x, t * h);
6066        for r in 0..t {
6067            let row = xv.slice(r * h..(r + 1) * h);
6068            e.copy_view_into(&mut taps.buf, r * n_taps * h + slot * h, &row, h)?;
6069        }
6070        Ok(())
6071    }
6072
6073    fn gemma4_verify_trunk(&self, e: &Engine, tokens: &[u32], pos0: usize, cache: &mut Cache,
6074                           tok_dev: Option<&CudaSlice<u32>>)
6075                           -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6076        let n_embd = self.cfg.n_embd as usize;
6077        let eps = self.cfg.rms_eps;
6078        let t = tokens.len();
6079        let pos: Vec<i32> = (0..t).map(|i| (pos0 + i) as i32).collect();
6080        let pos_d = e.htod_i32(&pos)?;
6081        let mut x = match tok_dev {
6082            Some(td) => {
6083                let embd_gpu = self.embd_gpu.get_or_init(|| {
6084                    e.upload_u8(&self.embd.raw).expect("embed table upload")
6085                });
6086                let (qt, rb) = self.embd.qt_and_row_bytes(n_embd);
6087                e.embed_gather_device_td(embd_gpu, td, t, n_embd, qt, rb)?
6088            }
6089            None => e.htod(&self.embd.gather(n_embd, tokens))?,
6090        };
6091        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
6092        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
6093        let n_layers = self.layers.len();
6094        for (il, layer) in self.layers.iter().enumerate() {
6095            let (hq, hdq) = match h_carry.take() {
6096                Some(p) => p,
6097                None => e.rms_norm_q8_1(&x, self.layers[0].attn_norm.float_data(), n_embd, t, eps)?,
6098            };
6099            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
6100            let o = self.gemma4_verify_attn(e, fa, il, &hq, &hdq, &pos_d, t, cache)?;
6101            let mut cur = e.uninit(t * n_embd)?;
6102            e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, t, eps)?;
6103            let next_norm = if il + 1 < n_layers {
6104                Some(self.layers[il + 1].attn_norm.float_data())
6105            } else { None };
6106            let (xn, hn) = self.gemma4_layer_tail_add_nq(e, layer, &cur, &x, t, next_norm)?;
6107            x = xn;
6108            h_carry = hn;
6109            self.dflash_tap(e, cache, il, &x, t)?;
6110        }
6111        let mut hn = e.uninit(t * n_embd)?;
6112        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
6113        let mut ld = e.matmul(&self.output, &hn, t)?;
6114        self.gemma4_suppress(e, &mut ld, t)?;   // before the per-row argmax consumers
6115        cache.pos += t;
6116        Ok((ld, hn))
6117    }
6118
6119    /// Verify attention: project/norm/rope t rows, append them to the cache, then attend each
6120    /// row causally over [win_off_i .. base+i] via the SAME fa_decode dispatch as T=1 decode.
6121    /// BURST verify attention (device-slot twin of `gemma4_verify_attn`): the append lands
6122    /// at the layer's len_d counter (rows_dc), attention bases ride the counter (rows /
6123    /// rows_w with base_dev), and NO host len is read or bumped — `hint` is a host UPPER
6124    /// bound on base_len used only for split sizing and arm gating (the burst loop passes
6125    /// pos0 + burst slack). Host len mirrors re-sync at the burst drain.
6126    #[allow(clippy::too_many_arguments)]
6127    fn gemma4_verify_attn_stream(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer, il: usize,
6128                                 hq: &CudaSlice<i8>, hdq: &CudaSlice<f32>,
6129                                 pos_d: &CudaSlice<i32>, t: usize,
6130                                 cache: &mut Cache, hint: usize,
6131                                 row_ctrs: &[CudaSlice<i32>])
6132                                 -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6133        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
6134        let eps = self.cfg.rms_eps;
6135        let aux = self.gemma4_aux.as_ref().unwrap();
6136        let h0 = e.zeros(0)?;
6137        let h = &h0;
6138        // BATCHED FUSED qkv (MEMRA_F2B=1, megakernel microcosm): swa layers fuse all three,
6139        // globals fuse q,k (v := k clone). Bit-identical per row; segments tail-fill.
6140        static F2B_QKV: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
6141        let f2b = *F2B_QKV.get_or_init(|| std::env::var("MEMRA_F2B").as_deref() != Ok("0"));
6142        let fused_qkv = if f2b {
6143            if swa {
6144                e.matmul_q4_fused3_batched(&fa.wq, &fa.wk, &fa.wv, hq, hdq, t)?
6145                    .map(|(a, b, c)| (a, b, Some(c)))
6146            } else {
6147                e.matmul_q4_fused2_batched(&fa.wq, &fa.wk, hq, hdq, t)?
6148                    .map(|(a, b)| (a, b, None))
6149            }
6150        } else { None };
6151        let (q0, k0, v0) = match fused_qkv {
6152            Some((a, b, cv)) => {
6153                let v = match cv { Some(c) => c, None => e.clone_dtod(&b)? };
6154                (a, b, v)
6155            }
6156            None => {
6157                let q0 = e.matmul_pre(&fa.wq, hq, hdq, h, t)?;
6158                let k0 = e.matmul_pre(&fa.wk, hq, hdq, h, t)?;
6159                let v0 = if swa { e.matmul_pre(&fa.wv, hq, hdq, h, t)? }
6160                         else { e.clone_dtod(&k0)? };
6161                (q0, k0, v0)
6162            }
6163        };
6164        let mut q = e.uninit(t * nh * hd)?;
6165        let mut k = e.uninit(t * nkv * hd)?;
6166        let mut v = e.uninit(t * nkv * hd)?;
6167        // E4B wave-3 fold (2026-07-23 decode-dust port): q/k/v norms + q/k rope in ONE
6168        // launch — rope math verbatim on the normed rows; V ones-rms, never roped.
6169        let ff = if swa { None } else {
6170            Some(aux.rope_freqs.as_ref().expect("gemma4 global rope needs rope_freqs.weight"))
6171        };
6172        e.rms_norm_qkv_rope(&q0, &k0, &v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
6173                            &aux.ones, &mut q, &mut k, &mut v, hd, nh * t, nkv * t,
6174                            pos_d, nh, nkv, base, 1.0, ff, eps)?;
6175        let kvl = cache.kv[il].as_mut().unwrap();
6176        // append at the DEVICE slot; the counter advances by t on-device.
6177        e.append_kv_quantized_rows_dc(&k, &v, &mut kvl.k, &mut kvl.v, &kvl.len_d, t,
6178                                      kvl.kv_dim_k, kvl.kv_dim_v,
6179                                      kvl.k_tok_bytes, kvl.v_tok_bytes,
6180                                      (!swa && crate::Engine::gkv_on())
6181                                          || (swa && crate::Engine::wkv_on()))?;
6182        // len_d is NOT advanced here: the burst's device rollback (spec_rollback_stream) is
6183        // the sole len writer after this round's attention (base stays = old len, plus = 0).
6184        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
6185        let mut attn = e.uninit(t * nh * hd)?;
6186        let k_view = e.view_u8(&kvl.k, kvl.k.len());
6187        let v_view = e.view_u8(&kvl.v, kvl.v.len());
6188        // arm gating on the host HINT (upper bound; burst entry guards hint >= the vec floor
6189        // and a stable window regime — the same rung/regime keys as the draft graph).
6190        if swa && hint + 1 >= win {
6191            // fully-windowed rows: per-row window geometry from the counter (plus = 0: len_d
6192            // still holds the pre-append len; row r's T_kv = ctr + r + 1).
6193            e.fa_decode_rows_w(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
6194                               &kvl.len_d, 0, t, scale, win,
6195                               kvl.k_tok_bytes, kvl.v_tok_bytes, None)?;
6196        } else if hd == 512 && hint + t < crate::fa512_min_tkv() {
6197            // globals UNDER the fa512 crossover: eager runs the per-row fa_decode_kvmod
6198            // fallback there (parity with t=1 decode) — mirror it with per-row fa_decode_dc
6199            // (bit-correct for any t_kv <= bucket; the drafter dc arc proved the pairing).
6200            // Burst entry gates the horizon onto one side of the crossover, so hint decides
6201            // for every row.
6202            // NO .max(512): fa_decode_dc gates its hd512 dpl16-vs-scalar pick on bucket_max
6203            // (mirroring eager's fa512 floor) — forcing 512 here flipped the arm to dpl16
6204            // while eager ran scalar (il=5 KV drift, the burst's 4/128). SAME LAW capped
6205            // from above (2026-07-13): a regime-pinned hint near the floor (round-graph
6206            // captures use f512-1) pow2-rounds PAST it — clamp under the floor, or the arm
6207            // re-flips to dpl16 (the round-graph 4/64). The scalar unified self-splits, so
6208            // any bucket >= the live length is exact.
6209            let bucket = (hint + t + 2).next_power_of_two()
6210                .min(crate::fa512_min_tkv().saturating_sub(1));
6211            let qv = e.view(&q, t * nh * hd);
6212            for i in 0..t {
6213                let q_row = qv.slice(i * nh * hd..(i + 1) * nh * hd);
6214                let mut q_one = e.uninit(nh * hd)?;
6215                e.copy_view_into(&mut q_one, 0, &q_row, nh * hd)?;
6216                let mut a_one = e.uninit(nh * hd)?;
6217                e.fa_decode_dc(&q_one, &k_view, &v_view, &mut a_one, hd, nh, nkv,
6218                               &row_ctrs[i], bucket, scale,
6219                               kvl.k_tok_bytes, kvl.v_tok_bytes, false)?;
6220                e.copy_into(&mut attn, i * nh * hd, &a_one, nh * hd)?;
6221            }
6222        } else if hd == 512 {
6223            // globals past the crossover: dpl16 dc twin via the shared rows wrapper (hint
6224            // sizes splits — upper bound; splits beyond the device len exit in-kernel).
6225            e.fa_decode_rows(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, hint, t, scale,
6226                             kvl.k_tok_bytes, kvl.v_tok_bytes,
6227                             Some((&kvl.len_d, 0)), false, false, None)?;
6228        } else {
6229            // hd256 under-window: v4 device-len rows twin.
6230            e.fa_decode_rows_dc(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
6231                                &kvl.len_d, hint + t, t, scale,
6232                                kvl.k_tok_bytes, kvl.v_tok_bytes, 0,
6233                                swa && crate::Engine::wkv_on())?;
6234        }
6235        Ok(e.matmul(&fa.wo, &attn, t)?)
6236    }
6237
6238    fn gemma4_verify_attn(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer, il: usize,
6239                          hq: &CudaSlice<i8>, hdq: &CudaSlice<f32>,
6240                          pos_d: &CudaSlice<i32>, t: usize,
6241                          cache: &mut Cache)
6242                          -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6243        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
6244        let eps = self.cfg.rms_eps;
6245        let aux = self.gemma4_aux.as_ref().unwrap();
6246        let n_embd = self.cfg.n_embd as usize;
6247        let _ = n_embd;
6248
6249        let h0 = e.zeros(0)?;
6250        let h = &h0;
6251        // BATCHED FUSED qkv (MEMRA_F2B=1, megakernel microcosm): swa layers fuse all three,
6252        // globals fuse q,k (v := k clone). Bit-identical per row; segments tail-fill.
6253        static F2B_QKV: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
6254        let f2b = *F2B_QKV.get_or_init(|| std::env::var("MEMRA_F2B").as_deref() != Ok("0"));
6255        let fused_qkv = if f2b {
6256            if swa {
6257                e.matmul_q4_fused3_batched(&fa.wq, &fa.wk, &fa.wv, hq, hdq, t)?
6258                    .map(|(a, b, c)| (a, b, Some(c)))
6259            } else {
6260                e.matmul_q4_fused2_batched(&fa.wq, &fa.wk, hq, hdq, t)?
6261                    .map(|(a, b)| (a, b, None))
6262            }
6263        } else { None };
6264        let (q0, k0, v0) = match fused_qkv {
6265            Some((a, b, cv)) => {
6266                let v = match cv { Some(c) => c, None => e.clone_dtod(&b)? };
6267                (a, b, v)
6268            }
6269            None => {
6270                let q0 = e.matmul_pre(&fa.wq, hq, hdq, h, t)?;
6271                let k0 = e.matmul_pre(&fa.wk, hq, hdq, h, t)?;
6272                let v0 = if swa { e.matmul_pre(&fa.wv, hq, hdq, h, t)? }
6273                         else { e.clone_dtod(&k0)? };
6274                (q0, k0, v0)
6275            }
6276        };
6277        let mut q = e.uninit(t * nh * hd)?;
6278        let mut k = e.uninit(t * nkv * hd)?;
6279        let mut v = e.uninit(t * nkv * hd)?;
6280        // E4B wave-3 fold (2026-07-23 decode-dust port): q/k/v norms + q/k rope in ONE
6281        // launch — rope math verbatim on the normed rows; V ones-rms, never roped.
6282        let ff = if swa { None } else {
6283            Some(aux.rope_freqs.as_ref().expect("gemma4 global rope needs rope_freqs.weight"))
6284        };
6285        e.rms_norm_qkv_rope(&q0, &k0, &v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
6286                            &aux.ones, &mut q, &mut k, &mut v, hd, nh * t, nkv * t,
6287                            pos_d, nh, nkv, base, 1.0, ff, eps)?;
6288        let kvl = cache.kv[il].as_mut().unwrap();
6289        let base_len = kvl.len;
6290        e.append_kv_quantized_rows(&k, &v, &mut kvl.k, &mut kvl.v, base_len, t,
6291                                   kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes, (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on()))?;
6292        kvl.len += t;
6293        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
6294        let mut attn = e.uninit(t * nh * hd)?;
6295        // ROWS twin when no window offset is in play (globals are hd512-ineligible; SWA rows
6296        // under the window need no offset): ONE launch, per-row causal == the per-token loop.
6297        let rows_ok = (hd == 256 && base_len + 1 >= crate::fa_vec_min_tkv())
6298            // gemma globals: hd512 rows twin in the dpl16 vec regime (row 0 gates the batch);
6299            // decode rides the SAME symbol at t=1 (parity law).
6300            || (hd == 512 && !swa && base_len + 1 >= crate::fa512_min_tkv());
6301        if rows_ok && (!swa || base_len + t <= win) {
6302            let k_view = e.view_u8(&kvl.k, (base_len + t) * kvl.k_tok_bytes);
6303            let v_view = e.view_u8(&kvl.v, (base_len + t) * kvl.v_tok_bytes);
6304            if hd == 512 {
6305                // device-len twin: sync the counter to the verify base (async arg-store).
6306                e.i32_set_k(&mut kvl.len_d, base_len as i32)?;
6307                e.fa_decode_rows(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, base_len, t,
6308                                 scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
6309                                 Some((&kvl.len_d, 0)), false,
6310                                 swa && crate::Engine::wkv_on(), None)?;
6311            } else {
6312                // hd256: the SAME v4_dc symbol the burst verify launches (PARITY LAW — the
6313                // host-base rows_v4 twin compiles apart from v4_dc and the two-symbol split
6314                // drifted the burst's persisted KV at il=8; one symbol, counter arg-stored).
6315                e.i32_set_k(&mut kvl.len_d, base_len as i32)?;
6316                e.fa_decode_rows_dc(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
6317                                    &kvl.len_d, base_len + t, t, scale,
6318                                    kvl.k_tok_bytes, kvl.v_tok_bytes, 0,
6319                                    swa && crate::Engine::wkv_on())?;
6320            }
6321            return Ok(e.matmul(&fa.wo, &attn, t)?);
6322        }
6323        // WINDOWED rows twin (deep ctx, every row fully windowed): one launch, ABSOLUTE-index
6324        // per-row geometry over the prefix view. PARITY LAW (2026-07-10 root-cause): textually
6325        // identical kernels do NOT compile bit-identically (nvcc unrolls fa_decode_vec_q 2x vs
6326        // its rows_w clone — SASS-proven; the unpinned score `+=` chain then rounds apart), so
6327        // decode-vs-verify parity comes from BOTH sides launching THIS SAME rows_w kernel
6328        // (decode passes t=1) — bitwise equal per position by symbol identity, any lane.
6329        // MEMRA_GEMMA_ROWS_W=0 -> per-token loop (decode falls back to fa_decode views too).
6330        if hd == 256 && swa && base_len + 1 >= win
6331            && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
6332            let k_view = e.view_u8(&kvl.k, (base_len + t) * kvl.k_tok_bytes);
6333            let v_view = e.view_u8(&kvl.v, (base_len + t) * kvl.v_tok_bytes);
6334            e.i32_set_k(&mut kvl.len_d, base_len as i32)?;
6335            e.fa_decode_rows_w(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, &kvl.len_d, 0,
6336                               t, scale, win, kvl.k_tok_bytes, kvl.v_tok_bytes, None)?;
6337            return Ok(e.matmul(&fa.wo, &attn, t)?);
6338        }
6339        for i in 0..t {
6340            let avail = base_len + i + 1;
6341            let (off_tok, t_kv) = if swa && avail > win { (avail - win, win) } else { (0, avail) };
6342            let k_view = e.view_u8_range(&kvl.k, off_tok * kvl.k_tok_bytes,
6343                                         (off_tok + t_kv) * kvl.k_tok_bytes);
6344            let v_view = e.view_u8_range(&kvl.v, off_tok * kvl.v_tok_bytes,
6345                                         (off_tok + t_kv) * kvl.v_tok_bytes);
6346            let qi = e.view(&q, t * nh * hd);
6347            let q_row = qi.slice(i * nh * hd..(i + 1) * nh * hd);
6348            let mut q_one = e.uninit(nh * hd)?;
6349            e.copy_view_into(&mut q_one, 0, &q_row, nh * hd)?;
6350            let mut a_one = e.uninit(nh * hd)?;
6351            // straddle rounds: windowed rows must ride the SAME rows_w kernel decode uses
6352            // (parity law above); global hd512 rows past the fa512 floor likewise ride the
6353            // rows_dpl16 twin; remaining rows keep the gated fa_decode pair.
6354            if swa && avail > win && hd == 256
6355                && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
6356                let kp = e.view_u8(&kvl.k, avail * kvl.k_tok_bytes);
6357                let vp = e.view_u8(&kvl.v, avail * kvl.v_tok_bytes);
6358                e.i32_set_k(&mut kvl.len_d, (avail - 1) as i32)?;
6359                e.fa_decode_rows_w(&q_one, &kp, &vp, &mut a_one, hd, nh, nkv, &kvl.len_d, 0,
6360                                   1, scale, win, kvl.k_tok_bytes, kvl.v_tok_bytes, None)?;
6361            } else if !swa && hd == 512 && avail >= crate::fa512_min_tkv()
6362                && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
6363                let kp = e.view_u8(&kvl.k, avail * kvl.k_tok_bytes);
6364                let vp = e.view_u8(&kvl.v, avail * kvl.v_tok_bytes);
6365                e.i32_set_k(&mut kvl.len_d, (avail - 1) as i32)?;
6366                e.fa_decode_rows(&q_one, &kp, &vp, &mut a_one, hd, nh, nkv, avail - 1, 1,
6367                                 scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
6368                                 Some((&kvl.len_d, 0)), false, false, None)?;
6369            } else {
6370                e.fa_decode_kvmod(&q_one, &k_view, &v_view, &mut a_one, hd, nh, nkv, t_kv, scale,
6371                            kvl.k_tok_bytes, kvl.v_tok_bytes, swa && crate::Engine::wkv_on())?;
6372            }
6373            e.copy_into(&mut attn, i * nh * hd, &a_one, nh * hd)?;
6374        }
6375        Ok(e.matmul(&fa.wo, &attn, t)?)
6376    }
6377
6378    /// gemma4 T=1 decode step: R8 layer graph over the cache; returns (softcapped logits host,
6379    /// h_seed = pre-output_norm hidden). Advances cache.pos.
6380    pub(crate) fn gemma4_decode_step_h(&self, e: &Engine, token: u32, cache: &mut Cache)
6381                                       -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6382        // M1-PP2 door (crate::pp): 2-stage split with an explicit activation handoff.
6383        // Default OFF — unset env means this branch is never taken. The gemma4 arm stays
6384        // 2-stage in M2 (the N-stage gate model is the generic-arm 9B); N>2 warns + runs
6385        // unsplit rather than guessing a fence.
6386        if let Some(split) = crate::pp::pp2_split(self.layers.len()) {
6387            return self.gemma4_decode_step_h_pp2(e, token, cache, split);
6388        }
6389        if crate::pp::pp_cuts(self.layers.len()).is_some() {
6390            crate::pp::warn_unwired_once("gemma4 eager decode (N>2)");
6391        }
6392        let n_embd = self.cfg.n_embd as usize;
6393        let eps = self.cfg.rms_eps;
6394        let pos_d = e.htod_i32(&[cache.pos as i32])?;
6395        let mut x = e.htod(&self.embd.gather(n_embd, &[token]))?;
6396        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
6397        // cross-layer fusion: each tail's closing add+scale also EMITS the next layer's
6398        // attn-normed input pre-quantized q8_1 (the mixer consumes only quantized matmuls).
6399        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
6400        let n_layers = self.layers.len();
6401        for (il, layer) in self.layers.iter().enumerate() {
6402            let (hq, hdq) = match h_carry.take() {
6403                Some(p) => p,
6404                None => e.rms_norm_q8_1(&x, self.layers[0].attn_norm.float_data(), n_embd, 1, eps)?,
6405            };
6406            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
6407            let o = self.gemma4_decode_attn(e, fa, il, &hq, &hdq, &pos_d, cache)?;
6408            let mut cur = e.uninit(n_embd)?;
6409            e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, 1, eps)?;
6410            let next_norm = if il + 1 < n_layers {
6411                Some(self.layers[il + 1].attn_norm.float_data())
6412            } else { None };
6413            let (xn, hn) = self.gemma4_layer_tail_add_nq(e, layer, &cur, &x, 1, next_norm)?;
6414            x = xn;
6415            h_carry = hn;
6416        }
6417        let mut hn = e.uninit(n_embd)?;
6418        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
6419        let h_seed = e.clone_dtod(&x)?;
6420        let mut ld = e.matmul(&self.output, &hn, 1)?;
6421        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
6422        e.softcap(&mut ld, cap, self.output.out_features())?;   // R4 on device (262k host tanh ~ms/step)
6423        self.gemma4_suppress(e, &mut ld, 1)?;
6424        let logits = e.dtoh(&ld)?;
6425        cache.pos += 1;
6426        Ok((logits, h_seed))
6427    }
6428
6429    /// M1-PP2 stage subgraph (gemma4 arm): layers [lo, hi) of the gemma4 T=1 walk with the
6430    /// pre-quantized next-norm carry LOCAL to the range. Enters with a materialized residual
6431    /// `x` (the range head runs its own `rms_norm_q8_1` against ITS layer's attn_norm — for
6432    /// lo == 0 that is exactly the unsplit loop's il==0 arm); exits with the residual
6433    /// materialized (range tail passes next_norm = None, exactly the unsplit last-layer arm).
6434    /// Bit-identity of the cut relies on the kernel-check-pinned `add_scale_rms_norm_q8_1 ==
6435    /// add_scale then rms_norm_q8_1` identity (`pp2-gate` verifies end-to-end).
6436    fn gemma4_decode_layers(&self, e: &Engine, mut x: CudaSlice<f32>, lo: usize, hi: usize,
6437                            pos_d: &CudaSlice<i32>, cache: &mut Cache)
6438                            -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6439        let n_embd = self.cfg.n_embd as usize;
6440        let eps = self.cfg.rms_eps;
6441        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
6442        for il in lo..hi {
6443            let layer = &self.layers[il];
6444            let (hq, hdq) = match h_carry.take() {
6445                Some(p) => p,
6446                // range head: il == lo — norm against THIS layer's attn_norm.
6447                None => e.rms_norm_q8_1(&x, self.layers[il].attn_norm.float_data(), n_embd, 1, eps)?,
6448            };
6449            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
6450            let o = self.gemma4_decode_attn(e, fa, il, &hq, &hdq, pos_d, cache)?;
6451            let mut cur = e.uninit(n_embd)?;
6452            e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, 1, eps)?;
6453            let next_norm = if il + 1 < hi {
6454                Some(self.layers[il + 1].attn_norm.float_data())
6455            } else { None };
6456            let (xn, hn) = self.gemma4_layer_tail_add_nq(e, layer, &cur, &x, 1, next_norm)?;
6457            x = xn;
6458            h_carry = hn;
6459        }
6460        Ok(x)
6461    }
6462
6463    /// M1-PP2 (increment 2): `gemma4_decode_step_h` as TWO stage subgraphs, each on its
6464    /// own stream (and device, under MEMRA_PP_DEVICES) with the transport-selected
6465    /// boundary handoff — same choreography as the generic arm (decode.rs), same
6466    /// ownership contract (crate::pp). Stage 0 = embed+scale + layers [0, split);
6467    /// stage 1 = layers [split, n) + output_norm + softcapped head.
6468    /// MEMRA_PP_STREAMS=0 = the increment-1 same-stream seam. Gate: `pp2-gate`.
6469    fn gemma4_decode_step_h_pp2(&self, e: &Engine, token: u32, cache: &mut Cache, split: usize)
6470                                -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6471        if crate::pp::pp2_streams_off() {
6472            return self.gemma4_decode_step_h_pp2_samestream(e, token, cache, split);
6473        }
6474        let rt = crate::pp::Pp2Rt::get(e)?;
6475        let e0 = rt.engine(0, e);
6476        let e1 = rt.engine(1, e);
6477        let n_embd = self.cfg.n_embd as usize;
6478        let eps = self.cfg.rms_eps;
6479
6480        // ---- STAGE 0 (its own stream): embed + sqrt(n_embd) scale + layers [0, split) ----
6481        let (pos_d, slot) = {
6482            let _st0 = rt.enter(0);
6483            let pos_d = e0.htod_i32(&[cache.pos as i32])?;
6484            let mut x = e0.htod(&self.embd.gather(n_embd, &[token]))?;
6485            e0.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
6486            let x = self.gemma4_decode_layers(e0, x, 0, split, &pos_d, cache)?;
6487            let slot = rt.tx(0, &x, n_embd)?;
6488            (pos_d, slot)
6489        };
6490
6491        // ---- STAGE 1 (its own stream): RX + layers [split, n) + softcapped head ----
6492        let _st1 = rt.enter(1);
6493        let x = rt.rx(0, slot, n_embd)?;
6494        let x = self.gemma4_decode_layers(e1, x, split, self.layers.len(), &pos_d, cache)?;
6495
6496        let mut hn = e1.uninit(n_embd)?;
6497        e1.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
6498        let h_seed = e1.clone_dtod(&x)?;
6499        let mut ld = e1.matmul(&self.output, &hn, 1)?;
6500        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
6501        e1.softcap(&mut ld, cap, self.output.out_features())?;
6502        self.gemma4_suppress(e1, &mut ld, 1)?;
6503        let logits = e1.dtoh(&ld)?;
6504        cache.pos += 1;
6505        Ok((logits, h_seed))
6506    }
6507
6508    /// MEMRA_PP_STREAMS=0 rollback seam: the increment-1 gemma4 pp2 body verbatim — both
6509    /// stage subgraphs on the ambient compute stream, boundary = two plain dtod copies.
6510    fn gemma4_decode_step_h_pp2_samestream(&self, e: &Engine, token: u32, cache: &mut Cache,
6511                                           split: usize)
6512                                           -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6513        let n_embd = self.cfg.n_embd as usize;
6514        let eps = self.cfg.rms_eps;
6515        let pos_d = e.htod_i32(&[cache.pos as i32])?;
6516
6517        // ---- STAGE 0: embed + sqrt(n_embd) scale + layers [0, split) ----
6518        let mut x = e.htod(&self.embd.gather(n_embd, &[token]))?;
6519        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
6520        let x = self.gemma4_decode_layers(e, x, 0, split, &pos_d, cache)?;
6521
6522        // ---- STAGE BOUNDARY: explicit [n_embd] activation handoff (TX copy, RX copy) ----
6523        let boundary_tx = e.clone_dtod(&x)?;
6524        let boundary_rx = e.clone_dtod(&boundary_tx)?;
6525
6526        // ---- STAGE 1: layers [split, n) + output_norm + softcapped head ----
6527        let x = self.gemma4_decode_layers(e, boundary_rx, split, self.layers.len(), &pos_d, cache)?;
6528
6529        let mut hn = e.uninit(n_embd)?;
6530        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
6531        let h_seed = e.clone_dtod(&x)?;
6532        let mut ld = e.matmul(&self.output, &hn, 1)?;
6533        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
6534        e.softcap(&mut ld, cap, self.output.out_features())?;
6535        self.gemma4_suppress(e, &mut ld, 1)?;
6536        let logits = e.dtoh(&ld)?;
6537        cache.pos += 1;
6538        Ok((logits, h_seed))
6539    }
6540}
6541
6542// ===================================================================================== //
6543//  gemma-4 E4B (per-layer embeddings + KV-sharing) — FIRST-LIGHT forward.               //
6544//  Dedicated simple path (Stage-B matmuls, per-row causal attention over the quantized  //
6545//  cache) so the tuned 26B/31B paths stay untouched. Arch: research/gemma4-bringup/     //
6546//  e4b-arch-map.md; llama reference: src/models/gemma4.cpp (E4B arms). Wired: forward   //
6547//  (prefill logits), prime (tokenwise-equivalent batched trunk), eager decode_step_h.   //
6548//  NOT wired: dc/graph serving arms, verify/spec, chunked prime (HANDOVER-E4B.md).      //
6549// ===================================================================================== //
6550impl HybridModel {
6551    pub fn is_gemma4_e4b(&self) -> bool {
6552        self.gemma4_aux.as_ref().is_some_and(|a| a.e4b.is_some())
6553    }
6554
6555    /// E4B per-layer geometry: nh/nkv derive from the layer's OWN tensor shapes (the E4B GGUF
6556    /// ships a scalar head_count_kv; swa q 8x256 / kv 2x256, global q 4x512 / kv 1x512).
6557    /// KV-shared layers report the SHARE TARGET's kv count (their wk IS the target's tensor).
6558    fn gemma4_e4b_geom(&self, il: usize) -> (usize, usize, usize, f32, f32, bool) {
6559        let g = self.cfg.gemma4.as_ref().unwrap();
6560        let swa = g.swa_pattern[il];
6561        let hd = if swa { g.key_length_swa } else { g.key_length_global } as usize;
6562        let Mixer::Full(fa) = &self.layers[il].mixer else { panic!("e4b layer {il} not full-attn") };
6563        let nh = fa.wq.out_features() / hd;
6564        let nkv = fa.wk.out_features() / hd;
6565        (hd, nkv, nh, if swa { g.rope_base_swa } else { g.rope_base_global }, 1.0, swa)
6566    }
6567
6568    /// E4B KV-share target: Some(target) for the trailing shared layers, None = own cache.
6569    fn gemma4_e4b_kv_target(&self, il: usize) -> Option<usize> {
6570        self.layers[il].gemma4.as_ref()
6571            .and_then(|b| b.e4b.as_ref())
6572            .and_then(|e4| e4.kv_share.map(|t| t as usize))
6573    }
6574
6575    /// E4B prologue: inp_pl [t][n_layer][n_epl] =
6576    ///   ( gather(per_layer_tok_embd, tok)*sqrt(n_epl)
6577    ///   + rms_norm(model_proj . x_scaled * 1/sqrt(n_embd), proj_norm) ) * 1/sqrt(2)
6578    /// (llama gemma4.cpp build_inp_per_layer + project_per_layer_inputs, exact order).
6579    fn gemma4_e4b_inp_pl(&self, e: &Engine, tokens: &[u32], x_scaled: &CudaSlice<f32>, t: usize)
6580                         -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6581        let tok_d = e.stream().clone_htod(&tokens.to_vec())?;
6582        self.gemma4_e4b_inp_pl_dev(e, &tok_d, x_scaled, t)
6583    }
6584
6585    /// Device-token prologue core (dc arm shares it: token ids never touch the host).
6586    fn gemma4_e4b_inp_pl_dev(&self, e: &Engine, tok_d: &CudaSlice<u32>,
6587                             x_scaled: &CudaSlice<f32>, t: usize)
6588                             -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6589        let aux = self.gemma4_aux.as_ref().unwrap();
6590        let m = aux.e4b.as_ref().unwrap();
6591        let n_embd = self.cfg.n_embd as usize;
6592        let n_layer = self.layers.len();
6593        let width = m.n_epl * n_layer;
6594        let tbl = m.tok_tbl_gpu.get_or_init(|| {
6595            e.upload_u8(&m.tok_embd_bytes).expect("e4b per-layer token table upload")
6596        });
6597        let mut a = e.embed_gather_device_td(tbl, tok_d, t, width, m.tok_embd_qt,
6598                                             m.tok_embd_row_bytes)?;
6599        e.scale_inplace(&mut a, (m.n_epl as f32).sqrt(), t * width)?;
6600        let mut p = e.matmul(&m.model_proj, x_scaled, t)?;
6601        e.scale_inplace(&mut p, 1.0 / (n_embd as f32).sqrt(), t * width)?;
6602        let mut pn = e.uninit(t * width)?;
6603        e.rms_norm(&p, m.proj_norm.float_data(), &mut pn, m.n_epl, t * n_layer,
6604                   self.cfg.rms_eps)?;
6605        let mut out = e.uninit(t * width)?;
6606        e.add_scale(&a, &pn, 1.0 / 2f32.sqrt(), &mut out, t * width)?;
6607        Ok(out)
6608    }
6609
6610    /// E4B attention (t-wide, causal, per-row fa over the QUANTIZED cache — first-light
6611    /// correctness path; the fa rows arms come later). Own-KV layers project+norm+rope k/v
6612    /// and append t rows; KV-shared layers are Q-only over the target layer's cache (which
6613    /// already holds this forward's rows — the target runs earlier in the stack).
6614    #[allow(clippy::too_many_arguments)]
6615    fn gemma4_e4b_attn(&self, e: &Engine, il: usize,
6616                       hq: &CudaSlice<i8>, hdq: &CudaSlice<f32>,
6617                       pos_d: &CudaSlice<i32>, t: usize, cache: &mut Cache,
6618                       dc_bucket: Option<usize>)
6619                       -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6620        let (hd, nkv, nh, base, scale, swa) = self.gemma4_e4b_geom(il);
6621        let eps = self.cfg.rms_eps;
6622        let aux = self.gemma4_aux.as_ref().unwrap();
6623        let Mixer::Full(fa) = &self.layers[il].mixer else { unreachable!() };
6624        // pre-quantized layer input (E4B fusion port, 2026-07-12): ONE norm+quant feeds
6625        // wq/wk/wv via matmul_pre — the first-light path quantized the same h three times
6626        // (the profile's 342 quantize_q8_1/token; glue = 26% of the 6.1ms token).
6627        let h0 = e.zeros(0)?;
6628        let h = &h0;
6629
6630        let ff = if swa { None } else {
6631            Some(aux.rope_freqs.as_ref().expect("e4b global rope needs rope_freqs.weight"))
6632        };
6633        let share = self.gemma4_e4b_kv_target(il);
6634        // Own-KV arms keep the f32 (k, v) alive for the prime fa arm below.
6635        let mut kv_f32: Option<(CudaSlice<f32>, CudaSlice<f32>)> = None;
6636        let mut q;
6637        if let Some(_tgt) = share {
6638            let q0 = e.matmul_pre(&fa.wq, hq, hdq, h, t)?;
6639            q = e.uninit(t * nh * hd)?;
6640            // Q-only through the same fused norm+rope kernel (rk = 0: the k/v segments are
6641            // empty; q0 stands in for the unused k/v pointers).
6642            let mut kdummy = e.uninit(1)?;
6643            let mut vdummy = e.uninit(1)?;
6644            e.rms_norm_qkv_rope(&q0, &q0, &q0, fa.q_norm.float_data(),
6645                                fa.q_norm.float_data(), &aux.ones,
6646                                &mut q, &mut kdummy, &mut vdummy, hd, nh * t, 0,
6647                                pos_d, nh, 1, base, 1.0, ff, eps)?;
6648        } else {
6649            // wave-4b: ONE concat matvec (wq|wk|wv) at t == 1 when the cat tensor exists;
6650            // else the fused3 grid launch; else per-matvec. The cat output is contiguous
6651            // q|k|v rows — the cat norm+rope twin consumes it directly.
6652            let e4bits = self.layers[il].gemma4.as_ref().and_then(|g| g.e4b.as_ref());
6653            let cat = e4bits.and_then(|e4| e4.qkv_cat.as_ref());
6654            q = e.uninit(t * nh * hd)?;
6655            let mut k = e.uninit(t * nkv * hd)?;
6656            let mut v = e.uninit(t * nkv * hd)?;
6657            if t == 1 && cat.is_some() {
6658                let qkv0 = e.matmul_pre(cat.unwrap(), hq, hdq, h, 1)?;
6659                e.rms_norm_qkv_rope_cat(&qkv0, fa.q_norm.float_data(), fa.k_norm.float_data(),
6660                                        &aux.ones, &mut q, &mut k, &mut v, hd, nh, nkv,
6661                                        pos_d, nh, nkv, base, 1.0, ff, eps)?;
6662            } else {
6663                let (q0, k0, v0) = match if t == 1 {
6664                    e.matmul_q4_fused3(&fa.wq, &fa.wk, &fa.wv, hq, hdq)?
6665                } else {
6666                    // E4B verify f3 port (2026-07-14): the 31B segmented-grid batched qkv
6667                    // on E4B's real-V triple — same MEMRA_F2B seam, bit-identical per row.
6668                    static F2B_QKV: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
6669                    if *F2B_QKV.get_or_init(|| std::env::var("MEMRA_F2B").as_deref() != Ok("0")) {
6670                        e.matmul_q4_fused3_batched(&fa.wq, &fa.wk, &fa.wv, hq, hdq, t)?
6671                    } else { None }
6672                } {
6673                    Some(triple) => triple,
6674                    None => (e.matmul_pre(&fa.wq, hq, hdq, h, t)?,
6675                             e.matmul_pre(&fa.wk, hq, hdq, h, t)?,
6676                             e.matmul_pre(&fa.wv, hq, hdq, h, t)?),   // E4B: real v (K != V)
6677                };
6678                // wave-3 fold: q/k/v norms + q/k rope in ONE launch (rope math verbatim on
6679                // the normed rows; V ones-rms, never roped).
6680                e.rms_norm_qkv_rope(&q0, &k0, &v0, fa.q_norm.float_data(),
6681                                    fa.k_norm.float_data(), &aux.ones, &mut q, &mut k, &mut v,
6682                                    hd, nh * t, nkv * t, pos_d, nh, nkv, base, 1.0, ff, eps)?;
6683            }
6684            let kvl = cache.kv[il].as_mut().unwrap();
6685            // class flag must match the cache dims (the g-threading sweep hardcoded `false`
6686            // here and the wkv default corrupted E4B: q8_0 bytes into an e4m3 cache — the
6687            // degenerate tok-0 stream, 2026-07-12).
6688            let cls = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
6689            if dc_bucket.is_some() {
6690                // DC arm (graph serving): append at the len_d slot, advance the counter
6691                // in-stream — replay-correct, no host len in the launch args. Host mirrors
6692                // are NOT touched here (the replay loop owns them; a bump at capture-record
6693                // time would double-count the capture iteration).
6694                debug_assert!(t == 1);
6695                // wave 5c: append + len_d inc fused (one launch; single-block ordering).
6696                e.append_kv_quantized_row_dc_inc(&k, &v, &mut kvl.k, &mut kvl.v,
6697                                                 &mut kvl.len_d, kvl.kv_dim_k, kvl.kv_dim_v,
6698                                                 kvl.k_tok_bytes, kvl.v_tok_bytes, cls)?;
6699            } else {
6700                e.append_kv_quantized_rows(&k, &v, &mut kvl.k, &mut kvl.v, kvl.len, t,
6701                                           kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes,
6702                                           kvl.v_tok_bytes, cls)?;
6703                kvl.len += t;
6704            }
6705            kv_f32 = Some((k, v));
6706        }
6707        // attention: per-row causal fa over the (own or target) quantized cache. The cache
6708        // already contains this forward's rows in both arms; row i attends [.., base+i].
6709        let kvl_idx = share.unwrap_or(il);
6710        let kvl = cache.kv[kvl_idx].as_ref().unwrap();
6711        let base_len = kvl.len - t;   // pre-append length (target appended this forward too)
6712        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
6713        let mut attn = e.uninit(t * nh * hd)?;
6714        // PRIME FA ARMS (perf lane 3, completed 2026-07-31 — the H100 board pinned E4B
6715        // prefill at 482 tok/s: the per-row loop ran the DECODE kernel once per token per
6716        // layer). Fresh-prompt (base_len == 0) batched attention, one launch per layer:
6717        //   - own-KV hd256 under the window: f32 fa_prefill (full causal exact — 26B pattern)
6718        //   - own-KV hd256 above the window (swa): fa_prefill_w windowed twin (12B pattern)
6719        //   - own-KV hd512 globals: fa_prefill_hd512 (12B/31B globals pattern)
6720        //   - KV-shared layers (no f32 k/v): fa_prefill_view over the target's QUANTIZED
6721        //     rows (the T=K verify kernel; the target appended this forward's rows already).
6722        //     swa-shared above the window keeps the per-row loop (no windowed view twin).
6723        // Same numeric class split as the 26B prime (f32/batched prefill vs per-row
6724        // quantized decode); run-gen argmax + chat gates arbitrate. MEMRA_NOFA=1 reverts all.
6725        if t > 1 && base_len == 0 && std::env::var("MEMRA_NOFA").is_err() {
6726            if let Some((kf, vf)) = &kv_f32 {
6727                if hd == 256 && t <= win {
6728                    e.fa_prefill(&q, kf, vf, &mut attn, hd, nh, nkv, t, t, scale, true)?;
6729                    return Ok(e.matmul(&fa.wo, &attn, t)?);
6730                }
6731                if hd == 256 && swa && t > win {
6732                    e.fa_prefill_w(&q, kf, vf, &mut attn, hd, nh, nkv, t, t, scale, true,
6733                                   win)?;
6734                    return Ok(e.matmul(&fa.wo, &attn, t)?);
6735                }
6736                if hd == 512 && !swa {
6737                    e.fa_prefill_hd512(&q, kf, vf, &mut attn, hd, nh, nkv, t, t, scale,
6738                                       true)?;
6739                    return Ok(e.matmul(&fa.wo, &attn, t)?);
6740                }
6741            } else if share.is_some() {
6742                let g = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
6743                let k_view = e.view_u8(&kvl.k, kvl.k.len());
6744                let v_view = e.view_u8(&kvl.v, kvl.v.len());
6745                if hd == 256 && (!swa || t <= win) {
6746                    // inline-dequant quantized-view prefill (fa_prefill_q stamps 256/128)
6747                    e.fa_prefill_view(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, t, t,
6748                                      scale, true, kvl.k_tok_bytes, kvl.v_tok_bytes, g)?;
6749                    return Ok(e.matmul(&fa.wo, &attn, t)?);
6750                }
6751                // remaining shared classes (swa above the window; hd512 globals): dequant
6752                // the target's t rows ONCE to f32, then the same f32 twins as own-KV.
6753                let kv_dim = nkv * hd;
6754                let mut kf = e.uninit(t * kv_dim)?;
6755                let mut vf = e.uninit(t * kv_dim)?;
6756                e.fa_dequant_kv_view_f32(&k_view, &v_view, &mut kf, &mut vf, kv_dim, kv_dim,
6757                                         t, kvl.k_tok_bytes, kvl.v_tok_bytes, g)?;
6758                if hd == 512 {
6759                    e.fa_prefill_hd512(&q, &kf, &vf, &mut attn, hd, nh, nkv, t, t, scale,
6760                                       true)?;
6761                } else {
6762                    e.fa_prefill_w(&q, &kf, &vf, &mut attn, hd, nh, nkv, t, t, scale, true,
6763                                   win)?;
6764                }
6765                return Ok(e.matmul(&fa.wo, &attn, t)?);
6766            }
6767        }
6768        if let Some(bucket) = dc_bucket {
6769            // DC arm (t == 1, under-window regime — the generate gate enforces it): ONE
6770            // fa_decode_dc over the live counter. len_d already advanced past this token
6771            // (t_kv = len_d[0], the cache.rs contract). KV-shared layers read the target's
6772            // counter (advanced when the target ran earlier in the stack).
6773            assert!(t == 1);
6774            // hd512 globals: eager (kvmod) picks the SCALAR unified below the fa512 floor,
6775            // and under the window every live t_kv sits below it — cap the capture bucket
6776            // under the floor so fa_decode_dc bakes the same scalar symbol, or the graph
6777            // rides the dpl16 twin and its numeric class diverges from the dc-eager stream
6778            // (E4B-GRAPH-GATE 2/64, 2026-07-12).
6779            let bucket = if hd == 512 && win <= crate::fa512_min_tkv() {
6780                bucket.min(crate::fa512_min_tkv().saturating_sub(1))
6781            } else { bucket };
6782            let k_view = e.view_u8(&kvl.k, kvl.k.len());
6783            let v_view = e.view_u8(&kvl.v, kvl.v.len());
6784            let g = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
6785            // Weight prefetch (SOTA item 3, 2026-07-13): wo's decode plane prefetched into
6786            // L2 across the fa window — fa reads KV only, the weight DRAM lanes are idle
6787            // there (E4B valid window +0.65%: 196.8 vs 195.6). Value-free scheduling op,
6788            // captured into the dc graph like any other launch. Extending the cascade to
6789            // the ffn gate/up planes measured NEGATIVE (193.9 vs 195.8 — 29MB/layer floods
6790            // the fill path and evicts still-hot lines); wo-only is the shipped shape.
6791            // MEMRA_WPF=0 rollback seam.
6792            if crate::Engine::wpf_level() >= 1 {
6793                e.prefetch_weight_l2(&fa.wo)?;
6794            }
6795            // wave 5b: the combine emits the wo input q8 pair directly — the standalone
6796            // quantize launch + the f32 attn round-trip fold away (t=1 fast path only).
6797            if e.uses_q8_1_fast(&fa.wo) {
6798                let mut oq = e.alloc_i8_uninit(nh * hd)?;
6799                let mut od = e.zeros(nh * hd / 32)?;
6800                e.fa_decode_dc_q8(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
6801                                  &kvl.len_d, bucket, scale,
6802                                  kvl.k_tok_bytes, kvl.v_tok_bytes, g,
6803                                  Some((&mut oq, &mut od)))?;
6804                return Ok(e.matmul_pre(&fa.wo, &oq, &od, &attn, t)?);
6805            }
6806            e.fa_decode_dc(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
6807                           &kvl.len_d, bucket, scale,
6808                           kvl.k_tok_bytes, kvl.v_tok_bytes, g)?;
6809            return Ok(e.matmul(&fa.wo, &attn, t)?);
6810        }
6811        for i in 0..t {
6812            let avail = base_len + i + 1;
6813            let (off_tok, t_kv) = if swa && avail > win { (avail - win, win) } else { (0, avail) };
6814            let k_view = e.view_u8_range(&kvl.k, off_tok * kvl.k_tok_bytes,
6815                                         (off_tok + t_kv) * kvl.k_tok_bytes);
6816            let v_view = e.view_u8_range(&kvl.v, off_tok * kvl.v_tok_bytes,
6817                                         (off_tok + t_kv) * kvl.v_tok_bytes);
6818            let qv = e.view(&q, t * nh * hd);
6819            let q_row = qv.slice(i * nh * hd..(i + 1) * nh * hd);
6820            let mut q_one = e.uninit(nh * hd)?;
6821            e.copy_view_into(&mut q_one, 0, &q_row, nh * hd)?;
6822            let mut a_one = e.uninit(nh * hd)?;
6823            // read class MUST match the append class (globals are e4m3 under gkv): the
6824            // swa-only flag decoded global-layer e4m3 bytes as q8_0/q5_1 — attention over
6825            // garbage on EVERY E4B global layer, the cross-mode maxdiff-30 root (2026-07-12).
6826            e.fa_decode_kvmod(&q_one, &k_view, &v_view, &mut a_one, hd, nh, nkv, t_kv, scale,
6827                        kvl.k_tok_bytes, kvl.v_tok_bytes,
6828                        (!swa && crate::Engine::gkv_on())
6829                            || (swa && crate::Engine::wkv_on()))?;
6830            e.copy_into(&mut attn, i * nh * hd, &a_one, nh * hd)?;
6831        }
6832        Ok(e.matmul(&fa.wo, &attn, t)?)
6833    }
6834
6835    /// E4B trunk: embed -> prologue -> layers (attn + dense ffn via the 31B tail_core + the
6836    /// per-layer-embedding tail + layer scale) -> output_norm. Returns (softcapped logits
6837    /// device [t, n_vocab], pre-output_norm hidden [t, n_embd]). Appends t rows per own-KV
6838    /// layer; does NOT advance cache.pos (caller owns pos).
6839    fn gemma4_e4b_trunk(&self, e: &Engine, tokens: &[u32], pos0: usize, cache: &mut Cache,
6840                        head_last: bool)
6841                        -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6842        let n_embd = self.cfg.n_embd as usize;
6843        let t = tokens.len();
6844        let pos: Vec<i32> = (0..t).map(|i| (pos0 + i) as i32).collect();
6845        let pos_d = e.htod_i32(&pos)?;
6846        let mut x = e.htod(&self.embd.gather(n_embd, tokens))?;
6847        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
6848        let inp_pl = self.gemma4_e4b_inp_pl(e, tokens, &x, t)?;
6849        self.gemma4_e4b_trunk_core(e, x, inp_pl, &pos_d, t, cache, None, true, head_last)
6850    }
6851
6852    /// Layer stack + head over prebuilt (x_scaled, inp_pl, device pos) — everything below
6853    /// here is device-driven, so the dc arm shares it verbatim (stream identity with the
6854    /// eager chain by construction: SAME functions, not twins).
6855    fn gemma4_e4b_trunk_core(&self, e: &Engine, x_in: CudaSlice<f32>, inp_pl: CudaSlice<f32>,
6856                             pos_d: &CudaSlice<i32>, t: usize, cache: &mut Cache,
6857                             dc_bucket: Option<usize>, cap_logits: bool, head_last: bool)
6858                             -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6859        let n_embd = self.cfg.n_embd as usize;
6860        let eps = self.cfg.rms_eps;
6861        let n_layer = self.layers.len();
6862        let mut x = x_in;
6863        let aux_e4b = self.gemma4_aux.as_ref().unwrap().e4b.as_ref().unwrap();
6864        let n_epl = aux_e4b.n_epl;
6865
6866        // cross-layer fusion (2026-07-12 port of the 26B/31B trunk structure): each layer's
6867        // closing add_scale also EMITS the next layer's attn-normed input pre-quantized
6868        // q8_1 (add_scale_rms_norm_q8_1); the LAST layer emits through output_norm so the
6869        // head rides matmul_pre too. First layer's pair comes from a standalone fused
6870        // norm+quant.
6871        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
6872        for il in 0..n_layer {
6873            let layer = &self.layers[il];
6874            let (hq, hdq) = match h_carry.take() {
6875                Some(p) => p,
6876                None => e.rms_norm_q8_1(&x, layer.attn_norm.float_data(), n_embd, t, eps)?,
6877            };
6878            let o = self.gemma4_e4b_attn(e, il, &hq, &hdq, pos_d, t, cache, dc_bucket)?;
6879            // dense ffn tail with the post-attn norm FOLDED into its entry (one launch for
6880            // rms(o, post_attn_norm) + residual add + ffn_norm — glue-fusion lane).
6881            let bits = layer.gemma4.as_ref().unwrap();
6882            let e4b = bits.e4b.as_ref().expect("e4b layer bits");
6883            // glue wave 5: the tail DEFERS its post_ffw norm — the FFN exit fuses
6884            // rms(f0, post_ffw) + residual add + q8 emit into ONE launch (rms_pre_add_q8_1),
6885            // killing the rms_norm + add_q8_1 pair per layer. T-GENERIC since 2026-07-13:
6886            // the fused single-phase reduction is NOT FP-order-identical to the unfused
6887            // rms_norm+add pair (the original "bit-identical chain" claim was FALSE — the
6888            // t==1 gate left the batched VERIFY on the unfused chain and split E4B verify
6889            // from decode by logit maxdiff ~0.45, the greedy tie-flip at depth: gate 135/256.
6890            // NOFUSE bisect: disabling ONLY this fusion -> verify maxdiff 0.000e0). With the
6891            // gate dropped, decode AND verify ride the same fused chain — parity by
6892            // construction, VERIFY-GATE 0.000e0.
6893            let fuse_exit = e.uses_q8_1_fast(&e4b.inp_gate);
6894            let (sn, attn_out) = self.gemma4_layer_tail_core_pn(
6895                e, layer, &o, &x, t, Some(layer.post_attn_norm.float_data()), fuse_exit)?;
6896            let mut resid = e.uninit(t * n_embd)?;
6897            // per-layer-embedding tail: resid += rms(proj . (gelu(inp_gate . resid) * inp_pl[il]))
6898            // wave-2: the residual add emits q8_1 alongside — inp_gate rides matmul_pre.
6899            // (PLE one-block mega-fusion PROBED NEGATIVE 2026-07-13: argmax-correct but
6900            // 126 vs 189 tok/s — one SM pulling 0.74MB of weights loses to the multi-block
6901            // launch chain it replaced; jsonl row. Kernel deleted per doctrine.)
6902            let g = if fuse_exit {
6903                // sn here = RAW f0 (post_ffw deferred).
6904                let (rq, rd) = e.rms_pre_add_q8_1(&sn, bits.post_ffw_norm.float_data(),
6905                                                  &attn_out, &mut resid, n_embd, t,
6906                                                  self.cfg.rms_eps)?;
6907                e.matmul_pre(&e4b.inp_gate, &rq, &rd, &resid, t)?
6908            } else {
6909                e.add(&sn, &attn_out, &mut resid, t * n_embd)?;
6910                e.matmul(&e4b.inp_gate, &resid, t)?
6911            };
6912            let mut act = e.uninit(t * n_epl)?;
6913            let y = if t == 1 && e.uses_q8_1_fast(&e4b.proj) {
6914                let ipv = e.view(&inp_pl, n_epl * n_layer);
6915                let row = ipv.slice(il * n_epl..(il + 1) * n_epl);
6916                let (aq, ad) = e.gelu_tanh_mul_q8_1(&g, &row, &mut act, n_epl, 1)?;
6917                e.matmul_pre(&e4b.proj, &aq, &ad, &act, t)?
6918            } else {
6919                let mut inp_this = e.uninit(t * n_epl)?;
6920                e.copy_rows_strided(&inp_pl, &mut inp_this, n_epl, t, n_epl * n_layer,
6921                                    il * n_epl)?;
6922                e.gelu_tanh_mul(&g, &inp_this, &mut act, t * n_epl)?;
6923                e.matmul(&e4b.proj, &act, t)?
6924            };
6925            // rms(y, post_norm) + (yn + resid)*layer_scale + next-layer norm+quant emit,
6926            // ONE launch (glue-fusion lane; last layer emits through output_norm).
6927            let next_norm = if il + 1 < n_layer {
6928                self.layers[il + 1].attn_norm.float_data()
6929            } else {
6930                self.output_norm.float_data()
6931            };
6932            let mut xn = e.uninit(t * n_embd)?;
6933            let pair = e.rms_pre_add_scale_rms_norm_q8_1(&y, e4b.post_norm.float_data(),
6934                                                         &resid, bits.layer_scale, next_norm,
6935                                                         &mut xn, n_embd, t, eps)?;
6936            h_carry = Some(pair);
6937            x = xn;
6938        }
6939        // the head consumes the last layer's fused (output_norm) emit. head_last callers
6940        // (prime, last_only forward) need only the final row's logits — the all-T head is
6941        // t*n_vocab of discarded work (E4B prime: ~134ms GEMM + a 2.26GB dtoh kept 1 row).
6942        let (oq, odq) = h_carry.take().unwrap();
6943        let h0 = e.zeros(0)?;
6944        let hm = if head_last { 1 } else { t };
6945        let (hq, hd) = if head_last && t > 1 {
6946            let mut q1 = e.uninit_i8(n_embd)?;
6947            e.dtod_copy_view_i8(&oq.slice((t - 1) * n_embd..t * n_embd), &mut q1)?;
6948            let nb = n_embd / 32;
6949            let mut d1 = e.uninit(nb)?;
6950            e.dtod_copy_view(&odq.slice((t - 1) * nb..t * nb), &mut d1)?;
6951            (q1, d1)
6952        } else {
6953            (oq, odq)
6954        };
6955        let mut ld = e.matmul_pre(&self.output, &hq, &hd, &h0, hm)?;
6956        // softcap is strictly monotonic — greedy (argmax-only) consumers skip it, matching
6957        // the 26B/31B dc precedent (their dc head goes matmul -> argmax with no cap).
6958        // Logit-returning callers (host logits / spec prime) keep the capped emit.
6959        if cap_logits {
6960            let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
6961            e.softcap(&mut ld, cap, hm * self.output.out_features())?;
6962        }
6963        self.gemma4_suppress(e, &mut ld, hm)?;  // mask both capped and argmax-only consumers
6964        Ok((ld, x))
6965    }
6966
6967    /// E4B batched VERIFY (device tokens, the spec round's t=K+1 step): t rows through the
6968    /// e4b trunk (per-row causal attention; own-KV layers append t rows host-len, KV-shared
6969    /// layers ride their targets), per-row device argmax + the POST-output_norm hidden
6970    /// stack (the drafter's h convention). Advances cache.pos/kvl.len by t — the spec
6971    /// round rolls back rejected rows (shared layers have no KvLayer, so the plain rewind
6972    /// covers exactly the layers that appended).
6973    pub fn gemma4_e4b_decode_step_t_am_dev(&self, e: &Engine, tok_d: &CudaSlice<u32>,
6974                                                  t: usize, pos0: usize, cache: &mut Cache)
6975                                                  -> Result<(CudaSlice<u32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6976        let n_embd = self.cfg.n_embd as usize;
6977        let eps = self.cfg.rms_eps;
6978        let pos: Vec<i32> = (0..t).map(|i| (pos0 + i) as i32).collect();
6979        let pos_d = e.htod_i32(&pos)?;
6980        let embd_gpu = self.embd_gpu.get_or_init(|| {
6981            e.upload_u8(&self.embd.raw).expect("embed table upload")
6982        });
6983        let (qt, rb) = self.embd.qt_and_row_bytes(n_embd);
6984        let mut x = e.embed_gather_device_td(embd_gpu, tok_d, t, n_embd, qt, rb)?;
6985        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
6986        let inp_pl = self.gemma4_e4b_inp_pl_dev(e, tok_d, &x, t)?;
6987        let (ld, xp) = self.gemma4_e4b_trunk_core(e, x, inp_pl, &pos_d, t, cache, None, true,
6988                                                  false)?;
6989        // softcap is monotonic — the per-row argmax is invariant to it (the trunk's head
6990        // emit is already capped, matching the eager chain bit-for-bit).
6991        let n_vocab = self.output.out_features();
6992        let mut vam = e.stream().alloc_zeros::<u32>(t)?;
6993        for i in 0..t {
6994            e.argmax_token_device_col(&ld, i, n_vocab, &mut vam, i)?;
6995        }
6996        let mut hn = e.uninit(t * n_embd)?;
6997        e.rms_norm(&xp, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
6998        cache.pos += t;
6999        Ok((vam, hn))
7000    }
7001
7002    /// E4B verify + host logits + POST-output_norm hidden stack (the short-prompt spec
7003    /// prime path — mirror of `gemma4_decode_step_t_h`).
7004    pub(crate) fn gemma4_e4b_decode_step_t_h(&self, e: &Engine, tokens: &[u32], pos0: usize,
7005                                             cache: &mut Cache)
7006                                             -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7007        let n_embd = self.cfg.n_embd as usize;
7008        let eps = self.cfg.rms_eps;
7009        let t = tokens.len();
7010        let (ld, xp) = self.gemma4_e4b_trunk(e, tokens, pos0, cache, false)?;
7011        let mut hn = e.uninit(t * n_embd)?;
7012        e.rms_norm(&xp, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
7013        cache.pos += t;
7014        Ok((e.dtoh(&ld)?, hn))
7015    }
7016
7017    /// E4B GRAPH-CAPTURABLE dc step: same trunk as the dc step but token_d is updated IN
7018    /// PLACE (self-feeding replay) and every launch arg is a device counter — pos from
7019    /// pos_d (inc'd in-stream), KV slots from len_d (advanced in-stream), attention from
7020    /// fa_decode_dc at `bucket`. Host mirrors (cache.pos / kvl.len) advance in the caller's
7021    /// replay loop. UNDER-WINDOW regime only (the caller gates pos + budget < window).
7022    pub fn gemma4_e4b_decode_step_dcg(&self, e: &Engine, token_d: &mut CudaSlice<u32>,
7023                                      pos_d: &mut CudaSlice<i32>, embd_gpu: &CudaSlice<u8>,
7024                                      embd_qt: i32, embd_rb: usize, cache: &mut Cache,
7025                                      n_vocab: usize, bucket: usize)
7026                                      -> Result<(), Box<dyn std::error::Error>> {
7027        let n_embd = self.cfg.n_embd as usize;
7028        let mut x = e.embed_gather_device(embd_gpu, token_d, n_embd, embd_qt, embd_rb)?;
7029        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
7030        let inp_pl = self.gemma4_e4b_inp_pl_dev(e, token_d, &x, 1)?;
7031        let (ld, _x) = self.gemma4_e4b_trunk_core(e, x, inp_pl, pos_d, 1, cache, Some(bucket),
7032                                                  false, false)?;
7033        e.argmax_token_device_into(&ld, token_d, n_vocab)?;
7034        e.inc_seqlen(pos_d)?;
7035        Ok(())
7036    }
7037
7038    /// E4B DEVICE-COUNTER decode step (the dc serving arm): token id rides `token_d`, the
7039    /// greedy argmax lands in the returned device buffer — 4B/token host traffic. The layer
7040    /// stack is gemma4_e4b_trunk_core, i.e. the SAME functions the eager chain runs (stream
7041    /// identity by construction, not by twin-kernel parity). Host KV mirrors advance like the
7042    /// 26B dc-eager arm (window views are host math); len_d stays synced by the caller's
7043    /// entry sync + the appends here don't read it. Graph capture is NOT wired (no
7044    /// cap_bucket_max) — the E4B graph arc comes after the perf gates.
7045    #[allow(clippy::too_many_arguments)]
7046    pub fn gemma4_e4b_decode_step_dc(&self, e: &Engine, token_d: &CudaSlice<u32>,
7047                                     pos_d: &mut CudaSlice<i32>, embd_gpu: &CudaSlice<u8>,
7048                                     embd_qt: i32, embd_rb: usize, cache: &mut Cache,
7049                                     n_vocab: usize)
7050                                     -> Result<CudaSlice<u32>, Box<dyn std::error::Error>> {
7051        let n_embd = self.cfg.n_embd as usize;
7052        let eps = self.cfg.rms_eps;
7053        let mut x = e.embed_gather_device(embd_gpu, token_d, n_embd, embd_qt, embd_rb)?;
7054        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
7055        let inp_pl = self.gemma4_e4b_inp_pl_dev(e, token_d, &x, 1)?;
7056        let (ld, _x) = self.gemma4_e4b_trunk_core(e, x, inp_pl, pos_d, 1, cache, None, false,
7057                                                  false)?;
7058        let mut tok_out = e.stream().alloc_zeros::<u32>(1)?;
7059        e.argmax_token_device_into(&ld, &mut tok_out, n_vocab)?;
7060        e.inc_seqlen(pos_d)?;
7061        cache.pos += 1;
7062        let _ = eps;
7063        Ok(tok_out)
7064    }
7065
7066    /// E4B eager T=1 decode step (decode_step_h contract): returns (softcapped logits host,
7067    /// pre-output_norm hidden). Advances cache.pos.
7068    pub(crate) fn gemma4_e4b_decode_step_h(&self, e: &Engine, token: u32, cache: &mut Cache)
7069                                           -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7070        let (ld, x) = self.gemma4_e4b_trunk(e, &[token], cache.pos, cache, false)?;
7071        let logits = e.dtoh(&ld)?;
7072        cache.pos += 1;
7073        Ok((logits, x))
7074    }
7075
7076    /// E4B batched prime (prime_cache contract: (last-row logits host, h_seed pre-norm last
7077    /// row, hidden stack)). First-light: the t-wide trunk (per-row attention) — correct, not
7078    /// fast; the prefill fa arms come later.
7079    pub(crate) fn gemma4_e4b_prime(&self, e: &Engine, tokens: &[u32], cache: &mut Cache)
7080                                   -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7081        assert_eq!(cache.pos, 0, "e4b prime is fresh-prompt only (v0)");
7082        let n_embd = self.cfg.n_embd as usize;
7083        let t = tokens.len();
7084        let (ld, x) = self.gemma4_e4b_trunk(e, tokens, 0, cache, true)?;
7085        cache.pos += t;
7086        let last = e.dtoh(&ld)?;   // head_last: ld is already the final row only
7087        let xv = e.view(&x, t * n_embd);
7088        let row = xv.slice((t - 1) * n_embd..t * n_embd);
7089        let mut h_seed = e.uninit(n_embd)?;
7090        e.copy_view_into(&mut h_seed, 0, &row, n_embd)?;
7091        Ok((last, h_seed, x))
7092    }
7093
7094    /// E4B prefill logits (forward contract — no persistent cache; scratch cache internally).
7095    pub(crate) fn gemma4_e4b_forward(&self, e: &Engine, tokens: &[u32], last_only: bool)
7096                                     -> Result<Vec<f32>, Box<dyn std::error::Error>> {
7097        let mut cache = Cache::new(e, &self.cfg, tokens.len() + 8)?;
7098        let (ld, _x) = self.gemma4_e4b_trunk(e, tokens, 0, &mut cache, last_only)?;
7099        Ok(e.dtoh(&ld)?)   // head_last already reduced to the final row when last_only
7100    }
7101}
7102
7103#[cfg(test)]
7104mod page_prefetch_tests {
7105    use super::{
7106        grouped_worker_prefetch_position, page_prefetch_positions,
7107        page_prefetch_window_from_values, worker_prefetch_positions,
7108    };
7109
7110    #[test]
7111    fn page_prefetch_window_keeps_existing_opt_in_default() {
7112        assert_eq!(page_prefetch_window_from_values(false, None), 0);
7113        assert_eq!(page_prefetch_window_from_values(false, Some("8")), 0);
7114        assert_eq!(page_prefetch_window_from_values(true, None), 1);
7115        assert_eq!(page_prefetch_window_from_values(true, Some("bad")), 1);
7116        assert_eq!(page_prefetch_window_from_values(true, Some("0")), 0);
7117        assert_eq!(page_prefetch_window_from_values(true, Some("8")), 8);
7118    }
7119
7120    #[test]
7121    fn rolling_page_prefetch_advises_each_future_expert_once() {
7122        let advised: Vec<_> = (0..7)
7123            .flat_map(|position| page_prefetch_positions(position, 7, 3))
7124            .collect();
7125        assert_eq!(advised, vec![1, 2, 3, 4, 5, 6]);
7126
7127        let one_ahead: Vec<_> = (0..4)
7128            .flat_map(|position| page_prefetch_positions(position, 4, 1))
7129            .collect();
7130        assert_eq!(one_ahead, vec![1, 2, 3]);
7131        assert!(page_prefetch_positions(0, 4, 0).is_empty());
7132    }
7133
7134    #[test]
7135    fn grouped_worker_prefetch_primes_first_then_each_known_next_once() {
7136        assert_eq!(grouped_worker_prefetch_position(0, None), None);
7137        let positions: Vec<_> = std::iter::once(grouped_worker_prefetch_position(4, None).unwrap())
7138            .chain((0..4).filter_map(|position| {
7139                grouped_worker_prefetch_position(4, Some(position))
7140            }))
7141            .collect();
7142        assert_eq!(positions, vec![0, 1, 2, 3]);
7143        assert_eq!(grouped_worker_prefetch_position(1, Some(0)), None);
7144    }
7145
7146    #[test]
7147    fn rolling_worker_prefetch_primes_current_and_each_future_expert_once() {
7148        let queued: Vec<_> = (0..8)
7149            .flat_map(|position| worker_prefetch_positions(position, 8, 5))
7150            .collect();
7151        assert_eq!(queued, (0..8).collect::<Vec<_>>());
7152
7153        let one_at_a_time: Vec<_> = (0..4)
7154            .flat_map(|position| worker_prefetch_positions(position, 4, 1))
7155            .collect();
7156        assert_eq!(one_at_a_time, vec![0, 1, 2, 3]);
7157        assert!(worker_prefetch_positions(0, 4, 0).is_empty());
7158    }
7159}
7160
7161pub struct G4DcSlots {
7162    x: CudaSlice<f32>, xn: CudaSlice<f32>, cur: CudaSlice<f32>,
7163    hq: CudaSlice<i8>, hd_: CudaSlice<f32>,
7164    q0: CudaSlice<f32>, k0: CudaSlice<f32>, v0: CudaSlice<f32>,
7165    q: CudaSlice<f32>, k: CudaSlice<f32>, v: CudaSlice<f32>,
7166    attn: CudaSlice<f32>, o: CudaSlice<f32>,
7167    attn_out: CudaSlice<f32>, zsh: CudaSlice<f32>,
7168    zq: CudaSlice<i8>, zd: CudaSlice<f32>,
7169    gate: CudaSlice<f32>, up: CudaSlice<f32>,
7170    act: CudaSlice<f32>, actq: CudaSlice<i8>, actd: CudaSlice<f32>,
7171    f0: CudaSlice<f32>, sn: CudaSlice<f32>,
7172    hn: CudaSlice<f32>, logits: CudaSlice<f32>,
7173}
7174