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 crate::Engine;
6use crate::cache::Cache;
7use cudarc::driver::CudaSlice;
8use memra_gguf::config::ModelConfig;
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// Split prime ranges cannot enter the full-range segment-graph arm, and every slab access
41// is serialized by its device mutex after binding that device's CUDA context on the thread.
42unsafe impl Send for PrimeSlabs {}
43
44fn empty_cache_layers<T>(n: usize) -> Vec<Option<T>> {
45    std::iter::repeat_with(|| None).take(n).collect()
46}
47
48/// Temporarily move a PP-2 cache's layer state into two independently-owned cache shells.
49/// The stage walkers then receive disjoint `&mut Cache` values and can run on separate host
50/// threads without aliasing. GPU buffers are moved, not copied; Drop restores every layer
51/// and publishes the last position completed by both stages.
52struct PrimeCacheStages<'a> {
53    parent: &'a mut Cache,
54    cut: usize,
55    stage0: Cache,
56    stage1: Cache,
57}
58
59impl<'a> PrimeCacheStages<'a> {
60    fn new(parent: &'a mut Cache, cut: usize) -> Self {
61        let n = parent.kv.len();
62        assert_eq!(parent.recur.len(), n, "cache layer vectors disagree");
63        assert!(cut <= n, "PP-2 cache cut {cut} exceeds {n} layers");
64        let mut kv0 = empty_cache_layers(n);
65        let mut kv1 = empty_cache_layers(n);
66        let mut recur0 = empty_cache_layers(n);
67        let mut recur1 = empty_cache_layers(n);
68        for i in 0..cut {
69            kv0[i] = parent.kv[i].take();
70            recur0[i] = parent.recur[i].take();
71        }
72        for i in cut..n {
73            kv1[i] = parent.kv[i].take();
74            recur1[i] = parent.recur[i].take();
75        }
76        let pos = parent.pos;
77        let max_ctx = parent.max_ctx;
78        Self {
79            parent,
80            cut,
81            stage0: Cache {
82                kv: kv0,
83                recur: recur0,
84                pos,
85                max_ctx,
86                last_logits_dev: None,
87                dflash_taps: None,
88            },
89            stage1: Cache {
90                kv: kv1,
91                recur: recur1,
92                pos,
93                max_ctx,
94                last_logits_dev: None,
95                dflash_taps: None,
96            },
97        }
98    }
99
100    fn parts(&mut self) -> (&mut Cache, &mut Cache) {
101        (&mut self.stage0, &mut self.stage1)
102    }
103}
104
105impl Drop for PrimeCacheStages<'_> {
106    fn drop(&mut self) {
107        let n = self.parent.kv.len();
108        for i in 0..n {
109            let source = if i < self.cut {
110                &mut self.stage0
111            } else {
112                &mut self.stage1
113            };
114            debug_assert!(self.parent.kv[i].is_none());
115            debug_assert!(self.parent.recur[i].is_none());
116            self.parent.kv[i] = source.kv[i].take();
117            self.parent.recur[i] = source.recur[i].take();
118        }
119        self.parent.pos = self.stage0.pos.min(self.stage1.pos);
120    }
121}
122
123/// task #18 (attn side): one sequence's pre-attention outputs (post-rope q/k, v, out-gate).
124pub(crate) struct AttnPre {
125    pub q: cudarc::driver::CudaSlice<f32>,
126    pub k: cudarc::driver::CudaSlice<f32>,
127    pub v: cudarc::driver::CudaSlice<f32>,
128    pub gate: Option<cudarc::driver::CudaSlice<f32>>,
129}
130
131/// task #18: one sequence's GDN prep outputs (the scan inputs).
132pub(crate) struct GdnPrep {
133    pub hk: usize,
134    pub q_l2: cudarc::driver::CudaSlice<f32>,
135    pub k_l2: cudarc::driver::CudaSlice<f32>,
136    pub v_g: cudarc::driver::CudaSlice<f32>,
137    pub beta: cudarc::driver::CudaSlice<f32>,
138    pub g_log: cudarc::driver::CudaSlice<f32>,
139    pub kb16: Option<cudarc::driver::CudaSlice<u8>>,
140    pub qb16: Option<cudarc::driver::CudaSlice<u8>>,
141}
142
143/// Device scratch for the burst verify stream (see `verify_stream_scratch`).
144pub(crate) struct VerifyStreamScratch {
145    pub pos_d: CudaSlice<i32>,
146    pub row_ctrs: Vec<CudaSlice<i32>>,
147}
148use crate::hybrid::{FullAttnLayer, HybridModel, LinearAttnLayer, Mixer, MoeWeights};
149
150struct MoeInputTraceWriter {
151    dir: std::path::PathBuf,
152    index: std::fs::File,
153    payloads: std::collections::HashMap<u16, (std::fs::File, u64)>,
154}
155
156static MOE_INPUT_TRACE_WRITER: std::sync::OnceLock<std::sync::Mutex<Option<MoeInputTraceWriter>>> =
157    std::sync::OnceLock::new();
158
159/// STAGE-2 GROUPED DECODE gate (MEMRA_MOE_GDEC, default ON; `=0` restores the sequential
160/// per-expert launch chain). See `moe_gdec_token`.
161fn gdec_enabled() -> bool {
162    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
163    *E.get_or_init(|| {
164        std::env::var("MEMRA_MOE_GDEC")
165            .map(|v| v != "0")
166            .unwrap_or(true)
167    })
168}
169
170/// SLAB-LOCAL RESIDENT ARM gate (lane/pp-leverb 2026-08-08, MEMRA_MOE_SLAB, default ON;
171/// `=0` restores the SLRU dispatch even when resident slabs exist). Read PER CALL, never
172/// memoized — probes A/B the two provenances in one process (the MEMRA_PRIME_PP pattern).
173/// See `moe_ffn_sequential_zq8`'s slab_local arm: the sigmoid-router archs (step35/M3/Hy3)
174/// are denied every `dev_exps` consumer (pairs/dev route softmax), so before this arm the
175/// fits-VRAM resident slabs were UPLOADED for them but never READ — the SLRU kept staging
176/// the same bytes beside a dead copy (37 GB H2D per pp4096 prime on the Step SKU, anatomy
177/// receipt). The arm reads the SAME bytes through the SAME kernels; only the pointer
178/// PROVENANCE changes (slab base + ex*stride vs SLRU slot address) — the bit-identity class
179/// `moe_ffn_dev`'s resident arm already documents against its SLRU arm.
180fn moe_slab_enabled() -> bool {
181    std::env::var("MEMRA_MOE_SLAB").as_deref() != Ok("0")
182}
183
184/// Expert-grouped dispatch remains opt-in after the local 5090 transfer gate rejected the
185/// default flip. `=0` selects the established path, while any other explicit value enables the
186/// grouped research arm for the current call.
187fn moe_grouped_enabled(_cfg: &ModelConfig, _prefill: bool) -> bool {
188    std::env::var("MEMRA_MOE_GROUPED")
189        .map(|value| value != "0")
190        .unwrap_or(false)
191}
192
193/// Deterministic in-token expert prefetch. `MEMRA_MOE_PREFETCH=1` overlaps memory-source H2D on the
194/// copy stream; selecting the opt-in worker spill backend enables the same known-next hook for disk.
195fn moe_prefetch_enabled() -> bool {
196    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
197    *E.get_or_init(|| {
198        std::env::var("MEMRA_MOE_PREFETCH").as_deref() == Ok("1")
199            || crate::spill_pread::worker_enabled()
200    })
201}
202
203/// Best-effort OS page-cache prefetch distance for mmap-backed expert ranges. Independent of the
204/// H2D copy-stream experiment so storage->RAM and RAM->HBM overlap can be measured separately.
205/// The opt-in default stays one expert to preserve the original experiment; spill rigs can widen
206/// it with `MEMRA_MOE_PAGE_PREFETCH_WINDOW` to cover NVMe latency.
207fn moe_page_prefetch_window() -> usize {
208    static W: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
209    *W.get_or_init(|| {
210        page_prefetch_window_from_values(
211            std::env::var("MEMRA_MOE_PAGE_PREFETCH").as_deref() == Ok("1"),
212            std::env::var("MEMRA_MOE_PAGE_PREFETCH_WINDOW")
213                .ok()
214                .as_deref(),
215        )
216    })
217}
218
219fn page_prefetch_window_from_values(enabled: bool, raw_window: Option<&str>) -> usize {
220    if !enabled {
221        return 0;
222    }
223    raw_window.and_then(|value| value.parse().ok()).unwrap_or(1)
224}
225
226/// Return only the newly exposed positions in a rolling lookahead window. Position zero seeds the
227/// full window; each later position adds one expert at the far edge. Thus widening the window does
228/// not repeatedly issue `MADV_WILLNEED` for the same range.
229fn page_prefetch_positions(position: usize, len: usize, window: usize) -> std::ops::Range<usize> {
230    if window == 0 || position >= len {
231        return len..len;
232    }
233    let (start, count) = if position == 0 {
234        (1, window)
235    } else {
236        (position.saturating_add(window), 1)
237    };
238    let start = start.min(len);
239    start..start.saturating_add(count).min(len)
240}
241
242/// Grouped worker-I/O schedule: prime the first active expert before the loop, then queue exactly
243/// one known-next expert at each iteration. Returning positions keeps expert ordering authoritative.
244fn grouped_worker_prefetch_position(order_len: usize, current: Option<usize>) -> Option<usize> {
245    let position = current.map_or(0, |position| position.saturating_add(1));
246    (position < order_len).then_some(position)
247}
248
249/// Fill the worker ring with complete experts, retaining one pinned buffer for an unexpected
250/// demand miss. Each expert has gate/up/down extents, so depth 16 admits a rolling five-expert
251/// window. Position zero primes the current expert too: its three independent reads can run in
252/// parallel instead of demand-serializing gate, up, and down before any useful GPU work exists.
253fn worker_prefetch_window() -> usize {
254    static WINDOW: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
255    *WINDOW.get_or_init(|| {
256        let automatic = crate::spill_pread::configured_depth().saturating_sub(1) / 3;
257        std::env::var("MEMRA_SPILL_WORKER_EXPERT_WINDOW")
258            .ok()
259            .and_then(|value| value.parse::<usize>().ok())
260            .unwrap_or(automatic.max(1))
261    })
262}
263
264/// Return only positions newly exposed by a rolling worker-I/O window. Unlike mmap page advice,
265/// this includes the current expert when the window is seeded so all three current projections
266/// enter the CPU pool together.
267fn worker_prefetch_positions(position: usize, len: usize, window: usize) -> std::ops::Range<usize> {
268    if window == 0 || position >= len {
269        return len..len;
270    }
271    let (start, count) = if position == 0 {
272        (0, window)
273    } else {
274        (position.saturating_add(window).saturating_sub(1), 1)
275    };
276    let start = start.min(len);
277    start..start.saturating_add(count).min(len)
278}
279
280/// LAUNCH-STRUCTURE STAGE 3 gate (MEMRA_MOE_DEV, default ON; `=0` restores host routing). The
281/// zero-DtoH device-dispatch path for fully-resident layers: router top-k output stays on device,
282/// expert weight pointers come from the per-layer device table. Requires the fused router (the
283/// dev path consumes the device sel/w directly), so MEMRA_FUSED_ROUTER=0 also disables it.
284fn moe_dev_enabled() -> bool {
285    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
286    *E.get_or_init(|| {
287        std::env::var("MEMRA_MOE_DEV")
288            .map(|v| v != "0")
289            .unwrap_or(true)
290            && !matches!(std::env::var("MEMRA_FUSED_ROUTER").as_deref(), Ok("0"))
291    })
292}
293
294/// Device sigmoid top-k is the default for Step-3.7 / M3 / Hy3 / GLM-DSA. `MEMRA_SIG_ROUTER=0` restores
295/// the full-logit DtoH plus `moe_route_sigmoid_host` oracle without changing expert dispatch.
296fn sigmoid_router_enabled() -> bool {
297    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
298    *E.get_or_init(|| {
299        std::env::var("MEMRA_SIG_ROUTER")
300            .map(|v| v != "0")
301            .unwrap_or(true)
302    })
303}
304
305/// MoE EXPERT dp4a gate (MEMRA_MOE_Q8, default ON; `=0` restores the Stage-A f32-dequant expert
306/// kernels). Applies when gate/up/down expert qtypes are all in the dp4a body set (IQ3_S/IQ4_XS).
307/// FP-order differs from Stage-A (int dp4a + warp tree) — argmax/run-gen/stream-identity gates
308/// arbitrate; the sequential and fused q8 paths ship as a matched pair (MEMRA_MOE_GATE contract).
309fn moe_q8_enabled() -> bool {
310    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
311    *E.get_or_init(|| {
312        std::env::var("MEMRA_MOE_Q8")
313            .map(|v| v != "0")
314            .unwrap_or(true)
315    })
316}
317
318/// gemma4 fast-arm gate: qtypes with an `expert_dot_g` dp4a body (superset used by the gelu
319/// dev arm; the qwen q8 arms keep their own battery-gated q8_expert_supported policy).
320fn expert_dp4a_supported(qt: i32) -> bool {
321    qt == crate::QT_Q4_0
322        || qt == crate::QT_IQ3_S
323        || qt == crate::QT_IQ4_XS
324        || qt == crate::QT_Q3_K
325        || qt == crate::QT_Q4_K
326        || qt == crate::QT_Q6_K
327}
328
329fn q8_expert_supported(qt: i32) -> bool {
330    // k-quant arms added 2026-07-06 (Q3_K/Q4_K/Q6_K bodies for the UD tail layers). Briefly
331    // default-excluded the same day when they appeared to break 35B real-prompt spec — the
332    // ACTUAL culprit was the MoE router's cuBLASLt n-dependence (d994271); with the router
333    // decode-exact at verify t, the k-quant arms pass the full spec battery (p1/p2/p3 + raw
334    // K=1..8) and are DEFAULT ON again (+9 tok/s: 148.9 -> 157.9). MEMRA_MOE_Q8_KQ=0 excludes.
335    static KQ: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
336    let kq = *KQ.get_or_init(|| {
337        std::env::var("MEMRA_MOE_Q8_KQ")
338            .map(|v| v != "0")
339            .unwrap_or(true)
340    });
341    // NVFP4 experts: DEFAULT ON (2026-07-17). The M3-era "decode-vs-verify MISMATCH 3.4e1"
342    // that had this excluded was the missing per-expert macro-scale fold, fixed in the
343    // dev-kernel epilogues + moe_w_scale_by_expert; the 35B ct-NVFP4 artifact now runs the
344    // q8 arm at parity with the IQ4_XS daily (174-178 tok/s, spec K=1..8 exact). M3/Hy3
345    // never reach the q8 arms regardless (sigmoid-router cfg gates on pairs/dev/gdec).
346    // MEMRA_MOE_Q8_NVFP4=0 restores the f32 arm.
347    let nvfp4_q8 = std::env::var("MEMRA_MOE_Q8_NVFP4")
348        .map(|v| v != "0")
349        .unwrap_or(true);
350    qt == crate::QT_IQ3_S
351        || qt == crate::QT_IQ4_XS
352        || (nvfp4_q8 && qt == crate::QT_NVFP4)
353        || (kq && (qt == crate::QT_Q3_K || qt == crate::QT_Q4_K || qt == crate::QT_Q6_K))
354}
355
356/// The decode-once (_dec) and IQ-MMA expert kernels dequant via IQ-specific extractors —
357/// k-quant tensors must fall to the _em dot path instead.
358fn q8_expert_dec_supported(qt: i32) -> bool {
359    qt == crate::QT_IQ3_S || qt == crate::QT_IQ4_XS || qt == crate::QT_Q4_0
360}
361
362/// Grouped-f16 door (MEMRA_MOE_F16G) per-projection admission: the qtype has a dequant-to-f16
363/// kernel in cu/moe_f16_grouped.cu AND the projection's k dimension tiles its block size.
364/// Round 49 widened coverage to q35's UD mix (gate/up IQ3_S x39 + Q3_K x1 + IQ4_XS x1; down
365/// IQ4_XS x37 + Q6_K x3 + Q4_K x1) — the round-47 IQ4_XS/Q4_0-only table admitted ~1 of 41
366/// q35 layers, which is why that cell measured FLAT.
367fn f16g_proj_ok(qt: i32, in_f: usize) -> bool {
368    match qt {
369        crate::QT_Q4_0 => in_f % 32 == 0,
370        crate::QT_IQ4_XS | crate::QT_IQ3_S | crate::QT_Q3_K | crate::QT_Q4_K | crate::QT_Q6_K => {
371            in_f % 256 == 0
372        }
373        _ => false,
374    }
375}
376
377/// STAGE 3 prewarm gate (MEMRA_MOE_PREWARM, default ON; `=0` leaves residency organic). One-shot
378/// per layer: force-admit every block while FREE slots cover the whole layer (never evicts).
379fn moe_prewarm_enabled() -> bool {
380    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
381    *E.get_or_init(|| {
382        std::env::var("MEMRA_MOE_PREWARM")
383            .map(|v| v != "0")
384            .unwrap_or(true)
385    })
386}
387
388/// During a discarded fixed-residency profile, admit CPU-routed misses after their current-token
389/// CPU result is complete. The current result and numeric path are unchanged; later warmup tokens
390/// can then vote for and exercise those experts on GPU before the cache is frozen.
391fn cpu_expert_profile_admit_enabled() -> bool {
392    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
393    *E.get_or_init(|| std::env::var("MEMRA_CPU_EXPERT_FREEZE_PROFILE_ADMIT").as_deref() == Ok("1"))
394}
395
396/// Minimum prompt length for the BATCHED cache prime (`prime_cache`). Below this the tokenwise
397/// decode loop wins anyway (the batched path's GEMM dispatch needs m>=16, and the stateful conv
398/// kernel needs T >= d_conv-1). Callers: generate / generate_spec.
399pub const PRIME_MIN_T: usize = 16;
400const PRIME_PIPE_MICROBATCHES: usize = 8;
401const PRIME_PIPE_MIN_CHUNK: usize = 128;
402const PRIME_PIPE_EDGE_MIN_CHUNK: usize = 64;
403const PRIME_PIPE_LINEAR_WORK: usize = 8;
404
405fn prime_pp2_auto_geometry(n_layers: usize) -> bool {
406    crate::pp::prime_pp_on()
407        && !crate::pp::pp2_streams_off()
408        && crate::pp::pp_cuts(n_layers).is_some_and(|cuts| cuts.len() == 3)
409}
410
411/// Effective internal prime chunk. An explicit MEMRA_PRIME_CHUNK is authoritative.
412/// Naked PP-2 primes use the measured pipeline geometry: up to eight microchunks, never
413/// below 128 tokens, while the legacy 4096-token cap remains the long-context bound.
414pub fn prime_chunk_tokens(t: usize, n_layers: usize) -> usize {
415    if let Ok(value) = std::env::var("MEMRA_PRIME_CHUNK") {
416        let parsed = value
417            .parse::<usize>()
418            .unwrap_or(crate::cache::PRIME_CHUNK_MAX_TOKENS);
419        return if crate::cache::swa_ring_on() {
420            if parsed == 0 {
421                crate::cache::PRIME_CHUNK_MAX_TOKENS
422            } else {
423                parsed.min(crate::cache::PRIME_CHUNK_MAX_TOKENS)
424            }
425        } else {
426            parsed
427        };
428    }
429    let chunk = crate::cache::PRIME_CHUNK_MAX_TOKENS;
430    if prime_pp2_auto_geometry(n_layers) && t >= 2 * PRIME_PIPE_MIN_CHUNK {
431        chunk.min(
432            t.div_ceil(PRIME_PIPE_MICROBATCHES)
433                .max(PRIME_PIPE_MIN_CHUNK),
434        )
435    } else {
436        chunk
437    }
438}
439
440fn fixed_prime_chunk_ranges(t: usize, chunk: usize) -> Vec<(usize, usize)> {
441    fixed_prime_chunk_ranges_for_ring(t, chunk, crate::cache::swa_ring_on())
442}
443
444fn fixed_prime_chunk_ranges_for_ring(t: usize, chunk: usize, ring_on: bool) -> Vec<(usize, usize)> {
445    if chunk == 0 || t <= chunk {
446        return vec![(0, t)];
447    }
448    let mut ranges = Vec::with_capacity(t.div_ceil(chunk));
449    let mut start = 0usize;
450    while start < t {
451        let mut end = (start + chunk).min(t);
452        if t - end > 0 && t - end < PRIME_MIN_T {
453            if ring_on {
454                let shifted = t - PRIME_MIN_T;
455                end = if shifted > start { shifted } else { t };
456            } else {
457                end = t;
458            }
459        }
460        ranges.push((start, end));
461        start = end;
462    }
463    ranges
464}
465
466fn prime_chunk_work(prefix: usize, total: usize) -> u128 {
467    let prefix = prefix as u128;
468    prefix * (prefix + (PRIME_PIPE_LINEAR_WORK as u128) * (total as u128))
469}
470
471fn dynamic_prime_chunk_ranges(
472    t: usize,
473    fixed_chunk: usize,
474    fixed: &[(usize, usize)],
475) -> Vec<(usize, usize)> {
476    let n = fixed.len();
477    if n < 3 {
478        return fixed.to_vec();
479    }
480
481    let max_first = t - (n - 1) * PRIME_MIN_T;
482    let first = fixed_chunk
483        .div_ceil(2)
484        .max(PRIME_PIPE_EDGE_MIN_CHUNK)
485        .min(max_first);
486    let mut ranges = Vec::with_capacity(n);
487    ranges.push((0, first));
488
489    let first_work = prime_chunk_work(first, t);
490    let work_span = prime_chunk_work(t, t) - first_work;
491    let denominator = (n - 1) as u128;
492    let mut previous = first;
493    for boundary in 1..n - 1 {
494        let target = first_work * denominator + work_span * (boundary as u128);
495        let remaining = n - 1 - boundary;
496        let mut low = previous + PRIME_MIN_T;
497        let mut high = t - remaining * PRIME_MIN_T;
498        while low < high {
499            let mid = low + (high - low) / 2;
500            if prime_chunk_work(mid, t) * denominator >= target {
501                high = mid;
502            } else {
503                low = mid + 1;
504            }
505        }
506        ranges.push((previous, low));
507        previous = low;
508    }
509    ranges.push((previous, t));
510    ranges
511}
512
513/// Internal prime ranges. The naked PP-2 pipeline defaults to a short-fill,
514/// equal-modeled-time schedule; MEMRA_PRIME_CHUNK_SCHED=fixed restores the measured
515/// equal-token ranges. An explicit MEMRA_PRIME_CHUNK always retains fixed semantics.
516pub fn prime_chunk_ranges(t: usize, n_layers: usize) -> Vec<(usize, usize)> {
517    let explicit_chunk = std::env::var_os("MEMRA_PRIME_CHUNK").is_some();
518    let chunk = prime_chunk_tokens(t, n_layers);
519    let fixed = fixed_prime_chunk_ranges(t, chunk);
520    let dynamic = match std::env::var("MEMRA_PRIME_CHUNK_SCHED") {
521        Ok(value) => value == "dynamic",
522        Err(_) => true,
523    };
524    if explicit_chunk || !dynamic || !prime_pp2_auto_geometry(n_layers) {
525        fixed
526    } else {
527        dynamic_prime_chunk_ranges(t, chunk, &fixed)
528    }
529}
530
531impl HybridModel {
532    /// CHUNK-INVARIANCE BISECT SEAM (lane/chunk-invariance): `MEMRA_PRIME_TRACE=<path>`
533    /// appends one JSONL row per (chunk, layer) with a hash of that layer's last-row
534    /// post-residual hidden. Diagnostic only — never on in a measured or gated run
535    /// (it forces a dtoh + host hash per layer).
536    fn prime_trace_path() -> Option<&'static str> {
537        static P: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
538        P.get_or_init(|| std::env::var("MEMRA_PRIME_TRACE").ok())
539            .as_deref()
540    }
541
542    /// Prefill forward over `tokens`; returns logits [T, n_vocab] (host f32).
543    pub fn forward(
544        &self,
545        e: &Engine,
546        tokens: &[u32],
547    ) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
548        if self.is_gemma4_e4b() {
549            return self.gemma4_e4b_forward(e, tokens, false);
550        }
551        if self.cfg.gemma4.is_some() {
552            return self.gemma4_forward(e, tokens, false);
553        }
554        let cfg = &self.cfg;
555        let n_embd = cfg.n_embd as usize;
556        let t = tokens.len();
557        let eps = cfg.rms_eps;
558        let pos: Vec<i32> = (0..t as i32).collect();
559        let pos_d = e.htod_i32(&pos)?;
560
561        let mut x = self.embed(e, tokens)?; // [T, n_embd]
562
563        for (il, layer) in self.layers.iter().enumerate() {
564            // attn_norm
565            let mut h = e.uninit(t * n_embd)?;
566            e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
567
568            let mixed = match &layer.mixer {
569                Mixer::Full(fa) => self.full_attn(e, fa, &h, &pos_d, t, il)?,
570                Mixer::Linear(la) => self.linear_attn(e, la, &h, t)?,
571                Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
572            };
573
574            // residual 1
575            let mut x1 = e.uninit(t * n_embd)?;
576            e.add(&x, &mixed, &mut x1, t * n_embd)?;
577
578            // pre-FFN norm (post_attention_norm), FFN (Dense or MoE), residual 2
579            let mut z = e.uninit(t * n_embd)?;
580            e.rms_norm(
581                &x1,
582                layer.post_attn_norm.float_data(),
583                &mut z,
584                n_embd,
585                t,
586                eps,
587            )?;
588            let ffn_out = match &layer.ffn {
589                crate::hybrid::Ffn::Dense {
590                    ffn_gate,
591                    ffn_up,
592                    ffn_down,
593                } => {
594                    let n_ff = ffn_gate.out_features();
595                    let mut g2 = e.matmul_group(&[ffn_gate, ffn_up], &z, t)?;
596                    let up = g2.pop().unwrap();
597                    let gate = g2.pop().unwrap();
598                    let mut act = e.uninit(t * n_ff)?;
599                    // A DENSE FFN reads the SHEXP clamp array: upstream's one `build_ffn` serves
600                    // both the dense MLP and the shared expert, and its limit is
601                    // `swiglu_clamp_shexp[il]` (llama-graph.cpp:1751). step35's leading dense
602                    // blocks 0-2 therefore key off clamp_shexp, not clamp_exp.
603                    Self::ffn_act_lim(
604                        e,
605                        &self.cfg,
606                        &gate,
607                        &up,
608                        1.0,
609                        1.0,
610                        self.cfg.clamp_shexp_at(il as u32),
611                        &mut act,
612                        t * n_ff,
613                    )?;
614                    e.matmul(ffn_down, &act, t)?
615                }
616                crate::hybrid::Ffn::Moe(m) => self.moe_ffn_il_prefill(e, m, &z, t, il as u16)?,
617            };
618            let mut x2 = e.uninit(t * n_embd)?;
619            e.add(&x1, &ffn_out, &mut x2, t * n_embd)?;
620            x = x2;
621        }
622
623        let mut hn = e.uninit(t * n_embd)?;
624        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
625        let logits = e.matmul(&self.output, &hn, t)?;
626        Ok(e.dtoh(&logits)?)
627    }
628
629    /// Prefill that returns ONLY the last token's logits — the common case (greedy/sample needs
630    /// just the final position to start decode). Runs the trunk over all T, then the lm_head
631    /// (output.weight, the largest matrix — 248320 rows) on the LAST hidden row ONLY, not all T.
632    /// On a 512-token prompt this turns a [512,248320] GEMM into [1,248320] — the dominant prefill
633    /// cost (nsys: ~99ms when done for all T). Bit-identical last-row logits to forward()[last].
634    pub fn forward_last(
635        &self,
636        e: &Engine,
637        tokens: &[u32],
638    ) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
639        if self.cfg.gemma4.is_some() {
640            return self.gemma4_forward(e, tokens, true);
641        }
642        let cfg = &self.cfg;
643        let n_embd = cfg.n_embd as usize;
644        let t = tokens.len();
645        let eps = cfg.rms_eps;
646        let pos: Vec<i32> = (0..t as i32).collect();
647        let pos_d = e.htod_i32(&pos)?;
648
649        let mut x = self.embed(e, tokens)?; // [T, n_embd]
650        // MEMRA_LAYER_PROBE=1: synchronize + print after every stage — bisects an in-graph
651        // ILLEGAL_ADDRESS to (layer, stage) at ~1 line of output per layer (M3 bring-up tool).
652        let probe = std::env::var("MEMRA_LAYER_PROBE").is_ok();
653        for (il, layer) in self.layers.iter().enumerate() {
654            let mut h = e.uninit(t * n_embd)?;
655            e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
656            if probe {
657                e.stream().synchronize()?;
658                eprintln!("[probe] L{il} norm ok");
659            }
660            let mixed = match &layer.mixer {
661                Mixer::Full(fa) => self.full_attn(e, fa, &h, &pos_d, t, il)?,
662                Mixer::Linear(la) => self.linear_attn(e, la, &h, t)?,
663                Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
664            };
665            if probe {
666                e.stream().synchronize()?;
667                eprintln!("[probe] L{il} mixer ok");
668            }
669            let mut x1 = e.uninit(t * n_embd)?;
670            e.add(&x, &mixed, &mut x1, t * n_embd)?;
671            let mut z = e.uninit(t * n_embd)?;
672            e.rms_norm(
673                &x1,
674                layer.post_attn_norm.float_data(),
675                &mut z,
676                n_embd,
677                t,
678                eps,
679            )?;
680            let ffn_out = match &layer.ffn {
681                crate::hybrid::Ffn::Dense {
682                    ffn_gate,
683                    ffn_up,
684                    ffn_down,
685                } => {
686                    let n_ff = ffn_gate.out_features();
687                    let mut g2 = e.matmul_group(&[ffn_gate, ffn_up], &z, t)?;
688                    let up = g2.pop().unwrap();
689                    let gate = g2.pop().unwrap();
690                    let mut act = e.uninit(t * n_ff)?;
691                    // dense FFN keys off the SHEXP clamp array — see forward()'s note.
692                    Self::ffn_act_lim(
693                        e,
694                        &self.cfg,
695                        &gate,
696                        &up,
697                        1.0,
698                        1.0,
699                        self.cfg.clamp_shexp_at(il as u32),
700                        &mut act,
701                        t * n_ff,
702                    )?;
703                    e.matmul(ffn_down, &act, t)?
704                }
705                crate::hybrid::Ffn::Moe(m) => self.moe_ffn_il_prefill(e, m, &z, t, il as u16)?,
706            };
707            if probe {
708                e.stream().synchronize()?;
709                eprintln!("[probe] L{il} ffn ok");
710            }
711            let mut x2 = e.uninit(t * n_embd)?;
712            e.add(&x1, &ffn_out, &mut x2, t * n_embd)?;
713            x = x2;
714        }
715        // norm over all T, then slice the LAST row and run lm_head on that single row.
716        let mut hn = e.uninit(t * n_embd)?;
717        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
718        let last = e.view(&hn, t * n_embd); // [T, n_embd]
719        let last_row = last.slice((t - 1) * n_embd..t * n_embd); // [1, n_embd]
720        let mut hlast = e.uninit(n_embd)?;
721        e.copy_view_into(&mut hlast, 0, &last_row, n_embd)?;
722        let logits = e.matmul(&self.output, &hlast, 1)?; // [1, n_vocab] — lm_head on ONE row
723        Ok(e.dtoh(&logits)?)
724    }
725
726    /// BATCHED PROMPT PRIME (the measured #1 e2e gap, e2e-image-1): `forward_last`'s batched
727    /// prefill body EXTENDED to leave a DECODE-READY cache behind — vs the tokenwise prime's
728    /// ~102/38 tok/s (9B/27B) decode_step loop, this runs the whole prompt at prefill throughput.
729    ///   (a) full-attn layers append their T post-RoPE K/V rows into `cache.kv[il]` via the SAME
730    ///       per-row quantize kernel as the decode append (bit-identical cache bytes per row);
731    ///   (b) linear layers run STATEFULLY from the cache's current recurrent state (zero at a
732    ///       fresh prime): carried-ring conv (ssm_conv1d_tm_state) + ONE gdn_scan(state_in,
733    ///       state_out) whose internal sequential t-loop equals T chained T=1 steps — but with
734    ///       the NORMAL prefill matmul dispatch (GEMM at m>=16), NOT the decode-exact MMVQ the
735    ///       spec verify uses (prime is a prefill-regime pass; the run-gen prefill==decode
736    ///       argmax gate is the accuracy authority, exactly as for forward_last);
737    ///   (c) `cache.pos`/KV len/len_d advance by T.
738    /// Returns (last-row logits host, h_seed = last-row PRE-output_norm hidden [n_embd],
739    /// hiddens = the full pre-output_norm hidden stack [T, n_embd] — generate_spec's prompt_h).
740    /// FRESH-PROMPT ONLY (cache.pos == 0): the fa_prefill tiles attend within `tokens` alone.
741    /// forward_last itself stays untouched (kernel-check / run-gen gate on it).
742    ///
743    /// `queued_after` (lane/tick-seg, 2026-08-07): the number of prompt tokens of the SAME
744    /// REQUEST that the caller will prime in LATER calls — 0 when this call is the whole
745    /// request (every single-shot caller). Serve splits a long prompt across SEVERAL
746    /// prime_cache calls (one per scheduler tick, plus the prefix-cache LCP split), and the
747    /// request's absolute end position `seq_end = cache.pos + t + queued_after` steers step35's
748    /// SWA prefill arm — computing it per CALL made the arm a function of the tick budget
749    /// (budgets 512/256/64 DIFFER 1.813e0 vs monolithic, greedy diverging at step 6; dark lanes
750    /// default to 256 AND cap by live SLO headroom, so identical judge requests primed
751    /// differently under load — research/tick-seg-20260807, receipt in
752    /// research/step35-chunkfix-20260807 §9). The parameter is what prime_cache structurally
753    /// lacked: it cannot know from `tokens` and `cache` alone whether more of the request is
754    /// coming. A SESSION CONTINUATION (a NEW user turn primed onto a live cache) is a NEW
755    /// request — its arithmetic is keyed to its own extent, so those callers pass 0; only a
756    /// caller that SPLITS one request across calls passes the remainder.
757    pub fn prime_cache(
758        &self,
759        e: &Engine,
760        tokens: &[u32],
761        cache: &mut Cache,
762        queued_after: usize,
763    ) -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
764        self.prime_cache_overlaid(e, tokens, cache, queued_after, None)
765    }
766
767    /// `prime_cache` with a vision embedding overlay (lane/vision): image merger outputs
768    /// replace the `<|image_pad|>` token embeddings at prompt-relative positions before the
769    /// trunk walk — the mixed-embedding prime. Text-only callers use `prime_cache` (overlay
770    /// None, byte-identical path). v1 scope: the serial chunk walk only — PP prime arms and
771    /// gemma4 refuse loudly (the vision serving box is single-GPU).
772    pub fn prime_cache_overlaid(
773        &self,
774        e: &Engine,
775        tokens: &[u32],
776        cache: &mut Cache,
777        queued_after: usize,
778        overlay: Option<&crate::vision::EmbedOverlay>,
779    ) -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
780        let n_embd = self.cfg.n_embd as usize;
781        let t = tokens.len();
782        // SESSION CONTINUATION (2026-07-05): cache.pos > 0 = priming a NEW SUFFIX onto a live
783        // session cache — every chunk (including the first) takes the continuation arm
784        // (fa_prefill_view over the quantized past + this chunk). Fresh prime (pos==0) unchanged.
785        assert!(
786            t >= PRIME_MIN_T,
787            "prime_cache needs T >= {PRIME_MIN_T} (caller gates)"
788        );
789        assert!(
790            cache.pos + t <= cache.max_ctx,
791            "prime_cache: prompt exceeds cache max_ctx"
792        );
793
794        // CHUNKED PRIME (2026-07-05, the long-ctx OOM fix): the monolithic prime allocates
795        // per-layer transients proportional to T (gate/up/act = T*n_ff*4B EACH — 1.5GB apiece at
796        // 16k on the 27B), which OOMs a 24GB card around 16k prompt tokens. Chunk the prompt:
797        // each chunk runs the full layer stack with transients sized to the chunk, appending its
798        // K/V to the resident quantized cache and carrying the GDN conv-ring + recurrent state
799        // through `cache.recur` (linear_attn_prime is already stateful — a chunk boundary is
800        // exactly the state carry it was built for). Full-attn chunks after the first attend to
801        // the QUANTIZED past KV via fa_prefill_view (the spec-verify pattern) — same numeric
802        // class as decode reading the cache. Prompts <= one chunk take the ORIGINAL monolithic
803        // body byte-for-byte (chunk 0 short-circuits to the f32 fa_prefill path).
804        // MEMRA_PRIME_CHUNK sets the chunk size (tokens); 0 disables chunking (monolithic).
805        if self.is_gemma4_e4b() || self.cfg.gemma4.is_some() {
806            if self.is_gemma4_e4b() {
807                if overlay.is_some() {
808                    return Err(
809                        "vision embedding overlay is unsupported on gemma4 E4B (PLE prime)".into(),
810                    );
811                }
812                return self.gemma4_e4b_prime(e, tokens, cache);
813            }
814            // gemma4 v0: monolithic fresh-prompt prime (chunked/continuation arms later).
815            // An overlay takes the masked-prefill arm: image rows splice in unscaled
816            // (gemma4.cpp:182 — embd batches skip the sqrt(n_embd) scale) and the image
817            // spans become bidirectional attention islands (lane/gemma-vision).
818            return self.gemma4_prime(e, tokens, cache, overlay);
819        }
820        let ranges = prime_chunk_ranges(t, self.layers.len());
821        // CHUNK-ORDER INVARIANCE (lane/chunk-invariance, 2026-08-05; vLLM #38561 shape).
822        // MEMRA_PRIME_CHUNK is documented as a memory-transient knob, but it also decides
823        // the prefill's ARITHMETIC, so two rigs with different values produced different
824        // greedy text for the same prompt (research/session-affinity-20260805: 97- and
825        // 149-token prompts). ROOT CAUSE, measured in research/chunk-invariance-20260805
826        // (VERDICT.md) — and it is NOT what docs originally said:
827        //   * NOT trunk GEMM m / reduction order. REFUTED: the prefill GEMM is m-INVARIANT
828        //     (rows [0,32) bit-identical at m=32 vs m=33..80, both quantized wq and the
829        //     output head), so growing a chunk cannot move an existing row's value.
830        //   * NOT the GDN scan segmentation. REFUTED: MEMRA_GDN_CHUNKED=0 (sequential scan,
831        //     no WY segmentation at all) still diverges, so vLLM's mamba-boundary fix does
832        //     not describe our leak.
833        //   * IT IS the attention numeric CLASS edge in full_attn_prime_fa_dispatch, which
834        //     selects on `base_len == 0`: chunk 0 attends over this batch's f32 K/V
835        //     (fa_prefill) while every later chunk attends over the q8_0/q5_1 quantized KV
836        //     cache (fa_prefill_view_ws). The chunk size therefore decides WHERE in the
837        //     prompt that precision edge falls. Signature: per-row maxdiff is exactly 0.0
838        //     before the first boundary and O(1) right after, first_div_pos == chunk size.
839        // The grain-free fix (full_attn_prime_fa_dispatch below) removed that class edge at
840        // the source — every row is in one numeric class, so the chunk size no longer steers
841        // arithmetic and MEMRA_PRIME_CHUNK is a pure memory/transient knob again. The interim
842        // MEMRA_PRIME_INVARIANT/MEMRA_PRIME_GRAIN pin-the-boundary door was superseded by that
843        // fix and KILLED at v0.71 per the flags doctrine (the jsonl + VERDICT.md are the
844        // record); the chunkinv gate asserts byte-identity across chunk sizes naked.
845        // The REQUEST's absolute end position, computed ONCE before the loop: every chunk sees the
846        // same value, whatever the chunk size. step35's SWA arm selects on it so that kernel
847        // selection — and therefore the logits — cannot depend on MEMRA_PRIME_CHUNK
848        // (research/step35-chunkfix-20260807; see step35_attn_pre_wo's doc note).
849        // `+ queued_after` closes the SECOND axis (lane/tick-seg): when serve splits the request
850        // across calls, the request still ends at the same absolute position, whatever the tick
851        // budget or LCP split point. MEMRA_PRIME_CALLLOCAL=1 is the ROLLBACK SEAM to the FULL
852        // pre-fix arithmetic: the per-call value here AND the unaligned FA view offset in
853        // step35_attn_pre_wo. Both halves are required for tickinv35's canary teeth under the FA
854        // default. Read per call, not cached (the probe flips it in-process between arms). Never
855        // on in a measured default run.
856        let legacy_calllocal = std::env::var("MEMRA_PRIME_CALLLOCAL").as_deref() == Ok("1");
857        let seq_end = if legacy_calllocal {
858            cache.pos + t
859        } else {
860            cache.pos + t + queued_after
861        };
862        if ranges.len() == 1 {
863            return self.prime_chunk(e, tokens, cache, seq_end, 0, overlay);
864        }
865        // PIPELINED PP-2 PRIME (lane/cx-pipeline-prime, 2026-08-08): overlap stage 0 of
866        // chunk N+1 with stage 1 of chunk N. The serial split stays reachable through
867        // MEMRA_PRIME_PIPE=0 and is the exactness oracle. N>2 keeps the serial walker;
868        // this lane owns the balanced two-stage schedule only.
869        if crate::pp::prime_pipe_on() && crate::pp::prime_pp_on() && !crate::pp::pp2_streams_off() {
870            if let Some(fence) = crate::pp::pp_cuts(self.layers.len()).filter(|f| f.len() == 3) {
871                if overlay.is_some() {
872                    return Err(
873                        "vision embedding overlay + pipelined PP prime unsupported (v1); \
874                         run the serial prime (single device or MEMRA_PRIME_PIPE=0)"
875                            .into(),
876                    );
877                }
878                if crate::pp::pp_multi_stream_same_device() {
879                    return Err(
880                        "prime chunk pipeline refused with 2 stage streams on one device — \
881                         that concurrent-stream placement remains quarantined by the deferred \
882                         pp flake record. Use one device per stage or MEMRA_PRIME_PIPE=0 for \
883                         the serial split."
884                            .into(),
885                    );
886                }
887                return self.prime_cache_pp2_pipelined(e, tokens, cache, seq_end, &ranges, &fence);
888            }
889        }
890        let mut hiddens = e.uninit(t * n_embd)?;
891        let mut last: Option<(Vec<f32>, CudaSlice<f32>)> = None;
892        for &(start, end) in &ranges {
893            // chunked prime writes tap rows at the chunk's absolute offset
894            if let Some(taps) = cache.dflash_taps.as_mut() {
895                taps.base = start;
896            }
897            let (l, hs, x) =
898                self.prime_chunk(e, &tokens[start..end], cache, seq_end, start, overlay)?;
899            e.copy_into(&mut hiddens, start * n_embd, &x, (end - start) * n_embd)?;
900            last = Some((l, hs));
901        }
902        let (logits, h_seed) = last.unwrap();
903        Ok((logits, h_seed, hiddens))
904    }
905
906    /// PP-2 chunk scheduler: stage 1 of chunk N and stage 0 of chunk N+1 are both queued
907    /// before N's epilogue D2H drains the last-stage stream. Arithmetic is unchanged:
908    /// every chunk still runs the same two `prime_layers` ranges, boundary copy, output
909    /// norm, lm head, and caller hidden-stack copy as the serial split.
910    fn prime_cache_pp2_pipelined(
911        &self,
912        e: &Engine,
913        tokens: &[u32],
914        cache: &mut Cache,
915        seq_end: usize,
916        ranges: &[(usize, usize)],
917        fence: &[usize],
918    ) -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
919        debug_assert_eq!(fence.len(), 3);
920        debug_assert!(ranges.len() >= 2);
921        let rt = crate::pp::PpNRt::get(e)?;
922        assert_eq!(
923            rt.n_stages(),
924            2,
925            "prime pipeline requires exactly two PP stages"
926        );
927        let n_embd = self.cfg.n_embd as usize;
928        let t = tokens.len();
929        let initial_base = cache.pos;
930        let caller_stream = e.stream();
931
932        // #87 reverse publication before any new stage allocation, then prewarm both
933        // boundary slots while the stage streams are otherwise empty. Lazy-growing slot B
934        // after stage 1(N) is queued would synchronize that stream and erase the first
935        // overlap on a two-chunk prompt.
936        rt.fence_stages_behind(&caller_stream)?;
937        let max_payload = ranges.iter().map(|(s, e)| (e - s) * n_embd).max().unwrap();
938        rt.prepare_overlap_slots(0, max_payload)?;
939
940        let mut hiddens = e.uninit(t * n_embd)?;
941        let mut last: Option<(Vec<f32>, CudaSlice<f32>)> = None;
942        let mut stage_caches = PrimeCacheStages::new(cache, fence[1]);
943        let (cache0, cache1) = stage_caches.parts();
944        let (first_start, first_end) = ranges[0];
945        let mut slot = self.prime_pp2_stage0_enqueue(
946            e,
947            rt,
948            &tokens[first_start..first_end],
949            cache0,
950            seq_end,
951            fence,
952            initial_base + first_start,
953            true,
954        )?;
955        cache0.pos = initial_base + first_end;
956
957        for (i, &(start, end)) in ranges.iter().enumerate() {
958            let base = initial_base + start;
959            debug_assert_eq!(
960                cache1.pos, base,
961                "stage 1 must drain chunks in original position order"
962            );
963            let (out, next_slot) = if let Some(&(next_start, next_end)) = ranges.get(i + 1) {
964                let next_base = initial_base + next_start;
965                debug_assert_eq!(
966                    cache0.pos, next_base,
967                    "stage 0 must issue chunks in original position order"
968                );
969                let cache0_stage = &mut *cache0;
970                // Step's MoE router readback synchronizes once per layer. Two CUDA streams
971                // on one host thread therefore serialize even if the calls are ordered as
972                // a pipeline. Drive the disjoint stage caches from two scoped host threads:
973                // stage 1 consumes slot N while stage 0 produces slot N+1.
974                std::thread::scope(|scope| -> Result<_, Box<dyn std::error::Error>> {
975                    let stage0 = scope.spawn(move || -> Result<usize, String> {
976                        let next = self
977                            .prime_pp2_stage0_enqueue(
978                                e,
979                                rt,
980                                &tokens[next_start..next_end],
981                                cache0_stage,
982                                seq_end,
983                                fence,
984                                next_base,
985                                true,
986                            )
987                            .map_err(|err| err.to_string())?;
988                        cache0_stage.pos = initial_base + next_end;
989                        Ok(next)
990                    });
991                    let x = self.prime_pp2_stage1_enqueue(
992                        e,
993                        rt,
994                        slot,
995                        end - start,
996                        cache1,
997                        seq_end,
998                        fence,
999                        base,
1000                        true,
1001                    )?;
1002                    let out = {
1003                        rt.bind_stage(1)?;
1004                        let _st1 = rt.enter(1);
1005                        let e1 = rt.engine(1, e);
1006                        self.prime_chunk_epilogue(e1, x, end - start, cache1)?
1007                    };
1008                    let next = stage0
1009                        .join()
1010                        .map_err(|_| "pipeprime stage-0 host walker panicked")?
1011                        .map_err(|err| -> Box<dyn std::error::Error> { err.into() })?;
1012                    Ok((out, Some(next)))
1013                })?
1014            } else {
1015                let x = self.prime_pp2_stage1_enqueue(
1016                    e,
1017                    rt,
1018                    slot,
1019                    end - start,
1020                    cache1,
1021                    seq_end,
1022                    fence,
1023                    base,
1024                    true,
1025                )?;
1026                let out = {
1027                    rt.bind_stage(1)?;
1028                    let _st1 = rt.enter(1);
1029                    let e1 = rt.engine(1, e);
1030                    self.prime_chunk_epilogue(e1, x, end - start, cache1)?
1031                };
1032                (out, None)
1033            };
1034
1035            rt.publish_to(1, &caller_stream)?;
1036            e.copy_into(&mut hiddens, start * n_embd, &out.2, (end - start) * n_embd)?;
1037            last = Some((out.0, out.1));
1038            crate::pp::PRIME_SPLIT_CHUNKS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1039
1040            if let Some(next) = next_slot {
1041                // The caller copy above reads a stage-1 allocation. Before stage 1 of the
1042                // next chunk can allocate/reuse blocks, mirror #87's body-entry fence.
1043                // Stage 0(N+1) is already queued before this wait is appended, so its
1044                // overlap with stage 1(N) is preserved.
1045                rt.fence_stages_behind(&caller_stream)?;
1046                slot = next;
1047            }
1048        }
1049
1050        debug_assert_eq!(cache0.pos, initial_base + t);
1051        debug_assert_eq!(cache1.pos, initial_base + t);
1052        let (logits, h_seed) = last.unwrap();
1053        Ok((logits, h_seed, hiddens))
1054    }
1055
1056    /// One prime chunk: the full layer stack over `tokens`, continuing from the cache's current
1057    /// state (`cache.pos` = tokens already primed; 0 = fresh). Positions/RoPE are absolute
1058    /// task #21 de-broadcast: the q/k head count PREP must emit so the scan's consumers
1059    /// agree. Compact (num_k) ONLY when the scan will take the chunked+mma route AND
1060    /// num_k == num_v/2 (the engine-side hint mirrors this exact formula); everything
1061    /// else (s128 verify tier, chunked-off, mma-off) keeps the broadcast layout.
1062    fn gdn_hk(e: &Engine, t: usize, num_v: usize, num_k: usize) -> usize {
1063        if Engine::gdn_db_on()
1064            && Engine::gdn_chunked_enabled()
1065            && t >= 16
1066            && e.gdn_mma_enabled(Engine::gdn_chunk_size())
1067            && num_k * 2 == num_v
1068        {
1069            num_k
1070        } else {
1071            num_v
1072        }
1073    }
1074
1075    /// task #17: gate for the fused fp16-operand epilogues (silu_mul/gated_rmsnorm/sig_mul
1076    /// `_f16out` twins). Bit-identical class (the twins emit the cvt kernel's exact halves),
1077    /// but seam-gated (MEMRA_F16OUT=0) so a bit-check can arbitrate, and OFF under verify-exact
1078    /// (matmul_group skips the f16 lane there; the twins must not resurrect it).
1079    fn f16out_on(e: &Engine, t: usize) -> bool {
1080        crate::f16_ffi::pp_f16_enabled()
1081            && t >= 16
1082            && !e.verify_exact_on()
1083            && std::env::var("MEMRA_F16OUT").as_deref() != Ok("0")
1084    }
1085
1086    /// (cache.pos + i). Returns (last-row logits, h_seed, this chunk's hidden stack [T, n_embd]).
1087    /// See HybridModel::prime_slabs — the eager prime's resident trunk transients.
1088    /// PER-DEVICE since lane/pp-leverb (2026-08-08): the map is keyed by the allocating
1089    /// engine's CUDA ordinal — under the prime stage split each stage's range walks through
1090    /// its OWN slabs on its own device (a dev0 slab dereferenced by a dev1 kernel would be
1091    /// a peer read per GEMM operand, the exact class Lever B removes). Single-device rigs
1092    /// see one entry, byte-identical behavior.
1093    pub fn prime_slabs_get(
1094        &self,
1095        e: &Engine,
1096        t: usize,
1097        n_embd: usize,
1098        n_ff_max: usize,
1099    ) -> Result<std::sync::Arc<std::sync::Mutex<PrimeSlabs>>, Box<dyn std::error::Error>> {
1100        let mut slabs = self.prime_slabs.lock().unwrap();
1101        let dev = e.ctx().ordinal();
1102        let need_new = match slabs.get(&dev) {
1103            None => true,
1104            Some(sl) => sl.lock().unwrap().t_cap < t,
1105        };
1106        if need_new {
1107            slabs.insert(
1108                dev,
1109                std::sync::Arc::new(std::sync::Mutex::new(PrimeSlabs {
1110                    t_cap: t,
1111                    h: e.uninit(t * n_embd)?,
1112                    x1: e.uninit(t * n_embd)?,
1113                    z: e.uninit(t * n_embd)?,
1114                    act: e.uninit(t * n_ff_max)?,
1115                    xa: e.uninit(t * n_embd)?,
1116                    xb: e.uninit(t * n_embd)?,
1117                    h16: e.alloc_u8_uninit(t * n_embd * 2)?,
1118                    z16: e.alloc_u8_uninit(t * n_embd * 2)?,
1119                    gate: e.uninit(t * n_ff_max)?,
1120                    up: e.uninit(t * n_ff_max)?,
1121                    ffn_out: e.uninit(t * n_embd)?,
1122                    seg_glue: Vec::new(),
1123                    mixed: e.uninit(t * n_embd)?,
1124                    seg_mid: Vec::new(),
1125                    seg_t: 0,
1126                })),
1127            );
1128        }
1129        Ok(slabs.get(&dev).expect("prime slab inserted").clone())
1130    }
1131
1132    /// `seq_end` = the whole REQUEST's absolute end position (`cache.pos + prompt_len` at
1133    /// `prime_cache` entry), NOT this chunk's end. Chunk-size-invariant by construction; step35's
1134    /// SWA arm selects on it (see `step35_attn_pre_wo`). Every other arch ignores it.
1135    fn prime_chunk(
1136        &self,
1137        e: &Engine,
1138        tokens: &[u32],
1139        cache: &mut Cache,
1140        seq_end: usize,
1141        chunk_off: usize,
1142        overlay: Option<&crate::vision::EmbedOverlay>,
1143    ) -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
1144        if crate::pp::pp_host_bounce_active()
1145            && (self.cfg.gemma4.is_some() || !crate::pp::prime_pp_on())
1146        {
1147            return Err(
1148                "prime_chunk: refused with MEMRA_PP_HOST_BOUNCE=1 because this configuration \
1149                 has no active prime stage split and would peer-read remote weights; keep \
1150                 MEMRA_PRIME_PP enabled and use a PP-prime-supported model"
1151                    .into(),
1152            );
1153        }
1154        // LEVER B (lane/pp-leverb, 2026-08-08): the ppN door for the chunked prime. With the
1155        // door open + per-stage streams, each chunk walks its layer ranges on the OWNING
1156        // stage's engine/device (the anatomy receipt this kills: dev1 ran ZERO prefill
1157        // kernels; stage-1 trunk weights were peer-read = 22% of the pp4096 wall).
1158        // MEMRA_PRIME_PP=0 = the unsplit rollback (also the prime-split-gate's reference
1159        // arm — prime deliberately keeps NO refuse_unsplit_if_remote, see pp.rs). The
1160        // MEMRA_PP_STREAMS=0 seam keeps the unsplit walk too: in that regime the sharded
1161        // loader is off and there is nothing remote to split for.
1162        if self.cfg.gemma4.is_none() && !crate::pp::pp2_streams_off() && crate::pp::prime_pp_on() {
1163            if let Some(fence) = crate::pp::pp_cuts(self.layers.len()) {
1164                if overlay.is_some() {
1165                    return Err("vision embedding overlay + PP prime unsupported (v1); \
1166                         run single-device or MEMRA_PRIME_PP=0"
1167                        .into());
1168                }
1169                return self.prime_chunk_ppn(e, tokens, cache, seq_end, &fence);
1170            }
1171        }
1172        if crate::pp::pp_host_bounce_active() {
1173            return Err(
1174                "prime_chunk: MEMRA_PP_HOST_BOUNCE=1 found no valid prime stage split; \
1175                 refusing an unsplit remote-weight walk"
1176                    .into(),
1177            );
1178        }
1179        let t = tokens.len();
1180        let base = cache.pos;
1181        debug_assert!(
1182            seq_end >= base + t,
1183            "prime_chunk: seq_end must cover this chunk"
1184        );
1185        let pos: Vec<i32> = (base as i32..(base + t) as i32).collect();
1186        let pos_d = e.htod_i32(&pos)?;
1187
1188        let mut x_embed = self.embed(e, tokens)?; // [T, n_embd]
1189        if let Some(ov) = overlay {
1190            // Mixed-embedding splice: image rows overwrite the pad-token embeddings that
1191            // fall inside this chunk's prompt-relative window [chunk_off, chunk_off+t).
1192            // Images larger than one prime chunk straddle boundaries, hence the clipping.
1193            let n_embd = self.cfg.n_embd as usize;
1194            for &(pos, row_off, n_rows) in &ov.spans {
1195                let lo = pos.max(chunk_off);
1196                let hi = (pos + n_rows).min(chunk_off + t);
1197                if lo < hi {
1198                    let src_row = row_off + (lo - pos);
1199                    let view = ov
1200                        .rows
1201                        .slice(src_row * n_embd..(src_row + (hi - lo)) * n_embd);
1202                    e.copy_view_into(
1203                        &mut x_embed,
1204                        (lo - chunk_off) * n_embd,
1205                        &view,
1206                        (hi - lo) * n_embd,
1207                    )?;
1208                }
1209            }
1210        }
1211        let x = self.prime_layers(
1212            e,
1213            x_embed,
1214            0,
1215            self.layers.len(),
1216            &pos_d,
1217            t,
1218            base,
1219            cache,
1220            seq_end,
1221        )?;
1222        self.prime_chunk_epilogue(e, x, t, cache)
1223    }
1224
1225    /// PRIME RANGE SUBGRAPH (lane/pp-leverb, 2026-08-08): layers `[lo, hi)` of the chunked
1226    /// prime walk — `prime_chunk`'s trunk loop extracted verbatim to the
1227    /// `decode_layers_eager(lo, hi)` / `verify_layers(lo, hi)` contract: enters with a
1228    /// MATERIALIZED `[T, n_embd]` residual, exits with the range's final residual
1229    /// materialized (cloned out of the slab). At `lo=0, hi=n_layers` — the unsplit call —
1230    /// the launch sequence is byte-identical to the pre-extraction body. Range semantics:
1231    ///   - the cross-layer [down-add + NEXT attn-norm] fusion is range-LOCAL (`il + 1 < hi`):
1232    ///     layer `hi`'s attn_norm belongs to the NEXT stage's device, so the range ends with
1233    ///     the plain add (materialize) and the next stage hoists its own first norm — the
1234    ///     kernel-check-pinned `add_rms_norm == add then rms_norm` identity, the same law
1235    ///     the decode split rests on (`prime-split-gate` arbitrates end-to-end);
1236    ///   - prime slabs are PER-DEVICE (`prime_slabs_get` keys on the engine ordinal), so
1237    ///     each stage walks through its own resident transients;
1238    ///   - the S-glue/S-mid capture path requires the FULL range (its lookahead fuses
1239    ///     `self.layers[il+1]` unconditionally) — `use_seg` gains `lo == 0 && hi == n_layers`.
1240    #[allow(clippy::too_many_arguments)]
1241    fn prime_layers(
1242        &self,
1243        e: &Engine,
1244        x_in: CudaSlice<f32>,
1245        lo: usize,
1246        hi: usize,
1247        pos_d: &CudaSlice<i32>,
1248        t: usize,
1249        base: usize,
1250        cache: &mut Cache,
1251        seq_end: usize,
1252    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1253        let cfg = &self.cfg;
1254        let n_embd = cfg.n_embd as usize;
1255        let eps = cfg.rms_eps;
1256        // task #14: fuse the fp16 GEMM-operand emission into the trunk norms (kills the
1257        // standalone convert launches). Only when the f16 lane serves and T reaches the
1258        // GEMM tier; bit-identical either way.
1259        let f16fuse = crate::f16_ffi::pp_f16_enabled() && t >= 16;
1260        // PRIME SLABS (piecewise foundation): trunk transients in resident buffers —
1261        // ~224 fewer alloc/free calls per prime and FROZEN Lt operand addresses
1262        // (nvjet's alignment-variant selection becomes run-to-run stable). Every slab is
1263        // fully overwritten before use; x ping-pongs xa<->xb; the hidden-stack return
1264        // clones the final x (the slab cannot leave). Non-slab fallback: MEMRA_PRIME_SLABS=0.
1265        let n_ff_max = self
1266            .layers
1267            .iter()
1268            .map(|l| match &l.ffn {
1269                crate::hybrid::Ffn::Dense { ffn_gate, .. } => ffn_gate.out_features(),
1270                _ => n_embd,
1271            })
1272            .max()
1273            .unwrap_or(n_embd)
1274            .max(n_embd);
1275        let use_slabs = std::env::var("MEMRA_PRIME_SLABS").as_deref() != Ok("0");
1276        let slab = if use_slabs {
1277            Some(self.prime_slabs_get(e, t, n_embd, n_ff_max)?)
1278        } else {
1279            None
1280        };
1281        let mut slab_guard = slab.as_ref().map(|sl| sl.lock().unwrap());
1282        let mut x_own; // fallback storage when slabs are off
1283        type SlabRefs<'a> = (
1284            &'a mut CudaSlice<f32>,
1285            &'a mut CudaSlice<f32>,
1286            &'a mut CudaSlice<f32>,
1287            &'a mut CudaSlice<f32>,
1288            &'a mut CudaSlice<u8>,
1289            &'a mut CudaSlice<u8>,
1290            &'a mut CudaSlice<f32>,
1291            &'a mut CudaSlice<f32>,
1292            &'a mut CudaSlice<f32>,
1293        );
1294        let (mut x_cur, mut x_nxt, sl): (
1295            &mut CudaSlice<f32>,
1296            &mut CudaSlice<f32>,
1297            Option<SlabRefs>,
1298        );
1299        let mut seg: Option<(
1300            &mut Vec<Option<cudarc::driver::CudaGraph>>,
1301            &mut Vec<Option<cudarc::driver::CudaGraph>>,
1302            &mut CudaSlice<f32>,
1303            &mut usize,
1304        )> = None;
1305        let mut x_own2;
1306        match slab_guard.as_mut() {
1307            Some(g) => {
1308                let slabs = &mut **g;
1309                e.copy_into(&mut slabs.xa, 0, &x_in, t * n_embd)?;
1310                let PrimeSlabs {
1311                    xa,
1312                    xb,
1313                    h,
1314                    x1,
1315                    z,
1316                    act,
1317                    h16,
1318                    z16,
1319                    gate,
1320                    up,
1321                    ffn_out,
1322                    seg_glue,
1323                    mixed,
1324                    seg_mid,
1325                    seg_t,
1326                    ..
1327                } = slabs;
1328                x_cur = xa;
1329                x_nxt = xb;
1330                seg = Some((seg_glue, seg_mid, mixed, seg_t));
1331                sl = Some((h, x1, z, act, h16, z16, gate, up, ffn_out));
1332            }
1333            None => {
1334                x_own = x_in;
1335                x_own2 = e.uninit(t * n_embd)?;
1336                x_cur = &mut x_own;
1337                x_nxt = &mut x_own2;
1338                sl = None;
1339            }
1340        }
1341        let mut alloc_h;
1342        let mut alloc_x1;
1343        let mut alloc_z;
1344        let mut alloc_act;
1345        let mut alloc_h16;
1346        let mut alloc_z16;
1347        let mut alloc_gate;
1348        let mut alloc_up;
1349        let mut alloc_fo;
1350        let (h, x1, z, act): (
1351            &mut CudaSlice<f32>,
1352            &mut CudaSlice<f32>,
1353            &mut CudaSlice<f32>,
1354            &mut CudaSlice<f32>,
1355        );
1356        let (h16, z16): (&mut CudaSlice<u8>, &mut CudaSlice<u8>);
1357        let (sl_gate, sl_up, sl_fo): (
1358            &mut CudaSlice<f32>,
1359            &mut CudaSlice<f32>,
1360            &mut CudaSlice<f32>,
1361        );
1362        match sl {
1363            Some((a, b, c, d, e16, f16b, g, u, fo)) => {
1364                h = a;
1365                x1 = b;
1366                z = c;
1367                act = d;
1368                h16 = e16;
1369                z16 = f16b;
1370                sl_gate = g;
1371                sl_up = u;
1372                sl_fo = fo;
1373            }
1374            None => {
1375                alloc_h = e.uninit(t * n_embd)?;
1376                alloc_x1 = e.uninit(t * n_embd)?;
1377                alloc_z = e.uninit(t * n_embd)?;
1378                alloc_act = e.uninit(t * n_ff_max)?;
1379                alloc_h16 = e.alloc_u8_uninit(t * n_embd * 2)?;
1380                alloc_z16 = e.alloc_u8_uninit(t * n_embd * 2)?;
1381                alloc_gate = e.uninit(t * n_ff_max)?;
1382                alloc_up = e.uninit(t * n_ff_max)?;
1383                alloc_fo = e.uninit(t * n_embd)?;
1384                h = &mut alloc_h;
1385                x1 = &mut alloc_x1;
1386                z = &mut alloc_z;
1387                act = &mut alloc_act;
1388                h16 = &mut alloc_h16;
1389                z16 = &mut alloc_z16;
1390                sl_gate = &mut alloc_gate;
1391                sl_up = &mut alloc_up;
1392                sl_fo = &mut alloc_fo;
1393            }
1394        }
1395        // piecewise increment 3: layer-0 norm hoisted; the per-layer tail fuses
1396        // [down-add + NEXT layer's attn-norm] into one captured S-glue segment
1397        // (all-slab IO, zero in-graph allocations). Capture happens lazily on the
1398        // first prime at this t (capture does not execute -> launch right after).
1399        let n_layers = self.layers.len();
1400        // OPT-IN (2026-07-26 interleaved verdict): 2-kernel segments measured NET
1401        // -0.7%-to-neutral under the interleaved A/B protocol — one cuGraphLaunch costs
1402        // about what two kernel submissions do. The earlier "+0.9%" was cross-run clock
1403        // drift (the repo's interleaved-A/B law exists for exactly this). Larger segments
1404        // (S-prep/S-attn, 7-9 kernels) remain the open hypothesis; the core-split
1405        // machinery stays (byte-identical) as their foundation.
1406        // step35 is excluded: the core-split path calls `full_attn_prime_core_inner`, which is
1407        // the GENERIC attn core (uniform n_head, rope_dim_count, no window, no head-wise gate).
1408        // step35 rides its own mixer through the normal per-layer arm below.
1409        let use_seg = f16fuse
1410            && seg.is_some()
1411            && self.cfg.step35.is_none()
1412            && lo == 0
1413            && hi == n_layers
1414            && std::env::var("MEMRA_PRIME_SEG").as_deref() == Ok("1");
1415        if let Some((sg, sm, _, st)) = seg.as_mut() {
1416            if **st != t {
1417                sg.clear();
1418                sg.extend((0..n_layers).map(|_| None));
1419                sm.clear();
1420                sm.extend((0..n_layers).map(|_| None));
1421                **st = t;
1422            }
1423        }
1424        {
1425            let layer_lo = &self.layers[lo];
1426            if f16fuse {
1427                e.rms_norm_f16out(
1428                    x_cur,
1429                    layer_lo.attn_norm.float_data(),
1430                    h,
1431                    h16,
1432                    n_embd,
1433                    t,
1434                    eps,
1435                )?;
1436            } else {
1437                e.rms_norm(x_cur, layer_lo.attn_norm.float_data(), h, n_embd, t, eps)?;
1438            }
1439        }
1440        for il in lo..hi {
1441            let layer = &self.layers[il];
1442            let hx16 = if f16fuse { Some(&*h16) } else { None };
1443            if use_seg {
1444                // core-split path: projections -> _inner core -> out-GEMM INTO the mixed
1445                // slab (no copies) -> S-mid segment [add + post-norm] as one graph launch.
1446                let (pre, pre16, w_out) = match &layer.mixer {
1447                    Mixer::Full(fa) => {
1448                        let g3 = match hx16 {
1449                            Some(xh) => e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv], h, xh, t)?,
1450                            None => e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?,
1451                        };
1452                        let (pre, pre16) =
1453                            self.full_attn_prime_core_inner(e, fa, g3, &pos_d, t, cache, il)?;
1454                        (pre, pre16, &fa.wo)
1455                    }
1456                    Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
1457                    Mixer::Linear(la) => {
1458                        let ws = [&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha];
1459                        let g4 = match hx16 {
1460                            Some(xh) => e.matmul_group_xh(&ws, h, xh, t)?,
1461                            None => e.matmul_group(&ws, h, t)?,
1462                        };
1463                        let (pre, pre16) =
1464                            self.linear_attn_prime_core_pad_inner(e, la, g4, t, cache, il, None)?;
1465                        (pre, pre16, &la.ssm_out)
1466                    }
1467                };
1468                {
1469                    let (_, sm, mslab, _) = seg.as_mut().unwrap();
1470                    let pre_n = pre.len() / t;
1471                    let xh_pre = match pre16 {
1472                        Some(x) => x,
1473                        None => e.f16_act(&pre, t * pre_n, pre_n)?,
1474                    };
1475                    if !e.try_f16_gemm_pre_into(w_out, &xh_pre, t, mslab)? {
1476                        let y = e.matmul(w_out, &pre, t)?;
1477                        e.copy_into(mslab, 0, &y, t * n_embd)?;
1478                    }
1479                    if sm[il].is_none() {
1480                        use cudarc::driver::sys::{CUgraphInstantiate_flags, CUstreamCaptureMode};
1481                        let w_post = layer.post_attn_norm.float_data();
1482                        e.stream().synchronize()?;
1483                        e.stream()
1484                            .begin_capture(CUstreamCaptureMode::CU_STREAM_CAPTURE_MODE_RELAXED)?;
1485                        let r = (|| -> Result<(), Box<dyn std::error::Error>> {
1486                            e.add(x_cur, mslab, x1, t * n_embd)?;
1487                            e.rms_norm_f16out(x1, w_post, z, z16, n_embd, t, eps)?;
1488                            Ok(())
1489                        })();
1490                        let g = e.stream().end_capture(
1491                            CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH);
1492                        r?;
1493                        sm[il] = Some(g?.ok_or("S-mid capture produced no graph")?);
1494                    }
1495                    sm[il].as_ref().unwrap().launch()?;
1496                }
1497            } else {
1498                let mixed = match &layer.mixer {
1499                    Mixer::Full(fa) => {
1500                        self.full_attn_prime(e, fa, h, hx16, &pos_d, t, cache, il, seq_end)?
1501                    }
1502                    Mixer::Linear(la) => self.linear_attn_prime(e, la, h, hx16, t, cache, il)?,
1503                    Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
1504                };
1505                if f16fuse {
1506                    // round 28: residual+norm in ONE kernel (add_rms_norm precedent,
1507                    // bit-identical) — the standalone add pass disappears.
1508                    e.add_rms_norm_f16out(
1509                        x_cur,
1510                        &mixed,
1511                        layer.post_attn_norm.float_data(),
1512                        x1,
1513                        z,
1514                        z16,
1515                        n_embd,
1516                        t,
1517                        eps,
1518                    )?;
1519                } else {
1520                    e.add(x_cur, &mixed, x1, t * n_embd)?;
1521                    e.rms_norm(x1, layer.post_attn_norm.float_data(), z, n_embd, t, eps)?;
1522                }
1523            }
1524            let zx16 = if f16fuse { Some(&*z16) } else { None };
1525            match &layer.ffn {
1526                crate::hybrid::Ffn::Dense {
1527                    ffn_gate,
1528                    ffn_up,
1529                    ffn_down,
1530                } => {
1531                    let n_ff = ffn_gate.out_features();
1532                    // gate/up INTO boundary slabs (piecewise increment 2); fall back to
1533                    // the allocating group + copy when a mirror is missing.
1534                    let mut into_ok = false;
1535                    if let Some(xh) = zx16 {
1536                        into_ok = e.try_f16_gemm_pre_into(ffn_gate, xh, t, sl_gate)?
1537                            && e.try_f16_gemm_pre_into(ffn_up, xh, t, sl_up)?;
1538                    }
1539                    if !into_ok {
1540                        let mut g2 = match zx16 {
1541                            Some(xh) => e.matmul_group_xh(&[ffn_gate, ffn_up], z, xh, t)?,
1542                            None => e.matmul_group(&[ffn_gate, ffn_up], z, t)?,
1543                        };
1544                        let up_y = g2.pop().unwrap();
1545                        let gate_y = g2.pop().unwrap();
1546                        e.copy_into(sl_gate, 0, &gate_y, t * n_ff)?;
1547                        e.copy_into(sl_up, 0, &up_y, t * n_ff)?;
1548                    }
1549                    // task #17: the silu arm's f16out twin emits the down GEMM's fp16
1550                    // operand in-epilogue; non-silu activations keep the standalone convert.
1551                    // silu_mul_f16out is PLAIN silu(gate)*up — a clamped layer (step35 dense
1552                    // blocks under a live swiglu_clamp_shexp) must take the ffn_act_lim arm.
1553                    let d_lim = self.cfg.clamp_shexp_at(il as u32);
1554                    let act16 = if Self::f16out_on(e, t) && self.cfg.m3.is_none() && d_lim.is_none()
1555                    {
1556                        let mut a16 = e.alloc_u8_uninit(t * n_ff * 2)?;
1557                        e.silu_mul_f16out(sl_gate, sl_up, act, &mut a16, t * n_ff)?;
1558                        Some(a16)
1559                    } else {
1560                        Self::ffn_act_lim(
1561                            e,
1562                            &self.cfg,
1563                            sl_gate,
1564                            sl_up,
1565                            1.0,
1566                            1.0,
1567                            d_lim,
1568                            act,
1569                            t * n_ff,
1570                        )?;
1571                        None
1572                    };
1573                    // down GEMM into the ffn_out slab (f16 arm; fallback copies)
1574                    let xh_act = match act16 {
1575                        Some(x) => x,
1576                        None => e.f16_act(act, t * n_ff, n_ff)?,
1577                    };
1578                    if !e.try_f16_gemm_pre_into(ffn_down, &xh_act, t, sl_fo)? {
1579                        let y = e.matmul(ffn_down, &*act, t)?;
1580                        e.copy_into(sl_fo, 0, &y, t * n_embd)?;
1581                    }
1582                }
1583                crate::hybrid::Ffn::Moe(m) => {
1584                    let y = self.moe_ffn_il_prefill(e, m, z, t, il as u16)?;
1585                    e.copy_into(sl_fo, 0, &y, t * n_embd)?;
1586                }
1587            }
1588            if use_seg && il + 1 < hi {
1589                // S-glue segment: [add + next attn-norm(+f16out)] — one cuGraphLaunch
1590                let w_next = self.layers[il + 1].attn_norm.float_data();
1591                let (sg, _, _, _) = seg.as_mut().unwrap();
1592                if sg[il].is_none() {
1593                    use cudarc::driver::sys::{CUgraphInstantiate_flags, CUstreamCaptureMode};
1594                    e.stream().synchronize()?;
1595                    e.stream()
1596                        .begin_capture(CUstreamCaptureMode::CU_STREAM_CAPTURE_MODE_RELAXED)?;
1597                    let r = (|| -> Result<(), Box<dyn std::error::Error>> {
1598                        e.add(x1, sl_fo, x_nxt, t * n_embd)?;
1599                        e.rms_norm_f16out(x_nxt, w_next, h, h16, n_embd, t, eps)?;
1600                        Ok(())
1601                    })();
1602                    let g = e.stream().end_capture(
1603                        CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH,
1604                    );
1605                    r?;
1606                    sg[il] = Some(g?.ok_or("S-glue capture produced no graph")?);
1607                }
1608                sg[il].as_ref().unwrap().launch()?;
1609            } else {
1610                if il + 1 < hi {
1611                    let w_next = self.layers[il + 1].attn_norm.float_data();
1612                    if f16fuse {
1613                        e.add_rms_norm_f16out(x1, sl_fo, w_next, x_nxt, h, h16, n_embd, t, eps)?;
1614                    } else {
1615                        e.add(x1, sl_fo, x_nxt, t * n_embd)?;
1616                        e.rms_norm(x_nxt, w_next, h, n_embd, t, eps)?;
1617                    }
1618                } else {
1619                    e.add(x1, sl_fo, x_nxt, t * n_embd)?;
1620                }
1621            }
1622            // CHUNK-INVARIANCE BISECT SEAM (lane/chunk-invariance, 2026-08-05): dump the
1623            // per-layer post-residual hidden for the LAST row of this chunk, keyed by
1624            // absolute position, so two runs at different MEMRA_PRIME_CHUNK can be diffed
1625            // layer-by-layer to find the FIRST diverging layer. Diagnostic only —
1626            // unset (the default) costs one OnceLock read per layer.
1627            if let Some(path) = Self::prime_trace_path() {
1628                let row = (base + t - 1) as usize;
1629                let host = e.dtoh(x_nxt)?;
1630                let last = &host[(t - 1) * n_embd..t * n_embd];
1631                use std::io::Write as _;
1632                let mut f = std::fs::OpenOptions::new()
1633                    .create(true)
1634                    .append(true)
1635                    .open(path)?;
1636                let mut h64: u64 = 0xcbf29ce484222325;
1637                for v in last {
1638                    h64 ^= v.to_bits() as u64;
1639                    h64 = h64.wrapping_mul(0x100000001b3);
1640                }
1641                writeln!(
1642                    f,
1643                    "{{\"pos\":{row},\"layer\":{il},\"t\":{t},\"base\":{base},\
1644                             \"hash\":\"{h64:016x}\",\"v0\":{:.9e},\"v1\":{:.9e},\"v2\":{:.9e}}}",
1645                    last[0], last[1], last[2]
1646                )?;
1647            }
1648            // dflash/dspark tap (no-op when no sink armed): post-layer residual rows for
1649            // drafter conditioning — the qwen twin of the gemma4 tap sites.
1650            self.dflash_tap(e, cache, il, x_nxt, t)?;
1651            std::mem::swap(&mut x_cur, &mut x_nxt);
1652        }
1653        // hidden-stack return: clone the final x out of the slab
1654        let mut x = e.uninit(t * n_embd)?;
1655        e.copy_into(&mut x, 0, x_cur, t * n_embd)?;
1656        drop(slab_guard);
1657        Ok(x)
1658    }
1659
1660    /// The prime chunk's tail — h_seed + output_norm + one-row lm head + cache.pos advance —
1661    /// shared verbatim by the unsplit walk and the last stage of the ppN walk (`e` = the
1662    /// engine that produced `x`, i.e. the last stage's under the split; output_norm/output
1663    /// were loaded through that engine by the sharded loader, hybrid.rs `e_head`).
1664    fn prime_chunk_epilogue(
1665        &self,
1666        e: &Engine,
1667        x: CudaSlice<f32>,
1668        t: usize,
1669        cache: &mut Cache,
1670    ) -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
1671        let n_embd = self.cfg.n_embd as usize;
1672        let eps = self.cfg.rms_eps;
1673        // h_seed = LAST row of x BEFORE output_norm (MTP-PLAN §A default) or AFTER it
1674        // (MEMRA_SPEC_HPOST — reference convention; hn is computed just below either way, so
1675        // the post-norm copy happens after hn exists).
1676        let mut h_seed = e.uninit(n_embd)?;
1677        if !crate::spec::spec_hpost() {
1678            e.copy_view_into(
1679                &mut h_seed,
1680                0,
1681                &x.slice((t - 1) * n_embd..t * n_embd),
1682                n_embd,
1683            )?;
1684        }
1685        // last-row logits, exactly like forward_last (norm all T — per-row op — then lm_head on 1 row).
1686        let mut hn = e.uninit(t * n_embd)?;
1687        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
1688        if crate::spec::spec_hpost() {
1689            e.copy_view_into(
1690                &mut h_seed,
1691                0,
1692                &hn.slice((t - 1) * n_embd..t * n_embd),
1693                n_embd,
1694            )?;
1695        }
1696        let last = e.view(&hn, t * n_embd);
1697        let last_row = last.slice((t - 1) * n_embd..t * n_embd);
1698        let mut hlast = e.uninit(n_embd)?;
1699        e.copy_view_into(&mut hlast, 0, &last_row, n_embd)?;
1700        let logits = e.matmul(&self.output, &hlast, 1)?;
1701        cache.pos += t;
1702        // Hidden stack handed to generate_spec as prompt_h: pre-norm x (default) or the full
1703        // post-norm stack hn (MEMRA_SPEC_HPOST).
1704        Ok((
1705            e.dtoh(&logits)?,
1706            h_seed,
1707            if crate::spec::spec_hpost() { hn } else { x },
1708        ))
1709    }
1710
1711    /// THE PRIME STAGE SPLIT (lane/pp-leverb, 2026-08-08): one prime chunk as N stage
1712    /// subgraphs, each range on ITS OWN engine/stream/device, with the `[T, n_embd]`
1713    /// boundary handoff at every fence cut — the prime twin of `decode_step_h_ppn` /
1714    /// `decode_step_t_core_ppn`, and the kill for the anatomy's two receipts: stage-1 trunk
1715    /// weights stop being peer-read (22% of the pp4096 wall) and dev1 stops running zero
1716    /// prefill kernels. Structure mirrors the verify split exactly:
1717    ///   stage 0        `rt.enter(0)` → per-stage pos_d + embed (the table lives with
1718    ///                  stage 0) → `prime_layers(fence[0], fence[1])` → `rt.tx`
1719    ///   middle stages  `rt.rx` → per-stage pos_d → range → `rt.tx`
1720    ///   last stage     `rt.rx` → range → the shared epilogue (output_norm + head live
1721    ///                  there via the sharded loader) → `publish_to`
1722    /// Laws inherited (not relearned): `fence_stages_behind` at entry (#87 — a previous
1723    /// round's stage-freed buffers must not be reused under the caller's queued reads);
1724    /// per-stage `pos_d` (allocated/consumed/freed on one stream); per-stage Engines from
1725    /// `PpNRt` (shared-scratch race); EXIT PUBLICATION for the device-resident returns
1726    /// (h_seed + hidden stack live on the last stage — the caller's stream must wait).
1727    /// KV/MoE locality falls out: each range's KV appends run on the owning stage
1728    /// (`pp::new_cache` placed the buffers there), each stage engine owns its own SLRU
1729    /// pool (per-Engine `moe_cache`, sized on ITS device — the SGLang #33666 law), and the
1730    /// slab-local MoE arm's `DevExps.dev` gate now matches on stage-1 layers too.
1731    /// EXACTNESS: the split adds zero deviation by construction (same kernels, same bytes,
1732    /// boundary = straight f32 copy); `prime-split-gate` (ppsplit) arbitrates bit-for-bit
1733    /// and its liveness counter is bumped here — the gate goes green with this function.
1734    fn prime_chunk_ppn(
1735        &self,
1736        e: &Engine,
1737        tokens: &[u32],
1738        cache: &mut Cache,
1739        seq_end: usize,
1740        fence: &[usize],
1741    ) -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
1742        let rt = crate::pp::PpNRt::get(e)?;
1743        let n_st = fence.len() - 1;
1744        assert_eq!(
1745            rt.n_stages(),
1746            n_st,
1747            "PpNRt stage count {} != fence stages {n_st}",
1748            rt.n_stages()
1749        );
1750        let n_embd = self.cfg.n_embd as usize;
1751        let t = tokens.len();
1752        let base = cache.pos;
1753        debug_assert!(
1754            seq_end >= base + t,
1755            "prime_chunk_ppn: seq_end must cover this chunk"
1756        );
1757        let payload = t * n_embd;
1758        // The caller's ambient stream, captured BEFORE any enter() pushes a stage stream
1759        // (inside a stage scope e.stream() IS the stage stream and the exit wait would
1760        // self-order into a no-op) — the decode_step_t_core_ppn pattern.
1761        let caller_stream = e.stream();
1762        rt.fence_stages_behind(&caller_stream)?;
1763
1764        if n_st == 2 {
1765            let slot =
1766                self.prime_pp2_stage0_enqueue(e, rt, tokens, cache, seq_end, fence, base, false)?;
1767            let x =
1768                self.prime_pp2_stage1_enqueue(e, rt, slot, t, cache, seq_end, fence, base, false)?;
1769            let out = {
1770                rt.bind_stage(1)?;
1771                let _st1 = rt.enter(1);
1772                let e1 = rt.engine(1, e);
1773                self.prime_chunk_epilogue(e1, x, t, cache)?
1774            };
1775            rt.publish_to(1, &caller_stream)?;
1776            crate::pp::PRIME_SPLIT_CHUNKS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1777            return Ok(out);
1778        }
1779
1780        let pos: Vec<i32> = (base as i32..(base + t) as i32).collect();
1781
1782        // ---- STAGE 0: embed + layers [0, fence[1]) + boundary-0 TX ----
1783        let mut slot = {
1784            let _st0 = rt.enter(0);
1785            let e0 = rt.engine(0, e);
1786            let pos_d = e0.htod_i32(&pos)?;
1787            let x = self.embed(e0, tokens)?;
1788            let x =
1789                self.prime_layers(e0, x, fence[0], fence[1], &pos_d, t, base, cache, seq_end)?;
1790            rt.tx(0, &x, payload)?
1791            // x + pos_d drop here: freed stream-ordered on stage-0's stream after use.
1792        };
1793
1794        // ---- MIDDLE STAGES: RX boundary s-1 -> range -> TX boundary s ----
1795        for s in 1..n_st - 1 {
1796            let _st = rt.enter(s);
1797            let es = rt.engine(s, e);
1798            let pos_d = es.htod_i32(&pos)?;
1799            let x = rt.rx(s - 1, slot, payload)?;
1800            let x = self.prime_layers(
1801                es,
1802                x,
1803                fence[s],
1804                fence[s + 1],
1805                &pos_d,
1806                t,
1807                base,
1808                cache,
1809                seq_end,
1810            )?;
1811            slot = rt.tx(s, &x, payload)?;
1812        }
1813
1814        // ---- LAST STAGE: RX + final range + the shared epilogue ----
1815        let _stl = rt.enter(n_st - 1);
1816        let el = rt.engine(n_st - 1, e);
1817        let pos_d = el.htod_i32(&pos)?;
1818        let x = rt.rx(n_st - 2, slot, payload)?;
1819        let x = self.prime_layers(
1820            el,
1821            x,
1822            fence[n_st - 1],
1823            fence[n_st],
1824            &pos_d,
1825            t,
1826            base,
1827            cache,
1828            seq_end,
1829        )?;
1830        let out = self.prime_chunk_epilogue(el, x, t, cache)?;
1831        // EXIT PUBLICATION: h_seed + the hidden stack are device-resident on the last
1832        // stage's stream; the caller resumes on its own stream (chunk-loop copy_into /
1833        // generate_spec's prompt_h consumer). The logits dtoh above already drained the
1834        // stage stream host-side, but the law is stated in events, not in a dtoh side
1835        // effect a later deferred form would remove.
1836        rt.publish_to(n_st - 1, &caller_stream)?;
1837        crate::pp::PRIME_SPLIT_CHUNKS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1838        Ok(out)
1839    }
1840
1841    fn prime_pp2_stage0_enqueue(
1842        &self,
1843        e: &Engine,
1844        rt: &crate::pp::PpNRt,
1845        tokens: &[u32],
1846        cache: &mut Cache,
1847        seq_end: usize,
1848        fence: &[usize],
1849        base: usize,
1850        pipelined: bool,
1851    ) -> Result<usize, Box<dyn std::error::Error>> {
1852        let t = tokens.len();
1853        let n_embd = self.cfg.n_embd as usize;
1854        let pos: Vec<i32> = (base as i32..(base + t) as i32).collect();
1855        rt.bind_stage(0)?;
1856        let _st0 = rt.enter(0);
1857        let e0 = rt.engine(0, e);
1858        let pos_d = e0.htod_i32(&pos)?;
1859        let x = self.embed(e0, tokens)?;
1860        let _overlap = pipelined.then(crate::pp::enter_prime_pipe_stage);
1861        let x = self.prime_layers(e0, x, fence[0], fence[1], &pos_d, t, base, cache, seq_end)?;
1862        if pipelined {
1863            rt.tx_pipelined(0, &x, t * n_embd)
1864        } else {
1865            rt.tx(0, &x, t * n_embd)
1866        }
1867    }
1868
1869    fn prime_pp2_stage1_enqueue(
1870        &self,
1871        e: &Engine,
1872        rt: &crate::pp::PpNRt,
1873        slot: usize,
1874        t: usize,
1875        cache: &mut Cache,
1876        seq_end: usize,
1877        fence: &[usize],
1878        base: usize,
1879        pipelined: bool,
1880    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1881        let n_embd = self.cfg.n_embd as usize;
1882        let pos: Vec<i32> = (base as i32..(base + t) as i32).collect();
1883        rt.bind_stage(1)?;
1884        let _st1 = rt.enter(1);
1885        let e1 = rt.engine(1, e);
1886        let pos_d = e1.htod_i32(&pos)?;
1887        let x = rt.rx(0, slot, t * n_embd)?;
1888        let _overlap = pipelined.then(crate::pp::enter_prime_pipe_stage);
1889        self.prime_layers(e1, x, fence[1], fence[2], &pos_d, t, base, cache, seq_end)
1890    }
1891
1892    /// CAPTURE-SAFE prime trunk (task #14 increment 1, ARCHITECTURE-H100.md design v2):
1893    /// prime_chunk's layer stack with every capture hazard hoisted — `x` is the
1894    /// PRE-EMBEDDED input in a STABLE graph-input buffer (embed + its 8MB htod stay
1895    /// eager, one launch), `pos_d` is a baked device param (fresh prime = 0..T, constant
1896    /// per bucket), logits/h_seed/hidden stay DEVICE-resident (no dtoh), and cache.pos is
1897    /// NOT advanced (host state — the replay wrapper owns it). Body mirrors prime_chunk
1898    /// (the prime-graph-gate pins them together). KNOWN smoke-scope gap: append host-len
1899    /// bookkeeping still runs on the host per call — the real replay path moves the write
1900    /// slot to the len_d device counter (increment 3).
1901    /// GRAPH-OUTPUT CONTRACT: results are COPIED into caller-provided stable buffers
1902    /// (`logits_out` [n_vocab], `h_seed_out` [n_embd]) — every internal allocation drops
1903    /// INSIDE the capture region (alloc+free node pairs). Retaining an in-capture
1904    /// allocation across end_capture makes instantiate throw INVALID_VALUE (smoke finding
1905    /// 2026-07-26), and under AUTO_FREE_ON_LAUNCH its address wouldn't survive a launch
1906    /// anyway — the decode GraphSession's pre-allocated-output pattern is the law here.
1907    pub fn prime_chunk_captured(
1908        &self,
1909        e: &Engine,
1910        x_in: &CudaSlice<f32>,
1911        pos_d: &CudaSlice<i32>,
1912        t: usize,
1913        cache: &mut Cache,
1914        len_d: &CudaSlice<i32>,
1915        logits_out: &mut CudaSlice<f32>,
1916        h_seed_out: &mut CudaSlice<f32>,
1917    ) -> Result<(), Box<dyn std::error::Error>> {
1918        let cfg = &self.cfg;
1919        let n_embd = cfg.n_embd as usize;
1920        let eps = cfg.rms_eps;
1921        let f16fuse = crate::f16_ffi::pp_f16_enabled() && t >= 16;
1922        let mut x = e.uninit(t * n_embd)?;
1923        e.copy_into(&mut x, 0, x_in, t * n_embd)?;
1924        for (il, layer) in self.layers.iter().enumerate() {
1925            let mut h = e.uninit(t * n_embd)?;
1926            let mut hx16: Option<CudaSlice<u8>> = None;
1927            if f16fuse {
1928                let mut b16 = e.alloc_u8_uninit(t * n_embd * 2)?;
1929                e.rms_norm_f16out(
1930                    &x,
1931                    layer.attn_norm.float_data(),
1932                    &mut h,
1933                    &mut b16,
1934                    n_embd,
1935                    t,
1936                    eps,
1937                )?;
1938                hx16 = Some(b16);
1939            } else {
1940                e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
1941            }
1942            let mixed = match &layer.mixer {
1943                // The captured prime is ONE unchunked bucket over a FRESH cache (pos == 0), so the
1944                // request ends at t. If bucketed capture ever composes with chunking, seq_end must
1945                // come from the caller (see step35_attn_pre_wo's doc note).
1946                Mixer::Full(fa) => {
1947                    self.full_attn_prime(e, fa, &h, hx16.as_ref(), pos_d, t, cache, il, t)?
1948                }
1949                Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
1950                Mixer::Linear(la) => {
1951                    let ws = [&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha];
1952                    let g4 = match hx16.as_ref() {
1953                        Some(xh) => e.matmul_group_xh(&ws, &h, xh, t)?,
1954                        None => e.matmul_group(&ws, &h, t)?,
1955                    };
1956                    self.linear_attn_prime_core_pad(e, la, g4, t, cache, il, Some(len_d))?
1957                }
1958            };
1959            let mut x1 = e.uninit(t * n_embd)?;
1960            e.add(&x, &mixed, &mut x1, t * n_embd)?;
1961            let mut z = e.uninit(t * n_embd)?;
1962            let mut zx16: Option<CudaSlice<u8>> = None;
1963            if f16fuse {
1964                let mut b16 = e.alloc_u8_uninit(t * n_embd * 2)?;
1965                e.rms_norm_f16out(
1966                    &x1,
1967                    layer.post_attn_norm.float_data(),
1968                    &mut z,
1969                    &mut b16,
1970                    n_embd,
1971                    t,
1972                    eps,
1973                )?;
1974                zx16 = Some(b16);
1975            } else {
1976                e.rms_norm(
1977                    &x1,
1978                    layer.post_attn_norm.float_data(),
1979                    &mut z,
1980                    n_embd,
1981                    t,
1982                    eps,
1983                )?;
1984            }
1985            let ffn_out = match &layer.ffn {
1986                crate::hybrid::Ffn::Dense {
1987                    ffn_gate,
1988                    ffn_up,
1989                    ffn_down,
1990                } => {
1991                    let n_ff = ffn_gate.out_features();
1992                    let mut g2 = match &zx16 {
1993                        Some(xh) => e.matmul_group_xh(&[ffn_gate, ffn_up], &z, xh, t)?,
1994                        None => e.matmul_group(&[ffn_gate, ffn_up], &z, t)?,
1995                    };
1996                    let up = g2.pop().unwrap();
1997                    let gate = g2.pop().unwrap();
1998                    let mut act = e.uninit(t * n_ff)?;
1999                    // dense FFN keys off the SHEXP clamp array — see forward()'s note.
2000                    Self::ffn_act_lim(
2001                        e,
2002                        &self.cfg,
2003                        &gate,
2004                        &up,
2005                        1.0,
2006                        1.0,
2007                        self.cfg.clamp_shexp_at(il as u32),
2008                        &mut act,
2009                        t * n_ff,
2010                    )?;
2011                    e.matmul(ffn_down, &act, t)?
2012                }
2013                crate::hybrid::Ffn::Moe(m) => self.moe_ffn_il_prefill(e, m, &z, t, il as u16)?,
2014            };
2015            let mut x2 = e.uninit(t * n_embd)?;
2016            e.add(&x1, &ffn_out, &mut x2, t * n_embd)?;
2017            x = x2;
2018        }
2019        // device-indexed TRUE last row (pads sit past it in a bucketed graph)
2020        if !crate::spec::spec_hpost() {
2021            e.row_gather_dev(&x, h_seed_out, len_d, n_embd)?;
2022        }
2023        let mut hn = e.uninit(t * n_embd)?;
2024        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
2025        if crate::spec::spec_hpost() {
2026            e.row_gather_dev(&hn, h_seed_out, len_d, n_embd)?;
2027        }
2028        let mut hlast = e.uninit(n_embd)?;
2029        e.row_gather_dev(&hn, &mut hlast, len_d, n_embd)?;
2030        let logits = e.matmul(&self.output, &hlast, 1)?;
2031        let nv = logits.len();
2032        e.copy_into(logits_out, 0, &logits, nv)?;
2033        Ok(())
2034    }
2035
2036    fn step35_prime_batch_on() -> bool {
2037        std::env::var("MEMRA_STEP35_PRIME_BATCH").as_deref() != Ok("0")
2038    }
2039
2040    /// Step35 cross-request prime range: weight-streaming work runs once at `m=sum(T)`;
2041    /// sequence-scoped attention/KV work stays on each request's own cache and positions.
2042    #[allow(clippy::too_many_arguments)]
2043    fn step35_prime_batch_layers(
2044        &self,
2045        e: &Engine,
2046        mut x: CudaSlice<f32>,
2047        lo: usize,
2048        hi: usize,
2049        ts: &[usize],
2050        offs: &[usize],
2051        pos_ds: &[CudaSlice<i32>],
2052        caches: &mut [&mut Cache],
2053    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2054        let cfg = &self.cfg;
2055        let n_embd = cfg.n_embd as usize;
2056        let eps = cfg.rms_eps;
2057        let b = ts.len();
2058        let total: usize = ts.iter().sum();
2059        let f16fuse = crate::f16_ffi::pp_f16_enabled() && total >= 16;
2060
2061        let split = |e: &Engine,
2062                     y: &CudaSlice<f32>,
2063                     dim: usize|
2064         -> Result<Vec<CudaSlice<f32>>, Box<dyn std::error::Error>> {
2065            let mut out = Vec::with_capacity(b);
2066            for s in 0..b {
2067                let mut ys = e.uninit(ts[s] * dim)?;
2068                e.copy_view_into(
2069                    &mut ys,
2070                    0,
2071                    &y.slice(offs[s] * dim..(offs[s] + ts[s]) * dim),
2072                    ts[s] * dim,
2073                )?;
2074                out.push(ys);
2075            }
2076            Ok(out)
2077        };
2078
2079        for il in lo..hi {
2080            let layer = &self.layers[il];
2081            let Mixer::Full(fa) = &layer.mixer else {
2082                return Err(format!("step35 layer {il} is not full-attn — corrupt config").into());
2083            };
2084
2085            let mut h = e.uninit(total * n_embd)?;
2086            let mut hx16 = e.alloc_u8_uninit(total * n_embd * 2)?;
2087            if f16fuse {
2088                e.rms_norm_f16out(
2089                    &x,
2090                    layer.attn_norm.float_data(),
2091                    &mut h,
2092                    &mut hx16,
2093                    n_embd,
2094                    total,
2095                    eps,
2096                )?;
2097            } else {
2098                e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, total, eps)?;
2099            }
2100
2101            // Q/K/V + gate projections and wo are batched weight streams. The step35 core
2102            // remains per sequence, so its partial RoPE, SWA view, KV append, and gate
2103            // application stay verbatim.
2104            let gate_w = fa
2105                .attn_gate
2106                .as_ref()
2107                .ok_or("step35 layer is missing attn_gate.weight (head-wise attention gate)")?;
2108            let mut g4 = if f16fuse {
2109                e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv, gate_w], &h, &hx16, total)?
2110            } else {
2111                e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv, gate_w], &h, total)?
2112            };
2113            let gate = g4.pop().unwrap();
2114            let mut parts: Vec<Vec<CudaSlice<f32>>> =
2115                (0..b).map(|_| Vec::with_capacity(3)).collect();
2116            for (w, y) in [&fa.wq, &fa.wk, &fa.wv].iter().zip(g4) {
2117                for (s, ys) in split(e, &y, w.out_features())?.into_iter().enumerate() {
2118                    parts[s].push(ys);
2119                }
2120            }
2121            let gates = split(e, &gate, gate_w.out_features())?;
2122            let geometry = self.step35_geom(il);
2123            let hd = geometry.head_dim_k as usize;
2124            let nh = geometry.n_head as usize;
2125            let mut ag_cat = e.uninit(total * nh * hd)?;
2126            for (s, (g3s, gate)) in parts.into_iter().zip(gates).enumerate() {
2127                let ag = self.step35_attn_pre_wo(
2128                    e,
2129                    fa,
2130                    g3s,
2131                    None,
2132                    Some(&gate),
2133                    &pos_ds[s],
2134                    ts[s],
2135                    Some(&mut *caches[s]),
2136                    il,
2137                    ts[s],
2138                )?;
2139                e.copy_into(&mut ag_cat, offs[s] * nh * hd, &ag, ts[s] * nh * hd)?;
2140            }
2141            let mixed = e.matmul(&fa.wo, &ag_cat, total)?;
2142
2143            let mut x1 = e.uninit(total * n_embd)?;
2144            let mut z = e.uninit(total * n_embd)?;
2145            let mut zx16 = e.alloc_u8_uninit(total * n_embd * 2)?;
2146            if f16fuse {
2147                e.add_rms_norm_f16out(
2148                    &x,
2149                    &mixed,
2150                    layer.post_attn_norm.float_data(),
2151                    &mut x1,
2152                    &mut z,
2153                    &mut zx16,
2154                    n_embd,
2155                    total,
2156                    eps,
2157                )?;
2158            } else {
2159                e.add(&x, &mixed, &mut x1, total * n_embd)?;
2160                e.rms_norm(
2161                    &x1,
2162                    layer.post_attn_norm.float_data(),
2163                    &mut z,
2164                    n_embd,
2165                    total,
2166                    eps,
2167                )?;
2168            }
2169
2170            let ffn_out = match &layer.ffn {
2171                crate::hybrid::Ffn::Dense {
2172                    ffn_gate,
2173                    ffn_up,
2174                    ffn_down,
2175                } => {
2176                    let n_ff = ffn_gate.out_features();
2177                    let mut g2 = if f16fuse {
2178                        e.matmul_group_xh(&[ffn_gate, ffn_up], &z, &zx16, total)?
2179                    } else {
2180                        e.matmul_group(&[ffn_gate, ffn_up], &z, total)?
2181                    };
2182                    let up = g2.pop().unwrap();
2183                    let gate = g2.pop().unwrap();
2184                    let mut act = e.uninit(total * n_ff)?;
2185                    let d_lim = cfg.clamp_shexp_at(il as u32);
2186                    if Self::f16out_on(e, total) && cfg.m3.is_none() && d_lim.is_none() {
2187                        let mut a16 = e.alloc_u8_uninit(total * n_ff * 2)?;
2188                        e.silu_mul_f16out(&gate, &up, &mut act, &mut a16, total * n_ff)?;
2189                        match e.try_f16_gemm_pre(ffn_down, &a16, total)? {
2190                            Some(y) => y,
2191                            None => e.matmul(ffn_down, &act, total)?,
2192                        }
2193                    } else {
2194                        Self::ffn_act_lim(
2195                            e,
2196                            cfg,
2197                            &gate,
2198                            &up,
2199                            1.0,
2200                            1.0,
2201                            d_lim,
2202                            &mut act,
2203                            total * n_ff,
2204                        )?;
2205                        e.matmul(ffn_down, &act, total)?
2206                    }
2207                }
2208                crate::hybrid::Ffn::Moe(m) => self.moe_ffn_il(e, m, &z, total, il as u16)?,
2209            };
2210            let mut x2 = e.uninit(total * n_embd)?;
2211            e.add(&x1, &ffn_out, &mut x2, total * n_embd)?;
2212            x = x2;
2213        }
2214        Ok(x)
2215    }
2216
2217    fn step35_prime_batch_epilogue(
2218        &self,
2219        e: &Engine,
2220        x: CudaSlice<f32>,
2221        ts: &[usize],
2222        offs: &[usize],
2223        caches: &mut [&mut Cache],
2224    ) -> Result<Vec<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
2225        let n_embd = self.cfg.n_embd as usize;
2226        let total: usize = ts.iter().sum();
2227        let mut hn = e.uninit(total * n_embd)?;
2228        e.rms_norm(
2229            &x,
2230            self.output_norm.float_data(),
2231            &mut hn,
2232            n_embd,
2233            total,
2234            self.cfg.rms_eps,
2235        )?;
2236
2237        let hidden_src = if crate::spec::spec_hpost() { &hn } else { &x };
2238        let mut out = Vec::with_capacity(ts.len());
2239        for s in 0..ts.len() {
2240            let mut hidden = e.uninit(ts[s] * n_embd)?;
2241            e.copy_view_into(
2242                &mut hidden,
2243                0,
2244                &hidden_src.slice(offs[s] * n_embd..(offs[s] + ts[s]) * n_embd),
2245                ts[s] * n_embd,
2246            )?;
2247            let last0 = (offs[s] + ts[s] - 1) * n_embd;
2248            let mut h_seed = e.uninit(n_embd)?;
2249            e.copy_view_into(
2250                &mut h_seed,
2251                0,
2252                &hidden_src.slice(last0..last0 + n_embd),
2253                n_embd,
2254            )?;
2255            // Exactness-first: the serial reference runs the output head at m=1.
2256            let mut hlast = e.uninit(n_embd)?;
2257            e.copy_view_into(&mut hlast, 0, &hn.slice(last0..last0 + n_embd), n_embd)?;
2258            let logits = e.dtoh(&e.matmul(&self.output, &hlast, 1)?)?;
2259            caches[s].pos += ts[s];
2260            out.push((logits, h_seed, hidden));
2261        }
2262        Ok(out)
2263    }
2264
2265    fn step35_prime_cache_batch(
2266        &self,
2267        e: &Engine,
2268        prompts: &[&[u32]],
2269        caches: &mut [&mut Cache],
2270    ) -> Result<Vec<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
2271        if crate::pp::pp_host_bounce_active()
2272            && (!crate::pp::prime_pp_on() || crate::pp::pp_cuts(self.layers.len()).is_none())
2273        {
2274            return Err(
2275                "step35_prime_cache_batch: MEMRA_PP_HOST_BOUNCE=1 requires a valid prime \
2276                 stage split; refusing an unsplit remote-weight walk"
2277                    .into(),
2278            );
2279        }
2280        if !Self::step35_prime_batch_on() {
2281            return Err("step35 batched prime is disabled (MEMRA_STEP35_PRIME_BATCH=0)".into());
2282        }
2283        if caches.iter().any(|c| c.pos != 0) {
2284            return Err(
2285                "step35 batched prime currently supports complete fresh prompts only; \
2286                 continuation/tick chunks require per-request queued_after"
2287                    .into(),
2288            );
2289        }
2290
2291        let ts: Vec<usize> = prompts.iter().map(|p| p.len()).collect();
2292        for &t in &ts {
2293            assert!(
2294                t >= PRIME_MIN_T,
2295                "step35 batched prime needs T >= {PRIME_MIN_T}"
2296            );
2297        }
2298        for (s, c) in caches.iter().enumerate() {
2299            assert!(
2300                ts[s] <= c.max_ctx,
2301                "step35 batched prime exceeds cache max_ctx"
2302            );
2303        }
2304        let offs: Vec<usize> = ts
2305            .iter()
2306            .scan(0usize, |a, &t| {
2307                let o = *a;
2308                *a += t;
2309                Some(o)
2310            })
2311            .collect();
2312        let total: usize = ts.iter().sum();
2313        let payload = total * self.cfg.n_embd as usize;
2314        let cat_tokens: Vec<u32> = prompts.iter().flat_map(|p| p.iter().copied()).collect();
2315        let positions: Vec<Vec<i32>> = ts.iter().map(|&t| (0..t as i32).collect()).collect();
2316        let upload_positions =
2317            |e: &Engine| -> Result<Vec<CudaSlice<i32>>, Box<dyn std::error::Error>> {
2318                positions
2319                    .iter()
2320                    .map(|p| e.htod_i32(p))
2321                    .collect::<Result<_, _>>()
2322            };
2323
2324        static ONCE: std::sync::Once = std::sync::Once::new();
2325        ONCE.call_once(|| {
2326            eprintln!(
2327                "[step35-prime-batch] first concat prime: B={} tokens={total}",
2328                prompts.len()
2329            );
2330        });
2331
2332        let out = if !crate::pp::pp2_streams_off() && crate::pp::prime_pp_on() {
2333            if let Some(fence) = crate::pp::pp_cuts(self.layers.len()) {
2334                let rt = crate::pp::PpNRt::get(e)?;
2335                let n_st = fence.len() - 1;
2336                assert_eq!(
2337                    rt.n_stages(),
2338                    n_st,
2339                    "step35 prime batch stage count mismatch"
2340                );
2341                let caller_stream = e.stream();
2342                rt.fence_stages_behind(&caller_stream)?;
2343
2344                let mut slot = {
2345                    let _st0 = rt.enter(0);
2346                    let e0 = rt.engine(0, e);
2347                    let pos_ds = upload_positions(e0)?;
2348                    let x = self.embed(e0, &cat_tokens)?;
2349                    let x = self.step35_prime_batch_layers(
2350                        e0, x, fence[0], fence[1], &ts, &offs, &pos_ds, caches,
2351                    )?;
2352                    rt.tx(0, &x, payload)?
2353                };
2354                for s in 1..n_st - 1 {
2355                    let _st = rt.enter(s);
2356                    let es = rt.engine(s, e);
2357                    let pos_ds = upload_positions(es)?;
2358                    let x = rt.rx(s - 1, slot, payload)?;
2359                    let x = self.step35_prime_batch_layers(
2360                        es,
2361                        x,
2362                        fence[s],
2363                        fence[s + 1],
2364                        &ts,
2365                        &offs,
2366                        &pos_ds,
2367                        caches,
2368                    )?;
2369                    slot = rt.tx(s, &x, payload)?;
2370                }
2371
2372                let _stl = rt.enter(n_st - 1);
2373                let el = rt.engine(n_st - 1, e);
2374                let pos_ds = upload_positions(el)?;
2375                let x = rt.rx(n_st - 2, slot, payload)?;
2376                let x = self.step35_prime_batch_layers(
2377                    el,
2378                    x,
2379                    fence[n_st - 1],
2380                    fence[n_st],
2381                    &ts,
2382                    &offs,
2383                    &pos_ds,
2384                    caches,
2385                )?;
2386                let out = self.step35_prime_batch_epilogue(el, x, &ts, &offs, caches)?;
2387                rt.publish_to(n_st - 1, &caller_stream)?;
2388                crate::pp::STEP35_PRIME_BATCH_SPLITS
2389                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2390                out
2391            } else {
2392                let pos_ds = upload_positions(e)?;
2393                let x = self.embed(e, &cat_tokens)?;
2394                let x = self.step35_prime_batch_layers(
2395                    e,
2396                    x,
2397                    0,
2398                    self.layers.len(),
2399                    &ts,
2400                    &offs,
2401                    &pos_ds,
2402                    caches,
2403                )?;
2404                self.step35_prime_batch_epilogue(e, x, &ts, &offs, caches)?
2405            }
2406        } else {
2407            let pos_ds = upload_positions(e)?;
2408            let x = self.embed(e, &cat_tokens)?;
2409            let x = self.step35_prime_batch_layers(
2410                e,
2411                x,
2412                0,
2413                self.layers.len(),
2414                &ts,
2415                &offs,
2416                &pos_ds,
2417                caches,
2418            )?;
2419            self.step35_prime_batch_epilogue(e, x, &ts, &offs, caches)?
2420        };
2421        crate::pp::STEP35_PRIME_BATCHES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2422        Ok(out)
2423    }
2424
2425    /// Cross-request BATCHED fresh prime (task #13, design in ARCHITECTURE-H100.md): the
2426    /// trunk's token-parallel ops (embed, norms, adds, ffn, projection GROUPS) run once on
2427    /// the CONCATENATION of B sequences — GEMMs at m = sum_T, the continuous-batching win
2428    /// the serving-lane bench attributed the remaining vLLM gap to. The stateful mixer
2429    /// CORES (QK-norm/RoPE/FA/append, conv/GDN scans) run per sequence on split projection
2430    /// buffers (D2D row copies; the mixers' own out-projections stay per-seq this
2431    /// increment). CONTINUATION primes (increment (b), 2026-07-30): cache.pos > 0 seqs
2432    /// batch the projections/FFN/lm_head exactly like fresh; the mixer cores take the
2433    /// per-seq CONTINUATION arms (Full: core_inner with carried pos_d + fa_prefill_view
2434    /// over the quantized past; Linear: the stateful pad_view twin — the same state
2435    /// carry the chunked single-seq prime rides). The fresh-only favl/gdn-vl fast paths
2436    /// stay byte-identical (gated on !carried). gemma4 models have no continuation
2437    /// prime (v0 monolithic fresh) — carried gemma4 batches return Err (caller falls
2438    /// back to single-chunk serving).
2439    /// NUMERIC CONFIG: a concat GEMM tiles K differently than per-seq GEMMs — same class
2440    /// as every prefill GEMM change; prime_batch_gate arbitrates (argmax + stream battery).
2441    pub fn prime_cache_batch(
2442        &self,
2443        e: &Engine,
2444        prompts: &[&[u32]],
2445        caches: &mut [&mut Cache],
2446    ) -> Result<Vec<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
2447        let cfg = &self.cfg;
2448        let n_embd = cfg.n_embd as usize;
2449        let eps = cfg.rms_eps;
2450        let b = prompts.len();
2451        assert!(b >= 1 && b == caches.len());
2452        let pos0s: Vec<usize> = caches.iter().map(|c| c.pos).collect();
2453        let carried = pos0s.iter().any(|&p| p > 0);
2454        // gemma4: refuse UNCONDITIONALLY (2026-08-07, lane/gemma4-serve-gaps). The old guard
2455        // covered only `carried` — two concurrent FRESH gemma4 prompts batched into the
2456        // generic concat attn core below (uniform geometry, no per-layer swa window, no
2457        // softcapped head): compiles, runs, wrong logits. Same silent-wrong class as the
2458        // step35 refusal beneath. The per-sequence `gemma4_prime` is the supported prefill.
2459        if cfg.gemma4.is_some() {
2460            return Err(
2461                "prime_cache_batch: gemma4 has no batched prime core (per-layer \
2462                        swa/global geometry, softcapped head) — use gemma4_prime per sequence"
2463                    .into(),
2464            );
2465        }
2466        // Step35 has a dedicated concat walk: the generic core below cannot express its
2467        // per-layer geometry/SWA/head gate, and under PP the dedicated path is stage-scoped.
2468        if cfg.step35.is_some() {
2469            return self.step35_prime_cache_batch(e, prompts, caches);
2470        }
2471        let ts: Vec<usize> = prompts.iter().map(|p| p.len()).collect();
2472        for &t in &ts {
2473            assert!(
2474                t >= PRIME_MIN_T,
2475                "prime_cache_batch needs T >= {PRIME_MIN_T}"
2476            );
2477        }
2478        for (s, c) in caches.iter().enumerate() {
2479            assert!(
2480                c.pos + ts[s] <= c.max_ctx,
2481                "prime_cache_batch: prompt exceeds cache max_ctx"
2482            );
2483        }
2484        let total: usize = ts.iter().sum();
2485        let offs: Vec<usize> = ts
2486            .iter()
2487            .scan(0usize, |a, &t| {
2488                let o = *a;
2489                *a += t;
2490                Some(o)
2491            })
2492            .collect();
2493        // per-seq positions (fresh: 0..T_s; continuation: pos0..pos0+T_s)
2494        let pos_ds: Vec<CudaSlice<i32>> = ts
2495            .iter()
2496            .zip(&pos0s)
2497            .map(|(&t, &p0)| e.htod_i32(&(p0 as i32..(p0 + t) as i32).collect::<Vec<_>>()))
2498            .collect::<Result<_, _>>()?;
2499        // split a concat [total, dim] buffer into per-seq copies
2500        let split = |e: &Engine,
2501                     y: &CudaSlice<f32>,
2502                     dim: usize|
2503         -> Result<Vec<CudaSlice<f32>>, Box<dyn std::error::Error>> {
2504            let mut out = Vec::with_capacity(b);
2505            for s in 0..b {
2506                let mut ys = e.uninit(ts[s] * dim)?;
2507                e.copy_view_into(
2508                    &mut ys,
2509                    0,
2510                    &y.slice(offs[s] * dim..(offs[s] + ts[s]) * dim),
2511                    ts[s] * dim,
2512                )?;
2513                out.push(ys);
2514            }
2515            Ok(out)
2516        };
2517
2518        let cat_tokens: Vec<u32> = prompts.iter().flat_map(|p| p.iter().copied()).collect();
2519        let mut x = self.embed(e, &cat_tokens)?; // [total, n_embd]
2520        for (il, layer) in self.layers.iter().enumerate() {
2521            let mut h = e.uninit(total * n_embd)?;
2522            let mut hx16 = e.alloc_u8_uninit(total * n_embd * 2)?;
2523            e.rms_norm_f16out(
2524                &x,
2525                layer.attn_norm.float_data(),
2526                &mut h,
2527                &mut hx16,
2528                n_embd,
2529                total,
2530                eps,
2531            )?;
2532            // mixer: projection GROUP on the concat (m = total), stateful core per seq
2533            let mut mixed = e.uninit(total * n_embd)?;
2534            match &layer.mixer {
2535                Mixer::Full(fa) => {
2536                    let g3 = e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv], &h, &hx16, total)?;
2537                    // task #18 (attn side): the WHOLE attn core is varlen for fresh gated
2538                    // batches — split/QK-norm/RoPE/append (attn_pre_vl8, view inputs: the
2539                    // q/k/v split copies vanish) + ONE varlen FA. Per-block math identical
2540                    // everywhere (bit-gateable); MEMRA_FA_VL=0 or a non-bf16kv config falls
2541                    // back to the per-seq dispatch.
2542                    let geometry = self.cfg.full_attention_geometry_at(il as u32);
2543                    let (n_head, n_head_kv, head_dim) = (
2544                        geometry.n_head as usize,
2545                        geometry.n_head_kv as usize,
2546                        geometry.head_dim_k as usize,
2547                    );
2548                    let fa_scale = geometry.attention_scale();
2549                    let use_favl = !carried
2550                        && (2..=8).contains(&b)
2551                        && (head_dim == 256 || head_dim == 128)
2552                        && geometry.attention_gate == memra_gguf::config::AttentionGateKind::FusedQ
2553                        && std::env::var("MEMRA_NOFA").is_err()
2554                        && std::env::var("MEMRA_FA_FLOOR").is_err()
2555                        && std::env::var("MEMRA_FA_PP_W2").as_deref() != Ok("1")
2556                        && std::env::var("MEMRA_FA_BF16KV").as_deref() != Ok("0")
2557                        && std::env::var("MEMRA_FA_VL").as_deref() != Ok("0");
2558                    if use_favl {
2559                        let (qf_w, kf_w, vf_w) = (
2560                            fa.wq.out_features(),
2561                            fa.wk.out_features(),
2562                            fa.wv.out_features(),
2563                        );
2564                        struct APre {
2565                            q: CudaSlice<f32>,
2566                            gate: Option<CudaSlice<f32>>,
2567                            qn: CudaSlice<f32>,
2568                            kn: CudaSlice<f32>,
2569                        }
2570                        let mut aps = Vec::with_capacity(b);
2571                        for &t in ts.iter().take(b) {
2572                            aps.push(APre {
2573                                q: e.uninit(t * n_head * head_dim)?,
2574                                gate: Some(e.uninit(t * n_head * head_dim)?),
2575                                qn: e.uninit(t * n_head * head_dim)?,
2576                                kn: e.uninit(t * n_head_kv * head_dim)?,
2577                            });
2578                        }
2579                        let (kv_dim_k, kv_dim_v, ktb, vtb) = {
2580                            let kvl = caches[0].kv[il].as_ref().unwrap();
2581                            (kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes)
2582                        };
2583                        let pargs: Vec<crate::AttnPreVl> = (0..b)
2584                            .map(|s| {
2585                                let (o, t) = (offs[s], ts[s]);
2586                                let kvl = caches[s].kv[il].as_ref().unwrap();
2587                                assert!(
2588                                    kvl.len == 0 && kvl.len + t <= caches[s].max_ctx,
2589                                    "prime_cache_batch attn vl: fresh + capacity"
2590                                );
2591                                crate::AttnPreVl {
2592                                    qf: e.addr_f32v(&g3[0].slice(o * qf_w..(o + t) * qf_w)),
2593                                    kf: e.addr_f32v(&g3[1].slice(o * kf_w..(o + t) * kf_w)),
2594                                    vf: e.addr_f32v(&g3[2].slice(o * vf_w..(o + t) * vf_w)),
2595                                    q: e.addr_f32(&aps[s].q),
2596                                    gate: e.addr_f32(aps[s].gate.as_ref().unwrap()),
2597                                    qn: e.addr_f32(&aps[s].qn),
2598                                    kn: e.addr_f32(&aps[s].kn),
2599                                    kc: e.addr_u8(&kvl.k),
2600                                    vc: e.addr_u8(&kvl.v),
2601                                    t: t as i32,
2602                                    pad: 0,
2603                                }
2604                            })
2605                            .collect();
2606                        e.attn_pre_vl8(
2607                            &pargs,
2608                            fa.q_norm.float_data(),
2609                            fa.k_norm.float_data(),
2610                            head_dim,
2611                            geometry.n_rot as usize,
2612                            n_head,
2613                            n_head_kv,
2614                            self.cfg.rms_eps,
2615                            geometry.rope_base,
2616                            1.0,
2617                            kv_dim_k,
2618                            kv_dim_v,
2619                            ktb,
2620                            vtb,
2621                        )?;
2622                        for s in 0..b {
2623                            let kvl = caches[s].kv[il].as_mut().unwrap();
2624                            kvl.len += ts[s];
2625                            let new_len = kvl.len as i32;
2626                            e.set_i32_one(&mut kvl.len_d, new_len)?;
2627                        }
2628                        let mut attns = Vec::with_capacity(b);
2629                        let mut mirrors = Vec::with_capacity(b);
2630                        for &t in ts.iter().take(b) {
2631                            attns.push(e.uninit(t * n_head * head_dim)?);
2632                            let n = t * n_head_kv * head_dim;
2633                            mirrors.push((e.alloc_u8_uninit(n * 2)?, e.alloc_u8_uninit(n * 2)?));
2634                        }
2635                        // FA3 batched twin (round 31): TMA-swizzled wgmma vl when the
2636                        // promoted single-seq config is on; else the mma favl.
2637                        let fa3_on = match std::env::var("MEMRA_FA3").as_deref() {
2638                            Ok("0") => false,
2639                            Ok("1") => true,
2640                            _ => cfg!(memra_hopper_mma),
2641                        };
2642                        if fa3_on {
2643                            let mut q16s = Vec::with_capacity(b);
2644                            let mut v16s = Vec::with_capacity(b);
2645                            for s in 0..b {
2646                                let t = ts[s];
2647                                let mut q16 = e.alloc_u8_uninit(t * n_head * head_dim * 2)?;
2648                                e.f32_to_bf16_into(&aps[s].qn, &mut q16, t * n_head * head_dim)?;
2649                                let mut k16 = e.alloc_u8_uninit(t * n_head_kv * head_dim * 2)?;
2650                                e.f32_to_bf16_into(&aps[s].kn, &mut k16, t * n_head_kv * head_dim)?;
2651                                let mut v16 = e.alloc_u8_uninit(t * n_head_kv * head_dim * 2)?;
2652                                e.f32_to_bf16_v(
2653                                    &g3[2].slice(offs[s] * vf_w..(offs[s] + t) * vf_w),
2654                                    &mut v16,
2655                                    t * n_head_kv * head_dim,
2656                                )?;
2657                                q16s.push(q16);
2658                                v16s.push((k16, v16));
2659                            }
2660                            let mut qp = [core::ptr::null::<core::ffi::c_void>(); 8];
2661                            let mut kp = qp;
2662                            let mut vp = qp;
2663                            let mut op = [core::ptr::null_mut::<f32>(); 8];
2664                            let mut tsv = [0i32; 8];
2665                            for s in 0..b {
2666                                qp[s] = e.addr_u8(&q16s[s]) as *const core::ffi::c_void;
2667                                kp[s] = e.addr_u8(&v16s[s].0) as *const core::ffi::c_void;
2668                                vp[s] = e.addr_u8(&v16s[s].1) as *const core::ffi::c_void;
2669                                op[s] = e.addr_f32(&attns[s]) as *mut f32;
2670                                tsv[s] = ts[s] as i32;
2671                            }
2672                            let rc = unsafe {
2673                                crate::fa3_vl_raw(
2674                                    qp.as_ptr(),
2675                                    kp.as_ptr(),
2676                                    vp.as_ptr(),
2677                                    op.as_ptr(),
2678                                    tsv.as_ptr(),
2679                                    b as i32,
2680                                    n_head as i32,
2681                                    n_head_kv as i32,
2682                                    head_dim as i32,
2683                                    fa_scale,
2684                                    e.stream().cu_stream() as *mut core::ffi::c_void,
2685                                )
2686                            };
2687                            if rc != 0 {
2688                                return Err(format!("memra_fa3_vl rc={rc}").into());
2689                            }
2690                        } else {
2691                            let fargs: Vec<crate::FaSeqVl> = (0..b)
2692                                .map(|s| crate::FaSeqVl {
2693                                    q: e.addr_f32(&aps[s].qn),
2694                                    k16: e.addr_u8(&mirrors[s].0),
2695                                    v16: e.addr_u8(&mirrors[s].1),
2696                                    o: e.addr_f32(&attns[s]),
2697                                    kf: e.addr_f32(&aps[s].kn),
2698                                    vf: e.addr_f32v(
2699                                        &g3[2].slice(offs[s] * vf_w..(offs[s] + ts[s]) * vf_w),
2700                                    ),
2701                                    t: ts[s] as i32,
2702                                    pad: 0,
2703                                })
2704                                .collect();
2705                            e.fa_prefill_vl8(&fargs, head_dim, n_head, n_head_kv, fa_scale)?;
2706                        }
2707                        for (s, attn) in attns.into_iter().enumerate() {
2708                            let (attn_g, ag16) = self.full_attn_prime_post_fa(
2709                                e,
2710                                attn,
2711                                &aps[s].gate,
2712                                ts[s],
2713                                n_head,
2714                                head_dim,
2715                            )?;
2716                            let mut done = false;
2717                            if let Some(xh) = &ag16 {
2718                                done = e.try_f16_gemm_pre_into_off(
2719                                    &fa.wo,
2720                                    xh,
2721                                    ts[s],
2722                                    &mut mixed,
2723                                    offs[s] * n_embd,
2724                                )?;
2725                            }
2726                            if !done {
2727                                let m = e.matmul(&fa.wo, &attn_g, ts[s])?;
2728                                e.copy_into(&mut mixed, offs[s] * n_embd, &m, ts[s] * n_embd)?;
2729                            }
2730                        }
2731                    } else {
2732                        let mut parts: Vec<Vec<CudaSlice<f32>>> =
2733                            (0..b).map(|_| Vec::new()).collect();
2734                        for (w, y) in [&fa.wq, &fa.wk, &fa.wv].iter().zip(g3) {
2735                            for (s, ys) in split(e, &y, w.out_features())?.into_iter().enumerate() {
2736                                parts[s].push(ys);
2737                            }
2738                        }
2739                        for (s, g3s) in parts.into_iter().enumerate() {
2740                            // task #16 gather removal: wo writes into `mixed` at offs[s] directly.
2741                            let (attn_g, ag16) = self.full_attn_prime_core_inner(
2742                                e, fa, g3s, &pos_ds[s], ts[s], caches[s], il,
2743                            )?;
2744                            let mut done = false;
2745                            if let Some(xh) = &ag16 {
2746                                done = e.try_f16_gemm_pre_into_off(
2747                                    &fa.wo,
2748                                    xh,
2749                                    ts[s],
2750                                    &mut mixed,
2751                                    offs[s] * n_embd,
2752                                )?;
2753                            }
2754                            if !done {
2755                                let m = e.matmul(&fa.wo, &attn_g, ts[s])?;
2756                                e.copy_into(&mut mixed, offs[s] * n_embd, &m, ts[s] * n_embd)?;
2757                            }
2758                        }
2759                    }
2760                }
2761                Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
2762                Mixer::Linear(la) => {
2763                    // task #16: NO split copies (cores read row-offset views of the concat
2764                    // outputs; out-GEMMs write into `mixed` at offs[s]). task #18: the core
2765                    // itself is BATCHED — per-seq prep/K1-K3, then ONE varlen K4 + ONE
2766                    // varlen K5 launch for all sequences.
2767                    let ws = [&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha];
2768                    let g4 = e.matmul_group_xh(&ws, &h, &hx16, total)?;
2769                    let outs =
2770                        self.linear_attn_prime_core_batch(e, la, &g4, &offs, &ts, caches, il)?;
2771                    for (s, (gn, gn16)) in outs.into_iter().enumerate() {
2772                        let (o, t) = (offs[s], ts[s]);
2773                        let mut done = false;
2774                        if let Some(xh) = &gn16 {
2775                            done = e.try_f16_gemm_pre_into_off(
2776                                &la.ssm_out,
2777                                xh,
2778                                t,
2779                                &mut mixed,
2780                                o * n_embd,
2781                            )?;
2782                        }
2783                        if !done {
2784                            let m = e.matmul(&la.ssm_out, &gn, t)?;
2785                            e.copy_into(&mut mixed, o * n_embd, &m, t * n_embd)?;
2786                        }
2787                    }
2788                }
2789            }
2790            let mut x1 = e.uninit(total * n_embd)?;
2791            let mut z = e.uninit(total * n_embd)?;
2792            let mut zx16 = e.alloc_u8_uninit(total * n_embd * 2)?;
2793            e.add_rms_norm_f16out(
2794                &x,
2795                &mixed,
2796                layer.post_attn_norm.float_data(),
2797                &mut x1,
2798                &mut z,
2799                &mut zx16,
2800                n_embd,
2801                total,
2802                eps,
2803            )?;
2804            let ffn_out = match &layer.ffn {
2805                crate::hybrid::Ffn::Dense {
2806                    ffn_gate,
2807                    ffn_up,
2808                    ffn_down,
2809                } => {
2810                    let n_ff = ffn_gate.out_features();
2811                    let mut g2 = e.matmul_group_xh(&[ffn_gate, ffn_up], &z, &zx16, total)?;
2812                    let up = g2.pop().unwrap();
2813                    let gate = g2.pop().unwrap();
2814                    let mut act = e.uninit(total * n_ff)?;
2815                    // task #17 (batch trunk): silu twin emits the down GEMM's fp16 operand
2816                    // in-epilogue (nsys round-26: this trunk still paid 32 cvt passes).
2817                    // A clamped layer must skip the plain-SiLU twin (see prime_chunk's note).
2818                    let d_lim = self.cfg.clamp_shexp_at(il as u32);
2819                    if Self::f16out_on(e, total) && self.cfg.m3.is_none() && d_lim.is_none() {
2820                        let mut a16 = e.alloc_u8_uninit(total * n_ff * 2)?;
2821                        e.silu_mul_f16out(&gate, &up, &mut act, &mut a16, total * n_ff)?;
2822                        match e.try_f16_gemm_pre(ffn_down, &a16, total)? {
2823                            Some(y) => y,
2824                            None => e.matmul(ffn_down, &act, total)?,
2825                        }
2826                    } else {
2827                        Self::ffn_act_lim(
2828                            e,
2829                            &self.cfg,
2830                            &gate,
2831                            &up,
2832                            1.0,
2833                            1.0,
2834                            d_lim,
2835                            &mut act,
2836                            total * n_ff,
2837                        )?;
2838                        e.matmul(ffn_down, &act, total)?
2839                    }
2840                }
2841                crate::hybrid::Ffn::Moe(m) => {
2842                    self.moe_ffn_il_prefill(e, m, &z, total, il as u16)?
2843                }
2844            };
2845            let mut x2 = e.uninit(total * n_embd)?;
2846            e.add(&x1, &ffn_out, &mut x2, total * n_embd)?;
2847            x = x2;
2848        }
2849        // epilogue per seq (identical math to prime_chunk: norm all rows, lm_head on last row)
2850        let mut hn = e.uninit(total * n_embd)?;
2851        e.rms_norm(
2852            &x,
2853            self.output_norm.float_data(),
2854            &mut hn,
2855            n_embd,
2856            total,
2857            eps,
2858        )?;
2859        // batched lm_head (nsys round-26: B sequential m=1 matvecs re-read the 600MB
2860        // lm_head weight B times — 2.2ms at B=6): gather the B last rows and run ONE
2861        // m=B GEMM (f16 lane; falls back to the per-seq matvec when no mirror). The
2862        // f16-vs-mmvq logits delta is the prefill GEMM numeric class — prime_batch_gate's
2863        // argmax battery arbitrates, same as every other prefill GEMM change.
2864        let mut hcat = e.uninit(b * n_embd)?;
2865        for s in 0..b {
2866            let last0 = (offs[s] + ts[s] - 1) * n_embd;
2867            e.copy_view_into(
2868                &mut hcat,
2869                s * n_embd,
2870                &hn.slice(last0..last0 + n_embd),
2871                n_embd,
2872            )?;
2873        }
2874        let logits_cat = if b >= 2 {
2875            e.try_f16_gemm(&self.output, &hcat, b)?
2876        } else {
2877            None
2878        };
2879        let logits_host: Option<Vec<f32>> = match &logits_cat {
2880            Some(lc) => Some(e.dtoh(lc)?),
2881            None => None,
2882        };
2883        let n_vocab = self.output.out_features();
2884        let mut hidden_all = if crate::spec::spec_hpost() {
2885            split(e, &hn, n_embd)?
2886        } else {
2887            split(e, &x, n_embd)?
2888        };
2889        let mut out = Vec::with_capacity(b);
2890        for s in 0..b {
2891            let last0 = (offs[s] + ts[s] - 1) * n_embd;
2892            let mut h_seed = e.uninit(n_embd)?;
2893            if !crate::spec::spec_hpost() {
2894                e.copy_view_into(&mut h_seed, 0, &x.slice(last0..last0 + n_embd), n_embd)?;
2895            } else {
2896                e.copy_view_into(&mut h_seed, 0, &hn.slice(last0..last0 + n_embd), n_embd)?;
2897            }
2898            let logits = match &logits_host {
2899                Some(lh) => lh[s * n_vocab..(s + 1) * n_vocab].to_vec(),
2900                None => {
2901                    let mut hlast = e.uninit(n_embd)?;
2902                    e.copy_view_into(&mut hlast, 0, &hn.slice(last0..last0 + n_embd), n_embd)?;
2903                    e.dtoh(&e.matmul(&self.output, &hlast, 1)?)?
2904                }
2905            };
2906            caches[s].pos += ts[s];
2907            out.push((logits, h_seed, hidden_all.remove(0)));
2908        }
2909        Ok(out)
2910    }
2911
2912    /// `full_attn` (batched prefill mixer) + the cache side-effect: append the T post-RoPE K/V
2913    /// rows into the resident quantized KV cache (q8_0 K / q5_1 V) and advance len/len_d. Row
2914    /// bytes are BIT-IDENTICAL to the decode append (same per-warp quant kernel per row; the
2915    /// batched `append_kv_quantized_rows` runs that exact warp math on a (block, token) grid).
2916    /// The attention itself is unchanged prefill math (fa_prefill over the f32 K/V).
2917    ///
2918    /// `seq_end` = the ABSOLUTE end position of the WHOLE prime request (`cache.pos + prompt_len`
2919    /// measured BEFORE the chunk loop starts), i.e. a chunk-size-invariant property of the request.
2920    /// Only step35's SWA arm reads it (`step35_attn_pre_wo`'s doc note explains why keying on the
2921    /// chunk's own `t_kv` made the output depend on MEMRA_PRIME_CHUNK); every other arch ignores it.
2922    #[allow(clippy::too_many_arguments)]
2923    fn full_attn_prime(
2924        &self,
2925        e: &Engine,
2926        fa: &FullAttnLayer,
2927        h: &CudaSlice<f32>,
2928        hx: Option<&CudaSlice<u8>>,
2929        pos_d: &CudaSlice<i32>,
2930        t: usize,
2931        cache: &mut Cache,
2932        il: usize,
2933        seq_end: usize,
2934    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2935        if self.cfg.step35.is_some() {
2936            return self.step35_attn_prime(e, fa, h, hx, pos_d, t, cache, il, seq_end);
2937        }
2938        // PROJ/CORE SPLIT (task #13, 2026-07-26): the q/k/v group projection is hoisted so
2939        // the cross-request batch driver can run it at m = sum_T over concatenated tokens;
2940        // this single-seq path composes proj+core identically (byte-for-byte the old body).
2941        // task #14: `hx` = the norm-fused fp16 twin of `h` (skips the convert launch).
2942        let g3 = match hx {
2943            Some(xh) => e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv], h, xh, t)?,
2944            None => e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?,
2945        };
2946        self.full_attn_prime_core(e, fa, g3, pos_d, t, cache, il)
2947    }
2948
2949    /// Everything after the q/k/v projections (split/QK-norm/RoPE/FA/gate/append + wo).
2950    /// `g3` = matmul_group([wq, wk, wv]) output rows for THIS sequence.
2951    /// Composes inner + wo (== the pre-split core; every existing caller unchanged).
2952    fn full_attn_prime_core(
2953        &self,
2954        e: &Engine,
2955        fa: &FullAttnLayer,
2956        g3: Vec<CudaSlice<f32>>,
2957        pos_d: &CudaSlice<i32>,
2958        t: usize,
2959        cache: &mut Cache,
2960        il: usize,
2961    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2962        let (attn_g, ag16) = self.full_attn_prime_core_inner(e, fa, g3, pos_d, t, cache, il)?;
2963        if let Some(xh) = &ag16 {
2964            if let Some(y) = e.try_f16_gemm_pre(&fa.wo, xh, t)? {
2965                return Ok(y);
2966            }
2967        }
2968        Ok(e.matmul(&fa.wo, &attn_g, t)?)
2969    }
2970
2971    fn full_attn_prime_core_inner(
2972        &self,
2973        e: &Engine,
2974        fa: &FullAttnLayer,
2975        g3: Vec<CudaSlice<f32>>,
2976        pos_d: &CudaSlice<i32>,
2977        t: usize,
2978        cache: &mut Cache,
2979        il: usize,
2980    ) -> Result<(CudaSlice<f32>, Option<CudaSlice<u8>>), Box<dyn std::error::Error>> {
2981        let cfg = &self.cfg;
2982        let geometry = cfg.full_attention_geometry_at(il as u32);
2983        let n_head = geometry.n_head as usize;
2984        let n_head_kv = geometry.n_head_kv as usize;
2985        let head_dim = geometry.head_dim_k as usize;
2986        let scale = geometry.attention_scale();
2987        let (pre, base_len) = self.full_attn_prime_pre_fa(e, fa, g3, pos_d, t, cache, il)?;
2988        let AttnPre { q, k, v, gate } = pre;
2989        let mut attn = e.uninit(t * n_head * head_dim)?;
2990        self.full_attn_prime_fa_dispatch(
2991            e, &q, &k, &v, &mut attn, base_len, t, cache, il, head_dim, n_head, n_head_kv, scale,
2992        )?;
2993        self.full_attn_prime_post_fa(e, attn, &gate, t, n_head, head_dim)
2994    }
2995
2996    /// task #18 (attn side): projections tail through KV append — everything before the
2997    /// attention kernel. Returns the post-rope q/k, v, optional out-gate, and the KV rows
2998    /// present BEFORE this chunk's append (base_len; 0 == fresh).
2999    #[allow(clippy::type_complexity)]
3000    fn full_attn_prime_pre_fa(
3001        &self,
3002        e: &Engine,
3003        fa: &FullAttnLayer,
3004        mut g3: Vec<CudaSlice<f32>>,
3005        pos_d: &CudaSlice<i32>,
3006        t: usize,
3007        cache: &mut Cache,
3008        il: usize,
3009    ) -> Result<(AttnPre, usize), Box<dyn std::error::Error>> {
3010        let cfg = &self.cfg;
3011        let geometry = cfg.full_attention_geometry_at(il as u32);
3012        let n_head = geometry.n_head as usize;
3013        let n_head_kv = geometry.n_head_kv as usize;
3014        let head_dim = geometry.head_dim_k as usize;
3015        let eps = cfg.rms_eps;
3016
3017        // qwen35 fuses [q|gate] per head in wq (2*head_dim stride); M3/Hy3 have NO output gate
3018        // (attention_output_gate=false) — wq out = n_head*head_dim exactly, and q_gate_split
3019        // would read 2x out of bounds. `gated` keys both the split and the sigmoid epilogue.
3020        let gated = geometry.attention_gate == memra_gguf::config::AttentionGateKind::FusedQ;
3021        let v = g3.pop().unwrap();
3022        let mut k = g3.pop().unwrap();
3023        let qf = g3.pop().unwrap();
3024        let (mut q, gate) = if gated {
3025            let mut q = e.uninit(t * n_head * head_dim)?;
3026            let mut gate = e.uninit(t * n_head * head_dim)?;
3027            e.q_gate_split(&qf, &mut q, &mut gate, head_dim, n_head, t)?;
3028            (q, Some(gate))
3029        } else {
3030            (qf, None)
3031        };
3032
3033        let mut qn = e.uninit(t * n_head * head_dim)?;
3034        e.rms_norm(
3035            &q,
3036            fa.q_norm.float_data(),
3037            &mut qn,
3038            head_dim,
3039            n_head * t,
3040            eps,
3041        )?;
3042        q = qn;
3043        let mut kn = e.uninit(t * n_head_kv * head_dim)?;
3044        e.rms_norm(
3045            &k,
3046            fa.k_norm.float_data(),
3047            &mut kn,
3048            head_dim,
3049            n_head_kv * t,
3050            eps,
3051        )?;
3052        k = kn;
3053        let rope_dims = geometry.n_rot as usize;
3054        e.rope_neox(
3055            &mut q,
3056            pos_d,
3057            head_dim,
3058            rope_dims,
3059            n_head,
3060            t,
3061            geometry.rope_base,
3062            1.0,
3063        )?;
3064        e.rope_neox(
3065            &mut k,
3066            pos_d,
3067            head_dim,
3068            rope_dims,
3069            n_head_kv,
3070            t,
3071            geometry.rope_base,
3072            1.0,
3073        )?;
3074
3075        // CACHE SIDE-EFFECT: append the T post-rope K/V token rows (token-major [T, kv_dim] ==
3076        // the cache row layout) quantized into cache.kv[il], then advance len + device len_d.
3077        {
3078            let kvl = cache.kv[il].as_mut().unwrap();
3079            assert!(kvl.len + t <= cache.max_ctx, "prime_cache: KV overflow");
3080            e.append_kv_quantized_rows(
3081                &k,
3082                &v,
3083                &mut kvl.k,
3084                &mut kvl.v,
3085                kvl.len,
3086                t,
3087                kvl.kv_dim_k,
3088                kvl.kv_dim_v,
3089                kvl.k_tok_bytes,
3090                kvl.v_tok_bytes,
3091                crate::Engine::kv_fp8_on(),
3092            )?;
3093            kvl.len += t;
3094            let new_len = kvl.len as i32;
3095            e.set_i32_one(&mut kvl.len_d, new_len)?;
3096        }
3097
3098        let base_len = {
3099            let kvl = cache.kv[il].as_ref().unwrap();
3100            kvl.len - t // KV rows present BEFORE this chunk's append above
3101        };
3102        Ok((AttnPre { q, k, v, gate }, base_len))
3103    }
3104
3105    /// batched prefill attention. FRESH prime (no past KV): unchanged forward_last math over
3106    /// the f32 K/V of this batch. CONTINUATION chunk (past KV present): the chunk's queries
3107    /// must attend to [0 .. base+t) — run fa_prefill_view over the resident QUANTIZED cache
3108    /// (the spec-verify pattern; kernel's causal mask offsets by T_kv-T). Numerically this
3109    /// reads q8_0/q5_1-dequantized K/V for the past AND the current chunk — the same class as
3110    /// decode reading the cache; the run-gen/first-16 battery is the accuracy authority.
3111    #[allow(clippy::too_many_arguments)]
3112    fn full_attn_prime_fa_dispatch(
3113        &self,
3114        e: &Engine,
3115        q: &CudaSlice<f32>,
3116        k: &CudaSlice<f32>,
3117        v: &CudaSlice<f32>,
3118        attn: &mut CudaSlice<f32>,
3119        base_len: usize,
3120        t: usize,
3121        cache: &mut Cache,
3122        il: usize,
3123        head_dim: usize,
3124        n_head: usize,
3125        n_head_kv: usize,
3126        scale: f32,
3127    ) -> Result<(), Box<dyn std::error::Error>> {
3128        // GRAIN-FREE CHUNK-INVARIANCE FIX (lane/chunkinv-flip, 2026-08-05): the base_len == 0
3129        // f32 special case (fa_prefill over this batch's f32 K/V) is DROPPED. pre_fa appends
3130        // the chunk's quantized rows into cache.kv[il] BEFORE this dispatch, so chunk 0 can
3131        // attend through the quantized cache exactly like every later chunk (quantize-then-
3132        // attend). One numeric class for every row => the chunk size cannot decide where a
3133        // precision edge falls, so chunked prefill is reduction-order-stable with NO grain
3134        // knob (the stronger fix VERDICT.md filed; supersedes the MEMRA_PRIME_INVARIANT door's
3135        // pin-the-boundary approach).
3136        // MEMRA_PRIME_F32CHUNK0=1 is the ROLLBACK SEAM to the old arithmetic (chunk 0 attends
3137        // f32 K/V) — flags-doctrine rollback door AND the chunkinv gate's canary injection:
3138        // with the fix unconditional, only re-introducing the class edge can prove the gate
3139        // still detects the mechanism. Never on in a measured default run.
3140        if base_len == 0 && std::env::var("MEMRA_PRIME_F32CHUNK0").as_deref() == Ok("1") {
3141            if std::env::var("MEMRA_NOFA").is_ok() || !(head_dim == 256 || head_dim == 128) {
3142                e.sdpa_naive(
3143                    q, k, v, attn, head_dim, n_head, n_head_kv, t, t, scale, true,
3144                )?;
3145            } else {
3146                e.fa_prefill(
3147                    q, k, v, attn, head_dim, n_head, n_head_kv, t, t, scale, true,
3148                )?;
3149            }
3150            return Ok(());
3151        }
3152        let kvl = cache.kv[il].as_ref().unwrap();
3153        let t_kv = base_len + t;
3154        let k_view = e.view_u8(&kvl.k, t_kv * kvl.k_tok_bytes);
3155        let v_view = e.view_u8(&kvl.v, t_kv * kvl.v_tok_bytes);
3156        // fa_prefill_q/_qw twins are stamped for head_dim 256 (qwen35) and 128 (M3) only;
3157        // other dims (and MEMRA_NOFA) take the naive quantized-view SDPA — SAME cache bytes,
3158        // same numeric class, so the uniform contract holds on the fallback too.
3159        if std::env::var("MEMRA_NOFA").is_ok() || !(head_dim == 256 || head_dim == 128) {
3160            e.sdpa_naive_quantized_view(
3161                q,
3162                &k_view,
3163                &v_view,
3164                attn,
3165                head_dim,
3166                n_head,
3167                n_head_kv,
3168                t,
3169                t_kv,
3170                scale,
3171                true,
3172                kvl.k_tok_bytes,
3173                kvl.v_tok_bytes,
3174            )?;
3175            return Ok(());
3176        }
3177        // ARC B (2026-07-05): dequant-once workspace, DEFAULT ON. fa_prefill_q's inline
3178        // dequant re-reads+re-dequants the whole quantized KV stream from every one of the
3179        // T/64 x n_head CTAs (64x+ redundant at chunk=4096; 30.5% of the 32k prime wall).
3180        // fa_prefill_view_ws dequants K/V ONCE into a resident bf16 workspace then runs the
3181        // bit-identical bf16 twin (fa_prefill_qw) — same staged values, same FP order, token-
3182        // identical output (gate: MEMRA_PRIME_CHUNK=4096 ws-on vs ws-off vs monolithic).
3183        // MEMRA_PRIME_DEQW=0 reverts to the inline-dequant kernel.
3184        let deqw = std::env::var("MEMRA_PRIME_DEQW")
3185            .map(|v| v != "0")
3186            .unwrap_or(true);
3187        if deqw {
3188            e.fa_prefill_view_ws(
3189                q,
3190                &k_view,
3191                &v_view,
3192                attn,
3193                head_dim,
3194                n_head,
3195                n_head_kv,
3196                t,
3197                t_kv,
3198                scale,
3199                true,
3200                kvl.k_tok_bytes,
3201                kvl.v_tok_bytes,
3202                crate::Engine::kv_fp8_on(),
3203            )?;
3204        } else {
3205            e.fa_prefill_view(
3206                q,
3207                &k_view,
3208                &v_view,
3209                attn,
3210                head_dim,
3211                n_head,
3212                n_head_kv,
3213                t,
3214                t_kv,
3215                scale,
3216                true,
3217                kvl.k_tok_bytes,
3218                kvl.v_tok_bytes,
3219                crate::Engine::kv_fp8_on(),
3220            )?;
3221        }
3222        Ok(())
3223    }
3224
3225    /// task #17: sig_mul_f16out fuses [sigmoid + mul + f16 convert] into one launch
3226    /// (bit-identical composition) and hands wo its fp16 operand directly.
3227    fn full_attn_prime_post_fa(
3228        &self,
3229        e: &Engine,
3230        attn: CudaSlice<f32>,
3231        gate: &Option<CudaSlice<f32>>,
3232        t: usize,
3233        n_head: usize,
3234        head_dim: usize,
3235    ) -> Result<(CudaSlice<f32>, Option<CudaSlice<u8>>), Box<dyn std::error::Error>> {
3236        let (attn_g, ag16) = match gate {
3237            Some(gate) => {
3238                let n = t * n_head * head_dim;
3239                let mut ag = e.uninit(n)?;
3240                if Self::f16out_on(e, t) {
3241                    let mut a16 = e.alloc_u8_uninit(n * 2)?;
3242                    e.sig_mul_f16out(&attn, gate, &mut ag, &mut a16, n)?;
3243                    (ag, Some(a16))
3244                } else {
3245                    let mut gsig = e.uninit(n)?;
3246                    e.sigmoid(gate, &mut gsig, n)?;
3247                    e.mul(&attn, &gsig, &mut ag, n)?;
3248                    (ag, None)
3249                }
3250            }
3251            None => (attn, None),
3252        };
3253        Ok((attn_g, ag16))
3254    }
3255
3256    /// STATEFUL batched linear-attention prime: `linear_attn`'s prefill-dispatch pass (normal
3257    /// `e.matmul` — GEMM at m>=16 — plus the prefill repack/L2/glog kernels) but with the state
3258    /// carried THROUGH the cache like the spec verify does: carried-ring conv
3259    /// (ssm_conv1d_tm_state writes the final ring back) + ONE gdn_scan from cache.recur[il]'s
3260    /// current state (zero at a fresh prime) whose final state ping-pongs back into the cache.
3261    /// Wiring mirrors `linear_attn_verify_t` (spec.rs); dispatch mirrors `linear_attn` (prefill).
3262    fn linear_attn_prime(
3263        &self,
3264        e: &Engine,
3265        la: &LinearAttnLayer,
3266        h: &CudaSlice<f32>,
3267        hx: Option<&CudaSlice<u8>>,
3268        t: usize,
3269        cache: &mut Cache,
3270        il: usize,
3271    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3272        // PROJ/CORE SPLIT (task #13): see full_attn_prime — same hoist for the GDN 4-tuple.
3273        let ws = [&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha];
3274        let g4 = match hx {
3275            Some(xh) => e.matmul_group_xh(&ws, h, xh, t)?,
3276            None => e.matmul_group(&ws, h, t)?,
3277        };
3278        self.linear_attn_prime_core(e, la, g4, t, cache, il)
3279    }
3280
3281    /// Everything after the GDN 4-tuple projections (conv/chunk stack/gated norm + ssm_out).
3282    fn linear_attn_prime_core(
3283        &self,
3284        e: &Engine,
3285        la: &LinearAttnLayer,
3286        mut g4: Vec<CudaSlice<f32>>,
3287        t: usize,
3288        cache: &mut Cache,
3289        il: usize,
3290    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3291        self.linear_attn_prime_core_pad(e, la, g4.drain(..).collect(), t, cache, il, None)
3292    }
3293
3294    /// task #14: `pad_len` = device true length for PADDED prime graphs. Pads become
3295    /// identity GDN steps (gdn_pad_mask zeroes beta/g_log past the true length) and the
3296    /// conv ring writes back from the true tail. None = classic path, byte-identical.
3297    #[allow(clippy::too_many_arguments)]
3298    fn linear_attn_prime_core_pad_inner(
3299        &self,
3300        e: &Engine,
3301        la: &LinearAttnLayer,
3302        mut g4: Vec<CudaSlice<f32>>,
3303        t: usize,
3304        cache: &mut Cache,
3305        il: usize,
3306        pad_len: Option<&CudaSlice<i32>>,
3307    ) -> Result<(CudaSlice<f32>, Option<CudaSlice<u8>>), Box<dyn std::error::Error>> {
3308        // shim over the view twin (task #16): full-range views of the owned buffers.
3309        let ssm = self.cfg.ssm.as_ref().unwrap();
3310        let d_state = ssm.state_size as usize;
3311        let num_k = ssm.group_count as usize;
3312        let num_v = ssm.time_step_rank as usize;
3313        let key_dim = d_state * num_k;
3314        let value_dim = d_state * num_v;
3315        let conv_dim = key_dim * 2 + value_dim;
3316        let alpha = g4.pop().unwrap(); // [T, num_v]
3317        let beta_raw = g4.pop().unwrap(); // [T, num_v]
3318        let z = g4.pop().unwrap(); // [T, value_dim]
3319        let qkv_mixed = g4.pop().unwrap(); // [T, conv_dim] token-major
3320        self.linear_attn_prime_core_pad_view(
3321            e,
3322            la,
3323            &qkv_mixed.slice(0..t * conv_dim),
3324            &z.slice(0..t * value_dim),
3325            &beta_raw.slice(0..t * num_v),
3326            &alpha.slice(0..t * num_v),
3327            t,
3328            cache,
3329            il,
3330            pad_len,
3331        )
3332    }
3333
3334    /// task #18: the GDN prep stage (conv + repack + l2 x2 + sigmoid + glog + pad-mask) —
3335    /// shared verbatim by the per-seq scan path and the varlen batched path.
3336    #[allow(clippy::too_many_arguments)]
3337    fn linear_attn_gdn_prep(
3338        &self,
3339        e: &Engine,
3340        la: &LinearAttnLayer,
3341        qkv_mixed: &cudarc::driver::CudaView<f32>,
3342        beta_raw: &cudarc::driver::CudaView<f32>,
3343        alpha: &cudarc::driver::CudaView<f32>,
3344        t: usize,
3345        cache: &mut Cache,
3346        il: usize,
3347        pad_len: Option<&CudaSlice<i32>>,
3348    ) -> Result<GdnPrep, Box<dyn std::error::Error>> {
3349        let cfg = &self.cfg;
3350        let ssm = cfg.ssm.as_ref().unwrap();
3351        let d_state = ssm.state_size as usize; // 128
3352        let num_k = ssm.group_count as usize; // 16
3353        let num_v = ssm.time_step_rank as usize; // 32
3354        let d_conv = ssm.conv_kernel as usize; // 4
3355        let key_dim = d_state * num_k; // 2048
3356        let value_dim = d_state * num_v; // 4096
3357        let conv_dim = key_dim * 2 + value_dim; // 8192
3358        let eps = cfg.rms_eps;
3359        debug_assert!(
3360            t >= d_conv - 1,
3361            "stateful conv needs T >= pad (PRIME_MIN_T gates)"
3362        );
3363
3364        // conv with CARRIED ring state + ring roll (state read + final-window write-back).
3365        // task #18 conv-fuse (default ON): conv + SiLU + repack in one pass — the 67MB
3366        // conv_out intermediate and its transposed re-read disappear (values bit-identical).
3367        // MEMRA_CONV_FUSE=0 reverts to the two-kernel chain.
3368        let rl = cache.recur[il].as_mut().unwrap();
3369        let hk = Self::gdn_hk(e, t, num_v, num_k);
3370        let conv_fuse = std::env::var("MEMRA_CONV_FUSE").as_deref() != Ok("0");
3371        let hk = if conv_fuse { hk } else { num_v }; // de-broadcast rides the fused conv
3372        let mut q_g = e.uninit(d_state * hk * t)?;
3373        let mut k_g = e.uninit(d_state * hk * t)?;
3374        let mut v_g = e.uninit(d_state * num_v * t)?;
3375        if conv_fuse {
3376            e.ssm_conv1d_gdn_state_pad(
3377                qkv_mixed,
3378                &mut rl.conv_state,
3379                la.ssm_conv1d.float_data(),
3380                &mut q_g,
3381                &mut k_g,
3382                &mut v_g,
3383                conv_dim,
3384                t,
3385                d_conv,
3386                d_state,
3387                num_v,
3388                num_k,
3389                key_dim,
3390                hk,
3391                pad_len,
3392            )?;
3393        } else {
3394            let mut conv_out = e.uninit(conv_dim * t)?; // [conv_dim, T] channel-major, SiLU
3395            e.ssm_conv1d_tm_state_pad_v(
3396                qkv_mixed,
3397                &mut rl.conv_state,
3398                la.ssm_conv1d.float_data(),
3399                &mut conv_out,
3400                conv_dim,
3401                t,
3402                d_conv,
3403                pad_len,
3404            )?;
3405            e.qkv_to_gdn_repack(
3406                &conv_out, &mut q_g, &mut k_g, &mut v_g, d_state, num_v, num_k, key_dim, t,
3407            )?;
3408        }
3409        let mut q_l2 = e.uninit(d_state * hk * t)?;
3410        // mirror-fold (round 35): q's bf16 twin (wgmma K45/K2 A-operand) in-epilogue too.
3411        // Emitted only where a consumer exists (the wgmma config) — on other arches the
3412        // alloc + epilogue stores would be pure waste.
3413        let qb16 = if Engine::l2_v2_on(d_state) && e.gdn_wgmma_on(32) {
3414            let mut qb = e.alloc_u8_uninit(d_state * hk * t * 2)?;
3415            e.l2_norm_pp(&q_g, &mut q_l2, Some(&mut qb), d_state, hk * t, eps)?;
3416            Some(qb)
3417        } else {
3418            e.l2_norm_pp(&q_g, &mut q_l2, None, d_state, hk * t, eps)?;
3419            None
3420        };
3421        let mut k_l2 = e.uninit(d_state * hk * t)?;
3422        // mirror-fold: emit k's bf16 twin (the chunked-scan kb16 mirror) in-epilogue
3423        let kb16 = if Engine::l2_v2_on(d_state) {
3424            let mut kb = e.alloc_u8_uninit(d_state * hk * t * 2)?;
3425            e.l2_norm_pp(&k_g, &mut k_l2, Some(&mut kb), d_state, hk * t, eps)?;
3426            Some(kb)
3427        } else {
3428            e.l2_norm_pp(&k_g, &mut k_l2, None, d_state, hk * t, eps)?;
3429            None
3430        };
3431        let mut beta = e.uninit(t * num_v)?;
3432        e.sigmoid_v(beta_raw, &mut beta, t * num_v)?;
3433        let mut g_log = e.uninit(t * num_v)?;
3434        e.gdn_glog_v(
3435            alpha,
3436            la.ssm_dt.float_data(),
3437            la.ssm_a.float_data(),
3438            &mut g_log,
3439            num_v,
3440            t,
3441        )?;
3442        if let Some(len_d) = pad_len {
3443            e.gdn_pad_mask(&mut beta, &mut g_log, len_d, num_v, t)?;
3444        }
3445        Ok(GdnPrep {
3446            hk,
3447            q_l2,
3448            k_l2,
3449            v_g,
3450            beta,
3451            g_log,
3452            kb16,
3453            qb16,
3454        })
3455    }
3456
3457    /// task #18: batched GDN mixer core — per-seq prep + K1-K3, then ONE varlen K4 and
3458    /// ONE varlen K5 launch for all B sequences (full-machine occupancy vs B underfilled
3459    /// per-seq trains). Per-block math identical to the per-seq path (bit-gateable);
3460    /// MEMRA_GDN_VL=0 or a non-mma/non-C32 config falls back to the per-seq loop.
3461    #[allow(clippy::too_many_arguments)]
3462    fn linear_attn_prime_core_batch(
3463        &self,
3464        e: &Engine,
3465        la: &LinearAttnLayer,
3466        g4: &[CudaSlice<f32>],
3467        offs: &[usize],
3468        ts: &[usize],
3469        caches: &mut [&mut Cache],
3470        il: usize,
3471    ) -> Result<Vec<(CudaSlice<f32>, Option<CudaSlice<u8>>)>, Box<dyn std::error::Error>> {
3472        let ssm = self.cfg.ssm.as_ref().unwrap();
3473        let d_state = ssm.state_size as usize;
3474        let num_k = ssm.group_count as usize;
3475        let num_v = ssm.time_step_rank as usize;
3476        let key_dim = d_state * num_k;
3477        let value_dim = d_state * num_v;
3478        let conv_dim = key_dim * 2 + value_dim;
3479        let eps = self.cfg.rms_eps;
3480        let scale = 1.0 / (d_state as f32).sqrt();
3481        let b = ts.len();
3482        let c = Engine::gdn_chunk_size();
3483        // CONTINUATION batches (increment (b)) take the per-seq stateful pad_view twin
3484        // below — the varlen K4/K5 chain is fresh-only (zero initial state assumed).
3485        let carried = caches.iter().any(|c| c.pos > 0);
3486        let use_vl = !carried
3487            && (2..=8).contains(&b)
3488            && Engine::gdn_chunked_enabled()
3489            && ts.iter().all(|&t| t >= 16)
3490            && e.gdn_mma_enabled(c)
3491            && std::env::var("MEMRA_GDN_VL").as_deref() != Ok("0");
3492        if !use_vl {
3493            return (0..b)
3494                .map(|s| {
3495                    let (o, t) = (offs[s], ts[s]);
3496                    self.linear_attn_prime_core_pad_view(
3497                        e,
3498                        la,
3499                        &g4[0].slice(o * conv_dim..(o + t) * conv_dim),
3500                        &g4[1].slice(o * value_dim..(o + t) * value_dim),
3501                        &g4[2].slice(o * num_v..(o + t) * num_v),
3502                        &g4[3].slice(o * num_v..(o + t) * num_v),
3503                        t,
3504                        caches[s],
3505                        il,
3506                        None,
3507                    )
3508                })
3509                .collect();
3510        }
3511        // increment 3: the ENTIRE core is varlen — allocs only per seq, then 13 launches
3512        // total for the whole batch: [conv, ring, repack, l2(q+k), gate-prep] +
3513        // [k-mirror, K1, K2, K3, w-mirror, K4, K5] + [gated-norm tail].
3514        struct SeqBufs {
3515            conv_out: CudaSlice<f32>,
3516            q_g: CudaSlice<f32>,
3517            k_g: CudaSlice<f32>,
3518            v_g: CudaSlice<f32>,
3519            q_l2: CudaSlice<f32>,
3520            k_l2: CudaSlice<f32>,
3521            beta: CudaSlice<f32>,
3522            g_log: CudaSlice<f32>,
3523            gn: CudaSlice<f32>,
3524            gn16: CudaSlice<u8>,
3525        }
3526        let d_conv = ssm.conv_kernel as usize;
3527        let f16o = Self::f16out_on(e, 16);
3528        let hk = Self::gdn_hk(e, 16, num_v, num_k); // vl path is always chunked+mma
3529        let mut sb = Vec::with_capacity(b);
3530        let mut pres = Vec::with_capacity(b);
3531        for &t in ts.iter().take(b) {
3532            sb.push(SeqBufs {
3533                conv_out: e.uninit(conv_dim * t)?,
3534                q_g: e.uninit(d_state * hk * t)?,
3535                k_g: e.uninit(d_state * hk * t)?,
3536                v_g: e.uninit(d_state * num_v * t)?,
3537                q_l2: e.uninit(d_state * hk * t)?,
3538                k_l2: e.uninit(d_state * hk * t)?,
3539                beta: e.uninit(t * num_v)?,
3540                g_log: e.uninit(t * num_v)?,
3541                gn: e.uninit(d_state * num_v * t)?,
3542                gn16: e.alloc_u8_uninit(d_state * num_v * t * 2)?,
3543            });
3544            pres.push(e.gdn_chunk_alloc(num_v, t, c, hk)?);
3545        }
3546        let prep_args: Vec<crate::GdnPrepVl> = (0..b)
3547            .map(|s| {
3548                let (o, t) = (offs[s], ts[s]);
3549                let rl = caches[s].recur[il].as_ref().unwrap();
3550                crate::GdnPrepVl {
3551                    qkv: e.addr_f32v(&g4[0].slice(o * conv_dim..(o + t) * conv_dim)),
3552                    conv_state: e.addr_f32(&rl.conv_state),
3553                    conv_out: e.addr_f32(&sb[s].conv_out),
3554                    q_g: e.addr_f32(&sb[s].q_g),
3555                    k_g: e.addr_f32(&sb[s].k_g),
3556                    v_g: e.addr_f32(&sb[s].v_g),
3557                    q_l2: e.addr_f32(&sb[s].q_l2),
3558                    k_l2: e.addr_f32(&sb[s].k_l2),
3559                    beta_raw: e.addr_f32v(&g4[2].slice(o * num_v..(o + t) * num_v)),
3560                    alpha: e.addr_f32v(&g4[3].slice(o * num_v..(o + t) * num_v)),
3561                    beta: e.addr_f32(&sb[s].beta),
3562                    g_log: e.addr_f32(&sb[s].g_log),
3563                    o: e.addr_f32(&pres[s].o),
3564                    z: e.addr_f32v(&g4[1].slice(o * value_dim..(o + t) * value_dim)),
3565                    gn: e.addr_f32(&sb[s].gn),
3566                    gn16: e.addr_u8(&sb[s].gn16),
3567                    kb16: if Engine::l2_v2_on(d_state) {
3568                        e.addr_u8(&pres[s].kb16)
3569                    } else {
3570                        0
3571                    },
3572                    qb16: if Engine::l2_v2_on(d_state) && e.gdn_wgmma_on(c) {
3573                        e.addr_u8(&pres[s].qb16)
3574                    } else {
3575                        0
3576                    },
3577                    t: t as i32,
3578                    pad: 0,
3579                }
3580            })
3581            .collect();
3582        let args: Vec<crate::GdnSeqVl> = (0..b)
3583            .map(|s| {
3584                let rl = caches[s].recur[il].as_ref().unwrap();
3585                crate::GdnSeqVl {
3586                    kb16: e.addr_u8(&pres[s].kb16),
3587                    gcum: e.addr_f32(&pres[s].gcum),
3588                    beta: e.addr_f32(&sb[s].beta),
3589                    u: e.addr_f32(&pres[s].u),
3590                    wb16: e.addr_u8(&pres[s].wb16),
3591                    y: e.addr_u8(&pres[s].y16),
3592                    ssnap: e.addr_u8(&pres[s].ssnap16),
3593                    state_in: e.addr_f32(&rl.ssm_state),
3594                    state_out: e.addr_f32(&rl.ssm_state_alt),
3595                    q: e.addr_f32(&sb[s].q_l2),
3596                    p: e.addr_f32(&pres[s].p),
3597                    o: e.addr_f32(&pres[s].o),
3598                    k: e.addr_f32(&sb[s].k_l2),
3599                    v: e.addr_f32(&sb[s].v_g),
3600                    g: e.addr_f32(&sb[s].g_log),
3601                    a: e.addr_f32(&pres[s].a),
3602                    w: e.addr_f32(&pres[s].w),
3603                    t: ts[s] as i32,
3604                    nc: pres[s].nc as i32,
3605                }
3606            })
3607            .collect();
3608        e.gdn_prep_vl8(
3609            &prep_args,
3610            la.ssm_conv1d.float_data(),
3611            la.ssm_dt.float_data(),
3612            la.ssm_a.float_data(),
3613            conv_dim,
3614            d_conv,
3615            d_state,
3616            num_v,
3617            num_k,
3618            key_dim,
3619            hk,
3620            eps,
3621        )?;
3622        // mirror-fold (round 27): l2 v2 emits kb16 in-epilogue; K3's store emits wb16 —
3623        // both standalone mirror launches vanish on the default config.
3624        if !Engine::l2_v2_on(d_state) {
3625            e.gdn_mirror_vl8(&args, num_v, 0, hk)?;
3626        }
3627        // task #22: wgmma-fused vl twins — qb16 mirrors + GdnWVl8 side-struct.
3628        let wq8: Option<crate::GdnWVl8> = if e.gdn_wgmma_on(c) {
3629            // qb16 emitted by the vl l2 mirror-fold when l2-v2 serves; bulk cvt otherwise
3630            if !Engine::l2_v2_on(d_state) {
3631                for s in 0..b {
3632                    e.f32_to_bf16_into(&sb[s].q_l2, &mut pres[s].qb16, d_state * hk * ts[s])?;
3633                }
3634            }
3635            let mut wa = [crate::GdnWVl::default(); 8];
3636            for s in 0..b {
3637                wa[s] = crate::GdnWVl {
3638                    qb16: e.addr_u8(&pres[s].qb16),
3639                    pb16: e.addr_u8(&pres[s].pb16),
3640                };
3641            }
3642            Some(crate::GdnWVl8(wa))
3643        } else {
3644            None
3645        };
3646        e.gdn_chunk_k123_vl8(&args, num_v, hk, wq8.as_ref())?;
3647        e.gdn_chunk_vl8(&args, num_v, scale, hk, wq8.as_ref())?;
3648        if f16o {
3649            e.gdn_tail_vl8(&prep_args, la.ssm_norm.float_data(), d_state, num_v, eps)?;
3650        }
3651        // per-seq state swap (+ non-f16out tail fallback)
3652        let mut out = Vec::with_capacity(b);
3653        for (s, bufs) in sb.into_iter().enumerate() {
3654            let rl = caches[s].recur[il].as_mut().unwrap();
3655            std::mem::swap(&mut rl.ssm_state, &mut rl.ssm_state_alt);
3656            let (o, t) = (offs[s], ts[s]);
3657            let SeqBufs { mut gn, gn16, .. } = bufs;
3658            if f16o {
3659                out.push((gn, Some(gn16)));
3660            } else {
3661                let z_v = g4[1].slice(o * value_dim..(o + t) * value_dim);
3662                e.gated_rmsnorm_zv(
3663                    &pres[s].o,
3664                    la.ssm_norm.float_data(),
3665                    &z_v,
3666                    &mut gn,
3667                    d_state,
3668                    num_v * t,
3669                    eps,
3670                )?;
3671                out.push((gn, None));
3672            }
3673        }
3674        Ok(out)
3675    }
3676
3677    /// task #16: view-consuming GDN prime core — the batched prime hands row-offset
3678    /// views of the CONCAT projection outputs directly (no per-seq split copies).
3679    /// Same kernels, same values, byte-identical to the Vec shim above.
3680    #[allow(clippy::too_many_arguments)]
3681    fn linear_attn_prime_core_pad_view(
3682        &self,
3683        e: &Engine,
3684        la: &LinearAttnLayer,
3685        qkv_mixed: &cudarc::driver::CudaView<f32>,
3686        z: &cudarc::driver::CudaView<f32>,
3687        beta_raw: &cudarc::driver::CudaView<f32>,
3688        alpha: &cudarc::driver::CudaView<f32>,
3689        t: usize,
3690        cache: &mut Cache,
3691        il: usize,
3692        pad_len: Option<&CudaSlice<i32>>,
3693    ) -> Result<(CudaSlice<f32>, Option<CudaSlice<u8>>), Box<dyn std::error::Error>> {
3694        let cfg = &self.cfg;
3695        let ssm = cfg.ssm.as_ref().unwrap();
3696        let d_state = ssm.state_size as usize; // 128
3697        let num_v = ssm.time_step_rank as usize; // 32
3698        let eps = cfg.rms_eps;
3699        let scale = 1.0 / (d_state as f32).sqrt();
3700
3701        let prep =
3702            self.linear_attn_gdn_prep(e, la, qkv_mixed, beta_raw, alpha, t, cache, il, pad_len)?;
3703
3704        // ONE gdn_scan over T from the cache's CURRENT state (zero at fresh prime); the final
3705        // state lands in the spare buffer and ping-pongs back (stable resident pointers, the
3706        // decode-determinism discipline from linear_attn_decode_inner). A4: `gdn_scan_prefill`
3707        // dispatches the chunked WY form under MEMRA_GDN_CHUNKED (prefill-only seam; decode +
3708        // verify keep the sequential kernel).
3709        let mut o = e.uninit(d_state * num_v * t)?;
3710        let rl = cache.recur[il].as_mut().unwrap();
3711        {
3712            let crate::cache::RecurLayer {
3713                ssm_state,
3714                ssm_state_alt,
3715                ..
3716            } = rl;
3717            e.gdn_scan_prefill(
3718                &prep.q_l2,
3719                &prep.k_l2,
3720                &prep.v_g,
3721                &prep.g_log,
3722                &prep.beta,
3723                prep.kb16.as_ref(),
3724                prep.qb16.as_ref(),
3725                ssm_state,
3726                ssm_state_alt,
3727                &mut o,
3728                num_v,
3729                t,
3730                scale,
3731                prep.hk,
3732            )?;
3733        }
3734        std::mem::swap(&mut rl.ssm_state, &mut rl.ssm_state_alt);
3735
3736        // gated RMSNorm + out projection (prefill dispatch). task #17: the f16out twin also
3737        // emits the ssm_out GEMM's fp16 operand in-epilogue (kills the standalone convert).
3738        let mut gn = e.uninit(d_state * num_v * t)?;
3739        let gn16 = if Self::f16out_on(e, t) {
3740            let mut g16 = e.alloc_u8_uninit(d_state * num_v * t * 2)?;
3741            e.gated_rmsnorm_f16out_zv(
3742                &o,
3743                la.ssm_norm.float_data(),
3744                z,
3745                &mut gn,
3746                &mut g16,
3747                d_state,
3748                num_v * t,
3749                eps,
3750            )?;
3751            Some(g16)
3752        } else {
3753            e.gated_rmsnorm_zv(
3754                &o,
3755                la.ssm_norm.float_data(),
3756                z,
3757                &mut gn,
3758                d_state,
3759                num_v * t,
3760                eps,
3761            )?;
3762            None
3763        };
3764        Ok((gn, gn16))
3765    }
3766
3767    /// Task #15 core-split wrapper: composes inner + ssm_out (== the old core).
3768    #[allow(clippy::too_many_arguments)]
3769    fn linear_attn_prime_core_pad(
3770        &self,
3771        e: &Engine,
3772        la: &LinearAttnLayer,
3773        g4: Vec<CudaSlice<f32>>,
3774        t: usize,
3775        cache: &mut Cache,
3776        il: usize,
3777        pad_len: Option<&CudaSlice<i32>>,
3778    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3779        let (gn, gn16) = self.linear_attn_prime_core_pad_inner(e, la, g4, t, cache, il, pad_len)?;
3780        if let Some(xh) = &gn16 {
3781            if let Some(y) = e.try_f16_gemm_pre(&la.ssm_out, xh, t)? {
3782                return Ok(y);
3783            }
3784        }
3785        Ok(e.matmul(&la.ssm_out, &gn, t)?)
3786    }
3787
3788    /// Full-attention mixer with QK-norm, partial RoPE, sigmoid output gate (qwen35 :257-336).
3789    ///
3790    /// `il` = layer index: step35 needs it (per-layer n_head / rope width / window / gate) and
3791    /// routes to its own mixer. Every other arch ignores it (uniform geometry).
3792    pub fn full_attn(
3793        &self,
3794        e: &Engine,
3795        fa: &FullAttnLayer,
3796        h: &CudaSlice<f32>,
3797        pos_d: &CudaSlice<i32>,
3798        t: usize,
3799        il: usize,
3800    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3801        if self.cfg.step35.is_some() {
3802            return self.step35_attn(e, fa, h, pos_d, t, il);
3803        }
3804        let cfg = &self.cfg;
3805        let _n_embd = cfg.n_embd as usize;
3806        let geometry = cfg.full_attention_geometry_at(il as u32);
3807        let n_head = geometry.n_head as usize;
3808        let n_head_kv = geometry.n_head_kv as usize;
3809        let head_dim = geometry.head_dim_k as usize;
3810        let eps = cfg.rms_eps;
3811        let scale = geometry.attention_scale();
3812
3813        // qwen35: wq output = head_dim*2*n_head (fused [q|gate] per head). M3/Hy3: NO output
3814        // gate — wq out = n_head*head_dim, no split (see prime-path note).
3815        let gated = geometry.attention_gate == memra_gguf::config::AttentionGateKind::FusedQ;
3816        // grouped: one f16 activation convert feeds q/k/v (matmul_group)
3817        let mut g3 = e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?;
3818        let v = g3.pop().unwrap();
3819        let mut k = g3.pop().unwrap();
3820        let qf = g3.pop().unwrap();
3821        let (mut q, gate) = if gated {
3822            let mut q = e.uninit(t * n_head * head_dim)?;
3823            let mut gate = e.uninit(t * n_head * head_dim)?;
3824            e.q_gate_split(&qf, &mut q, &mut gate, head_dim, n_head, t)?;
3825            (q, Some(gate))
3826        } else {
3827            (qf, None)
3828        };
3829
3830        // QK-norm (per head_dim row), then partial RoPE.
3831        let mut qn = e.uninit(t * n_head * head_dim)?;
3832        e.rms_norm(
3833            &q,
3834            fa.q_norm.float_data(),
3835            &mut qn,
3836            head_dim,
3837            n_head * t,
3838            eps,
3839        )?;
3840        q = qn;
3841        let mut kn = e.uninit(t * n_head_kv * head_dim)?;
3842        e.rms_norm(
3843            &k,
3844            fa.k_norm.float_data(),
3845            &mut kn,
3846            head_dim,
3847            n_head_kv * t,
3848            eps,
3849        )?;
3850        k = kn;
3851        let rope_dims = geometry.n_rot as usize;
3852        e.rope_neox(
3853            &mut q,
3854            pos_d,
3855            head_dim,
3856            rope_dims,
3857            n_head,
3858            t,
3859            geometry.rope_base,
3860            1.0,
3861        )?;
3862        e.rope_neox(
3863            &mut k,
3864            pos_d,
3865            head_dim,
3866            rope_dims,
3867            n_head_kv,
3868            t,
3869            geometry.rope_base,
3870            1.0,
3871        )?;
3872
3873        // SDPA
3874        let mut attn = e.uninit(t * n_head * head_dim)?;
3875        // hand-written FlashAttention prefill (head_dim 256/128 stamped twins). MEMRA_NOFA
3876        // falls back to naive sdpa.
3877        if std::env::var("MEMRA_NOFA").is_ok() || !(head_dim == 256 || head_dim == 128) {
3878            // head_dim gate: see prime-path note (fa_prefill is stamped at 256 and 128 only).
3879            e.sdpa_naive(
3880                &q, &k, &v, &mut attn, head_dim, n_head, n_head_kv, t, t, scale, true,
3881            )?;
3882        } else {
3883            e.fa_prefill(
3884                &q, &k, &v, &mut attn, head_dim, n_head, n_head_kv, t, t, scale, true,
3885            )?;
3886        }
3887
3888        // output gate: attn * sigmoid(gate) — qwen35 only (M3 has no gate).
3889        let attn_g = match &gate {
3890            Some(gate) => {
3891                let mut gsig = e.uninit(t * n_head * head_dim)?;
3892                e.sigmoid(gate, &mut gsig, t * n_head * head_dim)?;
3893                let mut ag = e.uninit(t * n_head * head_dim)?;
3894                e.mul(&attn, &gsig, &mut ag, t * n_head * head_dim)?;
3895                ag
3896            }
3897            None => attn,
3898        };
3899
3900        // o projection
3901        let o = e.matmul(&fa.wo, &attn_g, t)?;
3902        Ok(o)
3903    }
3904
3905    /// Linear-attention (Gated DeltaNet) mixer (qwen35 :338-470).
3906    pub fn linear_attn(
3907        &self,
3908        e: &Engine,
3909        la: &LinearAttnLayer,
3910        h: &CudaSlice<f32>,
3911        t: usize,
3912    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3913        let cfg = &self.cfg;
3914        let _n_embd = cfg.n_embd as usize;
3915        let ssm = cfg.ssm.as_ref().unwrap();
3916        let d_state = ssm.state_size as usize; // 128
3917        let num_k = ssm.group_count as usize; // 16
3918        let num_v = ssm.time_step_rank as usize; // 32
3919        let d_conv = ssm.conv_kernel as usize; // 4
3920        let head_k = d_state;
3921        let head_v = d_state;
3922        let key_dim = head_k * num_k; // 2048
3923        let value_dim = head_v * num_v; // 4096
3924        let conv_dim = key_dim * 2 + value_dim; // 8192
3925        let eps = cfg.rms_eps;
3926        let scale = 1.0 / (d_state as f32).sqrt();
3927
3928        // projections
3929        // grouped: one f16 activation convert feeds all four projections (matmul_group)
3930        let mut g4 = e.matmul_group(
3931            &[&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha],
3932            h,
3933            t,
3934        )?;
3935        let alpha = g4.pop().unwrap(); // [T, num_v]
3936        let beta_raw = g4.pop().unwrap(); // [T, num_v]
3937        let z = g4.pop().unwrap(); // [T, value_dim]
3938        let qkv_mixed = g4.pop().unwrap(); // [T, conv_dim] token-major
3939
3940        // conv + GDN repack, FUSED (2026-07-03): ssm_conv1d_gdn reads qkv_mixed [T, conv_dim]
3941        // token-major DIRECTLY (causal window rows t-pad..t, rows<0 = zero prefill state), applies
3942        // the 8-tap conv + SiLU, and scatters straight into the GDN [d_state, num_v, T] q/k/v
3943        // layout with the modulo head-repeat. Replaces transpose + zeros + conv_left_pad +
3944        // ssm_conv1d + qkv_to_gdn_repack (5 launches, conv_in/conv_out scratch + a 16MB@T=512
3945        // round-trip). BIT-IDENTICAL accumulation and scatter mapping.
3946        let _ = (head_k, head_v);
3947        let mut q_g = e.uninit(d_state * num_v * t)?;
3948        let mut k_g = e.uninit(d_state * num_v * t)?;
3949        let mut v_g = e.uninit(d_state * num_v * t)?;
3950        e.ssm_conv1d_gdn(
3951            &qkv_mixed,
3952            la.ssm_conv1d.float_data(),
3953            &mut q_g,
3954            &mut k_g,
3955            &mut v_g,
3956            conv_dim,
3957            t,
3958            d_conv,
3959            d_state,
3960            num_v,
3961            num_k,
3962            key_dim,
3963        )?;
3964        // L2-norm q,k per (head_dim) row — rows are contiguous d_state in q_g.
3965        let mut q_l2 = e.uninit(d_state * num_v * t)?;
3966        e.l2_norm(&q_g, &mut q_l2, d_state, num_v * t, eps)?;
3967        let mut k_l2 = e.uninit(d_state * num_v * t)?;
3968        e.l2_norm(&k_g, &mut k_l2, d_state, num_v * t, eps)?;
3969        let v_gd = v_g;
3970
3971        // beta = sigmoid(beta_raw) ; g_log = a * softplus(alpha + dt). Both need [num_v, T] layout
3972        // (g[t*num_v + h]). beta_raw/alpha are [T, num_v] token-major == that layout already.
3973        let mut beta = e.uninit(t * num_v)?;
3974        e.sigmoid(&beta_raw, &mut beta, t * num_v)?;
3975        // gdn_glog expects alpha [H,T] with alpha[t*H+h] and dt_bias/a [H] — matches token-major [T,num_v].
3976        let mut g_log = e.uninit(t * num_v)?;
3977        e.gdn_glog(
3978            &alpha,
3979            la.ssm_dt.float_data(),
3980            la.ssm_a.float_data(),
3981            &mut g_log,
3982            num_v,
3983            t,
3984        )?;
3985
3986        // GDN scan (A4: gdn_scan_prefill dispatches chunked WY under MEMRA_GDN_CHUNKED)
3987        let state_in = e.zeros(d_state * d_state * num_v)?; // zero state (prefill)
3988        let mut state_out = e.zeros(d_state * d_state * num_v)?;
3989        let mut o = e.uninit(d_state * num_v * t)?;
3990        e.gdn_scan_prefill(
3991            &q_l2,
3992            &k_l2,
3993            &v_gd,
3994            &g_log,
3995            &beta,
3996            None,
3997            None,
3998            &state_in,
3999            &mut state_out,
4000            &mut o,
4001            num_v,
4002            t,
4003            scale,
4004            num_v,
4005        )?;
4006
4007        // gated RMSNorm: dst = RMSNorm(o, ssm_norm[head_v]) * silu(z). o is [d_state, num_v, T];
4008        // rows of head_v=d_state, nrows = num_v*T. z must match row layout: z is [T, value_dim] token-major
4009        // = [T, num_v*head_v]; per (t, vh) the head_v slice is contiguous -> rows align as (t*num_v+vh).
4010        // o rows are (t*num_v+vh) too. Good.
4011        let mut gn = e.uninit(d_state * num_v * t)?;
4012        e.gated_rmsnorm(
4013            &o,
4014            la.ssm_norm.float_data(),
4015            &z,
4016            &mut gn,
4017            d_state,
4018            num_v * t,
4019            eps,
4020        )?;
4021
4022        // ssm_out projection: gn is [d_state, num_v, T] = [value_dim, T] viewed token-major as [T, value_dim]?
4023        // gn layout: (t*num_v+vh)*d_state + i  == token t, then (vh,i) = channel vh*d_state+i. That's
4024        // token-major [T, value_dim]. linear wants [T, in=value_dim]. Good.
4025        let out = e.matmul(&la.ssm_out, &gn, t)?;
4026        Ok(out)
4027    }
4028}
4029
4030impl HybridModel {
4031    /// MoE FFN (EDGE-1). z: [T, n_embd] (already post-attention-normed). Returns moe_out [T, n_embd].
4032    /// Node-for-node vs llama.cpp build_moe_ffn + qwen35moe::build_layer_ffn.
4033    ///
4034    /// `il` is the trunk layer index — the residency-cache key prefix (a gate-expert of layer 3 is a
4035    /// different 860160-byte block than the same expert of layer 7).
4036    ///
4037    /// Routing: host softmax+sort (default) OR the fused router kernel (MEMRA_FUSED_ROUTER).
4038    /// Dispatch: stage-every-token into 3 scratch slots (default) OR the SLRU residency cache
4039    /// (MEMRA_MOE_CACHE). The cache-HIT weight path is bit-identical to stage-every-token (§B.3).
4040    /// Convenience wrapper used by the hybrid trunk/MTP loops: pulls dims + max-block from `self`.
4041    pub fn moe_ffn_il(
4042        &self,
4043        e: &Engine,
4044        m: &MoeWeights,
4045        z: &CudaSlice<f32>,
4046        t: usize,
4047        il: u16,
4048    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4049        Self::moe_ffn_inner(e, m, z, None, t, &self.cfg, il, self.max_moe_block(), false)
4050    }
4051
4052    /// Prefill twin: Step35 promotes expert-grouped dispatch by default while decode/spec callers
4053    /// keep `moe_ffn_il` and therefore retain their existing dispatch class.
4054    pub fn moe_ffn_il_prefill(
4055        &self,
4056        e: &Engine,
4057        m: &MoeWeights,
4058        z: &CudaSlice<f32>,
4059        t: usize,
4060        il: u16,
4061    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4062        Self::moe_ffn_inner(e, m, z, None, t, &self.cfg, il, self.max_moe_block(), true)
4063    }
4064
4065    /// Decode-path twin with a PRE-QUANTIZED z (from add_rms_norm_zq8): threads (zq, zd) into the
4066    /// t=1 dev arm so the per-layer standalone quantize_q8_1 launch folds away. Identical bytes
4067    /// (the fused kernel reproduces quantize_q8_1 exactly); every other path ignores the pair.
4068    pub fn moe_ffn_il_zq8(
4069        &self,
4070        e: &Engine,
4071        m: &MoeWeights,
4072        z: &CudaSlice<f32>,
4073        zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>,
4074        t: usize,
4075        il: u16,
4076    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4077        Self::moe_ffn_inner(e, m, z, zq8, t, &self.cfg, il, self.max_moe_block(), false)
4078    }
4079
4080    /// MoE FFN (EDGE-1), source-/model-agnostic. z: [T, n_embd] (already post-attention-normed).
4081    /// Returns moe_out [T, n_embd]. Node-for-node vs llama.cpp build_moe_ffn. Shared by the hybrid
4082    /// (qwen35moe, shared expert present) and the dense-attention MoE (OLMoE, no shared expert) paths;
4083    /// `cfg.moe` supplies the dims and the optional shexp fields decide whether step 3 runs.
4084    ///
4085    /// `il` is the layer index — the residency-cache key prefix. `max_block` is the global max expert
4086    /// stride (fixed cache-slot size); pass `self.max_moe_block()`.
4087    pub(crate) fn moe_ffn(
4088        e: &Engine,
4089        m: &MoeWeights,
4090        z: &CudaSlice<f32>,
4091        t: usize,
4092        cfg: &ModelConfig,
4093        il: u16,
4094        max_block: usize,
4095    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4096        Self::moe_ffn_inner(e, m, z, None, t, cfg, il, max_block, false)
4097    }
4098
4099    #[allow(clippy::too_many_arguments)]
4100    pub(crate) fn moe_ffn_inner(
4101        e: &Engine,
4102        m: &MoeWeights,
4103        z: &CudaSlice<f32>,
4104        zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>,
4105        t: usize,
4106        cfg: &ModelConfig,
4107        il: u16,
4108        max_block: usize,
4109        prefill: bool,
4110    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4111        let worker_io = crate::spill_pread::worker_enabled();
4112        let epoch_lfu = std::env::var_os("MEMRA_MOE_LFU_DECAY").is_some();
4113        if Engine::moe_cache_enabled() && (worker_io || epoch_lfu) {
4114            e.with_moe_cache(max_block, |cache, _| {
4115                cache.begin_forward_epoch(il, t);
4116                if worker_io {
4117                    cache.begin_worker_scope();
4118                }
4119                Ok(())
4120            })?;
4121        }
4122        if Self::sigmoid_resident_dev_eligible(e, m, cfg) {
4123            let moe = cfg.moe.as_ref().unwrap();
4124            let n_expert = moe.expert_count as usize;
4125            let n_used = moe.expert_used_count as usize;
4126            let sigmoid = cfg.sigmoid_router().unwrap();
4127            let logits = Self::moe_router_logits(e, m, z, t, cfg)?;
4128            Self::trace_sigmoid_router_logits(e, il, t, n_expert, n_used, &logits, m, sigmoid)?;
4129            return Self::moe_ffn_sigmoid_dev(e, m, z, zq8, &logits, t, cfg, il, sigmoid);
4130        }
4131        // Expert-grouped dispatch for prefill (T>1). MEMRA_MOE_GROUPED explicitly opts the
4132        // current caller into this research arm; the naked default stays on the established path.
4133        if t > 1 && moe_grouped_enabled(cfg, prefill) {
4134            let grouped_out = Self::moe_ffn_grouped(e, m, z, t, cfg, il, max_block)?;
4135            // MEMRA_MOE_GATE: byte-identity comparison vs sequential path.
4136            // The grouped q8 path uses the same row-wise quantize/dot/activation/FMA programs as
4137            // sequential dispatch; mixed or q8-disabled layouts fall back to the matching f32
4138            // path. A mismatch is therefore a correctness failure, not an accepted numeric class.
4139            if std::env::var("MEMRA_MOE_GATE").is_ok() {
4140                let seq_out = Self::moe_ffn_sequential(e, m, z, t, cfg, il, max_block)?;
4141                let g_host = e.dtoh(&grouped_out)?;
4142                let s_host = e.dtoh(&seq_out)?;
4143                let g_bytes: &[u8] = unsafe {
4144                    std::slice::from_raw_parts(g_host.as_ptr() as *const u8, g_host.len() * 4)
4145                };
4146                let s_bytes: &[u8] = unsafe {
4147                    std::slice::from_raw_parts(s_host.as_ptr() as *const u8, s_host.len() * 4)
4148                };
4149                if g_bytes == s_bytes {
4150                    println!("moe-gate il={il} t={t} BYTE-IDENTICAL");
4151                } else {
4152                    let diffs = g_host
4153                        .iter()
4154                        .zip(s_host.iter())
4155                        .enumerate()
4156                        .filter(|(_, (a, b))| a != b)
4157                        .count();
4158                    let maxdiff = g_host
4159                        .iter()
4160                        .zip(s_host.iter())
4161                        .map(|(a, b)| (a - b).abs())
4162                        .fold(0.0f32, f32::max);
4163                    panic!(
4164                        "moe-gate il={il} t={t} MISMATCH: {diffs}/{} elems differ, maxdiff={maxdiff:.6e}",
4165                        g_host.len()
4166                    );
4167                }
4168            }
4169            return Ok(grouped_out);
4170        }
4171        Self::moe_ffn_sequential_zq8(e, m, z, zq8, t, cfg, il, max_block)
4172    }
4173
4174    fn sigmoid_resident_dev_eligible(e: &Engine, m: &MoeWeights, cfg: &ModelConfig) -> bool {
4175        let Some(moe) = cfg.moe.as_ref() else {
4176            return false;
4177        };
4178        // Cached once per process: this predicate runs per MoE layer per decode step, and five
4179        // uncached env reads were ~300+ environ scans/step on the exact path this arm de-hosts.
4180        static OBSERVATION_MODE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
4181        let observation_mode = *OBSERVATION_MODE.get_or_init(|| {
4182            std::env::var("MEMRA_MOE_STATS").is_ok()
4183                || std::env::var("MEMRA_MOE_TRACE").is_ok()
4184                || std::env::var("MEMRA_MOE_WEIGHT_TRACE").is_ok()
4185                || std::env::var("MEMRA_MOE_INPUT_TRACE_DIR").is_ok()
4186                || std::env::var("MEMRA_MOE_GATE").is_ok()
4187        });
4188        cfg.step35.is_some()
4189            && sigmoid_router_enabled()
4190            && moe_dev_enabled()
4191            && moe_slab_enabled()
4192            && !observation_mode
4193            && moe.expert_used_count <= 8
4194            && m.has_uniform_expert_layout()
4195            && m.gate_exps.macros.is_none()
4196            && m.up_exps.macros.is_none()
4197            && m.down_exps.macros.is_none()
4198            && !m.has_macros
4199            && moe_q8_enabled()
4200            && q8_expert_supported(m.gate_exps.qtype)
4201            && q8_expert_supported(m.up_exps.qtype)
4202            && q8_expert_supported(m.down_exps.qtype)
4203            && m.dev_exps
4204                .as_ref()
4205                .is_some_and(|dev| dev.dev == e.ctx().ordinal())
4206    }
4207
4208    /// Sequential (per-token) MoE FFN -- the original path. Factored out for the gate comparison.
4209    pub(crate) fn moe_ffn_sequential(
4210        e: &Engine,
4211        m: &MoeWeights,
4212        z: &CudaSlice<f32>,
4213        t: usize,
4214        cfg: &ModelConfig,
4215        il: u16,
4216        max_block: usize,
4217    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4218        Self::moe_ffn_sequential_zq8(e, m, z, None, t, cfg, il, max_block)
4219    }
4220
4221    /// One router-logit selector shared by sequential and grouped dispatch. Real prefill must use
4222    /// the row-wise GEMV when exact routing is enabled: cuBLASLt's reduction changes with `m`,
4223    /// which makes expert selection depend on the caller's chunk or concat-batch shape.
4224    fn moe_router_logits(
4225        e: &Engine,
4226        m: &MoeWeights,
4227        z: &CudaSlice<f32>,
4228        t: usize,
4229        cfg: &ModelConfig,
4230    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4231        if t < PRIME_MIN_T {
4232            // Decode and speculative verify use one fixed per-row reduction program.
4233            if crate::router_kernel_on() {
4234                e.router_gemv(
4235                    m.gate_inp.float_data(),
4236                    z,
4237                    cfg.n_embd as usize,
4238                    m.gate_exps.n_expert,
4239                    t,
4240                )
4241            } else {
4242                e.matmul_decode_exact(&m.gate_inp, z, t)
4243            }
4244        } else if crate::router_prefill_exact_on() && crate::router_kernel_on() {
4245            e.router_gemv(
4246                m.gate_inp.float_data(),
4247                z,
4248                cfg.n_embd as usize,
4249                m.gate_exps.n_expert,
4250                t,
4251            )
4252        } else {
4253            e.matmul(&m.gate_inp, z, t)
4254        }
4255    }
4256
4257    /// Append the host-visible router selection for one layer/forward when calibration tracing is
4258    /// enabled. Both sequential and expert-grouped prefill must call this after routing so the
4259    /// trace is independent of the dispatch optimization selected for the forward.
4260    fn trace_moe_routes(
4261        il: u16,
4262        t: usize,
4263        sel_all: &[u32],
4264        weights: &[f32],
4265    ) -> Result<(), Box<dyn std::error::Error>> {
4266        use std::io::Write as _;
4267        if let Ok(path) = std::env::var("MEMRA_MOE_TRACE") {
4268            let mut f = std::fs::OpenOptions::new()
4269                .create(true)
4270                .append(true)
4271                .open(path)?;
4272            let ids: Vec<String> = sel_all.iter().map(|s| s.to_string()).collect();
4273            writeln!(f, "{} {} {}", il, t, ids.join(","))?;
4274        }
4275        if let Ok(path) = std::env::var("MEMRA_MOE_WEIGHT_TRACE") {
4276            let mut f = std::fs::OpenOptions::new()
4277                .create(true)
4278                .append(true)
4279                .open(path)?;
4280            let pairs: Vec<String> = sel_all
4281                .iter()
4282                .zip(weights)
4283                .map(|(expert, weight)| format!("{expert}:{weight:.9}"))
4284                .collect();
4285            writeln!(f, "{} {} {}", il, t, pairs.join(","))?;
4286        }
4287        Ok(())
4288    }
4289
4290    #[allow(clippy::too_many_arguments)]
4291    fn trace_sigmoid_router_logits(
4292        e: &Engine,
4293        il: u16,
4294        t: usize,
4295        n_expert: usize,
4296        n_used: usize,
4297        logits: &CudaSlice<f32>,
4298        m: &MoeWeights,
4299        (scaling_factor, route_norm): (f32, bool),
4300    ) -> Result<(), Box<dyn std::error::Error>> {
4301        if !crate::sigrouter_contract::served_logit_trace_enabled() || t != 1 {
4302            return Ok(());
4303        }
4304        let logits = e.dtoh(logits)?;
4305        let active: Vec<u8> = m
4306            .active_experts
4307            .as_ref()
4308            .map(|mask| mask.iter().map(|&enabled| u8::from(enabled)).collect())
4309            .unwrap_or_else(|| vec![1; n_expert]);
4310        let bias = m.exp_probs_b.clone().unwrap_or_else(|| vec![0.0; n_expert]);
4311        crate::sigrouter_contract::capture_served_logits(
4312            il as u32,
4313            t,
4314            n_expert,
4315            n_used,
4316            scaling_factor,
4317            route_norm,
4318            &active,
4319            &bias,
4320            &logits,
4321        )?;
4322        Ok(())
4323    }
4324
4325    /// Append the f32 input to one MoE layer for offline, layerwise calibration. This diagnostic
4326    /// intentionally performs a DtoH copy and is therefore disabled unless an explicit fresh trace
4327    /// directory is supplied. Each layer owns one payload file; index.jsonl records byte offsets so
4328    /// a validator can prove request/layer coverage before the trace is used for pruning or healing.
4329    fn trace_moe_input(
4330        e: &Engine,
4331        il: u16,
4332        t: usize,
4333        n_embd: usize,
4334        z: &CudaSlice<f32>,
4335    ) -> Result<(), Box<dyn std::error::Error>> {
4336        use std::io::Write as _;
4337        let Ok(dir) = std::env::var("MEMRA_MOE_INPUT_TRACE_DIR") else {
4338            return Ok(());
4339        };
4340        let host = e.dtoh(z)?;
4341        if host.len() != t * n_embd {
4342            return Err(format!(
4343                "MoE input trace shape mismatch at layer {il}: got {} values, expected {}x{}",
4344                host.len(),
4345                t,
4346                n_embd
4347            )
4348            .into());
4349        }
4350        let bytes = unsafe {
4351            std::slice::from_raw_parts(
4352                host.as_ptr().cast::<u8>(),
4353                host.len() * std::mem::size_of::<f32>(),
4354            )
4355        };
4356        let state = MOE_INPUT_TRACE_WRITER.get_or_init(|| std::sync::Mutex::new(None));
4357        let mut state = state
4358            .lock()
4359            .map_err(|_| "MoE input trace writer lock is poisoned")?;
4360        if state.is_none() {
4361            let dir = std::path::PathBuf::from(&dir);
4362            std::fs::create_dir_all(&dir)?;
4363            let index = std::fs::OpenOptions::new()
4364                .create(true)
4365                .append(true)
4366                .open(dir.join("index.jsonl"))?;
4367            *state = Some(MoeInputTraceWriter {
4368                dir,
4369                index,
4370                payloads: std::collections::HashMap::new(),
4371            });
4372        }
4373        let writer = state.as_mut().unwrap();
4374        if writer.dir != std::path::Path::new(&dir) {
4375            return Err("MEMRA_MOE_INPUT_TRACE_DIR changed after capture started".into());
4376        }
4377        let file_name = format!("layer-{il:03}.f32");
4378        if !writer.payloads.contains_key(&il) {
4379            let payload = std::fs::OpenOptions::new()
4380                .create(true)
4381                .append(true)
4382                .open(writer.dir.join(&file_name))?;
4383            let offset = payload.metadata()?.len();
4384            writer.payloads.insert(il, (payload, offset));
4385        }
4386        let (payload, offset) = writer.payloads.get_mut(&il).unwrap();
4387        let row_offset = *offset;
4388        payload.write_all(bytes)?;
4389        *offset += bytes.len() as u64;
4390        writeln!(
4391            writer.index,
4392            "{{\"format\":\"memra-moe-input-trace-v1\",\"layer\":{il},\"tokens\":{t},\
4393             \"hidden_size\":{n_embd},\"file\":\"{file_name}\",\"offset\":{row_offset},\
4394             \"payload_bytes\":{}}}",
4395            bytes.len()
4396        )?;
4397        Ok(())
4398    }
4399
4400    #[allow(clippy::too_many_arguments)]
4401    pub(crate) fn moe_ffn_sequential_zq8(
4402        e: &Engine,
4403        m: &MoeWeights,
4404        z: &CudaSlice<f32>,
4405        zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>,
4406        t: usize,
4407        cfg: &ModelConfig,
4408        il: u16,
4409        max_block: usize,
4410    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4411        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
4412        let moe = cfg.moe.as_ref().unwrap();
4413        let n_embd = cfg.n_embd as usize; // 2048 (gate/up in_f, down out_f)
4414        let n_expert = moe.expert_count as usize; // 256
4415        let n_used = moe.expert_used_count as usize; // 8
4416        let n_ff_exp = moe.expert_ff_length as usize; // 512 (gate/up out_f, down in_f)
4417
4418        // verify the HostExps dims match cfg (catches a wrong-file / transpose mixup)
4419        debug_assert_eq!(m.gate_exps.in_f, n_embd);
4420        debug_assert_eq!(m.gate_exps.out_f, n_ff_exp);
4421        debug_assert_eq!(m.down_exps.in_f, n_ff_exp); // down is TRANSPOSED: in=512
4422        debug_assert_eq!(m.down_exps.out_f, n_embd); //                     out=2048
4423        debug_assert_eq!(m.gate_exps.n_expert, n_expert);
4424
4425        // step35 PER-LAYER SwiGLU clamp (None on every other arch and on every unclamped layer).
4426        // Routed experts and the shared expert read SEPARATE arrays — never share one value.
4427        let lim_exp = cfg.clamp_exp_at(il as u32);
4428        let lim_shexp = cfg.clamp_shexp_at(il as u32);
4429        let use_cache = Engine::moe_cache_enabled();
4430        let uniform_experts = m.has_uniform_expert_layout();
4431        let moe_q8 = uniform_experts
4432            && moe_q8_enabled()
4433            && q8_expert_supported(m.gate_exps.qtype)
4434            && q8_expert_supported(m.up_exps.qtype)
4435            && q8_expert_supported(m.down_exps.qtype);
4436        // Experimental secondary backend: complete experts already resident in the SLRU stay on
4437        // CUDA; any expert missing one or more projections runs from the original host GGUF bytes
4438        // through llama.cpp CPU quant dots. Small-t only keeps decode and speculative verification
4439        // in the same numeric/dispatch class; real prefill remains on the established GPU path and
4440        // seeds the residency cache. An explicit library path is the build/runtime gate, so naked
4441        // commands and CI have no llama.cpp or OpenMP dependency.
4442        let cpu_expert_requested = crate::cpu_experts::configured();
4443        if cpu_expert_requested && (cfg.hy3.is_none() || cfg.m3.is_some()) {
4444            return Err(std::io::Error::other(
4445                "MEMRA_CPU_EXPERT_LIB is experimental and currently gated to Hy3",
4446            )
4447            .into());
4448        }
4449        let cpu_hybrid = cpu_expert_requested && t < PRIME_MIN_T && m.dev_exps.is_none();
4450        // A dynamic SLRU changes which complete experts run on CUDA versus llama.cpp CPU dots.
4451        // Those backends are each deterministic but are different numeric configurations, so a
4452        // later prefill eviction can change greedy output. Freeze after the first real prefill;
4453        // decode/spec reads the fixed resident set, while later prefill misses use transient GPU
4454        // staging below and cannot change backend assignment.
4455        let freeze_cpu_residency = cpu_expert_requested
4456            && std::env::var("MEMRA_CPU_EXPERT_FREEZE_CACHE").as_deref() == Ok("1");
4457        let caller_warms_before_freeze = std::env::var("MEMRA_CPU_EXPERT_FREEZE_WARMUP_TOKENS")
4458            .ok()
4459            .and_then(|value| value.parse::<usize>().ok())
4460            .is_some_and(|tokens| tokens > 0);
4461        if cpu_hybrid && freeze_cpu_residency && !caller_warms_before_freeze {
4462            e.freeze_moe_cache();
4463        }
4464        let cache_frozen = use_cache && e.moe_cache_frozen();
4465        let cache_dispatch = use_cache && (!cache_frozen || cpu_hybrid);
4466
4467        // 1. ROUTER: one selector is shared with expert-grouped prefill so changing dispatch
4468        // cannot change logits, selected expert ids, or routing weights.
4469        let logits = Self::moe_router_logits(e, m, z, t, cfg)?;
4470        if let Some(sig) = cfg.sigmoid_router() {
4471            Self::trace_sigmoid_router_logits(e, il, t, n_expert, n_used, &logits, m, sig)?;
4472        }
4473
4474        // LAUNCH-STRUCTURE STAGE 3 (2026-07-05, MEMRA_MOE_DEV default ON, =0 rollback): ZERO-DtoH
4475        // device-dispatch when this layer's expert blocks are ALL cache-resident. The fused
4476        // router's sel/w stay ON DEVICE; the expert weight pointers come from the per-layer
4477        // device table of fixed slot addresses; gate/up/silu + down/fma run as the same TWO
4478        // launches per token as gdec. Removes the per-layer router DtoH + stream sync — the
4479        // per-token host stall that dominated the 35B decode wall after stages 1+2.
4480        // BIT-IDENTITY: the router kernel is selection-exact vs the host oracle (kernel-check
4481        // tie gate) and the _dev matvec twins reproduce the gdec kernels' exact FP chains; the
4482        // only difference is where sel/w/pointers are READ from (device instead of params).
4483        // Residency: one-shot PREWARM force-admits the layer while free slots cover it
4484        // (MEMRA_MOE_PREWARM=0 -> organic residency, dev path fires when the SLRU fills).
4485        // Any non-resident layer falls through to host routing + the gdec/sequential path.
4486        // FITS-VRAM RESIDENT EXPERTS (2026-07-06): the layer's expert slabs are device-resident
4487        // (load-time decision) — fire the zero-DtoH dev path unconditionally with the prebuilt
4488        // pointer row. No cache, no dispatch, no residency check: the llama full-offload regime
4489        // (it measured 169.55 vs the cache path's 28.5 on the local 35B — the residency-gate
4490        // all-or-nothing fallback was the 6x). BIT-IDENTITY: same _dev kernels, same math; only
4491        // the pointer table's provenance differs (slab base+stride vs SLRU slot addresses).
4492        // MoE PREFILL PAIR-BATCH (2026-07-06, the 16x pp hole): t>1 on resident experts — ONE
4493        // launch per proj covers ALL (token,expert) pairs (grid.y=pair, warp-per-row), replacing
4494        // the per-expert loop (256 experts x 3-4 launches x tiny m_e). Scatter is slot-ordered
4495        // per token (the sequential-axpy bit-identity class). Requires q8-supported qtypes +
4496        // resident slabs. MEMRA_MOE_PAIRS=0 rollback.
4497        // t >= PRIME_MIN_T only (2026-07-06 exactness fix, verify-probe proof): spec VERIFY
4498        // batches (t = 2..K+2) previously rode these pairs kernels while T=1 decode rode the
4499        // dev_q8 loop — different FP chains, verify-T2 logit maxdiff 2.6e-1 vs eager -> greedy
4500        // flips at tight margins -> 35B real-prompt spec self-consistency FAIL (the 27B
4501        // "verify must be kernel-DISPATCH-identical to decode" lesson, MoE edition). Small-t
4502        // now rides the dev loop below (same kernels per token as decode); pairs serves real
4503        // prefill (t >= 16, where spec never verifies).
4504        // sigmoid-router archs must NOT enter the pairs/dev arms: those dispatch arms still route
4505        // via the fused SOFTMAX device router (moe_router_topk) — silently wrong experts (the
4506        // M3 gate-MISMATCH 74602-vs-92 lesson, 2026-07-07). Sigmoid routing has its own device
4507        // top-k kernel; MEMRA_SIG_ROUTER=0 selects the host sigmoid oracle instead.
4508        // Per-expert macro-scales (compressed-tensors NVFP4 ST class, e.g. unsloth 35B-A3B):
4509        // the device-dispatch kernels (pairs/dev) do NOT fold them — those checkpoints must
4510        // ride the macro-aware sequential/staged paths below or every expert output is off by
4511        // its global scale (~3e4x, measured garbage 2026-07-16). GGUF experts: macros None.
4512        let no_exp_macros = m.gate_exps.macros.is_none()
4513            && m.up_exps.macros.is_none()
4514            && m.down_exps.macros.is_none();
4515        // A clamped-SwiGLU layer (step35 43/44) also cannot ride pairs: moe_pairs_silu_mul's
4516        // epilogue is plain silu(gate)*up with no clamp form, and moe_ffn_pairs takes no `il`
4517        // so it cannot even see the per-layer limit.
4518        if cfg.sigmoid_router().is_none()
4519            && cfg.m3.is_none()
4520            && cfg.hy3.is_none()
4521            && !cfg.swiglu_clamped_at(il as u32)
4522            && no_exp_macros
4523            && t >= PRIME_MIN_T
4524            && m.dev_exps.is_some()
4525            && moe_q8_enabled()
4526            && q8_expert_supported(m.gate_exps.qtype)
4527            && q8_expert_supported(m.up_exps.qtype)
4528            && q8_expert_supported(m.down_exps.qtype)
4529            && std::env::var("MEMRA_MOE_PAIRS")
4530                .map(|v| v != "0")
4531                .unwrap_or(true)
4532            && std::env::var("MEMRA_MOE_STATS").is_err()
4533        {
4534            return Self::moe_ffn_pairs(e, m, z, &logits, t, cfg);
4535        }
4536
4537        // t < PRIME_MIN_T: moe_ffn_dev loops tokens serially (1 launch-pair per token) — the
4538        // decode path AND the spec-verify path (dispatch parity = exactness; see pairs gate
4539        // above). Serial launches are fine at t<=10 (K+2); real prefill never lands here.
4540        // moe_ffn_dev routes via the FUSED SOFTMAX device router (moe_router_topk). Sigmoid
4541        // routing has a separate device top-k kernel, but this dev arm cannot consume its sel/w
4542        // contract, so sigmoid-router arches must NOT enter it: with MOE_CACHE=1 M3 silently
4543        // routed softmax = wrong experts (gate MISMATCH 74602 vs 92, caught 2026-07-07).
4544        // MEMRA_SIG_ROUTER=0 selects the host sigmoid oracle for the supported sigmoid path.
4545        // macro-carrying experts are handled inside the dev path now (epilogue fold + w-scale);
4546        // pairs/gdec/csr keep their macro gates until their kernels grow the fold.
4547        // step35 (2026-08-06) is the third sigmoid-router arch and the deny above was written as
4548        // two arch names, not as the mechanism — so `dev_ok` let it into moe_ffn_dev's
4549        // moe_router_topk (softmax, no exp_probs_b bias, no expert_weights_scale) = silently
4550        // wrong experts, the same failure the pairs gate at :2254 already blocks by predicate.
4551        // Keyed off sigmoid_router() so arch #4 is denied by construction.
4552        // The clamped-SwiGLU layers must also fall through: every moe_ffn_dev kernel's fused
4553        // epilogue is plain silu(gate)*up (moe_gate_up_silu8_dev_*), which has no clamp form.
4554        let dev_ok = uniform_experts
4555            && cfg.sigmoid_router().is_none()
4556            && cfg.m3.is_none()
4557            && cfg.hy3.is_none()
4558            && !cfg.swiglu_clamped_at(il as u32);
4559        // Observation modes must route through the host-visible selection below. Otherwise a fully
4560        // resident layer returns through device dispatch before its trace/stats row is recorded,
4561        // silently biasing calibration toward only non-resident layers on large-VRAM machines.
4562        let observe_routes = std::env::var("MEMRA_MOE_STATS").is_ok()
4563            || std::env::var("MEMRA_MOE_TRACE").is_ok()
4564            || std::env::var("MEMRA_MOE_WEIGHT_TRACE").is_ok()
4565            || std::env::var("MEMRA_MOE_INPUT_TRACE_DIR").is_ok();
4566        if dev_ok
4567            && t < PRIME_MIN_T
4568            && m.dev_exps.is_some()
4569            && n_used <= 8
4570            && moe_dev_enabled()
4571            && !observe_routes
4572        {
4573            return Self::moe_ffn_dev(e, m, z, zq8, &logits, t, cfg, il, max_block);
4574        }
4575        if dev_ok && use_cache && n_used <= 8 && moe_dev_enabled() && !observe_routes {
4576            let row_ok = e.with_moe_cache(max_block, |c, eng| {
4577                if moe_prewarm_enabled() {
4578                    c.prewarm_layer(il, m, eng)?;
4579                }
4580                Ok(c.layer_dev_row(il, n_expert, eng)?.is_some())
4581            })?;
4582            if row_ok {
4583                return Self::moe_ffn_dev(e, m, z, zq8, &logits, t, cfg, il, max_block);
4584            }
4585        }
4586
4587        // Per-token (sel[8], w[8]) — sigmoid host oracle (M3/Hy3), else fused-router/softmax.
4588        let (sel_all, w_all, routed_cpu_input) = if let Some(sig) = cfg.sigmoid_router() {
4589            if cpu_hybrid {
4590                let (sel, w, input) = Self::moe_route_sigmoid_with_input(
4591                    e,
4592                    &logits,
4593                    z,
4594                    t,
4595                    n_expert,
4596                    n_used,
4597                    m.exp_probs_b.as_deref(),
4598                    sig,
4599                    m.active_experts.as_deref(),
4600                )?;
4601                (sel, w, Some(input))
4602            } else {
4603                let (sel, w) =
4604                    Self::moe_route_sigmoid_cfg(e, &logits, t, n_expert, n_used, m, sig)?;
4605                (sel, w, None)
4606            }
4607        } else {
4608            let (sel, w) =
4609                Self::moe_route_cfg(e, &logits, t, n_expert, n_used, m.active_experts.as_deref())?;
4610            (sel, w, None)
4611        };
4612        crate::moesd::record_host_routes(il, n_expert, n_used, &sel_all)?;
4613
4614        // MEMRA_MOE_TRACE=<path>: append one line per (layer, step) with the selected expert ids —
4615        // offline analysis derives the decode working set + step-to-step reuse (the go/no-go
4616        // measurement for resident-expert tiering; see rig5090.jsonl 2026-07-07 pinned-tier row).
4617        Self::trace_moe_routes(il, t, &sel_all, &w_all)?;
4618        Self::trace_moe_input(e, il, t, n_embd, z)?;
4619
4620        // Worker reads ordinarily overlap NVMe with the current expert but leave their H2D copies
4621        // demand-serialized on the compute stream. Hy3 decode already has a safe owner-thread
4622        // boundary here: sigmoid routing read `logits` back to the host, which synchronized all
4623        // earlier-layer compute. Submit the complete selected set first, then reserve only victims
4624        // outside that set and queue its H2D copies on the copy stream. Dispatch inserts an event
4625        // wait for each pending block, so later copies can overlap the earlier expert kernels while
4626        // the selected weights and numerical kernels remain unchanged. Restrict this experiment to
4627        // T=1; batched forwards can have token-local consumers still in flight between selections.
4628        // The CPU backend reads mmap-backed model bytes directly. Positioned-read worker buffers
4629        // are CUDA write-combined staging allocations and are intentionally not CPU-compute inputs;
4630        // do not spend NVMe bandwidth filling them for a layer whose misses go to CPU.
4631        let worker_disk_prefetch =
4632            cache_dispatch && crate::spill_pread::worker_enabled() && !cpu_hybrid;
4633        let promote_worker_h2d =
4634            t == 1 && worker_disk_prefetch && crate::spill_pread::copy_h2d_enabled();
4635        if promote_worker_h2d {
4636            let mut selected_blocks = Vec::with_capacity(n_used * 3);
4637            for &ex in sel_all.iter().take(n_used) {
4638                let ex = ex as u16;
4639                selected_blocks.extend([
4640                    BlockId::new(il, PROJ_GATE, ex),
4641                    BlockId::new(il, PROJ_UP, ex),
4642                    BlockId::new(il, PROJ_DOWN, ex),
4643                ]);
4644            }
4645            for &ex in sel_all.iter().take(n_used) {
4646                Self::moe_prefetch_disk_expert(e, il, ex as usize, m, max_block, &selected_blocks)?;
4647            }
4648            e.with_moe_cache(max_block, |cache, eng| {
4649                cache.promote_worker_reads_at_safe_boundary(
4650                    &selected_blocks,
4651                    &selected_blocks,
4652                    eng,
4653                )?;
4654                Ok(())
4655            })?;
4656        }
4657
4658        // MEMRA_MOE_STATS: per-layer routing stats for the A2 (expert-grouped prefill) baseline —
4659        // per-token expert-id entropy, active-expert coverage, tokens-per-expert group sizes.
4660        if t > 1 && std::env::var("MEMRA_MOE_STATS").is_ok() {
4661            let mut cnt = vec![0u32; n_expert];
4662            for &s in sel_all.iter() {
4663                cnt[s as usize] += 1;
4664            }
4665            let total = sel_all.len() as f64;
4666            let mut h = 0.0f64;
4667            let mut active = 0usize;
4668            for &c in &cnt {
4669                if c > 0 {
4670                    active += 1;
4671                    let p = c as f64 / total;
4672                    h -= p * p.log2();
4673                }
4674            }
4675            let maxc = cnt.iter().copied().max().unwrap_or(0);
4676            println!(
4677                "moe-stats il={} t={} assignments={} active={}/{} entropy={:.3}b (max {:.3}b) mean_tok_per_active={:.2} max_tok_per_expert={}",
4678                il,
4679                t,
4680                sel_all.len(),
4681                active,
4682                n_expert,
4683                h,
4684                (n_expert as f64).log2(),
4685                total / active.max(1) as f64,
4686                maxc
4687            );
4688        }
4689
4690        // LAUNCH-STRUCTURE STAGE 2 (2026-07-05): moe_out memset elision on the gdec path.
4691        // moe_down8_fma_f32 FULLY overwrites its token row (dst[o] = the in-kernel FMA chain that
4692        // starts at 0.0f — numerically the axpy-into-zeroed-row chain), so when the grouped-decode
4693        // path fires the upfront `e.zeros(t*n_embd)` memset is pure launch churn. Allocate uninit
4694        // when gdec CAN fire (any t — decode t=1 AND the spec verify t=K+1 route here per token)
4695        // and lazily zero ONLY the row of a token that falls through to the sequential axpy loop.
4696        // BIT-IDENTITY: unchanged — every row is either fully overwritten (gdec) or
4697        // zeroed-then-accumulated exactly as before (fallback).
4698        // `!swiglu_clamped_at`: the grouped-decode kernels' fused epilogue is plain
4699        // silu(gate)*up (see the m3 note at the call sites below) — a clamped layer must fall
4700        // through to the sequential loop's ffn_act_lim. Hoisted into the fire predicate so the
4701        // uninit/memset invariant at :2418 stays consistent with what actually dispatches.
4702        let gdec_may_fire = uniform_experts
4703            && use_cache
4704            && n_used <= 8
4705            && gdec_enabled()
4706            && !cfg.swiglu_clamped_at(il as u32);
4707        // SLAB-LOCAL RESIDENT ARM (lane/pp-leverb 2026-08-08): the fits-VRAM resident slabs
4708        // (`dev_exps`, sized per PP device by cx-503b) were consumed ONLY by the pairs/dev
4709        // arms — every one of which DENIES sigmoid-router archs (step35/M3/Hy3). So on those
4710        // archs the slabs were uploaded but never read, and every expert went through the
4711        // SLRU (37 GB H2D staging per pp4096 prime on the Step SKU; and post-cx-503b the
4712        // dead slabs additionally STARVE the SLRU, which sizes itself on free-VRAM-after-
4713        // residents). This arm reads the slabs through the SAME kernels the SLRU arm runs —
4714        // gdec's fused pair when eligible, the per-expert qmatvec twins otherwise — with the
4715        // slab base + ex*stride as the pointer provenance (the exact bit-identity class
4716        // `moe_ffn_dev` documents for its resident-vs-SLRU arms; bytes are the same HostExps
4717        // bytes either way). LOCALITY GATE: `d.dev == e.ctx().ordinal()` — a slab on another
4718        // device must NOT be dereferenced (m=1 peer reads are the measured 34-150x class,
4719        // strictly worse than staging); under PP-2 without the prime walker this admits
4720        // stage-0 layers on dev0 and leaves stage-1 layers on the SLRU, which is the honest
4721        // interim shape. MEMRA_MOE_SLAB=0 = rollback/A-B seam (read per call).
4722        let slab_local = m
4723            .dev_exps
4724            .as_ref()
4725            .filter(|d| !d.gu_il && moe_slab_enabled() && d.dev == e.ctx().ordinal());
4726        let slab_bases = slab_local.map(|d| {
4727            use cudarc::driver::DevicePtr;
4728            let s = e.stream();
4729            let (pg, _g0) = d.gate.device_ptr(&s);
4730            let (pu, _g1) = d.up.device_ptr(&s);
4731            let (pd, _g2) = d.down.device_ptr(&s);
4732            (pg as u64, pu as u64, pd as u64)
4733        });
4734        // Fused-pair eligibility mirrors the gdec call sites exactly (plain-SiLU epilogue,
4735        // no macros, m3 clamp excluded, q8-supported qtypes, <=8 experts) — INCLUDING
4736        // `gdec_enabled()`: the pair IS the gdec kernel pair, so MEMRA_MOE_GDEC=0 must
4737        // disable it here too. That seam is also the exactness localizer: with GDEC=0 both
4738        // provenances run the SAME per-expert qmatvec kernels (slab base+stride vs SLRU
4739        // slot) and must be BIT-IDENTICAL — the true provenance-only pair — while GDEC=1
4740        // compares the fused-pair class against the SLRU's hit/miss MIX (gdec for
4741        // all-resident tokens, staged loop for misses), which is a dispatch-class
4742        // comparison, not a provenance one.
4743        let slab_fused_may_fire = slab_bases.is_some()
4744            && n_used <= 8
4745            && gdec_enabled()
4746            && !cfg.swiglu_clamped_at(il as u32)
4747            && cfg.m3.is_none()
4748            && no_exp_macros
4749            && moe_q8;
4750        // moe_out memset elision: BOTH full-row-overwrite arms (gdec + slab fused) allocate
4751        // uninit; a token that falls through to any accumulating loop zeroes its own row.
4752        let mut moe_out = if gdec_may_fire || slab_fused_may_fire {
4753            e.uninit(t * n_embd)?
4754        } else {
4755            e.zeros(t * n_embd)?
4756        };
4757        // The router readback above already established a host boundary. Copy each small-t hidden
4758        // row once so CPU miss experts can start while the owner thread queues resident GPU work.
4759        let cpu_input = if cpu_hybrid {
4760            Some(routed_cpu_input.ok_or("CPU expert routing did not return the MoE input")?)
4761        } else {
4762            None
4763        };
4764
4765        // GPU scratch: one slot per proj, big enough for ONE expert (default stage-every-token path).
4766        // STAGE 2: LAZY — allocated only if the no-cache staging path actually runs (under
4767        // MEMRA_MOE_CACHE they were 3 dead ~1MB alloc_zeros + memset + free per layer per token,
4768        // measured ~123 memsets/token of the decode wall).
4769        let g_len = m.gate_exps.max_expert_bytes(); // 860160 for the uniform 35B gate
4770        let u_len = m.up_exps.max_expert_bytes(); // 860160 for the uniform 35B up
4771        let d_len = m.down_exps.max_expert_bytes(); // 1114112 for the uniform 35B down
4772        let mut scratch_g: Option<CudaSlice<u8>> = None;
4773        let mut scratch_u: Option<CudaSlice<u8>> = None;
4774        let mut scratch_d: Option<CudaSlice<u8>> = None;
4775        // `max_block` (the GLOBAL max expert stride across all layers) is passed in — the cache slots
4776        // are FIXED-ADDRESS and must fit any layer's block (UD/dynamic GGUFs vary quant per layer).
4777
4778        // EDGE-1 §C.2/C.3: the optional pipeline queues the next selected expert's MISS blocks on
4779        // the copy stream before launching the current expert's compute. Pending slots stay invisible
4780        // to cache hits until dispatch inserts the completion-event wait; current gate/up/down ids are
4781        // protected from eviction. This changes scheduling only, never the GGUF bytes or GEMM path.
4782        let page_window = moe_page_prefetch_window();
4783
4784        // 2. PER TOKEN: routed-expert loop. The ONE dispatch change vs Stage-1: a resident slot
4785        //    (cache HIT, no H2D) OR a staged slot (MISS) feeds the SAME unchanged qmatvec_view.
4786        for tok in 0..t {
4787            let sel = &sel_all[tok * n_used..(tok + 1) * n_used];
4788            let w = &w_all[tok * n_used..(tok + 1) * n_used];
4789            let zt = z.slice(tok * n_embd..(tok + 1) * n_embd); // CudaView<f32>
4790            let mut tok_q8: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
4791
4792            // STAGE-2 GROUPED DECODE (2026-07-04, MEMRA_MOE_GDEC default ON, =0 rollback): fold
4793            // this token's whole routed-expert FFN (8x gate/up/silu + 8x down/axpy = 40 launches)
4794            // into TWO launches via expert-pointer indirection over the fixed-address cache slots.
4795            // Fires only when ALL 3*n_used blocks are ALREADY cache-resident (pure-HIT: zero
4796            // memcpy, zero admission, so no slot can move under the collected pointers) — any
4797            // miss falls through to the sequential loop below, which admits as before. In steady
4798            // state on a fully-resident rig every token-layer takes the grouped path.
4799            // BIT-IDENTITY: each in-kernel dot reproduces qmatvec_f32's exact reduction; SiLU is
4800            // silu_mul_f32's exact expression; the down accumulation is a slot-ordered
4801            // __fmaf_rn chain == the sequential axpy_f32 chain (MEMRA_MOE_GDEC_GATE compares).
4802            // cfg.m3: the grouped kernels' fused epilogues are plain SiLU — M3's swigluoai must
4803            // NOT take them until the kernels grow the clamped variant. NVFP4 experts carry
4804            // per-expert macro-scales the fused kernels don't fold — those fall through too.
4805            let no_macros = m.gate_exps.macros.is_none()
4806                && m.up_exps.macros.is_none()
4807                && m.down_exps.macros.is_none();
4808            // SLAB-LOCAL FUSED PAIR (lane/pp-leverb 2026-08-08): the gdec launch pair
4809            // (moe_gate_up_silu8_q8 + moe_down8_fma_q8 — the EXACT kernels, same FP chains)
4810            // with pointers computed from the resident slab base + ex*stride instead of
4811            // collected SLRU slot addresses. No cache lock, no residency predicate — the
4812            // slab holds every expert by construction, so this arm never falls through
4813            // (gdec's P(all 24 resident) ≈ 0.37 coin-flip and the miss path's ~49-launch
4814            // staging both die). Bit-identity class: pointer provenance only, the same
4815            // slab-vs-SLRU equivalence moe_ffn_dev documents. Ordered BEFORE gdec: when a
4816            // slab exists it is strictly better (no lock, no miss).
4817            if slab_fused_may_fire {
4818                let (pg, pu, pd) = slab_bases.unwrap();
4819                let mut gp = [0u64; 8];
4820                let mut up = [0u64; 8];
4821                let mut dp = [0u64; 8];
4822                for (j, &ex) in sel.iter().enumerate() {
4823                    let ex = ex as usize;
4824                    gp[j] = pg + (ex * m.gate_exps.expert_stride) as u64;
4825                    up[j] = pu + (ex * m.up_exps.expert_stride) as u64;
4826                    dp[j] = pd + (ex * m.down_exps.expert_stride) as u64;
4827                }
4828                let mut wv = [0f32; 8];
4829                wv[..n_used].copy_from_slice(w);
4830                if tok_q8.is_none() {
4831                    tok_q8 = Some(e.quantize_q8_1_view(&zt, 1, n_embd)?);
4832                }
4833                let (zq, zd) = tok_q8.as_ref().unwrap();
4834                let act = e.moe_gate_up_silu8_q8(
4835                    crate::WPtr8(gp),
4836                    crate::WPtr8(up),
4837                    zq,
4838                    zd,
4839                    n_embd,
4840                    n_ff_exp,
4841                    n_used,
4842                    m.gate_exps.qtype,
4843                    m.up_exps.qtype,
4844                    m.gate_exps.row_bytes,
4845                    m.up_exps.row_bytes,
4846                )?;
4847                let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
4848                let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
4849                e.moe_down8_fma_q8(
4850                    crate::WPtr8(dp),
4851                    crate::F32x8(wv),
4852                    &aq2,
4853                    &ad2,
4854                    &mut dst,
4855                    n_ff_exp,
4856                    n_embd,
4857                    n_used,
4858                    m.down_exps.qtype,
4859                    m.down_exps.row_bytes,
4860                )?;
4861                continue;
4862            }
4863            if gdec_may_fire && moe_q8 && cfg.m3.is_none() && no_macros {
4864                if tok_q8.is_none() {
4865                    tok_q8 = Some(e.quantize_q8_1_view(&zt, 1, n_embd)?);
4866                }
4867                let (zq, zd) = tok_q8.as_ref().unwrap();
4868                if Self::moe_gdec_token_q8(
4869                    e,
4870                    m,
4871                    il,
4872                    max_block,
4873                    zq,
4874                    zd,
4875                    sel,
4876                    w,
4877                    &mut moe_out,
4878                    tok,
4879                    n_embd,
4880                    n_ff_exp,
4881                    n_used,
4882                )? {
4883                    continue;
4884                }
4885            } else if gdec_may_fire
4886                && cfg.m3.is_none()
4887                && no_macros
4888                && Self::moe_gdec_token(
4889                    e,
4890                    m,
4891                    il,
4892                    max_block,
4893                    &zt,
4894                    sel,
4895                    w,
4896                    &mut moe_out,
4897                    tok,
4898                    n_embd,
4899                    n_ff_exp,
4900                    n_used,
4901                )?
4902            {
4903                continue;
4904            }
4905
4906            // STAGE 2 memset-elision invariant: moe_out was allocated UNINIT when gdec or the
4907            // slab pair could fire. This token fell through to a sequential axpy loop, which
4908            // ACCUMULATES — zero its row first (row-sized memset; other rows are owned by the
4909            // full-overwrite arms). slab_fused_may_fire never actually reaches here (its arm
4910            // has no fallible predicate), included for the allocation invariant's symmetry.
4911            if gdec_may_fire || slab_fused_may_fire {
4912                let mut row = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
4913                e.memset_zeros_view(&mut row)?;
4914            }
4915
4916            // Fiddler-style split at whole-expert granularity. Partial residency is deliberately a
4917            // CPU assignment: transferring the missing projection would reintroduce the PCIe/NVMe
4918            // stall this path exists to remove, while mixing projections would require another
4919            // activation round-trip. Weight addresses remain valid until this worker is joined at
4920            // the bottom of the token scope.
4921            let mut cpu_mask = vec![false; sel.len()];
4922            let cpu_worker = if let Some(host_input) = cpu_input.as_ref() {
4923                let gpu_resident = if use_cache {
4924                    e.with_moe_cache(max_block, |cache, _| {
4925                        Ok(sel
4926                            .iter()
4927                            .map(|&expert| {
4928                                let expert = expert as u16;
4929                                [PROJ_GATE, PROJ_UP, PROJ_DOWN]
4930                                    .into_iter()
4931                                    .filter(|&projection| {
4932                                        cache
4933                                            .resident(BlockId::new(il, projection, expert))
4934                                            .is_some()
4935                                    })
4936                                    .count()
4937                            })
4938                            .collect::<Vec<_>>())
4939                    })?
4940                } else {
4941                    vec![0; sel.len()]
4942                };
4943                let mut cpu_selected = Vec::new();
4944                for (index, (&expert, &route_weight)) in sel.iter().zip(w).enumerate() {
4945                    if gpu_resident[index] != 3 {
4946                        cpu_mask[index] = true;
4947                        crate::cpu_experts::record_incomplete_gpu_residency(gpu_resident[index]);
4948                        let expert = expert as usize;
4949                        cpu_selected.push((expert, route_weight));
4950                    }
4951                }
4952                if crate::cpu_experts::predictor_enabled() {
4953                    // Fire-and-forget lookahead: the predictor worker scores layers il+1..
4954                    // from this layer's MoE input and prefetches predicted-and-missing
4955                    // experts into the companion RAM cache. Never blocks this thread.
4956                    let row = &host_input[tok * n_embd..(tok + 1) * n_embd];
4957                    crate::cpu_experts::predictor_submit(il, row);
4958                }
4959                if cpu_selected.is_empty() {
4960                    None
4961                } else {
4962                    let row = &host_input[tok * n_embd..(tok + 1) * n_embd];
4963                    let job = crate::cpu_experts::prepare_job(m, il, &cpu_selected, row)
4964                        .map_err(std::io::Error::other)?;
4965                    Some(crate::cpu_experts::submit(job).map_err(std::io::Error::other)?)
4966                }
4967            } else {
4968                None
4969            };
4970
4971            let worker_window = worker_disk_prefetch
4972                .then(worker_prefetch_window)
4973                .unwrap_or(0);
4974            for (j, &ex) in sel.iter().enumerate() {
4975                if cpu_mask[j] {
4976                    continue;
4977                }
4978                let ex = ex as usize;
4979                // PER-EXPERT SLAB READ (lane/pp-leverb 2026-08-08): the layers the fused
4980                // pair above excludes — step35's CLAMPED layers 43/44 (ffn_act_lim has no
4981                // fused form) and macro-carrying artifacts — still have their bytes in the
4982                // local resident slab. Same kernels as the SLRU arms (`qmatvec_expert_q8` /
4983                // `qmatvec_view`), same ffn_act_lim/macro folds; provenance = slab base +
4984                // ex*stride. No dispatch lock, no admission, no prefetch — nothing to miss.
4985                if let Some(d) = slab_local {
4986                    let gl = m.gate_exps.expert_layout(ex);
4987                    let ul = m.up_exps.expert_layout(ex);
4988                    let dl = m.down_exps.expert_layout(ex);
4989                    let (g0, u0, d0) = (
4990                        ex * m.gate_exps.expert_stride,
4991                        ex * m.up_exps.expert_stride,
4992                        ex * m.down_exps.expert_stride,
4993                    );
4994                    let (gate, up) = if moe_q8 {
4995                        if tok_q8.is_none() {
4996                            tok_q8 = Some(e.quantize_q8_1_view(&zt, 1, n_embd)?);
4997                        }
4998                        let (zq, zd) = tok_q8.as_ref().unwrap();
4999                        (
5000                            e.qmatvec_expert_q8(
5001                                &d.gate,
5002                                g0..g0 + gl.len,
5003                                zq,
5004                                zd,
5005                                1,
5006                                m.gate_exps.in_f,
5007                                m.gate_exps.out_f,
5008                                gl.qtype,
5009                                gl.row_bytes,
5010                            )?,
5011                            e.qmatvec_expert_q8(
5012                                &d.up,
5013                                u0..u0 + ul.len,
5014                                zq,
5015                                zd,
5016                                1,
5017                                m.up_exps.in_f,
5018                                m.up_exps.out_f,
5019                                ul.qtype,
5020                                ul.row_bytes,
5021                            )?,
5022                        )
5023                    } else {
5024                        (
5025                            e.qmatvec_view(
5026                                &d.gate,
5027                                g0..g0 + gl.len,
5028                                &zt,
5029                                1,
5030                                m.gate_exps.in_f,
5031                                m.gate_exps.out_f,
5032                                gl.qtype,
5033                                gl.row_bytes,
5034                            )?,
5035                            e.qmatvec_view(
5036                                &d.up,
5037                                u0..u0 + ul.len,
5038                                &zt,
5039                                1,
5040                                m.up_exps.in_f,
5041                                m.up_exps.out_f,
5042                                ul.qtype,
5043                                ul.row_bytes,
5044                            )?,
5045                        )
5046                    };
5047                    let mut act = e.uninit(n_ff_exp)?;
5048                    Self::ffn_act_lim(
5049                        e,
5050                        cfg,
5051                        &gate,
5052                        &up,
5053                        m.gate_exps.macro_scale(ex),
5054                        m.up_exps.macro_scale(ex),
5055                        lim_exp,
5056                        &mut act,
5057                        n_ff_exp,
5058                    )?;
5059                    let y = if moe_q8 {
5060                        let (aq2, ad2) = e.quantize_q8_1(&act, 1, n_ff_exp)?;
5061                        e.qmatvec_expert_q8(
5062                            &d.down,
5063                            d0..d0 + dl.len,
5064                            &aq2,
5065                            &ad2,
5066                            1,
5067                            m.down_exps.in_f,
5068                            m.down_exps.out_f,
5069                            dl.qtype,
5070                            dl.row_bytes,
5071                        )?
5072                    } else {
5073                        let actv = act.slice(0..n_ff_exp);
5074                        e.qmatvec_view(
5075                            &d.down,
5076                            d0..d0 + dl.len,
5077                            &actv,
5078                            1,
5079                            m.down_exps.in_f,
5080                            m.down_exps.out_f,
5081                            dl.qtype,
5082                            dl.row_bytes,
5083                        )?
5084                    };
5085                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
5086                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
5087                    continue;
5088                }
5089                for next in page_prefetch_positions(j, sel.len(), page_window) {
5090                    Self::moe_prefetch_host_expert(sel[next] as usize, m);
5091                }
5092                let keep = [
5093                    crate::moe_cache::BlockId::new(il, crate::moe_cache::PROJ_GATE, ex as u16),
5094                    crate::moe_cache::BlockId::new(il, crate::moe_cache::PROJ_UP, ex as u16),
5095                    crate::moe_cache::BlockId::new(il, crate::moe_cache::PROJ_DOWN, ex as u16),
5096                ];
5097                if worker_disk_prefetch && worker_window > 0 {
5098                    for next in worker_prefetch_positions(j, sel.len(), worker_window) {
5099                        Self::moe_prefetch_disk_expert(
5100                            e,
5101                            il,
5102                            sel[next] as usize,
5103                            m,
5104                            max_block,
5105                            &keep,
5106                        )?;
5107                    }
5108                } else if cache_dispatch
5109                    && !cpu_hybrid
5110                    && moe_prefetch_enabled()
5111                    && j + 1 < sel.len()
5112                {
5113                    let next = sel[j + 1] as usize;
5114                    Self::moe_prefetch_expert(e, il, next, m, max_block, &keep)?;
5115                }
5116                let [gate_q8, up_q8, down_q8] = [moe_q8; 3];
5117                if cache_dispatch && (gate_q8 || up_q8 || down_q8) {
5118                    // dp4a EXPERT PATH (MEMRA_MOE_Q8): quantize z-row once per token. Mixed expert
5119                    // layouts stay on the metadata-aware f32 path.
5120                    if (gate_q8 || up_q8) && tok_q8.is_none() {
5121                        tok_q8 = Some(e.quantize_q8_1_view(&zt, 1, n_embd)?);
5122                    }
5123                    let gate = if gate_q8 {
5124                        let (zq, zd) = tok_q8.as_ref().unwrap();
5125                        Self::moe_cached_gemm_q8(e, il, PROJ_GATE, ex, m, max_block, zq, zd)?
5126                    } else {
5127                        Self::moe_cached_gemm(e, il, PROJ_GATE, ex, m, max_block, &zt)?
5128                    };
5129                    let up = if up_q8 {
5130                        let (zq, zd) = tok_q8.as_ref().unwrap();
5131                        Self::moe_cached_gemm_q8(e, il, PROJ_UP, ex, m, max_block, zq, zd)?
5132                    } else {
5133                        Self::moe_cached_gemm(e, il, PROJ_UP, ex, m, max_block, &zt)?
5134                    };
5135                    let mut act = e.uninit(n_ff_exp)?;
5136                    Self::ffn_act_lim(
5137                        e,
5138                        cfg,
5139                        &gate,
5140                        &up,
5141                        m.gate_exps.macro_scale(ex),
5142                        m.up_exps.macro_scale(ex),
5143                        lim_exp,
5144                        &mut act,
5145                        n_ff_exp,
5146                    )?;
5147                    let y = if down_q8 {
5148                        let (aq2, ad2) = e.quantize_q8_1(&act, 1, n_ff_exp)?;
5149                        Self::moe_cached_gemm_q8(e, il, PROJ_DOWN, ex, m, max_block, &aq2, &ad2)?
5150                    } else {
5151                        let actv = act.slice(0..n_ff_exp);
5152                        Self::moe_cached_gemm(e, il, PROJ_DOWN, ex, m, max_block, &actv)?
5153                    };
5154                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
5155                    // down-proj macro folds into the accumulate weight (1.0 for non-macro archs).
5156                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
5157                } else if cache_dispatch {
5158                    // SLRU residency cache: per-projection, dispatch the block (HIT => resident slot,
5159                    // MISS => staged slot) then run the SAME unchanged qmatvec_view from that slot.
5160                    // The bytes the kernel reads are byte-for-byte the same GGUF block (§B.3); the
5161                    // only difference between HIT and MISS is whether the memcpy_htod ran.
5162                    let gate = Self::moe_cached_gemm(e, il, PROJ_GATE, ex, m, max_block, &zt)?;
5163                    let up = Self::moe_cached_gemm(e, il, PROJ_UP, ex, m, max_block, &zt)?;
5164                    let mut act = e.uninit(n_ff_exp)?; // activation fully overwrites
5165                    Self::ffn_act_lim(
5166                        e,
5167                        cfg,
5168                        &gate,
5169                        &up,
5170                        m.gate_exps.macro_scale(ex),
5171                        m.up_exps.macro_scale(ex),
5172                        lim_exp,
5173                        &mut act,
5174                        n_ff_exp,
5175                    )?;
5176                    let actv = act.slice(0..n_ff_exp);
5177                    let y = Self::moe_cached_gemm(e, il, PROJ_DOWN, ex, m, max_block, &actv)?;
5178                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
5179                    // down-proj macro folds into the accumulate weight (post-matmul linear scale).
5180                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
5181                } else if cache_frozen {
5182                    // A later prompt prime must not change the CPU/GPU assignment frozen after the
5183                    // first prime. Reuse every fixed resident projection directly and stage only a
5184                    // true miss through the ordinary scratch slot. This preserves the established
5185                    // f32-dequant numeric path while avoiding a full-bank reread on every prime.
5186                    let gate = Self::moe_frozen_gemm(
5187                        e,
5188                        il,
5189                        PROJ_GATE,
5190                        ex,
5191                        m,
5192                        max_block,
5193                        &zt,
5194                        &mut scratch_g,
5195                        g_len,
5196                    )?;
5197                    let up = Self::moe_frozen_gemm(
5198                        e,
5199                        il,
5200                        PROJ_UP,
5201                        ex,
5202                        m,
5203                        max_block,
5204                        &zt,
5205                        &mut scratch_u,
5206                        u_len,
5207                    )?;
5208                    let mut act = e.uninit(n_ff_exp)?;
5209                    Self::ffn_act_lim(
5210                        e,
5211                        cfg,
5212                        &gate,
5213                        &up,
5214                        m.gate_exps.macro_scale(ex),
5215                        m.up_exps.macro_scale(ex),
5216                        lim_exp,
5217                        &mut act,
5218                        n_ff_exp,
5219                    )?;
5220                    let actv = act.slice(0..n_ff_exp);
5221                    let y = Self::moe_frozen_gemm(
5222                        e,
5223                        il,
5224                        PROJ_DOWN,
5225                        ex,
5226                        m,
5227                        max_block,
5228                        &actv,
5229                        &mut scratch_d,
5230                        d_len,
5231                    )?;
5232                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
5233                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
5234                } else {
5235                    // Stage-1: stage gate/up/down for expert `ex` into the scratch slots, then GEMM.
5236                    // Lazy scratch: first no-cache expert allocates the 3 slots (uninit — stage_expert
5237                    // fully overwrites the byte range the GEMM reads).
5238                    if scratch_g.is_none() {
5239                        scratch_g = Some(e.alloc_u8_uninit(g_len)?);
5240                        scratch_u = Some(e.alloc_u8_uninit(u_len)?);
5241                        scratch_d = Some(e.alloc_u8_uninit(d_len)?);
5242                    }
5243                    let (sg, su, sd) = (
5244                        scratch_g.as_mut().unwrap(),
5245                        scratch_u.as_mut().unwrap(),
5246                        scratch_d.as_mut().unwrap(),
5247                    );
5248                    let gl = m.gate_exps.expert_layout(ex);
5249                    let ul = m.up_exps.expert_layout(ex);
5250                    let dl = m.down_exps.expert_layout(ex);
5251                    e.stage_expert(m.gate_exps.expert_bytes(ex), sg, 0)?;
5252                    let gate = e.qmatvec_view(
5253                        sg,
5254                        0..gl.len,
5255                        &zt,
5256                        1,
5257                        m.gate_exps.in_f,
5258                        m.gate_exps.out_f,
5259                        gl.qtype,
5260                        gl.row_bytes,
5261                    )?;
5262
5263                    e.stage_expert(m.up_exps.expert_bytes(ex), su, 0)?;
5264                    let up = e.qmatvec_view(
5265                        su,
5266                        0..ul.len,
5267                        &zt,
5268                        1,
5269                        m.up_exps.in_f,
5270                        m.up_exps.out_f,
5271                        ul.qtype,
5272                        ul.row_bytes,
5273                    )?;
5274
5275                    let mut act = e.uninit(n_ff_exp)?; // activation fully overwrites
5276                    Self::ffn_act_lim(
5277                        e,
5278                        cfg,
5279                        &gate,
5280                        &up,
5281                        m.gate_exps.macro_scale(ex),
5282                        m.up_exps.macro_scale(ex),
5283                        lim_exp,
5284                        &mut act,
5285                        n_ff_exp,
5286                    )?;
5287
5288                    e.stage_expert(m.down_exps.expert_bytes(ex), sd, 0)?;
5289                    let actv = act.slice(0..n_ff_exp);
5290                    let y = e.qmatvec_view(
5291                        sd,
5292                        0..dl.len,
5293                        &actv,
5294                        1,
5295                        m.down_exps.in_f,
5296                        m.down_exps.out_f,
5297                        dl.qtype,
5298                        dl.row_bytes,
5299                    )?;
5300
5301                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
5302                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
5303                }
5304            }
5305            if let Some(worker) = cpu_worker {
5306                let cpu_output = worker.wait().map_err(std::io::Error::other)?;
5307                let cpu_output = e.htod(&cpu_output)?;
5308                let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
5309                e.axpy_into(&cpu_output, 1.0, &mut dst, n_embd)?;
5310            }
5311            if cpu_hybrid && !cache_frozen && cpu_expert_profile_admit_enabled() {
5312                for (j, &ex) in sel.iter().enumerate() {
5313                    if cpu_mask[j] {
5314                        Self::moe_profile_admit_expert(e, il, ex as usize, m, max_block)?;
5315                    }
5316                }
5317            }
5318        }
5319
5320        // 3. SHARED EXPERT (ALWAYS-ON, no routing) on the SAME z — qwen35moe only. OLMoE and most
5321        //    vanilla MoE have NO shared expert (the shexp tensors are absent / `None`); skip it then.
5322        // gate_inp_shexp is OPTIONAL: qwen35moe gates the shared expert (sigmoid(gate_inp) x sh);
5323        // MiniMax-M3 (DeepSeek-V3 class) has NO shexp gate — the shared expert adds directly.
5324        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
5325            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
5326        {
5327            let n_ff_sh = gate_shexp.out_features(); // 512
5328            // Q8 TRUNK-FUSION (decode t=1): gate_shexp+up_shexp are Q8_0 same-shape on the 35B —
5329            // ONE fused2 launch (also folds the two per-matmul re-quantizes of z into one).
5330            // Bit-identical per (tensor,row); falls back to the two matmul calls when ineligible.
5331            // Small-t (spec verify 2..15) rides matmul_decode_exact so shexp FP chains match the
5332            // t==1 decode chain per column (cuBLASLt n-dependence + dp4a-vs-mmvq class); real
5333            // prefill keeps the batched matmul. Activation routes through ffn_act (SiLU for
5334            // softmax archs, clamped swigluoai for M3 — identical to silu_mul when cfg.m3 is None).
5335            let verify_t = t > 1 && t < PRIME_MIN_T;
5336            let (sg_gate, sg_up) = if t == 1 {
5337                match e.matmul_q8_fused2_x(gate_shexp, up_shexp, z)? {
5338                    Some(pair) => pair,
5339                    None => (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?),
5340                }
5341            } else if verify_t {
5342                (
5343                    e.matmul_decode_exact(gate_shexp, z, t)?,
5344                    e.matmul_decode_exact(up_shexp, z, t)?,
5345                )
5346            } else {
5347                (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?) // [T, 512] each
5348            };
5349            let mut sa = e.uninit(t * n_ff_sh)?; // activation fully overwrites
5350            Self::ffn_act_lim(
5351                e,
5352                cfg,
5353                &sg_gate,
5354                &sg_up,
5355                1.0,
5356                1.0,
5357                lim_shexp,
5358                &mut sa,
5359                t * n_ff_sh,
5360            )?;
5361            let sh = if verify_t {
5362                e.matmul_decode_exact(down_shexp, &sa, t)?
5363            } else {
5364                e.matmul(down_shexp, &sa, t)?
5365            }; // [T, n_embd]
5366
5367            // shexp gate: qwen35moe sigmoid-gates via ffn_gate_inp_shexp (1-D ne=[n_embd] ->
5368            // out_f=1); M3 has no gate tensor -> weight 1.0. Decode + verify ride the fused
5369            // sigmoid-dot kernel (one fold order for both chains; kills the per-layer
5370            // cuBLASLt m=1 splitK GEMM — 40x/step, ~10% of the H100 q35 decode step).
5371            // SERVE ISOLATION (lane/concat-prime-exact, 2026-08-02): PREFILL rides it too.
5372            // This out_f=1 cuBLASLt GEMV is the SECOND m-dependent op in the trunk (probed:
5373            // rows [0,19) move by 1.07e-4 between m=74 and m=75 while sigmoid_dot_rows is
5374            // BIT-IDENTICAL — allw-shexpgate-o35b.log). The gate multiplies the shared
5375            // expert's contribution into every token's residual, so under cross-request
5376            // concat prefill a session's hidden state depended on its co-arrivals' token
5377            // count. MEMRA_ROUTER_PREFILL_EXACT=0 reverts to the batched cuBLASLt linear.
5378            let g = match &m.gate_inp_shexp {
5379                Some(gate_inp_shexp) => {
5380                    if t < PRIME_MIN_T || crate::router_prefill_exact_on() {
5381                        e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
5382                    } else {
5383                        let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
5384                        let mut g = e.uninit(t)?; // sigmoid fully overwrites
5385                        e.sigmoid(&gs, &mut g, t)?;
5386                        g
5387                    }
5388                }
5389                None => e.htod(&vec![1.0f32; t])?,
5390            };
5391            // moe_out[r, :] += sh[r, :] * g[r]   (per-token scalar gate; g=1 ungated)
5392            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
5393        }
5394
5395        Ok(moe_out)
5396    }
5397
5398    /// Stage-1 (no-cache) per-DECODE-TOKEN H2D bytes: every routed block re-staged every layer every
5399    /// token = sum over MoE layers of n_used * (gate+up+down expert_stride). The §D.4 PCIe baseline.
5400    pub fn stage1_h2d_per_token(&self) -> u64 {
5401        use crate::hybrid::Ffn;
5402        let n_used = self
5403            .cfg
5404            .moe
5405            .as_ref()
5406            .map(|m| m.expert_used_count as u64)
5407            .unwrap_or(0);
5408        let mut bytes = 0u64;
5409        for l in self.layers.iter() {
5410            if let Ffn::Moe(m) = &l.ffn {
5411                bytes += n_used
5412                    * (m.gate_exps.max_expert_bytes()
5413                        + m.up_exps.max_expert_bytes()
5414                        + m.down_exps.max_expert_bytes()) as u64;
5415            }
5416        }
5417        bytes
5418    }
5419
5420    /// Largest expert block (bytes) across ALL MoE layers + the MTP head — the fixed cache slot size.
5421    /// UD/dynamic GGUFs quant different layers differently, so `expert_stride` varies per layer; the
5422    /// residency cache slots are fixed-address and must fit any block, so size to this global max.
5423    pub(crate) fn max_moe_block(&self) -> usize {
5424        use crate::hybrid::Ffn;
5425        let mut mx = 0usize;
5426        let mut scan = |ffn: &Ffn| {
5427            if let Ffn::Moe(m) = ffn {
5428                mx = mx
5429                    .max(m.gate_exps.max_expert_bytes())
5430                    .max(m.up_exps.max_expert_bytes())
5431                    .max(m.down_exps.max_expert_bytes());
5432            }
5433        };
5434        for l in self.layers.iter() {
5435            scan(&l.ffn);
5436        }
5437        if let Some(mtp) = self.mtp.as_ref() {
5438            scan(&mtp.ffn);
5439        }
5440        mx
5441    }
5442
5443    /// Exact retained projection lengths for cache class sizing. Pruned ids keep router positions
5444    /// but have no bytes and therefore consume no residency slot.
5445    pub(crate) fn moe_cache_block_sizes(&self) -> Vec<usize> {
5446        use crate::hybrid::Ffn;
5447        let mut sizes = Vec::new();
5448        let mut scan = |ffn: &Ffn| {
5449            let Ffn::Moe(m) = ffn else { return };
5450            for ex in 0..m.gate_exps.n_expert {
5451                if m.active_experts.as_ref().is_some_and(|active| !active[ex]) {
5452                    continue;
5453                }
5454                for exps in [&m.gate_exps, &m.up_exps, &m.down_exps] {
5455                    let len = exps.expert_layout(ex).len;
5456                    if len > 0 {
5457                        sizes.push(len);
5458                    }
5459                }
5460            }
5461        };
5462        for layer in &self.layers {
5463            scan(&layer.ffn);
5464        }
5465        if let Some(mtp) = &self.mtp {
5466            scan(&mtp.ffn);
5467        }
5468        sizes
5469    }
5470
5471    /// Persist the frozen residency set so a later process can restage it directly and skip
5472    /// the profiling warmup. Plain text: a versioned header binding slot geometry, then one
5473    /// `layer proj ex` triple per line. A mismatched or stale profile is rejected at load
5474    /// (header check) or degrades to fewer restaged blocks (per-id checks); either way the
5475    /// post-freeze argmax gate still validates the serving assignment.
5476    pub fn save_cpu_expert_residency_profile(
5477        &self,
5478        e: &Engine,
5479        path: &std::path::Path,
5480    ) -> Result<(), Box<dyn std::error::Error>> {
5481        let Some(ids) = e.export_moe_residency() else {
5482            return Err("no MoE residency cache to persist".into());
5483        };
5484        let mut body = format!(
5485            "memra-freeze-profile v1 max_block={} blocks={}\n",
5486            self.max_moe_block(),
5487            ids.len()
5488        );
5489        for (layer, proj, ex) in &ids {
5490            body.push_str(&format!("{layer} {proj} {ex}\n"));
5491        }
5492        let tmp = path.with_extension("tmp");
5493        std::fs::write(&tmp, body)?;
5494        std::fs::rename(&tmp, path)?;
5495        println!(
5496            "[moe-cache] freeze profile saved: {} blocks -> {}",
5497            ids.len(),
5498            path.display()
5499        );
5500        Ok(())
5501    }
5502
5503    /// Restage a saved freeze profile and freeze immediately, skipping the profiling warmup.
5504    /// Returns false (leaving the cache untouched for a normal warmup) when the profile is
5505    /// missing or its header does not match this model's slot geometry.
5506    pub fn restore_cpu_expert_residency_profile(
5507        &self,
5508        e: &Engine,
5509        path: &std::path::Path,
5510    ) -> Result<bool, Box<dyn std::error::Error>> {
5511        use crate::hybrid::Ffn;
5512        use crate::moe_cache::BlockId;
5513        let Ok(content) = std::fs::read_to_string(path) else {
5514            return Ok(false);
5515        };
5516        let mut lines = content.lines();
5517        let Some(header) = lines.next() else {
5518            return Ok(false);
5519        };
5520        let expected = format!("memra-freeze-profile v1 max_block={}", self.max_moe_block());
5521        if !header.starts_with(&expected) {
5522            println!(
5523                "[moe-cache] freeze profile ignored (geometry mismatch): {}",
5524                path.display()
5525            );
5526            return Ok(false);
5527        }
5528        let mut by_layer: std::collections::HashMap<u16, Vec<BlockId>> =
5529            std::collections::HashMap::new();
5530        for line in lines {
5531            let mut fields = line.split_whitespace();
5532            let (Some(layer), Some(proj), Some(ex)) = (fields.next(), fields.next(), fields.next())
5533            else {
5534                continue;
5535            };
5536            let (Ok(layer), Ok(proj), Ok(ex)) =
5537                (layer.parse::<u16>(), proj.parse::<u8>(), ex.parse::<u16>())
5538            else {
5539                continue;
5540            };
5541            by_layer
5542                .entry(layer)
5543                .or_default()
5544                .push(BlockId::new(layer, proj, ex));
5545        }
5546        let requested: usize = by_layer.values().map(Vec::len).sum();
5547        if requested == 0 {
5548            return Ok(false);
5549        }
5550        let max_block = self.max_moe_block();
5551        let mut restaged = 0usize;
5552        let mut stage_layer =
5553            |layer_index: u16, ffn: &Ffn| -> Result<(), Box<dyn std::error::Error>> {
5554                let Ffn::Moe(m) = ffn else { return Ok(()) };
5555                let Some(ids) = by_layer.get(&layer_index) else {
5556                    return Ok(());
5557                };
5558                e.with_moe_cache(max_block, |cache, eng| {
5559                    for id in ids {
5560                        if cache.restage_block(*id, m, eng)? {
5561                            restaged += 1;
5562                        }
5563                    }
5564                    Ok(())
5565                })
5566            };
5567        for (index, layer) in self.layers.iter().enumerate() {
5568            stage_layer(index as u16, &layer.ffn)?;
5569        }
5570        if let Some(mtp) = self.mtp.as_ref() {
5571            stage_layer(u16::MAX, &mtp.ffn)?;
5572        }
5573        e.freeze_moe_cache();
5574        println!(
5575            "[moe-cache] freeze profile restored: {restaged}/{requested} blocks restaged from {}",
5576            path.display()
5577        );
5578        Ok(true)
5579    }
5580
5581    /// Freeze the heterogeneous CPU/GPU split after the caller's discarded profile warmup.
5582    pub fn freeze_cpu_expert_residency(
5583        &self,
5584        e: &Engine,
5585    ) -> Result<(), Box<dyn std::error::Error>> {
5586        e.freeze_moe_cache();
5587        Ok(())
5588    }
5589
5590    /// FFN activation dispatch: swigluoai (clamped, alpha/limit) when cfg.m3 says so, else the
5591    /// standard SiLU*up. One seam so every FFN site (dense, routed expert, shared expert) follows
5592    /// the model's activation exactly.
5593    ///
5594    /// NO-`il` FORM: cannot apply step35's PER-LAYER SwiGLU clamp. Only call it from a site whose
5595    /// layer provably has no live limit (dense-FFN layers, MTP blocks) — `ffn_act_lim` is the
5596    /// form for anything that can land on a clamped layer.
5597    pub fn ffn_act(
5598        e: &Engine,
5599        cfg: &ModelConfig,
5600        gate: &CudaSlice<f32>,
5601        up: &CudaSlice<f32>,
5602        act: &mut CudaSlice<f32>,
5603        n: usize,
5604    ) -> Result<(), Box<dyn std::error::Error>> {
5605        Self::ffn_act_scaled(e, cfg, gate, up, 1.0, 1.0, act, n)
5606    }
5607
5608    /// ffn_act with per-tensor post-matmul macro-scales folded in (gs/us == 1.0 -> identical
5609    /// float ops to ffn_act; used by the ModelOpt NVFP4 expert path where each expert tensor
5610    /// carries a `weight_scale_2`). Same no-`il` contract as `ffn_act`.
5611    #[allow(clippy::too_many_arguments)]
5612    pub(crate) fn ffn_act_scaled(
5613        e: &Engine,
5614        cfg: &ModelConfig,
5615        gate: &CudaSlice<f32>,
5616        up: &CudaSlice<f32>,
5617        gs: f32,
5618        us: f32,
5619        act: &mut CudaSlice<f32>,
5620        n: usize,
5621    ) -> Result<(), Box<dyn std::error::Error>> {
5622        Self::ffn_act_lim(e, cfg, gate, up, gs, us, None, act, n)
5623    }
5624
5625    /// ffn_act_scaled + step35's PER-LAYER clamped SwiGLU. `limit`:
5626    ///   * `None`   -> the unclamped dispatch (every arch except step35's layers 43-44).
5627    ///   * `Some(l)`-> `min(silu(gate*gs), l) * clamp(up*us, +-l)` (llama-graph.cpp:2146/1751,
5628    ///                 non-DEEPSEEK4 branch). Callers source it from `cfg.clamp_exp_at(il)`
5629    ///                 (routed experts) or `cfg.clamp_shexp_at(il)` (shared expert) — the two
5630    ///                 arrays are SEPARATE and a layer can have one without the other.
5631    /// The `> 1e-6` eps gate lives in `clamp_exp_at`/`clamp_shexp_at`, so a `Some` here is
5632    /// already known live.
5633    #[allow(clippy::too_many_arguments)]
5634    pub(crate) fn ffn_act_lim(
5635        e: &Engine,
5636        cfg: &ModelConfig,
5637        gate: &CudaSlice<f32>,
5638        up: &CudaSlice<f32>,
5639        gs: f32,
5640        us: f32,
5641        limit: Option<f32>,
5642        act: &mut CudaSlice<f32>,
5643        n: usize,
5644    ) -> Result<(), Box<dyn std::error::Error>> {
5645        if let Some(m3) = cfg.m3.as_ref() {
5646            debug_assert!(
5647                limit.is_none(),
5648                "m3 swigluoai and step35 clamp are different archs"
5649            );
5650            return e.swigluoai_mul_scaled(
5651                gate,
5652                up,
5653                gs,
5654                us,
5655                m3.swiglu_alpha,
5656                m3.swiglu_limit,
5657                act,
5658                n,
5659            );
5660        }
5661        if let Some(l) = limit {
5662            return e.swiglu_clamped_mul_scaled(gate, up, gs, us, l, act, n);
5663        }
5664        if gs == 1.0 && us == 1.0 {
5665            return e.silu_mul(gate, up, act, n);
5666        }
5667        e.silu_mul_scaled(gate, up, gs, us, act, n)
5668    }
5669
5670    /// Routing for the whole batch: returns (sel [T*n_used] expert ids, w [T*n_used] renorm weights),
5671    /// token-major. Default = the Stage-1 host path (dtoh logits, softmax-256, stable DESC top-k,
5672    /// renorm). MEMRA_FUSED_ROUTER = the device kernel (§A) which reproduces the same numerics; we
5673    /// still dtoh the tiny [T,n_used] sel/w buffers (64 B/token vs 1 KB/token) — the host loop
5674    /// indexes HostExps.bytes on the CPU to choose the DMA source (§A.2 output staging).
5675    fn moe_route(
5676        e: &Engine,
5677        logits: &CudaSlice<f32>,
5678        t: usize,
5679        n_expert: usize,
5680        n_used: usize,
5681    ) -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
5682        Self::moe_route_cfg(e, logits, t, n_expert, n_used, None)
5683    }
5684
5685    /// DeepSeek-V3-class sigmoid routing (Step-3.7, MiniMax-M3, Hy3, GLM-DSA). Reference:
5686    /// M3/Hy3 modeling code — scores = sigmoid(logits); selection over scores + expert bias
5687    /// (M3 `e_score_correction_bias` / Hy3 `expert_bias`, both surfaced as `exp_probs_b`);
5688    /// weights = un-biased scores of the selected experts, sum-normalized when `route_norm`,
5689    /// x scaling factor (M3 routed_scaling_factor 2.0 / Hy3 router_scaling_factor 2.826).
5690    /// Default uses one device kernel plus the same pinned sel/w readback contract as the fused
5691    /// softmax router. `MEMRA_SIG_ROUTER=0` restores the full-logit DtoH host oracle.
5692    #[allow(clippy::too_many_arguments)]
5693    fn moe_route_sigmoid_cfg(
5694        e: &Engine,
5695        logits: &CudaSlice<f32>,
5696        t: usize,
5697        n_expert: usize,
5698        n_used: usize,
5699        m: &MoeWeights,
5700        (sf, route_norm): (f32, bool),
5701    ) -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
5702        if sigmoid_router_enabled() {
5703            return e.moe_router_sigmoid_topk_host(
5704                logits,
5705                t,
5706                n_expert,
5707                n_used,
5708                m.active_count(),
5709                &m.exp_probs_b_dev,
5710                &m.active_experts_dev,
5711                sf,
5712                route_norm,
5713            );
5714        }
5715        let lg = e.dtoh(logits)?;
5716        Self::moe_route_sigmoid_host(
5717            &lg,
5718            t,
5719            n_expert,
5720            n_used,
5721            m.exp_probs_b.as_deref(),
5722            sf,
5723            route_norm,
5724            m.active_experts.as_deref(),
5725        )
5726    }
5727
5728    /// Softmax router for qwen35moe/OLMoE. An active-mask overlay retains the host oracle because
5729    /// the existing softmax device kernel has no mask input.
5730    fn moe_route_cfg(
5731        e: &Engine,
5732        logits: &CudaSlice<f32>,
5733        t: usize,
5734        n_expert: usize,
5735        n_used: usize,
5736        active: Option<&[bool]>,
5737    ) -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
5738        // LAUNCH-STRUCTURE STAGE 1 (2026-07-05): fused router DEFAULT ON (MEMRA_FUSED_ROUTER=0
5739        // rollback) via the single-sync pinned readback — softmax arch only.
5740        if active.is_none() && !matches!(std::env::var("MEMRA_FUSED_ROUTER").as_deref(), Ok("0")) {
5741            return e.moe_router_topk_host(logits, t, n_expert, n_used);
5742        }
5743        // Host oracle (the §D bit-identity reference).
5744        let lg = e.dtoh(logits)?; // [T*n_expert] host
5745        let mut sel = vec![0u32; t * n_used];
5746        let mut w_out = vec![0f32; t * n_used];
5747        for tok in 0..t {
5748            let row = &lg[tok * n_expert..(tok + 1) * n_expert];
5749            // softmax over ALL n_expert (stable: subtract max)
5750            let maxl = row
5751                .iter()
5752                .enumerate()
5753                .filter(|(i, _)| active.is_none_or(|mask| mask[*i]))
5754                .map(|(_, &x)| x)
5755                .fold(f32::NEG_INFINITY, f32::max);
5756            let mut probs = vec![0f32; n_expert];
5757            let mut den = 0f32;
5758            for i in 0..n_expert {
5759                if active.is_some_and(|mask| !mask[i]) {
5760                    continue;
5761                }
5762                let x = (row[i] - maxl).exp();
5763                probs[i] = x;
5764                den += x;
5765            }
5766            for p in probs.iter_mut() {
5767                *p /= den;
5768            }
5769            // stable DESC sort: prob DESC, ascending-index tiebreak.
5770            let mut idx: Vec<usize> = (0..n_expert)
5771                .filter(|&i| active.is_none_or(|mask| mask[i]))
5772                .collect();
5773            idx.sort_by(|&a, &b| probs[b].total_cmp(&probs[a]).then(a.cmp(&b)));
5774            let sl = &idx[..n_used];
5775            let mut wv: Vec<f32> = sl.iter().map(|&i| probs[i]).collect();
5776            let mut ws: f32 = wv.iter().sum();
5777            ws = ws.max(6.103515625e-5_f32); // F16 smallest normal, clamp BEFORE divide
5778            for x in wv.iter_mut() {
5779                *x /= ws;
5780            }
5781            for j in 0..n_used {
5782                sel[tok * n_used + j] = sl[j] as u32;
5783                w_out[tok * n_used + j] = wv[j];
5784            }
5785        }
5786        Ok((sel, w_out))
5787    }
5788
5789    #[allow(clippy::too_many_arguments)]
5790    fn moe_route_sigmoid_with_input(
5791        e: &Engine,
5792        logits: &CudaSlice<f32>,
5793        input: &CudaSlice<f32>,
5794        t: usize,
5795        n_expert: usize,
5796        n_used: usize,
5797        bias: Option<&[f32]>,
5798        (sf, route_norm): (f32, bool),
5799        active: Option<&[bool]>,
5800    ) -> Result<(Vec<u32>, Vec<f32>, Vec<f32>), Box<dyn std::error::Error>> {
5801        let (lg, input) = e.dtoh_pair(logits, input)?;
5802        let (sel, w) =
5803            Self::moe_route_sigmoid_host(&lg, t, n_expert, n_used, bias, sf, route_norm, active)?;
5804        Ok((sel, w, input))
5805    }
5806
5807    /// Start the prediction-guided prefetch worker (MEMRA_MOE_PREFETCH=depth). Call after
5808    /// residency freeze: the worker filters against a static snapshot of the frozen HBM set.
5809    /// Builds a fully-owned per-layer table (host router copies via one-time DtoH, bias,
5810    /// active mask, prebuilt projection descriptors) so no model reference escapes.
5811    pub fn start_moe_prefetch_predictor(
5812        &self,
5813        e: &Engine,
5814        cfg: &ModelConfig,
5815    ) -> Result<(), Box<dyn std::error::Error>> {
5816        use crate::hybrid::Ffn;
5817        let Some(sig) = cfg.sigmoid_router() else {
5818            return Err("prefetch predictor requires a sigmoid-router arch".into());
5819        };
5820        let resident: std::collections::HashSet<(u16, u8, u16)> = e
5821            .export_moe_residency()
5822            .ok_or("prefetch predictor needs the frozen MoE residency cache")?
5823            .into_iter()
5824            .collect();
5825        let mut layers = Vec::new();
5826        for (index, layer) in self.layers.iter().enumerate() {
5827            let Ffn::Moe(m) = &layer.ffn else { continue };
5828            let crate::model::GpuTensor::Float { data, .. } = &m.gate_inp else {
5829                continue;
5830            };
5831            let router = e.dtoh(data)?;
5832            let n_expert = m.gate_exps.n_expert;
5833            let n_embd = m.gate_exps.in_f;
5834            if router.len() != n_embd * n_expert {
5835                continue;
5836            }
5837            let build = |exps: &crate::model::HostExps| {
5838                (0..n_expert)
5839                    .map(|expert| crate::cpu_experts::predictor_projection(exps, expert))
5840                    .collect::<Vec<_>>()
5841            };
5842            layers.push((
5843                index as u16,
5844                crate::cpu_experts::PredictLayerInit {
5845                    router,
5846                    bias: m.exp_probs_b.clone(),
5847                    active: m.active_experts.clone(),
5848                    n_embd,
5849                    n_used: cfg
5850                        .moe
5851                        .as_ref()
5852                        .map(|moe| moe.expert_used_count as usize)
5853                        .ok_or("prefetch predictor requires MoE config")?,
5854                    sig,
5855                    weights_n_expert: n_expert,
5856                    gate: build(&m.gate_exps),
5857                    up: build(&m.up_exps),
5858                    down: build(&m.down_exps),
5859                },
5860            ));
5861        }
5862        crate::cpu_experts::start_prefetch_predictor(layers, resident).map_err(|error| error.into())
5863    }
5864
5865    /// Sigmoid-routing oracle shared by the prefetch predictor and `kernel-check`: identical
5866    /// selection math to the rollback runtime, applied to host-computed logits.
5867    #[allow(clippy::too_many_arguments)]
5868    pub fn moe_route_sigmoid_host_public(
5869        logits: &[f32],
5870        t: usize,
5871        n_expert: usize,
5872        n_used: usize,
5873        bias: Option<&[f32]>,
5874        sf: f32,
5875        route_norm: bool,
5876        active: Option<&[bool]>,
5877    ) -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
5878        Self::moe_route_sigmoid_host(logits, t, n_expert, n_used, bias, sf, route_norm, active)
5879    }
5880
5881    #[allow(clippy::too_many_arguments)]
5882    fn moe_route_sigmoid_host(
5883        lg: &[f32],
5884        t: usize,
5885        n_expert: usize,
5886        n_used: usize,
5887        bias: Option<&[f32]>,
5888        sf: f32,
5889        route_norm: bool,
5890        active: Option<&[bool]>,
5891    ) -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
5892        let active_count = active
5893            .map(|mask| mask.iter().filter(|&&enabled| enabled).count())
5894            .unwrap_or(n_expert);
5895        crate::sigrouter_contract::validate_active_count(n_used, active_count)?;
5896        if lg.len() != t * n_expert {
5897            return Err(format!(
5898                "sigmoid router logits length mismatch: got {}, expected {}",
5899                lg.len(),
5900                t * n_expert,
5901            )
5902            .into());
5903        }
5904        let mut sel = vec![0u32; t * n_used];
5905        let mut w_out = vec![0f32; t * n_used];
5906        for tok in 0..t {
5907            let row = &lg[tok * n_expert..(tok + 1) * n_expert];
5908            let scores: Vec<f32> = row.iter().map(|&x| 1.0 / (1.0 + (-x).exp())).collect();
5909            // selection score = sigmoid + bias; weight = plain sigmoid.
5910            let selsc: Vec<f32> = match bias {
5911                Some(b) => scores.iter().zip(b).map(|(s, bb)| s + bb).collect(),
5912                None => scores.clone(),
5913            };
5914            let mut idx: Vec<usize> = (0..n_expert)
5915                .filter(|&i| active.is_none_or(|mask| mask[i]))
5916                .collect();
5917            idx.sort_by(|&a, &b| selsc[b].total_cmp(&selsc[a]).then(a.cmp(&b)));
5918            let sl = &idx[..n_used];
5919            let mut wv: Vec<f32> = sl.iter().map(|&i| scores[i]).collect();
5920            if route_norm {
5921                let ws: f32 = wv.iter().sum::<f32>().max(1e-20);
5922                for x in wv.iter_mut() {
5923                    *x = *x / ws * sf;
5924                }
5925            } else {
5926                for x in wv.iter_mut() {
5927                    *x *= sf;
5928                }
5929            }
5930            for j in 0..n_used {
5931                sel[tok * n_used + j] = sl[j] as u32;
5932                w_out[tok * n_used + j] = wv[j];
5933            }
5934        }
5935        Ok((sel, w_out))
5936    }
5937
5938    /// Step-3.7 resident-slab dispatch: sigmoid top-k outputs remain on device and feed the
5939    /// existing slot-ordered q8 expert kernels directly. Mixed layouts, spill, remote slabs,
5940    /// macro-scaled experts, and observation modes are denied by the caller.
5941    #[allow(clippy::too_many_arguments)]
5942    fn moe_ffn_sigmoid_dev(
5943        e: &Engine,
5944        m: &MoeWeights,
5945        z: &CudaSlice<f32>,
5946        zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>,
5947        logits: &CudaSlice<f32>,
5948        t: usize,
5949        cfg: &ModelConfig,
5950        il: u16,
5951        (scaling_factor, route_norm): (f32, bool),
5952    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
5953        let moe = cfg.moe.as_ref().unwrap();
5954        let n_embd = cfg.n_embd as usize;
5955        let n_expert = moe.expert_count as usize;
5956        let n_used = moe.expert_used_count as usize;
5957        let n_ff_exp = moe.expert_ff_length as usize;
5958        let dev = m.dev_exps.as_ref().unwrap();
5959        debug_assert!(cfg.step35.is_some());
5960        debug_assert_eq!(dev.dev, e.ctx().ordinal());
5961        debug_assert!(m.has_uniform_expert_layout());
5962        debug_assert!(!m.has_macros);
5963
5964        let (sel_d, w_d) = e.moe_router_sigmoid_topk(
5965            logits,
5966            t,
5967            n_expert,
5968            n_used,
5969            m.active_count(),
5970            &m.exp_probs_b_dev,
5971            &m.active_experts_dev,
5972            scaling_factor,
5973            route_norm,
5974        )?;
5975        crate::moesd::record_device_routes(e, il, n_expert, n_used, &sel_d)?;
5976        let (gate_row_bytes, up_row_bytes) = if dev.gu_il {
5977            let combined = m.gate_exps.row_bytes + m.up_exps.row_bytes;
5978            (combined, combined)
5979        } else {
5980            (m.gate_exps.row_bytes, m.up_exps.row_bytes)
5981        };
5982        let (zq, zd) = match (t, zq8) {
5983            (1, Some((q, d))) => (q.clone(), d.clone()),
5984            _ => e.quantize_q8_1(z, t, n_embd)?,
5985        };
5986        let n_pairs = t * n_used;
5987        let mut moe_out = if cfg.clamp_exp_at(il as u32).is_some() {
5988            // The final Step layers retain the established separate gate/up -> clamp -> down
5989            // arithmetic. Pair rows are derived from token position; selected expert ids and
5990            // routing weights remain the device router's buffers throughout.
5991            let pair_tok: Vec<i32> = (0..n_pairs).map(|pair| (pair / n_used) as i32).collect();
5992            let pair_tok_d = e.htod_i32(&pair_tok)?;
5993            let gate = e.moe_pairs_matvec_q8(
5994                &dev.ptr_row,
5995                0,
5996                &pair_tok_d,
5997                &sel_d,
5998                &zq,
5999                &zd,
6000                n_embd,
6001                n_ff_exp,
6002                n_expert,
6003                n_pairs,
6004                m.gate_exps.qtype,
6005                gate_row_bytes,
6006            )?;
6007            let up = e.moe_pairs_matvec_q8(
6008                &dev.ptr_row,
6009                1,
6010                &pair_tok_d,
6011                &sel_d,
6012                &zq,
6013                &zd,
6014                n_embd,
6015                n_ff_exp,
6016                n_expert,
6017                n_pairs,
6018                m.up_exps.qtype,
6019                up_row_bytes,
6020            )?;
6021            let mut act = e.uninit(n_pairs * n_ff_exp)?;
6022            Self::ffn_act_lim(
6023                e,
6024                cfg,
6025                &gate,
6026                &up,
6027                1.0,
6028                1.0,
6029                cfg.clamp_exp_at(il as u32),
6030                &mut act,
6031                n_pairs * n_ff_exp,
6032            )?;
6033            let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
6034            let pair_self: Vec<i32> = (0..n_pairs as i32).collect();
6035            let pair_self_d = e.htod_i32(&pair_self)?;
6036            let down = e.moe_pairs_matvec_q8(
6037                &dev.ptr_row,
6038                2,
6039                &pair_self_d,
6040                &sel_d,
6041                &aq2,
6042                &ad2,
6043                n_ff_exp,
6044                n_embd,
6045                n_expert,
6046                n_pairs,
6047                m.down_exps.qtype,
6048                m.down_exps.row_bytes,
6049            )?;
6050            let tok_off: Vec<i32> = (0..=t).map(|tok| (tok * n_used) as i32).collect();
6051            let tok_ids: Vec<i32> = (0..n_pairs as i32).collect();
6052            let tok_off_d = e.htod_i32(&tok_off)?;
6053            let tok_ids_d = e.htod_i32(&tok_ids)?;
6054            let mut output = e.uninit(t * n_embd)?;
6055            e.moe_pairs_scatter(&down, &w_d, &tok_off_d, &tok_ids_d, &mut output, t, n_embd)?;
6056            output
6057        } else {
6058            let act = e.moe_gate_up_silu8_dev_q8_rows(
6059                &dev.ptr_row,
6060                &sel_d,
6061                &zq,
6062                &zd,
6063                t,
6064                n_embd,
6065                n_ff_exp,
6066                n_used,
6067                n_expert,
6068                m.gate_exps.qtype,
6069                m.up_exps.qtype,
6070                gate_row_bytes,
6071                up_row_bytes,
6072                &m.dev_macros,
6073            )?;
6074            let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
6075            let mut output = e.uninit(t * n_embd)?;
6076            e.moe_down8_fma_dev_q8_rows_g(
6077                &dev.ptr_row,
6078                &sel_d,
6079                &w_d,
6080                &aq2,
6081                &ad2,
6082                &mut output,
6083                t,
6084                n_ff_exp,
6085                n_embd,
6086                n_used,
6087                n_expert,
6088                m.down_exps.qtype,
6089                m.down_exps.row_bytes,
6090            )?;
6091            output
6092        };
6093
6094        if std::env::var("MEMRA_SIG_ROUTER_DISPATCH_TRACE").as_deref() == Ok("1") {
6095            eprintln!(
6096                "[sigrouter-dev] layer={il} tokens={t} experts={n_expert} used={n_used} clamp={} gu_il={}",
6097                cfg.clamp_exp_at(il as u32).is_some(),
6098                dev.gu_il,
6099            );
6100        }
6101        Self::moe_ffn_grouped_add_shared(e, m, z, t, cfg, il, &mut moe_out)?;
6102        Ok(moe_out)
6103    }
6104
6105    /// LAUNCH-STRUCTURE STAGE 3: the ZERO-DtoH fully-resident MoE FFN. Caller guarantees the
6106    /// layer's device pointer row exists (checked under the cache lock). Router top-k runs on
6107    /// device; sel/w are consumed by the `_dev` matvec twins directly; NOTHING crosses PCIe.
6108    /// Same numerics as the fused-router + gdec chain (kernel-level bit-identity, see the
6109    /// MoE PREFILL PAIR-BATCH: host routing (sel/w like the sequential path), then 5 launches
6110    /// TOTAL per layer (quantize z, gate-pairs, up-pairs, silu, act-quantize, down-pairs,
6111    /// scatter) regardless of T or expert count. Bit-identity class: per (pair,row) dot =
6112    /// qmatvec_expert_q8 order; per-token accumulation slot-ordered (scatter kernel).
6113    fn moe_ffn_pairs(
6114        e: &Engine,
6115        m: &MoeWeights,
6116        z: &CudaSlice<f32>,
6117        logits: &CudaSlice<f32>,
6118        t: usize,
6119        cfg: &ModelConfig,
6120    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6121        let moe = cfg.moe.as_ref().unwrap();
6122        let n_embd = cfg.n_embd as usize;
6123        let n_expert = moe.expert_count as usize;
6124        let n_used = moe.expert_used_count as usize;
6125        let n_ff_exp = moe.expert_ff_length as usize;
6126        // This arm has no `il` and every kernel in it fuses PLAIN silu(gate)*up, so a clamped
6127        // model must never reach it. The caller's gate at `moe_ffn_sequential_zq8` denies per
6128        // layer via `swiglu_clamped_at(il)`; assert the whole-model form here so a future caller
6129        // that forgets the gate fails loudly in debug instead of returning wrong logits.
6130        debug_assert!(
6131            !cfg.swiglu_clamped_anywhere(),
6132            "moe_ffn_pairs has no per-layer clamp: fused epilogues are plain SiLU"
6133        );
6134        let dev = m.dev_exps.as_ref().unwrap();
6135        // WALL-GAP ARC: interleaved gate/up slab strides (see moe_ffn_dev).
6136        let (rbg_d, rbu_d) = if dev.gu_il {
6137            let sxx = m.gate_exps.row_bytes + m.up_exps.row_bytes;
6138            (sxx, sxx)
6139        } else {
6140            (m.gate_exps.row_bytes, m.up_exps.row_bytes)
6141        };
6142
6143        let (sel_all, w_all) = Self::moe_route(e, logits, t, n_expert, n_used)?;
6144        let n_pairs = t * n_used;
6145        // pair arrays: pair p = (token p/n_used, slot p%n_used) — ALREADY slot-ordered per token,
6146        // so the CSR is trivial: tok_pair_off[tok] = tok*n_used, ids identity.
6147        let pair_tok: Vec<i32> = (0..n_pairs).map(|p| (p / n_used) as i32).collect();
6148        let pair_ex: Vec<i32> = sel_all.iter().map(|&x| x as i32).collect();
6149        let pair_w: Vec<f32> = w_all.clone();
6150        let tok_off: Vec<i32> = (0..=t).map(|tok| (tok * n_used) as i32).collect();
6151        let tok_ids: Vec<i32> = (0..n_pairs as i32).collect();
6152        let pt = e.htod_i32(&pair_tok)?;
6153        let px = e.htod_i32(&pair_ex)?;
6154        let pw = e.htod(&pair_w)?;
6155        let toff = e.htod_i32(&tok_off)?;
6156        let tids = e.htod_i32(&tok_ids)?;
6157
6158        // z quantized ONCE for all tokens; gate/up pair matvecs; silu; act quantize; down; scatter.
6159        // EXPERT-MAJOR CSR (rung 2): pairs grouped by expert -> the kernel reuses each weight
6160        // row across the expert's token group (llama-MMQ's core win). Host grouping is O(pairs).
6161        let mut by_ex: Vec<Vec<i32>> = vec![Vec::new(); n_expert];
6162        for p in 0..n_pairs {
6163            by_ex[pair_ex[p] as usize].push(p as i32);
6164        }
6165        let mut ex_ids: Vec<i32> = Vec::new();
6166        let mut ex_off: Vec<i32> = vec![0];
6167        let mut ex_pairs: Vec<i32> = Vec::with_capacity(n_pairs);
6168        for (ex, list) in by_ex.iter().enumerate() {
6169            if list.is_empty() {
6170                continue;
6171            }
6172            ex_ids.push(ex as i32);
6173            ex_pairs.extend_from_slice(list);
6174            ex_off.push(ex_pairs.len() as i32);
6175        }
6176        let n_active = ex_ids.len();
6177        let exi = e.htod_i32(&ex_ids)?;
6178        let exo = e.htod_i32(&ex_off)?;
6179        let exp_d = e.htod_i32(&ex_pairs)?;
6180        let _ = &px; // pair-major twin keeps it; em path uses CSR
6181
6182        // INT8-MMA EXPERT MMQ (MEMRA_MOE_MMA=1, opt-in): the m16n8k16.s8 tensor-core analog of the
6183        // _dec dp4a kernel (cu/mmq_iq_experts.cu). Same CSR grouping; per-expert matvec runs as a
6184        // 128x128-tile int8 MMA GEMM over the expert's token group. Weight IQ nibbles decode to int8
6185        // at tile-load + per-32 float scale; activation is q8_1_mmq (D4, same quant class as dp4a).
6186        // FP-ORDER differs from dp4a (MMA reduction) — logits SHIFT, gated on argmax/spec/closeness,
6187        // NOT byte-identity (like the W4A8 path). Requires IQ3_S/IQ4_XS + in_f % 256 == 0.
6188        // t >= 16 (GEMM_M-class rule): the MMA tile needs token volume (crossover ~200 tok/expert;
6189        // microbench: dp4a wins at tiny groups). ALSO an exactness requirement — spec verify
6190        // batches (t=2..K+2) must ride the dp4a path whose FP order matches the T=1 decode chain,
6191        // else K=1 self-consistency FAILs (caught 2026-07-06: MMA at T=2 flipped a verify argmax).
6192        // DEFAULT ON (2026-07-06, third flip — this time with the real culprit fixed): the
6193        // "MMA prime breaks spec" failure was the ROUTER's cuBLASLt n-dependence (d994271),
6194        // not MMA's own FP order — both this and the k-quant arms were innocent suspects whose
6195        // margin shifts surfaced the router bug. With the router decode-exact at verify t, the
6196        // full battery is green with MMA on (spec p1/p2/p3 PASS, raw K=1..8 PASS, argmax MATCH,
6197        // pp6257 2862 = 2.1x dec). t>=16 floor still required: verify batches must ride dp4a
6198        // (dispatch parity with the T=1 decode chain). MEMRA_MOE_MMA=0 rollback;
6199        // MEMRA_MOE_MMA_T overrides the floor (bisect seam).
6200        static MMA_T: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
6201        let mma_t = *MMA_T.get_or_init(|| {
6202            std::env::var("MEMRA_MOE_MMA_T")
6203                .ok()
6204                .and_then(|v| v.parse().ok())
6205                .unwrap_or(16)
6206        });
6207        let use_mma = std::env::var("MEMRA_MOE_MMA")
6208            .map(|v| v != "0")
6209            .unwrap_or(true)
6210            && t >= mma_t
6211            && q8_expert_dec_supported(m.gate_exps.qtype)
6212            && q8_expert_dec_supported(m.up_exps.qtype)
6213            && q8_expert_dec_supported(m.down_exps.qtype)
6214            && n_embd % 256 == 0
6215            && n_ff_exp % 256 == 0;
6216        // GROUPED f16 LANE admission (MEMRA_MOE_F16G, rounds 46-49): its own door, no longer a
6217        // subset of the MMQ arm — q35's k-quant stragglers (Q6_K/Q4_K down, one Q3_K gate/up
6218        // layer) fail q8_expert_dec_supported but dequant fine to f16, so f16g must be able to
6219        // take a layer the MMQ arm would reject. Same t >= mma_t floor as MMA: decode and
6220        // spec-verify batches must ride the dp4a path whose FP order matches the T=1 chain.
6221        // MODE 2 (sm_120a naked default, lane/f16g-default-rearb 2026-08-02): every layer
6222        // whose three projections pass f16g_proj_ok rides the sk visitor with direct tile
6223        // loaders — with IQ4_XS/IQ3_S direct coverage the sk arm beats the int8-MMA MMQ
6224        // tiles on the IQ-bank models too (q35 board-2048 +33.9%, KAT pp512 +46.7%).
6225        // AUTO-KQUANT (mode 3, MEMRA_MOE_F16G=3, lane/q4k-expert-prefill): admit f16g ONLY
6226        // where the MMA arm can't take the layer's QTYPES — the k-quant expert class whose
6227        // baseline is the per-pair _em fallback (Ornith-35B Q4_K: board-2048 3.14x). Its
6228        // MMA-capable carve-out (IQ3_S/IQ4_XS/Q4_0 banks on MMQ) was priced pre-IQ-direct;
6229        // it survives as the rollback seam. Keyed on qtype capability, NOT use_mma, so
6230        // MEMRA_MOE_MMA=0 stays a pure dp4a rollback seam.
6231        let mma_capable = q8_expert_dec_supported(m.gate_exps.qtype)
6232            && q8_expert_dec_supported(m.up_exps.qtype)
6233            && q8_expert_dec_supported(m.down_exps.qtype)
6234            && n_embd % 256 == 0
6235            && n_ff_exp % 256 == 0;
6236        let f16g_mode = crate::moe_f16g_mode();
6237        let f16g = f16g_mode != 0
6238            && t >= mma_t
6239            && (f16g_mode != 3 || !mma_capable)
6240            && f16g_proj_ok(m.gate_exps.qtype, n_embd)
6241            && f16g_proj_ok(m.up_exps.qtype, n_embd)
6242            && f16g_proj_ok(m.down_exps.qtype, n_ff_exp);
6243        if use_mma || f16g {
6244            // GROUPED f16 LANE (MEMRA_MOE_F16G, rounds 46-49, experimental door): dequant
6245            // the active experts ONCE per projection to f16 and run one grouped f16 GEMM over
6246            // the CSR groups. f16-mirror numeric class — argmax/spec gated before promotion.
6247            // NOTE: the pair-gather kernel needs pair p ordered EXPERT-MAJOR (ex_pairs order);
6248            // the y rows come back in that same CSR order, so gate/up/down all stay pair-major
6249            // in ex_pairs order — but moe_pairs_silu_mul and the scatter consume PAIR-ID order.
6250            // We therefore gather activations per ex_pairs and scatter y back through ex_pairs.
6251            let y_down = if f16g {
6252                // CSR order end-to-end: gather z rows by the pair's TOKEN (pair p's token is
6253                // p / n_used — the trivial CSR above), silu in CSR order (elementwise), one
6254                // permute at the very end back to pair-id order for the scatter.
6255                let csr_tok: Vec<i32> = ex_pairs.iter().map(|&p| p / n_used as i32).collect();
6256                let csr_tok_d = e.htod_i32(&csr_tok)?;
6257                let (z_f16, z_s) = e.moe_f16g_act(z, Some(&csr_tok_d), n_embd, n_pairs)?;
6258                let g_csr = e.moe_f16_grouped(
6259                    &dev.ptr_row,
6260                    0,
6261                    n_expert,
6262                    &exi,
6263                    &ex_off,
6264                    &exo,
6265                    &z_f16,
6266                    &z_s,
6267                    n_embd,
6268                    n_ff_exp,
6269                    n_active,
6270                    n_pairs,
6271                    m.gate_exps.qtype,
6272                    rbg_d,
6273                )?;
6274                let u_csr = e.moe_f16_grouped(
6275                    &dev.ptr_row,
6276                    1,
6277                    n_expert,
6278                    &exi,
6279                    &ex_off,
6280                    &exo,
6281                    &z_f16,
6282                    &z_s,
6283                    n_embd,
6284                    n_ff_exp,
6285                    n_active,
6286                    n_pairs,
6287                    m.up_exps.qtype,
6288                    rbu_d,
6289                )?;
6290                let act_csr = e.moe_pairs_silu_mul(&g_csr, &u_csr, n_pairs * n_ff_exp)?;
6291                let (a_f16, a_s) = e.moe_f16g_act(&act_csr, None, n_ff_exp, n_pairs)?;
6292                let d_csr = e.moe_f16_grouped(
6293                    &dev.ptr_row,
6294                    2,
6295                    n_expert,
6296                    &exi,
6297                    &ex_off,
6298                    &exo,
6299                    &a_f16,
6300                    &a_s,
6301                    n_ff_exp,
6302                    n_embd,
6303                    n_active,
6304                    n_pairs,
6305                    m.down_exps.qtype,
6306                    m.down_exps.row_bytes,
6307                )?;
6308                e.rows_permute(&d_csr, &exp_d, n_pairs, n_embd)?
6309            } else {
6310                // gate/up: activation = z, token-major over t tokens; pair_tok gathers the routed row.
6311                let z_scr = e.mmq_iq_quantize_act(z, n_embd, t)?;
6312                let gate = e.mmq_iq_experts(
6313                    &dev.ptr_row,
6314                    0,
6315                    n_expert,
6316                    &exi,
6317                    &exo,
6318                    &exp_d,
6319                    &pt,
6320                    &z_scr,
6321                    n_embd,
6322                    n_ff_exp,
6323                    n_active,
6324                    n_pairs,
6325                    t,
6326                    m.gate_exps.qtype,
6327                    rbg_d,
6328                )?;
6329                let up = e.mmq_iq_experts(
6330                    &dev.ptr_row,
6331                    1,
6332                    n_expert,
6333                    &exi,
6334                    &exo,
6335                    &exp_d,
6336                    &pt,
6337                    &z_scr,
6338                    n_embd,
6339                    n_ff_exp,
6340                    n_active,
6341                    n_pairs,
6342                    t,
6343                    m.up_exps.qtype,
6344                    rbu_d,
6345                )?;
6346                // down: activation = silu(gate)*up, pair-major [n_pairs, n_ff_exp]; pair_tok =
6347                // identity. FUSED ACT-EPILOGUE (default on): one launch computes the activation in
6348                // registers and writes ONLY the quantized scratch — the two-pass chain
6349                // (moe_pairs_silu_mul writes act f32, mmq_iq_quantize_act re-reads it) is the
6350                // MEMRA_MOE_FUSE_ACTQ=0 rollback. Scratch bytes are BYTE-IDENTICAL (kernel-check).
6351                let a_scr = if crate::moe_fuse_actq_on() {
6352                    e.mmq_iq_fused_act_quant(&gate, &up, n_ff_exp, n_pairs, 0)?
6353                } else {
6354                    let act = e.moe_pairs_silu_mul(&gate, &up, n_pairs * n_ff_exp)?;
6355                    e.mmq_iq_quantize_act(&act, n_ff_exp, n_pairs)?
6356                };
6357                let pair_self: Vec<i32> = (0..n_pairs as i32).collect();
6358                let pself = e.htod_i32(&pair_self)?;
6359                e.mmq_iq_experts(
6360                    &dev.ptr_row,
6361                    2,
6362                    n_expert,
6363                    &exi,
6364                    &exo,
6365                    &exp_d,
6366                    &pself,
6367                    &a_scr,
6368                    n_ff_exp,
6369                    n_embd,
6370                    n_active,
6371                    n_pairs,
6372                    n_pairs,
6373                    m.down_exps.qtype,
6374                    m.down_exps.row_bytes,
6375                )?
6376            };
6377            let mut moe_out = e.uninit(t * n_embd)?;
6378            e.moe_pairs_scatter(&y_down, &pw, &toff, &tids, &mut moe_out, t, n_embd)?;
6379            if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
6380                (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
6381            {
6382                let n_ff_sh = gate_shexp.out_features();
6383                let sg_gate = e.matmul(gate_shexp, z, t)?;
6384                let sg_up = e.matmul(up_shexp, z, t)?;
6385                let mut sa = e.uninit(t * n_ff_sh)?;
6386                Self::ffn_act(e, cfg, &sg_gate, &sg_up, &mut sa, t * n_ff_sh)?;
6387                let sh = e.matmul(down_shexp, &sa, t)?;
6388                // shexp gate: qwen35moe sigmoid-gates; M3 has no gate tensor -> weight 1.0.
6389                // SERVE ISOLATION (lane/concat-prime-exact): the fused sigmoid-dot is the
6390                // m-INVARIANT form — see the sequential arm's note. This is the PAIRS arm,
6391                // i.e. the one real prefill actually takes on a resident-expert MoE model,
6392                // so the concat-prime isolation fix has to land here as well.
6393                let g = match &m.gate_inp_shexp {
6394                    Some(gate_inp_shexp) if crate::router_prefill_exact_on() => {
6395                        e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
6396                    }
6397                    Some(gate_inp_shexp) => {
6398                        let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
6399                        let mut g = e.uninit(t)?;
6400                        e.sigmoid(&gs, &mut g, t)?;
6401                        g
6402                    }
6403                    None => e.htod(&vec![1.0f32; t])?,
6404                };
6405                e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
6406            }
6407            return Ok(moe_out);
6408        }
6409
6410        // DECODE-ONCE MMQ (rung 3, MEMRA_MOE_DEC=1 default-on): dequant each weight group once per
6411        // (row,group) then dp4a across the expert's tokens. _em re-decoded per token (NEUTRAL).
6412        let dec = std::env::var("MEMRA_MOE_DEC")
6413            .map(|v| v != "0")
6414            .unwrap_or(true);
6415        let matvec = |proj,
6416                      exi: &_,
6417                      exo: &_,
6418                      exp_d: &_,
6419                      pt: &_,
6420                      aq: &_,
6421                      ad: &_,
6422                      inf,
6423                      outf,
6424                      qtype,
6425                      rb|
6426         -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6427            // _dec's decode-once extractors are IQ-only; k-quant expert layers take the _em dot path.
6428            let dec = dec && q8_expert_dec_supported(qtype);
6429            if dec {
6430                e.moe_pairs_matvec_q8_dec(
6431                    &dev.ptr_row,
6432                    proj,
6433                    exi,
6434                    exo,
6435                    exp_d,
6436                    pt,
6437                    aq,
6438                    ad,
6439                    inf,
6440                    outf,
6441                    n_expert,
6442                    n_active,
6443                    n_pairs,
6444                    qtype,
6445                    rb,
6446                )
6447            } else {
6448                e.moe_pairs_matvec_q8_em(
6449                    &dev.ptr_row,
6450                    proj,
6451                    exi,
6452                    exo,
6453                    exp_d,
6454                    pt,
6455                    aq,
6456                    ad,
6457                    inf,
6458                    outf,
6459                    n_expert,
6460                    n_active,
6461                    n_pairs,
6462                    qtype,
6463                    rb,
6464                )
6465            }
6466        };
6467        let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
6468        let gate = matvec(
6469            0,
6470            &exi,
6471            &exo,
6472            &exp_d,
6473            &pt,
6474            &zq,
6475            &zd,
6476            n_embd,
6477            n_ff_exp,
6478            m.gate_exps.qtype,
6479            rbg_d,
6480        )?;
6481        let up = matvec(
6482            1,
6483            &exi,
6484            &exo,
6485            &exp_d,
6486            &pt,
6487            &zq,
6488            &zd,
6489            n_embd,
6490            n_ff_exp,
6491            m.up_exps.qtype,
6492            rbu_d,
6493        )?;
6494        let act = e.moe_pairs_silu_mul(&gate, &up, n_pairs * n_ff_exp)?;
6495        let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
6496        // down consumes PAIR-major activation rows: pair_tok = identity.
6497        let pair_self: Vec<i32> = (0..n_pairs as i32).collect();
6498        let pself = e.htod_i32(&pair_self)?;
6499        let y_down = matvec(
6500            2,
6501            &exi,
6502            &exo,
6503            &exp_d,
6504            &pself,
6505            &aq2,
6506            &ad2,
6507            n_ff_exp,
6508            n_embd,
6509            m.down_exps.qtype,
6510            m.down_exps.row_bytes,
6511        )?;
6512        let mut moe_out = e.uninit(t * n_embd)?; // scatter fully overwrites per (token,col)
6513        e.moe_pairs_scatter(&y_down, &pw, &toff, &tids, &mut moe_out, t, n_embd)?;
6514
6515        // SHARED EXPERT epilogue — same as the other paths.
6516        // gate_inp_shexp is OPTIONAL: qwen35moe gates the shared expert (sigmoid(gate_inp) x sh);
6517        // MiniMax-M3 (DeepSeek-V3 class) has NO shexp gate — the shared expert adds directly.
6518        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
6519            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
6520        {
6521            let n_ff_sh = gate_shexp.out_features();
6522            // These decode-exact forms are required by the new Step resident arm. Keep the
6523            // established grouped shared-expert program for every other architecture: widening
6524            // this to Gemma changed its speculative acceptance despite green argmax gates.
6525            let step_exact = cfg.step35.is_some();
6526            let verify_t = step_exact && t > 1 && t < PRIME_MIN_T;
6527            let (sg_gate, sg_up) = if step_exact && t == 1 {
6528                match e.matmul_q8_fused2_x(gate_shexp, up_shexp, z)? {
6529                    Some(pair) => pair,
6530                    None => (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?),
6531                }
6532            } else if verify_t {
6533                let mut fused = None;
6534                if crate::spec::spec_fused_t()
6535                    && (2..=4).contains(&t)
6536                    && e.uses_q8_1_fast(gate_shexp)
6537                    && e.uses_q8_1_fast(up_shexp)
6538                {
6539                    let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
6540                    fused = e.matmul_q8_fused2_t(gate_shexp, up_shexp, &zq, &zd, t)?;
6541                }
6542                match fused {
6543                    Some(pair) => pair,
6544                    None => (
6545                        e.matmul_decode_exact(gate_shexp, z, t)?,
6546                        e.matmul_decode_exact(up_shexp, z, t)?,
6547                    ),
6548                }
6549            } else {
6550                (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?)
6551            };
6552            let mut sa = e.uninit(t * n_ff_sh)?;
6553            e.silu_mul(&sg_gate, &sg_up, &mut sa, t * n_ff_sh)?;
6554            let sh = if verify_t {
6555                e.matmul_decode_exact(down_shexp, &sa, t)?
6556            } else {
6557                e.matmul(down_shexp, &sa, t)?
6558            };
6559            // shexp gate: qwen35moe sigmoid-gates; M3 has no gate tensor -> weight 1.0.
6560            // m-INVARIANT fused sigmoid-dot under router_prefill_exact (serve isolation,
6561            // lane/concat-prime-exact) — every shexp-gate arm shares the same form so a
6562            // dispatch choice cannot change bits.
6563            let g = match &m.gate_inp_shexp {
6564                Some(gate_inp_shexp) if crate::router_prefill_exact_on() => {
6565                    e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
6566                }
6567                Some(gate_inp_shexp) => {
6568                    let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
6569                    let mut g = e.uninit(t)?;
6570                    e.sigmoid(&gs, &mut g, t)?;
6571                    g
6572                }
6573                None => e.htod(&vec![1.0f32; t])?,
6574            };
6575            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
6576        }
6577        Ok(moe_out)
6578    }
6579
6580    /// kernel headers); the shared-expert epilogue is byte-identical to moe_ffn_sequential's.
6581    #[allow(clippy::too_many_arguments)]
6582    #[allow(clippy::too_many_arguments)]
6583    fn moe_ffn_dev(
6584        e: &Engine,
6585        m: &MoeWeights,
6586        z: &CudaSlice<f32>,
6587        zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>,
6588        logits: &CudaSlice<f32>,
6589        t: usize,
6590        cfg: &ModelConfig,
6591        il: u16,
6592        max_block: usize,
6593    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6594        let moe = cfg.moe.as_ref().unwrap();
6595        let n_embd = cfg.n_embd as usize;
6596        let n_expert = moe.expert_count as usize;
6597        let n_used = moe.expert_used_count as usize;
6598        let n_ff_exp = moe.expert_ff_length as usize;
6599        // moe_router_topk is SOFTMAX-only (no exp_probs_b bias, no expert_weights_scale) and every
6600        // gate_up kernel here fuses PLAIN silu(gate)*up. `dev_ok` denies sigmoid-router archs and
6601        // clamped layers; assert both so a future caller that skips the gate fails loudly.
6602        debug_assert!(
6603            cfg.sigmoid_router().is_none(),
6604            "moe_ffn_dev routes SOFTMAX: a sigmoid-router arch would pick wrong experts"
6605        );
6606        debug_assert!(
6607            !cfg.swiglu_clamped_at(il as u32),
6608            "moe_ffn_dev's fused epilogue is plain SiLU: no clamped form"
6609        );
6610
6611        // device top-k: sel [t, n_used] i32, w [t, n_used] f32 — stays on device.
6612        let (sel_d, mut w_d) = e.moe_router_topk(logits, t, n_expert, n_used)?;
6613        // Down-projection macro fold (compressed-tensors NVFP4 artifacts): one tiny launch,
6614        // skipped entirely for macro-free experts (every k-quant GGUF).
6615        if m.has_macros {
6616            e.moe_w_scale_by_expert(&mut w_d, &sel_d, &m.dev_macros, n_expert, t * n_used)?;
6617        }
6618
6619        // moe_out rows are FULLY overwritten by moe_down8_fma_dev — uninit (stage-2 rule).
6620        let mut moe_out = e.uninit(t * n_embd)?;
6621
6622        // RESIDENT-EXPERTS arm: the pointer row comes from the load-time slab (no cache, no
6623        // lock). Same kernels/loop as the SLRU arm below — only the row's provenance differs.
6624        if let Some(dev) = m.dev_exps.as_ref() {
6625            // WALL-GAP ARC: interleaved gate/up slab (MEMRA_MOE_GU_IL) -> both projections use
6626            // the combined stride; up's base is offset in the ptr table. Down unchanged.
6627            let (rbg_d, rbu_d) = if dev.gu_il {
6628                let sxx = m.gate_exps.row_bytes + m.up_exps.row_bytes;
6629                (sxx, sxx)
6630            } else {
6631                (m.gate_exps.row_bytes, m.up_exps.row_bytes)
6632            };
6633            let q8 = moe_q8_enabled()
6634                && q8_expert_supported(m.gate_exps.qtype)
6635                && q8_expert_supported(m.up_exps.qtype)
6636                && q8_expert_supported(m.down_exps.qtype);
6637            // SMALL-M ROWS ARM (MEMRA_SPEC_M2, lane/spec-m2): batch the verify token loop —
6638            // ONE batched z-quantize + ONE gate_up rows launch + ONE act quantize + ONE down
6639            // rows launch (4 launches/layer, was 4t). BIT-IDENTICAL per token to the serial
6640            // loop below (rows twins = the _v/w8h2v per-token programs on a grid.z token axis;
6641            // quantize_q8_1 is per-32-block row-independent). Gated to the AUTO kernel modes —
6642            // a custom MEMRA_MOE_DEVQ8_GU/DOWN diagnostic run keeps the serial loop so the
6643            // dispatched kernel stays exactly the env-selected one — and to the w8h2v shape
6644            // (n_ff_exp==512, n_used<=8), the same contract the AUTO down dispatch keys on.
6645            let rows_arm = q8
6646                && t > 1
6647                && crate::spec::spec_m2()
6648                && n_ff_exp == 512
6649                && n_used <= 8
6650                && std::env::var("MEMRA_MOE_DEVQ8_GU")
6651                    .map(|v| v.is_empty() || v == "v")
6652                    .unwrap_or(true)
6653                && std::env::var("MEMRA_MOE_DEVQ8_DOWN")
6654                    .map(|v| v.is_empty() || v == "w8h2v")
6655                    .unwrap_or(true);
6656            // CSR EXPERT-DEDUP gate_up (verify-cost target #1, DEFAULT ON 2026-07-10): the
6657            // owner-scan kernel serves every (token, slot) pair of an expert from ONE block,
6658            // deduping the 38-40% duplicated weight-stream+decode the overlap probe measured
6659            // (gate_up 55.0 -> 39.7us/launch; +1.3-2.1% spec e2e p2, +0.6-1.7% p3, all K).
6660            // Bit-identical to the _rows twins (explicit-intrinsic accumulate — the ULP/fmad
6661            // lesson; =2 byte-compare verified zero diffs). down stays on _rows (CSR down
6662            // measured 23.5 -> 37.5us: 16-group rows can't amortize the serial pair loop).
6663            // MEMRA_MOE_CSR=0 rollback; =2 runs BOTH paths and byte-compares (debug).
6664            let csr_mode = std::env::var("MEMRA_MOE_CSR")
6665                .ok()
6666                .and_then(|v| v.parse::<i32>().ok())
6667                .unwrap_or(1);
6668            let csr_qt = |qt: i32| qt == crate::QT_IQ4_XS || qt == crate::QT_IQ3_S;
6669            let csr_arm = rows_arm
6670                && csr_mode > 0
6671                && t <= 10
6672                && csr_qt(m.gate_exps.qtype)
6673                && csr_qt(m.up_exps.qtype)
6674                && csr_qt(m.down_exps.qtype);
6675            if csr_arm {
6676                if csr_mode == 2 {
6677                    static ENGAGED: std::sync::Once = std::sync::Once::new();
6678                    ENGAGED.call_once(|| eprintln!("[memra] moe CSR byte-compare mode ON (t={t})"));
6679                }
6680                let n_pairs = t * n_used;
6681                let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
6682                let act = e.moe_gate_up_silu8_dev_q8_csr(
6683                    &dev.ptr_row,
6684                    &sel_d,
6685                    &zq,
6686                    &zd,
6687                    n_pairs,
6688                    n_embd,
6689                    n_ff_exp,
6690                    n_used,
6691                    n_expert,
6692                    m.gate_exps.qtype,
6693                    m.up_exps.qtype,
6694                    rbg_d,
6695                    rbu_d,
6696                )?;
6697                let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
6698                // down stays on the _rows twin — BOTH CSR down variants measured negative
6699                // (v1 serial pairs 23.5->37.5us; v2 warp-parallel+SMEM -14% e2e, K=8 -37%):
6700                // 16-group rows have too little decode to amortize any dedup structure.
6701                e.moe_down8_fma_dev_q8_rows(
6702                    &dev.ptr_row,
6703                    &sel_d,
6704                    &w_d,
6705                    &aq2,
6706                    &ad2,
6707                    &mut moe_out,
6708                    t,
6709                    n_ff_exp,
6710                    n_embd,
6711                    n_used,
6712                    n_expert,
6713                    m.down_exps.qtype,
6714                    m.down_exps.row_bytes,
6715                )?;
6716                if csr_mode == 2 {
6717                    // DEBUG BYTE-COMPARE: run the _rows twins on the same inputs, diff bits.
6718                    let act_r = e.moe_gate_up_silu8_dev_q8_rows(
6719                        &dev.ptr_row,
6720                        &sel_d,
6721                        &zq,
6722                        &zd,
6723                        t,
6724                        n_embd,
6725                        n_ff_exp,
6726                        n_used,
6727                        n_expert,
6728                        m.gate_exps.qtype,
6729                        m.up_exps.qtype,
6730                        rbg_d,
6731                        rbu_d,
6732                        &m.dev_macros,
6733                    )?;
6734                    let mut out_r = e.uninit(t * n_embd)?;
6735                    let (aq2r, ad2r) = e.quantize_q8_1(&act_r, n_pairs, n_ff_exp)?;
6736                    e.moe_down8_fma_dev_q8_rows(
6737                        &dev.ptr_row,
6738                        &sel_d,
6739                        &w_d,
6740                        &aq2r,
6741                        &ad2r,
6742                        &mut out_r,
6743                        t,
6744                        n_ff_exp,
6745                        n_embd,
6746                        n_used,
6747                        n_expert,
6748                        m.down_exps.qtype,
6749                        m.down_exps.row_bytes,
6750                    )?;
6751                    let (a1, a2) = (e.dtoh(&act)?, e.dtoh(&act_r)?);
6752                    let (o1, o2) = (e.dtoh(&moe_out)?, e.dtoh(&out_r)?);
6753                    let ba = a1
6754                        .iter()
6755                        .zip(&a2)
6756                        .filter(|(x, y)| x.to_bits() != y.to_bits())
6757                        .count();
6758                    let bo = o1
6759                        .iter()
6760                        .zip(&o2)
6761                        .filter(|(x, y)| x.to_bits() != y.to_bits())
6762                        .count();
6763                    if ba + bo > 0 {
6764                        eprintln!(
6765                            "[csr-check] il={il} t={t} ACT diffs={ba}/{} OUT diffs={bo}/{}",
6766                            a1.len(),
6767                            o1.len()
6768                        );
6769                        // First 4 differing ACT elements: (pair, o, csr, rows) + that pair's expert
6770                        let sel_h = e.dtoh_i32(&sel_d)?;
6771                        let mut shown = 0;
6772                        for (i, (x, y)) in a1.iter().zip(&a2).enumerate() {
6773                            if x.to_bits() != y.to_bits() && shown < 4 {
6774                                let (p, o) = (i / n_ff_exp, i % n_ff_exp);
6775                                let ex = sel_h[p];
6776                                let npx = sel_h.iter().filter(|&&v| v == ex).count();
6777                                eprintln!(
6778                                    "  ACT p={p} ex={ex} np={npx} o={o} csr={x:e} rows={y:e}"
6779                                );
6780                                shown += 1;
6781                            }
6782                        }
6783                        std::process::exit(3);
6784                    }
6785                }
6786            } else if rows_arm {
6787                // TEMP PROBE (MEMRA_MOE_OVERLAP=1): cross-token expert-activation overlap at
6788                // verify — sizes the CSR dedup win (unique experts vs t*n_used pairs).
6789                if std::env::var("MEMRA_MOE_OVERLAP").as_deref() == Ok("1") {
6790                    use std::sync::atomic::{AtomicU64, Ordering};
6791                    static PAIRS: AtomicU64 = AtomicU64::new(0);
6792                    static UNIQ: AtomicU64 = AtomicU64::new(0);
6793                    static CALLS: AtomicU64 = AtomicU64::new(0);
6794                    let sel_h = e.dtoh_i32(&sel_d)?;
6795                    let mut u: Vec<i32> = sel_h.clone();
6796                    u.sort_unstable();
6797                    u.dedup();
6798                    PAIRS.fetch_add(sel_h.len() as u64, Ordering::Relaxed);
6799                    UNIQ.fetch_add(u.len() as u64, Ordering::Relaxed);
6800                    let c = CALLS.fetch_add(1, Ordering::Relaxed) + 1;
6801                    if c % 480 == 0 {
6802                        let p = PAIRS.load(Ordering::Relaxed);
6803                        let q = UNIQ.load(Ordering::Relaxed);
6804                        eprintln!(
6805                            "[overlap] calls={c} pairs={p} unique={q} ratio={:.3} (t={t})",
6806                            q as f64 / p as f64
6807                        );
6808                    }
6809                }
6810                let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
6811                let act = e.moe_gate_up_silu8_dev_q8_rows(
6812                    &dev.ptr_row,
6813                    &sel_d,
6814                    &zq,
6815                    &zd,
6816                    t,
6817                    n_embd,
6818                    n_ff_exp,
6819                    n_used,
6820                    n_expert,
6821                    m.gate_exps.qtype,
6822                    m.up_exps.qtype,
6823                    rbg_d,
6824                    rbu_d,
6825                    &m.dev_macros,
6826                )?;
6827                let (aq2, ad2) = e.quantize_q8_1(&act, t * n_used, n_ff_exp)?;
6828                e.moe_down8_fma_dev_q8_rows(
6829                    &dev.ptr_row,
6830                    &sel_d,
6831                    &w_d,
6832                    &aq2,
6833                    &ad2,
6834                    &mut moe_out,
6835                    t,
6836                    n_ff_exp,
6837                    n_embd,
6838                    n_used,
6839                    n_expert,
6840                    m.down_exps.qtype,
6841                    m.down_exps.row_bytes,
6842                )?;
6843            } else {
6844                for tok in 0..t {
6845                    let zt = z.slice(tok * n_embd..(tok + 1) * n_embd);
6846                    let selt = sel_d.slice(tok * n_used..(tok + 1) * n_used);
6847                    let wt = w_d.slice(tok * n_used..(tok + 1) * n_used);
6848                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
6849                    if q8 {
6850                        let (zq, zd) = match (t, zq8) {
6851                            (1, Some((q, d))) => (q.clone(), d.clone()),
6852                            _ => e.quantize_q8_1_view(&zt, 1, n_embd)?,
6853                        };
6854                        let act = e.moe_gate_up_silu8_dev_q8(
6855                            &dev.ptr_row,
6856                            &selt,
6857                            &zq,
6858                            &zd,
6859                            n_embd,
6860                            n_ff_exp,
6861                            n_used,
6862                            n_expert,
6863                            m.gate_exps.qtype,
6864                            m.up_exps.qtype,
6865                            rbg_d,
6866                            rbu_d,
6867                            &m.dev_macros,
6868                        )?;
6869                        let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
6870                        e.moe_down8_fma_dev_q8(
6871                            &dev.ptr_row,
6872                            &selt,
6873                            &wt,
6874                            &aq2,
6875                            &ad2,
6876                            &mut dst,
6877                            n_ff_exp,
6878                            n_embd,
6879                            n_used,
6880                            n_expert,
6881                            m.down_exps.qtype,
6882                            m.down_exps.row_bytes,
6883                        )?;
6884                    } else {
6885                        let act = e.moe_gate_up_silu8_dev(
6886                            &dev.ptr_row,
6887                            &selt,
6888                            &zt,
6889                            n_embd,
6890                            n_ff_exp,
6891                            n_used,
6892                            n_expert,
6893                            m.gate_exps.qtype,
6894                            m.up_exps.qtype,
6895                            rbg_d,
6896                            rbu_d,
6897                            &m.dev_macros,
6898                        )?;
6899                        e.moe_down8_fma_dev(
6900                            &dev.ptr_row,
6901                            &selt,
6902                            &wt,
6903                            &act,
6904                            &mut dst,
6905                            n_ff_exp,
6906                            n_embd,
6907                            n_used,
6908                            n_expert,
6909                            m.down_exps.qtype,
6910                            m.down_exps.row_bytes,
6911                        )?;
6912                    }
6913                }
6914            }
6915        } else {
6916            // Launch under the cache lock: the row borrow lives as long as the closure, and the
6917            // lock covers only launch ISSUE (µs), same policy as moe_cached_gemm.
6918            // Q8 ARM PARITY (2026-07-06): the SLRU arm ran the f32-dequant _dev kernels only —
6919            // 80us/launch vs the q8 twins' 15us on the SAME shapes (fixed-build profile: 228
6920            // f32 launches = 36ms of the 64-tok window). Same q8 gate + kernels as the resident
6921            // arm above; MEMRA_MOE_Q8=0 restores the byte-identical f32 path.
6922            let q8 = moe_q8_enabled()
6923                && q8_expert_supported(m.gate_exps.qtype)
6924                && q8_expert_supported(m.up_exps.qtype)
6925                && q8_expert_supported(m.down_exps.qtype);
6926            e.with_moe_cache(max_block, |c, eng| {
6927                let row = c
6928                    .layer_dev_row(il, n_expert, eng)?
6929                    .ok_or("moe_ffn_dev: layer row vanished under the lock")?;
6930                for tok in 0..t {
6931                    let zt = z.slice(tok * n_embd..(tok + 1) * n_embd);
6932                    let selt = sel_d.slice(tok * n_used..(tok + 1) * n_used);
6933                    let wt = w_d.slice(tok * n_used..(tok + 1) * n_used);
6934                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
6935                    if q8 {
6936                        let (zq, zd) = match (t, zq8) {
6937                            (1, Some((q, d))) => (q.clone(), d.clone()),
6938                            _ => eng.quantize_q8_1_view(&zt, 1, n_embd)?,
6939                        };
6940                        let act = eng.moe_gate_up_silu8_dev_q8(
6941                            row,
6942                            &selt,
6943                            &zq,
6944                            &zd,
6945                            n_embd,
6946                            n_ff_exp,
6947                            n_used,
6948                            n_expert,
6949                            m.gate_exps.qtype,
6950                            m.up_exps.qtype,
6951                            m.gate_exps.row_bytes,
6952                            m.up_exps.row_bytes,
6953                            &m.dev_macros,
6954                        )?;
6955                        let (aq2, ad2) = eng.quantize_q8_1(&act, n_used, n_ff_exp)?;
6956                        eng.moe_down8_fma_dev_q8(
6957                            row,
6958                            &selt,
6959                            &wt,
6960                            &aq2,
6961                            &ad2,
6962                            &mut dst,
6963                            n_ff_exp,
6964                            n_embd,
6965                            n_used,
6966                            n_expert,
6967                            m.down_exps.qtype,
6968                            m.down_exps.row_bytes,
6969                        )?;
6970                    } else {
6971                        let act = eng.moe_gate_up_silu8_dev(
6972                            row,
6973                            &selt,
6974                            &zt,
6975                            n_embd,
6976                            n_ff_exp,
6977                            n_used,
6978                            n_expert,
6979                            m.gate_exps.qtype,
6980                            m.up_exps.qtype,
6981                            m.gate_exps.row_bytes,
6982                            m.up_exps.row_bytes,
6983                            &m.dev_macros,
6984                        )?;
6985                        eng.moe_down8_fma_dev(
6986                            row,
6987                            &selt,
6988                            &wt,
6989                            &act,
6990                            &mut dst,
6991                            n_ff_exp,
6992                            n_embd,
6993                            n_used,
6994                            n_expert,
6995                            m.down_exps.qtype,
6996                            m.down_exps.row_bytes,
6997                        )?;
6998                    }
6999                }
7000                // instrumentation parity with the host paths (3 blocks/expert-slot, all hits).
7001                c.hits += (t * 3 * n_used) as u64;
7002                Ok(())
7003            })?;
7004        }
7005
7006        // SHARED EXPERT epilogue — byte-identical to moe_ffn_sequential step 3 (incl. its Q8
7007        // TRUNK-FUSION arm: fused2 is bit-identical to the two matmul calls per (tensor,row)).
7008        // gate_inp_shexp is OPTIONAL: qwen35moe gates the shared expert (sigmoid(gate_inp) x sh);
7009        // MiniMax-M3 (DeepSeek-V3 class) has NO shexp gate — the shared expert adds directly.
7010        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
7011            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
7012        {
7013            let n_ff_sh = gate_shexp.out_features();
7014            // verify-t (2..15) decode-exact arm: this fn now serves the spec verify batches
7015            // (pairs gate moved to t>=PRIME_MIN_T), so the shexp chain must match t==1 per col.
7016            let verify_t = t > 1 && t < PRIME_MIN_T;
7017            let (sg_gate, sg_up) = if t == 1 {
7018                match e.matmul_q8_fused2_x(gate_shexp, up_shexp, z)? {
7019                    Some(pair) => pair,
7020                    None => (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?),
7021                }
7022            } else if verify_t {
7023                // VERIFY-TIER TRUNK FUSION (MEMRA_SPEC_FUSED_T, t=2-4): the shexp gate+up pair
7024                // rides one shared quantize + one fused2 batched launch instead of two
7025                // decode-exact calls. Bit-identical per (tensor,token,row) — see spec_fused_t.
7026                let mut fused = None;
7027                if crate::spec::spec_fused_t()
7028                    && (2..=4).contains(&t)
7029                    && e.uses_q8_1_fast(gate_shexp)
7030                    && e.uses_q8_1_fast(up_shexp)
7031                {
7032                    let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
7033                    fused = e.matmul_q8_fused2_t(gate_shexp, up_shexp, &zq, &zd, t)?;
7034                }
7035                match fused {
7036                    Some(pair) => pair,
7037                    None => (
7038                        e.matmul_decode_exact(gate_shexp, z, t)?,
7039                        e.matmul_decode_exact(up_shexp, z, t)?,
7040                    ),
7041                }
7042            } else {
7043                (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?)
7044            };
7045            let mut sa = e.uninit(t * n_ff_sh)?; // silu_mul fully overwrites
7046            e.silu_mul(&sg_gate, &sg_up, &mut sa, t * n_ff_sh)?;
7047            let sh = if verify_t {
7048                e.matmul_decode_exact(down_shexp, &sa, t)?
7049            } else {
7050                e.matmul(down_shexp, &sa, t)?
7051            };
7052            // shexp gate: qwen35moe sigmoid-gates; M3 has no gate tensor -> weight 1.0.
7053            // Same fused sigmoid-dot as moe_ffn_sequential step 3 (byte-identity contract
7054            // between the two arms; prefill keeps the batched cuBLASLt linear).
7055            let g = match &m.gate_inp_shexp {
7056                Some(gate_inp_shexp) => {
7057                    // router_prefill_exact_on(): the fused sigmoid-dot is m-INVARIANT and
7058                    // serves EVERY t (serve isolation, lane/concat-prime-exact).
7059                    if t < PRIME_MIN_T || crate::router_prefill_exact_on() {
7060                        e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
7061                    } else {
7062                        let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
7063                        let mut g = e.uninit(t)?;
7064                        e.sigmoid(&gs, &mut g, t)?;
7065                        g
7066                    }
7067                }
7068                None => e.htod(&vec![1.0f32; t])?,
7069            };
7070            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
7071        }
7072
7073        Ok(moe_out)
7074    }
7075
7076    /// STAGE-2 GROUPED DECODE (2026-07-04): run ONE token's whole routed-expert FFN in TWO
7077    /// launches when every one of its 3*n_used blocks is ALREADY cache-resident. Returns
7078    /// Ok(true) if the grouped path ran (caller skips the sequential loop for this token);
7079    /// Ok(false) on ANY miss (caller falls through — the sequential loop stages/admits as
7080    /// before, so the NEXT occurrence takes the grouped path). Pointer safety: cache slots are
7081    /// fixed-address for the engine's lifetime and the pure-HIT path performs no admission, so
7082    /// the collected raw pointers cannot move between collection and launch (single-threaded
7083    /// decode; the lock is held only for collection, launches are stream-ordered after any
7084    /// prior same-stream staging writes).
7085    #[allow(clippy::too_many_arguments)]
7086    /// q8 twin of moe_gdec_token (dp4a arc): same residency check + 2-launch shape; the fused
7087    /// kernels consume the pre-quantized z-row and re-quantize act per slot batch.
7088    #[allow(clippy::too_many_arguments)]
7089    fn moe_gdec_token_q8(
7090        e: &Engine,
7091        m: &MoeWeights,
7092        il: u16,
7093        max_block: usize,
7094        zq: &CudaSlice<i8>,
7095        zd: &CudaSlice<f32>,
7096        sel: &[u32],
7097        w: &[f32],
7098        moe_out: &mut CudaSlice<f32>,
7099        tok: usize,
7100        n_embd: usize,
7101        n_ff_exp: usize,
7102        n_used: usize,
7103    ) -> Result<bool, Box<dyn std::error::Error>> {
7104        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
7105        use cudarc::driver::DevicePtr;
7106        let ptrs = e.with_moe_cache(max_block, |c, eng| {
7107            let mut g = [0u64; 8];
7108            let mut u = [0u64; 8];
7109            let mut d = [0u64; 8];
7110            for (j, &ex) in sel.iter().enumerate() {
7111                let ex = ex as u16;
7112                let (Some(sg), Some(su), Some(sd)) = (
7113                    c.resident(BlockId::new(il, PROJ_GATE, ex)),
7114                    c.resident(BlockId::new(il, PROJ_UP, ex)),
7115                    c.resident(BlockId::new(il, PROJ_DOWN, ex)),
7116                ) else {
7117                    return Ok(None);
7118                };
7119                let __s = eng.stream();
7120                let (pg, _e0) = c.slot(sg).device_ptr(&__s);
7121                let (pu, _e1) = c.slot(su).device_ptr(&__s);
7122                let (pd, _e2) = c.slot(sd).device_ptr(&__s);
7123                g[j] = pg as u64;
7124                u[j] = pu as u64;
7125                d[j] = pd as u64;
7126            }
7127            if cpu_expert_profile_admit_enabled() && !c.is_frozen() {
7128                for &ex in sel {
7129                    let ex = ex as u16;
7130                    for proj in [PROJ_GATE, PROJ_UP, PROJ_DOWN] {
7131                        c.note_profile_hit(BlockId::new(il, proj, ex));
7132                    }
7133                }
7134            }
7135            c.hits += (3 * n_used) as u64;
7136            Ok(Some((g, u, d)))
7137        })?;
7138        let Some((g, u, d)) = ptrs else {
7139            return Ok(false);
7140        };
7141        let mut wv = [0f32; 8];
7142        wv[..n_used].copy_from_slice(w);
7143        let act = e.moe_gate_up_silu8_q8(
7144            crate::WPtr8(g),
7145            crate::WPtr8(u),
7146            zq,
7147            zd,
7148            n_embd,
7149            n_ff_exp,
7150            n_used,
7151            m.gate_exps.qtype,
7152            m.up_exps.qtype,
7153            m.gate_exps.row_bytes,
7154            m.up_exps.row_bytes,
7155        )?;
7156        // per-slot act quantize: [n_used, n_ff] rows in one quantize launch.
7157        let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
7158        let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
7159        e.moe_down8_fma_q8(
7160            crate::WPtr8(d),
7161            crate::F32x8(wv),
7162            &aq2,
7163            &ad2,
7164            &mut dst,
7165            n_ff_exp,
7166            n_embd,
7167            n_used,
7168            m.down_exps.qtype,
7169            m.down_exps.row_bytes,
7170        )?;
7171        Ok(true)
7172    }
7173
7174    fn moe_gdec_token(
7175        e: &Engine,
7176        m: &MoeWeights,
7177        il: u16,
7178        max_block: usize,
7179        zt: &cudarc::driver::CudaView<f32>,
7180        sel: &[u32],
7181        w: &[f32],
7182        moe_out: &mut CudaSlice<f32>,
7183        tok: usize,
7184        n_embd: usize,
7185        n_ff_exp: usize,
7186        n_used: usize,
7187    ) -> Result<bool, Box<dyn std::error::Error>> {
7188        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
7189        use cudarc::driver::DevicePtr;
7190        // One lock hold: residency-check all 3*n_used blocks, collect raw slot pointers.
7191        let ptrs = e.with_moe_cache(max_block, |c, eng| {
7192            let mut g = [0u64; 8];
7193            let mut u = [0u64; 8];
7194            let mut d = [0u64; 8];
7195            for (j, &ex) in sel.iter().enumerate() {
7196                let ex = ex as u16;
7197                let (Some(sg), Some(su), Some(sd)) = (
7198                    c.resident(BlockId::new(il, PROJ_GATE, ex)),
7199                    c.resident(BlockId::new(il, PROJ_UP, ex)),
7200                    c.resident(BlockId::new(il, PROJ_DOWN, ex)),
7201                ) else {
7202                    return Ok(None);
7203                };
7204                let __s = eng.stream();
7205                let (pg, _e0) = c.slot(sg).device_ptr(&__s);
7206                let (pu, _e1) = c.slot(su).device_ptr(&__s);
7207                let (pd, _e2) = c.slot(sd).device_ptr(&__s);
7208                g[j] = pg as u64;
7209                u[j] = pu as u64;
7210                d[j] = pd as u64;
7211            }
7212            if cpu_expert_profile_admit_enabled() && !c.is_frozen() {
7213                for &ex in sel {
7214                    let ex = ex as u16;
7215                    for proj in [PROJ_GATE, PROJ_UP, PROJ_DOWN] {
7216                        c.note_profile_hit(BlockId::new(il, proj, ex));
7217                    }
7218                }
7219            }
7220            c.hits += (3 * n_used) as u64; // instrumentation parity with dispatch()
7221            Ok(Some((g, u, d)))
7222        })?;
7223        let Some((g, u, d)) = ptrs else {
7224            return Ok(false);
7225        };
7226        let mut wv = [0f32; 8];
7227        wv[..n_used].copy_from_slice(w);
7228        // 2 launches: (gate+up+silu) x8, then (down + slot-ordered FMA accumulate) x8.
7229        let act = e.moe_gate_up_silu8(
7230            crate::WPtr8(g),
7231            crate::WPtr8(u),
7232            zt,
7233            n_embd,
7234            n_ff_exp,
7235            n_used,
7236            m.gate_exps.qtype,
7237            m.up_exps.qtype,
7238            m.gate_exps.row_bytes,
7239            m.up_exps.row_bytes,
7240        )?;
7241        let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
7242        e.moe_down8_fma_into(
7243            crate::WPtr8(d),
7244            crate::F32x8(wv),
7245            &act,
7246            &mut dst,
7247            n_ff_exp,
7248            n_embd,
7249            n_used,
7250            m.down_exps.qtype,
7251            m.down_exps.row_bytes,
7252        )?;
7253        Ok(true)
7254    }
7255
7256    /// EDGE-1 §B.3: dispatch one expert projection through the SLRU cache, then run the SAME
7257    /// `qmatvec_view` from whichever slot it landed in (resident HIT or staged MISS). `x` is the
7258    /// sliced activation row. `proj` selects the gate/up/down HostExps tensor. Returns y = W_expert @ x.
7259    /// q8 twin of moe_cached_gemm: same dispatch/slot mechanics, dp4a expert kernel.
7260    fn moe_cached_gemm_q8(
7261        e: &Engine,
7262        il: u16,
7263        proj: u8,
7264        ex: usize,
7265        m: &MoeWeights,
7266        max_block: usize,
7267        aq: &CudaSlice<i8>,
7268        ad: &CudaSlice<f32>,
7269    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7270        use crate::moe_cache::{BlockId, DispatchSlot, PROJ_GATE, PROJ_UP};
7271        let exps = match proj {
7272            PROJ_GATE => &m.gate_exps,
7273            PROJ_UP => &m.up_exps,
7274            _ => &m.down_exps,
7275        };
7276        let layout = exps.expert_layout(ex);
7277        let id = BlockId::new(il, proj, ex as u16);
7278        let source = exps.expert_source(ex);
7279        e.with_moe_cache(max_block, |c, eng| {
7280            let slot = c.dispatch_source(id, source, eng)?;
7281            let DispatchSlot::Resident(sl) = slot;
7282            let buf = c.slot(sl);
7283            eng.qmatvec_expert_q8(
7284                buf,
7285                0..layout.len,
7286                aq,
7287                ad,
7288                1,
7289                exps.in_f,
7290                exps.out_f,
7291                layout.qtype,
7292                layout.row_bytes,
7293            )
7294        })
7295    }
7296
7297    fn moe_cached_gemm(
7298        e: &Engine,
7299        il: u16,
7300        proj: u8,
7301        ex: usize,
7302        m: &MoeWeights,
7303        max_block: usize,
7304        x: &cudarc::driver::CudaView<f32>,
7305    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7306        use crate::moe_cache::{BlockId, DispatchSlot, PROJ_GATE, PROJ_UP};
7307        let exps = match proj {
7308            PROJ_GATE => &m.gate_exps,
7309            PROJ_UP => &m.up_exps,
7310            _ => &m.down_exps,
7311        };
7312        let layout = exps.expert_layout(ex);
7313        let id = BlockId::new(il, proj, ex as u16);
7314        let source = exps.expert_source(ex);
7315        // dispatch under the lock (lookup/admit/memcpy-issue), then resolve the slot and GEMM.
7316        e.with_moe_cache(max_block, |c, eng| {
7317            let slot = c.dispatch_source(id, source, eng)?;
7318            // resolve the device buffer for this slot; the GEMM is enqueued on the compute stream
7319            // (the same stream the memcpy was issued on, so ordering holds without extra sync).
7320            let DispatchSlot::Resident(sl) = slot;
7321            let buf = c.slot(sl);
7322            eng.qmatvec_view(
7323                buf,
7324                0..layout.len,
7325                x,
7326                1,
7327                exps.in_f,
7328                exps.out_f,
7329                layout.qtype,
7330                layout.row_bytes,
7331            )
7332        })
7333    }
7334
7335    /// Populate the warmup cache for an expert whose current-token output intentionally ran on
7336    /// CPU. No GEMM is launched and callers invoke this only after the CPU result has completed,
7337    /// so the current forward's backend assignment and output remain unchanged.
7338    fn moe_profile_admit_expert(
7339        e: &Engine,
7340        il: u16,
7341        ex: usize,
7342        m: &MoeWeights,
7343        max_block: usize,
7344    ) -> Result<(), Box<dyn std::error::Error>> {
7345        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
7346        e.with_moe_cache(max_block, |cache, eng| {
7347            for (proj, exps) in [
7348                (PROJ_GATE, &m.gate_exps),
7349                (PROJ_UP, &m.up_exps),
7350                (PROJ_DOWN, &m.down_exps),
7351            ] {
7352                let id = BlockId::new(il, proj, ex as u16);
7353                let _ = cache.dispatch_source(id, exps.expert_source(ex), eng)?;
7354            }
7355            Ok(())
7356        })
7357    }
7358
7359    /// Read a projection from the immutable residency set when present; otherwise use one
7360    /// transient slot without admitting or evicting anything. Used only by post-freeze prefill.
7361    #[allow(clippy::too_many_arguments)]
7362    fn moe_frozen_gemm(
7363        e: &Engine,
7364        il: u16,
7365        proj: u8,
7366        ex: usize,
7367        m: &MoeWeights,
7368        max_block: usize,
7369        x: &cudarc::driver::CudaView<f32>,
7370        scratch: &mut Option<CudaSlice<u8>>,
7371        scratch_len: usize,
7372    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7373        use crate::moe_cache::{BlockId, PROJ_GATE, PROJ_UP};
7374        let exps = match proj {
7375            PROJ_GATE => &m.gate_exps,
7376            PROJ_UP => &m.up_exps,
7377            _ => &m.down_exps,
7378        };
7379        let layout = exps.expert_layout(ex);
7380        let id = BlockId::new(il, proj, ex as u16);
7381        if let Some(output) = e.with_moe_cache(max_block, |cache, eng| {
7382            let Some(slot) = cache.resident(id) else {
7383                return Ok(None);
7384            };
7385            let buf = cache.slot(slot);
7386            Ok(Some(eng.qmatvec_view(
7387                buf,
7388                0..layout.len,
7389                x,
7390                1,
7391                exps.in_f,
7392                exps.out_f,
7393                layout.qtype,
7394                layout.row_bytes,
7395            )?))
7396        })? {
7397            return Ok(output);
7398        }
7399        if scratch.is_none() {
7400            *scratch = Some(e.alloc_u8_uninit(scratch_len)?);
7401        }
7402        let scratch = scratch.as_mut().unwrap();
7403        e.stage_expert(exps.expert_bytes(ex), scratch, 0)?;
7404        e.qmatvec_view(
7405            scratch,
7406            0..layout.len,
7407            x,
7408            1,
7409            exps.in_f,
7410            exps.out_f,
7411            layout.qtype,
7412            layout.row_bytes,
7413        )
7414    }
7415
7416    fn moe_prefetch_expert(
7417        e: &Engine,
7418        il: u16,
7419        ex: usize,
7420        m: &MoeWeights,
7421        max_block: usize,
7422        keep: &[crate::moe_cache::BlockId],
7423    ) -> Result<(), Box<dyn std::error::Error>> {
7424        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
7425        e.with_moe_cache(max_block, |c, eng| {
7426            for (proj, exps) in [
7427                (PROJ_GATE, &m.gate_exps),
7428                (PROJ_UP, &m.up_exps),
7429                (PROJ_DOWN, &m.down_exps),
7430            ] {
7431                let id = BlockId::new(il, proj, ex as u16);
7432                let _ = c.prefetch_source(id, exps.expert_source(ex), keep, eng)?;
7433            }
7434            Ok(())
7435        })
7436    }
7437
7438    /// Worker-mode disk lookahead for grouped prefill. Memory sources are deliberately skipped so
7439    /// selecting `worker` changes only storage scheduling here; all CUDA work stays in dispatch.
7440    fn moe_prefetch_disk_expert(
7441        e: &Engine,
7442        il: u16,
7443        ex: usize,
7444        m: &MoeWeights,
7445        max_block: usize,
7446        keep: &[crate::moe_cache::BlockId],
7447    ) -> Result<(), Box<dyn std::error::Error>> {
7448        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
7449        e.with_moe_cache(max_block, |c, eng| {
7450            for (proj, exps) in [
7451                (PROJ_GATE, &m.gate_exps),
7452                (PROJ_UP, &m.up_exps),
7453                (PROJ_DOWN, &m.down_exps),
7454            ] {
7455                let source = exps.expert_source(ex);
7456                if let crate::model::ExpertSource::Disk { .. } = &source {
7457                    let id = BlockId::new(il, proj, ex as u16);
7458                    let _ = c.prefetch_source(id, source, keep, eng)?;
7459                }
7460            }
7461            Ok(())
7462        })
7463    }
7464
7465    #[inline]
7466    fn moe_prefetch_host_expert(ex: usize, m: &MoeWeights) {
7467        let _ = m.gate_exps.prefetch_expert_pages(ex);
7468        let _ = m.up_exps.prefetch_expert_pages(ex);
7469        let _ = m.down_exps.prefetch_expert_pages(ex);
7470    }
7471}
7472
7473// ================================================================================================
7474// A2: EXPERT-GROUPED MoE PREFILL (MEMRA_MOE_GROUPED=1). Resident-case prototype.
7475//
7476// Instead of the per-token loop (T * 8 experts * 3 projections = 12024 individual m=1 matvecs),
7477// this groups tokens by expert and runs ONE matmul per active expert per projection at m=m_e.
7478// On a 501-token prefill with ~170 active experts, that's ~510 matmuls (vs 12024).
7479//
7480// EXACTNESS: per-token accumulation across its 8 experts is reordered (grouped processes experts
7481// in expert-id order, not the router's top-k order). To preserve bit-identity with the sequential
7482// loop, we use an 8-SLOT scheme: expert outputs are scattered into slots keyed by the token's
7483// top-k position (0..7), then reduced in that fixed order. This makes the f32 addition order
7484// identical to the per-token loop regardless of expert processing order.
7485//
7486// Memory: T * 8 * n_embd * 4 = 501 * 8 * 2048 * 4 = ~32 MB (slot buffer). Fine on 96GB.
7487// ================================================================================================
7488
7489impl HybridModel {
7490    /// Resident-slab fast path for host-routed grouped prefill. Unclamped layers batch the
7491    /// sequential fused q8 program over the token axis; clamped layers use the separate
7492    /// expert-major q8 chain so `ffn_act_lim` remains authoritative.
7493    #[allow(clippy::too_many_arguments)]
7494    fn moe_ffn_grouped_resident_q8(
7495        e: &Engine,
7496        m: &MoeWeights,
7497        z: &CudaSlice<f32>,
7498        t: usize,
7499        cfg: &ModelConfig,
7500        il: u16,
7501        sel_all: &[u32],
7502        w_all: &[f32],
7503        table: &CudaSlice<u64>,
7504        gu_il: bool,
7505    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7506        let moe = cfg.moe.as_ref().unwrap();
7507        let n_embd = cfg.n_embd as usize;
7508        let n_expert = moe.expert_count as usize;
7509        let n_used = moe.expert_used_count as usize;
7510        let n_ff_exp = moe.expert_ff_length as usize;
7511        let n_pairs = t * n_used;
7512        debug_assert_eq!(sel_all.len(), n_pairs);
7513        debug_assert_eq!(w_all.len(), n_pairs);
7514        debug_assert!(
7515            m.gate_exps.macros.is_none()
7516                && m.up_exps.macros.is_none()
7517                && m.down_exps.macros.is_none(),
7518            "resident grouped q8 does not fold per-expert macro scales",
7519        );
7520
7521        // The rows twins run the resident sequential program verbatim on grid.z = token:
7522        // fused gate/up/SiLU per slot, batched activation quantization, then the original
7523        // slot-ordered down/FMA chain. Routing remains the sigmoid selector above (device by
7524        // default; MEMRA_SIG_ROUTER=0 is the host oracle); these kernels consume sel/w only and
7525        // never enter the softmax router.
7526        if !cfg.swiglu_clamped_at(il as u32) {
7527            let sel: Vec<i32> = sel_all.iter().map(|&expert| expert as i32).collect();
7528            let sel_d = e.htod_i32(&sel)?;
7529            let w_d = e.htod(w_all)?;
7530            let (gate_row_bytes, up_row_bytes) = if gu_il {
7531                let combined = m.gate_exps.row_bytes + m.up_exps.row_bytes;
7532                (combined, combined)
7533            } else {
7534                (m.gate_exps.row_bytes, m.up_exps.row_bytes)
7535            };
7536            let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
7537            let act = e.moe_gate_up_silu8_dev_q8_rows(
7538                table,
7539                &sel_d,
7540                &zq,
7541                &zd,
7542                t,
7543                n_embd,
7544                n_ff_exp,
7545                n_used,
7546                n_expert,
7547                m.gate_exps.qtype,
7548                m.up_exps.qtype,
7549                gate_row_bytes,
7550                up_row_bytes,
7551                &m.dev_macros,
7552            )?;
7553            let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
7554            let mut moe_out = e.uninit(t * n_embd)?;
7555            e.moe_down8_fma_dev_q8_rows_g(
7556                table,
7557                &sel_d,
7558                &w_d,
7559                &aq2,
7560                &ad2,
7561                &mut moe_out,
7562                t,
7563                n_ff_exp,
7564                n_embd,
7565                n_used,
7566                n_expert,
7567                m.down_exps.qtype,
7568                m.down_exps.row_bytes,
7569            )?;
7570
7571            if std::env::var("MEMRA_MOE_STATS").is_ok() {
7572                let mut counts = vec![0usize; n_expert];
7573                for &expert in sel_all {
7574                    counts[expert as usize] += 1;
7575                }
7576                let mut sizes: Vec<usize> =
7577                    counts.into_iter().filter(|&count| count != 0).collect();
7578                sizes.sort_unstable();
7579                let mean = sizes.iter().sum::<usize>() as f64 / sizes.len().max(1) as f64;
7580                println!(
7581                    "moe-grouped il={il} t={t} dispatch=resident-q8-rows active={}/{} \
7582                     m_e: min={} median={} mean={mean:.1} max={}",
7583                    sizes.len(),
7584                    n_expert,
7585                    sizes.first().copied().unwrap_or(0),
7586                    sizes.get(sizes.len() / 2).copied().unwrap_or(0),
7587                    sizes.last().copied().unwrap_or(0),
7588                );
7589            }
7590            return Ok(moe_out);
7591        }
7592
7593        // Clamped layers keep gate/up, activation, and down as separate stages. The pair-major
7594        // matvec body is qmatvec_expert_q8 verbatim, while one launch covers every routed pair.
7595        // Pair ids stay in router slot order so scatter preserves the sequential FMA chain.
7596        let pair_tok: Vec<i32> = (0..n_pairs).map(|pair| (pair / n_used) as i32).collect();
7597        let pair_ex: Vec<i32> = sel_all.iter().map(|&expert| expert as i32).collect();
7598        let tok_off: Vec<i32> = (0..=t).map(|tok| (tok * n_used) as i32).collect();
7599        let tok_ids: Vec<i32> = (0..n_pairs as i32).collect();
7600
7601        let mut by_expert: Vec<Vec<i32>> = vec![Vec::new(); n_expert];
7602        for (pair, &expert) in pair_ex.iter().enumerate() {
7603            by_expert[expert as usize].push(pair as i32);
7604        }
7605
7606        let pair_tok_d = e.htod_i32(&pair_tok)?;
7607        let pair_ex_d = e.htod_i32(&pair_ex)?;
7608        let pair_w_d = e.htod(w_all)?;
7609        let tok_off_d = e.htod_i32(&tok_off)?;
7610        let tok_ids_d = e.htod_i32(&tok_ids)?;
7611
7612        let matvec = |proj: i32,
7613                      pair_rows: &CudaSlice<i32>,
7614                      aq: &CudaSlice<i8>,
7615                      ad: &CudaSlice<f32>,
7616                      in_f: usize,
7617                      out_f: usize,
7618                      qtype: i32,
7619                      row_bytes: usize|
7620         -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7621            e.moe_pairs_matvec_q8(
7622                table, proj, pair_rows, &pair_ex_d, aq, ad, in_f, out_f, n_expert, n_pairs, qtype,
7623                row_bytes,
7624            )
7625        };
7626
7627        let (gate_row_bytes, up_row_bytes) = if gu_il {
7628            let combined = m.gate_exps.row_bytes + m.up_exps.row_bytes;
7629            (combined, combined)
7630        } else {
7631            (m.gate_exps.row_bytes, m.up_exps.row_bytes)
7632        };
7633        let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
7634        let gate = matvec(
7635            0,
7636            &pair_tok_d,
7637            &zq,
7638            &zd,
7639            n_embd,
7640            n_ff_exp,
7641            m.gate_exps.qtype,
7642            gate_row_bytes,
7643        )?;
7644        let up = matvec(
7645            1,
7646            &pair_tok_d,
7647            &zq,
7648            &zd,
7649            n_embd,
7650            n_ff_exp,
7651            m.up_exps.qtype,
7652            up_row_bytes,
7653        )?;
7654        let mut act = e.uninit(n_pairs * n_ff_exp)?;
7655        Self::ffn_act_lim(
7656            e,
7657            cfg,
7658            &gate,
7659            &up,
7660            1.0,
7661            1.0,
7662            cfg.clamp_exp_at(il as u32),
7663            &mut act,
7664            n_pairs * n_ff_exp,
7665        )?;
7666        let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
7667        let pair_self: Vec<i32> = (0..n_pairs as i32).collect();
7668        let pair_self_d = e.htod_i32(&pair_self)?;
7669        let down = matvec(
7670            2,
7671            &pair_self_d,
7672            &aq2,
7673            &ad2,
7674            n_ff_exp,
7675            n_embd,
7676            m.down_exps.qtype,
7677            m.down_exps.row_bytes,
7678        )?;
7679        let mut moe_out = e.uninit(t * n_embd)?;
7680        e.moe_pairs_scatter(
7681            &down,
7682            &pair_w_d,
7683            &tok_off_d,
7684            &tok_ids_d,
7685            &mut moe_out,
7686            t,
7687            n_embd,
7688        )?;
7689
7690        if std::env::var("MEMRA_MOE_STATS").is_ok() {
7691            let mut sizes: Vec<usize> = by_expert
7692                .iter()
7693                .filter_map(|pairs| (!pairs.is_empty()).then_some(pairs.len()))
7694                .collect();
7695            sizes.sort_unstable();
7696            let mean = sizes.iter().sum::<usize>() as f64 / sizes.len().max(1) as f64;
7697            println!(
7698                "moe-grouped il={il} t={t} dispatch=resident-q8-clamped-pairs active={}/{} \
7699                 m_e: min={} median={} mean={mean:.1} max={}",
7700                sizes.len(),
7701                n_expert,
7702                sizes.first().copied().unwrap_or(0),
7703                sizes.get(sizes.len() / 2).copied().unwrap_or(0),
7704                sizes.last().copied().unwrap_or(0),
7705            );
7706        }
7707        Ok(moe_out)
7708    }
7709
7710    fn moe_ffn_grouped_add_shared(
7711        e: &Engine,
7712        m: &MoeWeights,
7713        z: &CudaSlice<f32>,
7714        t: usize,
7715        cfg: &ModelConfig,
7716        il: u16,
7717        moe_out: &mut CudaSlice<f32>,
7718    ) -> Result<(), Box<dyn std::error::Error>> {
7719        let n_embd = cfg.n_embd as usize;
7720        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
7721            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
7722        {
7723            let n_ff_sh = gate_shexp.out_features();
7724            let sg_gate = e.matmul(gate_shexp, z, t)?;
7725            let sg_up = e.matmul(up_shexp, z, t)?;
7726            let mut sa = e.uninit(t * n_ff_sh)?;
7727            Self::ffn_act_lim(
7728                e,
7729                cfg,
7730                &sg_gate,
7731                &sg_up,
7732                1.0,
7733                1.0,
7734                cfg.clamp_shexp_at(il as u32),
7735                &mut sa,
7736                t * n_ff_sh,
7737            )?;
7738            let sh = e.matmul(down_shexp, &sa, t)?;
7739            let gate = match &m.gate_inp_shexp {
7740                Some(gate_inp_shexp) => {
7741                    if t < PRIME_MIN_T || crate::router_prefill_exact_on() {
7742                        e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
7743                    } else {
7744                        let raw = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
7745                        let mut gate = e.uninit(t)?;
7746                        e.sigmoid(&raw, &mut gate, t)?;
7747                        gate
7748                    }
7749                }
7750                None => e.htod(&vec![1.0f32; t])?,
7751            };
7752            e.add_scaled_rows(&sh, &gate, moe_out, n_embd, t)?;
7753        }
7754        Ok(())
7755    }
7756
7757    /// A2 expert-grouped MoE FFN (prefill path, MEMRA_MOE_GROUPED=1). Same semantics as moe_ffn:
7758    /// z [T, n_embd] -> moe_out [T, n_embd]. BIT-IDENTICAL to moe_ffn when using the slot scheme.
7759    pub(crate) fn moe_ffn_grouped(
7760        e: &Engine,
7761        m: &MoeWeights,
7762        z: &CudaSlice<f32>,
7763        t: usize,
7764        cfg: &ModelConfig,
7765        il: u16,
7766        max_block: usize,
7767    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7768        let moe = cfg.moe.as_ref().unwrap();
7769        let n_embd = cfg.n_embd as usize;
7770        let n_expert = moe.expert_count as usize;
7771        let n_used = moe.expert_used_count as usize;
7772        let n_ff_exp = moe.expert_ff_length as usize;
7773        // step35 per-layer SwiGLU clamp; None on every other arch / unclamped layer.
7774        let lim_exp = cfg.clamp_exp_at(il as u32);
7775
7776        // 1. ROUTER: exactly the same m-invariant sigmoid selector as the sequential path
7777        // (device by default; MEMRA_SIG_ROUTER=0 is the host oracle). The grouped dispatch never
7778        // enters the softmax-only pairs/dev router.
7779        let logits = Self::moe_router_logits(e, m, z, t, cfg)?;
7780        if let Some(sig) = cfg.sigmoid_router() {
7781            Self::trace_sigmoid_router_logits(e, il, t, n_expert, n_used, &logits, m, sig)?;
7782        }
7783        let (sel_all, w_all) = if let Some(sig) = cfg.sigmoid_router() {
7784            Self::moe_route_sigmoid_cfg(e, &logits, t, n_expert, n_used, m, sig)?
7785        } else {
7786            Self::moe_route_cfg(e, &logits, t, n_expert, n_used, m.active_experts.as_deref())?
7787        };
7788        crate::moesd::record_host_routes(il, n_expert, n_used, &sel_all)?;
7789        Self::trace_moe_routes(il, t, &sel_all, &w_all)?;
7790        Self::trace_moe_input(e, il, t, n_embd, z)?;
7791
7792        // Fits-VRAM Step35 under the PP stage split: use the expert-major q8 arithmetic directly
7793        // from the owning device's uniform resident slab. This is not `moe_ffn_pairs`: routing
7794        // already happened above and the activation is clamp-aware. Mixed layouts, remote slabs,
7795        // macro-scaled experts, q8-disabled configs, and spill fall through to metadata-aware A2.
7796        let no_exp_macros = m.gate_exps.macros.is_none()
7797            && m.up_exps.macros.is_none()
7798            && m.down_exps.macros.is_none();
7799        let resident_q8 = m.dev_exps.as_ref().filter(|dev| {
7800            m.has_uniform_expert_layout()
7801                && no_exp_macros
7802                && moe_q8_enabled()
7803                && q8_expert_supported(m.gate_exps.qtype)
7804                && q8_expert_supported(m.up_exps.qtype)
7805                && q8_expert_supported(m.down_exps.qtype)
7806                && moe_slab_enabled()
7807                && dev.dev == e.ctx().ordinal()
7808        });
7809        if let Some(dev) = resident_q8 {
7810            let mut moe_out = Self::moe_ffn_grouped_resident_q8(
7811                e,
7812                m,
7813                z,
7814                t,
7815                cfg,
7816                il,
7817                &sel_all,
7818                &w_all,
7819                &dev.ptr_row,
7820                dev.gu_il,
7821            )?;
7822            Self::moe_ffn_grouped_add_shared(e, m, z, t, cfg, il, &mut moe_out)?;
7823            return Ok(moe_out);
7824        }
7825
7826        // 2. BUILD PER-EXPERT TOKEN LISTS (host-side grouping).
7827        // For each expert e, we need: which tokens use it, their positions in z, their top-k
7828        // slot index (for bit-identical accumulation), and their weights.
7829        struct ExpertGroup {
7830            tok_indices: Vec<i32>,  // indices into z rows (0..T-1)
7831            slot_indices: Vec<i32>, // top-k slot (0..n_used-1) for that token-expert pair
7832            weights: Vec<f32>,      // renormalized weight for that token-expert pair
7833        }
7834        let mut groups: Vec<ExpertGroup> = (0..n_expert)
7835            .map(|_| ExpertGroup {
7836                tok_indices: Vec::new(),
7837                slot_indices: Vec::new(),
7838                weights: Vec::new(),
7839            })
7840            .collect();
7841
7842        for tok in 0..t {
7843            for j in 0..n_used {
7844                let ex = sel_all[tok * n_used + j] as usize;
7845                let w = w_all[tok * n_used + j];
7846                groups[ex].tok_indices.push(tok as i32);
7847                groups[ex].slot_indices.push(j as i32);
7848                groups[ex].weights.push(w);
7849            }
7850        }
7851
7852        // 3. ALLOCATE SLOT BUFFER: [T, n_used, n_embd] f32, zero-initialized.
7853        // Each token's 8 expert contributions land in their respective slots.
7854        let mut slot_buf = e.zeros(t * n_used * n_embd)?;
7855        let mut wbuf = e.zeros(t * n_used)?; // [T, n_used] weight buffer for FMA reduce
7856
7857        // Expert weight dimensions (used in both cache and staging paths).
7858        let g_len = m.gate_exps.max_expert_bytes();
7859        let u_len = m.up_exps.max_expert_bytes();
7860        let d_len = m.down_exps.max_expert_bytes();
7861        let moe_q8 = m.has_uniform_expert_layout()
7862            && moe_q8_enabled()
7863            && q8_expert_supported(m.gate_exps.qtype)
7864            && q8_expert_supported(m.up_exps.qtype)
7865            && q8_expert_supported(m.down_exps.qtype);
7866        // The per-expert slab view is legal only for the ordinary contiguous uniform layout.
7867        // Interleaved GU slabs require the pointer-table fast path above.
7868        let slab_local = m
7869            .dev_exps
7870            .as_ref()
7871            .filter(|dev| !dev.gu_il && moe_slab_enabled() && dev.dev == e.ctx().ordinal());
7872        let use_cache =
7873            slab_local.is_none() && Engine::moe_cache_enabled() && !e.moe_cache_frozen();
7874        // The sequential no-cache/frozen staging oracle is f32. Use q8 only where sequential
7875        // also does: a local resident slab or a live SLRU dispatch.
7876        let grouped_q8 = moe_q8 && (slab_local.is_some() || use_cache);
7877
7878        // GPU scratch for staging (only allocated without a local slab or cache).
7879        let (mut scratch_g, mut scratch_u, mut scratch_d) = if slab_local.is_none() && !use_cache {
7880            (
7881                Some(e.alloc_u8(g_len)?),
7882                Some(e.alloc_u8(u_len)?),
7883                Some(e.alloc_u8(d_len)?),
7884            )
7885        } else {
7886            (None, None, None)
7887        };
7888
7889        // 4. PER ACTIVE EXPERT: gather, compute, scatter.
7890        // Processing ORDER: DESCENDING m_e (biggest token batches first) — the concluded winner
7891        // (rig5090 2026-07-04, the ascending-id arm and its MEMRA_MOE_ORDER seam removed): desc is
7892        // a first-forward win at partial cache capacity — the hot (big-m_e) experts are admitted
7893        // to the SLRU before the small-m_e tail can pollute it, so residency converges in ONE
7894        // forward instead of several: auto-cache T=501 126.9 -> 169.9 tok/s (1.34x), cap512
7895        // 119.6 -> 160.8 (and kills the rep-to-rep bimodal); wash (<2%) at cap64 pure-spill and
7896        // at long prompts where every expert stages regardless. Order is FREE to change without
7897        // breaking the byte-identity gate: the slot scheme pins each token's accumulation order
7898        // regardless of expert processing order (the whole point of the slots).
7899        let mut order: Vec<usize> = (0..n_expert)
7900            .filter(|&ex| !groups[ex].tok_indices.is_empty())
7901            .collect();
7902        order.sort_by(|&a, &b| {
7903            groups[b]
7904                .tok_indices
7905                .len()
7906                .cmp(&groups[a].tok_indices.len())
7907                .then(a.cmp(&b))
7908        });
7909        let mut m_dist: Vec<usize> = Vec::new(); // for stats
7910        let page_window = moe_page_prefetch_window();
7911        let worker_disk_prefetch = use_cache && crate::spill_pread::worker_enabled();
7912        if worker_disk_prefetch {
7913            if let Some(first) = grouped_worker_prefetch_position(order.len(), None) {
7914                Self::moe_prefetch_disk_expert(e, il, order[first], m, max_block, &[])?;
7915            }
7916        }
7917        for (order_pos, &ex) in order.iter().enumerate() {
7918            for next in page_prefetch_positions(order_pos, order.len(), page_window) {
7919                Self::moe_prefetch_host_expert(order[next], m);
7920            }
7921            if worker_disk_prefetch {
7922                if let Some(next) = grouped_worker_prefetch_position(order.len(), Some(order_pos)) {
7923                    use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
7924                    let keep = [
7925                        BlockId::new(il, PROJ_GATE, ex as u16),
7926                        BlockId::new(il, PROJ_UP, ex as u16),
7927                        BlockId::new(il, PROJ_DOWN, ex as u16),
7928                    ];
7929                    Self::moe_prefetch_disk_expert(e, il, order[next], m, max_block, &keep)?;
7930                }
7931            }
7932            let grp = &groups[ex];
7933            let m_e = grp.tok_indices.len();
7934            m_dist.push(m_e);
7935            let gl = m.gate_exps.expert_layout(ex);
7936            let ul = m.up_exps.expert_layout(ex);
7937            let dl = m.down_exps.expert_layout(ex);
7938
7939            // Upload index/weight arrays to device. The down-proj per-expert macro-scale
7940            // (ModelOpt weight_scale_2) folds into the scatter weights — post-matmul linear,
7941            // same fold as the sequential loop's `w[j] * macro_scale(ex)`. 1.0 for GGUF experts.
7942            let tok_idx_d = e.htod_i32(&grp.tok_indices)?;
7943            let slot_idx_d = e.htod_i32(&grp.slot_indices)?;
7944            let dmac = m.down_exps.macro_scale(ex);
7945            let weight_d = if dmac == 1.0 {
7946                e.htod(&grp.weights)?
7947            } else {
7948                let scaled: Vec<f32> = grp.weights.iter().map(|&w| w * dmac).collect();
7949                e.htod(&scaled)?
7950            };
7951
7952            // GATHER: collect m_e activation rows from z into a contiguous buffer.
7953            let mut gathered = e.zeros(m_e * n_embd)?;
7954            e.gather_rows(z, &tok_idx_d, &mut gathered, n_embd, m_e)?;
7955            let gv = gathered.slice(0..m_e * n_embd);
7956
7957            // Compute gate/up/down from the local slab, metadata-aware cache, or staging. The q8
7958            // form keeps each gathered row in the sequential arithmetic class at `m=m_e`.
7959            let y = if let Some(dev) = slab_local {
7960                let gate_start = ex * m.gate_exps.expert_stride;
7961                let up_start = ex * m.up_exps.expert_stride;
7962                let down_start = ex * m.down_exps.expert_stride;
7963                if grouped_q8 {
7964                    let (zq, zd) = e.quantize_q8_1(&gathered, m_e, n_embd)?;
7965                    let gate = e.qmatvec_expert_q8(
7966                        &dev.gate,
7967                        gate_start..gate_start + gl.len,
7968                        &zq,
7969                        &zd,
7970                        m_e,
7971                        m.gate_exps.in_f,
7972                        m.gate_exps.out_f,
7973                        gl.qtype,
7974                        gl.row_bytes,
7975                    )?;
7976                    let up = e.qmatvec_expert_q8(
7977                        &dev.up,
7978                        up_start..up_start + ul.len,
7979                        &zq,
7980                        &zd,
7981                        m_e,
7982                        m.up_exps.in_f,
7983                        m.up_exps.out_f,
7984                        ul.qtype,
7985                        ul.row_bytes,
7986                    )?;
7987                    let mut act = e.uninit(m_e * n_ff_exp)?;
7988                    Self::ffn_act_lim(
7989                        e,
7990                        cfg,
7991                        &gate,
7992                        &up,
7993                        m.gate_exps.macro_scale(ex),
7994                        m.up_exps.macro_scale(ex),
7995                        lim_exp,
7996                        &mut act,
7997                        m_e * n_ff_exp,
7998                    )?;
7999                    let (aq2, ad2) = e.quantize_q8_1(&act, m_e, n_ff_exp)?;
8000                    e.qmatvec_expert_q8(
8001                        &dev.down,
8002                        down_start..down_start + dl.len,
8003                        &aq2,
8004                        &ad2,
8005                        m_e,
8006                        m.down_exps.in_f,
8007                        m.down_exps.out_f,
8008                        dl.qtype,
8009                        dl.row_bytes,
8010                    )?
8011                } else {
8012                    let gate = e.qmatvec_view(
8013                        &dev.gate,
8014                        gate_start..gate_start + gl.len,
8015                        &gv,
8016                        m_e,
8017                        m.gate_exps.in_f,
8018                        m.gate_exps.out_f,
8019                        gl.qtype,
8020                        gl.row_bytes,
8021                    )?;
8022                    let up = e.qmatvec_view(
8023                        &dev.up,
8024                        up_start..up_start + ul.len,
8025                        &gv,
8026                        m_e,
8027                        m.up_exps.in_f,
8028                        m.up_exps.out_f,
8029                        ul.qtype,
8030                        ul.row_bytes,
8031                    )?;
8032                    let mut act = e.uninit(m_e * n_ff_exp)?;
8033                    Self::ffn_act_lim(
8034                        e,
8035                        cfg,
8036                        &gate,
8037                        &up,
8038                        m.gate_exps.macro_scale(ex),
8039                        m.up_exps.macro_scale(ex),
8040                        lim_exp,
8041                        &mut act,
8042                        m_e * n_ff_exp,
8043                    )?;
8044                    let actv = act.slice(0..m_e * n_ff_exp);
8045                    e.qmatvec_view(
8046                        &dev.down,
8047                        down_start..down_start + dl.len,
8048                        &actv,
8049                        m_e,
8050                        m.down_exps.in_f,
8051                        m.down_exps.out_f,
8052                        dl.qtype,
8053                        dl.row_bytes,
8054                    )?
8055                }
8056            } else if use_cache {
8057                use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
8058                if grouped_q8 {
8059                    let (zq, zd) = e.quantize_q8_1(&gathered, m_e, n_embd)?;
8060                    let gate = e.with_moe_cache(max_block, |cache, eng| {
8061                        let id = BlockId::new(il, PROJ_GATE, ex as u16);
8062                        let slot = cache.dispatch_source(id, m.gate_exps.expert_source(ex), eng)?;
8063                        eng.qmatvec_expert_q8(
8064                            cache.buf(slot),
8065                            0..gl.len,
8066                            &zq,
8067                            &zd,
8068                            m_e,
8069                            m.gate_exps.in_f,
8070                            m.gate_exps.out_f,
8071                            gl.qtype,
8072                            gl.row_bytes,
8073                        )
8074                    })?;
8075                    let up = e.with_moe_cache(max_block, |cache, eng| {
8076                        let id = BlockId::new(il, PROJ_UP, ex as u16);
8077                        let slot = cache.dispatch_source(id, m.up_exps.expert_source(ex), eng)?;
8078                        eng.qmatvec_expert_q8(
8079                            cache.buf(slot),
8080                            0..ul.len,
8081                            &zq,
8082                            &zd,
8083                            m_e,
8084                            m.up_exps.in_f,
8085                            m.up_exps.out_f,
8086                            ul.qtype,
8087                            ul.row_bytes,
8088                        )
8089                    })?;
8090                    let mut act = e.uninit(m_e * n_ff_exp)?;
8091                    Self::ffn_act_lim(
8092                        e,
8093                        cfg,
8094                        &gate,
8095                        &up,
8096                        m.gate_exps.macro_scale(ex),
8097                        m.up_exps.macro_scale(ex),
8098                        lim_exp,
8099                        &mut act,
8100                        m_e * n_ff_exp,
8101                    )?;
8102                    let (aq2, ad2) = e.quantize_q8_1(&act, m_e, n_ff_exp)?;
8103                    e.with_moe_cache(max_block, |cache, eng| {
8104                        let id = BlockId::new(il, PROJ_DOWN, ex as u16);
8105                        let slot = cache.dispatch_source(id, m.down_exps.expert_source(ex), eng)?;
8106                        eng.qmatvec_expert_q8(
8107                            cache.buf(slot),
8108                            0..dl.len,
8109                            &aq2,
8110                            &ad2,
8111                            m_e,
8112                            m.down_exps.in_f,
8113                            m.down_exps.out_f,
8114                            dl.qtype,
8115                            dl.row_bytes,
8116                        )
8117                    })?
8118                } else {
8119                    let gate = e.with_moe_cache(max_block, |cache, eng| {
8120                        let id = BlockId::new(il, PROJ_GATE, ex as u16);
8121                        let slot = cache.dispatch_source(id, m.gate_exps.expert_source(ex), eng)?;
8122                        eng.qmatvec_view(
8123                            cache.buf(slot),
8124                            0..gl.len,
8125                            &gv,
8126                            m_e,
8127                            m.gate_exps.in_f,
8128                            m.gate_exps.out_f,
8129                            gl.qtype,
8130                            gl.row_bytes,
8131                        )
8132                    })?;
8133                    let up = e.with_moe_cache(max_block, |cache, eng| {
8134                        let id = BlockId::new(il, PROJ_UP, ex as u16);
8135                        let slot = cache.dispatch_source(id, m.up_exps.expert_source(ex), eng)?;
8136                        eng.qmatvec_view(
8137                            cache.buf(slot),
8138                            0..ul.len,
8139                            &gv,
8140                            m_e,
8141                            m.up_exps.in_f,
8142                            m.up_exps.out_f,
8143                            ul.qtype,
8144                            ul.row_bytes,
8145                        )
8146                    })?;
8147                    let mut act = e.uninit(m_e * n_ff_exp)?;
8148                    Self::ffn_act_lim(
8149                        e,
8150                        cfg,
8151                        &gate,
8152                        &up,
8153                        m.gate_exps.macro_scale(ex),
8154                        m.up_exps.macro_scale(ex),
8155                        lim_exp,
8156                        &mut act,
8157                        m_e * n_ff_exp,
8158                    )?;
8159                    let actv = act.slice(0..m_e * n_ff_exp);
8160                    e.with_moe_cache(max_block, |cache, eng| {
8161                        let id = BlockId::new(il, PROJ_DOWN, ex as u16);
8162                        let slot = cache.dispatch_source(id, m.down_exps.expert_source(ex), eng)?;
8163                        eng.qmatvec_view(
8164                            cache.buf(slot),
8165                            0..dl.len,
8166                            &actv,
8167                            m_e,
8168                            m.down_exps.in_f,
8169                            m.down_exps.out_f,
8170                            dl.qtype,
8171                            dl.row_bytes,
8172                        )
8173                    })?
8174                }
8175            } else {
8176                let sg = scratch_g.as_mut().unwrap();
8177                let su = scratch_u.as_mut().unwrap();
8178                let sd = scratch_d.as_mut().unwrap();
8179                e.stage_expert(m.gate_exps.expert_bytes(ex), sg, 0)?;
8180                e.stage_expert(m.up_exps.expert_bytes(ex), su, 0)?;
8181                e.stage_expert(m.down_exps.expert_bytes(ex), sd, 0)?;
8182                if grouped_q8 {
8183                    let (zq, zd) = e.quantize_q8_1(&gathered, m_e, n_embd)?;
8184                    let gate = e.qmatvec_expert_q8(
8185                        sg,
8186                        0..gl.len,
8187                        &zq,
8188                        &zd,
8189                        m_e,
8190                        m.gate_exps.in_f,
8191                        m.gate_exps.out_f,
8192                        gl.qtype,
8193                        gl.row_bytes,
8194                    )?;
8195                    let up = e.qmatvec_expert_q8(
8196                        su,
8197                        0..ul.len,
8198                        &zq,
8199                        &zd,
8200                        m_e,
8201                        m.up_exps.in_f,
8202                        m.up_exps.out_f,
8203                        ul.qtype,
8204                        ul.row_bytes,
8205                    )?;
8206                    let mut act = e.uninit(m_e * n_ff_exp)?;
8207                    Self::ffn_act_lim(
8208                        e,
8209                        cfg,
8210                        &gate,
8211                        &up,
8212                        m.gate_exps.macro_scale(ex),
8213                        m.up_exps.macro_scale(ex),
8214                        lim_exp,
8215                        &mut act,
8216                        m_e * n_ff_exp,
8217                    )?;
8218                    let (aq2, ad2) = e.quantize_q8_1(&act, m_e, n_ff_exp)?;
8219                    e.qmatvec_expert_q8(
8220                        sd,
8221                        0..dl.len,
8222                        &aq2,
8223                        &ad2,
8224                        m_e,
8225                        m.down_exps.in_f,
8226                        m.down_exps.out_f,
8227                        dl.qtype,
8228                        dl.row_bytes,
8229                    )?
8230                } else {
8231                    let gate = e.qmatvec_view(
8232                        sg,
8233                        0..gl.len,
8234                        &gv,
8235                        m_e,
8236                        m.gate_exps.in_f,
8237                        m.gate_exps.out_f,
8238                        gl.qtype,
8239                        gl.row_bytes,
8240                    )?;
8241                    let up = e.qmatvec_view(
8242                        su,
8243                        0..ul.len,
8244                        &gv,
8245                        m_e,
8246                        m.up_exps.in_f,
8247                        m.up_exps.out_f,
8248                        ul.qtype,
8249                        ul.row_bytes,
8250                    )?;
8251                    let mut act = e.uninit(m_e * n_ff_exp)?;
8252                    Self::ffn_act_lim(
8253                        e,
8254                        cfg,
8255                        &gate,
8256                        &up,
8257                        m.gate_exps.macro_scale(ex),
8258                        m.up_exps.macro_scale(ex),
8259                        lim_exp,
8260                        &mut act,
8261                        m_e * n_ff_exp,
8262                    )?;
8263                    let actv = act.slice(0..m_e * n_ff_exp);
8264                    e.qmatvec_view(
8265                        sd,
8266                        0..dl.len,
8267                        &actv,
8268                        m_e,
8269                        m.down_exps.in_f,
8270                        m.down_exps.out_f,
8271                        dl.qtype,
8272                        dl.row_bytes,
8273                    )?
8274                }
8275            };
8276
8277            // SCATTER into slot buffer: each row goes to slot_buf[tok, slot, :].
8278            e.scatter_slot(
8279                &y,
8280                &tok_idx_d,
8281                &slot_idx_d,
8282                &weight_d,
8283                &mut slot_buf,
8284                &mut wbuf,
8285                n_embd,
8286                n_used,
8287                m_e,
8288            )?;
8289        }
8290
8291        // 5. REDUCE SLOTS: sum the 8 slots per token into the final moe_out.
8292        let mut moe_out = e.zeros(t * n_embd)?;
8293        e.reduce_slots(&slot_buf, &wbuf, &mut moe_out, n_embd, n_used, t)?;
8294
8295        // STATS: print m-distribution when MEMRA_MOE_STATS is set.
8296        if std::env::var("MEMRA_MOE_STATS").is_ok() && !m_dist.is_empty() {
8297            m_dist.sort_unstable();
8298            let active = m_dist.len();
8299            let mean = m_dist.iter().sum::<usize>() as f64 / active as f64;
8300            let median = m_dist[active / 2];
8301            let max_m = *m_dist.last().unwrap();
8302            let min_m = m_dist[0];
8303            let above16 = m_dist.iter().filter(|&&x| x >= 16).count();
8304            println!(
8305                "moe-grouped il={il} t={t} active={active}/{n_expert} \
8306                      m_e: min={min_m} median={median} mean={mean:.1} max={max_m} \
8307                      above_gemm_threshold(>=16)={above16}/{active}"
8308            );
8309        }
8310
8311        Self::moe_ffn_grouped_add_shared(e, m, z, t, cfg, il, &mut moe_out)?;
8312        Ok(moe_out)
8313    }
8314
8315    /// Lane-3 M2: cross-stream MoE for lockstep decode. Routes all m stream rows in one
8316    /// batch, executes fully-HBM-resident experts through the grouped gather/GEMM/scatter
8317    /// machinery at m_e>1 (weight reads amortized across streams), and assigns any expert
8318    /// with a missing projection to that row's CPU companion call (whole-expert granularity,
8319    /// same rule as the sequential frozen path). Slot-pinned accumulation keeps each row's
8320    /// expert-sum order identical to the sequential path.
8321    pub(crate) fn moe_ffn_lockstep(
8322        &self,
8323        e: &Engine,
8324        m: &MoeWeights,
8325        zbatch: &CudaSlice<f32>,
8326        mrows: usize,
8327        il: u16,
8328        max_block: usize,
8329    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8330        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
8331        let cfg = &self.cfg;
8332        let moe = cfg.moe.as_ref().unwrap();
8333        let n_embd = cfg.n_embd as usize;
8334        let n_expert = moe.expert_count as usize;
8335        let n_used = moe.expert_used_count as usize;
8336        let n_ff_exp = moe.expert_ff_length as usize;
8337        // step35 per-layer SwiGLU clamp; None on every other arch / unclamped layer.
8338        let lim_exp = cfg.clamp_exp_at(il as u32);
8339        let lim_shexp = cfg.clamp_shexp_at(il as u32);
8340
8341        let logits = e.matmul(&m.gate_inp, zbatch, mrows)?;
8342        if let Some(sig) = cfg.sigmoid_router() {
8343            Self::trace_sigmoid_router_logits(e, il, mrows, n_expert, n_used, &logits, m, sig)?;
8344        }
8345        let (sel_all, w_all) = if let Some(sig) = cfg.sigmoid_router() {
8346            Self::moe_route_sigmoid_cfg(e, &logits, mrows, n_expert, n_used, m, sig)?
8347        } else {
8348            Self::moe_route_cfg(
8349                e,
8350                &logits,
8351                mrows,
8352                n_expert,
8353                n_used,
8354                m.active_experts.as_deref(),
8355            )?
8356        };
8357        Self::trace_moe_routes(il, mrows, &sel_all, &w_all)?;
8358
8359        // Residency split at whole-expert granularity against the (frozen) cache.
8360        let resident_expert: Vec<bool> = e.with_moe_cache(max_block, |c, _| {
8361            Ok((0..n_expert)
8362                .map(|ex| {
8363                    [PROJ_GATE, PROJ_UP, PROJ_DOWN]
8364                        .into_iter()
8365                        .all(|p| c.resident(BlockId::new(il, p, ex as u16)).is_some())
8366                })
8367                .collect())
8368        })?;
8369
8370        struct Group {
8371            rows: Vec<i32>,
8372            slots: Vec<i32>,
8373            weights: Vec<f32>,
8374        }
8375        let mut groups: std::collections::HashMap<usize, Group> = Default::default();
8376        let mut cpu_rows: Vec<Vec<(usize, f32)>> = vec![Vec::new(); mrows];
8377        let mut cpu_by_expert: std::collections::HashMap<usize, Vec<(usize, f32)>> =
8378            Default::default();
8379        for row in 0..mrows {
8380            for j in 0..n_used {
8381                let ex = sel_all[row * n_used + j] as usize;
8382                let w = w_all[row * n_used + j];
8383                if resident_expert[ex] {
8384                    let group = groups.entry(ex).or_insert_with(|| Group {
8385                        rows: Vec::new(),
8386                        slots: Vec::new(),
8387                        weights: Vec::new(),
8388                    });
8389                    group.rows.push(row as i32);
8390                    group.slots.push(j as i32);
8391                    group.weights.push(w);
8392                } else {
8393                    crate::cpu_experts::record_incomplete_gpu_residency(0);
8394                    cpu_rows[row].push((ex, w));
8395                    cpu_by_expert.entry(ex).or_default().push((row, w));
8396                }
8397            }
8398        }
8399
8400        // CPU tickets first: reads/compute overlap the GPU grouped work below. Experts routed
8401        // by >=2 streams go through the multi-row ABI (weight decode amortized across rows);
8402        // each row's remaining experts stay one ordinary per-row call. Contribution FP-sum
8403        // order per row differs from the sequential single-call chunk — part of the
8404        // documented lockstep numeric class.
8405        let host_rows = e.dtoh(zbatch)?;
8406        let rows_ok = crate::cpu_experts::rows_supported();
8407        enum CpuPart {
8408            Single { row: usize },
8409            Rows { rows: Vec<usize> },
8410        }
8411        let mut tickets: Vec<(CpuPart, crate::cpu_experts::CpuExpertTicket)> = Vec::new();
8412        let mut rows_served: std::collections::HashSet<(usize, usize)> = Default::default();
8413        if rows_ok {
8414            let mut shared: Vec<(usize, Vec<(usize, f32)>)> = cpu_by_expert
8415                .into_iter()
8416                .filter(|(_, rows)| rows.len() >= 2)
8417                .collect();
8418            shared.sort_by_key(|(ex, _)| *ex);
8419            for (ex, mut row_weights) in shared {
8420                row_weights.sort_by_key(|(row, _)| *row);
8421                let inputs: Vec<(&[f32], f32)> = row_weights
8422                    .iter()
8423                    .map(|&(row, w)| (&host_rows[row * n_embd..(row + 1) * n_embd], w))
8424                    .collect();
8425                let job = crate::cpu_experts::prepare_rows_job(m, ex, &inputs)
8426                    .map_err(std::io::Error::other)?;
8427                for &(row, _) in &row_weights {
8428                    rows_served.insert((row, ex));
8429                }
8430                tickets.push((
8431                    CpuPart::Rows {
8432                        rows: row_weights.iter().map(|&(row, _)| row).collect(),
8433                    },
8434                    crate::cpu_experts::submit_rows(job).map_err(std::io::Error::other)?,
8435                ));
8436            }
8437        }
8438        for (row, selected) in cpu_rows.iter().enumerate() {
8439            let leftover: Vec<(usize, f32)> = selected
8440                .iter()
8441                .copied()
8442                .filter(|&(ex, _)| !rows_served.contains(&(row, ex)))
8443                .collect();
8444            if leftover.is_empty() {
8445                continue;
8446            }
8447            let host_row = &host_rows[row * n_embd..(row + 1) * n_embd];
8448            let job = crate::cpu_experts::prepare_job(m, il, &leftover, host_row)
8449                .map_err(std::io::Error::other)?;
8450            tickets.push((
8451                CpuPart::Single { row },
8452                crate::cpu_experts::submit(job).map_err(std::io::Error::other)?,
8453            ));
8454        }
8455
8456        let mut slot_buf = e.zeros(mrows * n_used * n_embd)?;
8457        let mut wbuf = e.zeros(mrows * n_used)?;
8458        let mut order: Vec<usize> = groups.keys().copied().collect();
8459        order.sort_by(|&a, &b| {
8460            groups[&b]
8461                .rows
8462                .len()
8463                .cmp(&groups[&a].rows.len())
8464                .then(a.cmp(&b))
8465        });
8466        for &ex in &order {
8467            let group = &groups[&ex];
8468            let m_e = group.rows.len();
8469            let gl = m.gate_exps.expert_layout(ex);
8470            let ul = m.up_exps.expert_layout(ex);
8471            let dl = m.down_exps.expert_layout(ex);
8472            let row_idx_d = e.htod_i32(&group.rows)?;
8473            let slot_idx_d = e.htod_i32(&group.slots)?;
8474            let dmac = m.down_exps.macro_scale(ex);
8475            let weight_d = if dmac == 1.0 {
8476                e.htod(&group.weights)?
8477            } else {
8478                let scaled: Vec<f32> = group.weights.iter().map(|&w| w * dmac).collect();
8479                e.htod(&scaled)?
8480            };
8481            let mut gathered = e.zeros(m_e * n_embd)?;
8482            e.gather_rows(zbatch, &row_idx_d, &mut gathered, n_embd, m_e)?;
8483            let gv = gathered.slice(0..m_e * n_embd);
8484            let gate = e.with_moe_cache(max_block, |c, eng| {
8485                let slot = c
8486                    .resident(BlockId::new(il, PROJ_GATE, ex as u16))
8487                    .ok_or("lockstep resident expert vanished (cache not frozen?)")?;
8488                eng.qmatvec_view(
8489                    c.buf(crate::moe_cache::DispatchSlot::Resident(slot)),
8490                    0..gl.len,
8491                    &gv,
8492                    m_e,
8493                    m.gate_exps.in_f,
8494                    m.gate_exps.out_f,
8495                    gl.qtype,
8496                    gl.row_bytes,
8497                )
8498            })?;
8499            let up = e.with_moe_cache(max_block, |c, eng| {
8500                let slot = c
8501                    .resident(BlockId::new(il, PROJ_UP, ex as u16))
8502                    .ok_or("lockstep resident expert vanished (cache not frozen?)")?;
8503                eng.qmatvec_view(
8504                    c.buf(crate::moe_cache::DispatchSlot::Resident(slot)),
8505                    0..ul.len,
8506                    &gv,
8507                    m_e,
8508                    m.up_exps.in_f,
8509                    m.up_exps.out_f,
8510                    ul.qtype,
8511                    ul.row_bytes,
8512                )
8513            })?;
8514            let mut act = e.zeros(m_e * n_ff_exp)?;
8515            Self::ffn_act_lim(
8516                e,
8517                cfg,
8518                &gate,
8519                &up,
8520                m.gate_exps.macro_scale(ex),
8521                m.up_exps.macro_scale(ex),
8522                lim_exp,
8523                &mut act,
8524                m_e * n_ff_exp,
8525            )?;
8526            let actv = act.slice(0..m_e * n_ff_exp);
8527            let y = e.with_moe_cache(max_block, |c, eng| {
8528                let slot = c
8529                    .resident(BlockId::new(il, PROJ_DOWN, ex as u16))
8530                    .ok_or("lockstep resident expert vanished (cache not frozen?)")?;
8531                eng.qmatvec_view(
8532                    c.buf(crate::moe_cache::DispatchSlot::Resident(slot)),
8533                    0..dl.len,
8534                    &actv,
8535                    m_e,
8536                    m.down_exps.in_f,
8537                    m.down_exps.out_f,
8538                    dl.qtype,
8539                    dl.row_bytes,
8540                )
8541            })?;
8542            e.scatter_slot(
8543                &y,
8544                &row_idx_d,
8545                &slot_idx_d,
8546                &weight_d,
8547                &mut slot_buf,
8548                &mut wbuf,
8549                n_embd,
8550                n_used,
8551                m_e,
8552            )?;
8553        }
8554        let mut moe_out = e.zeros(mrows * n_embd)?;
8555        e.reduce_slots(&slot_buf, &wbuf, &mut moe_out, n_embd, n_used, mrows)?;
8556
8557        // CPU contributions join BEFORE the shared expert (the sequential path's placement).
8558        let mut row_sums: Vec<Option<Vec<f32>>> = vec![None; mrows];
8559        for (part, ticket) in tickets {
8560            let cpu_output = ticket.wait().map_err(std::io::Error::other)?;
8561            let mut add_row = |row: usize, chunk: &[f32]| {
8562                let sum = row_sums[row].get_or_insert_with(|| vec![0.0f32; n_embd]);
8563                for (accumulator, value) in sum.iter_mut().zip(chunk) {
8564                    *accumulator += value;
8565                }
8566            };
8567            match part {
8568                CpuPart::Single { row } => add_row(row, &cpu_output),
8569                CpuPart::Rows { rows } => {
8570                    for (slot, row) in rows.into_iter().enumerate() {
8571                        add_row(row, &cpu_output[slot * n_embd..(slot + 1) * n_embd]);
8572                    }
8573                }
8574            }
8575        }
8576        for (row, sum) in row_sums.into_iter().enumerate() {
8577            let Some(sum) = sum else { continue };
8578            let cpu_output = e.htod(&sum)?;
8579            let mut dst = moe_out.slice_mut(row * n_embd..(row + 1) * n_embd);
8580            e.axpy_into(&cpu_output, 1.0, &mut dst, n_embd)?;
8581        }
8582
8583        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
8584            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
8585        {
8586            let n_ff_sh = gate_shexp.out_features();
8587            let sg_gate = e.matmul(gate_shexp, zbatch, mrows)?;
8588            let sg_up = e.matmul(up_shexp, zbatch, mrows)?;
8589            let mut sa = e.zeros(mrows * n_ff_sh)?;
8590            Self::ffn_act_lim(
8591                e,
8592                cfg,
8593                &sg_gate,
8594                &sg_up,
8595                1.0,
8596                1.0,
8597                lim_shexp,
8598                &mut sa,
8599                mrows * n_ff_sh,
8600            )?;
8601            let sh = e.matmul(down_shexp, &sa, mrows)?;
8602            // lockstep rows ARE decode tokens: fused sigmoid-dot per row so batched serving
8603            // decode matches the single-sequence decode chain bit-for-bit.
8604            let g = match &m.gate_inp_shexp {
8605                Some(gate_inp_shexp) => {
8606                    e.sigmoid_dot_rows(zbatch, gate_inp_shexp.float_data(), n_embd, mrows)?
8607                }
8608                None => e.htod(&vec![1.0f32; mrows])?,
8609            };
8610            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, mrows)?;
8611        }
8612
8613        Ok(moe_out)
8614    }
8615}
8616
8617// ============================ gemma4 (R8 verified wiring) ==================================
8618// Node-for-node vs llama.cpp src/models/gemma4.cpp:180-405 (HANDOVER "R8 VERIFIED WIRING").
8619// v0 bring-up: full attention everywhere (exact for prompts < sliding_window 1024 — R6 masking
8620// later), sdpa_naive (hd 512 has no FA stamp), sequential host-staged MoE (the perf arms grow
8621// gemma variants after the correctness gate).
8622impl HybridModel {
8623    /// Per-layer attention geometry (R5): (head_dim, n_kv, n_head, rope_base, scale, is_swa).
8624    pub(crate) fn gemma4_geom(&self, il: usize) -> (usize, usize, usize, f32, f32, bool) {
8625        let g = self.cfg.gemma4.as_ref().unwrap();
8626        let swa = g.swa_pattern[il];
8627        let hd = if swa {
8628            g.key_length_swa
8629        } else {
8630            g.key_length_global
8631        } as usize;
8632        // attention scale = 1.0 (llama gemma4.cpp:11 "Gemma4 uses self.scaling = 1.0" — q/k are
8633        // per-head rms-normed; NOT the 1/sqrt(hd) default. Bring-up bug: 1/sqrt(hd) left token-0
8634        // rows exact (softmax over one element) while every later position drifted).
8635        (
8636            hd,
8637            g.head_count_kv[il] as usize,
8638            self.cfg.n_head as usize,
8639            if swa {
8640                g.rope_base_swa
8641            } else {
8642                g.rope_base_global
8643            },
8644            1.0,
8645            swa,
8646        )
8647    }
8648
8649    /// Suppress-token mask over t logits rows (tokenizer.ggml.suppress_tokens; no-op when the
8650    /// model ships none). NOT monotonic like softcap — must run before every argmax/sample, so
8651    /// every gemma4 logits tail (forward/prime/decode/dc/verify/e4b) calls this on device ld.
8652    pub(crate) fn gemma4_suppress(
8653        &self,
8654        e: &Engine,
8655        ld: &mut CudaSlice<f32>,
8656        t: usize,
8657    ) -> Result<(), Box<dyn std::error::Error>> {
8658        if let Some((ids, n)) = self.gemma4_aux.as_ref().and_then(|a| a.suppress_d.as_ref()) {
8659            // suppress_d is primary-owned by the audited invariant (serving picks the last/head
8660            // stage as primary, and this tail runs only after the last stage). The assert turns
8661            // that argued invariant into a checked one: any topology violating primary==head
8662            // trips here in debug instead of silently peer-reading a device-0 buffer.
8663            #[cfg(debug_assertions)]
8664            crate::debug_assert_tensor_stream_device(
8665                ids,
8666                &e.stream(),
8667                "gemma4_suppress.suppress_d",
8668            );
8669            e.mask_ids_rows(ld, ids, *n, self.output.out_features(), t)?;
8670        }
8671        Ok(())
8672    }
8673
8674    /// gemma4 attention (R5 geometry, R7 weightless V-norm on the RAW K projection, R9 dual rope).
8675    /// `cache`: Some => PRIME mode — append the T post-rope K / normed V rows into the quantized
8676    /// KV cache (same per-row quantize math as the decode append) and advance len. Fresh-prompt
8677    /// only (v0): attends within `tokens` via the f32 sdpa.
8678    #[allow(clippy::too_many_arguments)]
8679    fn gemma4_attn_prime(
8680        &self,
8681        e: &Engine,
8682        fa: &crate::hybrid::FullAttnLayer,
8683        il: usize,
8684        h: &CudaSlice<f32>,
8685        pos_d: &CudaSlice<i32>,
8686        t: usize,
8687        cache: Option<&mut Cache>,
8688        island: Option<&CudaSlice<i32>>,
8689    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8690        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
8691        let eps = self.cfg.rms_eps;
8692        let aux = self.gemma4_aux.as_ref().unwrap();
8693        let ones = aux.ones(e);
8694        #[cfg(debug_assertions)]
8695        crate::debug_assert_tensor_stream_device(ones, &e.stream(), "gemma4_attn_prime.ones");
8696
8697        // quantize-once window: q/k/v share `h` — the MMQ D4 activation quantizes once
8698        // (h stays borrowed across the triple, so the cache key can't go stale).
8699        e.mmq_act_begin();
8700        let q0 = e.matmul(&fa.wq, h, t)?; // [t, nh*hd]
8701        if il == 0 && std::env::var("MEMRA_G4_PRIME_TRACE").as_deref() == Ok("1") {
8702            let v = e.dtoh(&q0)?;
8703            let nan = v.iter().filter(|x| x.is_nan()).count();
8704            let amax = v.iter().fold(0f32, |a, x| a.max(x.abs()));
8705            eprintln!(
8706                "[g4-prime-trace] L0 q0: nan={nan}/{} amax={amax:.3}",
8707                v.len()
8708            );
8709        }
8710        let k0 = e.matmul(&fa.wk, h, t)?; // [t, nkv*hd]
8711        // globals ship no v_proj (wv := wk at load) — V input is the SAME projection output;
8712        // reuse k0 instead of re-running the identical matmul (K=V dedup, 5 layers).
8713        let v0 = if swa {
8714            e.matmul(&fa.wv, h, t)?
8715        } else {
8716            e.clone_dtod(&k0)?
8717        };
8718        if il == 0 && std::env::var("MEMRA_G4_PRIME_TRACE").as_deref() == Ok("1") {
8719            for (tag, buf) in [("k0", &k0), ("v0", &v0)] {
8720                let v = e.dtoh(buf)?;
8721                let nan = v.iter().filter(|x| x.is_nan()).count();
8722                let amax = v.iter().fold(0f32, |a, x| a.max(x.abs()));
8723                eprintln!(
8724                    "[g4-prime-trace] L0 {tag}: nan={nan}/{} amax={amax:.3}",
8725                    v.len()
8726                );
8727            }
8728        }
8729
8730        let mut q = e.uninit(t * nh * hd)?;
8731        let mut k = e.uninit(t * nkv * hd)?;
8732        // R7: V = weightless rms_norm of the raw projection; NEVER roped.
8733        let mut v = e.uninit(t * nkv * hd)?;
8734        // 31B glue lane: producers emit the bf16 FA operands (norm emits vb; rope emits qb/kb
8735        // post-rope) — kills 3 f32->bf16 converts + re-reads per layer. Bit-identical operands
8736        // (same __float2bfloat16); MEMRA_FA_EMIT=0 reverts to the convert-in-FA path.
8737        static EMIT: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
8738        // Island primes take the mask-capable naive kernel below; keep the operands f32
8739        // (the bf16 FA emit path has no island consumer). Text-only keeps emit unchanged.
8740        let emit = island.is_none()
8741            && t >= 16
8742            && crate::Engine::qkvnorm_w_on_prefill(nh * t + 2 * nkv * t, hd)
8743            && *EMIT.get_or_init(|| {
8744                std::env::var("MEMRA_FA_EMIT")
8745                    .map(|s| s != "0")
8746                    .unwrap_or(true)
8747            });
8748        let mut qb = e.alloc_uninit::<u8>(if emit { t * nh * hd * 2 } else { 1 })?;
8749        let mut kb = e.alloc_uninit::<u8>(if emit { t * nkv * hd * 2 } else { 1 })?;
8750        let mut vb = e.alloc_uninit::<u8>(if emit { t * nkv * hd * 2 } else { 1 })?;
8751        // f16-P/V door: emit V as f16 straight from the norm when this layer's FA consumer
8752        // reads f16 — kills the per-layer bf16->f16 re-encode (1.4GB/pass on 31B SWA).
8753        let v_f16 = emit
8754            && crate::fa_f16pv_on()
8755            && match hd {
8756                512 => true,
8757                256 => swa && crate::faw_hp_on() && nh % 2 == 0 && (nh / nkv) % 2 == 0,
8758                _ => false,
8759            };
8760        if emit {
8761            e.rms_norm_qkv_w4b(
8762                &q0,
8763                &k0,
8764                &v0,
8765                fa.q_norm.float_data(),
8766                fa.k_norm.float_data(),
8767                ones,
8768                &mut q,
8769                &mut k,
8770                &mut v,
8771                &mut vb,
8772                hd,
8773                nh * t,
8774                nkv * t,
8775                eps,
8776                v_f16,
8777            )?;
8778        } else {
8779            e.rms_norm_qkv(
8780                &q0,
8781                &k0,
8782                &v0,
8783                fa.q_norm.float_data(),
8784                fa.k_norm.float_data(),
8785                ones,
8786                &mut q,
8787                &mut k,
8788                &mut v,
8789                hd,
8790                nh * t,
8791                nkv * t,
8792                eps,
8793            )?;
8794        }
8795
8796        let ff = if swa {
8797            None
8798        } else {
8799            Some(
8800                aux.rope_freqs(e)
8801                    .expect("gemma4 global rope needs rope_freqs.weight"),
8802            )
8803        };
8804        #[cfg(debug_assertions)]
8805        if let Some(ff) = ff {
8806            crate::debug_assert_tensor_stream_device(
8807                ff,
8808                &e.stream(),
8809                "gemma4_attn_prime.rope_freqs",
8810            );
8811        }
8812        if emit {
8813            e.rope_neox2_bf16e(
8814                &mut q, &mut k, &mut qb, &mut kb, pos_d, hd, hd, nh, nkv, t, base, 1.0, ff,
8815            )?;
8816        } else {
8817            e.rope_neox2(&mut q, &mut k, pos_d, hd, hd, nh, nkv, t, base, 1.0, ff)?;
8818        }
8819
8820        if let Some(cache) = cache {
8821            let kvl = cache.kv[il].as_mut().unwrap();
8822            assert_eq!(kvl.len, 0, "gemma4 prime is fresh-prompt only (v0)");
8823            e.append_kv_quantized_rows(
8824                &k,
8825                &v,
8826                &mut kvl.k,
8827                &mut kvl.v,
8828                kvl.len,
8829                t,
8830                kvl.kv_dim_k,
8831                kvl.kv_dim_v,
8832                kvl.k_tok_bytes,
8833                kvl.v_tok_bytes,
8834                (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on()),
8835            )?;
8836            kvl.len += t;
8837        }
8838        let mut attn = e.zeros(t * nh * hd)?;
8839        // R6: SWA layers mask keys older than sliding_window once the prompt exceeds it
8840        // (windowed naive twin; fa windowed stamps later). Under the window, full attention
8841        // is exact — SWA rides fa_prefill (hd-256 stamp), the hd-512 globals stay naive.
8842        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
8843        if let Some(span) = island {
8844            // Masked-prefill arm: every layer routes through the island-aware naive
8845            // kernel (correctness-first, same posture as the vision tower v1). The
8846            // window argument keeps the R6 shortcut: 0 while the prompt fits the
8847            // window, the real window beyond it.
8848            let w = if swa && t > win { win } else { 0 };
8849            e.sdpa_naive_island(&q, &k, &v, &mut attn, span, hd, nh, nkv, t, t, scale, w)?;
8850        } else if swa && t > win {
8851            if hd == 256 && std::env::var("MEMRA_NOFA").is_err() {
8852                if emit {
8853                    e.fa_prefill_w_pre(
8854                        &qb, &kb, &vb, &mut attn, hd, nh, nkv, t, t, scale, true, win, v_f16,
8855                    )?;
8856                } else {
8857                    e.fa_prefill_w(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true, win)?;
8858                }
8859            } else {
8860                e.sdpa_naive_w(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true, win)?;
8861            }
8862        } else if hd == 256 && std::env::var("MEMRA_NOFA").is_err() {
8863            e.fa_prefill(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?;
8864        } else if hd == 512 && std::env::var("MEMRA_NOFA").is_err() {
8865            if emit {
8866                e.fa_prefill_hd512_pre(
8867                    &qb, &kb, &vb, &mut attn, hd, nh, nkv, t, t, scale, true, v_f16,
8868                )?;
8869            } else {
8870                e.fa_prefill_hd512(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?;
8871            }
8872        } else {
8873            e.sdpa_naive(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?;
8874        }
8875        Ok(e.matmul(&fa.wo, &attn, t)?)
8876    }
8877
8878    /// Back-compat wrapper (pure prefill, no cache).
8879    fn gemma4_attn(
8880        &self,
8881        e: &Engine,
8882        fa: &crate::hybrid::FullAttnLayer,
8883        il: usize,
8884        h: &CudaSlice<f32>,
8885        pos_d: &CudaSlice<i32>,
8886        t: usize,
8887    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8888        self.gemma4_attn_prime(e, fa, il, h, pos_d, t, None, None)
8889    }
8890
8891    /// gemma4 MoE with the expert input PRE-QUANTIZED (the q8z tail fusion). Caller guarantees
8892    /// the fast-arm conditions (resident dev slabs + dp4a qtypes) — decode t=1 and verify rows
8893    /// arms only, per-token kernel chains identical to the f32-input path (same quantize bytes:
8894    /// the q8z epilogue is quantize_q8_1 verbatim).
8895    fn gemma4_moe_q8(
8896        &self,
8897        e: &Engine,
8898        m: &crate::hybrid::MoeWeights,
8899        bits: &crate::hybrid::Gemma4MoeBits,
8900        mq: &(CudaSlice<i8>, CudaSlice<f32>),
8901        router_in: &CudaSlice<f32>,
8902        t: usize,
8903    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8904        let cfg = &self.cfg;
8905        let moe = cfg.moe.as_ref().unwrap();
8906        let n_embd = cfg.n_embd as usize;
8907        let n_expert = moe.expert_count as usize;
8908        let n_used = moe.expert_used_count as usize;
8909        let n_ff_exp = moe.expert_ff_length as usize;
8910        // router stays the two-launch pair: THREE fuse variants measured worse (serial-dot
8911        // -50%, 1024-thread warp-parallel -12% on topk sync overhead — jsonl 2026-07-11);
8912        // the pair's 12us is kernel time, not launch gaps.
8913        let logits = if crate::router_kernel_on() {
8914            e.router_gemv(m.gate_inp.float_data(), router_in, n_embd, n_expert, t)?
8915        } else {
8916            e.matmul(&m.gate_inp, router_in, t)?
8917        };
8918        let dev = m.dev_exps.as_ref().unwrap();
8919        let (sel_d, w_d) =
8920            e.moe_router_topk_scaled(&logits, t, n_expert, n_used, &bits.per_expert_scale_d)?;
8921        let (zq, zd) = mq;
8922        if t == 1 {
8923            let selv = sel_d.slice(0..n_used);
8924            let wv = w_d.slice(0..n_used);
8925            let act = e.moe_gate_up_gelu8_dev_q8(
8926                &dev.ptr_row,
8927                &selv,
8928                zq,
8929                zd,
8930                n_embd,
8931                n_ff_exp,
8932                n_used,
8933                n_expert,
8934                m.gate_exps.qtype,
8935                m.up_exps.qtype,
8936                m.gate_exps.row_bytes,
8937                m.up_exps.row_bytes,
8938            )?;
8939            let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
8940            let mut moe_out = e.uninit(n_embd)?;
8941            e.moe_down8_fma_dev_q8(
8942                &dev.ptr_row,
8943                &selv,
8944                &wv,
8945                &aq2,
8946                &ad2,
8947                &mut moe_out.slice_mut(0..n_embd),
8948                n_ff_exp,
8949                n_embd,
8950                n_used,
8951                n_expert,
8952                m.down_exps.qtype,
8953                m.down_exps.row_bytes,
8954            )?;
8955            return Ok(moe_out);
8956        }
8957        let csr = t <= 10 && std::env::var("MEMRA_GEMMA_CSR").as_deref() != Ok("0");
8958        let act = if csr {
8959            e.moe_gate_up_gelu8_dev_q8_csr(
8960                &dev.ptr_row,
8961                &sel_d,
8962                zq,
8963                zd,
8964                t * n_used,
8965                n_embd,
8966                n_ff_exp,
8967                n_used,
8968                n_expert,
8969                m.gate_exps.qtype,
8970                m.up_exps.qtype,
8971                m.gate_exps.row_bytes,
8972                m.up_exps.row_bytes,
8973            )?
8974        } else {
8975            e.moe_gate_up_gelu8_dev_q8_rows(
8976                &dev.ptr_row,
8977                &sel_d,
8978                zq,
8979                zd,
8980                t,
8981                n_embd,
8982                n_ff_exp,
8983                n_used,
8984                n_expert,
8985                m.gate_exps.qtype,
8986                m.up_exps.qtype,
8987                m.gate_exps.row_bytes,
8988                m.up_exps.row_bytes,
8989            )?
8990        };
8991        let (aq2, ad2) = e.quantize_q8_1(&act, t * n_used, n_ff_exp)?;
8992        let mut moe_out = e.uninit(t * n_embd)?;
8993        // down stays rows_g: the CSR dedup twin measured NEGATIVE at nsb=22 too (189.4 vs
8994        // 207.0 depth spec, bitwise-exact — jsonl 2026-07-10; qwen nsb=16 same verdict).
8995        e.moe_down8_fma_dev_q8_rows_g(
8996            &dev.ptr_row,
8997            &sel_d,
8998            &w_d,
8999            &aq2,
9000            &ad2,
9001            &mut moe_out,
9002            t,
9003            n_ff_exp,
9004            n_embd,
9005            n_used,
9006            n_expert,
9007            m.down_exps.qtype,
9008            m.down_exps.row_bytes,
9009        )?;
9010        Ok(moe_out)
9011    }
9012
9013    /// gemma4 MoE (R2 router prologue input supplied by caller, R3 per-expert output scale).
9014    /// Sequential host-staged v0 — softmax gating + renorm (moe_route, the qwen recipe), GELU
9015    /// experts, scale folded into the accumulate weight (post-matmul linear scale, exact fold).
9016    fn gemma4_moe(
9017        &self,
9018        e: &Engine,
9019        m: &crate::hybrid::MoeWeights,
9020        bits: &crate::hybrid::Gemma4MoeBits,
9021        moe_in: &CudaSlice<f32>,
9022        router_in: &CudaSlice<f32>,
9023        t: usize,
9024    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
9025        let cfg = &self.cfg;
9026        let moe = cfg.moe.as_ref().unwrap();
9027        let n_embd = cfg.n_embd as usize;
9028        let n_expert = moe.expert_count as usize;
9029        let n_used = moe.expert_used_count as usize;
9030        let n_ff_exp = moe.expert_ff_length as usize;
9031
9032        // Router: the in-house GEMV for ALL small t (decode AND verify ride the same per-column
9033        // kernel — the cuBLASLt n-dependence flipped top-k at verify t on the 27B, d994271);
9034        // batched matmul only at real prefill.
9035        let logits = if t < PRIME_MIN_T && crate::router_kernel_on() {
9036            e.router_gemv(m.gate_inp.float_data(), router_in, n_embd, n_expert, t)?
9037        } else {
9038            e.matmul(&m.gate_inp, router_in, t)?
9039        };
9040
9041        // FAST SMALL-T ARM (decode t=1 AND spec verify t=2..15): device softmax-topk router,
9042        // then PER TOKEN the same fused gate_up GELU + down8 FMA launch pair over the resident
9043        // dev slabs — verify rides the EXACT decode kernel chain per token (dispatch-parity
9044        // law; the qwen "verify must be kernel-dispatch-identical to decode" lesson).
9045        if t < PRIME_MIN_T
9046            && m.dev_exps.as_ref().is_some_and(|d| !d.gu_il)
9047            && expert_dp4a_supported(m.gate_exps.qtype)
9048            && expert_dp4a_supported(m.up_exps.qtype)
9049            && expert_dp4a_supported(m.down_exps.qtype)
9050            && std::env::var("MEMRA_GEMMA_MOE_FAST").as_deref() != Ok("0")
9051        {
9052            let dev = m.dev_exps.as_ref().unwrap();
9053            let (sel_d, w_d) =
9054                e.moe_router_topk_scaled(&logits, t, n_expert, n_used, &bits.per_expert_scale_d)?;
9055            if t == 1 {
9056                let (zq, zd) = e.quantize_q8_1(moe_in, 1, n_embd)?;
9057                let selv = sel_d.slice(0..n_used);
9058                let wv = w_d.slice(0..n_used);
9059                let act = e.moe_gate_up_gelu8_dev_q8(
9060                    &dev.ptr_row,
9061                    &selv,
9062                    &zq,
9063                    &zd,
9064                    n_embd,
9065                    n_ff_exp,
9066                    n_used,
9067                    n_expert,
9068                    m.gate_exps.qtype,
9069                    m.up_exps.qtype,
9070                    m.gate_exps.row_bytes,
9071                    m.up_exps.row_bytes,
9072                )?;
9073                let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
9074                let mut moe_out = e.uninit(n_embd)?;
9075                e.moe_down8_fma_dev_q8(
9076                    &dev.ptr_row,
9077                    &selv,
9078                    &wv,
9079                    &aq2,
9080                    &ad2,
9081                    &mut moe_out.slice_mut(0..n_embd),
9082                    n_ff_exp,
9083                    n_embd,
9084                    n_used,
9085                    n_expert,
9086                    m.down_exps.qtype,
9087                    m.down_exps.row_bytes,
9088                )?;
9089                return Ok(moe_out);
9090            }
9091            // VERIFY ROWS TWINS (t=2..15): ONE launch pair for all tokens; per (token,row,slot)
9092            // bodies are the t=1 kernels VERBATIM (bit-identical to the per-token loop).
9093            // gate_up rides the CSR owner-scan dedup when t <= 10 (each duplicated expert's
9094            // weight stream decoded once; the qwen CSR class). MEMRA_GEMMA_CSR=0 -> rows.
9095            let (zq, zd) = e.quantize_q8_1(moe_in, t, n_embd)?;
9096            let csr = t <= 10 && std::env::var("MEMRA_GEMMA_CSR").as_deref() != Ok("0");
9097            let act = if csr {
9098                e.moe_gate_up_gelu8_dev_q8_csr(
9099                    &dev.ptr_row,
9100                    &sel_d,
9101                    &zq,
9102                    &zd,
9103                    t * n_used,
9104                    n_embd,
9105                    n_ff_exp,
9106                    n_used,
9107                    n_expert,
9108                    m.gate_exps.qtype,
9109                    m.up_exps.qtype,
9110                    m.gate_exps.row_bytes,
9111                    m.up_exps.row_bytes,
9112                )?
9113            } else {
9114                e.moe_gate_up_gelu8_dev_q8_rows(
9115                    &dev.ptr_row,
9116                    &sel_d,
9117                    &zq,
9118                    &zd,
9119                    t,
9120                    n_embd,
9121                    n_ff_exp,
9122                    n_used,
9123                    n_expert,
9124                    m.gate_exps.qtype,
9125                    m.up_exps.qtype,
9126                    m.gate_exps.row_bytes,
9127                    m.up_exps.row_bytes,
9128                )?
9129            };
9130            let (aq2, ad2) = e.quantize_q8_1(&act, t * n_used, n_ff_exp)?;
9131            let mut moe_out = e.uninit(t * n_embd)?;
9132            e.moe_down8_fma_dev_q8_rows_g(
9133                &dev.ptr_row,
9134                &sel_d,
9135                &w_d,
9136                &aq2,
9137                &ad2,
9138                &mut moe_out,
9139                t,
9140                n_ff_exp,
9141                n_embd,
9142                n_used,
9143                n_expert,
9144                m.down_exps.qtype,
9145                m.down_exps.row_bytes,
9146            )?;
9147            return Ok(moe_out);
9148        }
9149
9150        let (sel_all, mut w_all) = Self::moe_route(e, &logits, t, n_expert, n_used)?;
9151        for (i, &sx) in sel_all.iter().enumerate() {
9152            w_all[i] *= bits.per_expert_scale[sx as usize];
9153        }
9154
9155        // PREFILL PAIRS ARM (t >= 16): expert-major CSR over the resident slabs — ONE launch per
9156        // projection covers ALL (token,expert) pairs (the qwen pairs recipe, _em dot = expert_dot_g
9157        // which has the Q4_0 body; GELU pairs epilogue; R3 scale folded into pair_w).
9158        if t >= PRIME_MIN_T
9159            && m.dev_exps.as_ref().is_some_and(|d| !d.gu_il)
9160            && expert_dp4a_supported(m.gate_exps.qtype)
9161            && expert_dp4a_supported(m.up_exps.qtype)
9162            && expert_dp4a_supported(m.down_exps.qtype)
9163            && std::env::var("MEMRA_GEMMA_MOE_PAIRS").as_deref() != Ok("0")
9164        {
9165            let dev = m.dev_exps.as_ref().unwrap();
9166            let n_pairs = t * n_used;
9167            let pair_ex: Vec<i32> = sel_all.iter().map(|&x| x as i32).collect();
9168            let pair_tok: Vec<i32> = (0..n_pairs).map(|p| (p / n_used) as i32).collect();
9169            let tok_off: Vec<i32> = (0..=t).map(|tok| (tok * n_used) as i32).collect();
9170            let tok_ids: Vec<i32> = (0..n_pairs as i32).collect();
9171            let pt = e.htod_i32(&pair_tok)?;
9172            let pw = e.htod(&w_all)?;
9173            let toff = e.htod_i32(&tok_off)?;
9174            let tids = e.htod_i32(&tok_ids)?;
9175            let mut by_ex: Vec<Vec<i32>> = vec![Vec::new(); n_expert];
9176            for p in 0..n_pairs {
9177                by_ex[pair_ex[p] as usize].push(p as i32);
9178            }
9179            let mut ex_ids: Vec<i32> = Vec::new();
9180            let mut ex_off: Vec<i32> = vec![0];
9181            let mut ex_pairs: Vec<i32> = Vec::with_capacity(n_pairs);
9182            for (ex, list) in by_ex.iter().enumerate() {
9183                if list.is_empty() {
9184                    continue;
9185                }
9186                ex_ids.push(ex as i32);
9187                ex_pairs.extend_from_slice(list);
9188                ex_off.push(ex_pairs.len() as i32);
9189            }
9190            let n_active = ex_ids.len();
9191            let exi = e.htod_i32(&ex_ids)?;
9192            let exo = e.htod_i32(&ex_off)?;
9193            let exp_d = e.htod_i32(&ex_pairs)?;
9194            // GROUPED f16 LANE (MEMRA_MOE_F16G=1, round 46 arc 2): dequant active experts to
9195            // f16 once per projection + one grouped f16 GEMM over the CSR groups; CSR order
9196            // end-to-end (gelu is elementwise), one row permute before the scatter. The
9197            // ragged down k (704) needs no padding here — cublas takes any k.
9198            // f16-mirror numeric class, argmax/spec gated. PER-MODEL default OFF (round 50):
9199            // the gelu class regressed g26 board-2048 prefill -8.3% under the round-49
9200            // Hopper default — see moe_f16g_gemma_on.
9201            if crate::moe_f16g_gemma_on()
9202                && f16g_proj_ok(m.gate_exps.qtype, n_embd)
9203                && f16g_proj_ok(m.up_exps.qtype, n_embd)
9204                && f16g_proj_ok(m.down_exps.qtype, n_ff_exp)
9205            {
9206                let csr_tok: Vec<i32> = ex_pairs.iter().map(|&p| p / n_used as i32).collect();
9207                let csr_tok_d = e.htod_i32(&csr_tok)?;
9208                let (z_f16, z_s) = e.moe_f16g_act(moe_in, Some(&csr_tok_d), n_embd, n_pairs)?;
9209                let g_csr = e.moe_f16_grouped(
9210                    &dev.ptr_row,
9211                    0,
9212                    n_expert,
9213                    &exi,
9214                    &ex_off,
9215                    &exo,
9216                    &z_f16,
9217                    &z_s,
9218                    n_embd,
9219                    n_ff_exp,
9220                    n_active,
9221                    n_pairs,
9222                    m.gate_exps.qtype,
9223                    m.gate_exps.row_bytes,
9224                )?;
9225                let u_csr = e.moe_f16_grouped(
9226                    &dev.ptr_row,
9227                    1,
9228                    n_expert,
9229                    &exi,
9230                    &ex_off,
9231                    &exo,
9232                    &z_f16,
9233                    &z_s,
9234                    n_embd,
9235                    n_ff_exp,
9236                    n_active,
9237                    n_pairs,
9238                    m.up_exps.qtype,
9239                    m.up_exps.row_bytes,
9240                )?;
9241                let act_csr = e.moe_pairs_gelu_mul(&g_csr, &u_csr, n_pairs * n_ff_exp)?;
9242                let (a_f16, a_s) = e.moe_f16g_act(&act_csr, None, n_ff_exp, n_pairs)?;
9243                let d_csr = e.moe_f16_grouped(
9244                    &dev.ptr_row,
9245                    2,
9246                    n_expert,
9247                    &exi,
9248                    &ex_off,
9249                    &exo,
9250                    &a_f16,
9251                    &a_s,
9252                    n_ff_exp,
9253                    n_embd,
9254                    n_active,
9255                    n_pairs,
9256                    m.down_exps.qtype,
9257                    m.down_exps.row_bytes,
9258                )?;
9259                let y_down = e.rows_permute(&d_csr, &exp_d, n_pairs, n_embd)?;
9260                let mut moe_out = e.uninit(t * n_embd)?;
9261                e.moe_pairs_scatter(&y_down, &pw, &toff, &tids, &mut moe_out, t, n_embd)?;
9262                if std::env::var("MEMRA_F16G_DEBUG").is_ok() {
9263                    let scan = |v: &[f32]| v.iter().filter(|x| !x.is_finite()).count();
9264                    let (yd, mo) = (e.dtoh(&y_down)?, e.dtoh(&moe_out)?);
9265                    eprintln!(
9266                        "[f16g-debug] post-permute bad={} post-scatter bad={}",
9267                        scan(&yd),
9268                        scan(&mo)
9269                    );
9270                }
9271                return Ok(moe_out);
9272            }
9273            // gate/up: int8-MMA expert GEMM (in_f = n_embd 2816 = 11x256 tiles ok); down keeps
9274            // the decode-once dp4a (in_f 704 fails the 256-superblock tiling).
9275            let mma =
9276                n_embd % 256 == 0 && std::env::var("MEMRA_GEMMA_MOE_MMA").as_deref() != Ok("0");
9277            let (gate, up) = if mma {
9278                let z_scr = e.mmq_iq_quantize_act(moe_in, n_embd, t)?;
9279                (
9280                    e.mmq_iq_experts(
9281                        &dev.ptr_row,
9282                        0,
9283                        n_expert,
9284                        &exi,
9285                        &exo,
9286                        &exp_d,
9287                        &pt,
9288                        &z_scr,
9289                        n_embd,
9290                        n_ff_exp,
9291                        n_active,
9292                        n_pairs,
9293                        t,
9294                        m.gate_exps.qtype,
9295                        m.gate_exps.row_bytes,
9296                    )?,
9297                    e.mmq_iq_experts(
9298                        &dev.ptr_row,
9299                        1,
9300                        n_expert,
9301                        &exi,
9302                        &exo,
9303                        &exp_d,
9304                        &pt,
9305                        &z_scr,
9306                        n_embd,
9307                        n_ff_exp,
9308                        n_active,
9309                        n_pairs,
9310                        t,
9311                        m.up_exps.qtype,
9312                        m.up_exps.row_bytes,
9313                    )?,
9314                )
9315            } else {
9316                let (zq, zd) = e.quantize_q8_1(moe_in, t, n_embd)?;
9317                (
9318                    e.moe_pairs_matvec_q8_dec(
9319                        &dev.ptr_row,
9320                        0,
9321                        &exi,
9322                        &exo,
9323                        &exp_d,
9324                        &pt,
9325                        &zq,
9326                        &zd,
9327                        n_embd,
9328                        n_ff_exp,
9329                        n_expert,
9330                        n_active,
9331                        n_pairs,
9332                        m.gate_exps.qtype,
9333                        m.gate_exps.row_bytes,
9334                    )?,
9335                    e.moe_pairs_matvec_q8_dec(
9336                        &dev.ptr_row,
9337                        1,
9338                        &exi,
9339                        &exo,
9340                        &exp_d,
9341                        &pt,
9342                        &zq,
9343                        &zd,
9344                        n_embd,
9345                        n_ff_exp,
9346                        n_expert,
9347                        n_active,
9348                        n_pairs,
9349                        m.up_exps.qtype,
9350                        m.up_exps.row_bytes,
9351                    )?,
9352                )
9353            };
9354            let pair_self: Vec<i32> = (0..n_pairs as i32).collect();
9355            let pself = e.htod_i32(&pair_self)?;
9356            // DOWN through the int8-MMA expert GEMM (2026-07-31, g26 prefill lever): the
9357            // ragged k (n_ff_exp=704 on the 26B) rides a PADDED k-walk — in_f rounds up
9358            // to the 256-val superblock (768) while the act quantizer's zero padding
9359            // makes every padded-k product exactly zero (weight overread bytes multiply
9360            // zero int8 act values; the dev slab carries 144B tail slack for the OOB).
9361            // The old dp4a matvec was 11.3ms/call at m=T (the 0.07x prefill wall).
9362            // MEMRA_GEMMA_MOE_MMA=0 reverts down together with gate/up.
9363            // FUSED ACT-EPILOGUE (default on): gelu_tanh(gate)*up + D4 quantize in one
9364            // launch — no f32 act buffer (the fused kernel zero-pads the ragged tail
9365            // exactly like the two-pass quantizer). MEMRA_MOE_FUSE_ACTQ=0 rollback.
9366            // Scratch bytes are BYTE-IDENTICAL (kernel-check gated).
9367            let y_down = if mma {
9368                let in_pad = n_ff_exp.div_ceil(256) * 256;
9369                let a_scr = if crate::moe_fuse_actq_on() {
9370                    e.mmq_iq_fused_act_quant(&gate, &up, n_ff_exp, n_pairs, 1)?
9371                } else {
9372                    let act = e.moe_pairs_gelu_mul(&gate, &up, n_pairs * n_ff_exp)?;
9373                    e.mmq_iq_quantize_act(&act, n_ff_exp, n_pairs)?
9374                };
9375                e.mmq_iq_experts(
9376                    &dev.ptr_row,
9377                    2,
9378                    n_expert,
9379                    &exi,
9380                    &exo,
9381                    &exp_d,
9382                    &pself,
9383                    &a_scr,
9384                    in_pad,
9385                    n_embd,
9386                    n_active,
9387                    n_pairs,
9388                    n_pairs,
9389                    m.down_exps.qtype,
9390                    m.down_exps.row_bytes,
9391                )?
9392            } else {
9393                let act = e.moe_pairs_gelu_mul(&gate, &up, n_pairs * n_ff_exp)?;
9394                let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
9395                e.moe_pairs_matvec_q8_dec(
9396                    &dev.ptr_row,
9397                    2,
9398                    &exi,
9399                    &exo,
9400                    &exp_d,
9401                    &pself,
9402                    &aq2,
9403                    &ad2,
9404                    n_ff_exp,
9405                    n_embd,
9406                    n_expert,
9407                    n_active,
9408                    n_pairs,
9409                    m.down_exps.qtype,
9410                    m.down_exps.row_bytes,
9411                )?
9412            };
9413            let mut moe_out = e.uninit(t * n_embd)?;
9414            e.moe_pairs_scatter(&y_down, &pw, &toff, &tids, &mut moe_out, t, n_embd)?;
9415            return Ok(moe_out);
9416        }
9417
9418        let g_len = m.gate_exps.expert_stride;
9419        let u_len = m.up_exps.expert_stride;
9420        let d_len = m.down_exps.expert_stride;
9421        // Resident dev slabs (fits-VRAM regime): read each expert straight from the device slab
9422        // at ex*stride — zero H2D, SAME qmatvec_view kernel/bytes as the staged path. Staging is
9423        // the spill fallback.
9424        let dev = m.dev_exps.as_ref().filter(|d| !d.gu_il);
9425        let (mut sg, mut su, mut sd) = if dev.is_some() {
9426            (None, None, None)
9427        } else {
9428            (
9429                Some(e.alloc_u8_uninit(g_len)?),
9430                Some(e.alloc_u8_uninit(u_len)?),
9431                Some(e.alloc_u8_uninit(d_len)?),
9432            )
9433        };
9434        let mut moe_out = e.zeros(t * n_embd)?;
9435        for tok in 0..t {
9436            let sel = &sel_all[tok * n_used..(tok + 1) * n_used];
9437            let w = &w_all[tok * n_used..(tok + 1) * n_used];
9438            let zt = moe_in.slice(tok * n_embd..(tok + 1) * n_embd);
9439            for (j, &ex) in sel.iter().enumerate() {
9440                let ex = ex as usize;
9441                let gate = match dev {
9442                    Some(d) => e.qmatvec_view(
9443                        &d.gate,
9444                        ex * g_len..(ex + 1) * g_len,
9445                        &zt,
9446                        1,
9447                        m.gate_exps.in_f,
9448                        m.gate_exps.out_f,
9449                        m.gate_exps.qtype,
9450                        m.gate_exps.row_bytes,
9451                    )?,
9452                    None => {
9453                        let sg = sg.as_mut().unwrap();
9454                        e.stage_expert(m.gate_exps.expert_bytes(ex), sg, 0)?;
9455                        e.qmatvec_view(
9456                            sg,
9457                            0..g_len,
9458                            &zt,
9459                            1,
9460                            m.gate_exps.in_f,
9461                            m.gate_exps.out_f,
9462                            m.gate_exps.qtype,
9463                            m.gate_exps.row_bytes,
9464                        )?
9465                    }
9466                };
9467                let up = match dev {
9468                    Some(d) => e.qmatvec_view(
9469                        &d.up,
9470                        ex * u_len..(ex + 1) * u_len,
9471                        &zt,
9472                        1,
9473                        m.up_exps.in_f,
9474                        m.up_exps.out_f,
9475                        m.up_exps.qtype,
9476                        m.up_exps.row_bytes,
9477                    )?,
9478                    None => {
9479                        let su = su.as_mut().unwrap();
9480                        e.stage_expert(m.up_exps.expert_bytes(ex), su, 0)?;
9481                        e.qmatvec_view(
9482                            su,
9483                            0..u_len,
9484                            &zt,
9485                            1,
9486                            m.up_exps.in_f,
9487                            m.up_exps.out_f,
9488                            m.up_exps.qtype,
9489                            m.up_exps.row_bytes,
9490                        )?
9491                    }
9492                };
9493                let mut act = e.uninit(n_ff_exp)?;
9494                e.gelu_tanh_mul(&gate, &up, &mut act, n_ff_exp)?;
9495                let actv = act.slice(0..n_ff_exp);
9496                let y = match dev {
9497                    Some(d) => e.qmatvec_view(
9498                        &d.down,
9499                        ex * d_len..(ex + 1) * d_len,
9500                        &actv,
9501                        1,
9502                        m.down_exps.in_f,
9503                        m.down_exps.out_f,
9504                        m.down_exps.qtype,
9505                        m.down_exps.row_bytes,
9506                    )?,
9507                    None => {
9508                        let sd = sd.as_mut().unwrap();
9509                        e.stage_expert(m.down_exps.expert_bytes(ex), sd, 0)?;
9510                        e.qmatvec_view(
9511                            sd,
9512                            0..d_len,
9513                            &actv,
9514                            1,
9515                            m.down_exps.in_f,
9516                            m.down_exps.out_f,
9517                            m.down_exps.qtype,
9518                            m.down_exps.row_bytes,
9519                        )?
9520                    }
9521                };
9522                let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
9523                e.axpy_into(&y, w[j], &mut dst, n_embd)?;
9524            }
9525        }
9526        Ok(moe_out)
9527    }
9528
9529    /// One gemma4 trunk layer (R8): x -> x_next.
9530    fn gemma4_layer(
9531        &self,
9532        e: &Engine,
9533        il: usize,
9534        layer: &crate::hybrid::HybridLayer,
9535        x: &CudaSlice<f32>,
9536        pos_d: &CudaSlice<i32>,
9537        t: usize,
9538    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
9539        let n_embd = self.cfg.n_embd as usize;
9540        let eps = self.cfg.rms_eps;
9541
9542        let mut h = e.zeros(t * n_embd)?;
9543        e.rms_norm(x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
9544        let Mixer::Full(fa) = &layer.mixer else {
9545            panic!("gemma4 layer {il} not full-attn")
9546        };
9547        let o = self.gemma4_attn(e, fa, il, &h, pos_d, t)?;
9548        // gemma order: post_attention_norm applies to the ATTENTION OUTPUT, then the residual.
9549        let mut cur = e.zeros(t * n_embd)?;
9550        e.rms_norm(
9551            &o,
9552            layer.post_attn_norm.float_data(),
9553            &mut cur,
9554            n_embd,
9555            t,
9556            eps,
9557        )?;
9558        self.gemma4_layer_tail_add(e, layer, &cur, x, t)
9559    }
9560
9561    /// Everything after the attention output in a gemma4 layer: the residual add (cur + x ->
9562    /// attn_out) FUSED with the three attn_out norms, then shared FFN + router + MoE + combine +
9563    /// layer scale — shared verbatim by the prefill, decode and verify paths.
9564    fn gemma4_layer_tail_add(
9565        &self,
9566        e: &Engine,
9567        layer: &crate::hybrid::HybridLayer,
9568        cur: &CudaSlice<f32>,
9569        x: &CudaSlice<f32>,
9570        t: usize,
9571    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
9572        Ok(self.gemma4_layer_tail_add_n(e, layer, cur, x, t, None)?.0)
9573    }
9574
9575    /// tail_add with the NEXT layer's attn_norm fused into the closing add+scale (one launch
9576    /// produces both x_next and h_next — the cross-layer fusion). `next_norm` None = last layer.
9577    fn gemma4_layer_tail_add_n(
9578        &self,
9579        e: &Engine,
9580        layer: &crate::hybrid::HybridLayer,
9581        cur: &CudaSlice<f32>,
9582        x: &CudaSlice<f32>,
9583        t: usize,
9584        next_norm: Option<&CudaSlice<f32>>,
9585    ) -> Result<(CudaSlice<f32>, Option<CudaSlice<f32>>), Box<dyn std::error::Error>> {
9586        let n_embd = self.cfg.n_embd as usize;
9587        let bits = layer.gemma4.as_ref().unwrap();
9588        let (sn, attn_out) = self.gemma4_layer_tail_core(e, layer, cur, x, t)?;
9589        let mut xn = e.uninit(t * n_embd)?;
9590        match next_norm {
9591            Some(w) => {
9592                let mut hn = e.uninit(t * n_embd)?;
9593                e.add_scale_rms_norm(
9594                    &sn,
9595                    &attn_out,
9596                    bits.layer_scale,
9597                    w,
9598                    &mut xn,
9599                    &mut hn,
9600                    n_embd,
9601                    t,
9602                    self.cfg.rms_eps,
9603                )?;
9604                Ok((xn, Some(hn)))
9605            }
9606            None => {
9607                e.add_scale(&sn, &attn_out, bits.layer_scale, &mut xn, t * n_embd)?;
9608                Ok((xn, None))
9609            }
9610        }
9611    }
9612
9613    /// Tail core: attn_out = cur+x (fused with the 3 norms), both FFN branches, the combine
9614    /// norm — returns (sn, attn_out) for the closing add+scale variants.
9615    fn gemma4_layer_tail_core(
9616        &self,
9617        e: &Engine,
9618        layer: &crate::hybrid::HybridLayer,
9619        cur: &CudaSlice<f32>,
9620        x: &CudaSlice<f32>,
9621        t: usize,
9622    ) -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
9623        self.gemma4_layer_tail_core_pn(e, layer, cur, x, t, None, false)
9624    }
9625
9626    /// tail_core with an optional PRE-NORM fold (glue-fusion lane): `pre_norm = Some(wa)`
9627    /// means `cur` is the RAW attention output and the dense entry runs
9628    /// rms(cur, wa) + residual-add + ffn_norm as ONE launch (E4B's post-attn norm fold).
9629    /// `defer_post_norm`: dense-arm exit returns RAW f0 (ffn_down output) instead of
9630    /// sn = rms(f0, post_ffw) — the caller fuses the post-norm into its residual emit
9631    /// (rms_pre_add_q8_1, E4B glue wave 5). MoE arm ignores it.
9632    fn gemma4_layer_tail_core_pn(
9633        &self,
9634        e: &Engine,
9635        layer: &crate::hybrid::HybridLayer,
9636        cur: &CudaSlice<f32>,
9637        x: &CudaSlice<f32>,
9638        t: usize,
9639        pre_norm: Option<&CudaSlice<f32>>,
9640        defer_post_norm: bool,
9641    ) -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
9642        let n_embd = self.cfg.n_embd as usize;
9643        let eps = self.cfg.rms_eps;
9644        let bits = layer.gemma4.as_ref().unwrap();
9645
9646        // DENSE gemma4 variants (31B/E4B — no MoE, no parallel branch): attn_out = cur + x
9647        // fused with the single ffn_norm; GELU_PAR ffn; post_ffw_norm.
9648        let Some(mbits) = bits.moe_bits.as_ref() else {
9649            let crate::hybrid::Ffn::Dense {
9650                ffn_gate,
9651                ffn_up,
9652                ffn_down,
9653            } = &layer.ffn
9654            else {
9655                panic!("gemma4 dense layer without Dense ffn")
9656            };
9657            let mut attn_out = e.uninit(t * n_embd)?;
9658            let mut zsh = e.uninit(t * n_embd)?;
9659            // wave-2: with the pre-norm fold active the entry ALSO emits zsh q8_1 — the
9660            // t=1 fused2 gate/up consume the pair with no standalone quantize launch.
9661            let mut zpair: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
9662            match pre_norm {
9663                Some(wa) if t == 1 => {
9664                    zpair = Some(e.rms_pre_add_rms_norm_q8z(
9665                        cur,
9666                        wa,
9667                        x,
9668                        bits.ffn_norm.float_data(),
9669                        &mut attn_out,
9670                        &mut zsh,
9671                        n_embd,
9672                        t,
9673                        eps,
9674                    )?);
9675                }
9676                Some(wa) => e.rms_pre_add_rms_norm(
9677                    cur,
9678                    wa,
9679                    x,
9680                    bits.ffn_norm.float_data(),
9681                    &mut attn_out,
9682                    &mut zsh,
9683                    n_embd,
9684                    t,
9685                    eps,
9686                )?,
9687                None => e.add_rms_norm(
9688                    cur,
9689                    x,
9690                    bits.ffn_norm.float_data(),
9691                    &mut attn_out,
9692                    &mut zsh,
9693                    n_embd,
9694                    t,
9695                    eps,
9696                )?,
9697            }
9698            let n_ff = ffn_gate.out_features();
9699            // FFN persistent slab (counter-barrier form) FALSIFIED here 2026-07-14
9700            // (falsification #7, jsonl row): PDL glue already hides the launch boundaries
9701            // it fused; barrier + worst-segment occupancy net −0.3% (31B depth) / −2.3%
9702            // (E4B spec). Down's act dependency is all-to-all, so sentinel sync cannot
9703            // rescue segment C — the megakernel front is closed for the dense tail.
9704            let (gate, up) = if t == 1 {
9705                let (zq, zd) = match zpair {
9706                    Some(p) => p,
9707                    None => e.quantize_q8_1(&zsh, 1, n_embd)?,
9708                };
9709                match e.matmul_q4_fused2(ffn_gate, ffn_up, &zq, &zd)? {
9710                    Some(p) => p,
9711                    // NVFP4mix dense trunk: gate/up are both NVFP4 (down stays Q8_0).
9712                    None => match e.matmul_nvfp4_fused2(ffn_gate, ffn_up, &zq, &zd, 1)? {
9713                        Some(p) => p,
9714                        None => (
9715                            e.matmul_pre(ffn_gate, &zq, &zd, &zsh, 1)?,
9716                            e.matmul_pre(ffn_up, &zq, &zd, &zsh, 1)?,
9717                        ),
9718                    },
9719                }
9720            } else {
9721                // BATCHED FUSED2 (DEFAULT ON 2026-07-13, MEMRA_F2B=0 seam): one segmented
9722                // launch for the verify's gate+up — the up segment's blocks fill SMs as
9723                // the gate segment drains (the launch-tail mechanism behind the b-tier
9724                // plateau; first positive after six falsified in-kernel variants).
9725                static F2B: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
9726                let f2b = *F2B.get_or_init(|| std::env::var("MEMRA_F2B").as_deref() != Ok("0"));
9727                let fused = if f2b {
9728                    let (zq, zd) = e.quantize_q8_1(&zsh, t, n_embd)?;
9729                    e.matmul_q4_fused2_batched(ffn_gate, ffn_up, &zq, &zd, t)?
9730                } else {
9731                    None
9732                };
9733                match fused {
9734                    Some(p) => p,
9735                    None => {
9736                        // quantize-once window: gate/up share `zsh` (borrowed across the pair).
9737                        e.mmq_act_begin();
9738                        (e.matmul(ffn_gate, &zsh, t)?, e.matmul(ffn_up, &zsh, t)?)
9739                    }
9740                }
9741            };
9742            let mut act = e.uninit(t * n_ff)?;
9743            // act quantize folds into the GELU epilogue (bit-identical q8_1 rounding);
9744            // ffn_down rides matmul_pre — one quantize launch fewer per layer.
9745            let f0 = if e.uses_q8_1_fast(ffn_down) {
9746                let upv = e.view(&up, t * n_ff);
9747                let up_all = upv.slice(0..t * n_ff);
9748                let (aq, ad) = e.gelu_tanh_mul_q8_1(&gate, &up_all, &mut act, n_ff, t)?;
9749                e.matmul_pre(ffn_down, &aq, &ad, &act, t)?
9750            } else {
9751                e.gelu_tanh_mul(&gate, &up, &mut act, t * n_ff)?;
9752                e.matmul(ffn_down, &act, t)?
9753            };
9754            if defer_post_norm {
9755                return Ok((f0, attn_out));
9756            }
9757            let mut sn = e.uninit(t * n_embd)?;
9758            e.rms_norm(
9759                &f0,
9760                bits.post_ffw_norm.float_data(),
9761                &mut sn,
9762                n_embd,
9763                t,
9764                eps,
9765            )?;
9766            return Ok((sn, attn_out));
9767        };
9768
9769        assert!(pre_norm.is_none(), "pre-norm fold is dense-entry only");
9770        // MoE variant (26B): attn_out = cur + x fused with the three attn_out norms
9771        // (ffn_norm + router-scale + pre_ffw_norm_2): ONE launch, chains verbatim. At small t
9772        // (decode + verify) the zsh and moe_in outputs are EMITTED q8_1 (both consumers are
9773        // quantized matmuls — two quantize launches + two f32 round-trips fold away).
9774        let mut attn_out = e.uninit(t * n_embd)?;
9775        let mut router_in = e.uninit(t * n_embd)?;
9776        let fast_moe = match &layer.ffn {
9777            crate::hybrid::Ffn::Moe(m) => {
9778                m.dev_exps.as_ref().is_some_and(|d| !d.gu_il)
9779                    && expert_dp4a_supported(m.gate_exps.qtype)
9780                    && expert_dp4a_supported(m.up_exps.qtype)
9781                    && expert_dp4a_supported(m.down_exps.qtype)
9782                    && std::env::var("MEMRA_GEMMA_MOE_FAST").as_deref() != Ok("0")
9783            }
9784            _ => false,
9785        };
9786        let q8z = t < PRIME_MIN_T && fast_moe;
9787        let (zsh_f32, zsh_q8, moe_q8) = if q8z {
9788            let (z0, m2) = e.add_rms_norm3_q8z(
9789                cur,
9790                x,
9791                bits.ffn_norm.float_data(),
9792                &mbits.router_scale_pre,
9793                mbits.pre_ffw_norm_2.float_data(),
9794                &mut attn_out,
9795                &mut router_in,
9796                n_embd,
9797                t,
9798                eps,
9799            )?;
9800            (None, Some(z0), Some(m2))
9801        } else {
9802            let mut zsh = e.uninit(t * n_embd)?;
9803            let mut moe_in = e.uninit(t * n_embd)?;
9804            e.add_rms_norm3(
9805                cur,
9806                x,
9807                bits.ffn_norm.float_data(),
9808                &mbits.router_scale_pre,
9809                mbits.pre_ffw_norm_2.float_data(),
9810                &mut attn_out,
9811                &mut zsh,
9812                &mut router_in,
9813                &mut moe_in,
9814                n_embd,
9815                t,
9816                eps,
9817            )?;
9818            (Some((zsh, moe_in)), None, None)
9819        };
9820        let attn_out2 = attn_out;
9821        #[allow(unused_variables)]
9822        let attn_out = &attn_out2;
9823        let n_ff = mbits.shared_gate.out_features();
9824        let (gate, up) = if let Some((zq, zd)) = zsh_q8.as_ref() {
9825            if t == 1 {
9826                match e.matmul_q4_fused2(&mbits.shared_gate, &mbits.shared_up, zq, zd)? {
9827                    Some(p) => p,
9828                    None => match e.matmul_nvfp4_fused2(
9829                        &mbits.shared_gate,
9830                        &mbits.shared_up,
9831                        zq,
9832                        zd,
9833                        1,
9834                    )? {
9835                        Some(p) => p,
9836                        None => {
9837                            let h0 = e.zeros(0)?;
9838                            (
9839                                e.matmul_pre(&mbits.shared_gate, zq, zd, &h0, 1)?,
9840                                e.matmul_pre(&mbits.shared_up, zq, zd, &h0, 1)?,
9841                            )
9842                        }
9843                    },
9844                }
9845            } else {
9846                // verify t 2..15: the batched mmvq twins consume the pre-quantized pair.
9847                let h0 = e.zeros(0)?;
9848                (
9849                    e.matmul_pre(&mbits.shared_gate, zq, zd, &h0, t)?,
9850                    e.matmul_pre(&mbits.shared_up, zq, zd, &h0, t)?,
9851                )
9852            }
9853        } else {
9854            let (zsh, _) = zsh_f32.as_ref().unwrap();
9855            (
9856                e.matmul(&mbits.shared_gate, zsh, t)?,
9857                e.matmul(&mbits.shared_up, zsh, t)?,
9858            )
9859        };
9860        let mut act = e.uninit(t * n_ff)?;
9861        e.gelu_tanh_mul(&gate, &up, &mut act, t * n_ff)?;
9862        let mlp0 = e.matmul(&mbits.shared_down, &act, t)?;
9863        let crate::hybrid::Ffn::Moe(m) = &layer.ffn else {
9864            panic!("gemma4 layer not MoE")
9865        };
9866        let moe0 = match (&moe_q8, &zsh_f32) {
9867            (Some(mq), _) => self.gemma4_moe_q8(e, m, mbits, mq, &router_in, t)?,
9868            (None, Some((_, moe_in))) => self.gemma4_moe(e, m, mbits, moe_in, &router_in, t)?,
9869            _ => unreachable!(),
9870        };
9871        // post_ffw_norm_1(mlp0) + post_ffw_norm_2(moe0): one fused launch, per-row verbatim.
9872        let mut mlp = e.uninit(t * n_embd)?;
9873        let mut moe = e.uninit(t * n_embd)?;
9874        e.rms_norm2x(
9875            &mlp0,
9876            &moe0,
9877            mbits.post_ffw_norm_1.float_data(),
9878            mbits.post_ffw_norm_2.float_data(),
9879            &mut mlp,
9880            &mut moe,
9881            n_embd,
9882            t,
9883            eps,
9884        )?;
9885
9886        // combine: rms_norm(mlp + moe, post_ffw_norm) + attn_out, then the layer output scalar.
9887        // add+norm fused (add_rms_norm == add then rms_norm, kernel-check-pinned identity).
9888        let mut sum = e.uninit(t * n_embd)?;
9889        let mut sn = e.uninit(t * n_embd)?;
9890        e.add_rms_norm(
9891            &mlp,
9892            &moe,
9893            bits.post_ffw_norm.float_data(),
9894            &mut sum,
9895            &mut sn,
9896            n_embd,
9897            t,
9898            eps,
9899        )?;
9900        Ok((sn, attn_out2))
9901    }
9902
9903    /// tail_add with the next attn_norm emitted PRE-QUANTIZED q8_1 (decode/verify loops).
9904    /// pn-fold front of `gemma4_layer_tail_add_nq` (MEMRA_G4_PNFOLD, GAP-DIAGNOSIS
9905    /// verdict 7): takes the RAW attention output `o` and, on the dense trunk, folds
9906    /// post_attn_norm into the tail entry and post_ffw_norm into the residual exit —
9907    /// the E4B glue chain (13594) backported to the decode/verify trio. Kills the
9908    /// standalone rms_norm(o) + rms_norm(f0) (+ffn quantize) launches per layer.
9909    /// BITS-CHANGING vs the two-launch chain; every caller rides this ONE front so
9910    /// decode == verify == graph parity holds by construction at either seam value.
9911    /// Non-dense (26B MoE) and seam-off fall to the unfused chain unchanged.
9912    pub(crate) fn gemma4_layer_tail_add_nq_pn(
9913        &self,
9914        e: &Engine,
9915        layer: &crate::hybrid::HybridLayer,
9916        o: &CudaSlice<f32>,
9917        x: &CudaSlice<f32>,
9918        t: usize,
9919        next_norm: Option<&CudaSlice<f32>>,
9920    ) -> Result<(CudaSlice<f32>, Option<(CudaSlice<i8>, CudaSlice<f32>)>), Box<dyn std::error::Error>>
9921    {
9922        let n_embd = self.cfg.n_embd as usize;
9923        let eps = self.cfg.rms_eps;
9924        let bits = layer.gemma4.as_ref().unwrap();
9925        if Engine::g4_pnfold_on() && matches!(layer.ffn, crate::hybrid::Ffn::Dense { .. }) {
9926            let (f0, attn_out) = self.gemma4_layer_tail_core_pn(
9927                e,
9928                layer,
9929                o,
9930                x,
9931                t,
9932                Some(layer.post_attn_norm.float_data()),
9933                true,
9934            )?;
9935            let mut xn = e.uninit(t * n_embd)?;
9936            return match next_norm {
9937                Some(w) => {
9938                    let pair = e.rms_pre_add_scale_rms_norm_q8_1(
9939                        &f0,
9940                        bits.post_ffw_norm.float_data(),
9941                        &attn_out,
9942                        bits.layer_scale,
9943                        w,
9944                        &mut xn,
9945                        n_embd,
9946                        t,
9947                        eps,
9948                    )?;
9949                    Ok((xn, Some(pair)))
9950                }
9951                None => {
9952                    let mut sn = e.uninit(t * n_embd)?;
9953                    e.rms_norm(
9954                        &f0,
9955                        bits.post_ffw_norm.float_data(),
9956                        &mut sn,
9957                        n_embd,
9958                        t,
9959                        eps,
9960                    )?;
9961                    e.add_scale(&sn, &attn_out, bits.layer_scale, &mut xn, t * n_embd)?;
9962                    Ok((xn, None))
9963                }
9964            };
9965        }
9966        let mut cur = e.uninit(t * n_embd)?;
9967        e.rms_norm(
9968            o,
9969            layer.post_attn_norm.float_data(),
9970            &mut cur,
9971            n_embd,
9972            t,
9973            eps,
9974        )?;
9975        self.gemma4_layer_tail_add_nq(e, layer, &cur, x, t, next_norm)
9976    }
9977
9978    pub(crate) fn gemma4_layer_tail_add_nq(
9979        &self,
9980        e: &Engine,
9981        layer: &crate::hybrid::HybridLayer,
9982        cur: &CudaSlice<f32>,
9983        x: &CudaSlice<f32>,
9984        t: usize,
9985        next_norm: Option<&CudaSlice<f32>>,
9986    ) -> Result<(CudaSlice<f32>, Option<(CudaSlice<i8>, CudaSlice<f32>)>), Box<dyn std::error::Error>>
9987    {
9988        let n_embd = self.cfg.n_embd as usize;
9989        let bits = layer.gemma4.as_ref().unwrap();
9990        let (sn, attn_out) = self.gemma4_layer_tail_core(e, layer, cur, x, t)?;
9991        let mut xn = e.uninit(t * n_embd)?;
9992        match next_norm {
9993            Some(w) => {
9994                let pair = e.add_scale_rms_norm_q8_1(
9995                    &sn,
9996                    &attn_out,
9997                    bits.layer_scale,
9998                    w,
9999                    &mut xn,
10000                    n_embd,
10001                    t,
10002                    self.cfg.rms_eps,
10003                )?;
10004                Ok((xn, Some(pair)))
10005            }
10006            None => {
10007                e.add_scale(&sn, &attn_out, bits.layer_scale, &mut xn, t * n_embd)?;
10008                Ok((xn, None))
10009            }
10010        }
10011    }
10012
10013    /// gemma4 prefill: `last_only` = forward_last semantics (lm_head on the final row only).
10014    /// R4: final logits softcapped 30*tanh(l/30) on host (monotonic — argmax unaffected).
10015    fn gemma4_forward(
10016        &self,
10017        e: &Engine,
10018        tokens: &[u32],
10019        last_only: bool,
10020    ) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
10021        // E4B routes to its own forward regardless of the caller's entry point (forward /
10022        // forward_last / prime paths all funnel here for gemma4).
10023        if self.is_gemma4_e4b() {
10024            return self.gemma4_e4b_forward(e, tokens, last_only);
10025        }
10026        let n_embd = self.cfg.n_embd as usize;
10027        let t = tokens.len();
10028        let pos: Vec<i32> = (0..t as i32).collect();
10029        let pos_d = e.htod_i32(&pos)?;
10030
10031        let mut x = self.embed(e, tokens)?;
10032        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
10033        // MEMRA_GEMMA_PROBE=1: per-layer trunk stats (host rms + max of the LAST token row) —
10034        // the bring-up bisect vs llama-eval-callback node stats.
10035        let probe = std::env::var("MEMRA_GEMMA_PROBE").is_ok();
10036        let stat =
10037            |e: &Engine, x: &CudaSlice<f32>, tag: &str| -> Result<(), Box<dyn std::error::Error>> {
10038                let h = e.dtoh(x)?;
10039                let bad = h.iter().filter(|v| !v.is_finite()).count();
10040                let mx = h
10041                    .iter()
10042                    .filter(|v| v.is_finite())
10043                    .fold(0.0f32, |m, v| m.max(v.abs()));
10044                eprintln!(
10045                    "[gemma-probe] {tag}: tok0_first3={:?} bad={bad} max={mx:.3e}",
10046                    &h[..3]
10047                );
10048                Ok(())
10049            };
10050        if probe {
10051            stat(e, &x, "embed")?;
10052        }
10053        for (il, layer) in self.layers.iter().enumerate() {
10054            x = self.gemma4_layer(e, il, layer, &x, &pos_d, t)?;
10055            if probe {
10056                stat(e, &x, &format!("L{il}"))?;
10057            }
10058        }
10059        let mut hn = e.zeros(t * n_embd)?;
10060        e.rms_norm(
10061            &x,
10062            self.output_norm.float_data(),
10063            &mut hn,
10064            n_embd,
10065            t,
10066            self.cfg.rms_eps,
10067        )?;
10068        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
10069        let n_vocab = self.output.out_features();
10070        let logits = if last_only {
10071            let hv = e.view(&hn, t * n_embd);
10072            let last_row = hv.slice((t - 1) * n_embd..t * n_embd);
10073            let mut hlast = e.zeros(n_embd)?;
10074            e.copy_view_into(&mut hlast, 0, &last_row, n_embd)?;
10075            let mut ld = e.matmul(&self.output, &hlast, 1)?;
10076            e.softcap(&mut ld, cap, n_vocab)?;
10077            self.gemma4_suppress(e, &mut ld, 1)?;
10078            e.dtoh(&ld)?
10079        } else {
10080            let mut ld = e.matmul(&self.output, &hn, t)?;
10081            e.softcap(&mut ld, cap, t * n_vocab)?;
10082            self.gemma4_suppress(e, &mut ld, t)?;
10083            e.dtoh(&ld)?
10084        };
10085        Ok(logits)
10086    }
10087
10088    /// gemma4 BATCHED PROMPT PRIME v0 (fresh cache only): the prefill graph over the whole
10089    /// prompt with each layer's post-rope K / weightless-normed V appended into the quantized
10090    /// KV cache (decode-append row math). Returns (last-row softcapped logits, h_seed = last
10091    /// pre-output_norm hidden, hiddens = full pre-output_norm stack [T, n_embd]).
10092    pub(crate) fn gemma4_prime(
10093        &self,
10094        e: &Engine,
10095        tokens: &[u32],
10096        cache: &mut Cache,
10097        overlay: Option<&crate::vision::EmbedOverlay>,
10098    ) -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
10099        // Err, not assert (2026-08-07, lane/gemma4-serve-gaps): a served gemma4 prompt longer
10100        // than the worker's prefill tick used to chunk here, and chunk 2 (pos > 0) killed the
10101        // whole worker process on this line. The worker now primes gemma4 monolithically and
10102        // routes continuation suffixes tokenwise; this is the per-request backstop.
10103        if cache.pos != 0 {
10104            return Err(
10105                "gemma4 prime v0 is fresh-prompt only (no continuation/chunked prime) \
10106                        — prime the full prompt in one call or decode tokenwise"
10107                    .into(),
10108            );
10109        }
10110        let n_embd = self.cfg.n_embd as usize;
10111        let eps = self.cfg.rms_eps;
10112        let t = tokens.len();
10113        let pos: Vec<i32> = (0..t as i32).collect();
10114        let pos_d = e.htod_i32(&pos)?;
10115        let mut x = self.embed(e, tokens)?;
10116        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
10117        // Masked-prefill arm (lane/gemma-vision): splice the tower rows AFTER the
10118        // sqrt(n_embd) text scale — the reference scales token batches only
10119        // (llama.cpp gemma4.cpp:182 `ubatch.token ? sqrtf(n_embd) : 1.0f`), so image
10120        // embeddings enter the trunk raw. Each span becomes an attention ISLAND:
10121        // bidirectional within itself, causal+SWA everywhere else, matching the
10122        // reference's llama_set_causal_attn(false) image batch exactly.
10123        let island: Option<CudaSlice<i32>> = match overlay {
10124            Some(ov) => {
10125                let mut span_id = vec![-1i32; t];
10126                for (i, &(pos, row_off, n_rows)) in ov.spans.iter().enumerate() {
10127                    if pos + n_rows > t {
10128                        return Err(format!(
10129                            "gemma4 overlay span {i} [{pos}, {}) exceeds the prompt ({t})",
10130                            pos + n_rows
10131                        )
10132                        .into());
10133                    }
10134                    let view = ov.rows.slice(row_off * n_embd..(row_off + n_rows) * n_embd);
10135                    e.copy_view_into(&mut x, pos * n_embd, &view, n_rows * n_embd)?;
10136                    for s in span_id.iter_mut().skip(pos).take(n_rows) {
10137                        *s = i as i32;
10138                    }
10139                }
10140                // MEMRA_GV_FORCE_CAUSAL=1: WRONG-ARM probe seam — splice the rows but
10141                // keep the plain causal mask. Exists only so the decisive probe can show
10142                // the island mask itself changes the answer; never on in serving.
10143                if std::env::var("MEMRA_GV_FORCE_CAUSAL").as_deref() == Ok("1") {
10144                    None
10145                } else {
10146                    Some(e.htod_i32(&span_id)?)
10147                }
10148            }
10149            None => None,
10150        };
10151        for (il, layer) in self.layers.iter().enumerate() {
10152            let mut h = e.zeros(t * n_embd)?;
10153            e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
10154            let Mixer::Full(fa) = &layer.mixer else {
10155                panic!("gemma4 layer not full-attn")
10156            };
10157            let trace = il == 0 && std::env::var("MEMRA_G4_PRIME_TRACE").as_deref() == Ok("1");
10158            if trace {
10159                let v = e.dtoh(&h)?;
10160                let nan = v.iter().filter(|x| x.is_nan()).count();
10161                eprintln!("[g4-prime-trace] L0 post-attn_norm: nan={nan}/{}", v.len());
10162            }
10163            let o =
10164                self.gemma4_attn_prime(e, fa, il, &h, &pos_d, t, Some(cache), island.as_ref())?;
10165            if trace {
10166                let v = e.dtoh(&o)?;
10167                let nan = v.iter().filter(|x| x.is_nan()).count();
10168                eprintln!("[g4-prime-trace] L0 post-attn: nan={nan}/{}", v.len());
10169            }
10170            let mut cur = e.zeros(t * n_embd)?;
10171            e.rms_norm(
10172                &o,
10173                layer.post_attn_norm.float_data(),
10174                &mut cur,
10175                n_embd,
10176                t,
10177                eps,
10178            )?;
10179            x = self.gemma4_layer_tail_add(e, layer, &cur, &x, t)?;
10180            self.dflash_tap(e, cache, il, &x, t)?;
10181            // MEMRA_G4_PRIME_TRACE=1: per-layer NaN hunt (recipe-probe diagnostic; off = zero cost)
10182            if std::env::var("MEMRA_G4_PRIME_TRACE").as_deref() == Ok("1") {
10183                let h = e.dtoh(&x)?;
10184                let nan = h.iter().filter(|v| v.is_nan()).count();
10185                let amax = h.iter().fold(0f32, |a, v| a.max(v.abs()));
10186                eprintln!(
10187                    "[g4-prime-trace] layer {il}: nan={nan}/{} amax={amax:.3}",
10188                    h.len()
10189                );
10190                if nan > 0 {
10191                    return Err(format!("g4-prime-trace: first NaN at layer {il}").into());
10192                }
10193            }
10194        }
10195        cache.pos += t;
10196        let hiddens = e.clone_dtod(&x)?;
10197        let xv = e.view(&x, t * n_embd);
10198        let last_row = xv.slice((t - 1) * n_embd..t * n_embd);
10199        let mut h_seed = e.zeros(n_embd)?;
10200        e.copy_view_into(&mut h_seed, 0, &last_row, n_embd)?;
10201        let mut hn = e.uninit(n_embd)?;
10202        e.rms_norm(
10203            &h_seed,
10204            self.output_norm.float_data(),
10205            &mut hn,
10206            n_embd,
10207            1,
10208            eps,
10209        )?;
10210        let mut ld = e.matmul(&self.output, &hn, 1)?;
10211        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
10212        e.softcap(&mut ld, cap, self.output.out_features())?;
10213        self.gemma4_suppress(e, &mut ld, 1)?;
10214        let logits = e.dtoh(&ld)?;
10215        Ok((logits, h_seed, hiddens))
10216    }
10217
10218    /// gemma4 T=1 decode attention: per-layer geometry, quantized-KV append + fa_decode
10219    /// (vec kernels at hd 256, generic scalar at the globals' hd 512), weightless V-norm,
10220    /// dual rope, scale 1.0. Takes the attn-normed input PRE-QUANTIZED (the cross-layer
10221    /// fused norm emits q8 directly — the f32 h never materializes).
10222    fn gemma4_decode_attn(
10223        &self,
10224        e: &Engine,
10225        fa: &crate::hybrid::FullAttnLayer,
10226        il: usize,
10227        hq: &CudaSlice<i8>,
10228        hdq: &CudaSlice<f32>,
10229        pos_d: &CudaSlice<i32>,
10230        cache: &mut Cache,
10231    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
10232        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
10233        let eps = self.cfg.rms_eps;
10234        let aux = self.gemma4_aux.as_ref().unwrap();
10235        let ones = aux.ones(e);
10236        #[cfg(debug_assertions)]
10237        crate::debug_assert_tensor_stream_device(ones, &e.stream(), "gemma4_decode_attn.ones");
10238        let (hq, hdq) = (hq, hdq);
10239        let h0 = e.zeros(0)?;
10240        let h = &h0;
10241        let (q0, k0, v0) = if swa {
10242            match e.matmul_q4_fused3(&fa.wq, &fa.wk, &fa.wv, &hq, &hdq)? {
10243                Some(t3) => t3,
10244                // NVFP4mix trio is MIXED-type (wv stays Q8_0), so fused3 can never
10245                // match — fuse the uniform (q,k) pair and take v as its own single.
10246                None => match e.matmul_nvfp4_fused2(&fa.wq, &fa.wk, &hq, &hdq, 1)? {
10247                    Some((q0, k0)) => {
10248                        let v0 = e.matmul_pre(&fa.wv, &hq, &hdq, h, 1)?;
10249                        (q0, k0, v0)
10250                    }
10251                    None => (
10252                        e.matmul_pre(&fa.wq, &hq, &hdq, h, 1)?,
10253                        e.matmul_pre(&fa.wk, &hq, &hdq, h, 1)?,
10254                        e.matmul_pre(&fa.wv, &hq, &hdq, h, 1)?,
10255                    ),
10256                },
10257            }
10258        } else {
10259            let (q0, k0) = match e.matmul_q4_fused2(&fa.wq, &fa.wk, &hq, &hdq)? {
10260                Some(p) => p,
10261                None => match e.matmul_nvfp4_fused2(&fa.wq, &fa.wk, &hq, &hdq, 1)? {
10262                    Some(p) => p,
10263                    None => (
10264                        e.matmul_pre(&fa.wq, &hq, &hdq, h, 1)?,
10265                        e.matmul_pre(&fa.wk, &hq, &hdq, h, 1)?,
10266                    ),
10267                },
10268            };
10269            let v0 = e.clone_dtod(&k0)?;
10270            (q0, k0, v0)
10271        };
10272        let mut q = e.uninit(nh * hd)?;
10273        let mut k = e.uninit(nkv * hd)?;
10274        let mut v = e.uninit(nkv * hd)?;
10275        // E4B wave-3 fold, m=1 completion (2026-07-23): the rows arms took this fold at
10276        // 550fcfa5; the decode-step trio kept 2 launches/layer it doesn't need.
10277        let ff = if swa {
10278            None
10279        } else {
10280            Some(
10281                aux.rope_freqs(e)
10282                    .expect("gemma4 global rope needs rope_freqs.weight"),
10283            )
10284        };
10285        #[cfg(debug_assertions)]
10286        if let Some(ff) = ff {
10287            crate::debug_assert_tensor_stream_device(
10288                ff,
10289                &e.stream(),
10290                "gemma4_decode_attn.rope_freqs",
10291            );
10292        }
10293        let kvl = cache.kv[il].as_mut().unwrap();
10294        let kv_fp8 = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
10295        if crate::Engine::qkv_append_on() {
10296            // append fold, eager completion (zoo-fusion arc): the dc arms took this fold
10297            // 2026-07-23; the eager step kept the 2-launch pair it doesn't need. Host-len
10298            // twin of the dc fold — bit-identical bodies, one launch per layer.
10299            e.rms_norm_qkv_rope_append(
10300                &q0,
10301                &k0,
10302                &v0,
10303                fa.q_norm.float_data(),
10304                fa.k_norm.float_data(),
10305                ones,
10306                &mut q,
10307                &mut k,
10308                &mut v,
10309                hd,
10310                nh,
10311                nkv,
10312                pos_d,
10313                nh,
10314                nkv,
10315                base,
10316                1.0,
10317                ff,
10318                eps,
10319                &mut kvl.k,
10320                &mut kvl.v,
10321                kvl.len,
10322                kvl.k_tok_bytes,
10323                kvl.v_tok_bytes,
10324                kv_fp8,
10325            )?;
10326        } else {
10327            e.rms_norm_qkv_rope(
10328                &q0,
10329                &k0,
10330                &v0,
10331                fa.q_norm.float_data(),
10332                fa.k_norm.float_data(),
10333                ones,
10334                &mut q,
10335                &mut k,
10336                &mut v,
10337                hd,
10338                nh,
10339                nkv,
10340                pos_d,
10341                nh,
10342                nkv,
10343                base,
10344                1.0,
10345                ff,
10346                eps,
10347            )?;
10348            e.append_kv_quantized(
10349                &k,
10350                &v,
10351                &mut kvl.k,
10352                &mut kvl.v,
10353                kvl.len,
10354                kvl.kv_dim_k,
10355                kvl.kv_dim_v,
10356                kvl.k_tok_bytes,
10357                kvl.v_tok_bytes,
10358                kv_fp8,
10359            )?;
10360        }
10361        kvl.len += 1;
10362        // R6 decode: SWA layers attend only the last `sliding_window` keys — a token-aligned
10363        // VIEW OFFSET into the quantized cache (keys carry absolute rope; the mask is purely
10364        // positional). Globals attend the full history.
10365        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
10366        let mut attn = e.uninit(nh * hd)?;
10367        // global layers: SAME hd512 rows twin as verify with t=1 (parity law).
10368        if !swa
10369            && hd == 512
10370            && kvl.len >= crate::fa512_min_tkv()
10371            && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0")
10372        {
10373            let kp = e.view_u8(&kvl.k, kvl.len * kvl.k_tok_bytes);
10374            let vp = e.view_u8(&kvl.v, kvl.len * kvl.v_tok_bytes);
10375            // device-len: eager syncs the counter (async arg-store), dc keeps it live.
10376            let base = kvl.len as i32;
10377            e.i32_set_k(&mut kvl.len_d, base)?;
10378            e.fa_decode_rows(
10379                &q,
10380                &kp,
10381                &vp,
10382                &mut attn,
10383                hd,
10384                nh,
10385                nkv,
10386                kvl.len - 1,
10387                1,
10388                scale,
10389                kvl.k_tok_bytes,
10390                kvl.v_tok_bytes,
10391                Some((&kvl.len_d, -1)),
10392                false,
10393                false,
10394                None,
10395            )?;
10396            return Ok(e.matmul(&fa.wo, &attn, 1)?);
10397        }
10398        // windowed regime: SAME rows_w kernel as verify with t=1 (parity law — see verify_attn).
10399        if swa
10400            && kvl.len > win
10401            && hd == 256
10402            && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0")
10403        {
10404            let kp = e.view_u8(&kvl.k, kvl.len * kvl.k_tok_bytes);
10405            let vp = e.view_u8(&kvl.v, kvl.len * kvl.v_tok_bytes);
10406            let base = kvl.len as i32;
10407            e.i32_set_k(&mut kvl.len_d, base)?;
10408            e.fa_decode_rows_w(
10409                &q,
10410                &kp,
10411                &vp,
10412                &mut attn,
10413                hd,
10414                nh,
10415                nkv,
10416                &kvl.len_d,
10417                -1,
10418                1,
10419                scale,
10420                win,
10421                kvl.k_tok_bytes,
10422                kvl.v_tok_bytes,
10423                None,
10424            )?;
10425            return Ok(e.matmul(&fa.wo, &attn, 1)?);
10426        }
10427        let (off_tok, t_kv) = if swa && kvl.len > win {
10428            (kvl.len - win, win)
10429        } else {
10430            (0, kvl.len)
10431        };
10432        let k_view = e.view_u8_range(
10433            &kvl.k,
10434            off_tok * kvl.k_tok_bytes,
10435            (off_tok + t_kv) * kvl.k_tok_bytes,
10436        );
10437        let v_view = e.view_u8_range(
10438            &kvl.v,
10439            off_tok * kvl.v_tok_bytes,
10440            (off_tok + t_kv) * kvl.v_tok_bytes,
10441        );
10442        e.fa_decode_kvmod(
10443            &q,
10444            &k_view,
10445            &v_view,
10446            &mut attn,
10447            hd,
10448            nh,
10449            nkv,
10450            t_kv,
10451            scale,
10452            kvl.k_tok_bytes,
10453            kvl.v_tok_bytes,
10454            swa && crate::Engine::wkv_on(),
10455        )?;
10456        Ok(e.matmul(&fa.wo, &attn, 1)?)
10457    }
10458
10459    /// gemma4 DEVICE-COUNTER decode step (graph arc): token id + rope pos + KV lengths live in
10460    /// device counters; ZERO varying host kernel args. `cap_bucket_max` = Some(bucket) for graph
10461    /// capture (host mirrors untouched, n_splits from bucket, full-buffer KV views) / None for
10462    /// the eager-dc gate path (host mirrors advanced, live geometry — bit-identical target =
10463    /// gemma4_decode_step_h's token stream). V1 scope: t_kv <= sliding_window (no window views
10464    /// in-graph; the driver gates).
10465    #[allow(clippy::too_many_arguments)]
10466    pub fn gemma4_decode_step_dc(
10467        &self,
10468        e: &Engine,
10469        token_d: &CudaSlice<u32>,
10470        pos_d: &mut CudaSlice<i32>,
10471        embd_gpu: &CudaSlice<u8>,
10472        embd_qt: i32,
10473        embd_rb: usize,
10474        cache: &mut Cache,
10475        n_vocab: usize,
10476        cap_bucket_max: Option<(usize, usize)>,
10477    ) -> Result<CudaSlice<u32>, Box<dyn std::error::Error>> {
10478        let mut tok_out = e.stream().alloc_zeros::<u32>(1)?;
10479        self.gemma4_decode_step_dc_into(
10480            e,
10481            token_d,
10482            pos_d,
10483            embd_gpu,
10484            embd_qt,
10485            embd_rb,
10486            cache,
10487            n_vocab,
10488            cap_bucket_max,
10489            &mut tok_out,
10490        )?;
10491        Ok(tok_out)
10492    }
10493
10494    /// CAPTURE body: argmax lands in the PERSISTENT `tok_out` (same buffer = same address on
10495    /// every replay; pass `token_d` itself for the self-feeding graph loop).
10496    #[allow(clippy::too_many_arguments)]
10497    pub fn gemma4_decode_step_dc_into(
10498        &self,
10499        e: &Engine,
10500        token_d: &CudaSlice<u32>,
10501        pos_d: &mut CudaSlice<i32>,
10502        embd_gpu: &CudaSlice<u8>,
10503        embd_qt: i32,
10504        embd_rb: usize,
10505        cache: &mut Cache,
10506        n_vocab: usize,
10507        cap_bucket_max: Option<(usize, usize)>,
10508        tok_out: &mut CudaSlice<u32>,
10509    ) -> Result<(), Box<dyn std::error::Error>> {
10510        let n_embd = self.cfg.n_embd as usize;
10511        let eps = self.cfg.rms_eps;
10512        let mut x = e.embed_gather_device(embd_gpu, token_d, n_embd, embd_qt, embd_rb)?;
10513        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
10514        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
10515        let n_layers = self.layers.len();
10516        for (il, layer) in self.layers.iter().enumerate() {
10517            let (hq, hdq) = match h_carry.take() {
10518                Some(p) => p,
10519                None => {
10520                    e.rms_norm_q8_1(&x, self.layers[0].attn_norm.float_data(), n_embd, 1, eps)?
10521                }
10522            };
10523            let Mixer::Full(fa) = &layer.mixer else {
10524                panic!("gemma4 layer {il} not full-attn")
10525            };
10526            let o =
10527                self.gemma4_decode_attn_dc(e, fa, il, &hq, &hdq, pos_d, cache, cap_bucket_max)?;
10528            let next_norm = if il + 1 < n_layers {
10529                Some(self.layers[il + 1].attn_norm.float_data())
10530            } else {
10531                None
10532            };
10533            let (xn, hn) = self.gemma4_layer_tail_add_nq_pn(e, layer, &o, &x, 1, next_norm)?;
10534            x = xn;
10535            h_carry = hn;
10536        }
10537        let mut hn = e.uninit(n_embd)?;
10538        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
10539        let mut logits = e.matmul(&self.output, &hn, 1)?;
10540        self.gemma4_suppress(e, &mut logits, 1)?; // cap skipped (monotonic); the mask is not
10541        e.argmax_token_device_into(&logits, tok_out, n_vocab)?;
10542        e.inc_seqlen(pos_d)?;
10543        if cap_bucket_max.is_none() {
10544            cache.pos += 1;
10545        }
10546        Ok(())
10547    }
10548
10549    /// Persistent transient slots for the ALLOC-FREE captured dc step (the graph door):
10550    /// every buffer the step produces per token lives here, allocated ONCE pre-capture, so
10551    /// the captured graph carries zero cuMemAllocAsync/Free nodes (the 226us/launch tax,
10552    /// osrt 2026-07-23). Sized for the model's max per-layer shapes.
10553
10554    /// Build the slot set (call OUTSIDE any capture).
10555    pub fn g4_dc_slots(&self, e: &Engine) -> Result<G4DcSlots, Box<dyn std::error::Error>> {
10556        let n_embd = self.cfg.n_embd as usize;
10557        let n_vocab = self.output.out_features();
10558        let n_layers = self.layers.len();
10559        let (mut qmax, mut kvmax, mut ffmax) = (0usize, 0usize, 0usize);
10560        for il in 0..n_layers {
10561            let (hd, nkv, nh, _b, _s, _w) = self.gemma4_geom(il);
10562            qmax = qmax.max(nh * hd);
10563            kvmax = kvmax.max(nkv * hd);
10564            if let crate::hybrid::Ffn::Dense { ffn_gate, .. } = &self.layers[il].ffn {
10565                ffmax = ffmax.max(ffn_gate.out_features());
10566            }
10567        }
10568        Ok(G4DcSlots {
10569            x: e.uninit(n_embd)?,
10570            xn: e.uninit(n_embd)?,
10571            cur: e.uninit(n_embd)?,
10572            hq: e.alloc_i8_uninit(n_embd)?,
10573            hd_: e.uninit(n_embd / 32)?,
10574            q0: e.uninit(qmax)?,
10575            k0: e.uninit(kvmax)?,
10576            v0: e.uninit(kvmax)?,
10577            q: e.uninit(qmax)?,
10578            k: e.uninit(kvmax)?,
10579            v: e.uninit(kvmax)?,
10580            attn: e.uninit(qmax)?,
10581            o: e.uninit(n_embd)?,
10582            attn_out: e.uninit(n_embd)?,
10583            zsh: e.uninit(n_embd)?,
10584            // zq/zd feed the wo matvec (nh*hd rows — 4096 on hd512 globals > n_embd),
10585            // the ffn entry (n_embd) and the lm_head (n_embd): size for the max.
10586            zq: e.alloc_i8_uninit(n_embd.max(qmax))?,
10587            zd: e.uninit(n_embd.max(qmax) / 32)?,
10588            gate: e.uninit(ffmax)?,
10589            up: e.uninit(ffmax)?,
10590            act: e.uninit(ffmax)?,
10591            actq: e.alloc_i8_uninit(ffmax)?,
10592            actd: e.uninit(ffmax / 32)?,
10593            f0: e.uninit(n_embd)?,
10594            sn: e.uninit(n_embd)?,
10595            hn: e.uninit(n_embd)?,
10596            logits: e.uninit(n_vocab)?,
10597        })
10598    }
10599
10600    /// m=1 pre-quantized matvec into a slot — mirrors matmul_pre's m=1 mmvq route exactly
10601    /// (rp4-mirror bytes; mmvq_supports guaranteed for gemma4 q4_0/q6_K).
10602    fn g4_matvec_m1_into(
10603        &self,
10604        e: &Engine,
10605        w: &crate::model::GpuTensor,
10606        aq: &CudaSlice<i8>,
10607        ad: &CudaSlice<f32>,
10608        y: &mut CudaSlice<f32>,
10609    ) -> Result<(), Box<dyn std::error::Error>> {
10610        use crate::model::GpuTensor;
10611        let (bytes, qtype, row_bytes, scale, rp) = match w {
10612            GpuTensor::Quant {
10613                bytes,
10614                qtype,
10615                row_bytes,
10616                scale,
10617                rp,
10618                ..
10619            } => (bytes, *qtype, *row_bytes, *scale, *rp),
10620            _ => return Err("g4_matvec_m1_into: non-quant tensor".into()),
10621        };
10622        let (mbytes, mrp) = match w {
10623            GpuTensor::Quant { rp4: Some(m4), .. } => (m4, true),
10624            _ => (bytes, rp),
10625        };
10626        e.qmatvec_mmvq_into(
10627            mbytes,
10628            aq,
10629            ad,
10630            1,
10631            w.in_features(),
10632            w.out_features(),
10633            qtype,
10634            row_bytes,
10635            scale,
10636            mrp,
10637            y,
10638        )
10639    }
10640
10641    /// ALLOC-FREE dc step (capture body): kernel-for-kernel mirror of
10642    /// `gemma4_decode_step_dc_into` at t=1 with every transient slot-fed. Dense gemma4 only
10643    /// (12B/31B; uniform q4_0 trunk guarantees the fused2/3 arms).
10644    #[allow(clippy::too_many_arguments)]
10645    pub fn gemma4_decode_step_dc_slotted(
10646        &self,
10647        e: &Engine,
10648        token_d: &CudaSlice<u32>,
10649        pos_d: &mut CudaSlice<i32>,
10650        embd_gpu: &CudaSlice<u8>,
10651        embd_qt: i32,
10652        embd_rb: usize,
10653        cache: &mut Cache,
10654        n_vocab: usize,
10655        cap_bucket_max: Option<(usize, usize)>,
10656        sl: &mut G4DcSlots,
10657        tok_out: &mut CudaSlice<u32>,
10658        ring: Option<(&mut CudaSlice<u32>, usize)>,
10659    ) -> Result<(), Box<dyn std::error::Error>> {
10660        let n_embd = self.cfg.n_embd as usize;
10661        let eps = self.cfg.rms_eps;
10662        e.embed_gather_device_into(embd_gpu, token_d, &mut sl.x, n_embd, embd_qt, embd_rb)?;
10663        e.scale_inplace(&mut sl.x, (n_embd as f32).sqrt(), n_embd)?;
10664        let n_layers = self.layers.len();
10665        let mut has_carry = false;
10666        for il in 0..n_layers {
10667            if !has_carry {
10668                e.rms_norm_q8_1_into(
10669                    &sl.x,
10670                    self.layers[il].attn_norm.float_data(),
10671                    n_embd,
10672                    1,
10673                    eps,
10674                    &mut sl.hq,
10675                    &mut sl.hd_,
10676                )?;
10677            }
10678            has_carry = true;
10679            let layer = &self.layers[il];
10680            let Mixer::Full(fa) = &layer.mixer else {
10681                panic!("gemma4 layer {il} not full-attn")
10682            };
10683            self.gemma4_decode_attn_dc_slotted(e, fa, il, pos_d, cache, cap_bucket_max, sl)?;
10684            // pn-fold: the tail's entry launch consumes RAW sl.o (post_attn folded there);
10685            // the standalone norm only survives on the unfused seam arm.
10686            if !Engine::g4_pnfold_on() {
10687                e.rms_norm(
10688                    &sl.o,
10689                    layer.post_attn_norm.float_data(),
10690                    &mut sl.cur,
10691                    n_embd,
10692                    1,
10693                    eps,
10694                )?;
10695            }
10696            let next_norm = if il + 1 < n_layers {
10697                Some(self.layers[il + 1].attn_norm.float_data())
10698            } else {
10699                None
10700            };
10701            self.gemma4_layer_tail_slotted(e, layer, next_norm, sl)?;
10702            std::mem::swap(&mut sl.x, &mut sl.xn);
10703        }
10704        e.rms_norm(
10705            &sl.x,
10706            self.output_norm.float_data(),
10707            &mut sl.hn,
10708            n_embd,
10709            1,
10710            eps,
10711        )?;
10712        e.quantize_q8_1_into(&sl.hn, 1, n_embd, &mut sl.zq, &mut sl.zd)?;
10713        // lm_head via the same m=1 mmvq route as matmul (q6_K on the gemma4 family).
10714        {
10715            let (zq, zd) = (&sl.zq, &sl.zd);
10716            let zq = unsafe { &*(zq as *const CudaSlice<i8>) };
10717            let zd = unsafe { &*(zd as *const CudaSlice<f32>) };
10718            self.g4_matvec_m1_into(e, &self.output, zq, zd, &mut sl.logits)?;
10719        }
10720        self.gemma4_suppress(e, &mut sl.logits, 1)?;
10721        e.argmax_token_device_into(&sl.logits, tok_out, n_vocab)?;
10722        if let Some((ring, base)) = ring {
10723            // in-graph token parking: slot = (pos - base) % ring.len(); the door drains
10724            // with ONE sync per chunk instead of a per-token dtoh (the launch-serialization
10725            // fix — llama's 885us/launch overlaps GPU work because nothing waits per token).
10726            e.plain_tok_ring(tok_out, pos_d, base, ring)?;
10727        }
10728        e.inc_seqlen(pos_d)?;
10729        if cap_bucket_max.is_none() {
10730            cache.pos += 1;
10731        }
10732        Ok(())
10733    }
10734
10735    /// Slot-fed dc attention: mirrors gemma4_decode_attn_dc's CAPTURE arm kernel-for-kernel.
10736    #[allow(clippy::too_many_arguments)]
10737    fn gemma4_decode_attn_dc_slotted(
10738        &self,
10739        e: &Engine,
10740        fa: &crate::hybrid::FullAttnLayer,
10741        il: usize,
10742        pos_d: &CudaSlice<i32>,
10743        cache: &mut Cache,
10744        cap_bucket_max: Option<(usize, usize)>,
10745        sl: &mut G4DcSlots,
10746    ) -> Result<(), Box<dyn std::error::Error>> {
10747        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
10748        let eps = self.cfg.rms_eps;
10749        let aux = self.gemma4_aux.as_ref().unwrap();
10750        let ones = aux.ones(e);
10751        #[cfg(debug_assertions)]
10752        crate::debug_assert_tensor_stream_device(
10753            ones,
10754            &e.stream(),
10755            "gemma4_decode_attn_dc_slotted.ones",
10756        );
10757        {
10758            let hq = unsafe { &*(&sl.hq as *const CudaSlice<i8>) };
10759            let hdq = unsafe { &*(&sl.hd_ as *const CudaSlice<f32>) };
10760            if swa {
10761                if !e.matmul_q4_fused3_into(
10762                    &fa.wq, &fa.wk, &fa.wv, hq, hdq, &mut sl.q0, &mut sl.k0, &mut sl.v0,
10763                )? {
10764                    // NVFP4mix trio is MIXED-type (wv stays Q8_0): fuse the uniform
10765                    // (q,k) pair, v through the generic m1 slot matvec — the same two
10766                    // bodies the eager arm runs, slot-fed for the alloc-free capture.
10767                    if e.matmul_nvfp4_fused2_into(&fa.wq, &fa.wk, hq, hdq, &mut sl.q0, &mut sl.k0)?
10768                    {
10769                        self.g4_matvec_m1_into(e, &fa.wv, hq, hdq, &mut sl.v0)?;
10770                    } else {
10771                        return Err("slotted step: fused3 unavailable (non-uniform trunk)".into());
10772                    }
10773                }
10774            } else {
10775                if !e.matmul_q4_fused2_into(&fa.wq, &fa.wk, hq, hdq, &mut sl.q0, &mut sl.k0)?
10776                    && !e
10777                        .matmul_nvfp4_fused2_into(&fa.wq, &fa.wk, hq, hdq, &mut sl.q0, &mut sl.k0)?
10778                {
10779                    return Err("slotted step: fused2 unavailable".into());
10780                }
10781                let k0r = unsafe { &*(&sl.k0 as *const CudaSlice<f32>) };
10782                e.copy_into(&mut sl.v0, 0, k0r, nkv * hd)?;
10783            }
10784        }
10785        // E4B wave-3 fold, m=1 completion (2026-07-23) — MUST mirror the dc_into arm
10786        // kernel-for-kernel (graph stream-identity gate).
10787        let ff = if swa {
10788            None
10789        } else {
10790            Some(
10791                aux.rope_freqs(e)
10792                    .expect("gemma4 global rope needs rope_freqs.weight"),
10793            )
10794        };
10795        #[cfg(debug_assertions)]
10796        if let Some(ff) = ff {
10797            crate::debug_assert_tensor_stream_device(
10798                ff,
10799                &e.stream(),
10800                "gemma4_decode_attn_dc_slotted.rope_freqs",
10801            );
10802        }
10803        let kvl = cache.kv[il].as_mut().unwrap();
10804        let kv_fp8 = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
10805        if crate::Engine::qkv_append_on() {
10806            // append fold (2026-07-23): mirrors dc_into.
10807            e.rms_norm_qkv_rope_append_dc(
10808                &sl.q0,
10809                &sl.k0,
10810                &sl.v0,
10811                fa.q_norm.float_data(),
10812                fa.k_norm.float_data(),
10813                ones,
10814                &mut sl.q,
10815                &mut sl.k,
10816                &mut sl.v,
10817                hd,
10818                nh,
10819                nkv,
10820                pos_d,
10821                nh,
10822                nkv,
10823                base,
10824                1.0,
10825                ff,
10826                eps,
10827                &mut kvl.k,
10828                &mut kvl.v,
10829                &kvl.len_d,
10830                kvl.k_tok_bytes,
10831                kvl.v_tok_bytes,
10832                kv_fp8,
10833            )?;
10834        } else {
10835            e.rms_norm_qkv_rope(
10836                &sl.q0,
10837                &sl.k0,
10838                &sl.v0,
10839                fa.q_norm.float_data(),
10840                fa.k_norm.float_data(),
10841                ones,
10842                &mut sl.q,
10843                &mut sl.k,
10844                &mut sl.v,
10845                hd,
10846                nh,
10847                nkv,
10848                pos_d,
10849                nh,
10850                nkv,
10851                base,
10852                1.0,
10853                ff,
10854                eps,
10855            )?;
10856            e.append_kv_quantized_dc(
10857                &sl.k,
10858                &sl.v,
10859                &mut kvl.k,
10860                &mut kvl.v,
10861                &kvl.len_d,
10862                kvl.kv_dim_k,
10863                kvl.kv_dim_v,
10864                kvl.k_tok_bytes,
10865                kvl.v_tok_bytes,
10866                kv_fp8,
10867            )?;
10868        }
10869        e.inc_seqlen(&mut kvl.len_d)?;
10870        let (b_swa, b_glob) = cap_bucket_max.expect("slotted step is capture-only");
10871        let k_view = e.view_u8(&kvl.k, kvl.k.len());
10872        let v_view = e.view_u8(&kvl.v, kvl.v.len());
10873        let rows_on = std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0");
10874        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
10875        // combine-q8 emit (wave-5b m=1 port): the rows arms quantize inside the combine —
10876        // the standalone quantize launch runs only on the non-rows fallback. MUST mirror
10877        // the dc_into arm branch-for-branch (stream gate).
10878        let mut fa_q8 = false;
10879        if !swa && hd == 512 && b_glob >= crate::fa512_min_tkv() && rows_on {
10880            e.fa_decode_rows(
10881                &sl.q,
10882                &k_view,
10883                &v_view,
10884                &mut sl.attn,
10885                hd,
10886                nh,
10887                nkv,
10888                b_glob - 1,
10889                1,
10890                scale,
10891                kvl.k_tok_bytes,
10892                kvl.v_tok_bytes,
10893                Some((&kvl.len_d, -1)),
10894                false,
10895                false,
10896                Some((&mut sl.zq, &mut sl.zd)),
10897            )?;
10898            fa_q8 = true;
10899        } else if swa && b_swa > win && hd == 256 && rows_on {
10900            e.fa_decode_rows_w(
10901                &sl.q,
10902                &k_view,
10903                &v_view,
10904                &mut sl.attn,
10905                hd,
10906                nh,
10907                nkv,
10908                &kvl.len_d,
10909                -1,
10910                1,
10911                scale,
10912                win,
10913                kvl.k_tok_bytes,
10914                kvl.v_tok_bytes,
10915                Some((&mut sl.zq, &mut sl.zd)),
10916            )?;
10917            fa_q8 = true;
10918        } else {
10919            let b = if swa { b_swa } else { b_glob };
10920            e.fa_decode_dc(
10921                &sl.q,
10922                &k_view,
10923                &v_view,
10924                &mut sl.attn,
10925                hd,
10926                nh,
10927                nkv,
10928                &kvl.len_d,
10929                b,
10930                scale,
10931                kvl.k_tok_bytes,
10932                kvl.v_tok_bytes,
10933                swa && crate::Engine::wkv_on(),
10934            )?;
10935        }
10936        if !fa_q8 {
10937            let aq = unsafe { &*(&sl.attn as *const CudaSlice<f32>) };
10938            e.quantize_q8_1_into(aq, 1, nh * hd, &mut sl.zq, &mut sl.zd)?;
10939        }
10940        {
10941            let zq = unsafe { &*(&sl.zq as *const CudaSlice<i8>) };
10942            let zd = unsafe { &*(&sl.zd as *const CudaSlice<f32>) };
10943            self.g4_matvec_m1_into(e, &fa.wo, zq, zd, &mut sl.o)?;
10944        }
10945        Ok(())
10946    }
10947
10948    /// Slot-fed dense layer tail: mirrors gemma4_layer_tail_core (t=1, no pre-norm fold) +
10949    /// tail_add_nq kernel-for-kernel; the next layer's (hq, hd_) carry lands in the slots.
10950    fn gemma4_layer_tail_slotted(
10951        &self,
10952        e: &Engine,
10953        layer: &crate::hybrid::HybridLayer,
10954        next_norm: Option<&CudaSlice<f32>>,
10955        sl: &mut G4DcSlots,
10956    ) -> Result<(), Box<dyn std::error::Error>> {
10957        let n_embd = self.cfg.n_embd as usize;
10958        let eps = self.cfg.rms_eps;
10959        let bits = layer.gemma4.as_ref().unwrap();
10960        let crate::hybrid::Ffn::Dense {
10961            ffn_gate,
10962            ffn_up,
10963            ffn_down,
10964        } = &layer.ffn
10965        else {
10966            return Err("slotted tail: dense ffn only".into());
10967        };
10968        let pnfold = Engine::g4_pnfold_on();
10969        if pnfold {
10970            // pn-fold entry (E4B glue, slot-fed): rms(o, post_attn) + residual + ffn_norm
10971            // + q8 emit in ONE launch — replaces rms_norm + add_rms_norm + quantize.
10972            let or = unsafe { &*(&sl.o as *const CudaSlice<f32>) };
10973            let xr = unsafe { &*(&sl.x as *const CudaSlice<f32>) };
10974            e.rms_pre_add_rms_norm_q8z_into(
10975                or,
10976                layer.post_attn_norm.float_data(),
10977                xr,
10978                bits.ffn_norm.float_data(),
10979                &mut sl.attn_out,
10980                &mut sl.zsh,
10981                n_embd,
10982                1,
10983                eps,
10984                &mut sl.zq,
10985                &mut sl.zd,
10986            )?;
10987        } else {
10988            e.add_rms_norm(
10989                &sl.cur,
10990                &sl.x,
10991                bits.ffn_norm.float_data(),
10992                &mut sl.attn_out,
10993                &mut sl.zsh,
10994                n_embd,
10995                1,
10996                eps,
10997            )?;
10998        }
10999        let n_ff = ffn_gate.out_features();
11000        if !pnfold {
11001            let zshr = unsafe { &*(&sl.zsh as *const CudaSlice<f32>) };
11002            e.quantize_q8_1_into(zshr, 1, n_embd, &mut sl.zq, &mut sl.zd)?;
11003        }
11004        {
11005            let zq = unsafe { &*(&sl.zq as *const CudaSlice<i8>) };
11006            let zd = unsafe { &*(&sl.zd as *const CudaSlice<f32>) };
11007            if !e.matmul_q4_fused2_into(ffn_gate, ffn_up, zq, zd, &mut sl.gate, &mut sl.up)?
11008                && !e.matmul_nvfp4_fused2_into(
11009                    ffn_gate,
11010                    ffn_up,
11011                    zq,
11012                    zd,
11013                    &mut sl.gate,
11014                    &mut sl.up,
11015                )?
11016            {
11017                return Err("slotted tail: ffn fused2 unavailable".into());
11018            }
11019        }
11020        debug_assert!(e.uses_q8_1_fast(ffn_down));
11021        {
11022            let upr = unsafe { &*(&sl.up as *const CudaSlice<f32>) };
11023            let upv = e.view(upr, n_ff);
11024            let up_all = upv.slice(0..n_ff);
11025            let gr = unsafe { &*(&sl.gate as *const CudaSlice<f32>) };
11026            e.gelu_tanh_mul_q8_1_into(
11027                gr,
11028                &up_all,
11029                &mut sl.act,
11030                n_ff,
11031                1,
11032                &mut sl.actq,
11033                &mut sl.actd,
11034            )?;
11035        }
11036        {
11037            let aq = unsafe { &*(&sl.actq as *const CudaSlice<i8>) };
11038            let ad = unsafe { &*(&sl.actd as *const CudaSlice<f32>) };
11039            self.g4_matvec_m1_into(e, ffn_down, aq, ad, &mut sl.f0)?;
11040        }
11041        if pnfold {
11042            // pn-fold exit: rms(f0, post_ffw) + scaled residual + next attn_norm + q8
11043            // emit in ONE launch — replaces rms_norm + add_scale_rms_norm_q8_1.
11044            if let Some(w) = next_norm {
11045                let f0r = unsafe { &*(&sl.f0 as *const CudaSlice<f32>) };
11046                let aor = unsafe { &*(&sl.attn_out as *const CudaSlice<f32>) };
11047                e.rms_pre_add_scale_rms_norm_q8_1_into(
11048                    f0r,
11049                    bits.post_ffw_norm.float_data(),
11050                    aor,
11051                    bits.layer_scale,
11052                    w,
11053                    &mut sl.xn,
11054                    n_embd,
11055                    1,
11056                    eps,
11057                    &mut sl.hq,
11058                    &mut sl.hd_,
11059                )?;
11060                return Ok(());
11061            }
11062        }
11063        e.rms_norm(
11064            &sl.f0,
11065            bits.post_ffw_norm.float_data(),
11066            &mut sl.sn,
11067            n_embd,
11068            1,
11069            eps,
11070        )?;
11071        match next_norm {
11072            Some(w) => {
11073                e.add_scale_rms_norm_q8_1_into(
11074                    &sl.sn,
11075                    &sl.attn_out,
11076                    bits.layer_scale,
11077                    w,
11078                    &mut sl.xn,
11079                    n_embd,
11080                    1,
11081                    eps,
11082                    &mut sl.hq,
11083                    &mut sl.hd_,
11084                )?;
11085            }
11086            None => {
11087                e.add_scale(&sl.sn, &sl.attn_out, bits.layer_scale, &mut sl.xn, n_embd)?;
11088            }
11089        }
11090        Ok(())
11091    }
11092
11093    /// dc attention: same math as gemma4_decode_attn, KV slot/lengths from device counters.
11094    #[allow(clippy::too_many_arguments)]
11095    fn gemma4_decode_attn_dc(
11096        &self,
11097        e: &Engine,
11098        fa: &crate::hybrid::FullAttnLayer,
11099        il: usize,
11100        hq: &CudaSlice<i8>,
11101        hdq: &CudaSlice<f32>,
11102        pos_d: &CudaSlice<i32>,
11103        cache: &mut Cache,
11104        cap_bucket_max: Option<(usize, usize)>,
11105    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
11106        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
11107        let eps = self.cfg.rms_eps;
11108        let aux = self.gemma4_aux.as_ref().unwrap();
11109        let ones = aux.ones(e);
11110        #[cfg(debug_assertions)]
11111        crate::debug_assert_tensor_stream_device(ones, &e.stream(), "gemma4_decode_attn_dc.ones");
11112        let (q0, k0, v0) = if swa {
11113            match e.matmul_q4_fused3(&fa.wq, &fa.wk, &fa.wv, hq, hdq)? {
11114                Some(t3) => t3,
11115                // NVFP4mix: wv is Q8_0 (mixed trio) — fuse the (q,k) pair, v single.
11116                None => match e.matmul_nvfp4_fused2(&fa.wq, &fa.wk, hq, hdq, 1)? {
11117                    Some((q0, k0)) => {
11118                        let h0 = e.zeros(0)?;
11119                        let v0 = e.matmul_pre(&fa.wv, hq, hdq, &h0, 1)?;
11120                        (q0, k0, v0)
11121                    }
11122                    None => {
11123                        let h0 = e.zeros(0)?;
11124                        (
11125                            e.matmul_pre(&fa.wq, hq, hdq, &h0, 1)?,
11126                            e.matmul_pre(&fa.wk, hq, hdq, &h0, 1)?,
11127                            e.matmul_pre(&fa.wv, hq, hdq, &h0, 1)?,
11128                        )
11129                    }
11130                },
11131            }
11132        } else {
11133            let (q0, k0) = match e.matmul_q4_fused2(&fa.wq, &fa.wk, hq, hdq)? {
11134                Some(p) => p,
11135                None => match e.matmul_nvfp4_fused2(&fa.wq, &fa.wk, hq, hdq, 1)? {
11136                    Some(p) => p,
11137                    None => {
11138                        let h0 = e.zeros(0)?;
11139                        (
11140                            e.matmul_pre(&fa.wq, hq, hdq, &h0, 1)?,
11141                            e.matmul_pre(&fa.wk, hq, hdq, &h0, 1)?,
11142                        )
11143                    }
11144                },
11145            };
11146            let v0 = e.clone_dtod(&k0)?;
11147            (q0, k0, v0)
11148        };
11149        let mut q = e.uninit(nh * hd)?;
11150        let mut k = e.uninit(nkv * hd)?;
11151        let mut v = e.uninit(nkv * hd)?;
11152        // E4B wave-3 fold, m=1 completion (2026-07-23): mirrored by the slotted arm.
11153        let ff = if swa {
11154            None
11155        } else {
11156            Some(
11157                aux.rope_freqs(e)
11158                    .expect("gemma4 global rope needs rope_freqs.weight"),
11159            )
11160        };
11161        #[cfg(debug_assertions)]
11162        if let Some(ff) = ff {
11163            crate::debug_assert_tensor_stream_device(
11164                ff,
11165                &e.stream(),
11166                "gemma4_decode_attn_dc.rope_freqs",
11167            );
11168        }
11169        let kvl = cache.kv[il].as_mut().unwrap();
11170        let kv_fp8 = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
11171        if crate::Engine::qkv_append_on() {
11172            // append fold (2026-07-23): norm+rope+cache-append in ONE launch.
11173            e.rms_norm_qkv_rope_append_dc(
11174                &q0,
11175                &k0,
11176                &v0,
11177                fa.q_norm.float_data(),
11178                fa.k_norm.float_data(),
11179                ones,
11180                &mut q,
11181                &mut k,
11182                &mut v,
11183                hd,
11184                nh,
11185                nkv,
11186                pos_d,
11187                nh,
11188                nkv,
11189                base,
11190                1.0,
11191                ff,
11192                eps,
11193                &mut kvl.k,
11194                &mut kvl.v,
11195                &kvl.len_d,
11196                kvl.k_tok_bytes,
11197                kvl.v_tok_bytes,
11198                kv_fp8,
11199            )?;
11200        } else {
11201            e.rms_norm_qkv_rope(
11202                &q0,
11203                &k0,
11204                &v0,
11205                fa.q_norm.float_data(),
11206                fa.k_norm.float_data(),
11207                ones,
11208                &mut q,
11209                &mut k,
11210                &mut v,
11211                hd,
11212                nh,
11213                nkv,
11214                pos_d,
11215                nh,
11216                nkv,
11217                base,
11218                1.0,
11219                ff,
11220                eps,
11221            )?;
11222            e.append_kv_quantized_dc(
11223                &k,
11224                &v,
11225                &mut kvl.k,
11226                &mut kvl.v,
11227                &kvl.len_d,
11228                kvl.kv_dim_k,
11229                kvl.kv_dim_v,
11230                kvl.k_tok_bytes,
11231                kvl.v_tok_bytes,
11232                kv_fp8,
11233            )?;
11234        }
11235        e.inc_seqlen(&mut kvl.len_d)?;
11236        let mut attn = e.uninit(nh * hd)?;
11237        // combine-q8 emit carrier (wave-5b m=1 port): rows arms fill this; the tail then
11238        // rides g4_matvec_m1_into instead of matmul's internal quantize.
11239        let mut fa_q8: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
11240        // Weight prefetch NOT wired here (26B/31B/qwen probes 2026-07-13: 26B flat
11241        // 196.6/196.0 vs 196.4/196.1 — MoE ffn dilutes wo; 31B −0.2% — dense decode sits
11242        // at the DRAM wall, no idle window to front-load into). E4B keeps the arm
11243        // (gemma4_e4b_attn, +0.65% valid window).
11244        match cap_bucket_max {
11245            None => {
11246                // dc-EAGER: host knows the length — R6 window views EXACTLY like the eager
11247                // decode (SWA layers attend the last `sliding_window` keys); the device
11248                // counters carry only the append slot + the graph seam.
11249                kvl.len += 1;
11250                let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
11251                if !swa
11252                    && hd == 512
11253                    && kvl.len >= crate::fa512_min_tkv()
11254                    && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0")
11255                {
11256                    // global layers: SAME hd512 rows twin as verify, t=1 (parity law).
11257                    // dc: len_d is live (inc_seqlen) — device-len rides it, plus=-1.
11258                    let kp = e.view_u8(&kvl.k, kvl.len * kvl.k_tok_bytes);
11259                    let vp = e.view_u8(&kvl.v, kvl.len * kvl.v_tok_bytes);
11260                    let (mut aq8, mut ad8) = e.uninit_q8_pair(nh * hd)?;
11261                    e.fa_decode_rows(
11262                        &q,
11263                        &kp,
11264                        &vp,
11265                        &mut attn,
11266                        hd,
11267                        nh,
11268                        nkv,
11269                        kvl.len - 1,
11270                        1,
11271                        scale,
11272                        kvl.k_tok_bytes,
11273                        kvl.v_tok_bytes,
11274                        Some((&kvl.len_d, -1)),
11275                        false,
11276                        false,
11277                        Some((&mut aq8, &mut ad8)),
11278                    )?;
11279                    fa_q8 = Some((aq8, ad8));
11280                } else if swa
11281                    && kvl.len > win
11282                    && hd == 256
11283                    && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0")
11284                {
11285                    // windowed regime: SAME rows_w kernel as verify, t=1 (parity law).
11286                    let kp = e.view_u8(&kvl.k, kvl.len * kvl.k_tok_bytes);
11287                    let vp = e.view_u8(&kvl.v, kvl.len * kvl.v_tok_bytes);
11288                    let (mut aq8, mut ad8) = e.uninit_q8_pair(nh * hd)?;
11289                    e.fa_decode_rows_w(
11290                        &q,
11291                        &kp,
11292                        &vp,
11293                        &mut attn,
11294                        hd,
11295                        nh,
11296                        nkv,
11297                        &kvl.len_d,
11298                        -1,
11299                        1,
11300                        scale,
11301                        win,
11302                        kvl.k_tok_bytes,
11303                        kvl.v_tok_bytes,
11304                        Some((&mut aq8, &mut ad8)),
11305                    )?;
11306                    fa_q8 = Some((aq8, ad8));
11307                } else {
11308                    let (off_tok, t_kv) = if swa && kvl.len > win {
11309                        (kvl.len - win, win)
11310                    } else {
11311                        (0, kvl.len)
11312                    };
11313                    let k_view = e.view_u8_range(
11314                        &kvl.k,
11315                        off_tok * kvl.k_tok_bytes,
11316                        (off_tok + t_kv) * kvl.k_tok_bytes,
11317                    );
11318                    let v_view = e.view_u8_range(
11319                        &kvl.v,
11320                        off_tok * kvl.v_tok_bytes,
11321                        (off_tok + t_kv) * kvl.v_tok_bytes,
11322                    );
11323                    e.fa_decode_kvmod(
11324                        &q,
11325                        &k_view,
11326                        &v_view,
11327                        &mut attn,
11328                        hd,
11329                        nh,
11330                        nkv,
11331                        t_kv,
11332                        scale,
11333                        kvl.k_tok_bytes,
11334                        kvl.v_tok_bytes,
11335                        swa && crate::Engine::wkv_on(),
11336                    )?;
11337                }
11338            }
11339            Some((b_swa, b_glob)) => {
11340                // capture: full-buffer views + device length. PARITY LAW port (graph arc step
11341                // 3): the SAME regime branches as dc-eager, pinned per arm — b_swa is the
11342                // exact-key t_kv for the dc family (n_splits constant per bucket), b_glob is
11343                // the RUNG max for the rows family (kernels derive per-replay splits from
11344                // kvl.len_d; grid/partials sized for the rung end, excess splits exit).
11345                let k_view = e.view_u8(&kvl.k, kvl.k.len());
11346                let v_view = e.view_u8(&kvl.v, kvl.v.len());
11347                let rows_on = std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0");
11348                let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
11349                if !swa && hd == 512 && b_glob >= crate::fa512_min_tkv() && rows_on {
11350                    let (mut aq8, mut ad8) = e.uninit_q8_pair(nh * hd)?;
11351                    e.fa_decode_rows(
11352                        &q,
11353                        &k_view,
11354                        &v_view,
11355                        &mut attn,
11356                        hd,
11357                        nh,
11358                        nkv,
11359                        b_glob - 1,
11360                        1,
11361                        scale,
11362                        kvl.k_tok_bytes,
11363                        kvl.v_tok_bytes,
11364                        Some((&kvl.len_d, -1)),
11365                        false,
11366                        false,
11367                        Some((&mut aq8, &mut ad8)),
11368                    )?;
11369                    fa_q8 = Some((aq8, ad8));
11370                } else if swa && b_swa > win && hd == 256 && rows_on {
11371                    let (mut aq8, mut ad8) = e.uninit_q8_pair(nh * hd)?;
11372                    e.fa_decode_rows_w(
11373                        &q,
11374                        &k_view,
11375                        &v_view,
11376                        &mut attn,
11377                        hd,
11378                        nh,
11379                        nkv,
11380                        &kvl.len_d,
11381                        -1,
11382                        1,
11383                        scale,
11384                        win,
11385                        kvl.k_tok_bytes,
11386                        kvl.v_tok_bytes,
11387                        Some((&mut aq8, &mut ad8)),
11388                    )?;
11389                    fa_q8 = Some((aq8, ad8));
11390                } else {
11391                    let b = if swa { b_swa } else { b_glob };
11392                    e.fa_decode_dc(
11393                        &q,
11394                        &k_view,
11395                        &v_view,
11396                        &mut attn,
11397                        hd,
11398                        nh,
11399                        nkv,
11400                        &kvl.len_d,
11401                        b,
11402                        scale,
11403                        kvl.k_tok_bytes,
11404                        kvl.v_tok_bytes,
11405                        swa && crate::Engine::wkv_on(),
11406                    )?;
11407                }
11408            }
11409        }
11410        // combine-q8 emit (wave-5b m=1 port): the rows arms produced the wo activation pair
11411        // in-combine — ride the slotted arm's exact matvec route (parity by construction).
11412        if let Some((aq8, ad8)) = fa_q8 {
11413            let mut y = e.uninit(fa.wo.out_features())?;
11414            self.g4_matvec_m1_into(e, &fa.wo, &aq8, &ad8, &mut y)?;
11415            return Ok(y);
11416        }
11417        Ok(e.matmul(&fa.wo, &attn, 1)?)
11418    }
11419
11420    /// gemma4 GRAPH-REPLAY greedy loop: per (swa-key, global-key) fa bucket, capture ONE full
11421    /// dc step (self-feeding: argmax writes token_d in-graph) and replay it — one graph launch
11422    /// per token, one 4B dtoh. V1 scope: whole generation under the sliding window (no window
11423    /// views in-graph); caller gates and falls back to the dc-eager loop.
11424    pub fn gemma4_generate_graph(
11425        &self,
11426        e: &Engine,
11427        prompt_pos: usize,
11428        first_token: u32,
11429        cache: &mut Cache,
11430        max_new: usize,
11431        eos: &[u32],
11432        mut on_token: impl FnMut(u32) -> bool,
11433    ) -> Result<(Vec<u32>, crate::decode::StopReason), Box<dyn std::error::Error>> {
11434        if self.is_gemma4_e4b() {
11435            return Err(
11436                "E4B graph serving is unwired (HANDOVER-E4B.md) — dc-eager is the serving arm"
11437                    .into(),
11438            );
11439        }
11440        use crate::decode::StopReason;
11441        let n_vocab = self.output.out_features();
11442        let n_embd = self.cfg.n_embd as usize;
11443        let embd_gpu = self
11444            .embd_gpu
11445            .get_or_init(|| e.upload_u8(&self.embd.raw).expect("embed table upload"));
11446        let (qt, rb) = self.embd.qt_and_row_bytes(n_embd);
11447        for kvl in cache.kv.iter_mut().flatten() {
11448            e.set_i32_one(&mut kvl.len_d, kvl.len as i32)?;
11449        }
11450        let mut token_d = e.stream().clone_htod(&[first_token])?;
11451        let mut pos_d = e.htod_i32(&[prompt_pos as i32])?;
11452        let g4 = self.cfg.gemma4.as_ref().unwrap();
11453        let (hd_s, hd_g) = (g4.key_length_swa as usize, g4.key_length_global as usize);
11454        // per-layer nkv: swa vs global counts from the pattern (uniform within class).
11455        let nkv_s = g4
11456            .head_count_kv
11457            .iter()
11458            .zip(g4.swa_pattern.iter())
11459            .find(|p| *p.1)
11460            .map(|p| *p.0 as usize)
11461            .unwrap_or(8);
11462        let nkv_g = g4
11463            .head_count_kv
11464            .iter()
11465            .zip(g4.swa_pattern.iter())
11466            .find(|p| !*p.1)
11467            .map(|p| *p.0 as usize)
11468            .unwrap_or(2);
11469        let mut graphs: std::collections::HashMap<
11470            ((bool, usize), (bool, usize), bool, bool),
11471            (
11472                cudarc::driver::CudaGraph,
11473                Vec<Box<dyn std::any::Any + Send>>,
11474            ),
11475        > = Default::default();
11476        // ALLOC-FREE capture: persistent transient slots — the captured graph carries zero
11477        // mem nodes (the 226us/launch tax). Slots must outlive every cached graph.
11478        let mut slots = self.g4_dc_slots(e)?;
11479        // Chunked replay ring: tokens park on-device; ONE drain sync per chunk. ring_base is
11480        // baked at the door entry (the modulo keeps every capture valid indefinitely).
11481        const RING: usize = 64;
11482        // DRAIN pinned at 1 (2026-07-23): relaunching the SAME graph exec before its prior
11483        // launch completes is ILLEGAL (chunk=4 -> ILLEGAL_ADDRESS; chunk=1 clean). Pipelining
11484        // needs alternating exec instances — and the measured payoff was ~0 (the ~200us
11485        // cuGraphLaunch host cost already overlaps its own launch's GPU work; llama pays
11486        // 885us/launch the same way). MEMRA_GRAPH_DRAIN raises it only for experiments.
11487        const DRAIN: usize = 1;
11488        let mut ring = e.stream().alloc_zeros::<u32>(RING)?;
11489        let ring_base = prompt_pos;
11490        let mut out = Vec::with_capacity(max_new);
11491        let mut reason = StopReason::MaxNew;
11492        let mut next = first_token;
11493        let mut captures = 0usize;
11494        for _ in 0..max_new {
11495            out.push(next);
11496            if eos.contains(&next) {
11497                reason = StopReason::Eos;
11498                break;
11499            }
11500            if !on_token(next) {
11501                reason = StopReason::Callback;
11502                break;
11503            }
11504            let t_kv = cache.pos + 1;
11505            // Bucket key per ARM (graph arc step 3):
11506            //  - swa component: dc family under the window (exact (fa_vec, n_splits) key —
11507            //    n_splits must be constant per bucket); rows_w above it (window-constant, so
11508            //    the component collapses to a single marker).
11509            //  - global component: dc family under the fa512 floor (exact key); rows_dpl16
11510            //    at/above it — the kernel derives splits from len_d per replay, so buckets
11511            //    are power-of-2 RUNGS (one capture per doubling; grid sized for the rung end).
11512            let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
11513            let f512 = crate::fa512_min_tkv();
11514            let key_s = if t_kv > win {
11515                (true, usize::MAX)
11516            } else {
11517                e.fa_bucket_key(t_kv, hd_s, nkv_s, crate::Engine::wkv_on())
11518            };
11519            let (key_g, rung_end) = if t_kv >= f512 {
11520                // strict upper bound: the rung must ROLL at exact powers (t_kv==1024 starts
11521                // the [1024,2048) bucket) — sizing covers every replayed T_kv < end.
11522                let end = (t_kv + 1).next_power_of_two().max(f512 * 2);
11523                ((true, end), end)
11524            } else {
11525                (e.fa_bucket_key(t_kv, hd_g, nkv_g, false), t_kv)
11526            };
11527            let key = (key_s, key_g, t_kv >= f512, t_kv > win);
11528            if !graphs.contains_key(&key) {
11529                let bucket_max = (t_kv, rung_end);
11530                // snapshot device+host state (the 3 capture-warmup runs leave no residue).
11531                let snap = cache.snapshot(e)?;
11532                let pos_save = e.dtoh_i32_one(&pos_d)?;
11533                let len_save: Vec<Option<i32>> = cache
11534                    .kv
11535                    .iter()
11536                    .map(|k| k.as_ref().map(|kvl| e.dtoh_i32_one(&kvl.len_d).unwrap()))
11537                    .collect();
11538                let tok_save = e.dtoh_u32_one(&token_d)?;
11539                // RETAINED capture (2026-07-23): the plain capture_graph left pool-transient
11540                // clones as dead COPY NODES replayed every launch — the E4B 0.74ms/token
11541                // regression class, and this door's measured -8.8%. The keeper pins warmup
11542                // transients so the captured graph holds kernel nodes only.
11543                let graph = {
11544                    let tok_ref = &mut token_d;
11545                    let pos_ref = &mut pos_d;
11546                    let cache_ref = &mut *cache;
11547                    let slots_ref = &mut slots;
11548                    let ring_ref = &mut ring;
11549                    e.capture_graph_retained_flags(
11550                        cudarc::driver::sys::CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_USE_NODE_PRIORITY,
11551                        |e| {
11552                        // self-feeding: the argmax writes token_d itself.
11553                        let tok_in = unsafe { &*(tok_ref as *const CudaSlice<u32>) };
11554                        let sl = unsafe { &mut *(slots_ref as *mut G4DcSlots) };
11555                        let rg = unsafe { &mut *(ring_ref as *mut CudaSlice<u32>) };
11556                        self.gemma4_decode_step_dc_slotted(e, tok_in, pos_ref, embd_gpu, qt, rb,
11557                                                           cache_ref, n_vocab, Some(bucket_max),
11558                                                           sl, tok_ref, Some((rg, ring_base)))
11559                    })?
11560                };
11561                cache.rollback(e, &snap, 0)?;
11562                e.set_i32_one(&mut pos_d, pos_save)?;
11563                for (il, ls) in len_save.iter().enumerate() {
11564                    if let (Some(kvl), Some(v)) = (cache.kv[il].as_mut(), ls) {
11565                        e.set_i32_one(&mut kvl.len_d, *v)?;
11566                    }
11567                }
11568                e.set_u32_one(&mut token_d, tok_save)?;
11569                if std::env::var("MEMRA_GRAPH_CENSUS").as_deref() == Ok("1") {
11570                    if let Ok(c) = crate::graph_update::node_census(&graph.0) {
11571                        eprintln!("[graph-census] {c:?}");
11572                    }
11573                }
11574                graphs.insert(key, graph);
11575                captures += 1;
11576            }
11577            // CHUNKED REPLAY: enqueue up to DRAIN launches back-to-back (the ~200us host
11578            // cost of each cuGraphLaunch overlaps the PREVIOUS launch's ~11ms GPU work),
11579            // then ONE sync + ring drain. The chunk must stay inside this bucket key and
11580            // the budget; capture warmups already emitted their tokens through the ring.
11581            let mut chunk = 1usize;
11582            let drain_cap: usize = std::env::var("MEMRA_GRAPH_DRAIN")
11583                .ok()
11584                .and_then(|v| v.parse().ok())
11585                .unwrap_or(DRAIN);
11586            while chunk < drain_cap && out.len() + chunk < max_new {
11587                let t_next = cache.pos + 1 + chunk;
11588                let key_s2 = if t_next > win {
11589                    (true, usize::MAX)
11590                } else {
11591                    e.fa_bucket_key(t_next, hd_s, nkv_s, crate::Engine::wkv_on())
11592                };
11593                let key_g2 = if t_next >= f512 {
11594                    (true, (t_next + 1).next_power_of_two().max(f512 * 2))
11595                } else {
11596                    e.fa_bucket_key(t_next, hd_g, nkv_g, false)
11597                };
11598                if (key_s2, key_g2, t_next >= f512, t_next > win) != key {
11599                    break;
11600                }
11601                chunk += 1;
11602            }
11603            let g = &graphs.get(&key).unwrap().0;
11604            for _ in 0..chunk {
11605                g.launch()?;
11606            }
11607            e.stream().synchronize()?;
11608            let ringh = e.dtoh_u32(&ring)?;
11609            for j in 0..chunk {
11610                let pos_j = cache.pos + j;
11611                let tok_j = ringh[(pos_j - ring_base) % RING];
11612                cache.pos += 0; // advanced below in one shot
11613                if j + 1 == chunk {
11614                    next = tok_j;
11615                } else {
11616                    out.push(tok_j);
11617                    if eos.contains(&tok_j) || !on_token(tok_j) {
11618                        reason = if eos.contains(&tok_j) {
11619                            StopReason::Eos
11620                        } else {
11621                            StopReason::Callback
11622                        };
11623                        // roll device/host state back to the stop point.
11624                        let keep = cache.pos + j + 1;
11625                        e.set_i32_one(&mut pos_d, keep as i32)?;
11626                        for kvl in cache.kv.iter_mut().filter_map(|k| k.as_mut()) {
11627                            e.set_i32_one(&mut kvl.len_d, keep as i32)?;
11628                            kvl.len = keep;
11629                        }
11630                        cache.pos = keep;
11631                        if std::env::var("MEMRA_GRAPH_STATS").is_ok() {
11632                            eprintln!("[gemma-graph] captures={captures} buckets={}", graphs.len());
11633                        }
11634                        return Ok((out, reason));
11635                    }
11636                }
11637            }
11638            cache.pos += chunk;
11639            for kvl in cache.kv.iter_mut().filter_map(|k| k.as_mut()) {
11640                kvl.len += chunk;
11641            }
11642        }
11643        if std::env::var("MEMRA_GRAPH_STATS").is_ok() {
11644            eprintln!("[gemma-graph] captures={captures} buckets={}", graphs.len());
11645        }
11646        Ok((out, reason))
11647    }
11648
11649    /// gemma4 VERIFY step (spec decode): t tokens batched through the trunk at positions
11650    /// pos0..pos0+t-1, K/V rows appended to the quantized cache (caller rolls back rejected
11651    /// rows via kvl.len), per-token causal windowed attend (fa_decode per token — the same
11652    /// kernel family as T=1 decode at each token's t_kv). Returns [t, n_vocab] softcapped
11653    /// logits (host) + advances cache.pos by t.
11654    pub(crate) fn gemma4_decode_step_t(
11655        &self,
11656        e: &Engine,
11657        tokens: &[u32],
11658        pos0: usize,
11659        cache: &mut Cache,
11660    ) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
11661        Ok(self.gemma4_decode_step_t_h(e, tokens, pos0, cache)?.0)
11662    }
11663
11664    /// GREEDY verify: per-row DEVICE argmax (t x 4B host traffic instead of the t x 1MB logits
11665    /// stack — softcap skipped: tanh is monotonic, per-row argmax unaffected). Returns
11666    /// (argmax ids [t], post-output_norm hidden stack [t, n_embd]).
11667    pub(crate) fn gemma4_decode_step_t_am(
11668        &self,
11669        e: &Engine,
11670        tokens: &[u32],
11671        pos0: usize,
11672        cache: &mut Cache,
11673    ) -> Result<(Vec<u32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
11674        let (ld, hn) = self.gemma4_verify_trunk(e, tokens, pos0, cache, None)?;
11675        let t = tokens.len();
11676        let n_vocab = self.output.out_features();
11677        let mut toks = e.stream().alloc_zeros::<u32>(t)?;
11678        for i in 0..t {
11679            e.argmax_token_device_col(&ld, i, n_vocab, &mut toks, i)?;
11680        }
11681        Ok((e.dtoh_u32(&toks)?, hn))
11682    }
11683
11684    /// Device-token verify (async spec round): tokens live in tok_d[0..t]; per-row argmax
11685    /// lands in a DEVICE buffer (no host logits). Returns (vam_d [t] u32 device, hn stack).
11686    pub(crate) fn gemma4_decode_step_t_am_dev(
11687        &self,
11688        e: &Engine,
11689        tok_d: &CudaSlice<u32>,
11690        t: usize,
11691        pos0: usize,
11692        cache: &mut Cache,
11693    ) -> Result<(CudaSlice<u32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
11694        let (ld, hn) = self.gemma4_verify_trunk(e, &vec![0u32; t], pos0, cache, Some(tok_d))?;
11695        let n_vocab = self.output.out_features();
11696        let mut vam = e.stream().alloc_zeros::<u32>(t)?;
11697        for i in 0..t {
11698            e.argmax_token_device_col(&ld, i, n_vocab, &mut vam, i)?;
11699        }
11700        Ok((vam, hn))
11701    }
11702
11703    /// gemma4 verify + the POST-output_norm hidden stack [t, n_embd] (the drafter's h input —
11704    /// llama's h_nextn convention).
11705    pub(crate) fn gemma4_decode_step_t_h(
11706        &self,
11707        e: &Engine,
11708        tokens: &[u32],
11709        pos0: usize,
11710        cache: &mut Cache,
11711    ) -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
11712        let (mut ld, hn) = self.gemma4_verify_trunk(e, tokens, pos0, cache, None)?;
11713        let t = tokens.len();
11714        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
11715        e.softcap(&mut ld, cap, t * self.output.out_features())?;
11716        Ok((e.dtoh(&ld)?, hn))
11717    }
11718
11719    /// Persistent device scratch for the BURST verify stream (one per generation): rope pos
11720    /// rows [cap] + per-row t_kv counters (device-filled from the round counter, zero H2D).
11721    pub(crate) fn verify_stream_scratch(
11722        &self,
11723        e: &Engine,
11724        cap: usize,
11725    ) -> Result<VerifyStreamScratch, Box<dyn std::error::Error>> {
11726        Ok(VerifyStreamScratch {
11727            pos_d: e.htod_i32(&vec![0i32; cap])?,
11728            row_ctrs: (0..cap)
11729                .map(|_| e.htod_i32(&[0]))
11730                .collect::<Result<_, _>>()?,
11731        })
11732    }
11733
11734    /// BURST verify trunk (device-slot twin): tokens from a device buffer, rope positions
11735    /// iota'd from `ctr`, appends/attention at the layers' len_d counters (verify_attn_stream),
11736    /// NO host cache.pos/len advance. Returns (per-row device argmaxes [t], post-norm hidden
11737    /// stack [t, n_embd]) — the burst's accept/seed inputs, zero host readbacks.
11738    /// `scr` = PERSISTENT per-gen scratch (pos rows + per-row t_kv counters): the first cut
11739    /// htod_i32-allocated these per call — 9 pageable H2D copies per round, each a stream
11740    /// sync, exactly the turnaround the burst exists to remove.
11741    pub(crate) fn gemma4_verify_t_am_stream(
11742        &self,
11743        e: &Engine,
11744        tok_d: &CudaSlice<u32>,
11745        t: usize,
11746        ctr: &CudaSlice<i32>,
11747        hint: usize,
11748        cache: &mut Cache,
11749        scr: &mut VerifyStreamScratch,
11750    ) -> Result<(CudaSlice<u32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
11751        let n_embd = self.cfg.n_embd as usize;
11752        let eps = self.cfg.rms_eps;
11753        assert!(t <= scr.row_ctrs.len() && t <= 64);
11754        e.i32_iota_from(ctr, &mut scr.pos_d, t)?;
11755        for i in 0..t {
11756            e.i32_copy_add(ctr, &mut scr.row_ctrs[i], (i + 1) as i32)?;
11757        }
11758        let (pos_d, row_ctrs) = (&scr.pos_d, &scr.row_ctrs);
11759        let embd_gpu = self
11760            .embd_gpu
11761            .get_or_init(|| e.upload_u8(&self.embd.raw).expect("embed table upload"));
11762        let (qt, rb) = self.embd.qt_and_row_bytes(n_embd);
11763        let mut x = e.embed_gather_device_td(embd_gpu, tok_d, t, n_embd, qt, rb)?;
11764        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
11765        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
11766        let n_layers = self.layers.len();
11767        for (il, layer) in self.layers.iter().enumerate() {
11768            let (hq, hdq) = match h_carry.take() {
11769                Some(p) => p,
11770                None => {
11771                    e.rms_norm_q8_1(&x, self.layers[0].attn_norm.float_data(), n_embd, t, eps)?
11772                }
11773            };
11774            let Mixer::Full(fa) = &layer.mixer else {
11775                panic!("gemma4 layer {il} not full-attn")
11776            };
11777            let o = self
11778                .gemma4_verify_attn_stream(e, fa, il, &hq, &hdq, pos_d, t, cache, hint, row_ctrs)?;
11779            let next_norm = if il + 1 < n_layers {
11780                Some(self.layers[il + 1].attn_norm.float_data())
11781            } else {
11782                None
11783            };
11784            let (xn, hn) = self.gemma4_layer_tail_add_nq_pn(e, layer, &o, &x, t, next_norm)?;
11785            x = xn;
11786            h_carry = hn;
11787            self.dflash_tap(e, cache, il, &x, t)?;
11788        }
11789        let mut hn = e.uninit(t * n_embd)?;
11790        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
11791        let ld = e.matmul(&self.output, &hn, t)?;
11792        let n_vocab = self.output.out_features();
11793        let mut vam = e.stream().alloc_zeros::<u32>(t)?;
11794        for i in 0..t {
11795            e.argmax_token_device_col(&ld, i, n_vocab, &mut vam, i)?;
11796        }
11797        Ok((vam, hn))
11798    }
11799
11800    /// Verify trunk core: returns (UN-softcapped logits device [t, n_vocab], post-output_norm
11801    /// hidden stack [t, n_embd]); appends KV rows + advances cache.pos.
11802    /// DFlash tap write (dflash lane): copy the post-layer residual rows of `x` into the
11803    /// armed sink at the tap slot for `il` (row-major [t, n_taps*hidden]). Per-row D2D
11804    /// copies — t <= block_size on verify; prime pays t*n_taps once per prompt (dedicated
11805    /// kernel later if it shows in the profile).
11806    pub(crate) fn dflash_tap(
11807        &self,
11808        e: &Engine,
11809        cache: &mut Cache,
11810        il: usize,
11811        x: &CudaSlice<f32>,
11812        t: usize,
11813    ) -> Result<(), Box<dyn std::error::Error>> {
11814        let Some(taps) = cache.dflash_taps.as_mut() else {
11815            return Ok(());
11816        };
11817        let Some(slot) = taps.layer_ids.iter().position(|&l| l == il) else {
11818            return Ok(());
11819        };
11820        let h = taps.hidden;
11821        let n_taps = taps.layer_ids.len();
11822        let base = taps.base;
11823        debug_assert!(
11824            base + t <= taps.t,
11825            "tap window {base}+{t} exceeds sink {}",
11826            taps.t
11827        );
11828        let xv = e.view(x, t * h);
11829        for r in 0..t {
11830            let row = xv.slice(r * h..(r + 1) * h);
11831            e.copy_view_into(&mut taps.buf, (base + r) * n_taps * h + slot * h, &row, h)?;
11832        }
11833        Ok(())
11834    }
11835
11836    fn gemma4_verify_trunk(
11837        &self,
11838        e: &Engine,
11839        tokens: &[u32],
11840        pos0: usize,
11841        cache: &mut Cache,
11842        tok_dev: Option<&CudaSlice<u32>>,
11843    ) -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
11844        let n_embd = self.cfg.n_embd as usize;
11845        let eps = self.cfg.rms_eps;
11846        let t = tokens.len();
11847        let pos: Vec<i32> = (0..t).map(|i| (pos0 + i) as i32).collect();
11848        let pos_d = e.htod_i32(&pos)?;
11849        let mut x = match tok_dev {
11850            Some(td) => {
11851                let embd_gpu = self
11852                    .embd_gpu
11853                    .get_or_init(|| e.upload_u8(&self.embd.raw).expect("embed table upload"));
11854                let (qt, rb) = self.embd.qt_and_row_bytes(n_embd);
11855                e.embed_gather_device_td(embd_gpu, td, t, n_embd, qt, rb)?
11856            }
11857            None => e.htod(&self.embd.gather(n_embd, tokens))?,
11858        };
11859        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
11860        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
11861        let n_layers = self.layers.len();
11862        for (il, layer) in self.layers.iter().enumerate() {
11863            let (hq, hdq) = match h_carry.take() {
11864                Some(p) => p,
11865                None => {
11866                    e.rms_norm_q8_1(&x, self.layers[0].attn_norm.float_data(), n_embd, t, eps)?
11867                }
11868            };
11869            let Mixer::Full(fa) = &layer.mixer else {
11870                panic!("gemma4 layer {il} not full-attn")
11871            };
11872            let o = self.gemma4_verify_attn(e, fa, il, &hq, &hdq, &pos_d, t, cache)?;
11873            let next_norm = if il + 1 < n_layers {
11874                Some(self.layers[il + 1].attn_norm.float_data())
11875            } else {
11876                None
11877            };
11878            let (xn, hn) = self.gemma4_layer_tail_add_nq_pn(e, layer, &o, &x, t, next_norm)?;
11879            x = xn;
11880            h_carry = hn;
11881            self.dflash_tap(e, cache, il, &x, t)?;
11882        }
11883        let mut hn = e.uninit(t * n_embd)?;
11884        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
11885        let mut ld = e.matmul(&self.output, &hn, t)?;
11886        self.gemma4_suppress(e, &mut ld, t)?; // before the per-row argmax consumers
11887        cache.pos += t;
11888        Ok((ld, hn))
11889    }
11890
11891    /// Verify attention: project/norm/rope t rows, append them to the cache, then attend each
11892    /// row causally over [win_off_i .. base+i] via the SAME fa_decode dispatch as T=1 decode.
11893    /// BURST verify attention (device-slot twin of `gemma4_verify_attn`): the append lands
11894    /// at the layer's len_d counter (rows_dc), attention bases ride the counter (rows /
11895    /// rows_w with base_dev), and NO host len is read or bumped — `hint` is a host UPPER
11896    /// bound on base_len used only for split sizing and arm gating (the burst loop passes
11897    /// pos0 + burst slack). Host len mirrors re-sync at the burst drain.
11898    #[allow(clippy::too_many_arguments)]
11899    fn gemma4_verify_attn_stream(
11900        &self,
11901        e: &Engine,
11902        fa: &crate::hybrid::FullAttnLayer,
11903        il: usize,
11904        hq: &CudaSlice<i8>,
11905        hdq: &CudaSlice<f32>,
11906        pos_d: &CudaSlice<i32>,
11907        t: usize,
11908        cache: &mut Cache,
11909        hint: usize,
11910        row_ctrs: &[CudaSlice<i32>],
11911    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
11912        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
11913        let eps = self.cfg.rms_eps;
11914        let aux = self.gemma4_aux.as_ref().unwrap();
11915        let ones = aux.ones(e);
11916        #[cfg(debug_assertions)]
11917        crate::debug_assert_tensor_stream_device(
11918            ones,
11919            &e.stream(),
11920            "gemma4_verify_attn_stream.ones",
11921        );
11922        let h0 = e.zeros(0)?;
11923        let h = &h0;
11924        // BATCHED FUSED qkv (MEMRA_F2B=1, megakernel microcosm): swa layers fuse all three,
11925        // globals fuse q,k (v := k clone). Bit-identical per row; segments tail-fill.
11926        static F2B_QKV: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
11927        let f2b = *F2B_QKV.get_or_init(|| std::env::var("MEMRA_F2B").as_deref() != Ok("0"));
11928        let fused_qkv = if f2b {
11929            if swa {
11930                e.matmul_q4_fused3_batched(&fa.wq, &fa.wk, &fa.wv, hq, hdq, t)?
11931                    .map(|(a, b, c)| (a, b, Some(c)))
11932            } else {
11933                e.matmul_q4_fused2_batched(&fa.wq, &fa.wk, hq, hdq, t)?
11934                    .map(|(a, b)| (a, b, None))
11935            }
11936        } else {
11937            None
11938        };
11939        let (q0, k0, v0) = match fused_qkv {
11940            Some((a, b, cv)) => {
11941                let v = match cv {
11942                    Some(c) => c,
11943                    None => e.clone_dtod(&b)?,
11944                };
11945                (a, b, v)
11946            }
11947            None => {
11948                let q0 = e.matmul_pre(&fa.wq, hq, hdq, h, t)?;
11949                let k0 = e.matmul_pre(&fa.wk, hq, hdq, h, t)?;
11950                let v0 = if swa {
11951                    e.matmul_pre(&fa.wv, hq, hdq, h, t)?
11952                } else {
11953                    e.clone_dtod(&k0)?
11954                };
11955                (q0, k0, v0)
11956            }
11957        };
11958        let mut q = e.uninit(t * nh * hd)?;
11959        let mut k = e.uninit(t * nkv * hd)?;
11960        let mut v = e.uninit(t * nkv * hd)?;
11961        // E4B wave-3 fold (2026-07-23 decode-dust port): q/k/v norms + q/k rope in ONE
11962        // launch — rope math verbatim on the normed rows; V ones-rms, never roped.
11963        let ff = if swa {
11964            None
11965        } else {
11966            Some(
11967                aux.rope_freqs(e)
11968                    .expect("gemma4 global rope needs rope_freqs.weight"),
11969            )
11970        };
11971        #[cfg(debug_assertions)]
11972        if let Some(ff) = ff {
11973            crate::debug_assert_tensor_stream_device(
11974                ff,
11975                &e.stream(),
11976                "gemma4_verify_attn_stream.rope_freqs",
11977            );
11978        }
11979        e.rms_norm_qkv_rope(
11980            &q0,
11981            &k0,
11982            &v0,
11983            fa.q_norm.float_data(),
11984            fa.k_norm.float_data(),
11985            ones,
11986            &mut q,
11987            &mut k,
11988            &mut v,
11989            hd,
11990            nh * t,
11991            nkv * t,
11992            pos_d,
11993            nh,
11994            nkv,
11995            base,
11996            1.0,
11997            ff,
11998            eps,
11999        )?;
12000        let kvl = cache.kv[il].as_mut().unwrap();
12001        // append at the DEVICE slot; the counter advances by t on-device.
12002        e.append_kv_quantized_rows_dc(
12003            &k,
12004            &v,
12005            &mut kvl.k,
12006            &mut kvl.v,
12007            &kvl.len_d,
12008            t,
12009            kvl.kv_dim_k,
12010            kvl.kv_dim_v,
12011            kvl.k_tok_bytes,
12012            kvl.v_tok_bytes,
12013            (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on()),
12014        )?;
12015        // len_d is NOT advanced here: the burst's device rollback (spec_rollback_stream) is
12016        // the sole len writer after this round's attention (base stays = old len, plus = 0).
12017        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
12018        let mut attn = e.uninit(t * nh * hd)?;
12019        let k_view = e.view_u8(&kvl.k, kvl.k.len());
12020        let v_view = e.view_u8(&kvl.v, kvl.v.len());
12021        // arm gating on the host HINT (upper bound; burst entry guards hint >= the vec floor
12022        // and a stable window regime — the same rung/regime keys as the draft graph).
12023        if swa && hint + 1 >= win {
12024            // fully-windowed rows: per-row window geometry from the counter (plus = 0: len_d
12025            // still holds the pre-append len; row r's T_kv = ctr + r + 1).
12026            e.fa_decode_rows_w(
12027                &q,
12028                &k_view,
12029                &v_view,
12030                &mut attn,
12031                hd,
12032                nh,
12033                nkv,
12034                &kvl.len_d,
12035                0,
12036                t,
12037                scale,
12038                win,
12039                kvl.k_tok_bytes,
12040                kvl.v_tok_bytes,
12041                None,
12042            )?;
12043        } else if hd == 512 && hint + t < crate::fa512_min_tkv() {
12044            // globals UNDER the fa512 crossover: eager runs the per-row fa_decode_kvmod
12045            // fallback there (parity with t=1 decode) — mirror it with per-row fa_decode_dc
12046            // (bit-correct for any t_kv <= bucket; the drafter dc arc proved the pairing).
12047            // Burst entry gates the horizon onto one side of the crossover, so hint decides
12048            // for every row.
12049            // NO .max(512): fa_decode_dc gates its hd512 dpl16-vs-scalar pick on bucket_max
12050            // (mirroring eager's fa512 floor) — forcing 512 here flipped the arm to dpl16
12051            // while eager ran scalar (il=5 KV drift, the burst's 4/128). SAME LAW capped
12052            // from above (2026-07-13): a regime-pinned hint near the floor (round-graph
12053            // captures use f512-1) pow2-rounds PAST it — clamp under the floor, or the arm
12054            // re-flips to dpl16 (the round-graph 4/64). The scalar unified self-splits, so
12055            // any bucket >= the live length is exact.
12056            let bucket = (hint + t + 2)
12057                .next_power_of_two()
12058                .min(crate::fa512_min_tkv().saturating_sub(1));
12059            let qv = e.view(&q, t * nh * hd);
12060            for i in 0..t {
12061                let q_row = qv.slice(i * nh * hd..(i + 1) * nh * hd);
12062                let mut q_one = e.uninit(nh * hd)?;
12063                e.copy_view_into(&mut q_one, 0, &q_row, nh * hd)?;
12064                let mut a_one = e.uninit(nh * hd)?;
12065                e.fa_decode_dc(
12066                    &q_one,
12067                    &k_view,
12068                    &v_view,
12069                    &mut a_one,
12070                    hd,
12071                    nh,
12072                    nkv,
12073                    &row_ctrs[i],
12074                    bucket,
12075                    scale,
12076                    kvl.k_tok_bytes,
12077                    kvl.v_tok_bytes,
12078                    false,
12079                )?;
12080                e.copy_into(&mut attn, i * nh * hd, &a_one, nh * hd)?;
12081            }
12082        } else if hd == 512 {
12083            // globals past the crossover: dpl16 dc twin via the shared rows wrapper (hint
12084            // sizes splits — upper bound; splits beyond the device len exit in-kernel).
12085            e.fa_decode_rows(
12086                &q,
12087                &k_view,
12088                &v_view,
12089                &mut attn,
12090                hd,
12091                nh,
12092                nkv,
12093                hint,
12094                t,
12095                scale,
12096                kvl.k_tok_bytes,
12097                kvl.v_tok_bytes,
12098                Some((&kvl.len_d, 0)),
12099                false,
12100                false,
12101                None,
12102            )?;
12103        } else {
12104            // hd256 under-window: v4 device-len rows twin.
12105            e.fa_decode_rows_dc(
12106                &q,
12107                &k_view,
12108                &v_view,
12109                &mut attn,
12110                hd,
12111                nh,
12112                nkv,
12113                &kvl.len_d,
12114                hint + t,
12115                t,
12116                scale,
12117                kvl.k_tok_bytes,
12118                kvl.v_tok_bytes,
12119                0,
12120                swa && crate::Engine::wkv_on(),
12121            )?;
12122        }
12123        Ok(e.matmul(&fa.wo, &attn, t)?)
12124    }
12125
12126    fn gemma4_verify_attn(
12127        &self,
12128        e: &Engine,
12129        fa: &crate::hybrid::FullAttnLayer,
12130        il: usize,
12131        hq: &CudaSlice<i8>,
12132        hdq: &CudaSlice<f32>,
12133        pos_d: &CudaSlice<i32>,
12134        t: usize,
12135        cache: &mut Cache,
12136    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
12137        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
12138        let eps = self.cfg.rms_eps;
12139        let aux = self.gemma4_aux.as_ref().unwrap();
12140        let ones = aux.ones(e);
12141        #[cfg(debug_assertions)]
12142        crate::debug_assert_tensor_stream_device(ones, &e.stream(), "gemma4_verify_attn.ones");
12143        let n_embd = self.cfg.n_embd as usize;
12144        let _ = n_embd;
12145
12146        let h0 = e.zeros(0)?;
12147        let h = &h0;
12148        // BATCHED FUSED qkv (MEMRA_F2B=1, megakernel microcosm): swa layers fuse all three,
12149        // globals fuse q,k (v := k clone). Bit-identical per row; segments tail-fill.
12150        static F2B_QKV: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
12151        let f2b = *F2B_QKV.get_or_init(|| std::env::var("MEMRA_F2B").as_deref() != Ok("0"));
12152        let fused_qkv = if f2b {
12153            if swa {
12154                e.matmul_q4_fused3_batched(&fa.wq, &fa.wk, &fa.wv, hq, hdq, t)?
12155                    .map(|(a, b, c)| (a, b, Some(c)))
12156            } else {
12157                e.matmul_q4_fused2_batched(&fa.wq, &fa.wk, hq, hdq, t)?
12158                    .map(|(a, b)| (a, b, None))
12159            }
12160        } else {
12161            None
12162        };
12163        let (q0, k0, v0) = match fused_qkv {
12164            Some((a, b, cv)) => {
12165                let v = match cv {
12166                    Some(c) => c,
12167                    None => e.clone_dtod(&b)?,
12168                };
12169                (a, b, v)
12170            }
12171            None => {
12172                let q0 = e.matmul_pre(&fa.wq, hq, hdq, h, t)?;
12173                let k0 = e.matmul_pre(&fa.wk, hq, hdq, h, t)?;
12174                let v0 = if swa {
12175                    e.matmul_pre(&fa.wv, hq, hdq, h, t)?
12176                } else {
12177                    e.clone_dtod(&k0)?
12178                };
12179                (q0, k0, v0)
12180            }
12181        };
12182        let mut q = e.uninit(t * nh * hd)?;
12183        let mut k = e.uninit(t * nkv * hd)?;
12184        let mut v = e.uninit(t * nkv * hd)?;
12185        // E4B wave-3 fold (2026-07-23 decode-dust port): q/k/v norms + q/k rope in ONE
12186        // launch — rope math verbatim on the normed rows; V ones-rms, never roped.
12187        let ff = if swa {
12188            None
12189        } else {
12190            Some(
12191                aux.rope_freqs(e)
12192                    .expect("gemma4 global rope needs rope_freqs.weight"),
12193            )
12194        };
12195        #[cfg(debug_assertions)]
12196        if let Some(ff) = ff {
12197            crate::debug_assert_tensor_stream_device(
12198                ff,
12199                &e.stream(),
12200                "gemma4_verify_attn.rope_freqs",
12201            );
12202        }
12203        e.rms_norm_qkv_rope(
12204            &q0,
12205            &k0,
12206            &v0,
12207            fa.q_norm.float_data(),
12208            fa.k_norm.float_data(),
12209            ones,
12210            &mut q,
12211            &mut k,
12212            &mut v,
12213            hd,
12214            nh * t,
12215            nkv * t,
12216            pos_d,
12217            nh,
12218            nkv,
12219            base,
12220            1.0,
12221            ff,
12222            eps,
12223        )?;
12224        let kvl = cache.kv[il].as_mut().unwrap();
12225        let base_len = kvl.len;
12226        e.append_kv_quantized_rows(
12227            &k,
12228            &v,
12229            &mut kvl.k,
12230            &mut kvl.v,
12231            base_len,
12232            t,
12233            kvl.kv_dim_k,
12234            kvl.kv_dim_v,
12235            kvl.k_tok_bytes,
12236            kvl.v_tok_bytes,
12237            (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on()),
12238        )?;
12239        kvl.len += t;
12240        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
12241        let mut attn = e.uninit(t * nh * hd)?;
12242        // ROWS twin when no window offset is in play (globals are hd512-ineligible; SWA rows
12243        // under the window need no offset): ONE launch, per-row causal == the per-token loop.
12244        let rows_ok = (hd == 256 && base_len + 1 >= crate::fa_vec_min_tkv())
12245            // gemma globals: hd512 rows twin in the dpl16 vec regime (row 0 gates the batch);
12246            // decode rides the SAME symbol at t=1 (parity law).
12247            || (hd == 512 && !swa && base_len + 1 >= crate::fa512_min_tkv());
12248        if rows_ok && (!swa || base_len + t <= win) {
12249            let k_view = e.view_u8(&kvl.k, (base_len + t) * kvl.k_tok_bytes);
12250            let v_view = e.view_u8(&kvl.v, (base_len + t) * kvl.v_tok_bytes);
12251            if hd == 512 {
12252                // device-len twin: sync the counter to the verify base (async arg-store).
12253                e.i32_set_k(&mut kvl.len_d, base_len as i32)?;
12254                e.fa_decode_rows(
12255                    &q,
12256                    &k_view,
12257                    &v_view,
12258                    &mut attn,
12259                    hd,
12260                    nh,
12261                    nkv,
12262                    base_len,
12263                    t,
12264                    scale,
12265                    kvl.k_tok_bytes,
12266                    kvl.v_tok_bytes,
12267                    Some((&kvl.len_d, 0)),
12268                    false,
12269                    swa && crate::Engine::wkv_on(),
12270                    None,
12271                )?;
12272            } else {
12273                // hd256: the SAME v4_dc symbol the burst verify launches (PARITY LAW — the
12274                // host-base rows_v4 twin compiles apart from v4_dc and the two-symbol split
12275                // drifted the burst's persisted KV at il=8; one symbol, counter arg-stored).
12276                e.i32_set_k(&mut kvl.len_d, base_len as i32)?;
12277                e.fa_decode_rows_dc(
12278                    &q,
12279                    &k_view,
12280                    &v_view,
12281                    &mut attn,
12282                    hd,
12283                    nh,
12284                    nkv,
12285                    &kvl.len_d,
12286                    base_len + t,
12287                    t,
12288                    scale,
12289                    kvl.k_tok_bytes,
12290                    kvl.v_tok_bytes,
12291                    0,
12292                    swa && crate::Engine::wkv_on(),
12293                )?;
12294            }
12295            return Ok(e.matmul(&fa.wo, &attn, t)?);
12296        }
12297        // WINDOWED rows twin (deep ctx, every row fully windowed): one launch, ABSOLUTE-index
12298        // per-row geometry over the prefix view. PARITY LAW (2026-07-10 root-cause): textually
12299        // identical kernels do NOT compile bit-identically (nvcc unrolls fa_decode_vec_q 2x vs
12300        // its rows_w clone — SASS-proven; the unpinned score `+=` chain then rounds apart), so
12301        // decode-vs-verify parity comes from BOTH sides launching THIS SAME rows_w kernel
12302        // (decode passes t=1) — bitwise equal per position by symbol identity, any lane.
12303        // MEMRA_GEMMA_ROWS_W=0 -> per-token loop (decode falls back to fa_decode views too).
12304        if hd == 256
12305            && swa
12306            && base_len + 1 >= win
12307            && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0")
12308        {
12309            let k_view = e.view_u8(&kvl.k, (base_len + t) * kvl.k_tok_bytes);
12310            let v_view = e.view_u8(&kvl.v, (base_len + t) * kvl.v_tok_bytes);
12311            e.i32_set_k(&mut kvl.len_d, base_len as i32)?;
12312            e.fa_decode_rows_w(
12313                &q,
12314                &k_view,
12315                &v_view,
12316                &mut attn,
12317                hd,
12318                nh,
12319                nkv,
12320                &kvl.len_d,
12321                0,
12322                t,
12323                scale,
12324                win,
12325                kvl.k_tok_bytes,
12326                kvl.v_tok_bytes,
12327                None,
12328            )?;
12329            return Ok(e.matmul(&fa.wo, &attn, t)?);
12330        }
12331        for i in 0..t {
12332            let avail = base_len + i + 1;
12333            let (off_tok, t_kv) = if swa && avail > win {
12334                (avail - win, win)
12335            } else {
12336                (0, avail)
12337            };
12338            let k_view = e.view_u8_range(
12339                &kvl.k,
12340                off_tok * kvl.k_tok_bytes,
12341                (off_tok + t_kv) * kvl.k_tok_bytes,
12342            );
12343            let v_view = e.view_u8_range(
12344                &kvl.v,
12345                off_tok * kvl.v_tok_bytes,
12346                (off_tok + t_kv) * kvl.v_tok_bytes,
12347            );
12348            let qi = e.view(&q, t * nh * hd);
12349            let q_row = qi.slice(i * nh * hd..(i + 1) * nh * hd);
12350            let mut q_one = e.uninit(nh * hd)?;
12351            e.copy_view_into(&mut q_one, 0, &q_row, nh * hd)?;
12352            let mut a_one = e.uninit(nh * hd)?;
12353            // straddle rounds: windowed rows must ride the SAME rows_w kernel decode uses
12354            // (parity law above); global hd512 rows past the fa512 floor likewise ride the
12355            // rows_dpl16 twin; remaining rows keep the gated fa_decode pair.
12356            if swa
12357                && avail > win
12358                && hd == 256
12359                && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0")
12360            {
12361                let kp = e.view_u8(&kvl.k, avail * kvl.k_tok_bytes);
12362                let vp = e.view_u8(&kvl.v, avail * kvl.v_tok_bytes);
12363                e.i32_set_k(&mut kvl.len_d, (avail - 1) as i32)?;
12364                e.fa_decode_rows_w(
12365                    &q_one,
12366                    &kp,
12367                    &vp,
12368                    &mut a_one,
12369                    hd,
12370                    nh,
12371                    nkv,
12372                    &kvl.len_d,
12373                    0,
12374                    1,
12375                    scale,
12376                    win,
12377                    kvl.k_tok_bytes,
12378                    kvl.v_tok_bytes,
12379                    None,
12380                )?;
12381            } else if !swa
12382                && hd == 512
12383                && avail >= crate::fa512_min_tkv()
12384                && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0")
12385            {
12386                let kp = e.view_u8(&kvl.k, avail * kvl.k_tok_bytes);
12387                let vp = e.view_u8(&kvl.v, avail * kvl.v_tok_bytes);
12388                e.i32_set_k(&mut kvl.len_d, (avail - 1) as i32)?;
12389                e.fa_decode_rows(
12390                    &q_one,
12391                    &kp,
12392                    &vp,
12393                    &mut a_one,
12394                    hd,
12395                    nh,
12396                    nkv,
12397                    avail - 1,
12398                    1,
12399                    scale,
12400                    kvl.k_tok_bytes,
12401                    kvl.v_tok_bytes,
12402                    Some((&kvl.len_d, 0)),
12403                    false,
12404                    false,
12405                    None,
12406                )?;
12407            } else {
12408                e.fa_decode_kvmod(
12409                    &q_one,
12410                    &k_view,
12411                    &v_view,
12412                    &mut a_one,
12413                    hd,
12414                    nh,
12415                    nkv,
12416                    t_kv,
12417                    scale,
12418                    kvl.k_tok_bytes,
12419                    kvl.v_tok_bytes,
12420                    swa && crate::Engine::wkv_on(),
12421                )?;
12422            }
12423            e.copy_into(&mut attn, i * nh * hd, &a_one, nh * hd)?;
12424        }
12425        Ok(e.matmul(&fa.wo, &attn, t)?)
12426    }
12427
12428    /// gemma4 T=1 decode step: R8 layer graph over the cache; returns (softcapped logits host,
12429    /// h_seed = pre-output_norm hidden). Advances cache.pos.
12430    pub(crate) fn gemma4_decode_step_h(
12431        &self,
12432        e: &Engine,
12433        token: u32,
12434        cache: &mut Cache,
12435    ) -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
12436        // M1-PP2 door (crate::pp): 2-stage split with an explicit activation handoff.
12437        // Default OFF — unset env means this branch is never taken. The gemma4 arm stays
12438        // 2-stage in M2 (the N-stage gate model is the generic-arm 9B); N>2 warns + runs
12439        // unsplit rather than guessing a fence.
12440        if let Some(split) = crate::pp::pp2_split(self.layers.len()) {
12441            return self.gemma4_decode_step_h_pp2(e, token, cache, split);
12442        }
12443        if crate::pp::pp_cuts(self.layers.len()).is_some() {
12444            crate::pp::warn_unwired_once("gemma4 eager decode (N>2)");
12445        }
12446        let n_embd = self.cfg.n_embd as usize;
12447        let eps = self.cfg.rms_eps;
12448        let pos_d = e.htod_i32(&[cache.pos as i32])?;
12449        let mut x = e.htod(&self.embd.gather(n_embd, &[token]))?;
12450        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
12451        // cross-layer fusion: each tail's closing add+scale also EMITS the next layer's
12452        // attn-normed input pre-quantized q8_1 (the mixer consumes only quantized matmuls).
12453        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
12454        let n_layers = self.layers.len();
12455        for (il, layer) in self.layers.iter().enumerate() {
12456            let (hq, hdq) = match h_carry.take() {
12457                Some(p) => p,
12458                None => {
12459                    e.rms_norm_q8_1(&x, self.layers[0].attn_norm.float_data(), n_embd, 1, eps)?
12460                }
12461            };
12462            let Mixer::Full(fa) = &layer.mixer else {
12463                panic!("gemma4 layer {il} not full-attn")
12464            };
12465            let o = self.gemma4_decode_attn(e, fa, il, &hq, &hdq, &pos_d, cache)?;
12466            let next_norm = if il + 1 < n_layers {
12467                Some(self.layers[il + 1].attn_norm.float_data())
12468            } else {
12469                None
12470            };
12471            let (xn, hn) = self.gemma4_layer_tail_add_nq_pn(e, layer, &o, &x, 1, next_norm)?;
12472            x = xn;
12473            h_carry = hn;
12474        }
12475        let mut hn = e.uninit(n_embd)?;
12476        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
12477        let h_seed = e.clone_dtod(&x)?;
12478        let mut ld = e.matmul(&self.output, &hn, 1)?;
12479        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
12480        e.softcap(&mut ld, cap, self.output.out_features())?; // R4 on device (262k host tanh ~ms/step)
12481        self.gemma4_suppress(e, &mut ld, 1)?;
12482        let logits = e.dtoh(&ld)?;
12483        cache.pos += 1;
12484        Ok((logits, h_seed))
12485    }
12486
12487    /// M1-PP2 stage subgraph (gemma4 arm): layers [lo, hi) of the gemma4 T=1 walk with the
12488    /// pre-quantized next-norm carry LOCAL to the range. Enters with a materialized residual
12489    /// `x` (the range head runs its own `rms_norm_q8_1` against ITS layer's attn_norm — for
12490    /// lo == 0 that is exactly the unsplit loop's il==0 arm); exits with the residual
12491    /// materialized (range tail passes next_norm = None, exactly the unsplit last-layer arm).
12492    /// Bit-identity of the cut relies on the kernel-check-pinned `add_scale_rms_norm_q8_1 ==
12493    /// add_scale then rms_norm_q8_1` identity (`pp2-gate` verifies end-to-end).
12494    fn gemma4_decode_layers(
12495        &self,
12496        e: &Engine,
12497        mut x: CudaSlice<f32>,
12498        lo: usize,
12499        hi: usize,
12500        pos_d: &CudaSlice<i32>,
12501        cache: &mut Cache,
12502    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
12503        let n_embd = self.cfg.n_embd as usize;
12504        let eps = self.cfg.rms_eps;
12505        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
12506        for il in lo..hi {
12507            let layer = &self.layers[il];
12508            let (hq, hdq) = match h_carry.take() {
12509                Some(p) => p,
12510                // range head: il == lo — norm against THIS layer's attn_norm.
12511                None => {
12512                    e.rms_norm_q8_1(&x, self.layers[il].attn_norm.float_data(), n_embd, 1, eps)?
12513                }
12514            };
12515            let Mixer::Full(fa) = &layer.mixer else {
12516                panic!("gemma4 layer {il} not full-attn")
12517            };
12518            let o = self.gemma4_decode_attn(e, fa, il, &hq, &hdq, pos_d, cache)?;
12519            let next_norm = if il + 1 < hi {
12520                Some(self.layers[il + 1].attn_norm.float_data())
12521            } else {
12522                None
12523            };
12524            let (xn, hn) = self.gemma4_layer_tail_add_nq_pn(e, layer, &o, &x, 1, next_norm)?;
12525            x = xn;
12526            h_carry = hn;
12527        }
12528        Ok(x)
12529    }
12530
12531    /// M1-PP2 (increment 2): `gemma4_decode_step_h` as TWO stage subgraphs, each on its
12532    /// own stream (and device, under MEMRA_PP_DEVICES) with the transport-selected
12533    /// boundary handoff — same choreography as the generic arm (decode.rs), same
12534    /// ownership contract (crate::pp). Stage 0 = embed+scale + layers [0, split);
12535    /// stage 1 = layers [split, n) + output_norm + softcapped head.
12536    /// Each stage uploads its own copy of the step's position scalar on its own stream.
12537    /// MEMRA_PP_STREAMS=0 = the increment-1 same-stream seam. Gate: `pp2-gate`.
12538    fn gemma4_decode_step_h_pp2(
12539        &self,
12540        e: &Engine,
12541        token: u32,
12542        cache: &mut Cache,
12543        split: usize,
12544    ) -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
12545        if crate::pp::pp2_streams_off() {
12546            return self.gemma4_decode_step_h_pp2_samestream(e, token, cache, split);
12547        }
12548        let rt = crate::pp::Pp2Rt::get(e)?;
12549        let e0 = rt.engine(0, e);
12550        let e1 = rt.engine(1, e);
12551        let n_embd = self.cfg.n_embd as usize;
12552        let eps = self.cfg.rms_eps;
12553        let pos = cache.pos as i32;
12554
12555        // ---- STAGE 0 (its own stream): embed + sqrt(n_embd) scale + layers [0, split) ----
12556        let slot = {
12557            let _st0 = rt.enter(0);
12558            let pos_d = e0.htod_i32(&[pos])?;
12559            #[cfg(debug_assertions)]
12560            crate::debug_assert_tensor_stream_device(
12561                &pos_d,
12562                &e0.stream(),
12563                "gemma4_decode_step_h_pp2.stage0.pos_d",
12564            );
12565            let mut x = e0.htod(&self.embd.gather(n_embd, &[token]))?;
12566            e0.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
12567            let x = self.gemma4_decode_layers(e0, x, 0, split, &pos_d, cache)?;
12568            rt.tx(0, &x, n_embd)?
12569        };
12570
12571        // ---- STAGE 1 (its own stream): RX + layers [split, n) + softcapped head ----
12572        let _st1 = rt.enter(1);
12573        let pos_d = e1.htod_i32(&[pos])?;
12574        #[cfg(debug_assertions)]
12575        crate::debug_assert_tensor_stream_device(
12576            &pos_d,
12577            &e1.stream(),
12578            "gemma4_decode_step_h_pp2.stage1.pos_d",
12579        );
12580        let x = rt.rx(0, slot, n_embd)?;
12581        let x = self.gemma4_decode_layers(e1, x, split, self.layers.len(), &pos_d, cache)?;
12582
12583        let mut hn = e1.uninit(n_embd)?;
12584        e1.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
12585        let h_seed = e1.clone_dtod(&x)?;
12586        let mut ld = e1.matmul(&self.output, &hn, 1)?;
12587        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
12588        e1.softcap(&mut ld, cap, self.output.out_features())?;
12589        self.gemma4_suppress(e1, &mut ld, 1)?;
12590        let logits = e1.dtoh(&ld)?;
12591        cache.pos += 1;
12592        Ok((logits, h_seed))
12593    }
12594
12595    /// MEMRA_PP_STREAMS=0 rollback seam: the increment-1 gemma4 pp2 body verbatim — both
12596    /// stage subgraphs on the ambient compute stream, boundary = two plain dtod copies.
12597    fn gemma4_decode_step_h_pp2_samestream(
12598        &self,
12599        e: &Engine,
12600        token: u32,
12601        cache: &mut Cache,
12602        split: usize,
12603    ) -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
12604        let n_embd = self.cfg.n_embd as usize;
12605        let eps = self.cfg.rms_eps;
12606        let pos_d = e.htod_i32(&[cache.pos as i32])?;
12607
12608        // ---- STAGE 0: embed + sqrt(n_embd) scale + layers [0, split) ----
12609        let mut x = e.htod(&self.embd.gather(n_embd, &[token]))?;
12610        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
12611        let x = self.gemma4_decode_layers(e, x, 0, split, &pos_d, cache)?;
12612
12613        // ---- STAGE BOUNDARY: explicit [n_embd] activation handoff (TX copy, RX copy) ----
12614        let boundary_tx = e.clone_dtod(&x)?;
12615        let boundary_rx = e.clone_dtod(&boundary_tx)?;
12616
12617        // ---- STAGE 1: layers [split, n) + output_norm + softcapped head ----
12618        let x =
12619            self.gemma4_decode_layers(e, boundary_rx, split, self.layers.len(), &pos_d, cache)?;
12620
12621        let mut hn = e.uninit(n_embd)?;
12622        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
12623        let h_seed = e.clone_dtod(&x)?;
12624        let mut ld = e.matmul(&self.output, &hn, 1)?;
12625        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
12626        e.softcap(&mut ld, cap, self.output.out_features())?;
12627        self.gemma4_suppress(e, &mut ld, 1)?;
12628        let logits = e.dtoh(&ld)?;
12629        cache.pos += 1;
12630        Ok((logits, h_seed))
12631    }
12632}
12633
12634// ============================ step35 (Step-3.7-Flash) ==================================
12635// Node-for-node vs llama.cpp src/models/step35.cpp:216-300. WHY THIS IS A DEDICATED MIXER
12636// FAMILY and not a few branches inside the generic `full_attn*` chain:
12637//
12638//   1. `n_head` IS PER LAYER (`step35.attention.head_count` is an ARRAY: 64 on full-attn
12639//      layers, 96 on SWA). Every generic site reads the `cfg.n_head` scalar, so wq/wo/attn_gate
12640//      shapes and the FA head counts would be wrong on 33 of 45 layers.
12641//   2. `cfg.rope_dim_count` is 128 for this arch, but FULL-attn layers rotate only 64 dims
12642//      (upstream halves `n_rot_full` after the generic loader defaults it). A generic mixer
12643//      would rotate 128 dims on the full layers — silently wrong, plausible-looking logits.
12644//   3. Dual rope base (5e6 full / 1e4 SWA) + `rope_freqs.weight` on FULL layers ONLY.
12645//   4. SWA 3:1 (`sliding_window` 512, pattern [false,true,true,true]).
12646//   5. The head-wise gate is a SEPARATE `attn_gate.weight [n_embd, n_head_l]` (one pre-sigmoid
12647//      scalar per head, broadcast over head_dim) applied to the attention output BEFORE wo —
12648//      not the qwen35 fused-in-wq per-DIM gate that `attn_out_gate()`/`q_gate_split` handle.
12649//      Its input is the POST-attn_norm hidden (`cur`), not the residual.
12650//
12651// Attention scale is the DEFAULT 1/sqrt(n_embd_head_k) (step35.cpp:255) — NOT gemma4's 1.0.
12652impl HybridModel {
12653    /// Step35's declarative geometry row. Missing rows are artifact/config errors; never
12654    /// synthesize a drafter or trunk layer from a neighboring class.
12655    pub(crate) fn step35_geom(&self, il: usize) -> memra_gguf::config::LayerGeometry {
12656        let geometry = self
12657            .cfg
12658            .layer_geometry(il as u32)
12659            .unwrap_or_else(|| panic!("step35 layer {il} has no geometry-table row"));
12660        debug_assert_eq!(
12661            geometry.attention_gate,
12662            memra_gguf::config::AttentionGateKind::SeparateHead
12663        );
12664        geometry
12665    }
12666
12667    /// step35 attention core: everything from the q/k/v projection outputs through the head-wise
12668    /// gate, returning the GATED attention output PRE-`wo` (the caller runs wo, so this composes
12669    /// with both the plain `matmul(&fa.wo, ..)` sites and the f16/into-slab GEMM sites).
12670    ///
12671    /// `hg` = the POST-attn_norm hidden state (upstream's `cur`) — the gate projection's input
12672    /// on single-sequence paths. `gt_pre` supplies the already-projected per-head gate for a
12673    /// cross-request batch; exactly one of `hg` or `gt_pre` must be present.
12674    /// `cache`:
12675    ///   * `Some` => PRIME mode: append this chunk's post-rope K / raw V rows into the resident
12676    ///     quantized cache and attend THROUGH the cache view, exactly like the generic
12677    ///     `full_attn_prime_fa_dispatch` (quantize-then-attend on chunk 0 too, so chunk size
12678    ///     cannot decide where a precision edge falls — the grain-free chunk-invariance
12679    ///     contract, lane/chunkinv-flip).
12680    ///   * `None` => pure prefill (forward/forward_last/t2probe): attend over this batch's f32
12681    ///     q/k/v, no cache side effect.
12682    ///
12683    /// SWA WINDOW, and why prefill needs `sdpa_naive_w_quantized_view`: the view is trimmed to
12684    /// the oldest key ANY query in this chunk can reach (`off = base_len - (win-1)`), which
12685    /// bounds the view at `win-1+t` rows, but the mask is still required — inside the chunk,
12686    /// query `qt` may only see view keys `[qt, qt+win-1]`, so the earlier keys the trimmed view
12687    /// still contains must be masked per query. memra's window convention
12688    /// (`sdpa_naive_w_f32`: mask `t < q_pos-(window-1)`, `q_pos = (T_kv-T)+qt`) is bit-for-bit
12689    /// upstream's `LLAMA_SWA_TYPE_STANDARD` (`llama-hparams.h:359`: mask `p1-p0 >= n_swa`), and
12690    /// step35.cpp:6 sets exactly that swa_type — verified verbatim on both sides.
12691    ///
12692    /// Note the f32 `sdpa_naive_w` floor's shared memory is `t_kv*4` bytes, so the SWA arm needs
12693    /// `t_kv = win-1+t <= 12287` — i.e. it relies on chunked prefill (MEMRA_PRIME_CHUNK, default
12694    /// 4096). A monolithic 32k prime would exceed the 48 KiB dynamic-smem default.
12695    ///
12696    /// SWA ARM SELECTION IS KEYED ON `seq_end`, NOT ON THE CHUNK'S OWN `t_kv` (lane/step35-chunkfix,
12697    /// 2026-08-07). `seq_end` = the ABSOLUTE end position of the whole prime call
12698    /// (`cache.pos + prompt_len` at entry; `t` on the cacheless prefill path) — a property of the
12699    /// REQUEST, identical at every MEMRA_PRIME_CHUNK value. The old predicate `swa && t_kv > win`
12700    /// read the chunk's own extent, which made kernel selection — and therefore the logits, the
12701    /// hidden rows, and the generated text — a function of the chunk size:
12702    ///   a chunk [b,e) has off = max(0, b-(win-1)), t_kv = e-off, so b <= win-1 => off=0 => FA iff
12703    ///   e <= win, while every later chunk has t_kv = t+(win-1) > win. The FA rows were therefore a
12704    ///   contiguous PREFIX [0,P) with P = c*floor(win/c) for c <= win (else 0), and the output
12705    ///   depended only on P. Measured (research/step37-p2-20260806, closed-form receipt
12706    ///   `raw/chunkinv-step35-GAP2-CONFIRMED-20260807.txt`): at T=4883, c=512 and c=64 (P=512) are
12707    ///   byte-identical to each other but DIFFER from the c=4096 default (P=0) by maxdiff 1.813e0
12708    ///   with greedy text diverging at step 6; c=513 (P=0) is bit-identical to the default. A
12709    ///   one-token change in a documented machine-config knob changed the answer.
12710    /// So the pre-fix comment "same cache bytes, same numeric class" was false as written: same
12711    /// bytes, DIFFERENT numeric class — swapping `fa_prefill_view_ws` for the f32 windowed floor on
12712    /// the same rows moves the logits by ~1.8.
12713    ///
12714    /// Keying on `seq_end` makes P identically 0 for every chunk size, so MEMRA_PRIME_CHUNK is a
12715    /// pure memory/transient knob again (research/step35-chunkfix-20260807). It is
12716    /// correct-by-construction rather than a tolerance argument — the windowed kernel computes the
12717    /// same masked attention the FA arm computed (for e <= win the window mask is a no-op under
12718    /// causal, which is exactly why FA was legal there), it is now simply the ONLY arm used once
12719    /// the request passes the window. It also cannot move the shipped default: at chunk=4096 every
12720    /// chunk of a `seq_end > win` prime ALREADY had t_kv > win (chunk 0 has t_kv = min(chunk,
12721    /// seq_end) > win; every later chunk has t_kv >= t+win-1 > win since `PRIME_MIN_T` keeps
12722    /// t >= 16), and a `seq_end <= win` prime keeps every chunk on FA exactly as before — so this
12723    /// is a no-op on both default regimes and only removes the FA prefix at small chunk values.
12724    /// The `t_kv <= 12287` smem ceiling is respected: the chunks whose arm changes are precisely
12725    /// those with t_kv <= win = 512.
12726    #[allow(clippy::too_many_arguments)]
12727    fn step35_attn_pre_wo(
12728        &self,
12729        e: &Engine,
12730        fa: &FullAttnLayer,
12731        mut g3: Vec<CudaSlice<f32>>,
12732        hg: Option<&CudaSlice<f32>>,
12733        gt_pre: Option<&CudaSlice<f32>>,
12734        pos_d: &CudaSlice<i32>,
12735        t: usize,
12736        cache: Option<&mut Cache>,
12737        il: usize,
12738        seq_end: usize,
12739    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
12740        let geometry = self.step35_geom(il);
12741        let hd = geometry.head_dim_k as usize;
12742        let nkv = geometry.n_head_kv as usize;
12743        let nh = geometry.n_head as usize;
12744        let rbase = geometry.rope_base;
12745        let scale = geometry.attention_scale();
12746        let swa = geometry.window.is_some();
12747        let eps = self.cfg.rms_eps;
12748        let win = geometry.window.unwrap_or(0) as usize;
12749        let n_rot = geometry.n_rot as usize;
12750
12751        let v = g3.pop().unwrap();
12752        let k0 = g3.pop().unwrap();
12753        let q0 = g3.pop().unwrap();
12754
12755        // q/k RMSNorm over head_dim rows (attn_q_norm/attn_k_norm [128] F32), then the
12756        // per-layer PARTIAL NEOX rope: n_rot = 64 on full layers (dims 64..127 pass through
12757        // unrotated), 128 on SWA. rope_freqs (llama3-style per-dim factors) on FULL only.
12758        let mut q = e.uninit(t * nh * hd)?;
12759        e.rms_norm(&q0, fa.q_norm.float_data(), &mut q, hd, nh * t, eps)?;
12760        let mut k = e.uninit(t * nkv * hd)?;
12761        e.rms_norm(&k0, fa.k_norm.float_data(), &mut k, hd, nkv * t, eps)?;
12762        let ff = if geometry.rope_factors {
12763            self.step35_aux.as_ref().and_then(|a| a.rope_freqs(e))
12764        } else {
12765            None
12766        };
12767        #[cfg(debug_assertions)]
12768        if let Some(ff) = ff {
12769            crate::debug_assert_tensor_stream_device(
12770                ff,
12771                &e.stream(),
12772                "step35_attn_pre_wo.rope_freqs",
12773            );
12774        }
12775        e.rope_neox2(&mut q, &mut k, pos_d, hd, n_rot, nh, nkv, t, rbase, 1.0, ff)?;
12776
12777        let mut attn = e.uninit(t * nh * hd)?;
12778        match cache {
12779            Some(cache) => {
12780                let base_len = cache.kv[il].as_ref().unwrap().len;
12781                // Read per layer call, never in a measured default.
12782                let legacy_tkv = std::env::var("MEMRA_STEP35_SWA_TKV").as_deref() == Ok("1");
12783                let legacy_calllocal = std::env::var("MEMRA_PRIME_CALLLOCAL").as_deref() == Ok("1");
12784                let off = if swa {
12785                    let raw = base_len.saturating_sub(win - 1);
12786                    if legacy_tkv || legacy_calllocal {
12787                        raw
12788                    } else {
12789                        raw & !31usize
12790                    }
12791                } else {
12792                    0
12793                };
12794                {
12795                    let kvl = cache.kv[il].as_mut().unwrap();
12796                    assert!(kvl.len + t <= cache.max_ctx, "step35 prime: KV overflow");
12797                    let write_row = e.prepare_kv_append(kvl, off, t)?;
12798                    e.append_kv_quantized_rows(
12799                        &k,
12800                        &v,
12801                        &mut kvl.k,
12802                        &mut kvl.v,
12803                        write_row,
12804                        t,
12805                        kvl.kv_dim_k,
12806                        kvl.kv_dim_v,
12807                        kvl.k_tok_bytes,
12808                        kvl.v_tok_bytes,
12809                        crate::Engine::kv_fp8_on(),
12810                    )?;
12811                    kvl.len += t;
12812                    let new_len = kvl.len as i32;
12813                    e.set_i32_one(&mut kvl.len_d, new_len)?;
12814                }
12815                let kvl = cache.kv[il].as_ref().unwrap();
12816                // Both segmentation seams restore the FULL pre-fix arithmetic: their legacy
12817                // arm predicate (`t_kv` below, per-call `seq_end` in prime_cache) AND the
12818                // unaligned view offset here. Both halves are load-bearing for the canaries:
12819                // on the FA default the predicate arms agree bitwise wherever they can differ
12820                // (a t_kv<=win view has no maskable key, so windowed==unwindowed FA bit-for-bit).
12821                // That made predicate-only seams INERT under Lever A — first chunkinv35c, then
12822                // tickinv35c. The raw offset restores the live segmentation-variant mechanism
12823                // on the current FA path: its tile grid starts at the chunk/call boundary.
12824                // SWA: trim the view to the oldest key any query in this chunk can reach —
12825                // ALIGNED DOWN to the FA tile size (BK=32). The raw trim is chunk-DEPENDENT
12826                // (off = base_len-(win-1), and base_len is a chunk boundary), and the FA
12827                // kernel's online-softmax recurrence groups keys into BK tiles relative to
12828                // the VIEW START — so an unaligned off regroups the same absolute keys into
12829                // different tiles at different chunk sizes = different (m,l) rounding =
12830                // chunk-dependent bits. chunkinv35 caught exactly this on the first FA-arm
12831                // battery (first_div == chunk size; raw/leverA-gates-20260807T135541Z.log).
12832                // Aligning off to 32 pins tiles to ABSOLUTE key positions for every chunk
12833                // size; the <=31 extra leading keys are older than EVERY query's window
12834                // (all queries sit at >= base_len, so keys < base_len-(win-1) are masked
12835                // for all of them) and a fully-masked key is an exact-0.0 no-op in both
12836                // kernels (NEG_INF -> p=0.0; l+=0.0 and O+=0.0 are bitwise identity), so
12837                // the floor arm's bits do not move either (gated: G2f, battery 2).
12838                let t_kv = base_len + t - off;
12839                let physical = kvl.physical_rows(off, off + t_kv)?;
12840                let k_view = e.view_u8_range(
12841                    &kvl.k,
12842                    physical.start * kvl.k_tok_bytes,
12843                    physical.end * kvl.k_tok_bytes,
12844                );
12845                let v_view = e.view_u8_range(
12846                    &kvl.v,
12847                    physical.start * kvl.v_tok_bytes,
12848                    physical.end * kvl.v_tok_bytes,
12849                );
12850                // CHUNK-INVARIANT PREDICATE: `seq_end` (the request's absolute end position), not
12851                // this chunk's `t_kv`. See the doc note — keying on t_kv made P = c*floor(win/c)
12852                // rows take FA and the output a function of MEMRA_PRIME_CHUNK.
12853                // MEMRA_STEP35_SWA_TKV=1 is the ROLLBACK SEAM to the pre-fix arithmetic — BOTH
12854                // halves: this predicate AND the unaligned view offset above (`legacy_tkv`).
12855                // It is what gives the step35 chunkinv gate its canary teeth: chunk-VARIANT by
12856                // construction, so the invariance assertion MUST break under it (the seam whose
12857                // absence made the original canary inert — GAP 1 in research/step37-p2-20260806;
12858                // and a predicate-only seam went inert AGAIN under the FA default, battery 2's
12859                // CANARY UNEXPECTEDLY MATCHED — see the offset comment). Read per call, not
12860                // cached (probes flip it in-process). Never on in a measured default run.
12861                let swa_naive = if legacy_tkv {
12862                    t_kv > win
12863                } else {
12864                    seq_end > win
12865                };
12866                if swa && swa_naive {
12867                    // Windowed mask needed (see the doc note). DEFAULT since lane/pp-prefill
12868                    // 2026-08-07: the windowed hd128 FA stamp (`fa_prefill_view_ws_w_hd128`) —
12869                    // the anatomy profile measured the f32 floor at 565 ms/layer on a pp4096
12870                    // (41% of the whole prime) while the unwindowed hd128 FA family did the
12871                    // strictly harder causal-4096 in 3.3 ms. NOTE t_kv can be <= win here
12872                    // (a chunk-0 view on a small chunk); the windowed kernel handles that
12873                    // identically to the unwindowed one modulo the mask, which is the point.
12874                    // NEW NUMERIC CLASS vs the floor (bf16-MMA online softmax vs f32 serial),
12875                    // selected on `seq_end` like every arm here, so the class is uniform for
12876                    // the whole request at every MEMRA_PRIME_CHUNK — chunkinv holds by the
12877                    // same construction as the chunkfix. MEMRA_STEP35_SWA_FA=0 = rollback to
12878                    // the f32 floor (the previous numeric config, kept as the A/B seam).
12879                    if std::env::var("MEMRA_STEP35_SWA_FA").as_deref() == Ok("0") {
12880                        e.sdpa_naive_w_quantized_view(
12881                            &q,
12882                            &k_view,
12883                            &v_view,
12884                            &mut attn,
12885                            hd,
12886                            nh,
12887                            nkv,
12888                            t,
12889                            t_kv,
12890                            scale,
12891                            true,
12892                            win,
12893                            kvl.k_tok_bytes,
12894                            kvl.v_tok_bytes,
12895                        )?;
12896                    } else {
12897                        e.fa_prefill_view_ws_w_hd128(
12898                            &q,
12899                            &k_view,
12900                            &v_view,
12901                            &mut attn,
12902                            hd,
12903                            nh,
12904                            nkv,
12905                            t,
12906                            t_kv,
12907                            scale,
12908                            true,
12909                            win,
12910                            kvl.k_tok_bytes,
12911                            kvl.v_tok_bytes,
12912                        )?;
12913                    }
12914                } else if std::env::var("MEMRA_NOFA").is_ok() {
12915                    e.sdpa_naive_quantized_view(
12916                        &q,
12917                        &k_view,
12918                        &v_view,
12919                        &mut attn,
12920                        hd,
12921                        nh,
12922                        nkv,
12923                        t,
12924                        t_kv,
12925                        scale,
12926                        true,
12927                        kvl.k_tok_bytes,
12928                        kvl.v_tok_bytes,
12929                    )?;
12930                } else {
12931                    // seq_end <= win (or a full-attn layer): no query in the WHOLE request can
12932                    // reach past the window, so the window mask is a no-op under causal and every
12933                    // chunk rides the hd128 dequant-once FA prefill — one arm for the whole
12934                    // request either way, which is what makes the chunk size arithmetic-free.
12935                    e.fa_prefill_view_ws(
12936                        &q,
12937                        &k_view,
12938                        &v_view,
12939                        &mut attn,
12940                        hd,
12941                        nh,
12942                        nkv,
12943                        t,
12944                        t_kv,
12945                        scale,
12946                        true,
12947                        kvl.k_tok_bytes,
12948                        kvl.v_tok_bytes,
12949                        crate::Engine::kv_fp8_on(),
12950                    )?;
12951                }
12952            }
12953            None => {
12954                // Cacheless prefill (forward/forward_last/t2probe) is MONOLITHIC — there is no
12955                // chunk loop on this path, so seq_end == t and the predicate is unchanged. The
12956                // assert pins that: if a chunked cacheless prefill is ever added, it must thread
12957                // seq_end here too or it re-opens the same door.
12958                debug_assert_eq!(
12959                    seq_end, t,
12960                    "step35 cacheless prefill is monolithic (seq_end == t)"
12961                );
12962                if swa && seq_end > win {
12963                    e.sdpa_naive_w(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true, win)?;
12964                } else if std::env::var("MEMRA_NOFA").is_ok() {
12965                    e.sdpa_naive(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?;
12966                } else {
12967                    e.fa_prefill(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?;
12968                }
12969            }
12970        }
12971
12972        // HEAD-WISE GATE (step35.cpp:267-285): gate = attn_gate(cur) -> [t, n_head_l];
12973        // attn *= sigmoid(gate) broadcast over head_dim. BEFORE wo.
12974        let gw = fa
12975            .attn_gate
12976            .as_ref()
12977            .ok_or("step35 layer is missing attn_gate.weight (head-wise attention gate)")?;
12978        let gt_owned = if gt_pre.is_none() {
12979            Some(e.matmul(
12980                gw,
12981                hg.ok_or("step35 attention needs hg when gt_pre is absent")?,
12982                t,
12983            )?)
12984        } else {
12985            None
12986        };
12987        let gt = gt_pre.or(gt_owned.as_ref()).unwrap();
12988        let mut ag = e.uninit(t * nh * hd)?;
12989        e.attn_head_gate(&attn, gt, &mut ag, None, hd, nh, t)?;
12990        Ok(ag)
12991    }
12992
12993    /// step35 PREFILL mixer, no cache side effect (the `full_attn` contract: `forward`,
12994    /// `forward_last`, t2probe). Post-`wo`.
12995    pub(crate) fn step35_attn(
12996        &self,
12997        e: &Engine,
12998        fa: &FullAttnLayer,
12999        h: &CudaSlice<f32>,
13000        pos_d: &CudaSlice<i32>,
13001        t: usize,
13002        il: usize,
13003    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
13004        let g3 = e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?;
13005        // Monolithic path: the request ends at t (no chunk loop). See step35_attn_pre_wo's note.
13006        let ag = self.step35_attn_pre_wo(e, fa, g3, Some(h), None, pos_d, t, None, il, t)?;
13007        Ok(e.matmul(&fa.wo, &ag, t)?)
13008    }
13009
13010    /// step35 PRIME mixer (the `full_attn_prime` contract: append this chunk's K/V into the
13011    /// resident quantized cache, attend through the cache view). Post-`wo`.
13012    ///
13013    /// `seq_end` = the ABSOLUTE end position of the whole prime request (chunk-invariant); it
13014    /// selects the SWA arm. See `step35_attn_pre_wo`'s doc note for why it cannot be this chunk's
13015    /// own extent.
13016    #[allow(clippy::too_many_arguments)]
13017    pub(crate) fn step35_attn_prime(
13018        &self,
13019        e: &Engine,
13020        fa: &FullAttnLayer,
13021        h: &CudaSlice<f32>,
13022        hx: Option<&CudaSlice<u8>>,
13023        pos_d: &CudaSlice<i32>,
13024        t: usize,
13025        cache: &mut Cache,
13026        il: usize,
13027        seq_end: usize,
13028    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
13029        let g3 = match hx {
13030            Some(xh) => e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv], h, xh, t)?,
13031            None => e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?,
13032        };
13033        let ag =
13034            self.step35_attn_pre_wo(e, fa, g3, Some(h), None, pos_d, t, Some(cache), il, seq_end)?;
13035        Ok(e.matmul(&fa.wo, &ag, t)?)
13036    }
13037
13038    /// step35 T=1 decode attention (post-`wo`, matching `full_attn_decode_pre`'s contract).
13039    /// `pre_q` = the attn-input norm's q8_1 pair when the caller took the norm-fusion lever
13040    /// (then `h` is a zero-length placeholder and EVERY projection here — including the gate —
13041    /// must be on the q8_1 fast path; `mixer_in_q8_1_fast` enforces that for step35 by also
13042    /// requiring `attn_gate`).
13043    ///
13044    /// SWA decode is a token-aligned VIEW OFFSET into the quantized cache (the gemma4 R6
13045    /// pattern): keys carry absolute rope and the mask is purely positional, so the single
13046    /// query at `len-1` attending the last `win` rows IS the windowed result — no mask kernel.
13047    #[allow(clippy::too_many_arguments)]
13048    pub(crate) fn step35_decode_attn(
13049        &self,
13050        e: &Engine,
13051        fa: &FullAttnLayer,
13052        il: usize,
13053        h: &CudaSlice<f32>,
13054        pre_q: Option<(&CudaSlice<i8>, &CudaSlice<f32>)>,
13055        pos_d: &CudaSlice<i32>,
13056        cache: &mut Cache,
13057    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
13058        let geometry = self.step35_geom(il);
13059        let hd = geometry.head_dim_k as usize;
13060        let nkv = geometry.n_head_kv as usize;
13061        let nh = geometry.n_head as usize;
13062        let rbase = geometry.rope_base;
13063        let scale = geometry.attention_scale();
13064        let swa = geometry.window.is_some();
13065        let eps = self.cfg.rms_eps;
13066        let win = geometry.window.unwrap_or(0) as usize;
13067        let n_rot = geometry.n_rot as usize;
13068        let n_embd = self.cfg.n_embd as usize;
13069        let gw = fa
13070            .attn_gate
13071            .as_ref()
13072            .ok_or("step35 layer is missing attn_gate.weight (head-wise attention gate)")?;
13073
13074        let (q0, k0, v0, gt) = match pre_q {
13075            Some((hq, hdq)) => {
13076                debug_assert!(
13077                    e.uses_q8_1_fast(gw),
13078                    "step35 pre-quantized decode requires attn_gate on the q8_1 fast path \
13079                     (h is a zero-length placeholder here) — see mixer_in_q8_1_fast"
13080                );
13081                let (a, b, c) = match e.matmul_q8_fused3(&fa.wq, &fa.wk, &fa.wv, hq, hdq)? {
13082                    Some(t3) => t3,
13083                    None => (
13084                        e.matmul_pre(&fa.wq, hq, hdq, h, 1)?,
13085                        e.matmul_pre(&fa.wk, hq, hdq, h, 1)?,
13086                        e.matmul_pre(&fa.wv, hq, hdq, h, 1)?,
13087                    ),
13088                };
13089                let gt = e.matmul_pre(gw, hq, hdq, h, 1)?;
13090                (a, b, c, gt)
13091            }
13092            None => {
13093                if e.uses_q8_1_fast(&fa.wq)
13094                    && e.uses_q8_1_fast(&fa.wk)
13095                    && e.uses_q8_1_fast(&fa.wv)
13096                    && e.uses_q8_1_fast(gw)
13097                {
13098                    let (hq, hdq) = e.quantize_q8_1(h, 1, n_embd)?;
13099                    let (a, b, c) = match e.matmul_q8_fused3(&fa.wq, &fa.wk, &fa.wv, &hq, &hdq)? {
13100                        Some(t3) => t3,
13101                        None => (
13102                            e.matmul_pre(&fa.wq, &hq, &hdq, h, 1)?,
13103                            e.matmul_pre(&fa.wk, &hq, &hdq, h, 1)?,
13104                            e.matmul_pre(&fa.wv, &hq, &hdq, h, 1)?,
13105                        ),
13106                    };
13107                    let gt = e.matmul_pre(gw, &hq, &hdq, h, 1)?;
13108                    (a, b, c, gt)
13109                } else {
13110                    (
13111                        e.matmul(&fa.wq, h, 1)?,
13112                        e.matmul(&fa.wk, h, 1)?,
13113                        e.matmul(&fa.wv, h, 1)?,
13114                        e.matmul(gw, h, 1)?,
13115                    )
13116                }
13117            }
13118        };
13119
13120        let mut q = e.uninit(nh * hd)?;
13121        e.rms_norm(&q0, fa.q_norm.float_data(), &mut q, hd, nh, eps)?;
13122        let mut k = e.uninit(nkv * hd)?;
13123        e.rms_norm(&k0, fa.k_norm.float_data(), &mut k, hd, nkv, eps)?;
13124        let ff = if swa {
13125            None
13126        } else {
13127            self.step35_aux.as_ref().and_then(|a| a.rope_freqs(e))
13128        };
13129        #[cfg(debug_assertions)]
13130        if let Some(ff) = ff {
13131            crate::debug_assert_tensor_stream_device(
13132                ff,
13133                &e.stream(),
13134                "step35_decode_attn.rope_freqs",
13135            );
13136        }
13137        e.rope_neox2(&mut q, &mut k, pos_d, hd, n_rot, nh, nkv, 1, rbase, 1.0, ff)?;
13138
13139        if std::env::var("MEMRA_NOFA").is_ok() {
13140            return Err(
13141                "MEMRA_NOFA (naive f32 SDPA) is incompatible with the quantized KV \
13142                        cache; unset MEMRA_NOFA to use fa_decode"
13143                    .into(),
13144            );
13145        }
13146        let kvl = cache.kv[il].as_mut().unwrap();
13147        let next_len = kvl.len + 1;
13148        let (off, t_kv) = if swa && next_len > win {
13149            (next_len - win, win)
13150        } else {
13151            (0, next_len)
13152        };
13153        let write_row = e.prepare_kv_append(kvl, off & !31usize, 1)?;
13154        e.append_kv_quantized(
13155            &k,
13156            &v0,
13157            &mut kvl.k,
13158            &mut kvl.v,
13159            write_row,
13160            kvl.kv_dim_k,
13161            kvl.kv_dim_v,
13162            kvl.k_tok_bytes,
13163            kvl.v_tok_bytes,
13164            crate::Engine::kv_fp8_on(),
13165        )?;
13166        kvl.len = next_len;
13167        let physical = kvl.physical_rows(off, off + t_kv)?;
13168        let k_view = e.view_u8_range(
13169            &kvl.k,
13170            physical.start * kvl.k_tok_bytes,
13171            physical.end * kvl.k_tok_bytes,
13172        );
13173        let v_view = e.view_u8_range(
13174            &kvl.v,
13175            physical.start * kvl.v_tok_bytes,
13176            physical.end * kvl.v_tok_bytes,
13177        );
13178        let mut attn = e.uninit(nh * hd)?;
13179        e.fa_decode_kvmod(
13180            &q,
13181            &k_view,
13182            &v_view,
13183            &mut attn,
13184            hd,
13185            nh,
13186            nkv,
13187            t_kv,
13188            scale,
13189            kvl.k_tok_bytes,
13190            kvl.v_tok_bytes,
13191            crate::Engine::kv_fp8_on(),
13192        )?;
13193
13194        let mut ag = e.uninit(nh * hd)?;
13195        e.attn_head_gate(&attn, &gt, &mut ag, None, hd, nh, 1)?;
13196        Ok(e.matmul(&fa.wo, &ag, 1)?)
13197    }
13198}
13199
13200// ===================================================================================== //
13201//  gemma-4 E4B (per-layer embeddings + KV-sharing) — FIRST-LIGHT forward.               //
13202//  Dedicated simple path (Stage-B matmuls, per-row causal attention over the quantized  //
13203//  cache) so the tuned 26B/31B paths stay untouched. Arch: research/gemma4-bringup/     //
13204//  e4b-arch-map.md; llama reference: src/models/gemma4.cpp (E4B arms). Wired: forward   //
13205//  (prefill logits), prime (tokenwise-equivalent batched trunk), eager decode_step_h.   //
13206//  NOT wired: dc/graph serving arms, verify/spec, chunked prime (HANDOVER-E4B.md).      //
13207// ===================================================================================== //
13208impl HybridModel {
13209    pub fn is_gemma4_e4b(&self) -> bool {
13210        self.gemma4_aux.as_ref().is_some_and(|a| a.e4b.is_some())
13211    }
13212
13213    /// E4B per-layer geometry: nh/nkv derive from the layer's OWN tensor shapes (the E4B GGUF
13214    /// ships a scalar head_count_kv; swa q 8x256 / kv 2x256, global q 4x512 / kv 1x512).
13215    /// KV-shared layers report the SHARE TARGET's kv count (their wk IS the target's tensor).
13216    fn gemma4_e4b_geom(&self, il: usize) -> (usize, usize, usize, f32, f32, bool) {
13217        let g = self.cfg.gemma4.as_ref().unwrap();
13218        let swa = g.swa_pattern[il];
13219        let hd = if swa {
13220            g.key_length_swa
13221        } else {
13222            g.key_length_global
13223        } as usize;
13224        let Mixer::Full(fa) = &self.layers[il].mixer else {
13225            panic!("e4b layer {il} not full-attn")
13226        };
13227        let nh = fa.wq.out_features() / hd;
13228        let nkv = fa.wk.out_features() / hd;
13229        (
13230            hd,
13231            nkv,
13232            nh,
13233            if swa {
13234                g.rope_base_swa
13235            } else {
13236                g.rope_base_global
13237            },
13238            1.0,
13239            swa,
13240        )
13241    }
13242
13243    /// E4B KV-share target: Some(target) for the trailing shared layers, None = own cache.
13244    fn gemma4_e4b_kv_target(&self, il: usize) -> Option<usize> {
13245        self.layers[il]
13246            .gemma4
13247            .as_ref()
13248            .and_then(|b| b.e4b.as_ref())
13249            .and_then(|e4| e4.kv_share.map(|t| t as usize))
13250    }
13251
13252    /// E4B prologue: inp_pl [t][n_layer][n_epl] =
13253    ///   ( gather(per_layer_tok_embd, tok)*sqrt(n_epl)
13254    ///   + rms_norm(model_proj . x_scaled * 1/sqrt(n_embd), proj_norm) ) * 1/sqrt(2)
13255    /// (llama gemma4.cpp build_inp_per_layer + project_per_layer_inputs, exact order).
13256    fn gemma4_e4b_inp_pl(
13257        &self,
13258        e: &Engine,
13259        tokens: &[u32],
13260        x_scaled: &CudaSlice<f32>,
13261        t: usize,
13262    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
13263        let tok_d = e.stream().clone_htod(&tokens.to_vec())?;
13264        self.gemma4_e4b_inp_pl_dev(e, &tok_d, x_scaled, t)
13265    }
13266
13267    /// Device-token prologue core (dc arm shares it: token ids never touch the host).
13268    fn gemma4_e4b_inp_pl_dev(
13269        &self,
13270        e: &Engine,
13271        tok_d: &CudaSlice<u32>,
13272        x_scaled: &CudaSlice<f32>,
13273        t: usize,
13274    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
13275        let aux = self.gemma4_aux.as_ref().unwrap();
13276        let m = aux.e4b.as_ref().unwrap();
13277        let n_embd = self.cfg.n_embd as usize;
13278        let n_layer = self.layers.len();
13279        let width = m.n_epl * n_layer;
13280        let tbl = m.tok_tbl_gpu.get_or_init(|| {
13281            e.upload_u8(&m.tok_embd_bytes)
13282                .expect("e4b per-layer token table upload")
13283        });
13284        let mut a =
13285            e.embed_gather_device_td(tbl, tok_d, t, width, m.tok_embd_qt, m.tok_embd_row_bytes)?;
13286        e.scale_inplace(&mut a, (m.n_epl as f32).sqrt(), t * width)?;
13287        let mut p = e.matmul(&m.model_proj, x_scaled, t)?;
13288        e.scale_inplace(&mut p, 1.0 / (n_embd as f32).sqrt(), t * width)?;
13289        let mut pn = e.uninit(t * width)?;
13290        e.rms_norm(
13291            &p,
13292            m.proj_norm.float_data(),
13293            &mut pn,
13294            m.n_epl,
13295            t * n_layer,
13296            self.cfg.rms_eps,
13297        )?;
13298        let mut out = e.uninit(t * width)?;
13299        e.add_scale(&a, &pn, 1.0 / 2f32.sqrt(), &mut out, t * width)?;
13300        Ok(out)
13301    }
13302
13303    /// E4B attention (t-wide, causal, per-row fa over the QUANTIZED cache — first-light
13304    /// correctness path; the fa rows arms come later). Own-KV layers project+norm+rope k/v
13305    /// and append t rows; KV-shared layers are Q-only over the target layer's cache (which
13306    /// already holds this forward's rows — the target runs earlier in the stack).
13307    #[allow(clippy::too_many_arguments)]
13308    fn gemma4_e4b_attn(
13309        &self,
13310        e: &Engine,
13311        il: usize,
13312        hq: &CudaSlice<i8>,
13313        hdq: &CudaSlice<f32>,
13314        pos_d: &CudaSlice<i32>,
13315        t: usize,
13316        cache: &mut Cache,
13317        dc_bucket: Option<usize>,
13318    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
13319        let (hd, nkv, nh, base, scale, swa) = self.gemma4_e4b_geom(il);
13320        let eps = self.cfg.rms_eps;
13321        let aux = self.gemma4_aux.as_ref().unwrap();
13322        let ones = aux.ones(e);
13323        #[cfg(debug_assertions)]
13324        crate::debug_assert_tensor_stream_device(ones, &e.stream(), "gemma4_e4b_attn.ones");
13325        let Mixer::Full(fa) = &self.layers[il].mixer else {
13326            unreachable!()
13327        };
13328        // pre-quantized layer input (E4B fusion port, 2026-07-12): ONE norm+quant feeds
13329        // wq/wk/wv via matmul_pre — the first-light path quantized the same h three times
13330        // (the profile's 342 quantize_q8_1/token; glue = 26% of the 6.1ms token).
13331        let h0 = e.zeros(0)?;
13332        let h = &h0;
13333
13334        let ff = if swa {
13335            None
13336        } else {
13337            Some(
13338                aux.rope_freqs(e)
13339                    .expect("e4b global rope needs rope_freqs.weight"),
13340            )
13341        };
13342        #[cfg(debug_assertions)]
13343        if let Some(ff) = ff {
13344            crate::debug_assert_tensor_stream_device(ff, &e.stream(), "gemma4_e4b_attn.rope_freqs");
13345        }
13346        let share = self.gemma4_e4b_kv_target(il);
13347        // Own-KV arms keep the f32 (k, v) alive for the prime fa arm below.
13348        let mut kv_f32: Option<(CudaSlice<f32>, CudaSlice<f32>)> = None;
13349        let mut q;
13350        if let Some(_tgt) = share {
13351            let q0 = e.matmul_pre(&fa.wq, hq, hdq, h, t)?;
13352            q = e.uninit(t * nh * hd)?;
13353            // Q-only through the same fused norm+rope kernel (rk = 0: the k/v segments are
13354            // empty; q0 stands in for the unused k/v pointers).
13355            let mut kdummy = e.uninit(1)?;
13356            let mut vdummy = e.uninit(1)?;
13357            e.rms_norm_qkv_rope(
13358                &q0,
13359                &q0,
13360                &q0,
13361                fa.q_norm.float_data(),
13362                fa.q_norm.float_data(),
13363                ones,
13364                &mut q,
13365                &mut kdummy,
13366                &mut vdummy,
13367                hd,
13368                nh * t,
13369                0,
13370                pos_d,
13371                nh,
13372                1,
13373                base,
13374                1.0,
13375                ff,
13376                eps,
13377            )?;
13378        } else {
13379            // wave-4b: ONE concat matvec (wq|wk|wv) at t == 1 when the cat tensor exists;
13380            // else the fused3 grid launch; else per-matvec. The cat output is contiguous
13381            // q|k|v rows — the cat norm+rope twin consumes it directly.
13382            let e4bits = self.layers[il].gemma4.as_ref().and_then(|g| g.e4b.as_ref());
13383            let cat = e4bits.and_then(|e4| e4.qkv_cat.as_ref());
13384            q = e.uninit(t * nh * hd)?;
13385            let mut k = e.uninit(t * nkv * hd)?;
13386            let mut v = e.uninit(t * nkv * hd)?;
13387            if t == 1 && cat.is_some() {
13388                let qkv0 = e.matmul_pre(cat.unwrap(), hq, hdq, h, 1)?;
13389                e.rms_norm_qkv_rope_cat(
13390                    &qkv0,
13391                    fa.q_norm.float_data(),
13392                    fa.k_norm.float_data(),
13393                    ones,
13394                    &mut q,
13395                    &mut k,
13396                    &mut v,
13397                    hd,
13398                    nh,
13399                    nkv,
13400                    pos_d,
13401                    nh,
13402                    nkv,
13403                    base,
13404                    1.0,
13405                    ff,
13406                    eps,
13407                )?;
13408            } else {
13409                let (q0, k0, v0) = match if t == 1 {
13410                    e.matmul_q4_fused3(&fa.wq, &fa.wk, &fa.wv, hq, hdq)?
13411                } else {
13412                    // E4B verify f3 port (2026-07-14): the 31B segmented-grid batched qkv
13413                    // on E4B's real-V triple — same MEMRA_F2B seam, bit-identical per row.
13414                    static F2B_QKV: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
13415                    if *F2B_QKV.get_or_init(|| std::env::var("MEMRA_F2B").as_deref() != Ok("0")) {
13416                        e.matmul_q4_fused3_batched(&fa.wq, &fa.wk, &fa.wv, hq, hdq, t)?
13417                    } else {
13418                        None
13419                    }
13420                } {
13421                    Some(triple) => triple,
13422                    None => (
13423                        e.matmul_pre(&fa.wq, hq, hdq, h, t)?,
13424                        e.matmul_pre(&fa.wk, hq, hdq, h, t)?,
13425                        e.matmul_pre(&fa.wv, hq, hdq, h, t)?,
13426                    ), // E4B: real v (K != V)
13427                };
13428                // wave-3 fold: q/k/v norms + q/k rope in ONE launch (rope math verbatim on
13429                // the normed rows; V ones-rms, never roped).
13430                e.rms_norm_qkv_rope(
13431                    &q0,
13432                    &k0,
13433                    &v0,
13434                    fa.q_norm.float_data(),
13435                    fa.k_norm.float_data(),
13436                    ones,
13437                    &mut q,
13438                    &mut k,
13439                    &mut v,
13440                    hd,
13441                    nh * t,
13442                    nkv * t,
13443                    pos_d,
13444                    nh,
13445                    nkv,
13446                    base,
13447                    1.0,
13448                    ff,
13449                    eps,
13450                )?;
13451            }
13452            let kvl = cache.kv[il].as_mut().unwrap();
13453            // class flag must match the cache dims (the g-threading sweep hardcoded `false`
13454            // here and the wkv default corrupted E4B: q8_0 bytes into an e4m3 cache — the
13455            // degenerate tok-0 stream, 2026-07-12).
13456            let cls = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
13457            if dc_bucket.is_some() {
13458                // DC arm (graph serving): append at the len_d slot, advance the counter
13459                // in-stream — replay-correct, no host len in the launch args. Host mirrors
13460                // are NOT touched here (the replay loop owns them; a bump at capture-record
13461                // time would double-count the capture iteration).
13462                debug_assert!(t == 1);
13463                // wave 5c: append + len_d inc fused (one launch; single-block ordering).
13464                e.append_kv_quantized_row_dc_inc(
13465                    &k,
13466                    &v,
13467                    &mut kvl.k,
13468                    &mut kvl.v,
13469                    &mut kvl.len_d,
13470                    kvl.kv_dim_k,
13471                    kvl.kv_dim_v,
13472                    kvl.k_tok_bytes,
13473                    kvl.v_tok_bytes,
13474                    cls,
13475                )?;
13476            } else {
13477                e.append_kv_quantized_rows(
13478                    &k,
13479                    &v,
13480                    &mut kvl.k,
13481                    &mut kvl.v,
13482                    kvl.len,
13483                    t,
13484                    kvl.kv_dim_k,
13485                    kvl.kv_dim_v,
13486                    kvl.k_tok_bytes,
13487                    kvl.v_tok_bytes,
13488                    cls,
13489                )?;
13490                kvl.len += t;
13491            }
13492            kv_f32 = Some((k, v));
13493        }
13494        // attention: per-row causal fa over the (own or target) quantized cache. The cache
13495        // already contains this forward's rows in both arms; row i attends [.., base+i].
13496        let kvl_idx = share.unwrap_or(il);
13497        let kvl = cache.kv[kvl_idx].as_ref().unwrap();
13498        let base_len = kvl.len - t; // pre-append length (target appended this forward too)
13499        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
13500        let mut attn = e.uninit(t * nh * hd)?;
13501        // PRIME FA ARMS (perf lane 3, completed 2026-07-31 — the H100 board pinned E4B
13502        // prefill at 482 tok/s: the per-row loop ran the DECODE kernel once per token per
13503        // layer). Fresh-prompt (base_len == 0) batched attention, one launch per layer:
13504        //   - own-KV hd256 under the window: f32 fa_prefill (full causal exact — 26B pattern)
13505        //   - own-KV hd256 above the window (swa): fa_prefill_w windowed twin (12B pattern)
13506        //   - own-KV hd512 globals: fa_prefill_hd512 (12B/31B globals pattern)
13507        //   - KV-shared layers (no f32 k/v): fa_prefill_view over the target's QUANTIZED
13508        //     rows (the T=K verify kernel; the target appended this forward's rows already).
13509        //     swa-shared above the window keeps the per-row loop (no windowed view twin).
13510        // Same numeric class split as the 26B prime (f32/batched prefill vs per-row
13511        // quantized decode); run-gen argmax + chat gates arbitrate. MEMRA_NOFA=1 reverts all.
13512        if t > 1 && base_len == 0 && std::env::var("MEMRA_NOFA").is_err() {
13513            if let Some((kf, vf)) = &kv_f32 {
13514                if hd == 256 && t <= win {
13515                    e.fa_prefill(&q, kf, vf, &mut attn, hd, nh, nkv, t, t, scale, true)?;
13516                    return Ok(e.matmul(&fa.wo, &attn, t)?);
13517                }
13518                if hd == 256 && swa && t > win {
13519                    e.fa_prefill_w(&q, kf, vf, &mut attn, hd, nh, nkv, t, t, scale, true, win)?;
13520                    return Ok(e.matmul(&fa.wo, &attn, t)?);
13521                }
13522                if hd == 512 && !swa {
13523                    e.fa_prefill_hd512(&q, kf, vf, &mut attn, hd, nh, nkv, t, t, scale, true)?;
13524                    return Ok(e.matmul(&fa.wo, &attn, t)?);
13525                }
13526            } else if share.is_some() {
13527                let g = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
13528                let k_view = e.view_u8(&kvl.k, kvl.k.len());
13529                let v_view = e.view_u8(&kvl.v, kvl.v.len());
13530                if hd == 256 && (!swa || t <= win) {
13531                    // inline-dequant quantized-view prefill (fa_prefill_q stamps 256/128)
13532                    e.fa_prefill_view(
13533                        &q,
13534                        &k_view,
13535                        &v_view,
13536                        &mut attn,
13537                        hd,
13538                        nh,
13539                        nkv,
13540                        t,
13541                        t,
13542                        scale,
13543                        true,
13544                        kvl.k_tok_bytes,
13545                        kvl.v_tok_bytes,
13546                        g,
13547                    )?;
13548                    return Ok(e.matmul(&fa.wo, &attn, t)?);
13549                }
13550                // remaining shared classes (swa above the window; hd512 globals): dequant
13551                // the target's t rows ONCE to f32, then the same f32 twins as own-KV.
13552                let kv_dim = nkv * hd;
13553                let mut kf = e.uninit(t * kv_dim)?;
13554                let mut vf = e.uninit(t * kv_dim)?;
13555                e.fa_dequant_kv_view_f32(
13556                    &k_view,
13557                    &v_view,
13558                    &mut kf,
13559                    &mut vf,
13560                    kv_dim,
13561                    kv_dim,
13562                    t,
13563                    kvl.k_tok_bytes,
13564                    kvl.v_tok_bytes,
13565                    g,
13566                )?;
13567                if hd == 512 {
13568                    e.fa_prefill_hd512(&q, &kf, &vf, &mut attn, hd, nh, nkv, t, t, scale, true)?;
13569                } else {
13570                    e.fa_prefill_w(&q, &kf, &vf, &mut attn, hd, nh, nkv, t, t, scale, true, win)?;
13571                }
13572                return Ok(e.matmul(&fa.wo, &attn, t)?);
13573            }
13574        }
13575        if let Some(bucket) = dc_bucket {
13576            // DC arm (t == 1, under-window regime — the generate gate enforces it): ONE
13577            // fa_decode_dc over the live counter. len_d already advanced past this token
13578            // (t_kv = len_d[0], the cache.rs contract). KV-shared layers read the target's
13579            // counter (advanced when the target ran earlier in the stack).
13580            assert!(t == 1);
13581            // hd512 globals: eager (kvmod) picks the SCALAR unified below the fa512 floor,
13582            // and under the window every live t_kv sits below it — cap the capture bucket
13583            // under the floor so fa_decode_dc bakes the same scalar symbol, or the graph
13584            // rides the dpl16 twin and its numeric class diverges from the dc-eager stream
13585            // (E4B-GRAPH-GATE 2/64, 2026-07-12).
13586            let bucket = if hd == 512 && win <= crate::fa512_min_tkv() {
13587                bucket.min(crate::fa512_min_tkv().saturating_sub(1))
13588            } else {
13589                bucket
13590            };
13591            let k_view = e.view_u8(&kvl.k, kvl.k.len());
13592            let v_view = e.view_u8(&kvl.v, kvl.v.len());
13593            let g = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
13594            // Weight prefetch (SOTA item 3, 2026-07-13): wo's decode plane prefetched into
13595            // L2 across the fa window — fa reads KV only, the weight DRAM lanes are idle
13596            // there (E4B valid window +0.65%: 196.8 vs 195.6). Value-free scheduling op,
13597            // captured into the dc graph like any other launch. Extending the cascade to
13598            // the ffn gate/up planes measured NEGATIVE (193.9 vs 195.8 — 29MB/layer floods
13599            // the fill path and evicts still-hot lines); wo-only is the shipped shape.
13600            // MEMRA_WPF=0 rollback seam.
13601            if crate::Engine::wpf_level() >= 1 {
13602                e.prefetch_weight_l2(&fa.wo)?;
13603            }
13604            // wave 5b: the combine emits the wo input q8 pair directly — the standalone
13605            // quantize launch + the f32 attn round-trip fold away (t=1 fast path only).
13606            if e.uses_q8_1_fast(&fa.wo) {
13607                let mut oq = e.alloc_i8_uninit(nh * hd)?;
13608                let mut od = e.zeros(nh * hd / 32)?;
13609                e.fa_decode_dc_q8(
13610                    &q,
13611                    &k_view,
13612                    &v_view,
13613                    &mut attn,
13614                    hd,
13615                    nh,
13616                    nkv,
13617                    &kvl.len_d,
13618                    bucket,
13619                    scale,
13620                    kvl.k_tok_bytes,
13621                    kvl.v_tok_bytes,
13622                    g,
13623                    Some((&mut oq, &mut od)),
13624                )?;
13625                return Ok(e.matmul_pre(&fa.wo, &oq, &od, &attn, t)?);
13626            }
13627            e.fa_decode_dc(
13628                &q,
13629                &k_view,
13630                &v_view,
13631                &mut attn,
13632                hd,
13633                nh,
13634                nkv,
13635                &kvl.len_d,
13636                bucket,
13637                scale,
13638                kvl.k_tok_bytes,
13639                kvl.v_tok_bytes,
13640                g,
13641            )?;
13642            return Ok(e.matmul(&fa.wo, &attn, t)?);
13643        }
13644        for i in 0..t {
13645            let avail = base_len + i + 1;
13646            let (off_tok, t_kv) = if swa && avail > win {
13647                (avail - win, win)
13648            } else {
13649                (0, avail)
13650            };
13651            let k_view = e.view_u8_range(
13652                &kvl.k,
13653                off_tok * kvl.k_tok_bytes,
13654                (off_tok + t_kv) * kvl.k_tok_bytes,
13655            );
13656            let v_view = e.view_u8_range(
13657                &kvl.v,
13658                off_tok * kvl.v_tok_bytes,
13659                (off_tok + t_kv) * kvl.v_tok_bytes,
13660            );
13661            let qv = e.view(&q, t * nh * hd);
13662            let q_row = qv.slice(i * nh * hd..(i + 1) * nh * hd);
13663            let mut q_one = e.uninit(nh * hd)?;
13664            e.copy_view_into(&mut q_one, 0, &q_row, nh * hd)?;
13665            let mut a_one = e.uninit(nh * hd)?;
13666            // read class MUST match the append class (globals are e4m3 under gkv): the
13667            // swa-only flag decoded global-layer e4m3 bytes as q8_0/q5_1 — attention over
13668            // garbage on EVERY E4B global layer, the cross-mode maxdiff-30 root (2026-07-12).
13669            e.fa_decode_kvmod(
13670                &q_one,
13671                &k_view,
13672                &v_view,
13673                &mut a_one,
13674                hd,
13675                nh,
13676                nkv,
13677                t_kv,
13678                scale,
13679                kvl.k_tok_bytes,
13680                kvl.v_tok_bytes,
13681                (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on()),
13682            )?;
13683            e.copy_into(&mut attn, i * nh * hd, &a_one, nh * hd)?;
13684        }
13685        Ok(e.matmul(&fa.wo, &attn, t)?)
13686    }
13687
13688    /// E4B trunk: embed -> prologue -> layers (attn + dense ffn via the 31B tail_core + the
13689    /// per-layer-embedding tail + layer scale) -> output_norm. Returns (softcapped logits
13690    /// device [t, n_vocab], pre-output_norm hidden [t, n_embd]). Appends t rows per own-KV
13691    /// layer; does NOT advance cache.pos (caller owns pos).
13692    fn gemma4_e4b_trunk(
13693        &self,
13694        e: &Engine,
13695        tokens: &[u32],
13696        pos0: usize,
13697        cache: &mut Cache,
13698        head_last: bool,
13699    ) -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
13700        let n_embd = self.cfg.n_embd as usize;
13701        let t = tokens.len();
13702        let pos: Vec<i32> = (0..t).map(|i| (pos0 + i) as i32).collect();
13703        let pos_d = e.htod_i32(&pos)?;
13704        let mut x = e.htod(&self.embd.gather(n_embd, tokens))?;
13705        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
13706        let inp_pl = self.gemma4_e4b_inp_pl(e, tokens, &x, t)?;
13707        self.gemma4_e4b_trunk_core(e, x, inp_pl, &pos_d, t, cache, None, true, head_last)
13708    }
13709
13710    /// Layer stack + head over prebuilt (x_scaled, inp_pl, device pos) — everything below
13711    /// here is device-driven, so the dc arm shares it verbatim (stream identity with the
13712    /// eager chain by construction: SAME functions, not twins).
13713    fn gemma4_e4b_trunk_core(
13714        &self,
13715        e: &Engine,
13716        x_in: CudaSlice<f32>,
13717        inp_pl: CudaSlice<f32>,
13718        pos_d: &CudaSlice<i32>,
13719        t: usize,
13720        cache: &mut Cache,
13721        dc_bucket: Option<usize>,
13722        cap_logits: bool,
13723        head_last: bool,
13724    ) -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
13725        let n_embd = self.cfg.n_embd as usize;
13726        let eps = self.cfg.rms_eps;
13727        let n_layer = self.layers.len();
13728        let mut x = x_in;
13729        let aux_e4b = self.gemma4_aux.as_ref().unwrap().e4b.as_ref().unwrap();
13730        let n_epl = aux_e4b.n_epl;
13731
13732        // cross-layer fusion (2026-07-12 port of the 26B/31B trunk structure): each layer's
13733        // closing add_scale also EMITS the next layer's attn-normed input pre-quantized
13734        // q8_1 (add_scale_rms_norm_q8_1); the LAST layer emits through output_norm so the
13735        // head rides matmul_pre too. First layer's pair comes from a standalone fused
13736        // norm+quant.
13737        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
13738        for il in 0..n_layer {
13739            let layer = &self.layers[il];
13740            let (hq, hdq) = match h_carry.take() {
13741                Some(p) => p,
13742                None => e.rms_norm_q8_1(&x, layer.attn_norm.float_data(), n_embd, t, eps)?,
13743            };
13744            let o = self.gemma4_e4b_attn(e, il, &hq, &hdq, pos_d, t, cache, dc_bucket)?;
13745            // dense ffn tail with the post-attn norm FOLDED into its entry (one launch for
13746            // rms(o, post_attn_norm) + residual add + ffn_norm — glue-fusion lane).
13747            let bits = layer.gemma4.as_ref().unwrap();
13748            let e4b = bits.e4b.as_ref().expect("e4b layer bits");
13749            // glue wave 5: the tail DEFERS its post_ffw norm — the FFN exit fuses
13750            // rms(f0, post_ffw) + residual add + q8 emit into ONE launch (rms_pre_add_q8_1),
13751            // killing the rms_norm + add_q8_1 pair per layer. T-GENERIC since 2026-07-13:
13752            // the fused single-phase reduction is NOT FP-order-identical to the unfused
13753            // rms_norm+add pair (the original "bit-identical chain" claim was FALSE — the
13754            // t==1 gate left the batched VERIFY on the unfused chain and split E4B verify
13755            // from decode by logit maxdiff ~0.45, the greedy tie-flip at depth: gate 135/256.
13756            // NOFUSE bisect: disabling ONLY this fusion -> verify maxdiff 0.000e0). With the
13757            // gate dropped, decode AND verify ride the same fused chain — parity by
13758            // construction, VERIFY-GATE 0.000e0.
13759            let fuse_exit = e.uses_q8_1_fast(&e4b.inp_gate);
13760            let (sn, attn_out) = self.gemma4_layer_tail_core_pn(
13761                e,
13762                layer,
13763                &o,
13764                &x,
13765                t,
13766                Some(layer.post_attn_norm.float_data()),
13767                fuse_exit,
13768            )?;
13769            let mut resid = e.uninit(t * n_embd)?;
13770            // per-layer-embedding tail: resid += rms(proj . (gelu(inp_gate . resid) * inp_pl[il]))
13771            // wave-2: the residual add emits q8_1 alongside — inp_gate rides matmul_pre.
13772            // (PLE one-block mega-fusion PROBED NEGATIVE 2026-07-13: argmax-correct but
13773            // 126 vs 189 tok/s — one SM pulling 0.74MB of weights loses to the multi-block
13774            // launch chain it replaced; jsonl row. Kernel deleted per doctrine.)
13775            let g = if fuse_exit {
13776                // sn here = RAW f0 (post_ffw deferred).
13777                let (rq, rd) = e.rms_pre_add_q8_1(
13778                    &sn,
13779                    bits.post_ffw_norm.float_data(),
13780                    &attn_out,
13781                    &mut resid,
13782                    n_embd,
13783                    t,
13784                    self.cfg.rms_eps,
13785                )?;
13786                e.matmul_pre(&e4b.inp_gate, &rq, &rd, &resid, t)?
13787            } else {
13788                e.add(&sn, &attn_out, &mut resid, t * n_embd)?;
13789                e.matmul(&e4b.inp_gate, &resid, t)?
13790            };
13791            let mut act = e.uninit(t * n_epl)?;
13792            let y = if t == 1 && e.uses_q8_1_fast(&e4b.proj) {
13793                let ipv = e.view(&inp_pl, n_epl * n_layer);
13794                let row = ipv.slice(il * n_epl..(il + 1) * n_epl);
13795                let (aq, ad) = e.gelu_tanh_mul_q8_1(&g, &row, &mut act, n_epl, 1)?;
13796                e.matmul_pre(&e4b.proj, &aq, &ad, &act, t)?
13797            } else {
13798                let mut inp_this = e.uninit(t * n_epl)?;
13799                e.copy_rows_strided(
13800                    &inp_pl,
13801                    &mut inp_this,
13802                    n_epl,
13803                    t,
13804                    n_epl * n_layer,
13805                    il * n_epl,
13806                )?;
13807                e.gelu_tanh_mul(&g, &inp_this, &mut act, t * n_epl)?;
13808                e.matmul(&e4b.proj, &act, t)?
13809            };
13810            // rms(y, post_norm) + (yn + resid)*layer_scale + next-layer norm+quant emit,
13811            // ONE launch (glue-fusion lane; last layer emits through output_norm).
13812            let next_norm = if il + 1 < n_layer {
13813                self.layers[il + 1].attn_norm.float_data()
13814            } else {
13815                self.output_norm.float_data()
13816            };
13817            let mut xn = e.uninit(t * n_embd)?;
13818            let pair = e.rms_pre_add_scale_rms_norm_q8_1(
13819                &y,
13820                e4b.post_norm.float_data(),
13821                &resid,
13822                bits.layer_scale,
13823                next_norm,
13824                &mut xn,
13825                n_embd,
13826                t,
13827                eps,
13828            )?;
13829            h_carry = Some(pair);
13830            x = xn;
13831        }
13832        // the head consumes the last layer's fused (output_norm) emit. head_last callers
13833        // (prime, last_only forward) need only the final row's logits — the all-T head is
13834        // t*n_vocab of discarded work (E4B prime: ~134ms GEMM + a 2.26GB dtoh kept 1 row).
13835        let (oq, odq) = h_carry.take().unwrap();
13836        let h0 = e.zeros(0)?;
13837        let hm = if head_last { 1 } else { t };
13838        let (hq, hd) = if head_last && t > 1 {
13839            let mut q1 = e.uninit_i8(n_embd)?;
13840            e.dtod_copy_view_i8(&oq.slice((t - 1) * n_embd..t * n_embd), &mut q1)?;
13841            let nb = n_embd / 32;
13842            let mut d1 = e.uninit(nb)?;
13843            e.dtod_copy_view(&odq.slice((t - 1) * nb..t * nb), &mut d1)?;
13844            (q1, d1)
13845        } else {
13846            (oq, odq)
13847        };
13848        let mut ld = e.matmul_pre(&self.output, &hq, &hd, &h0, hm)?;
13849        // softcap is strictly monotonic — greedy (argmax-only) consumers skip it, matching
13850        // the 26B/31B dc precedent (their dc head goes matmul -> argmax with no cap).
13851        // Logit-returning callers (host logits / spec prime) keep the capped emit.
13852        if cap_logits {
13853            let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
13854            e.softcap(&mut ld, cap, hm * self.output.out_features())?;
13855        }
13856        self.gemma4_suppress(e, &mut ld, hm)?; // mask both capped and argmax-only consumers
13857        Ok((ld, x))
13858    }
13859
13860    /// E4B batched VERIFY (device tokens, the spec round's t=K+1 step): t rows through the
13861    /// e4b trunk (per-row causal attention; own-KV layers append t rows host-len, KV-shared
13862    /// layers ride their targets), per-row device argmax + the POST-output_norm hidden
13863    /// stack (the drafter's h convention). Advances cache.pos/kvl.len by t — the spec
13864    /// round rolls back rejected rows (shared layers have no KvLayer, so the plain rewind
13865    /// covers exactly the layers that appended).
13866    pub fn gemma4_e4b_decode_step_t_am_dev(
13867        &self,
13868        e: &Engine,
13869        tok_d: &CudaSlice<u32>,
13870        t: usize,
13871        pos0: usize,
13872        cache: &mut Cache,
13873    ) -> Result<(CudaSlice<u32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
13874        let n_embd = self.cfg.n_embd as usize;
13875        let eps = self.cfg.rms_eps;
13876        let pos: Vec<i32> = (0..t).map(|i| (pos0 + i) as i32).collect();
13877        let pos_d = e.htod_i32(&pos)?;
13878        let embd_gpu = self
13879            .embd_gpu
13880            .get_or_init(|| e.upload_u8(&self.embd.raw).expect("embed table upload"));
13881        let (qt, rb) = self.embd.qt_and_row_bytes(n_embd);
13882        let mut x = e.embed_gather_device_td(embd_gpu, tok_d, t, n_embd, qt, rb)?;
13883        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
13884        let inp_pl = self.gemma4_e4b_inp_pl_dev(e, tok_d, &x, t)?;
13885        let (ld, xp) =
13886            self.gemma4_e4b_trunk_core(e, x, inp_pl, &pos_d, t, cache, None, true, false)?;
13887        // softcap is monotonic — the per-row argmax is invariant to it (the trunk's head
13888        // emit is already capped, matching the eager chain bit-for-bit).
13889        let n_vocab = self.output.out_features();
13890        let mut vam = e.stream().alloc_zeros::<u32>(t)?;
13891        for i in 0..t {
13892            e.argmax_token_device_col(&ld, i, n_vocab, &mut vam, i)?;
13893        }
13894        let mut hn = e.uninit(t * n_embd)?;
13895        e.rms_norm(&xp, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
13896        cache.pos += t;
13897        Ok((vam, hn))
13898    }
13899
13900    /// E4B verify + host logits + POST-output_norm hidden stack (the short-prompt spec
13901    /// prime path — mirror of `gemma4_decode_step_t_h`).
13902    pub(crate) fn gemma4_e4b_decode_step_t_h(
13903        &self,
13904        e: &Engine,
13905        tokens: &[u32],
13906        pos0: usize,
13907        cache: &mut Cache,
13908    ) -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
13909        let n_embd = self.cfg.n_embd as usize;
13910        let eps = self.cfg.rms_eps;
13911        let t = tokens.len();
13912        let (ld, xp) = self.gemma4_e4b_trunk(e, tokens, pos0, cache, false)?;
13913        let mut hn = e.uninit(t * n_embd)?;
13914        e.rms_norm(&xp, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
13915        cache.pos += t;
13916        Ok((e.dtoh(&ld)?, hn))
13917    }
13918
13919    /// E4B GRAPH-CAPTURABLE dc step: same trunk as the dc step but token_d is updated IN
13920    /// PLACE (self-feeding replay) and every launch arg is a device counter — pos from
13921    /// pos_d (inc'd in-stream), KV slots from len_d (advanced in-stream), attention from
13922    /// fa_decode_dc at `bucket`. Host mirrors (cache.pos / kvl.len) advance in the caller's
13923    /// replay loop. UNDER-WINDOW regime only (the caller gates pos + budget < window).
13924    pub fn gemma4_e4b_decode_step_dcg(
13925        &self,
13926        e: &Engine,
13927        token_d: &mut CudaSlice<u32>,
13928        pos_d: &mut CudaSlice<i32>,
13929        embd_gpu: &CudaSlice<u8>,
13930        embd_qt: i32,
13931        embd_rb: usize,
13932        cache: &mut Cache,
13933        n_vocab: usize,
13934        bucket: usize,
13935    ) -> Result<(), Box<dyn std::error::Error>> {
13936        let n_embd = self.cfg.n_embd as usize;
13937        let mut x = e.embed_gather_device(embd_gpu, token_d, n_embd, embd_qt, embd_rb)?;
13938        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
13939        let inp_pl = self.gemma4_e4b_inp_pl_dev(e, token_d, &x, 1)?;
13940        let (ld, _x) =
13941            self.gemma4_e4b_trunk_core(e, x, inp_pl, pos_d, 1, cache, Some(bucket), false, false)?;
13942        e.argmax_token_device_into(&ld, token_d, n_vocab)?;
13943        e.inc_seqlen(pos_d)?;
13944        Ok(())
13945    }
13946
13947    /// E4B DEVICE-COUNTER decode step (the dc serving arm): token id rides `token_d`, the
13948    /// greedy argmax lands in the returned device buffer — 4B/token host traffic. The layer
13949    /// stack is gemma4_e4b_trunk_core, i.e. the SAME functions the eager chain runs (stream
13950    /// identity by construction, not by twin-kernel parity). Host KV mirrors advance like the
13951    /// 26B dc-eager arm (window views are host math); len_d stays synced by the caller's
13952    /// entry sync + the appends here don't read it. Graph capture is NOT wired (no
13953    /// cap_bucket_max) — the E4B graph arc comes after the perf gates.
13954    #[allow(clippy::too_many_arguments)]
13955    pub fn gemma4_e4b_decode_step_dc(
13956        &self,
13957        e: &Engine,
13958        token_d: &CudaSlice<u32>,
13959        pos_d: &mut CudaSlice<i32>,
13960        embd_gpu: &CudaSlice<u8>,
13961        embd_qt: i32,
13962        embd_rb: usize,
13963        cache: &mut Cache,
13964        n_vocab: usize,
13965    ) -> Result<CudaSlice<u32>, Box<dyn std::error::Error>> {
13966        let n_embd = self.cfg.n_embd as usize;
13967        let eps = self.cfg.rms_eps;
13968        let mut x = e.embed_gather_device(embd_gpu, token_d, n_embd, embd_qt, embd_rb)?;
13969        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
13970        let inp_pl = self.gemma4_e4b_inp_pl_dev(e, token_d, &x, 1)?;
13971        let (ld, _x) =
13972            self.gemma4_e4b_trunk_core(e, x, inp_pl, pos_d, 1, cache, None, false, false)?;
13973        let mut tok_out = e.stream().alloc_zeros::<u32>(1)?;
13974        e.argmax_token_device_into(&ld, &mut tok_out, n_vocab)?;
13975        e.inc_seqlen(pos_d)?;
13976        cache.pos += 1;
13977        let _ = eps;
13978        Ok(tok_out)
13979    }
13980
13981    /// E4B eager T=1 decode step (decode_step_h contract): returns (softcapped logits host,
13982    /// pre-output_norm hidden). Advances cache.pos.
13983    pub(crate) fn gemma4_e4b_decode_step_h(
13984        &self,
13985        e: &Engine,
13986        token: u32,
13987        cache: &mut Cache,
13988    ) -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
13989        let (ld, x) = self.gemma4_e4b_trunk(e, &[token], cache.pos, cache, false)?;
13990        let logits = e.dtoh(&ld)?;
13991        cache.pos += 1;
13992        Ok((logits, x))
13993    }
13994
13995    /// E4B batched prime (prime_cache contract: (last-row logits host, h_seed pre-norm last
13996    /// row, hidden stack)). First-light: the t-wide trunk (per-row attention) — correct, not
13997    /// fast; the prefill fa arms come later.
13998    pub(crate) fn gemma4_e4b_prime(
13999        &self,
14000        e: &Engine,
14001        tokens: &[u32],
14002        cache: &mut Cache,
14003    ) -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
14004        // Err, not assert (2026-08-07, lane/gemma4-serve-gaps): same served-chunked-prompt
14005        // process-kill as gemma4_prime — refuse per-request.
14006        if cache.pos != 0 {
14007            return Err(
14008                "e4b prime is fresh-prompt only (v0) — prime the full prompt in one \
14009                        call or decode tokenwise"
14010                    .into(),
14011            );
14012        }
14013        let n_embd = self.cfg.n_embd as usize;
14014        let t = tokens.len();
14015        let (ld, x) = self.gemma4_e4b_trunk(e, tokens, 0, cache, true)?;
14016        cache.pos += t;
14017        let last = e.dtoh(&ld)?; // head_last: ld is already the final row only
14018        let xv = e.view(&x, t * n_embd);
14019        let row = xv.slice((t - 1) * n_embd..t * n_embd);
14020        let mut h_seed = e.uninit(n_embd)?;
14021        e.copy_view_into(&mut h_seed, 0, &row, n_embd)?;
14022        Ok((last, h_seed, x))
14023    }
14024
14025    /// E4B prefill logits (forward contract — no persistent cache; scratch cache internally).
14026    pub(crate) fn gemma4_e4b_forward(
14027        &self,
14028        e: &Engine,
14029        tokens: &[u32],
14030        last_only: bool,
14031    ) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
14032        let mut cache = Cache::new(e, &self.cfg, tokens.len() + 8)?;
14033        let (ld, _x) = self.gemma4_e4b_trunk(e, tokens, 0, &mut cache, last_only)?;
14034        Ok(e.dtoh(&ld)?) // head_last already reduced to the final row when last_only
14035    }
14036}
14037
14038#[cfg(test)]
14039mod prime_chunk_schedule_tests {
14040    use super::{
14041        PRIME_MIN_T, PRIME_PIPE_MIN_CHUNK, dynamic_prime_chunk_ranges, fixed_prime_chunk_ranges,
14042        fixed_prime_chunk_ranges_for_ring,
14043    };
14044
14045    fn sizes(ranges: &[(usize, usize)]) -> Vec<usize> {
14046        ranges.iter().map(|(start, end)| end - start).collect()
14047    }
14048
14049    fn auto_chunk(t: usize) -> usize {
14050        t.div_ceil(8).max(PRIME_PIPE_MIN_CHUNK).min(4096)
14051    }
14052
14053    #[test]
14054    fn fixed_schedule_retains_measured_geometry() {
14055        assert_eq!(
14056            sizes(&fixed_prime_chunk_ranges(461, 128)),
14057            vec![128, 128, 128, 77]
14058        );
14059        assert_eq!(
14060            sizes(&fixed_prime_chunk_ranges(1833, 230)),
14061            vec![230, 230, 230, 230, 230, 230, 230, 223]
14062        );
14063        assert_eq!(sizes(&fixed_prime_chunk_ranges(4096, 512)), vec![512; 8]);
14064        let capped = sizes(&fixed_prime_chunk_ranges_for_ring(8200, 4096, true));
14065        assert_eq!(capped, vec![4096, 4088, 16]);
14066        assert!(capped.iter().all(|&rows| rows <= 4096));
14067        assert_eq!(
14068            sizes(&fixed_prime_chunk_ranges_for_ring(4100, 4096, false)),
14069            vec![4100],
14070            "flag-off schedule remains byte-for-byte the legacy monolithic tail",
14071        );
14072    }
14073
14074    #[test]
14075    fn dynamic_schedule_matches_registered_shapes() {
14076        let cases = [
14077            (461, vec![64, 141, 132, 124]),
14078            (1833, vec![115, 269, 260, 252, 244, 237, 231, 225]),
14079            (4096, vec![256, 602, 580, 563, 545, 531, 516, 503]),
14080        ];
14081        for (t, expected) in cases {
14082            let chunk = auto_chunk(t);
14083            let fixed = fixed_prime_chunk_ranges(t, chunk);
14084            assert_eq!(
14085                sizes(&dynamic_prime_chunk_ranges(t, chunk, &fixed)),
14086                expected
14087            );
14088        }
14089    }
14090
14091    #[test]
14092    fn dynamic_schedule_covers_exactly_and_shrinks_after_fill() {
14093        for t in 256..=8192 {
14094            let chunk = auto_chunk(t);
14095            let fixed = fixed_prime_chunk_ranges(t, chunk);
14096            let dynamic = dynamic_prime_chunk_ranges(t, chunk, &fixed);
14097            assert_eq!(dynamic.len(), fixed.len(), "T={t}");
14098            assert_eq!(dynamic.first().unwrap().0, 0, "T={t}");
14099            assert_eq!(dynamic.last().unwrap().1, t, "T={t}");
14100            for pair in dynamic.windows(2) {
14101                assert_eq!(pair[0].1, pair[1].0, "T={t}");
14102            }
14103            assert!(
14104                dynamic
14105                    .iter()
14106                    .all(|(start, end)| end - start >= PRIME_MIN_T),
14107                "T={t} sizes={:?}",
14108                sizes(&dynamic)
14109            );
14110            if dynamic.len() >= 3 {
14111                let chunk_sizes = sizes(&dynamic);
14112                assert!(
14113                    chunk_sizes[0] < chunk_sizes[1],
14114                    "T={t} sizes={chunk_sizes:?}"
14115                );
14116                assert!(
14117                    chunk_sizes[1..].windows(2).all(|pair| pair[0] >= pair[1]),
14118                    "T={t} sizes={chunk_sizes:?}"
14119                );
14120            }
14121        }
14122    }
14123}
14124
14125#[cfg(test)]
14126mod page_prefetch_tests {
14127    use super::{
14128        grouped_worker_prefetch_position, page_prefetch_positions,
14129        page_prefetch_window_from_values, worker_prefetch_positions,
14130    };
14131
14132    #[test]
14133    fn page_prefetch_window_keeps_existing_opt_in_default() {
14134        assert_eq!(page_prefetch_window_from_values(false, None), 0);
14135        assert_eq!(page_prefetch_window_from_values(false, Some("8")), 0);
14136        assert_eq!(page_prefetch_window_from_values(true, None), 1);
14137        assert_eq!(page_prefetch_window_from_values(true, Some("bad")), 1);
14138        assert_eq!(page_prefetch_window_from_values(true, Some("0")), 0);
14139        assert_eq!(page_prefetch_window_from_values(true, Some("8")), 8);
14140    }
14141
14142    #[test]
14143    fn rolling_page_prefetch_advises_each_future_expert_once() {
14144        let advised: Vec<_> = (0..7)
14145            .flat_map(|position| page_prefetch_positions(position, 7, 3))
14146            .collect();
14147        assert_eq!(advised, vec![1, 2, 3, 4, 5, 6]);
14148
14149        let one_ahead: Vec<_> = (0..4)
14150            .flat_map(|position| page_prefetch_positions(position, 4, 1))
14151            .collect();
14152        assert_eq!(one_ahead, vec![1, 2, 3]);
14153        assert!(page_prefetch_positions(0, 4, 0).is_empty());
14154    }
14155
14156    #[test]
14157    fn grouped_worker_prefetch_primes_first_then_each_known_next_once() {
14158        assert_eq!(grouped_worker_prefetch_position(0, None), None);
14159        let positions: Vec<_> = std::iter::once(grouped_worker_prefetch_position(4, None).unwrap())
14160            .chain(
14161                (0..4).filter_map(|position| grouped_worker_prefetch_position(4, Some(position))),
14162            )
14163            .collect();
14164        assert_eq!(positions, vec![0, 1, 2, 3]);
14165        assert_eq!(grouped_worker_prefetch_position(1, Some(0)), None);
14166    }
14167
14168    #[test]
14169    fn rolling_worker_prefetch_primes_current_and_each_future_expert_once() {
14170        let queued: Vec<_> = (0..8)
14171            .flat_map(|position| worker_prefetch_positions(position, 8, 5))
14172            .collect();
14173        assert_eq!(queued, (0..8).collect::<Vec<_>>());
14174
14175        let one_at_a_time: Vec<_> = (0..4)
14176            .flat_map(|position| worker_prefetch_positions(position, 4, 1))
14177            .collect();
14178        assert_eq!(one_at_a_time, vec![0, 1, 2, 3]);
14179        assert!(worker_prefetch_positions(0, 4, 0).is_empty());
14180    }
14181}
14182
14183pub struct G4DcSlots {
14184    x: CudaSlice<f32>,
14185    xn: CudaSlice<f32>,
14186    cur: CudaSlice<f32>,
14187    hq: CudaSlice<i8>,
14188    hd_: CudaSlice<f32>,
14189    q0: CudaSlice<f32>,
14190    k0: CudaSlice<f32>,
14191    v0: CudaSlice<f32>,
14192    q: CudaSlice<f32>,
14193    k: CudaSlice<f32>,
14194    v: CudaSlice<f32>,
14195    attn: CudaSlice<f32>,
14196    o: CudaSlice<f32>,
14197    attn_out: CudaSlice<f32>,
14198    zsh: CudaSlice<f32>,
14199    zq: CudaSlice<i8>,
14200    zd: CudaSlice<f32>,
14201    gate: CudaSlice<f32>,
14202    up: CudaSlice<f32>,
14203    act: CudaSlice<f32>,
14204    actq: CudaSlice<i8>,
14205    actd: CudaSlice<f32>,
14206    f0: CudaSlice<f32>,
14207    sn: CudaSlice<f32>,
14208    hn: CudaSlice<f32>,
14209    logits: CudaSlice<f32>,
14210}