Skip to main content

memra_engine/
hybrid_forward.rs

1//! Hybrid forward pass (Stage-1, f32, prefill, single sequence). Per layer dispatches to a
2//! linear-attention (Gated DeltaNet) or full-attention mixer, then SwiGLU FFN. Matches
3//! llama.cpp src/models/qwen35.cpp node-for-node.
4
5use cudarc::driver::CudaSlice;
6use memra_gguf::config::ModelConfig;
7use crate::Engine;
8use crate::cache::Cache;
9
10/// Resident trunk transients for the eager prime (piecewise-graph foundation; see
11/// HybridModel::prime_slabs). Every buffer is fully overwritten before use per prime.
12pub struct PrimeSlabs {
13    pub t_cap: usize,
14    pub h: CudaSlice<f32>,
15    pub x1: CudaSlice<f32>,
16    pub z: CudaSlice<f32>,
17    pub act: CudaSlice<f32>,
18    pub xa: CudaSlice<f32>,
19    pub xb: CudaSlice<f32>,
20    pub h16: CudaSlice<u8>,
21    pub z16: CudaSlice<u8>,
22    /// piecewise boundary slabs (increment 2): GEMM outputs land here so the
23    /// downstream captured segments see fixed addresses.
24    pub gate: CudaSlice<f32>,     // t * n_ff_max
25    pub up: CudaSlice<f32>,       // t * n_ff_max
26    pub ffn_out: CudaSlice<f32>,  // t * n_embd
27    /// piecewise increment 3: per-layer S-glue segment graphs (down-add + next
28    /// attn-norm, ALL-slab IO, zero in-graph allocations -> keeperless capture is
29    /// clean). Baked at this t_cap; replay only when t == t_cap. seg_glue[il] fires
30    /// between layer il and il+1 (ping-pong parity is deterministic per il).
31    pub seg_glue: Vec<Option<cudarc::driver::CudaGraph>>,
32    /// increment 5 (core-split edition): the mixer out-GEMM writes _into_ `mixed`
33    /// directly (no staging copy — the increment-4 copy route was refuted), making
34    /// S-mid [add + post-norm] all-slab and capturable.
35    pub mixed: CudaSlice<f32>,
36    pub seg_mid: Vec<Option<cudarc::driver::CudaGraph>>,
37    pub seg_t: usize,
38}
39
40// 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
124/// task #18 (attn side): one sequence's pre-attention outputs (post-rope q/k, v, out-gate).
125pub(crate) struct AttnPre {
126    pub q: cudarc::driver::CudaSlice<f32>,
127    pub k: cudarc::driver::CudaSlice<f32>,
128    pub v: cudarc::driver::CudaSlice<f32>,
129    pub gate: Option<cudarc::driver::CudaSlice<f32>>,
130}
131
132/// task #18: one sequence's GDN prep outputs (the scan inputs).
133pub(crate) struct GdnPrep {
134    pub hk: usize,
135    pub q_l2: cudarc::driver::CudaSlice<f32>,
136    pub k_l2: cudarc::driver::CudaSlice<f32>,
137    pub v_g: cudarc::driver::CudaSlice<f32>,
138    pub beta: cudarc::driver::CudaSlice<f32>,
139    pub g_log: cudarc::driver::CudaSlice<f32>,
140    pub kb16: Option<cudarc::driver::CudaSlice<u8>>,
141    pub qb16: Option<cudarc::driver::CudaSlice<u8>>,
142}
143
144/// Device scratch for the burst verify stream (see `verify_stream_scratch`).
145pub(crate) struct VerifyStreamScratch {
146    pub pos_d: CudaSlice<i32>,
147    pub row_ctrs: Vec<CudaSlice<i32>>,
148}
149use crate::hybrid::{HybridModel, Mixer, FullAttnLayer, LinearAttnLayer, MoeWeights};
150
151struct MoeInputTraceWriter {
152    dir: std::path::PathBuf,
153    index: std::fs::File,
154    payloads: std::collections::HashMap<u16, (std::fs::File, u64)>,
155}
156
157static MOE_INPUT_TRACE_WRITER: std::sync::OnceLock<
158    std::sync::Mutex<Option<MoeInputTraceWriter>>,
159> = std::sync::OnceLock::new();
160
161/// STAGE-2 GROUPED DECODE gate (MEMRA_MOE_GDEC, default ON; `=0` restores the sequential
162/// per-expert launch chain). See `moe_gdec_token`.
163fn gdec_enabled() -> bool {
164    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
165    *E.get_or_init(|| std::env::var("MEMRA_MOE_GDEC").map(|v| v != "0").unwrap_or(true))
166}
167
168/// SLAB-LOCAL RESIDENT ARM gate (lane/pp-leverb 2026-08-08, MEMRA_MOE_SLAB, default ON;
169/// `=0` restores the SLRU dispatch even when resident slabs exist). Read PER CALL, never
170/// memoized — probes A/B the two provenances in one process (the MEMRA_PRIME_PP pattern).
171/// See `moe_ffn_sequential_zq8`'s slab_local arm: the sigmoid-router archs (step35/M3/Hy3)
172/// are denied every `dev_exps` consumer (pairs/dev route softmax), so before this arm the
173/// fits-VRAM resident slabs were UPLOADED for them but never READ — the SLRU kept staging
174/// the same bytes beside a dead copy (37 GB H2D per pp4096 prime on the Step SKU, anatomy
175/// receipt). The arm reads the SAME bytes through the SAME kernels; only the pointer
176/// PROVENANCE changes (slab base + ex*stride vs SLRU slot address) — the bit-identity class
177/// `moe_ffn_dev`'s resident arm already documents against its SLRU arm.
178fn moe_slab_enabled() -> bool {
179    std::env::var("MEMRA_MOE_SLAB").as_deref() != Ok("0")
180}
181
182/// Expert-grouped dispatch remains opt-in after the local 5090 transfer gate rejected the
183/// default flip. `=0` selects the established path, while any other explicit value enables the
184/// grouped research arm for the current call.
185fn moe_grouped_enabled(_cfg: &ModelConfig, _prefill: bool) -> bool {
186    std::env::var("MEMRA_MOE_GROUPED")
187        .map(|value| value != "0")
188        .unwrap_or(false)
189}
190
191/// Deterministic in-token expert prefetch. `MEMRA_MOE_PREFETCH=1` overlaps memory-source H2D on the
192/// copy stream; selecting the opt-in worker spill backend enables the same known-next hook for disk.
193fn moe_prefetch_enabled() -> bool {
194    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
195    *E.get_or_init(|| std::env::var("MEMRA_MOE_PREFETCH").as_deref() == Ok("1")
196        || crate::spill_pread::worker_enabled())
197}
198
199/// Best-effort OS page-cache prefetch distance for mmap-backed expert ranges. Independent of the
200/// H2D copy-stream experiment so storage->RAM and RAM->HBM overlap can be measured separately.
201/// The opt-in default stays one expert to preserve the original experiment; spill rigs can widen
202/// it with `MEMRA_MOE_PAGE_PREFETCH_WINDOW` to cover NVMe latency.
203fn moe_page_prefetch_window() -> usize {
204    static W: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
205    *W.get_or_init(|| page_prefetch_window_from_values(
206        std::env::var("MEMRA_MOE_PAGE_PREFETCH").as_deref() == Ok("1"),
207        std::env::var("MEMRA_MOE_PAGE_PREFETCH_WINDOW").ok().as_deref(),
208    ))
209}
210
211fn page_prefetch_window_from_values(enabled: bool, raw_window: Option<&str>) -> usize {
212    if !enabled {
213        return 0;
214    }
215    raw_window
216        .and_then(|value| value.parse().ok())
217        .unwrap_or(1)
218}
219
220/// Return only the newly exposed positions in a rolling lookahead window. Position zero seeds the
221/// full window; each later position adds one expert at the far edge. Thus widening the window does
222/// not repeatedly issue `MADV_WILLNEED` for the same range.
223fn page_prefetch_positions(
224    position: usize,
225    len: usize,
226    window: usize,
227) -> std::ops::Range<usize> {
228    if window == 0 || position >= len {
229        return len..len;
230    }
231    let (start, count) = if position == 0 {
232        (1, window)
233    } else {
234        (position.saturating_add(window), 1)
235    };
236    let start = start.min(len);
237    start..start.saturating_add(count).min(len)
238}
239
240/// Grouped worker-I/O schedule: prime the first active expert before the loop, then queue exactly
241/// one known-next expert at each iteration. Returning positions keeps expert ordering authoritative.
242fn grouped_worker_prefetch_position(order_len: usize, current: Option<usize>) -> Option<usize> {
243    let position = current.map_or(0, |position| position.saturating_add(1));
244    (position < order_len).then_some(position)
245}
246
247/// Fill the worker ring with complete experts, retaining one pinned buffer for an unexpected
248/// demand miss. Each expert has gate/up/down extents, so depth 16 admits a rolling five-expert
249/// window. Position zero primes the current expert too: its three independent reads can run in
250/// parallel instead of demand-serializing gate, up, and down before any useful GPU work exists.
251fn worker_prefetch_window() -> usize {
252    static WINDOW: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
253    *WINDOW.get_or_init(|| {
254        let automatic = crate::spill_pread::configured_depth().saturating_sub(1) / 3;
255        std::env::var("MEMRA_SPILL_WORKER_EXPERT_WINDOW")
256            .ok()
257            .and_then(|value| value.parse::<usize>().ok())
258            .unwrap_or(automatic.max(1))
259    })
260}
261
262/// Return only positions newly exposed by a rolling worker-I/O window. Unlike mmap page advice,
263/// this includes the current expert when the window is seeded so all three current projections
264/// enter the CPU pool together.
265fn worker_prefetch_positions(position: usize, len: usize, window: usize) -> std::ops::Range<usize> {
266    if window == 0 || position >= len {
267        return len..len;
268    }
269    let (start, count) = if position == 0 {
270        (0, window)
271    } else {
272        (position.saturating_add(window).saturating_sub(1), 1)
273    };
274    let start = start.min(len);
275    start..start.saturating_add(count).min(len)
276}
277
278/// LAUNCH-STRUCTURE STAGE 3 gate (MEMRA_MOE_DEV, default ON; `=0` restores host routing). The
279/// zero-DtoH device-dispatch path for fully-resident layers: router top-k output stays on device,
280/// expert weight pointers come from the per-layer device table. Requires the fused router (the
281/// dev path consumes the device sel/w directly), so MEMRA_FUSED_ROUTER=0 also disables it.
282fn moe_dev_enabled() -> bool {
283    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
284    *E.get_or_init(|| std::env::var("MEMRA_MOE_DEV").map(|v| v != "0").unwrap_or(true)
285        && !matches!(std::env::var("MEMRA_FUSED_ROUTER").as_deref(), Ok("0")))
286}
287
288/// MoE EXPERT dp4a gate (MEMRA_MOE_Q8, default ON; `=0` restores the Stage-A f32-dequant expert
289/// kernels). Applies when gate/up/down expert qtypes are all in the dp4a body set (IQ3_S/IQ4_XS).
290/// FP-order differs from Stage-A (int dp4a + warp tree) — argmax/run-gen/stream-identity gates
291/// arbitrate; the sequential and fused q8 paths ship as a matched pair (MEMRA_MOE_GATE contract).
292fn moe_q8_enabled() -> bool {
293    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
294    *E.get_or_init(|| std::env::var("MEMRA_MOE_Q8").map(|v| v != "0").unwrap_or(true))
295}
296
297/// gemma4 fast-arm gate: qtypes with an `expert_dot_g` dp4a body (superset used by the gelu
298/// dev arm; the qwen q8 arms keep their own battery-gated q8_expert_supported policy).
299fn expert_dp4a_supported(qt: i32) -> bool {
300    qt == crate::QT_Q4_0 || qt == crate::QT_IQ3_S || qt == crate::QT_IQ4_XS
301        || qt == crate::QT_Q3_K || qt == crate::QT_Q4_K || qt == crate::QT_Q6_K
302}
303
304fn q8_expert_supported(qt: i32) -> bool {
305    // k-quant arms added 2026-07-06 (Q3_K/Q4_K/Q6_K bodies for the UD tail layers). Briefly
306    // default-excluded the same day when they appeared to break 35B real-prompt spec — the
307    // ACTUAL culprit was the MoE router's cuBLASLt n-dependence (d994271); with the router
308    // decode-exact at verify t, the k-quant arms pass the full spec battery (p1/p2/p3 + raw
309    // K=1..8) and are DEFAULT ON again (+9 tok/s: 148.9 -> 157.9). MEMRA_MOE_Q8_KQ=0 excludes.
310    static KQ: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
311    let kq = *KQ.get_or_init(|| {
312        std::env::var("MEMRA_MOE_Q8_KQ").map(|v| v != "0").unwrap_or(true)
313    });
314    // NVFP4 experts: DEFAULT ON (2026-07-17). The M3-era "decode-vs-verify MISMATCH 3.4e1"
315    // that had this excluded was the missing per-expert macro-scale fold, fixed in the
316    // dev-kernel epilogues + moe_w_scale_by_expert; the 35B ct-NVFP4 artifact now runs the
317    // q8 arm at parity with the IQ4_XS daily (174-178 tok/s, spec K=1..8 exact). M3/Hy3
318    // never reach the q8 arms regardless (sigmoid-router cfg gates on pairs/dev/gdec).
319    // MEMRA_MOE_Q8_NVFP4=0 restores the f32 arm.
320    let nvfp4_q8 = std::env::var("MEMRA_MOE_Q8_NVFP4").map(|v| v != "0").unwrap_or(true);
321    qt == crate::QT_IQ3_S || qt == crate::QT_IQ4_XS || (nvfp4_q8 && qt == crate::QT_NVFP4)
322        || (kq && (qt == crate::QT_Q3_K || qt == crate::QT_Q4_K || qt == crate::QT_Q6_K))
323}
324
325/// The decode-once (_dec) and IQ-MMA expert kernels dequant via IQ-specific extractors —
326/// k-quant tensors must fall to the _em dot path instead.
327fn q8_expert_dec_supported(qt: i32) -> bool {
328    qt == crate::QT_IQ3_S || qt == crate::QT_IQ4_XS || qt == crate::QT_Q4_0
329}
330
331/// Grouped-f16 door (MEMRA_MOE_F16G) per-projection admission: the qtype has a dequant-to-f16
332/// kernel in cu/moe_f16_grouped.cu AND the projection's k dimension tiles its block size.
333/// Round 49 widened coverage to q35's UD mix (gate/up IQ3_S x39 + Q3_K x1 + IQ4_XS x1; down
334/// IQ4_XS x37 + Q6_K x3 + Q4_K x1) — the round-47 IQ4_XS/Q4_0-only table admitted ~1 of 41
335/// q35 layers, which is why that cell measured FLAT.
336fn f16g_proj_ok(qt: i32, in_f: usize) -> bool {
337    match qt {
338        crate::QT_Q4_0 => in_f % 32 == 0,
339        crate::QT_IQ4_XS | crate::QT_IQ3_S | crate::QT_Q3_K | crate::QT_Q4_K
340        | crate::QT_Q6_K => in_f % 256 == 0,
341        _ => false,
342    }
343}
344
345/// STAGE 3 prewarm gate (MEMRA_MOE_PREWARM, default ON; `=0` leaves residency organic). One-shot
346/// per layer: force-admit every block while FREE slots cover the whole layer (never evicts).
347fn moe_prewarm_enabled() -> bool {
348    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
349    *E.get_or_init(|| std::env::var("MEMRA_MOE_PREWARM").map(|v| v != "0").unwrap_or(true))
350}
351
352/// During a discarded fixed-residency profile, admit CPU-routed misses after their current-token
353/// CPU result is complete. The current result and numeric path are unchanged; later warmup tokens
354/// can then vote for and exercise those experts on GPU before the cache is frozen.
355fn cpu_expert_profile_admit_enabled() -> bool {
356    static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
357    *E.get_or_init(|| std::env::var("MEMRA_CPU_EXPERT_FREEZE_PROFILE_ADMIT").as_deref() == Ok("1"))
358}
359
360/// Minimum prompt length for the BATCHED cache prime (`prime_cache`). Below this the tokenwise
361/// decode loop wins anyway (the batched path's GEMM dispatch needs m>=16, and the stateful conv
362/// kernel needs T >= d_conv-1). Callers: generate / generate_spec.
363pub const PRIME_MIN_T: usize = 16;
364const PRIME_PIPE_MICROBATCHES: usize = 8;
365const PRIME_PIPE_MIN_CHUNK: usize = 128;
366const PRIME_PIPE_EDGE_MIN_CHUNK: usize = 64;
367const PRIME_PIPE_LINEAR_WORK: usize = 8;
368
369fn prime_pp2_auto_geometry(n_layers: usize) -> bool {
370    crate::pp::prime_pp_on()
371        && !crate::pp::pp2_streams_off()
372        && crate::pp::pp_cuts(n_layers).is_some_and(|cuts| cuts.len() == 3)
373}
374
375/// Effective internal prime chunk. An explicit MEMRA_PRIME_CHUNK is authoritative.
376/// Naked PP-2 primes use the measured pipeline geometry: up to eight microchunks, never
377/// below 128 tokens, while the legacy 4096-token cap remains the long-context bound.
378pub fn prime_chunk_tokens(t: usize, n_layers: usize) -> usize {
379    if let Ok(value) = std::env::var("MEMRA_PRIME_CHUNK") {
380        let parsed = value.parse::<usize>().unwrap_or(crate::cache::PRIME_CHUNK_MAX_TOKENS);
381        return if crate::cache::swa_ring_on() {
382            if parsed == 0 {
383                crate::cache::PRIME_CHUNK_MAX_TOKENS
384            } else {
385                parsed.min(crate::cache::PRIME_CHUNK_MAX_TOKENS)
386            }
387        } else {
388            parsed
389        };
390    }
391    let chunk = crate::cache::PRIME_CHUNK_MAX_TOKENS;
392    if prime_pp2_auto_geometry(n_layers) && t >= 2 * PRIME_PIPE_MIN_CHUNK {
393        chunk.min(
394            t.div_ceil(PRIME_PIPE_MICROBATCHES)
395                .max(PRIME_PIPE_MIN_CHUNK),
396        )
397    } else {
398        chunk
399    }
400}
401
402fn fixed_prime_chunk_ranges(t: usize, chunk: usize) -> Vec<(usize, usize)> {
403    fixed_prime_chunk_ranges_for_ring(t, chunk, crate::cache::swa_ring_on())
404}
405
406fn fixed_prime_chunk_ranges_for_ring(t: usize, chunk: usize, ring_on: bool) -> Vec<(usize, usize)> {
407    if chunk == 0 || t <= chunk {
408        return vec![(0, t)];
409    }
410    let mut ranges = Vec::with_capacity(t.div_ceil(chunk));
411    let mut start = 0usize;
412    while start < t {
413        let mut end = (start + chunk).min(t);
414        if t - end > 0 && t - end < PRIME_MIN_T {
415            if ring_on {
416                let shifted = t - PRIME_MIN_T;
417                end = if shifted > start { shifted } else { t };
418            } else {
419                end = t;
420            }
421        }
422        ranges.push((start, end));
423        start = end;
424    }
425    ranges
426}
427
428fn prime_chunk_work(prefix: usize, total: usize) -> u128 {
429    let prefix = prefix as u128;
430    prefix * (prefix + (PRIME_PIPE_LINEAR_WORK as u128) * (total as u128))
431}
432
433fn dynamic_prime_chunk_ranges(
434    t: usize,
435    fixed_chunk: usize,
436    fixed: &[(usize, usize)],
437) -> Vec<(usize, usize)> {
438    let n = fixed.len();
439    if n < 3 {
440        return fixed.to_vec();
441    }
442
443    let max_first = t - (n - 1) * PRIME_MIN_T;
444    let first = fixed_chunk
445        .div_ceil(2)
446        .max(PRIME_PIPE_EDGE_MIN_CHUNK)
447        .min(max_first);
448    let mut ranges = Vec::with_capacity(n);
449    ranges.push((0, first));
450
451    let first_work = prime_chunk_work(first, t);
452    let work_span = prime_chunk_work(t, t) - first_work;
453    let denominator = (n - 1) as u128;
454    let mut previous = first;
455    for boundary in 1..n - 1 {
456        let target = first_work * denominator + work_span * (boundary as u128);
457        let remaining = n - 1 - boundary;
458        let mut low = previous + PRIME_MIN_T;
459        let mut high = t - remaining * PRIME_MIN_T;
460        while low < high {
461            let mid = low + (high - low) / 2;
462            if prime_chunk_work(mid, t) * denominator >= target {
463                high = mid;
464            } else {
465                low = mid + 1;
466            }
467        }
468        ranges.push((previous, low));
469        previous = low;
470    }
471    ranges.push((previous, t));
472    ranges
473}
474
475/// Internal prime ranges. The naked PP-2 pipeline defaults to a short-fill,
476/// equal-modeled-time schedule; MEMRA_PRIME_CHUNK_SCHED=fixed restores the measured
477/// equal-token ranges. An explicit MEMRA_PRIME_CHUNK always retains fixed semantics.
478pub fn prime_chunk_ranges(t: usize, n_layers: usize) -> Vec<(usize, usize)> {
479    let explicit_chunk = std::env::var_os("MEMRA_PRIME_CHUNK").is_some();
480    let chunk = prime_chunk_tokens(t, n_layers);
481    let fixed = fixed_prime_chunk_ranges(t, chunk);
482    let dynamic = match std::env::var("MEMRA_PRIME_CHUNK_SCHED") {
483        Ok(value) => value == "dynamic",
484        Err(_) => true,
485    };
486    if explicit_chunk || !dynamic || !prime_pp2_auto_geometry(n_layers) {
487        fixed
488    } else {
489        dynamic_prime_chunk_ranges(t, chunk, &fixed)
490    }
491}
492
493impl HybridModel {
494    /// CHUNK-INVARIANCE BISECT SEAM (lane/chunk-invariance): `MEMRA_PRIME_TRACE=<path>`
495    /// appends one JSONL row per (chunk, layer) with a hash of that layer's last-row
496    /// post-residual hidden. Diagnostic only — never on in a measured or gated run
497    /// (it forces a dtoh + host hash per layer).
498    fn prime_trace_path() -> Option<&'static str> {
499        static P: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
500        P.get_or_init(|| std::env::var("MEMRA_PRIME_TRACE").ok())
501            .as_deref()
502    }
503
504    /// Prefill forward over `tokens`; returns logits [T, n_vocab] (host f32).
505    pub fn forward(&self, e: &Engine, tokens: &[u32]) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
506        if self.is_gemma4_e4b() { return self.gemma4_e4b_forward(e, tokens, false); }
507        if self.cfg.gemma4.is_some() { return self.gemma4_forward(e, tokens, false); }
508        let cfg = &self.cfg;
509        let n_embd = cfg.n_embd as usize;
510        let t = tokens.len();
511        let eps = cfg.rms_eps;
512        let pos: Vec<i32> = (0..t as i32).collect();
513        let pos_d = e.htod_i32(&pos)?;
514
515        let mut x = self.embed(e, tokens)?;   // [T, n_embd]
516
517        for (il, layer) in self.layers.iter().enumerate() {
518            // attn_norm
519            let mut h = e.uninit(t * n_embd)?;
520            e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
521
522            let mixed = match &layer.mixer {
523                Mixer::Full(fa) => self.full_attn(e, fa, &h, &pos_d, t, il)?,
524                Mixer::Linear(la) => self.linear_attn(e, la, &h, t)?,
525                Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
526            };
527
528            // residual 1
529            let mut x1 = e.uninit(t * n_embd)?;
530            e.add(&x, &mixed, &mut x1, t * n_embd)?;
531
532            // pre-FFN norm (post_attention_norm), FFN (Dense or MoE), residual 2
533            let mut z = e.uninit(t * n_embd)?;
534            e.rms_norm(&x1, layer.post_attn_norm.float_data(), &mut z, n_embd, t, eps)?;
535            let ffn_out = match &layer.ffn {
536                crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } => {
537                    let n_ff = ffn_gate.out_features();
538                    let mut g2 = e.matmul_group(&[ffn_gate, ffn_up], &z, t)?;
539                    let up = g2.pop().unwrap();
540                    let gate = g2.pop().unwrap();
541                    let mut act = e.uninit(t * n_ff)?;
542                    // A DENSE FFN reads the SHEXP clamp array: upstream's one `build_ffn` serves
543                    // both the dense MLP and the shared expert, and its limit is
544                    // `swiglu_clamp_shexp[il]` (llama-graph.cpp:1751). step35's leading dense
545                    // blocks 0-2 therefore key off clamp_shexp, not clamp_exp.
546                    Self::ffn_act_lim(e, &self.cfg, &gate, &up, 1.0, 1.0,
547                                      self.cfg.clamp_shexp_at(il as u32), &mut act, t * n_ff)?;
548                    e.matmul(ffn_down, &act, t)?
549                }
550                crate::hybrid::Ffn::Moe(m) => self.moe_ffn_il_prefill(e, m, &z, t, il as u16)?,
551            };
552            let mut x2 = e.uninit(t * n_embd)?;
553            e.add(&x1, &ffn_out, &mut x2, t * n_embd)?;
554            x = x2;
555        }
556
557        let mut hn = e.uninit(t * n_embd)?;
558        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
559        let logits = e.matmul(&self.output, &hn, t)?;
560        Ok(e.dtoh(&logits)?)
561    }
562
563    /// Prefill that returns ONLY the last token's logits — the common case (greedy/sample needs
564    /// just the final position to start decode). Runs the trunk over all T, then the lm_head
565    /// (output.weight, the largest matrix — 248320 rows) on the LAST hidden row ONLY, not all T.
566    /// On a 512-token prompt this turns a [512,248320] GEMM into [1,248320] — the dominant prefill
567    /// cost (nsys: ~99ms when done for all T). Bit-identical last-row logits to forward()[last].
568    pub fn forward_last(&self, e: &Engine, tokens: &[u32]) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
569        if self.cfg.gemma4.is_some() { return self.gemma4_forward(e, tokens, true); }
570        let cfg = &self.cfg;
571        let n_embd = cfg.n_embd as usize;
572        let t = tokens.len();
573        let eps = cfg.rms_eps;
574        let pos: Vec<i32> = (0..t as i32).collect();
575        let pos_d = e.htod_i32(&pos)?;
576
577        let mut x = self.embed(e, tokens)?;   // [T, n_embd]
578        // MEMRA_LAYER_PROBE=1: synchronize + print after every stage — bisects an in-graph
579        // ILLEGAL_ADDRESS to (layer, stage) at ~1 line of output per layer (M3 bring-up tool).
580        let probe = std::env::var("MEMRA_LAYER_PROBE").is_ok();
581        for (il, layer) in self.layers.iter().enumerate() {
582            let mut h = e.uninit(t * n_embd)?;
583            e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
584            if probe { e.stream().synchronize()?; eprintln!("[probe] L{il} norm ok"); }
585            let mixed = match &layer.mixer {
586                Mixer::Full(fa) => self.full_attn(e, fa, &h, &pos_d, t, il)?,
587                Mixer::Linear(la) => self.linear_attn(e, la, &h, t)?,
588                Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
589            };
590            if probe { e.stream().synchronize()?; eprintln!("[probe] L{il} mixer ok"); }
591            let mut x1 = e.uninit(t * n_embd)?;
592            e.add(&x, &mixed, &mut x1, t * n_embd)?;
593            let mut z = e.uninit(t * n_embd)?;
594            e.rms_norm(&x1, layer.post_attn_norm.float_data(), &mut z, n_embd, t, eps)?;
595            let ffn_out = match &layer.ffn {
596                crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } => {
597                    let n_ff = ffn_gate.out_features();
598                    let mut g2 = e.matmul_group(&[ffn_gate, ffn_up], &z, t)?;
599                    let up = g2.pop().unwrap();
600                    let gate = g2.pop().unwrap();
601                    let mut act = e.uninit(t * n_ff)?;
602                    // dense FFN keys off the SHEXP clamp array — see forward()'s note.
603                    Self::ffn_act_lim(e, &self.cfg, &gate, &up, 1.0, 1.0,
604                                      self.cfg.clamp_shexp_at(il as u32), &mut act, t * n_ff)?;
605                    e.matmul(ffn_down, &act, t)?
606                }
607                crate::hybrid::Ffn::Moe(m) => self.moe_ffn_il_prefill(e, m, &z, t, il as u16)?,
608            };
609            if probe { e.stream().synchronize()?; eprintln!("[probe] L{il} ffn ok"); }
610            let mut x2 = e.uninit(t * n_embd)?;
611            e.add(&x1, &ffn_out, &mut x2, t * n_embd)?;
612            x = x2;
613        }
614        // norm over all T, then slice the LAST row and run lm_head on that single row.
615        let mut hn = e.uninit(t * n_embd)?;
616        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
617        let last = e.view(&hn, t * n_embd);            // [T, n_embd]
618        let last_row = last.slice((t - 1) * n_embd..t * n_embd);  // [1, n_embd]
619        let mut hlast = e.uninit(n_embd)?;
620        e.copy_view_into(&mut hlast, 0, &last_row, n_embd)?;
621        let logits = e.matmul(&self.output, &hlast, 1)?;   // [1, n_vocab] — lm_head on ONE row
622        Ok(e.dtoh(&logits)?)
623    }
624
625    /// BATCHED PROMPT PRIME (the measured #1 e2e gap, e2e-image-1): `forward_last`'s batched
626    /// prefill body EXTENDED to leave a DECODE-READY cache behind — vs the tokenwise prime's
627    /// ~102/38 tok/s (9B/27B) decode_step loop, this runs the whole prompt at prefill throughput.
628    ///   (a) full-attn layers append their T post-RoPE K/V rows into `cache.kv[il]` via the SAME
629    ///       per-row quantize kernel as the decode append (bit-identical cache bytes per row);
630    ///   (b) linear layers run STATEFULLY from the cache's current recurrent state (zero at a
631    ///       fresh prime): carried-ring conv (ssm_conv1d_tm_state) + ONE gdn_scan(state_in,
632    ///       state_out) whose internal sequential t-loop equals T chained T=1 steps — but with
633    ///       the NORMAL prefill matmul dispatch (GEMM at m>=16), NOT the decode-exact MMVQ the
634    ///       spec verify uses (prime is a prefill-regime pass; the run-gen prefill==decode
635    ///       argmax gate is the accuracy authority, exactly as for forward_last);
636    ///   (c) `cache.pos`/KV len/len_d advance by T.
637    /// Returns (last-row logits host, h_seed = last-row PRE-output_norm hidden [n_embd],
638    /// hiddens = the full pre-output_norm hidden stack [T, n_embd] — generate_spec's prompt_h).
639    /// FRESH-PROMPT ONLY (cache.pos == 0): the fa_prefill tiles attend within `tokens` alone.
640    /// forward_last itself stays untouched (kernel-check / run-gen gate on it).
641    ///
642    /// `queued_after` (lane/tick-seg, 2026-08-07): the number of prompt tokens of the SAME
643    /// REQUEST that the caller will prime in LATER calls — 0 when this call is the whole
644    /// request (every single-shot caller). Serve splits a long prompt across SEVERAL
645    /// prime_cache calls (one per scheduler tick, plus the prefix-cache LCP split), and the
646    /// request's absolute end position `seq_end = cache.pos + t + queued_after` steers step35's
647    /// SWA prefill arm — computing it per CALL made the arm a function of the tick budget
648    /// (budgets 512/256/64 DIFFER 1.813e0 vs monolithic, greedy diverging at step 6; dark lanes
649    /// default to 256 AND cap by live SLO headroom, so identical judge requests primed
650    /// differently under load — research/tick-seg-20260807, receipt in
651    /// research/step35-chunkfix-20260807 §9). The parameter is what prime_cache structurally
652    /// lacked: it cannot know from `tokens` and `cache` alone whether more of the request is
653    /// coming. A SESSION CONTINUATION (a NEW user turn primed onto a live cache) is a NEW
654    /// request — its arithmetic is keyed to its own extent, so those callers pass 0; only a
655    /// caller that SPLITS one request across calls passes the remainder.
656    pub fn prime_cache(&self, e: &Engine, tokens: &[u32], cache: &mut Cache, queued_after: usize)
657                       -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
658        let n_embd = self.cfg.n_embd as usize;
659        let t = tokens.len();
660        // SESSION CONTINUATION (2026-07-05): cache.pos > 0 = priming a NEW SUFFIX onto a live
661        // session cache — every chunk (including the first) takes the continuation arm
662        // (fa_prefill_view over the quantized past + this chunk). Fresh prime (pos==0) unchanged.
663        assert!(t >= PRIME_MIN_T, "prime_cache needs T >= {PRIME_MIN_T} (caller gates)");
664        assert!(cache.pos + t <= cache.max_ctx, "prime_cache: prompt exceeds cache max_ctx");
665
666        // CHUNKED PRIME (2026-07-05, the long-ctx OOM fix): the monolithic prime allocates
667        // per-layer transients proportional to T (gate/up/act = T*n_ff*4B EACH — 1.5GB apiece at
668        // 16k on the 27B), which OOMs a 24GB card around 16k prompt tokens. Chunk the prompt:
669        // each chunk runs the full layer stack with transients sized to the chunk, appending its
670        // K/V to the resident quantized cache and carrying the GDN conv-ring + recurrent state
671        // through `cache.recur` (linear_attn_prime is already stateful — a chunk boundary is
672        // exactly the state carry it was built for). Full-attn chunks after the first attend to
673        // the QUANTIZED past KV via fa_prefill_view (the spec-verify pattern) — same numeric
674        // class as decode reading the cache. Prompts <= one chunk take the ORIGINAL monolithic
675        // body byte-for-byte (chunk 0 short-circuits to the f32 fa_prefill path).
676        // MEMRA_PRIME_CHUNK sets the chunk size (tokens); 0 disables chunking (monolithic).
677        if self.is_gemma4_e4b() {
678            return self.gemma4_e4b_prime(e, tokens, cache);
679        }
680        if self.cfg.gemma4.is_some() {
681            // gemma4 v0: monolithic fresh-prompt prime (chunked/continuation arms later).
682            return self.gemma4_prime(e, tokens, cache);
683        }
684        let ranges = prime_chunk_ranges(t, self.layers.len());
685        // CHUNK-ORDER INVARIANCE (lane/chunk-invariance, 2026-08-05; vLLM #38561 shape).
686        // MEMRA_PRIME_CHUNK is documented as a memory-transient knob, but it also decides
687        // the prefill's ARITHMETIC, so two rigs with different values produced different
688        // greedy text for the same prompt (research/session-affinity-20260805: 97- and
689        // 149-token prompts). ROOT CAUSE, measured in research/chunk-invariance-20260805
690        // (VERDICT.md) — and it is NOT what docs originally said:
691        //   * NOT trunk GEMM m / reduction order. REFUTED: the prefill GEMM is m-INVARIANT
692        //     (rows [0,32) bit-identical at m=32 vs m=33..80, both quantized wq and the
693        //     output head), so growing a chunk cannot move an existing row's value.
694        //   * NOT the GDN scan segmentation. REFUTED: MEMRA_GDN_CHUNKED=0 (sequential scan,
695        //     no WY segmentation at all) still diverges, so vLLM's mamba-boundary fix does
696        //     not describe our leak.
697        //   * IT IS the attention numeric CLASS edge in full_attn_prime_fa_dispatch, which
698        //     selects on `base_len == 0`: chunk 0 attends over this batch's f32 K/V
699        //     (fa_prefill) while every later chunk attends over the q8_0/q5_1 quantized KV
700        //     cache (fa_prefill_view_ws). The chunk size therefore decides WHERE in the
701        //     prompt that precision edge falls. Signature: per-row maxdiff is exactly 0.0
702        //     before the first boundary and O(1) right after, first_div_pos == chunk size.
703        // The grain-free fix (full_attn_prime_fa_dispatch below) removed that class edge at
704        // the source — every row is in one numeric class, so the chunk size no longer steers
705        // arithmetic and MEMRA_PRIME_CHUNK is a pure memory/transient knob again. The interim
706        // MEMRA_PRIME_INVARIANT/MEMRA_PRIME_GRAIN pin-the-boundary door was superseded by that
707        // fix and KILLED at v0.71 per the flags doctrine (the jsonl + VERDICT.md are the
708        // record); the chunkinv gate asserts byte-identity across chunk sizes naked.
709        // The REQUEST's absolute end position, computed ONCE before the loop: every chunk sees the
710        // same value, whatever the chunk size. step35's SWA arm selects on it so that kernel
711        // selection — and therefore the logits — cannot depend on MEMRA_PRIME_CHUNK
712        // (research/step35-chunkfix-20260807; see step35_attn_pre_wo's doc note).
713        // `+ queued_after` closes the SECOND axis (lane/tick-seg): when serve splits the request
714        // across calls, the request still ends at the same absolute position, whatever the tick
715        // budget or LCP split point. MEMRA_PRIME_CALLLOCAL=1 is the ROLLBACK SEAM to the FULL
716        // pre-fix arithmetic: the per-call value here AND the unaligned FA view offset in
717        // step35_attn_pre_wo. Both halves are required for tickinv35's canary teeth under the FA
718        // default. Read per call, not cached (the probe flips it in-process between arms). Never
719        // on in a measured default run.
720        let legacy_calllocal =
721            std::env::var("MEMRA_PRIME_CALLLOCAL").as_deref() == Ok("1");
722        let seq_end = if legacy_calllocal {
723            cache.pos + t
724        } else {
725            cache.pos + t + queued_after
726        };
727        if ranges.len() == 1 {
728            return self.prime_chunk(e, tokens, cache, seq_end);
729        }
730        // PIPELINED PP-2 PRIME (lane/cx-pipeline-prime, 2026-08-08): overlap stage 0 of
731        // chunk N+1 with stage 1 of chunk N. The serial split stays reachable through
732        // MEMRA_PRIME_PIPE=0 and is the exactness oracle. N>2 keeps the serial walker;
733        // this lane owns the balanced two-stage schedule only.
734        if crate::pp::prime_pipe_on()
735            && crate::pp::prime_pp_on()
736            && !crate::pp::pp2_streams_off()
737        {
738            if let Some(fence) = crate::pp::pp_cuts(self.layers.len()).filter(|f| f.len() == 3) {
739                if crate::pp::pp_multi_stream_same_device() {
740                    return Err(
741                        "prime chunk pipeline refused with 2 stage streams on one device — \
742                         that concurrent-stream placement remains quarantined by the deferred \
743                         pp flake record. Use one device per stage or MEMRA_PRIME_PIPE=0 for \
744                         the serial split."
745                            .into(),
746                    );
747                }
748                return self.prime_cache_pp2_pipelined(
749                    e, tokens, cache, seq_end, &ranges, &fence,
750                );
751            }
752        }
753        let mut hiddens = e.uninit(t * n_embd)?;
754        let mut last: Option<(Vec<f32>, CudaSlice<f32>)> = None;
755        for &(start, end) in &ranges {
756            let (l, hs, x) = self.prime_chunk(e, &tokens[start..end], cache, seq_end)?;
757            e.copy_into(&mut hiddens, start * n_embd, &x, (end - start) * n_embd)?;
758            last = Some((l, hs));
759        }
760        let (logits, h_seed) = last.unwrap();
761        Ok((logits, h_seed, hiddens))
762    }
763
764    /// PP-2 chunk scheduler: stage 1 of chunk N and stage 0 of chunk N+1 are both queued
765    /// before N's epilogue D2H drains the last-stage stream. Arithmetic is unchanged:
766    /// every chunk still runs the same two `prime_layers` ranges, boundary copy, output
767    /// norm, lm head, and caller hidden-stack copy as the serial split.
768    fn prime_cache_pp2_pipelined(
769        &self,
770        e: &Engine,
771        tokens: &[u32],
772        cache: &mut Cache,
773        seq_end: usize,
774        ranges: &[(usize, usize)],
775        fence: &[usize],
776    ) -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
777        debug_assert_eq!(fence.len(), 3);
778        debug_assert!(ranges.len() >= 2);
779        let rt = crate::pp::PpNRt::get(e)?;
780        assert_eq!(rt.n_stages(), 2, "prime pipeline requires exactly two PP stages");
781        let n_embd = self.cfg.n_embd as usize;
782        let t = tokens.len();
783        let initial_base = cache.pos;
784        let caller_stream = e.stream();
785
786        // #87 reverse publication before any new stage allocation, then prewarm both
787        // boundary slots while the stage streams are otherwise empty. Lazy-growing slot B
788        // after stage 1(N) is queued would synchronize that stream and erase the first
789        // overlap on a two-chunk prompt.
790        rt.fence_stages_behind(&caller_stream)?;
791        let max_payload = ranges
792            .iter()
793            .map(|(s, e)| (e - s) * n_embd)
794            .max()
795            .unwrap();
796        rt.prepare_overlap_slots(0, max_payload)?;
797
798        let mut hiddens = e.uninit(t * n_embd)?;
799        let mut last: Option<(Vec<f32>, CudaSlice<f32>)> = None;
800        let mut stage_caches = PrimeCacheStages::new(cache, fence[1]);
801        let (cache0, cache1) = stage_caches.parts();
802        let (first_start, first_end) = ranges[0];
803        let mut slot = self.prime_pp2_stage0_enqueue(
804            e,
805            rt,
806            &tokens[first_start..first_end],
807            cache0,
808            seq_end,
809            fence,
810            initial_base + first_start,
811            true,
812        )?;
813        cache0.pos = initial_base + first_end;
814
815        for (i, &(start, end)) in ranges.iter().enumerate() {
816            let base = initial_base + start;
817            debug_assert_eq!(
818                cache1.pos, base,
819                "stage 1 must drain chunks in original position order"
820            );
821            let (out, next_slot) = if let Some(&(next_start, next_end)) = ranges.get(i + 1) {
822                let next_base = initial_base + next_start;
823                debug_assert_eq!(
824                    cache0.pos, next_base,
825                    "stage 0 must issue chunks in original position order"
826                );
827                let cache0_stage = &mut *cache0;
828                // Step's MoE router readback synchronizes once per layer. Two CUDA streams
829                // on one host thread therefore serialize even if the calls are ordered as
830                // a pipeline. Drive the disjoint stage caches from two scoped host threads:
831                // stage 1 consumes slot N while stage 0 produces slot N+1.
832                std::thread::scope(
833                    |scope| -> Result<_, Box<dyn std::error::Error>> {
834                        let stage0 = scope.spawn(move || -> Result<usize, String> {
835                            let next = self
836                                .prime_pp2_stage0_enqueue(
837                                    e,
838                                    rt,
839                                    &tokens[next_start..next_end],
840                                    cache0_stage,
841                                    seq_end,
842                                    fence,
843                                    next_base,
844                                    true,
845                                )
846                                .map_err(|err| err.to_string())?;
847                            cache0_stage.pos = initial_base + next_end;
848                            Ok(next)
849                        });
850                        let x = self.prime_pp2_stage1_enqueue(
851                            e,
852                            rt,
853                            slot,
854                            end - start,
855                            cache1,
856                            seq_end,
857                            fence,
858                            base,
859                            true,
860                        )?;
861                        let out = {
862                            rt.bind_stage(1)?;
863                            let _st1 = rt.enter(1);
864                            let e1 = rt.engine(1, e);
865                            self.prime_chunk_epilogue(e1, x, end - start, cache1)?
866                        };
867                        let next = stage0
868                            .join()
869                            .map_err(|_| "pipeprime stage-0 host walker panicked")?
870                            .map_err(|err| -> Box<dyn std::error::Error> { err.into() })?;
871                        Ok((out, Some(next)))
872                    },
873                )?
874            } else {
875                let x = self.prime_pp2_stage1_enqueue(
876                    e,
877                    rt,
878                    slot,
879                    end - start,
880                    cache1,
881                    seq_end,
882                    fence,
883                    base,
884                    true,
885                )?;
886                let out = {
887                    rt.bind_stage(1)?;
888                    let _st1 = rt.enter(1);
889                    let e1 = rt.engine(1, e);
890                    self.prime_chunk_epilogue(e1, x, end - start, cache1)?
891                };
892                (out, None)
893            };
894
895            rt.publish_to(1, &caller_stream)?;
896            e.copy_into(
897                &mut hiddens,
898                start * n_embd,
899                &out.2,
900                (end - start) * n_embd,
901            )?;
902            last = Some((out.0, out.1));
903            crate::pp::PRIME_SPLIT_CHUNKS
904                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
905
906            if let Some(next) = next_slot {
907                // The caller copy above reads a stage-1 allocation. Before stage 1 of the
908                // next chunk can allocate/reuse blocks, mirror #87's body-entry fence.
909                // Stage 0(N+1) is already queued before this wait is appended, so its
910                // overlap with stage 1(N) is preserved.
911                rt.fence_stages_behind(&caller_stream)?;
912                slot = next;
913            }
914        }
915
916        debug_assert_eq!(cache0.pos, initial_base + t);
917        debug_assert_eq!(cache1.pos, initial_base + t);
918        let (logits, h_seed) = last.unwrap();
919        Ok((logits, h_seed, hiddens))
920    }
921
922    /// One prime chunk: the full layer stack over `tokens`, continuing from the cache's current
923    /// state (`cache.pos` = tokens already primed; 0 = fresh). Positions/RoPE are absolute
924    /// task #21 de-broadcast: the q/k head count PREP must emit so the scan's consumers
925    /// agree. Compact (num_k) ONLY when the scan will take the chunked+mma route AND
926    /// num_k == num_v/2 (the engine-side hint mirrors this exact formula); everything
927    /// else (s128 verify tier, chunked-off, mma-off) keeps the broadcast layout.
928    fn gdn_hk(e: &Engine, t: usize, num_v: usize, num_k: usize) -> usize {
929        if Engine::gdn_db_on()
930            && Engine::gdn_chunked_enabled() && t >= 16
931            && e.gdn_mma_enabled(Engine::gdn_chunk_size())
932            && num_k * 2 == num_v
933        {
934            num_k
935        } else {
936            num_v
937        }
938    }
939
940    /// task #17: gate for the fused fp16-operand epilogues (silu_mul/gated_rmsnorm/sig_mul
941    /// `_f16out` twins). Bit-identical class (the twins emit the cvt kernel's exact halves),
942    /// but seam-gated (MEMRA_F16OUT=0) so a bit-check can arbitrate, and OFF under verify-exact
943    /// (matmul_group skips the f16 lane there; the twins must not resurrect it).
944    fn f16out_on(e: &Engine, t: usize) -> bool {
945        crate::f16_ffi::pp_f16_enabled() && t >= 16 && !e.verify_exact_on()
946            && std::env::var("MEMRA_F16OUT").as_deref() != Ok("0")
947    }
948
949    /// (cache.pos + i). Returns (last-row logits, h_seed, this chunk's hidden stack [T, n_embd]).
950    /// See HybridModel::prime_slabs — the eager prime's resident trunk transients.
951    /// PER-DEVICE since lane/pp-leverb (2026-08-08): the map is keyed by the allocating
952    /// engine's CUDA ordinal — under the prime stage split each stage's range walks through
953    /// its OWN slabs on its own device (a dev0 slab dereferenced by a dev1 kernel would be
954    /// a peer read per GEMM operand, the exact class Lever B removes). Single-device rigs
955    /// see one entry, byte-identical behavior.
956    pub fn prime_slabs_get(
957        &self,
958        e: &Engine,
959        t: usize,
960        n_embd: usize,
961        n_ff_max: usize,
962    ) -> Result<std::sync::Arc<std::sync::Mutex<PrimeSlabs>>, Box<dyn std::error::Error>> {
963        let mut slabs = self.prime_slabs.lock().unwrap();
964        let dev = e.ctx().ordinal();
965        let need_new = match slabs.get(&dev) {
966            None => true,
967            Some(sl) => sl.lock().unwrap().t_cap < t,
968        };
969        if need_new {
970            slabs.insert(dev, std::sync::Arc::new(std::sync::Mutex::new(PrimeSlabs {
971                t_cap: t,
972                h: e.uninit(t * n_embd)?,
973                x1: e.uninit(t * n_embd)?,
974                z: e.uninit(t * n_embd)?,
975                act: e.uninit(t * n_ff_max)?,
976                xa: e.uninit(t * n_embd)?,
977                xb: e.uninit(t * n_embd)?,
978                h16: e.alloc_u8_uninit(t * n_embd * 2)?,
979                z16: e.alloc_u8_uninit(t * n_embd * 2)?,
980                gate: e.uninit(t * n_ff_max)?,
981                up: e.uninit(t * n_ff_max)?,
982                ffn_out: e.uninit(t * n_embd)?,
983                seg_glue: Vec::new(),
984                mixed: e.uninit(t * n_embd)?,
985                seg_mid: Vec::new(),
986                seg_t: 0,
987            })));
988        }
989        Ok(slabs.get(&dev).expect("prime slab inserted").clone())
990    }
991
992    /// `seq_end` = the whole REQUEST's absolute end position (`cache.pos + prompt_len` at
993    /// `prime_cache` entry), NOT this chunk's end. Chunk-size-invariant by construction; step35's
994    /// SWA arm selects on it (see `step35_attn_pre_wo`). Every other arch ignores it.
995    fn prime_chunk(&self, e: &Engine, tokens: &[u32], cache: &mut Cache, seq_end: usize)
996                       -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
997        if crate::pp::pp_host_bounce_active()
998            && (self.cfg.gemma4.is_some() || !crate::pp::prime_pp_on())
999        {
1000            return Err(
1001                "prime_chunk: refused with MEMRA_PP_HOST_BOUNCE=1 because this configuration \
1002                 has no active prime stage split and would peer-read remote weights; keep \
1003                 MEMRA_PRIME_PP enabled and use a PP-prime-supported model"
1004                    .into(),
1005            );
1006        }
1007        // LEVER B (lane/pp-leverb, 2026-08-08): the ppN door for the chunked prime. With the
1008        // door open + per-stage streams, each chunk walks its layer ranges on the OWNING
1009        // stage's engine/device (the anatomy receipt this kills: dev1 ran ZERO prefill
1010        // kernels; stage-1 trunk weights were peer-read = 22% of the pp4096 wall).
1011        // MEMRA_PRIME_PP=0 = the unsplit rollback (also the prime-split-gate's reference
1012        // arm — prime deliberately keeps NO refuse_unsplit_if_remote, see pp.rs). The
1013        // MEMRA_PP_STREAMS=0 seam keeps the unsplit walk too: in that regime the sharded
1014        // loader is off and there is nothing remote to split for.
1015        if self.cfg.gemma4.is_none()
1016            && !crate::pp::pp2_streams_off()
1017            && crate::pp::prime_pp_on()
1018        {
1019            if let Some(fence) = crate::pp::pp_cuts(self.layers.len()) {
1020                return self.prime_chunk_ppn(e, tokens, cache, seq_end, &fence);
1021            }
1022        }
1023        if crate::pp::pp_host_bounce_active() {
1024            return Err(
1025                "prime_chunk: MEMRA_PP_HOST_BOUNCE=1 found no valid prime stage split; \
1026                 refusing an unsplit remote-weight walk"
1027                    .into(),
1028            );
1029        }
1030        let t = tokens.len();
1031        let base = cache.pos;
1032        debug_assert!(seq_end >= base + t, "prime_chunk: seq_end must cover this chunk");
1033        let pos: Vec<i32> = (base as i32..(base + t) as i32).collect();
1034        let pos_d = e.htod_i32(&pos)?;
1035
1036        let x_embed = self.embed(e, tokens)?;   // [T, n_embd]
1037        let x = self.prime_layers(
1038            e, x_embed, 0, self.layers.len(), &pos_d, t, base, cache, seq_end,
1039        )?;
1040        self.prime_chunk_epilogue(e, x, t, cache)
1041    }
1042
1043    /// PRIME RANGE SUBGRAPH (lane/pp-leverb, 2026-08-08): layers `[lo, hi)` of the chunked
1044    /// prime walk — `prime_chunk`'s trunk loop extracted verbatim to the
1045    /// `decode_layers_eager(lo, hi)` / `verify_layers(lo, hi)` contract: enters with a
1046    /// MATERIALIZED `[T, n_embd]` residual, exits with the range's final residual
1047    /// materialized (cloned out of the slab). At `lo=0, hi=n_layers` — the unsplit call —
1048    /// the launch sequence is byte-identical to the pre-extraction body. Range semantics:
1049    ///   - the cross-layer [down-add + NEXT attn-norm] fusion is range-LOCAL (`il + 1 < hi`):
1050    ///     layer `hi`'s attn_norm belongs to the NEXT stage's device, so the range ends with
1051    ///     the plain add (materialize) and the next stage hoists its own first norm — the
1052    ///     kernel-check-pinned `add_rms_norm == add then rms_norm` identity, the same law
1053    ///     the decode split rests on (`prime-split-gate` arbitrates end-to-end);
1054    ///   - prime slabs are PER-DEVICE (`prime_slabs_get` keys on the engine ordinal), so
1055    ///     each stage walks through its own resident transients;
1056    ///   - the S-glue/S-mid capture path requires the FULL range (its lookahead fuses
1057    ///     `self.layers[il+1]` unconditionally) — `use_seg` gains `lo == 0 && hi == n_layers`.
1058    #[allow(clippy::too_many_arguments)]
1059    fn prime_layers(&self, e: &Engine, x_in: CudaSlice<f32>, lo: usize, hi: usize,
1060                    pos_d: &CudaSlice<i32>, t: usize, base: usize, cache: &mut Cache,
1061                    seq_end: usize)
1062                    -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1063        let cfg = &self.cfg;
1064        let n_embd = cfg.n_embd as usize;
1065        let eps = cfg.rms_eps;
1066        // task #14: fuse the fp16 GEMM-operand emission into the trunk norms (kills the
1067        // standalone convert launches). Only when the f16 lane serves and T reaches the
1068        // GEMM tier; bit-identical either way.
1069        let f16fuse = crate::f16_ffi::pp_f16_enabled() && t >= 16;
1070        // PRIME SLABS (piecewise foundation): trunk transients in resident buffers —
1071        // ~224 fewer alloc/free calls per prime and FROZEN Lt operand addresses
1072        // (nvjet's alignment-variant selection becomes run-to-run stable). Every slab is
1073        // fully overwritten before use; x ping-pongs xa<->xb; the hidden-stack return
1074        // clones the final x (the slab cannot leave). Non-slab fallback: MEMRA_PRIME_SLABS=0.
1075        let n_ff_max = self.layers.iter().map(|l| match &l.ffn {
1076            crate::hybrid::Ffn::Dense { ffn_gate, .. } => ffn_gate.out_features(),
1077            _ => n_embd,
1078        }).max().unwrap_or(n_embd).max(n_embd);
1079        let use_slabs = std::env::var("MEMRA_PRIME_SLABS").as_deref() != Ok("0");
1080        let slab = if use_slabs {
1081            Some(self.prime_slabs_get(e, t, n_embd, n_ff_max)?)
1082        } else {
1083            None
1084        };
1085        let mut slab_guard = slab.as_ref().map(|sl| sl.lock().unwrap());
1086        let mut x_own;   // fallback storage when slabs are off
1087        type SlabRefs<'a> = (&'a mut CudaSlice<f32>, &'a mut CudaSlice<f32>, &'a mut CudaSlice<f32>, &'a mut CudaSlice<f32>, &'a mut CudaSlice<u8>, &'a mut CudaSlice<u8>, &'a mut CudaSlice<f32>, &'a mut CudaSlice<f32>, &'a mut CudaSlice<f32>);
1088        let (mut x_cur, mut x_nxt, sl): (&mut CudaSlice<f32>, &mut CudaSlice<f32>, Option<SlabRefs>);
1089        let mut seg: Option<(&mut Vec<Option<cudarc::driver::CudaGraph>>, &mut Vec<Option<cudarc::driver::CudaGraph>>, &mut CudaSlice<f32>, &mut usize)> = None;
1090        let mut x_own2;
1091        match slab_guard.as_mut() {
1092            Some(g) => {
1093                let slabs = &mut **g;
1094                e.copy_into(&mut slabs.xa, 0, &x_in, t * n_embd)?;
1095                let PrimeSlabs { xa, xb, h, x1, z, act, h16, z16, gate, up, ffn_out, seg_glue, mixed, seg_mid, seg_t, .. } = slabs;
1096                x_cur = xa;
1097                x_nxt = xb;
1098                seg = Some((seg_glue, seg_mid, mixed, seg_t));
1099                sl = Some((h, x1, z, act, h16, z16, gate, up, ffn_out));
1100            }
1101            None => {
1102                x_own = x_in;
1103                x_own2 = e.uninit(t * n_embd)?;
1104                x_cur = &mut x_own;
1105                x_nxt = &mut x_own2;
1106                sl = None;
1107            }
1108        }
1109        let mut alloc_h; let mut alloc_x1; let mut alloc_z; let mut alloc_act;
1110        let mut alloc_h16; let mut alloc_z16;
1111        let mut alloc_gate; let mut alloc_up; let mut alloc_fo;
1112        let (h, x1, z, act): (&mut CudaSlice<f32>, &mut CudaSlice<f32>, &mut CudaSlice<f32>, &mut CudaSlice<f32>);
1113        let (h16, z16): (&mut CudaSlice<u8>, &mut CudaSlice<u8>);
1114        let (sl_gate, sl_up, sl_fo): (&mut CudaSlice<f32>, &mut CudaSlice<f32>, &mut CudaSlice<f32>);
1115        match sl {
1116            Some((a, b, c, d, e16, f16b, g, u, fo)) => {
1117                h = a; x1 = b; z = c; act = d; h16 = e16; z16 = f16b;
1118                sl_gate = g; sl_up = u; sl_fo = fo;
1119            }
1120            None => {
1121                alloc_h = e.uninit(t * n_embd)?;
1122                alloc_x1 = e.uninit(t * n_embd)?;
1123                alloc_z = e.uninit(t * n_embd)?;
1124                alloc_act = e.uninit(t * n_ff_max)?;
1125                alloc_h16 = e.alloc_u8_uninit(t * n_embd * 2)?;
1126                alloc_z16 = e.alloc_u8_uninit(t * n_embd * 2)?;
1127                alloc_gate = e.uninit(t * n_ff_max)?;
1128                alloc_up = e.uninit(t * n_ff_max)?;
1129                alloc_fo = e.uninit(t * n_embd)?;
1130                h = &mut alloc_h; x1 = &mut alloc_x1; z = &mut alloc_z; act = &mut alloc_act;
1131                h16 = &mut alloc_h16; z16 = &mut alloc_z16;
1132                sl_gate = &mut alloc_gate; sl_up = &mut alloc_up; sl_fo = &mut alloc_fo;
1133            }
1134        }
1135        // piecewise increment 3: layer-0 norm hoisted; the per-layer tail fuses
1136        // [down-add + NEXT layer's attn-norm] into one captured S-glue segment
1137        // (all-slab IO, zero in-graph allocations). Capture happens lazily on the
1138        // first prime at this t (capture does not execute -> launch right after).
1139        let n_layers = self.layers.len();
1140        // OPT-IN (2026-07-26 interleaved verdict): 2-kernel segments measured NET
1141        // -0.7%-to-neutral under the interleaved A/B protocol — one cuGraphLaunch costs
1142        // about what two kernel submissions do. The earlier "+0.9%" was cross-run clock
1143        // drift (the repo's interleaved-A/B law exists for exactly this). Larger segments
1144        // (S-prep/S-attn, 7-9 kernels) remain the open hypothesis; the core-split
1145        // machinery stays (byte-identical) as their foundation.
1146        // step35 is excluded: the core-split path calls `full_attn_prime_core_inner`, which is
1147        // the GENERIC attn core (uniform n_head, rope_dim_count, no window, no head-wise gate).
1148        // step35 rides its own mixer through the normal per-layer arm below.
1149        let use_seg = f16fuse && seg.is_some() && self.cfg.step35.is_none()
1150            && lo == 0 && hi == n_layers
1151            && std::env::var("MEMRA_PRIME_SEG").as_deref() == Ok("1");
1152        if let Some((sg, sm, _, st)) = seg.as_mut() {
1153            if **st != t {
1154                sg.clear();
1155                sg.extend((0..n_layers).map(|_| None));
1156                sm.clear();
1157                sm.extend((0..n_layers).map(|_| None));
1158                **st = t;
1159            }
1160        }
1161        {
1162            let layer_lo = &self.layers[lo];
1163            if f16fuse {
1164                e.rms_norm_f16out(x_cur, layer_lo.attn_norm.float_data(), h, h16, n_embd, t, eps)?;
1165            } else {
1166                e.rms_norm(x_cur, layer_lo.attn_norm.float_data(), h, n_embd, t, eps)?;
1167            }
1168        }
1169        for il in lo..hi {
1170            let layer = &self.layers[il];
1171            let hx16 = if f16fuse { Some(&*h16) } else { None };
1172            if use_seg {
1173                // core-split path: projections -> _inner core -> out-GEMM INTO the mixed
1174                // slab (no copies) -> S-mid segment [add + post-norm] as one graph launch.
1175                let (pre, pre16, w_out) = match &layer.mixer {
1176                    Mixer::Full(fa) => {
1177                        let g3 = match hx16 {
1178                            Some(xh) => e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv], h, xh, t)?,
1179                            None => e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?,
1180                        };
1181                        let (pre, pre16) = self.full_attn_prime_core_inner(e, fa, g3, &pos_d, t, cache, il)?;
1182                        (pre, pre16, &fa.wo)
1183                    }
1184                    Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
1185                    Mixer::Linear(la) => {
1186                        let ws = [&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha];
1187                        let g4 = match hx16 {
1188                            Some(xh) => e.matmul_group_xh(&ws, h, xh, t)?,
1189                            None => e.matmul_group(&ws, h, t)?,
1190                        };
1191                        let (pre, pre16) = self.linear_attn_prime_core_pad_inner(e, la, g4, t, cache, il, None)?;
1192                        (pre, pre16, &la.ssm_out)
1193                    }
1194                };
1195                {
1196                    let (_, sm, mslab, _) = seg.as_mut().unwrap();
1197                    let pre_n = pre.len() / t;
1198                    let xh_pre = match pre16 {
1199                        Some(x) => x,
1200                        None => e.f16_act(&pre, t * pre_n, pre_n)?,
1201                    };
1202                    if !e.try_f16_gemm_pre_into(w_out, &xh_pre, t, mslab)? {
1203                        let y = e.matmul(w_out, &pre, t)?;
1204                        e.copy_into(mslab, 0, &y, t * n_embd)?;
1205                    }
1206                    if sm[il].is_none() {
1207                        use cudarc::driver::sys::{CUgraphInstantiate_flags, CUstreamCaptureMode};
1208                        let w_post = layer.post_attn_norm.float_data();
1209                        e.stream().synchronize()?;
1210                        e.stream().begin_capture(CUstreamCaptureMode::CU_STREAM_CAPTURE_MODE_RELAXED)?;
1211                        let r = (|| -> Result<(), Box<dyn std::error::Error>> {
1212                            e.add(x_cur, mslab, x1, t * n_embd)?;
1213                            e.rms_norm_f16out(x1, w_post, z, z16, n_embd, t, eps)?;
1214                            Ok(())
1215                        })();
1216                        let g = e.stream().end_capture(
1217                            CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH);
1218                        r?;
1219                        sm[il] = Some(g?.ok_or("S-mid capture produced no graph")?);
1220                    }
1221                    sm[il].as_ref().unwrap().launch()?;
1222                }
1223            } else {
1224                let mixed = match &layer.mixer {
1225                    Mixer::Full(fa) => self.full_attn_prime(e, fa, h, hx16, &pos_d, t, cache, il,
1226                                                            seq_end)?,
1227                    Mixer::Linear(la) => self.linear_attn_prime(e, la, h, hx16, t, cache, il)?,
1228                    Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
1229                };
1230                if f16fuse {
1231                    // round 28: residual+norm in ONE kernel (add_rms_norm precedent,
1232                    // bit-identical) — the standalone add pass disappears.
1233                    e.add_rms_norm_f16out(x_cur, &mixed, layer.post_attn_norm.float_data(),
1234                                          x1, z, z16, n_embd, t, eps)?;
1235                } else {
1236                    e.add(x_cur, &mixed, x1, t * n_embd)?;
1237                    e.rms_norm(x1, layer.post_attn_norm.float_data(), z, n_embd, t, eps)?;
1238                }
1239            }
1240            let zx16 = if f16fuse { Some(&*z16) } else { None };
1241            match &layer.ffn {
1242                crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } => {
1243                    let n_ff = ffn_gate.out_features();
1244                    // gate/up INTO boundary slabs (piecewise increment 2); fall back to
1245                    // the allocating group + copy when a mirror is missing.
1246                    let mut into_ok = false;
1247                    if let Some(xh) = zx16 {
1248                        into_ok = e.try_f16_gemm_pre_into(ffn_gate, xh, t, sl_gate)?
1249                            && e.try_f16_gemm_pre_into(ffn_up, xh, t, sl_up)?;
1250                    }
1251                    if !into_ok {
1252                        let mut g2 = match zx16 {
1253                            Some(xh) => e.matmul_group_xh(&[ffn_gate, ffn_up], z, xh, t)?,
1254                            None => e.matmul_group(&[ffn_gate, ffn_up], z, t)?,
1255                        };
1256                        let up_y = g2.pop().unwrap();
1257                        let gate_y = g2.pop().unwrap();
1258                        e.copy_into(sl_gate, 0, &gate_y, t * n_ff)?;
1259                        e.copy_into(sl_up, 0, &up_y, t * n_ff)?;
1260                    }
1261                    // task #17: the silu arm's f16out twin emits the down GEMM's fp16
1262                    // operand in-epilogue; non-silu activations keep the standalone convert.
1263                    // silu_mul_f16out is PLAIN silu(gate)*up — a clamped layer (step35 dense
1264                    // blocks under a live swiglu_clamp_shexp) must take the ffn_act_lim arm.
1265                    let d_lim = self.cfg.clamp_shexp_at(il as u32);
1266                    let act16 = if Self::f16out_on(e, t) && self.cfg.m3.is_none()
1267                        && d_lim.is_none() {
1268                        let mut a16 = e.alloc_u8_uninit(t * n_ff * 2)?;
1269                        e.silu_mul_f16out(sl_gate, sl_up, act, &mut a16, t * n_ff)?;
1270                        Some(a16)
1271                    } else {
1272                        Self::ffn_act_lim(e, &self.cfg, sl_gate, sl_up, 1.0, 1.0, d_lim,
1273                                          act, t * n_ff)?;
1274                        None
1275                    };
1276                    // down GEMM into the ffn_out slab (f16 arm; fallback copies)
1277                    let xh_act = match act16 {
1278                        Some(x) => x,
1279                        None => e.f16_act(act, t * n_ff, n_ff)?,
1280                    };
1281                    if !e.try_f16_gemm_pre_into(ffn_down, &xh_act, t, sl_fo)? {
1282                        let y = e.matmul(ffn_down, &*act, t)?;
1283                        e.copy_into(sl_fo, 0, &y, t * n_embd)?;
1284                    }
1285                }
1286                crate::hybrid::Ffn::Moe(m) => {
1287                    let y = self.moe_ffn_il_prefill(e, m, z, t, il as u16)?;
1288                    e.copy_into(sl_fo, 0, &y, t * n_embd)?;
1289                }
1290            }
1291            if use_seg && il + 1 < hi {
1292                // S-glue segment: [add + next attn-norm(+f16out)] — one cuGraphLaunch
1293                let w_next = self.layers[il + 1].attn_norm.float_data();
1294                let (sg, _, _, _) = seg.as_mut().unwrap();
1295                if sg[il].is_none() {
1296                    use cudarc::driver::sys::{CUgraphInstantiate_flags, CUstreamCaptureMode};
1297                    e.stream().synchronize()?;
1298                    e.stream().begin_capture(CUstreamCaptureMode::CU_STREAM_CAPTURE_MODE_RELAXED)?;
1299                    let r = (|| -> Result<(), Box<dyn std::error::Error>> {
1300                        e.add(x1, sl_fo, x_nxt, t * n_embd)?;
1301                        e.rms_norm_f16out(x_nxt, w_next, h, h16, n_embd, t, eps)?;
1302                        Ok(())
1303                    })();
1304                    let g = e.stream().end_capture(
1305                        CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH);
1306                    r?;
1307                    sg[il] = Some(g?.ok_or("S-glue capture produced no graph")?);
1308                }
1309                sg[il].as_ref().unwrap().launch()?;
1310            } else {
1311                if il + 1 < hi {
1312                    let w_next = self.layers[il + 1].attn_norm.float_data();
1313                    if f16fuse {
1314                        e.add_rms_norm_f16out(x1, sl_fo, w_next, x_nxt, h, h16, n_embd, t, eps)?;
1315                    } else {
1316                        e.add(x1, sl_fo, x_nxt, t * n_embd)?;
1317                        e.rms_norm(x_nxt, w_next, h, n_embd, t, eps)?;
1318                    }
1319                } else {
1320                    e.add(x1, sl_fo, x_nxt, t * n_embd)?;
1321                }
1322            }
1323            // CHUNK-INVARIANCE BISECT SEAM (lane/chunk-invariance, 2026-08-05): dump the
1324            // per-layer post-residual hidden for the LAST row of this chunk, keyed by
1325            // absolute position, so two runs at different MEMRA_PRIME_CHUNK can be diffed
1326            // layer-by-layer to find the FIRST diverging layer. Diagnostic only —
1327            // unset (the default) costs one OnceLock read per layer.
1328            if let Some(path) = Self::prime_trace_path() {
1329                let row = (base + t - 1) as usize;
1330                let host = e.dtoh(x_nxt)?;
1331                let last = &host[(t - 1) * n_embd..t * n_embd];
1332                use std::io::Write as _;
1333                let mut f = std::fs::OpenOptions::new().create(true).append(true).open(path)?;
1334                let mut h64: u64 = 0xcbf29ce484222325;
1335                for v in last {
1336                    h64 ^= v.to_bits() as u64;
1337                    h64 = h64.wrapping_mul(0x100000001b3);
1338                }
1339                writeln!(f, "{{\"pos\":{row},\"layer\":{il},\"t\":{t},\"base\":{base},\
1340                             \"hash\":\"{h64:016x}\",\"v0\":{:.9e},\"v1\":{:.9e},\"v2\":{:.9e}}}",
1341                         last[0], last[1], last[2])?;
1342            }
1343            std::mem::swap(&mut x_cur, &mut x_nxt);
1344        }
1345        // hidden-stack return: clone the final x out of the slab
1346        let mut x = e.uninit(t * n_embd)?;
1347        e.copy_into(&mut x, 0, x_cur, t * n_embd)?;
1348        drop(slab_guard);
1349        Ok(x)
1350    }
1351
1352    /// The prime chunk's tail — h_seed + output_norm + one-row lm head + cache.pos advance —
1353    /// shared verbatim by the unsplit walk and the last stage of the ppN walk (`e` = the
1354    /// engine that produced `x`, i.e. the last stage's under the split; output_norm/output
1355    /// were loaded through that engine by the sharded loader, hybrid.rs `e_head`).
1356    fn prime_chunk_epilogue(&self, e: &Engine, x: CudaSlice<f32>, t: usize, cache: &mut Cache)
1357                            -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
1358        let n_embd = self.cfg.n_embd as usize;
1359        let eps = self.cfg.rms_eps;
1360        // h_seed = LAST row of x BEFORE output_norm (MTP-PLAN §A default) or AFTER it
1361        // (MEMRA_SPEC_HPOST — reference convention; hn is computed just below either way, so
1362        // the post-norm copy happens after hn exists).
1363        let mut h_seed = e.uninit(n_embd)?;
1364        if !crate::spec::spec_hpost() {
1365            e.copy_view_into(&mut h_seed, 0, &x.slice((t - 1) * n_embd..t * n_embd), n_embd)?;
1366        }
1367        // last-row logits, exactly like forward_last (norm all T — per-row op — then lm_head on 1 row).
1368        let mut hn = e.uninit(t * n_embd)?;
1369        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
1370        if crate::spec::spec_hpost() {
1371            e.copy_view_into(&mut h_seed, 0, &hn.slice((t - 1) * n_embd..t * n_embd), n_embd)?;
1372        }
1373        let last = e.view(&hn, t * n_embd);
1374        let last_row = last.slice((t - 1) * n_embd..t * n_embd);
1375        let mut hlast = e.uninit(n_embd)?;
1376        e.copy_view_into(&mut hlast, 0, &last_row, n_embd)?;
1377        let logits = e.matmul(&self.output, &hlast, 1)?;
1378        cache.pos += t;
1379        // Hidden stack handed to generate_spec as prompt_h: pre-norm x (default) or the full
1380        // post-norm stack hn (MEMRA_SPEC_HPOST).
1381        Ok((e.dtoh(&logits)?, h_seed, if crate::spec::spec_hpost() { hn } else { x }))
1382    }
1383
1384    /// THE PRIME STAGE SPLIT (lane/pp-leverb, 2026-08-08): one prime chunk as N stage
1385    /// subgraphs, each range on ITS OWN engine/stream/device, with the `[T, n_embd]`
1386    /// boundary handoff at every fence cut — the prime twin of `decode_step_h_ppn` /
1387    /// `decode_step_t_core_ppn`, and the kill for the anatomy's two receipts: stage-1 trunk
1388    /// weights stop being peer-read (22% of the pp4096 wall) and dev1 stops running zero
1389    /// prefill kernels. Structure mirrors the verify split exactly:
1390    ///   stage 0        `rt.enter(0)` → per-stage pos_d + embed (the table lives with
1391    ///                  stage 0) → `prime_layers(fence[0], fence[1])` → `rt.tx`
1392    ///   middle stages  `rt.rx` → per-stage pos_d → range → `rt.tx`
1393    ///   last stage     `rt.rx` → range → the shared epilogue (output_norm + head live
1394    ///                  there via the sharded loader) → `publish_to`
1395    /// Laws inherited (not relearned): `fence_stages_behind` at entry (#87 — a previous
1396    /// round's stage-freed buffers must not be reused under the caller's queued reads);
1397    /// per-stage `pos_d` (allocated/consumed/freed on one stream); per-stage Engines from
1398    /// `PpNRt` (shared-scratch race); EXIT PUBLICATION for the device-resident returns
1399    /// (h_seed + hidden stack live on the last stage — the caller's stream must wait).
1400    /// KV/MoE locality falls out: each range's KV appends run on the owning stage
1401    /// (`pp::new_cache` placed the buffers there), each stage engine owns its own SLRU
1402    /// pool (per-Engine `moe_cache`, sized on ITS device — the SGLang #33666 law), and the
1403    /// slab-local MoE arm's `DevExps.dev` gate now matches on stage-1 layers too.
1404    /// EXACTNESS: the split adds zero deviation by construction (same kernels, same bytes,
1405    /// boundary = straight f32 copy); `prime-split-gate` (ppsplit) arbitrates bit-for-bit
1406    /// and its liveness counter is bumped here — the gate goes green with this function.
1407    fn prime_chunk_ppn(&self, e: &Engine, tokens: &[u32], cache: &mut Cache, seq_end: usize,
1408                       fence: &[usize])
1409                       -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
1410        let rt = crate::pp::PpNRt::get(e)?;
1411        let n_st = fence.len() - 1;
1412        assert_eq!(
1413            rt.n_stages(), n_st,
1414            "PpNRt stage count {} != fence stages {n_st}", rt.n_stages()
1415        );
1416        let n_embd = self.cfg.n_embd as usize;
1417        let t = tokens.len();
1418        let base = cache.pos;
1419        debug_assert!(seq_end >= base + t, "prime_chunk_ppn: seq_end must cover this chunk");
1420        let payload = t * n_embd;
1421        // The caller's ambient stream, captured BEFORE any enter() pushes a stage stream
1422        // (inside a stage scope e.stream() IS the stage stream and the exit wait would
1423        // self-order into a no-op) — the decode_step_t_core_ppn pattern.
1424        let caller_stream = e.stream();
1425        rt.fence_stages_behind(&caller_stream)?;
1426
1427        if n_st == 2 {
1428            let slot = self.prime_pp2_stage0_enqueue(
1429                e, rt, tokens, cache, seq_end, fence, base, false,
1430            )?;
1431            let x = self.prime_pp2_stage1_enqueue(
1432                e, rt, slot, t, cache, seq_end, fence, base, false,
1433            )?;
1434            let out = {
1435                rt.bind_stage(1)?;
1436                let _st1 = rt.enter(1);
1437                let e1 = rt.engine(1, e);
1438                self.prime_chunk_epilogue(e1, x, t, cache)?
1439            };
1440            rt.publish_to(1, &caller_stream)?;
1441            crate::pp::PRIME_SPLIT_CHUNKS
1442                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1443            return Ok(out);
1444        }
1445
1446        let pos: Vec<i32> = (base as i32..(base + t) as i32).collect();
1447
1448        // ---- STAGE 0: embed + layers [0, fence[1]) + boundary-0 TX ----
1449        let mut slot = {
1450            let _st0 = rt.enter(0);
1451            let e0 = rt.engine(0, e);
1452            let pos_d = e0.htod_i32(&pos)?;
1453            let x = self.embed(e0, tokens)?;
1454            let x = self.prime_layers(
1455                e0, x, fence[0], fence[1], &pos_d, t, base, cache, seq_end,
1456            )?;
1457            rt.tx(0, &x, payload)?
1458            // x + pos_d drop here: freed stream-ordered on stage-0's stream after use.
1459        };
1460
1461        // ---- MIDDLE STAGES: RX boundary s-1 -> range -> TX boundary s ----
1462        for s in 1..n_st - 1 {
1463            let _st = rt.enter(s);
1464            let es = rt.engine(s, e);
1465            let pos_d = es.htod_i32(&pos)?;
1466            let x = rt.rx(s - 1, slot, payload)?;
1467            let x = self.prime_layers(
1468                es, x, fence[s], fence[s + 1], &pos_d, t, base, cache, seq_end,
1469            )?;
1470            slot = rt.tx(s, &x, payload)?;
1471        }
1472
1473        // ---- LAST STAGE: RX + final range + the shared epilogue ----
1474        let _stl = rt.enter(n_st - 1);
1475        let el = rt.engine(n_st - 1, e);
1476        let pos_d = el.htod_i32(&pos)?;
1477        let x = rt.rx(n_st - 2, slot, payload)?;
1478        let x = self.prime_layers(
1479            el, x, fence[n_st - 1], fence[n_st], &pos_d, t, base, cache, seq_end,
1480        )?;
1481        let out = self.prime_chunk_epilogue(el, x, t, cache)?;
1482        // EXIT PUBLICATION: h_seed + the hidden stack are device-resident on the last
1483        // stage's stream; the caller resumes on its own stream (chunk-loop copy_into /
1484        // generate_spec's prompt_h consumer). The logits dtoh above already drained the
1485        // stage stream host-side, but the law is stated in events, not in a dtoh side
1486        // effect a later deferred form would remove.
1487        rt.publish_to(n_st - 1, &caller_stream)?;
1488        crate::pp::PRIME_SPLIT_CHUNKS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1489        Ok(out)
1490    }
1491
1492    fn prime_pp2_stage0_enqueue(
1493        &self,
1494        e: &Engine,
1495        rt: &crate::pp::PpNRt,
1496        tokens: &[u32],
1497        cache: &mut Cache,
1498        seq_end: usize,
1499        fence: &[usize],
1500        base: usize,
1501        pipelined: bool,
1502    ) -> Result<usize, Box<dyn std::error::Error>> {
1503        let t = tokens.len();
1504        let n_embd = self.cfg.n_embd as usize;
1505        let pos: Vec<i32> = (base as i32..(base + t) as i32).collect();
1506        rt.bind_stage(0)?;
1507        let _st0 = rt.enter(0);
1508        let e0 = rt.engine(0, e);
1509        let pos_d = e0.htod_i32(&pos)?;
1510        let x = self.embed(e0, tokens)?;
1511        let _overlap = pipelined.then(crate::pp::enter_prime_pipe_stage);
1512        let x = self.prime_layers(
1513            e0, x, fence[0], fence[1], &pos_d, t, base, cache, seq_end,
1514        )?;
1515        if pipelined {
1516            rt.tx_pipelined(0, &x, t * n_embd)
1517        } else {
1518            rt.tx(0, &x, t * n_embd)
1519        }
1520    }
1521
1522    fn prime_pp2_stage1_enqueue(
1523        &self,
1524        e: &Engine,
1525        rt: &crate::pp::PpNRt,
1526        slot: usize,
1527        t: usize,
1528        cache: &mut Cache,
1529        seq_end: usize,
1530        fence: &[usize],
1531        base: usize,
1532        pipelined: bool,
1533    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1534        let n_embd = self.cfg.n_embd as usize;
1535        let pos: Vec<i32> = (base as i32..(base + t) as i32).collect();
1536        rt.bind_stage(1)?;
1537        let _st1 = rt.enter(1);
1538        let e1 = rt.engine(1, e);
1539        let pos_d = e1.htod_i32(&pos)?;
1540        let x = rt.rx(0, slot, t * n_embd)?;
1541        let _overlap = pipelined.then(crate::pp::enter_prime_pipe_stage);
1542        self.prime_layers(
1543            e1, x, fence[1], fence[2], &pos_d, t, base, cache, seq_end,
1544        )
1545    }
1546
1547    /// CAPTURE-SAFE prime trunk (task #14 increment 1, ARCHITECTURE-H100.md design v2):
1548    /// prime_chunk's layer stack with every capture hazard hoisted — `x` is the
1549    /// PRE-EMBEDDED input in a STABLE graph-input buffer (embed + its 8MB htod stay
1550    /// eager, one launch), `pos_d` is a baked device param (fresh prime = 0..T, constant
1551    /// per bucket), logits/h_seed/hidden stay DEVICE-resident (no dtoh), and cache.pos is
1552    /// NOT advanced (host state — the replay wrapper owns it). Body mirrors prime_chunk
1553    /// (the prime-graph-gate pins them together). KNOWN smoke-scope gap: append host-len
1554    /// bookkeeping still runs on the host per call — the real replay path moves the write
1555    /// slot to the len_d device counter (increment 3).
1556    /// GRAPH-OUTPUT CONTRACT: results are COPIED into caller-provided stable buffers
1557    /// (`logits_out` [n_vocab], `h_seed_out` [n_embd]) — every internal allocation drops
1558    /// INSIDE the capture region (alloc+free node pairs). Retaining an in-capture
1559    /// allocation across end_capture makes instantiate throw INVALID_VALUE (smoke finding
1560    /// 2026-07-26), and under AUTO_FREE_ON_LAUNCH its address wouldn't survive a launch
1561    /// anyway — the decode GraphSession's pre-allocated-output pattern is the law here.
1562    pub fn prime_chunk_captured(&self, e: &Engine, x_in: &CudaSlice<f32>, pos_d: &CudaSlice<i32>,
1563                                t: usize, cache: &mut Cache,
1564                                len_d: &CudaSlice<i32>,
1565                                logits_out: &mut CudaSlice<f32>, h_seed_out: &mut CudaSlice<f32>)
1566                                -> Result<(), Box<dyn std::error::Error>> {
1567        let cfg = &self.cfg;
1568        let n_embd = cfg.n_embd as usize;
1569        let eps = cfg.rms_eps;
1570        let f16fuse = crate::f16_ffi::pp_f16_enabled() && t >= 16;
1571        let mut x = e.uninit(t * n_embd)?;
1572        e.copy_into(&mut x, 0, x_in, t * n_embd)?;
1573        for (il, layer) in self.layers.iter().enumerate() {
1574            let mut h = e.uninit(t * n_embd)?;
1575            let mut hx16: Option<CudaSlice<u8>> = None;
1576            if f16fuse {
1577                let mut b16 = e.alloc_u8_uninit(t * n_embd * 2)?;
1578                e.rms_norm_f16out(&x, layer.attn_norm.float_data(), &mut h, &mut b16, n_embd, t, eps)?;
1579                hx16 = Some(b16);
1580            } else {
1581                e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
1582            }
1583            let mixed = match &layer.mixer {
1584                // The captured prime is ONE unchunked bucket over a FRESH cache (pos == 0), so the
1585                // request ends at t. If bucketed capture ever composes with chunking, seq_end must
1586                // come from the caller (see step35_attn_pre_wo's doc note).
1587                Mixer::Full(fa) => self.full_attn_prime(e, fa, &h, hx16.as_ref(), pos_d, t, cache,
1588                                                        il, t)?,
1589                Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
1590                Mixer::Linear(la) => {
1591                    let ws = [&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha];
1592                    let g4 = match hx16.as_ref() {
1593                        Some(xh) => e.matmul_group_xh(&ws, &h, xh, t)?,
1594                        None => e.matmul_group(&ws, &h, t)?,
1595                    };
1596                    self.linear_attn_prime_core_pad(e, la, g4, t, cache, il, Some(len_d))?
1597                }
1598            };
1599            let mut x1 = e.uninit(t * n_embd)?;
1600            e.add(&x, &mixed, &mut x1, t * n_embd)?;
1601            let mut z = e.uninit(t * n_embd)?;
1602            let mut zx16: Option<CudaSlice<u8>> = None;
1603            if f16fuse {
1604                let mut b16 = e.alloc_u8_uninit(t * n_embd * 2)?;
1605                e.rms_norm_f16out(&x1, layer.post_attn_norm.float_data(), &mut z, &mut b16, n_embd, t, eps)?;
1606                zx16 = Some(b16);
1607            } else {
1608                e.rms_norm(&x1, layer.post_attn_norm.float_data(), &mut z, n_embd, t, eps)?;
1609            }
1610            let ffn_out = match &layer.ffn {
1611                crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } => {
1612                    let n_ff = ffn_gate.out_features();
1613                    let mut g2 = match &zx16 {
1614                        Some(xh) => e.matmul_group_xh(&[ffn_gate, ffn_up], &z, xh, t)?,
1615                        None => e.matmul_group(&[ffn_gate, ffn_up], &z, t)?,
1616                    };
1617                    let up = g2.pop().unwrap();
1618                    let gate = g2.pop().unwrap();
1619                    let mut act = e.uninit(t * n_ff)?;
1620                    // dense FFN keys off the SHEXP clamp array — see forward()'s note.
1621                    Self::ffn_act_lim(e, &self.cfg, &gate, &up, 1.0, 1.0,
1622                                      self.cfg.clamp_shexp_at(il as u32), &mut act, t * n_ff)?;
1623                    e.matmul(ffn_down, &act, t)?
1624                }
1625                crate::hybrid::Ffn::Moe(m) => self.moe_ffn_il_prefill(e, m, &z, t, il as u16)?,
1626            };
1627            let mut x2 = e.uninit(t * n_embd)?;
1628            e.add(&x1, &ffn_out, &mut x2, t * n_embd)?;
1629            x = x2;
1630        }
1631        // device-indexed TRUE last row (pads sit past it in a bucketed graph)
1632        if !crate::spec::spec_hpost() {
1633            e.row_gather_dev(&x, h_seed_out, len_d, n_embd)?;
1634        }
1635        let mut hn = e.uninit(t * n_embd)?;
1636        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
1637        if crate::spec::spec_hpost() {
1638            e.row_gather_dev(&hn, h_seed_out, len_d, n_embd)?;
1639        }
1640        let mut hlast = e.uninit(n_embd)?;
1641        e.row_gather_dev(&hn, &mut hlast, len_d, n_embd)?;
1642        let logits = e.matmul(&self.output, &hlast, 1)?;
1643        let nv = logits.len();
1644        e.copy_into(logits_out, 0, &logits, nv)?;
1645        Ok(())
1646    }
1647
1648    fn step35_prime_batch_on() -> bool {
1649        std::env::var("MEMRA_STEP35_PRIME_BATCH").as_deref() != Ok("0")
1650    }
1651
1652    /// Step35 cross-request prime range: weight-streaming work runs once at `m=sum(T)`;
1653    /// sequence-scoped attention/KV work stays on each request's own cache and positions.
1654    #[allow(clippy::too_many_arguments)]
1655    fn step35_prime_batch_layers(
1656        &self,
1657        e: &Engine,
1658        mut x: CudaSlice<f32>,
1659        lo: usize,
1660        hi: usize,
1661        ts: &[usize],
1662        offs: &[usize],
1663        pos_ds: &[CudaSlice<i32>],
1664        caches: &mut [&mut Cache],
1665    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1666        let cfg = &self.cfg;
1667        let n_embd = cfg.n_embd as usize;
1668        let eps = cfg.rms_eps;
1669        let b = ts.len();
1670        let total: usize = ts.iter().sum();
1671        let f16fuse = crate::f16_ffi::pp_f16_enabled() && total >= 16;
1672
1673        let split = |e: &Engine, y: &CudaSlice<f32>, dim: usize|
1674                     -> Result<Vec<CudaSlice<f32>>, Box<dyn std::error::Error>> {
1675            let mut out = Vec::with_capacity(b);
1676            for s in 0..b {
1677                let mut ys = e.uninit(ts[s] * dim)?;
1678                e.copy_view_into(
1679                    &mut ys,
1680                    0,
1681                    &y.slice(offs[s] * dim..(offs[s] + ts[s]) * dim),
1682                    ts[s] * dim,
1683                )?;
1684                out.push(ys);
1685            }
1686            Ok(out)
1687        };
1688
1689        for il in lo..hi {
1690            let layer = &self.layers[il];
1691            let Mixer::Full(fa) = &layer.mixer else {
1692                return Err(format!("step35 layer {il} is not full-attn — corrupt config").into());
1693            };
1694
1695            let mut h = e.uninit(total * n_embd)?;
1696            let mut hx16 = e.alloc_u8_uninit(total * n_embd * 2)?;
1697            if f16fuse {
1698                e.rms_norm_f16out(
1699                    &x,
1700                    layer.attn_norm.float_data(),
1701                    &mut h,
1702                    &mut hx16,
1703                    n_embd,
1704                    total,
1705                    eps,
1706                )?;
1707            } else {
1708                e.rms_norm(
1709                    &x,
1710                    layer.attn_norm.float_data(),
1711                    &mut h,
1712                    n_embd,
1713                    total,
1714                    eps,
1715                )?;
1716            }
1717
1718            // Q/K/V + gate projections and wo are batched weight streams. The step35 core
1719            // remains per sequence, so its partial RoPE, SWA view, KV append, and gate
1720            // application stay verbatim.
1721            let gate_w = fa
1722                .attn_gate
1723                .as_ref()
1724                .ok_or("step35 layer is missing attn_gate.weight (head-wise attention gate)")?;
1725            let mut g4 = if f16fuse {
1726                e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv, gate_w], &h, &hx16, total)?
1727            } else {
1728                e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv, gate_w], &h, total)?
1729            };
1730            let gate = g4.pop().unwrap();
1731            let mut parts: Vec<Vec<CudaSlice<f32>>> =
1732                (0..b).map(|_| Vec::with_capacity(3)).collect();
1733            for (w, y) in [&fa.wq, &fa.wk, &fa.wv].iter().zip(g4) {
1734                for (s, ys) in split(e, &y, w.out_features())?.into_iter().enumerate() {
1735                    parts[s].push(ys);
1736                }
1737            }
1738            let gates = split(e, &gate, gate_w.out_features())?;
1739            let geometry = self.step35_geom(il);
1740            let hd = geometry.head_dim_k as usize;
1741            let nh = geometry.n_head as usize;
1742            let mut ag_cat = e.uninit(total * nh * hd)?;
1743            for (s, (g3s, gate)) in parts.into_iter().zip(gates).enumerate() {
1744                let ag = self.step35_attn_pre_wo(
1745                    e,
1746                    fa,
1747                    g3s,
1748                    None,
1749                    Some(&gate),
1750                    &pos_ds[s],
1751                    ts[s],
1752                    Some(&mut *caches[s]),
1753                    il,
1754                    ts[s],
1755                )?;
1756                e.copy_into(
1757                    &mut ag_cat,
1758                    offs[s] * nh * hd,
1759                    &ag,
1760                    ts[s] * nh * hd,
1761                )?;
1762            }
1763            let mixed = e.matmul(&fa.wo, &ag_cat, total)?;
1764
1765            let mut x1 = e.uninit(total * n_embd)?;
1766            let mut z = e.uninit(total * n_embd)?;
1767            let mut zx16 = e.alloc_u8_uninit(total * n_embd * 2)?;
1768            if f16fuse {
1769                e.add_rms_norm_f16out(
1770                    &x,
1771                    &mixed,
1772                    layer.post_attn_norm.float_data(),
1773                    &mut x1,
1774                    &mut z,
1775                    &mut zx16,
1776                    n_embd,
1777                    total,
1778                    eps,
1779                )?;
1780            } else {
1781                e.add(&x, &mixed, &mut x1, total * n_embd)?;
1782                e.rms_norm(
1783                    &x1,
1784                    layer.post_attn_norm.float_data(),
1785                    &mut z,
1786                    n_embd,
1787                    total,
1788                    eps,
1789                )?;
1790            }
1791
1792            let ffn_out = match &layer.ffn {
1793                crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } => {
1794                    let n_ff = ffn_gate.out_features();
1795                    let mut g2 = if f16fuse {
1796                        e.matmul_group_xh(&[ffn_gate, ffn_up], &z, &zx16, total)?
1797                    } else {
1798                        e.matmul_group(&[ffn_gate, ffn_up], &z, total)?
1799                    };
1800                    let up = g2.pop().unwrap();
1801                    let gate = g2.pop().unwrap();
1802                    let mut act = e.uninit(total * n_ff)?;
1803                    let d_lim = cfg.clamp_shexp_at(il as u32);
1804                    if Self::f16out_on(e, total) && cfg.m3.is_none() && d_lim.is_none() {
1805                        let mut a16 = e.alloc_u8_uninit(total * n_ff * 2)?;
1806                        e.silu_mul_f16out(&gate, &up, &mut act, &mut a16, total * n_ff)?;
1807                        match e.try_f16_gemm_pre(ffn_down, &a16, total)? {
1808                            Some(y) => y,
1809                            None => e.matmul(ffn_down, &act, total)?,
1810                        }
1811                    } else {
1812                        Self::ffn_act_lim(
1813                            e,
1814                            cfg,
1815                            &gate,
1816                            &up,
1817                            1.0,
1818                            1.0,
1819                            d_lim,
1820                            &mut act,
1821                            total * n_ff,
1822                        )?;
1823                        e.matmul(ffn_down, &act, total)?
1824                    }
1825                }
1826                crate::hybrid::Ffn::Moe(m) => self.moe_ffn_il(e, m, &z, total, il as u16)?,
1827            };
1828            let mut x2 = e.uninit(total * n_embd)?;
1829            e.add(&x1, &ffn_out, &mut x2, total * n_embd)?;
1830            x = x2;
1831        }
1832        Ok(x)
1833    }
1834
1835    fn step35_prime_batch_epilogue(
1836        &self,
1837        e: &Engine,
1838        x: CudaSlice<f32>,
1839        ts: &[usize],
1840        offs: &[usize],
1841        caches: &mut [&mut Cache],
1842    ) -> Result<Vec<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
1843        let n_embd = self.cfg.n_embd as usize;
1844        let total: usize = ts.iter().sum();
1845        let mut hn = e.uninit(total * n_embd)?;
1846        e.rms_norm(
1847            &x,
1848            self.output_norm.float_data(),
1849            &mut hn,
1850            n_embd,
1851            total,
1852            self.cfg.rms_eps,
1853        )?;
1854
1855        let hidden_src = if crate::spec::spec_hpost() { &hn } else { &x };
1856        let mut out = Vec::with_capacity(ts.len());
1857        for s in 0..ts.len() {
1858            let mut hidden = e.uninit(ts[s] * n_embd)?;
1859            e.copy_view_into(
1860                &mut hidden,
1861                0,
1862                &hidden_src.slice(offs[s] * n_embd..(offs[s] + ts[s]) * n_embd),
1863                ts[s] * n_embd,
1864            )?;
1865            let last0 = (offs[s] + ts[s] - 1) * n_embd;
1866            let mut h_seed = e.uninit(n_embd)?;
1867            e.copy_view_into(
1868                &mut h_seed,
1869                0,
1870                &hidden_src.slice(last0..last0 + n_embd),
1871                n_embd,
1872            )?;
1873            // Exactness-first: the serial reference runs the output head at m=1.
1874            let mut hlast = e.uninit(n_embd)?;
1875            e.copy_view_into(
1876                &mut hlast,
1877                0,
1878                &hn.slice(last0..last0 + n_embd),
1879                n_embd,
1880            )?;
1881            let logits = e.dtoh(&e.matmul(&self.output, &hlast, 1)?)?;
1882            caches[s].pos += ts[s];
1883            out.push((logits, h_seed, hidden));
1884        }
1885        Ok(out)
1886    }
1887
1888    fn step35_prime_cache_batch(
1889        &self,
1890        e: &Engine,
1891        prompts: &[&[u32]],
1892        caches: &mut [&mut Cache],
1893    ) -> Result<Vec<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
1894        if crate::pp::pp_host_bounce_active()
1895            && (!crate::pp::prime_pp_on()
1896                || crate::pp::pp_cuts(self.layers.len()).is_none())
1897        {
1898            return Err(
1899                "step35_prime_cache_batch: MEMRA_PP_HOST_BOUNCE=1 requires a valid prime \
1900                 stage split; refusing an unsplit remote-weight walk"
1901                    .into(),
1902            );
1903        }
1904        if !Self::step35_prime_batch_on() {
1905            return Err("step35 batched prime is disabled (MEMRA_STEP35_PRIME_BATCH=0)".into());
1906        }
1907        if caches.iter().any(|c| c.pos != 0) {
1908            return Err(
1909                "step35 batched prime currently supports complete fresh prompts only; \
1910                 continuation/tick chunks require per-request queued_after"
1911                    .into(),
1912            );
1913        }
1914
1915        let ts: Vec<usize> = prompts.iter().map(|p| p.len()).collect();
1916        for &t in &ts {
1917            assert!(t >= PRIME_MIN_T, "step35 batched prime needs T >= {PRIME_MIN_T}");
1918        }
1919        for (s, c) in caches.iter().enumerate() {
1920            assert!(ts[s] <= c.max_ctx, "step35 batched prime exceeds cache max_ctx");
1921        }
1922        let offs: Vec<usize> = ts
1923            .iter()
1924            .scan(0usize, |a, &t| {
1925                let o = *a;
1926                *a += t;
1927                Some(o)
1928            })
1929            .collect();
1930        let total: usize = ts.iter().sum();
1931        let payload = total * self.cfg.n_embd as usize;
1932        let cat_tokens: Vec<u32> = prompts.iter().flat_map(|p| p.iter().copied()).collect();
1933        let positions: Vec<Vec<i32>> = ts
1934            .iter()
1935            .map(|&t| (0..t as i32).collect())
1936            .collect();
1937        let upload_positions = |e: &Engine|
1938                                -> Result<Vec<CudaSlice<i32>>, Box<dyn std::error::Error>> {
1939            positions
1940                .iter()
1941                .map(|p| e.htod_i32(p))
1942                .collect::<Result<_, _>>()
1943        };
1944
1945        static ONCE: std::sync::Once = std::sync::Once::new();
1946        ONCE.call_once(|| {
1947            eprintln!(
1948                "[step35-prime-batch] first concat prime: B={} tokens={total}",
1949                prompts.len()
1950            );
1951        });
1952
1953        let out = if !crate::pp::pp2_streams_off() && crate::pp::prime_pp_on() {
1954            if let Some(fence) = crate::pp::pp_cuts(self.layers.len()) {
1955                let rt = crate::pp::PpNRt::get(e)?;
1956                let n_st = fence.len() - 1;
1957                assert_eq!(rt.n_stages(), n_st, "step35 prime batch stage count mismatch");
1958                let caller_stream = e.stream();
1959                rt.fence_stages_behind(&caller_stream)?;
1960
1961                let mut slot = {
1962                    let _st0 = rt.enter(0);
1963                    let e0 = rt.engine(0, e);
1964                    let pos_ds = upload_positions(e0)?;
1965                    let x = self.embed(e0, &cat_tokens)?;
1966                    let x = self.step35_prime_batch_layers(
1967                        e0,
1968                        x,
1969                        fence[0],
1970                        fence[1],
1971                        &ts,
1972                        &offs,
1973                        &pos_ds,
1974                        caches,
1975                    )?;
1976                    rt.tx(0, &x, payload)?
1977                };
1978                for s in 1..n_st - 1 {
1979                    let _st = rt.enter(s);
1980                    let es = rt.engine(s, e);
1981                    let pos_ds = upload_positions(es)?;
1982                    let x = rt.rx(s - 1, slot, payload)?;
1983                    let x = self.step35_prime_batch_layers(
1984                        es,
1985                        x,
1986                        fence[s],
1987                        fence[s + 1],
1988                        &ts,
1989                        &offs,
1990                        &pos_ds,
1991                        caches,
1992                    )?;
1993                    slot = rt.tx(s, &x, payload)?;
1994                }
1995
1996                let _stl = rt.enter(n_st - 1);
1997                let el = rt.engine(n_st - 1, e);
1998                let pos_ds = upload_positions(el)?;
1999                let x = rt.rx(n_st - 2, slot, payload)?;
2000                let x = self.step35_prime_batch_layers(
2001                    el,
2002                    x,
2003                    fence[n_st - 1],
2004                    fence[n_st],
2005                    &ts,
2006                    &offs,
2007                    &pos_ds,
2008                    caches,
2009                )?;
2010                let out = self.step35_prime_batch_epilogue(el, x, &ts, &offs, caches)?;
2011                rt.publish_to(n_st - 1, &caller_stream)?;
2012                crate::pp::STEP35_PRIME_BATCH_SPLITS.fetch_add(
2013                    1,
2014                    std::sync::atomic::Ordering::Relaxed,
2015                );
2016                out
2017            } else {
2018                let pos_ds = upload_positions(e)?;
2019                let x = self.embed(e, &cat_tokens)?;
2020                let x = self.step35_prime_batch_layers(
2021                    e,
2022                    x,
2023                    0,
2024                    self.layers.len(),
2025                    &ts,
2026                    &offs,
2027                    &pos_ds,
2028                    caches,
2029                )?;
2030                self.step35_prime_batch_epilogue(e, x, &ts, &offs, caches)?
2031            }
2032        } else {
2033            let pos_ds = upload_positions(e)?;
2034            let x = self.embed(e, &cat_tokens)?;
2035            let x = self.step35_prime_batch_layers(
2036                e,
2037                x,
2038                0,
2039                self.layers.len(),
2040                &ts,
2041                &offs,
2042                &pos_ds,
2043                caches,
2044            )?;
2045            self.step35_prime_batch_epilogue(e, x, &ts, &offs, caches)?
2046        };
2047        crate::pp::STEP35_PRIME_BATCHES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2048        Ok(out)
2049    }
2050
2051    /// Cross-request BATCHED fresh prime (task #13, design in ARCHITECTURE-H100.md): the
2052    /// trunk's token-parallel ops (embed, norms, adds, ffn, projection GROUPS) run once on
2053    /// the CONCATENATION of B sequences — GEMMs at m = sum_T, the continuous-batching win
2054    /// the serving-lane bench attributed the remaining vLLM gap to. The stateful mixer
2055    /// CORES (QK-norm/RoPE/FA/append, conv/GDN scans) run per sequence on split projection
2056    /// buffers (D2D row copies; the mixers' own out-projections stay per-seq this
2057    /// increment). CONTINUATION primes (increment (b), 2026-07-30): cache.pos > 0 seqs
2058    /// batch the projections/FFN/lm_head exactly like fresh; the mixer cores take the
2059    /// per-seq CONTINUATION arms (Full: core_inner with carried pos_d + fa_prefill_view
2060    /// over the quantized past; Linear: the stateful pad_view twin — the same state
2061    /// carry the chunked single-seq prime rides). The fresh-only favl/gdn-vl fast paths
2062    /// stay byte-identical (gated on !carried). gemma4 models have no continuation
2063    /// prime (v0 monolithic fresh) — carried gemma4 batches return Err (caller falls
2064    /// back to single-chunk serving).
2065    /// NUMERIC CONFIG: a concat GEMM tiles K differently than per-seq GEMMs — same class
2066    /// as every prefill GEMM change; prime_batch_gate arbitrates (argmax + stream battery).
2067    pub fn prime_cache_batch(&self, e: &Engine, prompts: &[&[u32]], caches: &mut [&mut Cache])
2068                             -> Result<Vec<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
2069        let cfg = &self.cfg;
2070        let n_embd = cfg.n_embd as usize;
2071        let eps = cfg.rms_eps;
2072        let b = prompts.len();
2073        assert!(b >= 1 && b == caches.len());
2074        let pos0s: Vec<usize> = caches.iter().map(|c| c.pos).collect();
2075        let carried = pos0s.iter().any(|&p| p > 0);
2076        // gemma4: refuse UNCONDITIONALLY (2026-08-07, lane/gemma4-serve-gaps). The old guard
2077        // covered only `carried` — two concurrent FRESH gemma4 prompts batched into the
2078        // generic concat attn core below (uniform geometry, no per-layer swa window, no
2079        // softcapped head): compiles, runs, wrong logits. Same silent-wrong class as the
2080        // step35 refusal beneath. The per-sequence `gemma4_prime` is the supported prefill.
2081        if cfg.gemma4.is_some() {
2082            return Err("prime_cache_batch: gemma4 has no batched prime core (per-layer \
2083                        swa/global geometry, softcapped head) — use gemma4_prime per sequence".into());
2084        }
2085        // Step35 has a dedicated concat walk: the generic core below cannot express its
2086        // per-layer geometry/SWA/head gate, and under PP the dedicated path is stage-scoped.
2087        if cfg.step35.is_some() {
2088            return self.step35_prime_cache_batch(e, prompts, caches);
2089        }
2090        let ts: Vec<usize> = prompts.iter().map(|p| p.len()).collect();
2091        for &t in &ts { assert!(t >= PRIME_MIN_T, "prime_cache_batch needs T >= {PRIME_MIN_T}"); }
2092        for (s, c) in caches.iter().enumerate() {
2093            assert!(c.pos + ts[s] <= c.max_ctx, "prime_cache_batch: prompt exceeds cache max_ctx");
2094        }
2095        let total: usize = ts.iter().sum();
2096        let offs: Vec<usize> = ts.iter().scan(0usize, |a, &t| { let o = *a; *a += t; Some(o) }).collect();
2097        // per-seq positions (fresh: 0..T_s; continuation: pos0..pos0+T_s)
2098        let pos_ds: Vec<CudaSlice<i32>> = ts.iter().zip(&pos0s)
2099            .map(|(&t, &p0)| e.htod_i32(&(p0 as i32..(p0 + t) as i32).collect::<Vec<_>>()))
2100            .collect::<Result<_, _>>()?;
2101        // split a concat [total, dim] buffer into per-seq copies
2102        let split = |e: &Engine, y: &CudaSlice<f32>, dim: usize|
2103                     -> Result<Vec<CudaSlice<f32>>, Box<dyn std::error::Error>> {
2104            let mut out = Vec::with_capacity(b);
2105            for s in 0..b {
2106                let mut ys = e.uninit(ts[s] * dim)?;
2107                e.copy_view_into(&mut ys, 0, &y.slice(offs[s] * dim..(offs[s] + ts[s]) * dim), ts[s] * dim)?;
2108                out.push(ys);
2109            }
2110            Ok(out)
2111        };
2112
2113        let cat_tokens: Vec<u32> = prompts.iter().flat_map(|p| p.iter().copied()).collect();
2114        let mut x = self.embed(e, &cat_tokens)?;   // [total, n_embd]
2115        for (il, layer) in self.layers.iter().enumerate() {
2116            let mut h = e.uninit(total * n_embd)?;
2117            let mut hx16 = e.alloc_u8_uninit(total * n_embd * 2)?;
2118            e.rms_norm_f16out(&x, layer.attn_norm.float_data(), &mut h, &mut hx16, n_embd, total, eps)?;
2119            // mixer: projection GROUP on the concat (m = total), stateful core per seq
2120            let mut mixed = e.uninit(total * n_embd)?;
2121            match &layer.mixer {
2122                Mixer::Full(fa) => {
2123                    let g3 = e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv], &h, &hx16, total)?;
2124                    // task #18 (attn side): the WHOLE attn core is varlen for fresh gated
2125                    // batches — split/QK-norm/RoPE/append (attn_pre_vl8, view inputs: the
2126                    // q/k/v split copies vanish) + ONE varlen FA. Per-block math identical
2127                    // everywhere (bit-gateable); MEMRA_FA_VL=0 or a non-bf16kv config falls
2128                    // back to the per-seq dispatch.
2129                    let geometry = self.cfg.full_attention_geometry_at(il as u32);
2130                    let (n_head, n_head_kv, head_dim) = (
2131                        geometry.n_head as usize,
2132                        geometry.n_head_kv as usize,
2133                        geometry.head_dim_k as usize,
2134                    );
2135                    let fa_scale = geometry.attention_scale();
2136                    let use_favl = !carried
2137                        && (2..=8).contains(&b)
2138                        && (head_dim == 256 || head_dim == 128)
2139                        && geometry.attention_gate
2140                            == memra_gguf::config::AttentionGateKind::FusedQ
2141                        && std::env::var("MEMRA_NOFA").is_err()
2142                        && std::env::var("MEMRA_FA_FLOOR").is_err()
2143                        && std::env::var("MEMRA_FA_PP_W2").as_deref() != Ok("1")
2144                        && std::env::var("MEMRA_FA_BF16KV").as_deref() != Ok("0")
2145                        && std::env::var("MEMRA_FA_VL").as_deref() != Ok("0");
2146                    if use_favl {
2147                        let (qf_w, kf_w, vf_w) =
2148                            (fa.wq.out_features(), fa.wk.out_features(), fa.wv.out_features());
2149                        struct APre {
2150                            q: CudaSlice<f32>, gate: Option<CudaSlice<f32>>,
2151                            qn: CudaSlice<f32>, kn: CudaSlice<f32>,
2152                        }
2153                        let mut aps = Vec::with_capacity(b);
2154                        for &t in ts.iter().take(b) {
2155                            aps.push(APre {
2156                                q: e.uninit(t * n_head * head_dim)?,
2157                                gate: Some(e.uninit(t * n_head * head_dim)?),
2158                                qn: e.uninit(t * n_head * head_dim)?,
2159                                kn: e.uninit(t * n_head_kv * head_dim)?,
2160                            });
2161                        }
2162                        let (kv_dim_k, kv_dim_v, ktb, vtb) = {
2163                            let kvl = caches[0].kv[il].as_ref().unwrap();
2164                            (kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes)
2165                        };
2166                        let pargs: Vec<crate::AttnPreVl> = (0..b).map(|s| {
2167                            let (o, t) = (offs[s], ts[s]);
2168                            let kvl = caches[s].kv[il].as_ref().unwrap();
2169                            assert!(kvl.len == 0 && kvl.len + t <= caches[s].max_ctx,
2170                                    "prime_cache_batch attn vl: fresh + capacity");
2171                            crate::AttnPreVl {
2172                                qf: e.addr_f32v(&g3[0].slice(o * qf_w..(o + t) * qf_w)),
2173                                kf: e.addr_f32v(&g3[1].slice(o * kf_w..(o + t) * kf_w)),
2174                                vf: e.addr_f32v(&g3[2].slice(o * vf_w..(o + t) * vf_w)),
2175                                q: e.addr_f32(&aps[s].q),
2176                                gate: e.addr_f32(aps[s].gate.as_ref().unwrap()),
2177                                qn: e.addr_f32(&aps[s].qn), kn: e.addr_f32(&aps[s].kn),
2178                                kc: e.addr_u8(&kvl.k), vc: e.addr_u8(&kvl.v),
2179                                t: t as i32, pad: 0,
2180                            }
2181                        }).collect();
2182                        e.attn_pre_vl8(&pargs, fa.q_norm.float_data(), fa.k_norm.float_data(),
2183                                       head_dim, geometry.n_rot as usize, n_head, n_head_kv,
2184                                       self.cfg.rms_eps, geometry.rope_base, 1.0,
2185                                       kv_dim_k, kv_dim_v, ktb, vtb)?;
2186                        for s in 0..b {
2187                            let kvl = caches[s].kv[il].as_mut().unwrap();
2188                            kvl.len += ts[s];
2189                            let new_len = kvl.len as i32;
2190                            e.set_i32_one(&mut kvl.len_d, new_len)?;
2191                        }
2192                        let mut attns = Vec::with_capacity(b);
2193                        let mut mirrors = Vec::with_capacity(b);
2194                        for &t in ts.iter().take(b) {
2195                            attns.push(e.uninit(t * n_head * head_dim)?);
2196                            let n = t * n_head_kv * head_dim;
2197                            mirrors.push((e.alloc_u8_uninit(n * 2)?, e.alloc_u8_uninit(n * 2)?));
2198                        }
2199                        // FA3 batched twin (round 31): TMA-swizzled wgmma vl when the
2200                        // promoted single-seq config is on; else the mma favl.
2201                        let fa3_on = match std::env::var("MEMRA_FA3").as_deref() {
2202                            Ok("0") => false,
2203                            Ok("1") => true,
2204                            _ => cfg!(memra_hopper_mma),
2205                        };
2206                        if fa3_on {
2207                            let mut q16s = Vec::with_capacity(b);
2208                            let mut v16s = Vec::with_capacity(b);
2209                            for s in 0..b {
2210                                let t = ts[s];
2211                                let mut q16 = e.alloc_u8_uninit(t * n_head * head_dim * 2)?;
2212                                e.f32_to_bf16_into(&aps[s].qn, &mut q16, t * n_head * head_dim)?;
2213                                let mut k16 = e.alloc_u8_uninit(t * n_head_kv * head_dim * 2)?;
2214                                e.f32_to_bf16_into(&aps[s].kn, &mut k16, t * n_head_kv * head_dim)?;
2215                                let mut v16 = e.alloc_u8_uninit(t * n_head_kv * head_dim * 2)?;
2216                                e.f32_to_bf16_v(&g3[2].slice(offs[s] * vf_w..(offs[s] + t) * vf_w),
2217                                                &mut v16, t * n_head_kv * head_dim)?;
2218                                q16s.push(q16);
2219                                v16s.push((k16, v16));
2220                            }
2221                            let mut qp = [core::ptr::null::<core::ffi::c_void>(); 8];
2222                            let mut kp = qp;
2223                            let mut vp = qp;
2224                            let mut op = [core::ptr::null_mut::<f32>(); 8];
2225                            let mut tsv = [0i32; 8];
2226                            for s in 0..b {
2227                                qp[s] = e.addr_u8(&q16s[s]) as *const core::ffi::c_void;
2228                                kp[s] = e.addr_u8(&v16s[s].0) as *const core::ffi::c_void;
2229                                vp[s] = e.addr_u8(&v16s[s].1) as *const core::ffi::c_void;
2230                                op[s] = e.addr_f32(&attns[s]) as *mut f32;
2231                                tsv[s] = ts[s] as i32;
2232                            }
2233                            let rc = unsafe {
2234                                crate::fa3_vl_raw(qp.as_ptr(), kp.as_ptr(), vp.as_ptr(), op.as_ptr(),
2235                                                  tsv.as_ptr(), b as i32, n_head as i32,
2236                                                  n_head_kv as i32, head_dim as i32, fa_scale,
2237                                                  e.stream().cu_stream() as *mut core::ffi::c_void)
2238                            };
2239                            if rc != 0 {
2240                                return Err(format!("memra_fa3_vl rc={rc}").into());
2241                            }
2242                        } else {
2243                            let fargs: Vec<crate::FaSeqVl> = (0..b).map(|s| crate::FaSeqVl {
2244                                q: e.addr_f32(&aps[s].qn), k16: e.addr_u8(&mirrors[s].0),
2245                                v16: e.addr_u8(&mirrors[s].1), o: e.addr_f32(&attns[s]),
2246                                kf: e.addr_f32(&aps[s].kn),
2247                                vf: e.addr_f32v(&g3[2].slice(offs[s] * vf_w..(offs[s] + ts[s]) * vf_w)),
2248                                t: ts[s] as i32, pad: 0,
2249                            }).collect();
2250                            e.fa_prefill_vl8(&fargs, head_dim, n_head, n_head_kv, fa_scale)?;
2251                        }
2252                        for (s, attn) in attns.into_iter().enumerate() {
2253                            let (attn_g, ag16) = self.full_attn_prime_post_fa(
2254                                e, attn, &aps[s].gate, ts[s], n_head, head_dim)?;
2255                            let mut done = false;
2256                            if let Some(xh) = &ag16 {
2257                                done = e.try_f16_gemm_pre_into_off(&fa.wo, xh, ts[s], &mut mixed, offs[s] * n_embd)?;
2258                            }
2259                            if !done {
2260                                let m = e.matmul(&fa.wo, &attn_g, ts[s])?;
2261                                e.copy_into(&mut mixed, offs[s] * n_embd, &m, ts[s] * n_embd)?;
2262                            }
2263                        }
2264                    } else {
2265                        let mut parts: Vec<Vec<CudaSlice<f32>>> = (0..b).map(|_| Vec::new()).collect();
2266                        for (w, y) in [&fa.wq, &fa.wk, &fa.wv].iter().zip(g3) {
2267                            for (s, ys) in split(e, &y, w.out_features())?.into_iter().enumerate() {
2268                                parts[s].push(ys);
2269                            }
2270                        }
2271                        for (s, g3s) in parts.into_iter().enumerate() {
2272                            // task #16 gather removal: wo writes into `mixed` at offs[s] directly.
2273                            let (attn_g, ag16) = self.full_attn_prime_core_inner(
2274                                e, fa, g3s, &pos_ds[s], ts[s], caches[s], il)?;
2275                            let mut done = false;
2276                            if let Some(xh) = &ag16 {
2277                                done = e.try_f16_gemm_pre_into_off(&fa.wo, xh, ts[s], &mut mixed, offs[s] * n_embd)?;
2278                            }
2279                            if !done {
2280                                let m = e.matmul(&fa.wo, &attn_g, ts[s])?;
2281                                e.copy_into(&mut mixed, offs[s] * n_embd, &m, ts[s] * n_embd)?;
2282                            }
2283                        }
2284                    }
2285                }
2286                Mixer::Mla(_) => crate::hybrid::mla_forward_unimplemented(),
2287                Mixer::Linear(la) => {
2288                    // task #16: NO split copies (cores read row-offset views of the concat
2289                    // outputs; out-GEMMs write into `mixed` at offs[s]). task #18: the core
2290                    // itself is BATCHED — per-seq prep/K1-K3, then ONE varlen K4 + ONE
2291                    // varlen K5 launch for all sequences.
2292                    let ws = [&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha];
2293                    let g4 = e.matmul_group_xh(&ws, &h, &hx16, total)?;
2294                    let outs = self.linear_attn_prime_core_batch(e, la, &g4, &offs, &ts, caches, il)?;
2295                    for (s, (gn, gn16)) in outs.into_iter().enumerate() {
2296                        let (o, t) = (offs[s], ts[s]);
2297                        let mut done = false;
2298                        if let Some(xh) = &gn16 {
2299                            done = e.try_f16_gemm_pre_into_off(&la.ssm_out, xh, t, &mut mixed, o * n_embd)?;
2300                        }
2301                        if !done {
2302                            let m = e.matmul(&la.ssm_out, &gn, t)?;
2303                            e.copy_into(&mut mixed, o * n_embd, &m, t * n_embd)?;
2304                        }
2305                    }
2306                }
2307            }
2308            let mut x1 = e.uninit(total * n_embd)?;
2309            let mut z = e.uninit(total * n_embd)?;
2310            let mut zx16 = e.alloc_u8_uninit(total * n_embd * 2)?;
2311            e.add_rms_norm_f16out(&x, &mixed, layer.post_attn_norm.float_data(),
2312                                  &mut x1, &mut z, &mut zx16, n_embd, total, eps)?;
2313            let ffn_out = match &layer.ffn {
2314                crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } => {
2315                    let n_ff = ffn_gate.out_features();
2316                    let mut g2 = e.matmul_group_xh(&[ffn_gate, ffn_up], &z, &zx16, total)?;
2317                    let up = g2.pop().unwrap();
2318                    let gate = g2.pop().unwrap();
2319                    let mut act = e.uninit(total * n_ff)?;
2320                    // task #17 (batch trunk): silu twin emits the down GEMM's fp16 operand
2321                    // in-epilogue (nsys round-26: this trunk still paid 32 cvt passes).
2322                    // A clamped layer must skip the plain-SiLU twin (see prime_chunk's note).
2323                    let d_lim = self.cfg.clamp_shexp_at(il as u32);
2324                    if Self::f16out_on(e, total) && self.cfg.m3.is_none() && d_lim.is_none() {
2325                        let mut a16 = e.alloc_u8_uninit(total * n_ff * 2)?;
2326                        e.silu_mul_f16out(&gate, &up, &mut act, &mut a16, total * n_ff)?;
2327                        match e.try_f16_gemm_pre(ffn_down, &a16, total)? {
2328                            Some(y) => y,
2329                            None => e.matmul(ffn_down, &act, total)?,
2330                        }
2331                    } else {
2332                        Self::ffn_act_lim(e, &self.cfg, &gate, &up, 1.0, 1.0, d_lim,
2333                                          &mut act, total * n_ff)?;
2334                        e.matmul(ffn_down, &act, total)?
2335                    }
2336                }
2337                crate::hybrid::Ffn::Moe(m) => {
2338                    self.moe_ffn_il_prefill(e, m, &z, total, il as u16)?
2339                }
2340            };
2341            let mut x2 = e.uninit(total * n_embd)?;
2342            e.add(&x1, &ffn_out, &mut x2, total * n_embd)?;
2343            x = x2;
2344        }
2345        // epilogue per seq (identical math to prime_chunk: norm all rows, lm_head on last row)
2346        let mut hn = e.uninit(total * n_embd)?;
2347        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, total, eps)?;
2348        // batched lm_head (nsys round-26: B sequential m=1 matvecs re-read the 600MB
2349        // lm_head weight B times — 2.2ms at B=6): gather the B last rows and run ONE
2350        // m=B GEMM (f16 lane; falls back to the per-seq matvec when no mirror). The
2351        // f16-vs-mmvq logits delta is the prefill GEMM numeric class — prime_batch_gate's
2352        // argmax battery arbitrates, same as every other prefill GEMM change.
2353        let mut hcat = e.uninit(b * n_embd)?;
2354        for s in 0..b {
2355            let last0 = (offs[s] + ts[s] - 1) * n_embd;
2356            e.copy_view_into(&mut hcat, s * n_embd, &hn.slice(last0..last0 + n_embd), n_embd)?;
2357        }
2358        let logits_cat = if b >= 2 { e.try_f16_gemm(&self.output, &hcat, b)? } else { None };
2359        let logits_host: Option<Vec<f32>> = match &logits_cat {
2360            Some(lc) => Some(e.dtoh(lc)?),
2361            None => None,
2362        };
2363        let n_vocab = self.output.out_features();
2364        let mut hidden_all = if crate::spec::spec_hpost() {
2365            split(e, &hn, n_embd)?
2366        } else {
2367            split(e, &x, n_embd)?
2368        };
2369        let mut out = Vec::with_capacity(b);
2370        for s in 0..b {
2371            let last0 = (offs[s] + ts[s] - 1) * n_embd;
2372            let mut h_seed = e.uninit(n_embd)?;
2373            if !crate::spec::spec_hpost() {
2374                e.copy_view_into(&mut h_seed, 0, &x.slice(last0..last0 + n_embd), n_embd)?;
2375            } else {
2376                e.copy_view_into(&mut h_seed, 0, &hn.slice(last0..last0 + n_embd), n_embd)?;
2377            }
2378            let logits = match &logits_host {
2379                Some(lh) => lh[s * n_vocab..(s + 1) * n_vocab].to_vec(),
2380                None => {
2381                    let mut hlast = e.uninit(n_embd)?;
2382                    e.copy_view_into(&mut hlast, 0, &hn.slice(last0..last0 + n_embd), n_embd)?;
2383                    e.dtoh(&e.matmul(&self.output, &hlast, 1)?)?
2384                }
2385            };
2386            caches[s].pos += ts[s];
2387            out.push((logits, h_seed, hidden_all.remove(0)));
2388        }
2389        Ok(out)
2390    }
2391
2392    /// `full_attn` (batched prefill mixer) + the cache side-effect: append the T post-RoPE K/V
2393    /// rows into the resident quantized KV cache (q8_0 K / q5_1 V) and advance len/len_d. Row
2394    /// bytes are BIT-IDENTICAL to the decode append (same per-warp quant kernel per row; the
2395    /// batched `append_kv_quantized_rows` runs that exact warp math on a (block, token) grid).
2396    /// The attention itself is unchanged prefill math (fa_prefill over the f32 K/V).
2397    ///
2398    /// `seq_end` = the ABSOLUTE end position of the WHOLE prime request (`cache.pos + prompt_len`
2399    /// measured BEFORE the chunk loop starts), i.e. a chunk-size-invariant property of the request.
2400    /// Only step35's SWA arm reads it (`step35_attn_pre_wo`'s doc note explains why keying on the
2401    /// chunk's own `t_kv` made the output depend on MEMRA_PRIME_CHUNK); every other arch ignores it.
2402    #[allow(clippy::too_many_arguments)]
2403    fn full_attn_prime(&self, e: &Engine, fa: &FullAttnLayer, h: &CudaSlice<f32>,
2404                       hx: Option<&CudaSlice<u8>>,
2405                       pos_d: &CudaSlice<i32>, t: usize, cache: &mut Cache, il: usize,
2406                       seq_end: usize)
2407                       -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2408        if self.cfg.step35.is_some() {
2409            return self.step35_attn_prime(e, fa, h, hx, pos_d, t, cache, il, seq_end);
2410        }
2411        // PROJ/CORE SPLIT (task #13, 2026-07-26): the q/k/v group projection is hoisted so
2412        // the cross-request batch driver can run it at m = sum_T over concatenated tokens;
2413        // this single-seq path composes proj+core identically (byte-for-byte the old body).
2414        // task #14: `hx` = the norm-fused fp16 twin of `h` (skips the convert launch).
2415        let g3 = match hx {
2416            Some(xh) => e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv], h, xh, t)?,
2417            None => e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?,
2418        };
2419        self.full_attn_prime_core(e, fa, g3, pos_d, t, cache, il)
2420    }
2421
2422    /// Everything after the q/k/v projections (split/QK-norm/RoPE/FA/gate/append + wo).
2423    /// `g3` = matmul_group([wq, wk, wv]) output rows for THIS sequence.
2424    /// Composes inner + wo (== the pre-split core; every existing caller unchanged).
2425    fn full_attn_prime_core(&self, e: &Engine, fa: &FullAttnLayer, g3: Vec<CudaSlice<f32>>,
2426                            pos_d: &CudaSlice<i32>, t: usize, cache: &mut Cache, il: usize)
2427                            -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2428        let (attn_g, ag16) = self.full_attn_prime_core_inner(e, fa, g3, pos_d, t, cache, il)?;
2429        if let Some(xh) = &ag16 {
2430            if let Some(y) = e.try_f16_gemm_pre(&fa.wo, xh, t)? {
2431                return Ok(y);
2432            }
2433        }
2434        Ok(e.matmul(&fa.wo, &attn_g, t)?)
2435    }
2436
2437    fn full_attn_prime_core_inner(&self, e: &Engine, fa: &FullAttnLayer, g3: Vec<CudaSlice<f32>>,
2438                            pos_d: &CudaSlice<i32>, t: usize, cache: &mut Cache, il: usize)
2439                            -> Result<(CudaSlice<f32>, Option<CudaSlice<u8>>), Box<dyn std::error::Error>> {
2440        let cfg = &self.cfg;
2441        let geometry = cfg.full_attention_geometry_at(il as u32);
2442        let n_head = geometry.n_head as usize;
2443        let n_head_kv = geometry.n_head_kv as usize;
2444        let head_dim = geometry.head_dim_k as usize;
2445        let scale = geometry.attention_scale();
2446        let (pre, base_len) = self.full_attn_prime_pre_fa(e, fa, g3, pos_d, t, cache, il)?;
2447        let AttnPre { q, k, v, gate } = pre;
2448        let mut attn = e.uninit(t * n_head * head_dim)?;
2449        self.full_attn_prime_fa_dispatch(e, &q, &k, &v, &mut attn, base_len, t, cache, il,
2450                                         head_dim, n_head, n_head_kv, scale)?;
2451        self.full_attn_prime_post_fa(e, attn, &gate, t, n_head, head_dim)
2452    }
2453
2454    /// task #18 (attn side): projections tail through KV append — everything before the
2455    /// attention kernel. Returns the post-rope q/k, v, optional out-gate, and the KV rows
2456    /// present BEFORE this chunk's append (base_len; 0 == fresh).
2457    #[allow(clippy::type_complexity)]
2458    fn full_attn_prime_pre_fa(&self, e: &Engine, fa: &FullAttnLayer, mut g3: Vec<CudaSlice<f32>>,
2459                            pos_d: &CudaSlice<i32>, t: usize, cache: &mut Cache, il: usize)
2460                            -> Result<(AttnPre, usize), Box<dyn std::error::Error>> {
2461        let cfg = &self.cfg;
2462        let geometry = cfg.full_attention_geometry_at(il as u32);
2463        let n_head = geometry.n_head as usize;
2464        let n_head_kv = geometry.n_head_kv as usize;
2465        let head_dim = geometry.head_dim_k as usize;
2466        let eps = cfg.rms_eps;
2467
2468        // qwen35 fuses [q|gate] per head in wq (2*head_dim stride); M3/Hy3 have NO output gate
2469        // (attention_output_gate=false) — wq out = n_head*head_dim exactly, and q_gate_split
2470        // would read 2x out of bounds. `gated` keys both the split and the sigmoid epilogue.
2471        let gated = geometry.attention_gate
2472            == memra_gguf::config::AttentionGateKind::FusedQ;
2473        let v = g3.pop().unwrap();
2474        let mut k = g3.pop().unwrap();
2475        let qf = g3.pop().unwrap();
2476        let (mut q, gate) = if gated {
2477            let mut q = e.uninit(t * n_head * head_dim)?;
2478            let mut gate = e.uninit(t * n_head * head_dim)?;
2479            e.q_gate_split(&qf, &mut q, &mut gate, head_dim, n_head, t)?;
2480            (q, Some(gate))
2481        } else {
2482            (qf, None)
2483        };
2484
2485        let mut qn = e.uninit(t * n_head * head_dim)?;
2486        e.rms_norm(&q, fa.q_norm.float_data(), &mut qn, head_dim, n_head * t, eps)?;
2487        q = qn;
2488        let mut kn = e.uninit(t * n_head_kv * head_dim)?;
2489        e.rms_norm(&k, fa.k_norm.float_data(), &mut kn, head_dim, n_head_kv * t, eps)?;
2490        k = kn;
2491        let rope_dims = geometry.n_rot as usize;
2492        e.rope_neox(&mut q, pos_d, head_dim, rope_dims, n_head, t, geometry.rope_base, 1.0)?;
2493        e.rope_neox(&mut k, pos_d, head_dim, rope_dims, n_head_kv, t, geometry.rope_base, 1.0)?;
2494
2495        // CACHE SIDE-EFFECT: append the T post-rope K/V token rows (token-major [T, kv_dim] ==
2496        // the cache row layout) quantized into cache.kv[il], then advance len + device len_d.
2497        {
2498            let kvl = cache.kv[il].as_mut().unwrap();
2499            assert!(kvl.len + t <= cache.max_ctx, "prime_cache: KV overflow");
2500            e.append_kv_quantized_rows(&k, &v, &mut kvl.k, &mut kvl.v, kvl.len, t,
2501                                       kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes,
2502                                       crate::Engine::kv_fp8_on())?;
2503            kvl.len += t;
2504            let new_len = kvl.len as i32;
2505            e.set_i32_one(&mut kvl.len_d, new_len)?;
2506        }
2507
2508        let base_len = {
2509            let kvl = cache.kv[il].as_ref().unwrap();
2510            kvl.len - t   // KV rows present BEFORE this chunk's append above
2511        };
2512        Ok((AttnPre { q, k, v, gate }, base_len))
2513    }
2514
2515    /// batched prefill attention. FRESH prime (no past KV): unchanged forward_last math over
2516    /// the f32 K/V of this batch. CONTINUATION chunk (past KV present): the chunk's queries
2517    /// must attend to [0 .. base+t) — run fa_prefill_view over the resident QUANTIZED cache
2518    /// (the spec-verify pattern; kernel's causal mask offsets by T_kv-T). Numerically this
2519    /// reads q8_0/q5_1-dequantized K/V for the past AND the current chunk — the same class as
2520    /// decode reading the cache; the run-gen/first-16 battery is the accuracy authority.
2521    #[allow(clippy::too_many_arguments)]
2522    fn full_attn_prime_fa_dispatch(&self, e: &Engine, q: &CudaSlice<f32>, k: &CudaSlice<f32>,
2523                            v: &CudaSlice<f32>, attn: &mut CudaSlice<f32>, base_len: usize,
2524                            t: usize, cache: &mut Cache, il: usize,
2525                            head_dim: usize, n_head: usize, n_head_kv: usize, scale: f32)
2526                            -> Result<(), Box<dyn std::error::Error>> {
2527        // GRAIN-FREE CHUNK-INVARIANCE FIX (lane/chunkinv-flip, 2026-08-05): the base_len == 0
2528        // f32 special case (fa_prefill over this batch's f32 K/V) is DROPPED. pre_fa appends
2529        // the chunk's quantized rows into cache.kv[il] BEFORE this dispatch, so chunk 0 can
2530        // attend through the quantized cache exactly like every later chunk (quantize-then-
2531        // attend). One numeric class for every row => the chunk size cannot decide where a
2532        // precision edge falls, so chunked prefill is reduction-order-stable with NO grain
2533        // knob (the stronger fix VERDICT.md filed; supersedes the MEMRA_PRIME_INVARIANT door's
2534        // pin-the-boundary approach).
2535        // MEMRA_PRIME_F32CHUNK0=1 is the ROLLBACK SEAM to the old arithmetic (chunk 0 attends
2536        // f32 K/V) — flags-doctrine rollback door AND the chunkinv gate's canary injection:
2537        // with the fix unconditional, only re-introducing the class edge can prove the gate
2538        // still detects the mechanism. Never on in a measured default run.
2539        if base_len == 0 && std::env::var("MEMRA_PRIME_F32CHUNK0").as_deref() == Ok("1") {
2540            if std::env::var("MEMRA_NOFA").is_ok() || !(head_dim == 256 || head_dim == 128) {
2541                e.sdpa_naive(q, k, v, attn, head_dim, n_head, n_head_kv, t, t, scale, true)?;
2542            } else {
2543                e.fa_prefill(q, k, v, attn, head_dim, n_head, n_head_kv, t, t, scale, true)?;
2544            }
2545            return Ok(());
2546        }
2547        let kvl = cache.kv[il].as_ref().unwrap();
2548        let t_kv = base_len + t;
2549        let k_view = e.view_u8(&kvl.k, t_kv * kvl.k_tok_bytes);
2550        let v_view = e.view_u8(&kvl.v, t_kv * kvl.v_tok_bytes);
2551        // fa_prefill_q/_qw twins are stamped for head_dim 256 (qwen35) and 128 (M3) only;
2552        // other dims (and MEMRA_NOFA) take the naive quantized-view SDPA — SAME cache bytes,
2553        // same numeric class, so the uniform contract holds on the fallback too.
2554        if std::env::var("MEMRA_NOFA").is_ok() || !(head_dim == 256 || head_dim == 128) {
2555            e.sdpa_naive_quantized_view(q, &k_view, &v_view, attn, head_dim, n_head,
2556                                        n_head_kv, t, t_kv, scale, true,
2557                                        kvl.k_tok_bytes, kvl.v_tok_bytes)?;
2558            return Ok(());
2559        }
2560        // ARC B (2026-07-05): dequant-once workspace, DEFAULT ON. fa_prefill_q's inline
2561        // dequant re-reads+re-dequants the whole quantized KV stream from every one of the
2562        // T/64 x n_head CTAs (64x+ redundant at chunk=4096; 30.5% of the 32k prime wall).
2563        // fa_prefill_view_ws dequants K/V ONCE into a resident bf16 workspace then runs the
2564        // bit-identical bf16 twin (fa_prefill_qw) — same staged values, same FP order, token-
2565        // identical output (gate: MEMRA_PRIME_CHUNK=4096 ws-on vs ws-off vs monolithic).
2566        // MEMRA_PRIME_DEQW=0 reverts to the inline-dequant kernel.
2567        let deqw = std::env::var("MEMRA_PRIME_DEQW").map(|v| v != "0").unwrap_or(true);
2568        if deqw {
2569            e.fa_prefill_view_ws(q, &k_view, &v_view, attn, head_dim, n_head, n_head_kv,
2570                                 t, t_kv, scale, true, kvl.k_tok_bytes, kvl.v_tok_bytes,
2571                                 crate::Engine::kv_fp8_on())?;
2572        } else {
2573            e.fa_prefill_view(q, &k_view, &v_view, attn, head_dim, n_head, n_head_kv,
2574                              t, t_kv, scale, true, kvl.k_tok_bytes, kvl.v_tok_bytes,
2575                              crate::Engine::kv_fp8_on())?;
2576        }
2577        Ok(())
2578    }
2579
2580    /// task #17: sig_mul_f16out fuses [sigmoid + mul + f16 convert] into one launch
2581    /// (bit-identical composition) and hands wo its fp16 operand directly.
2582    fn full_attn_prime_post_fa(&self, e: &Engine, attn: CudaSlice<f32>,
2583                            gate: &Option<CudaSlice<f32>>, t: usize,
2584                            n_head: usize, head_dim: usize)
2585                            -> Result<(CudaSlice<f32>, Option<CudaSlice<u8>>), Box<dyn std::error::Error>> {
2586        let (attn_g, ag16) = match gate {
2587            Some(gate) => {
2588                let n = t * n_head * head_dim;
2589                let mut ag = e.uninit(n)?;
2590                if Self::f16out_on(e, t) {
2591                    let mut a16 = e.alloc_u8_uninit(n * 2)?;
2592                    e.sig_mul_f16out(&attn, gate, &mut ag, &mut a16, n)?;
2593                    (ag, Some(a16))
2594                } else {
2595                    let mut gsig = e.uninit(n)?;
2596                    e.sigmoid(gate, &mut gsig, n)?;
2597                    e.mul(&attn, &gsig, &mut ag, n)?;
2598                    (ag, None)
2599                }
2600            }
2601            None => (attn, None),
2602        };
2603        Ok((attn_g, ag16))
2604    }
2605
2606    /// STATEFUL batched linear-attention prime: `linear_attn`'s prefill-dispatch pass (normal
2607    /// `e.matmul` — GEMM at m>=16 — plus the prefill repack/L2/glog kernels) but with the state
2608    /// carried THROUGH the cache like the spec verify does: carried-ring conv
2609    /// (ssm_conv1d_tm_state writes the final ring back) + ONE gdn_scan from cache.recur[il]'s
2610    /// current state (zero at a fresh prime) whose final state ping-pongs back into the cache.
2611    /// Wiring mirrors `linear_attn_verify_t` (spec.rs); dispatch mirrors `linear_attn` (prefill).
2612    fn linear_attn_prime(&self, e: &Engine, la: &LinearAttnLayer, h: &CudaSlice<f32>,
2613                         hx: Option<&CudaSlice<u8>>, t: usize,
2614                         cache: &mut Cache, il: usize)
2615                         -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2616        // PROJ/CORE SPLIT (task #13): see full_attn_prime — same hoist for the GDN 4-tuple.
2617        let ws = [&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha];
2618        let g4 = match hx {
2619            Some(xh) => e.matmul_group_xh(&ws, h, xh, t)?,
2620            None => e.matmul_group(&ws, h, t)?,
2621        };
2622        self.linear_attn_prime_core(e, la, g4, t, cache, il)
2623    }
2624
2625    /// Everything after the GDN 4-tuple projections (conv/chunk stack/gated norm + ssm_out).
2626    fn linear_attn_prime_core(&self, e: &Engine, la: &LinearAttnLayer, mut g4: Vec<CudaSlice<f32>>,
2627                              t: usize, cache: &mut Cache, il: usize)
2628                              -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2629        self.linear_attn_prime_core_pad(e, la, g4.drain(..).collect(), t, cache, il, None)
2630    }
2631
2632    /// task #14: `pad_len` = device true length for PADDED prime graphs. Pads become
2633    /// identity GDN steps (gdn_pad_mask zeroes beta/g_log past the true length) and the
2634    /// conv ring writes back from the true tail. None = classic path, byte-identical.
2635    #[allow(clippy::too_many_arguments)]
2636    fn linear_attn_prime_core_pad_inner(&self, e: &Engine, la: &LinearAttnLayer, mut g4: Vec<CudaSlice<f32>>,
2637                              t: usize, cache: &mut Cache, il: usize,
2638                              pad_len: Option<&CudaSlice<i32>>)
2639                              -> Result<(CudaSlice<f32>, Option<CudaSlice<u8>>), Box<dyn std::error::Error>> {
2640        // shim over the view twin (task #16): full-range views of the owned buffers.
2641        let ssm = self.cfg.ssm.as_ref().unwrap();
2642        let d_state = ssm.state_size as usize;
2643        let num_k = ssm.group_count as usize;
2644        let num_v = ssm.time_step_rank as usize;
2645        let key_dim = d_state * num_k;
2646        let value_dim = d_state * num_v;
2647        let conv_dim = key_dim * 2 + value_dim;
2648        let alpha = g4.pop().unwrap();                   // [T, num_v]
2649        let beta_raw = g4.pop().unwrap();                // [T, num_v]
2650        let z = g4.pop().unwrap();                       // [T, value_dim]
2651        let qkv_mixed = g4.pop().unwrap();               // [T, conv_dim] token-major
2652        self.linear_attn_prime_core_pad_view(
2653            e, la,
2654            &qkv_mixed.slice(0..t * conv_dim), &z.slice(0..t * value_dim),
2655            &beta_raw.slice(0..t * num_v), &alpha.slice(0..t * num_v),
2656            t, cache, il, pad_len)
2657    }
2658
2659    /// task #18: the GDN prep stage (conv + repack + l2 x2 + sigmoid + glog + pad-mask) —
2660    /// shared verbatim by the per-seq scan path and the varlen batched path.
2661    #[allow(clippy::too_many_arguments)]
2662    fn linear_attn_gdn_prep(&self, e: &Engine, la: &LinearAttnLayer,
2663                            qkv_mixed: &cudarc::driver::CudaView<f32>,
2664                            beta_raw: &cudarc::driver::CudaView<f32>,
2665                            alpha: &cudarc::driver::CudaView<f32>,
2666                            t: usize, cache: &mut Cache, il: usize,
2667                            pad_len: Option<&CudaSlice<i32>>)
2668                            -> Result<GdnPrep, Box<dyn std::error::Error>> {
2669        let cfg = &self.cfg;
2670        let ssm = cfg.ssm.as_ref().unwrap();
2671        let d_state = ssm.state_size as usize;       // 128
2672        let num_k = ssm.group_count as usize;        // 16
2673        let num_v = ssm.time_step_rank as usize;     // 32
2674        let d_conv = ssm.conv_kernel as usize;       // 4
2675        let key_dim = d_state * num_k;               // 2048
2676        let value_dim = d_state * num_v;             // 4096
2677        let conv_dim = key_dim * 2 + value_dim;      // 8192
2678        let eps = cfg.rms_eps;
2679        debug_assert!(t >= d_conv - 1, "stateful conv needs T >= pad (PRIME_MIN_T gates)");
2680
2681        // conv with CARRIED ring state + ring roll (state read + final-window write-back).
2682        // task #18 conv-fuse (default ON): conv + SiLU + repack in one pass — the 67MB
2683        // conv_out intermediate and its transposed re-read disappear (values bit-identical).
2684        // MEMRA_CONV_FUSE=0 reverts to the two-kernel chain.
2685        let rl = cache.recur[il].as_mut().unwrap();
2686        let hk = Self::gdn_hk(e, t, num_v, num_k);
2687        let conv_fuse = std::env::var("MEMRA_CONV_FUSE").as_deref() != Ok("0");
2688        let hk = if conv_fuse { hk } else { num_v };   // de-broadcast rides the fused conv
2689        let mut q_g = e.uninit(d_state * hk * t)?;
2690        let mut k_g = e.uninit(d_state * hk * t)?;
2691        let mut v_g = e.uninit(d_state * num_v * t)?;
2692        if conv_fuse {
2693            e.ssm_conv1d_gdn_state_pad(qkv_mixed, &mut rl.conv_state, la.ssm_conv1d.float_data(),
2694                                  &mut q_g, &mut k_g, &mut v_g,
2695                                  conv_dim, t, d_conv, d_state, num_v, num_k, key_dim, hk, pad_len)?;
2696        } else {
2697            let mut conv_out = e.uninit(conv_dim * t)?;      // [conv_dim, T] channel-major, SiLU
2698            e.ssm_conv1d_tm_state_pad_v(qkv_mixed, &mut rl.conv_state, la.ssm_conv1d.float_data(),
2699                                  &mut conv_out, conv_dim, t, d_conv, pad_len)?;
2700            e.qkv_to_gdn_repack(&conv_out, &mut q_g, &mut k_g, &mut v_g, d_state, num_v, num_k, key_dim, t)?;
2701        }
2702        let mut q_l2 = e.uninit(d_state * hk * t)?;
2703        // mirror-fold (round 35): q's bf16 twin (wgmma K45/K2 A-operand) in-epilogue too.
2704        // Emitted only where a consumer exists (the wgmma config) — on other arches the
2705        // alloc + epilogue stores would be pure waste.
2706        let qb16 = if Engine::l2_v2_on(d_state) && e.gdn_wgmma_on(32) {
2707            let mut qb = e.alloc_u8_uninit(d_state * hk * t * 2)?;
2708            e.l2_norm_pp(&q_g, &mut q_l2, Some(&mut qb), d_state, hk * t, eps)?;
2709            Some(qb)
2710        } else {
2711            e.l2_norm_pp(&q_g, &mut q_l2, None, d_state, hk * t, eps)?;
2712            None
2713        };
2714        let mut k_l2 = e.uninit(d_state * hk * t)?;
2715        // mirror-fold: emit k's bf16 twin (the chunked-scan kb16 mirror) in-epilogue
2716        let kb16 = if Engine::l2_v2_on(d_state) {
2717            let mut kb = e.alloc_u8_uninit(d_state * hk * t * 2)?;
2718            e.l2_norm_pp(&k_g, &mut k_l2, Some(&mut kb), d_state, hk * t, eps)?;
2719            Some(kb)
2720        } else {
2721            e.l2_norm_pp(&k_g, &mut k_l2, None, d_state, hk * t, eps)?;
2722            None
2723        };
2724        let mut beta = e.uninit(t * num_v)?;
2725        e.sigmoid_v(beta_raw, &mut beta, t * num_v)?;
2726        let mut g_log = e.uninit(t * num_v)?;
2727        e.gdn_glog_v(alpha, la.ssm_dt.float_data(), la.ssm_a.float_data(), &mut g_log, num_v, t)?;
2728        if let Some(len_d) = pad_len {
2729            e.gdn_pad_mask(&mut beta, &mut g_log, len_d, num_v, t)?;
2730        }
2731        Ok(GdnPrep { hk, q_l2, k_l2, v_g, beta, g_log, kb16, qb16 })
2732    }
2733
2734    /// task #18: batched GDN mixer core — per-seq prep + K1-K3, then ONE varlen K4 and
2735    /// ONE varlen K5 launch for all B sequences (full-machine occupancy vs B underfilled
2736    /// per-seq trains). Per-block math identical to the per-seq path (bit-gateable);
2737    /// MEMRA_GDN_VL=0 or a non-mma/non-C32 config falls back to the per-seq loop.
2738    #[allow(clippy::too_many_arguments)]
2739    fn linear_attn_prime_core_batch(&self, e: &Engine, la: &LinearAttnLayer,
2740                                    g4: &[CudaSlice<f32>], offs: &[usize], ts: &[usize],
2741                                    caches: &mut [&mut Cache], il: usize)
2742                                    -> Result<Vec<(CudaSlice<f32>, Option<CudaSlice<u8>>)>, Box<dyn std::error::Error>> {
2743        let ssm = self.cfg.ssm.as_ref().unwrap();
2744        let d_state = ssm.state_size as usize;
2745        let num_k = ssm.group_count as usize;
2746        let num_v = ssm.time_step_rank as usize;
2747        let key_dim = d_state * num_k;
2748        let value_dim = d_state * num_v;
2749        let conv_dim = key_dim * 2 + value_dim;
2750        let eps = self.cfg.rms_eps;
2751        let scale = 1.0 / (d_state as f32).sqrt();
2752        let b = ts.len();
2753        let c = Engine::gdn_chunk_size();
2754        // CONTINUATION batches (increment (b)) take the per-seq stateful pad_view twin
2755        // below — the varlen K4/K5 chain is fresh-only (zero initial state assumed).
2756        let carried = caches.iter().any(|c| c.pos > 0);
2757        let use_vl = !carried
2758            && (2..=8).contains(&b)
2759            && Engine::gdn_chunked_enabled() && ts.iter().all(|&t| t >= 16)
2760            && e.gdn_mma_enabled(c)
2761            && std::env::var("MEMRA_GDN_VL").as_deref() != Ok("0");
2762        if !use_vl {
2763            return (0..b).map(|s| {
2764                let (o, t) = (offs[s], ts[s]);
2765                self.linear_attn_prime_core_pad_view(
2766                    e, la,
2767                    &g4[0].slice(o * conv_dim..(o + t) * conv_dim),
2768                    &g4[1].slice(o * value_dim..(o + t) * value_dim),
2769                    &g4[2].slice(o * num_v..(o + t) * num_v),
2770                    &g4[3].slice(o * num_v..(o + t) * num_v),
2771                    t, caches[s], il, None)
2772            }).collect();
2773        }
2774        // increment 3: the ENTIRE core is varlen — allocs only per seq, then 13 launches
2775        // total for the whole batch: [conv, ring, repack, l2(q+k), gate-prep] +
2776        // [k-mirror, K1, K2, K3, w-mirror, K4, K5] + [gated-norm tail].
2777        struct SeqBufs {
2778            conv_out: CudaSlice<f32>, q_g: CudaSlice<f32>, k_g: CudaSlice<f32>, v_g: CudaSlice<f32>,
2779            q_l2: CudaSlice<f32>, k_l2: CudaSlice<f32>, beta: CudaSlice<f32>, g_log: CudaSlice<f32>,
2780            gn: CudaSlice<f32>, gn16: CudaSlice<u8>,
2781        }
2782        let d_conv = ssm.conv_kernel as usize;
2783        let f16o = Self::f16out_on(e, 16);
2784        let hk = Self::gdn_hk(e, 16, num_v, num_k);   // vl path is always chunked+mma
2785        let mut sb = Vec::with_capacity(b);
2786        let mut pres = Vec::with_capacity(b);
2787        for &t in ts.iter().take(b) {
2788            sb.push(SeqBufs {
2789                conv_out: e.uninit(conv_dim * t)?,
2790                q_g: e.uninit(d_state * hk * t)?,
2791                k_g: e.uninit(d_state * hk * t)?,
2792                v_g: e.uninit(d_state * num_v * t)?,
2793                q_l2: e.uninit(d_state * hk * t)?,
2794                k_l2: e.uninit(d_state * hk * t)?,
2795                beta: e.uninit(t * num_v)?,
2796                g_log: e.uninit(t * num_v)?,
2797                gn: e.uninit(d_state * num_v * t)?,
2798                gn16: e.alloc_u8_uninit(d_state * num_v * t * 2)?,
2799            });
2800            pres.push(e.gdn_chunk_alloc(num_v, t, c, hk)?);
2801        }
2802        let prep_args: Vec<crate::GdnPrepVl> = (0..b).map(|s| {
2803            let (o, t) = (offs[s], ts[s]);
2804            let rl = caches[s].recur[il].as_ref().unwrap();
2805            crate::GdnPrepVl {
2806                qkv: e.addr_f32v(&g4[0].slice(o * conv_dim..(o + t) * conv_dim)),
2807                conv_state: e.addr_f32(&rl.conv_state),
2808                conv_out: e.addr_f32(&sb[s].conv_out),
2809                q_g: e.addr_f32(&sb[s].q_g), k_g: e.addr_f32(&sb[s].k_g), v_g: e.addr_f32(&sb[s].v_g),
2810                q_l2: e.addr_f32(&sb[s].q_l2), k_l2: e.addr_f32(&sb[s].k_l2),
2811                beta_raw: e.addr_f32v(&g4[2].slice(o * num_v..(o + t) * num_v)),
2812                alpha: e.addr_f32v(&g4[3].slice(o * num_v..(o + t) * num_v)),
2813                beta: e.addr_f32(&sb[s].beta), g_log: e.addr_f32(&sb[s].g_log),
2814                o: e.addr_f32(&pres[s].o),
2815                z: e.addr_f32v(&g4[1].slice(o * value_dim..(o + t) * value_dim)),
2816                gn: e.addr_f32(&sb[s].gn), gn16: e.addr_u8(&sb[s].gn16),
2817                kb16: if Engine::l2_v2_on(d_state) { e.addr_u8(&pres[s].kb16) } else { 0 },
2818                qb16: if Engine::l2_v2_on(d_state) && e.gdn_wgmma_on(c) { e.addr_u8(&pres[s].qb16) } else { 0 },
2819                t: t as i32, pad: 0,
2820            }
2821        }).collect();
2822        let args: Vec<crate::GdnSeqVl> = (0..b).map(|s| {
2823            let rl = caches[s].recur[il].as_ref().unwrap();
2824            crate::GdnSeqVl {
2825                kb16: e.addr_u8(&pres[s].kb16), gcum: e.addr_f32(&pres[s].gcum),
2826                beta: e.addr_f32(&sb[s].beta), u: e.addr_f32(&pres[s].u),
2827                wb16: e.addr_u8(&pres[s].wb16), y: e.addr_u8(&pres[s].y16),
2828                ssnap: e.addr_u8(&pres[s].ssnap16),
2829                state_in: e.addr_f32(&rl.ssm_state), state_out: e.addr_f32(&rl.ssm_state_alt),
2830                q: e.addr_f32(&sb[s].q_l2), p: e.addr_f32(&pres[s].p),
2831                o: e.addr_f32(&pres[s].o),
2832                k: e.addr_f32(&sb[s].k_l2), v: e.addr_f32(&sb[s].v_g),
2833                g: e.addr_f32(&sb[s].g_log), a: e.addr_f32(&pres[s].a),
2834                w: e.addr_f32(&pres[s].w),
2835                t: ts[s] as i32, nc: pres[s].nc as i32,
2836            }
2837        }).collect();
2838        e.gdn_prep_vl8(&prep_args, la.ssm_conv1d.float_data(), la.ssm_dt.float_data(),
2839                       la.ssm_a.float_data(), conv_dim, d_conv, d_state, num_v, num_k, key_dim, hk, eps)?;
2840        // mirror-fold (round 27): l2 v2 emits kb16 in-epilogue; K3's store emits wb16 —
2841        // both standalone mirror launches vanish on the default config.
2842        if !Engine::l2_v2_on(d_state) {
2843            e.gdn_mirror_vl8(&args, num_v, 0, hk)?;
2844        }
2845        // task #22: wgmma-fused vl twins — qb16 mirrors + GdnWVl8 side-struct.
2846        let wq8: Option<crate::GdnWVl8> = if e.gdn_wgmma_on(c) {
2847            // qb16 emitted by the vl l2 mirror-fold when l2-v2 serves; bulk cvt otherwise
2848            if !Engine::l2_v2_on(d_state) {
2849                for s in 0..b {
2850                    e.f32_to_bf16_into(&sb[s].q_l2, &mut pres[s].qb16, d_state * hk * ts[s])?;
2851                }
2852            }
2853            let mut wa = [crate::GdnWVl::default(); 8];
2854            for s in 0..b {
2855                wa[s] = crate::GdnWVl { qb16: e.addr_u8(&pres[s].qb16), pb16: e.addr_u8(&pres[s].pb16) };
2856            }
2857            Some(crate::GdnWVl8(wa))
2858        } else { None };
2859        e.gdn_chunk_k123_vl8(&args, num_v, hk, wq8.as_ref())?;
2860        e.gdn_chunk_vl8(&args, num_v, scale, hk, wq8.as_ref())?;
2861        if f16o {
2862            e.gdn_tail_vl8(&prep_args, la.ssm_norm.float_data(), d_state, num_v, eps)?;
2863        }
2864        // per-seq state swap (+ non-f16out tail fallback)
2865        let mut out = Vec::with_capacity(b);
2866        for (s, bufs) in sb.into_iter().enumerate() {
2867            let rl = caches[s].recur[il].as_mut().unwrap();
2868            std::mem::swap(&mut rl.ssm_state, &mut rl.ssm_state_alt);
2869            let (o, t) = (offs[s], ts[s]);
2870            let SeqBufs { mut gn, gn16, .. } = bufs;
2871            if f16o {
2872                out.push((gn, Some(gn16)));
2873            } else {
2874                let z_v = g4[1].slice(o * value_dim..(o + t) * value_dim);
2875                e.gated_rmsnorm_zv(&pres[s].o, la.ssm_norm.float_data(), &z_v, &mut gn,
2876                                   d_state, num_v * t, eps)?;
2877                out.push((gn, None));
2878            }
2879        }
2880        Ok(out)
2881    }
2882
2883    /// task #16: view-consuming GDN prime core — the batched prime hands row-offset
2884    /// views of the CONCAT projection outputs directly (no per-seq split copies).
2885    /// Same kernels, same values, byte-identical to the Vec shim above.
2886    #[allow(clippy::too_many_arguments)]
2887    fn linear_attn_prime_core_pad_view(&self, e: &Engine, la: &LinearAttnLayer,
2888                              qkv_mixed: &cudarc::driver::CudaView<f32>,
2889                              z: &cudarc::driver::CudaView<f32>,
2890                              beta_raw: &cudarc::driver::CudaView<f32>,
2891                              alpha: &cudarc::driver::CudaView<f32>,
2892                              t: usize, cache: &mut Cache, il: usize,
2893                              pad_len: Option<&CudaSlice<i32>>)
2894                              -> Result<(CudaSlice<f32>, Option<CudaSlice<u8>>), Box<dyn std::error::Error>> {
2895        let cfg = &self.cfg;
2896        let ssm = cfg.ssm.as_ref().unwrap();
2897        let d_state = ssm.state_size as usize;       // 128
2898        let num_v = ssm.time_step_rank as usize;     // 32
2899        let eps = cfg.rms_eps;
2900        let scale = 1.0 / (d_state as f32).sqrt();
2901
2902        let prep = self.linear_attn_gdn_prep(e, la, qkv_mixed, beta_raw, alpha, t, cache, il, pad_len)?;
2903
2904        // ONE gdn_scan over T from the cache's CURRENT state (zero at fresh prime); the final
2905        // state lands in the spare buffer and ping-pongs back (stable resident pointers, the
2906        // decode-determinism discipline from linear_attn_decode_inner). A4: `gdn_scan_prefill`
2907        // dispatches the chunked WY form under MEMRA_GDN_CHUNKED (prefill-only seam; decode +
2908        // verify keep the sequential kernel).
2909        let mut o = e.uninit(d_state * num_v * t)?;
2910        let rl = cache.recur[il].as_mut().unwrap();
2911        {
2912            let crate::cache::RecurLayer { ssm_state, ssm_state_alt, .. } = rl;
2913            e.gdn_scan_prefill(&prep.q_l2, &prep.k_l2, &prep.v_g, &prep.g_log, &prep.beta,
2914                               prep.kb16.as_ref(), prep.qb16.as_ref(), ssm_state, ssm_state_alt, &mut o, num_v, t, scale,
2915                               prep.hk)?;
2916        }
2917        std::mem::swap(&mut rl.ssm_state, &mut rl.ssm_state_alt);
2918
2919        // gated RMSNorm + out projection (prefill dispatch). task #17: the f16out twin also
2920        // emits the ssm_out GEMM's fp16 operand in-epilogue (kills the standalone convert).
2921        let mut gn = e.uninit(d_state * num_v * t)?;
2922        let gn16 = if Self::f16out_on(e, t) {
2923            let mut g16 = e.alloc_u8_uninit(d_state * num_v * t * 2)?;
2924            e.gated_rmsnorm_f16out_zv(&o, la.ssm_norm.float_data(), z, &mut gn, &mut g16,
2925                                      d_state, num_v * t, eps)?;
2926            Some(g16)
2927        } else {
2928            e.gated_rmsnorm_zv(&o, la.ssm_norm.float_data(), z, &mut gn, d_state, num_v * t, eps)?;
2929            None
2930        };
2931        Ok((gn, gn16))
2932    }
2933
2934    /// Task #15 core-split wrapper: composes inner + ssm_out (== the old core).
2935    #[allow(clippy::too_many_arguments)]
2936    fn linear_attn_prime_core_pad(&self, e: &Engine, la: &LinearAttnLayer, g4: Vec<CudaSlice<f32>>,
2937                              t: usize, cache: &mut Cache, il: usize,
2938                              pad_len: Option<&CudaSlice<i32>>)
2939                              -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2940        let (gn, gn16) = self.linear_attn_prime_core_pad_inner(e, la, g4, t, cache, il, pad_len)?;
2941        if let Some(xh) = &gn16 {
2942            if let Some(y) = e.try_f16_gemm_pre(&la.ssm_out, xh, t)? {
2943                return Ok(y);
2944            }
2945        }
2946        Ok(e.matmul(&la.ssm_out, &gn, t)?)
2947    }
2948
2949    /// Full-attention mixer with QK-norm, partial RoPE, sigmoid output gate (qwen35 :257-336).
2950    ///
2951    /// `il` = layer index: step35 needs it (per-layer n_head / rope width / window / gate) and
2952    /// routes to its own mixer. Every other arch ignores it (uniform geometry).
2953    pub fn full_attn(&self, e: &Engine, fa: &FullAttnLayer, h: &CudaSlice<f32>, pos_d: &CudaSlice<i32>, t: usize, il: usize)
2954                 -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2955        if self.cfg.step35.is_some() {
2956            return self.step35_attn(e, fa, h, pos_d, t, il);
2957        }
2958        let cfg = &self.cfg;
2959        let _n_embd = cfg.n_embd as usize;
2960        let geometry = cfg.full_attention_geometry_at(il as u32);
2961        let n_head = geometry.n_head as usize;
2962        let n_head_kv = geometry.n_head_kv as usize;
2963        let head_dim = geometry.head_dim_k as usize;
2964        let eps = cfg.rms_eps;
2965        let scale = geometry.attention_scale();
2966
2967        // qwen35: wq output = head_dim*2*n_head (fused [q|gate] per head). M3/Hy3: NO output
2968        // gate — wq out = n_head*head_dim, no split (see prime-path note).
2969        let gated = geometry.attention_gate
2970            == memra_gguf::config::AttentionGateKind::FusedQ;
2971        // grouped: one f16 activation convert feeds q/k/v (matmul_group)
2972        let mut g3 = e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?;
2973        let v = g3.pop().unwrap();
2974        let mut k = g3.pop().unwrap();
2975        let qf = g3.pop().unwrap();
2976        let (mut q, gate) = if gated {
2977            let mut q = e.uninit(t * n_head * head_dim)?;
2978            let mut gate = e.uninit(t * n_head * head_dim)?;
2979            e.q_gate_split(&qf, &mut q, &mut gate, head_dim, n_head, t)?;
2980            (q, Some(gate))
2981        } else {
2982            (qf, None)
2983        };
2984
2985        // QK-norm (per head_dim row), then partial RoPE.
2986        let mut qn = e.uninit(t * n_head * head_dim)?;
2987        e.rms_norm(&q, fa.q_norm.float_data(), &mut qn, head_dim, n_head * t, eps)?;
2988        q = qn;
2989        let mut kn = e.uninit(t * n_head_kv * head_dim)?;
2990        e.rms_norm(&k, fa.k_norm.float_data(), &mut kn, head_dim, n_head_kv * t, eps)?;
2991        k = kn;
2992        let rope_dims = geometry.n_rot as usize;
2993        e.rope_neox(&mut q, pos_d, head_dim, rope_dims, n_head, t, geometry.rope_base, 1.0)?;
2994        e.rope_neox(&mut k, pos_d, head_dim, rope_dims, n_head_kv, t, geometry.rope_base, 1.0)?;
2995
2996        // SDPA
2997        let mut attn = e.uninit(t * n_head * head_dim)?;
2998        // hand-written FlashAttention prefill (head_dim 256/128 stamped twins). MEMRA_NOFA
2999        // falls back to naive sdpa.
3000        if std::env::var("MEMRA_NOFA").is_ok() || !(head_dim == 256 || head_dim == 128) {
3001            // head_dim gate: see prime-path note (fa_prefill is stamped at 256 and 128 only).
3002            e.sdpa_naive(&q, &k, &v, &mut attn, head_dim, n_head, n_head_kv, t, t, scale, true)?;
3003        } else {
3004            e.fa_prefill(&q, &k, &v, &mut attn, head_dim, n_head, n_head_kv, t, t, scale, true)?;
3005        }
3006
3007        // output gate: attn * sigmoid(gate) — qwen35 only (M3 has no gate).
3008        let attn_g = match &gate {
3009            Some(gate) => {
3010                let mut gsig = e.uninit(t * n_head * head_dim)?;
3011                e.sigmoid(gate, &mut gsig, t * n_head * head_dim)?;
3012                let mut ag = e.uninit(t * n_head * head_dim)?;
3013                e.mul(&attn, &gsig, &mut ag, t * n_head * head_dim)?;
3014                ag
3015            }
3016            None => attn,
3017        };
3018
3019        // o projection
3020        let o = e.matmul(&fa.wo, &attn_g, t)?;
3021        Ok(o)
3022    }
3023
3024    /// Linear-attention (Gated DeltaNet) mixer (qwen35 :338-470).
3025    pub fn linear_attn(&self, e: &Engine, la: &LinearAttnLayer, h: &CudaSlice<f32>, t: usize)
3026                   -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3027        let cfg = &self.cfg;
3028        let _n_embd = cfg.n_embd as usize;
3029        let ssm = cfg.ssm.as_ref().unwrap();
3030        let d_state = ssm.state_size as usize;       // 128
3031        let num_k = ssm.group_count as usize;        // 16
3032        let num_v = ssm.time_step_rank as usize;     // 32
3033        let d_conv = ssm.conv_kernel as usize;       // 4
3034        let head_k = d_state; let head_v = d_state;
3035        let key_dim = head_k * num_k;                // 2048
3036        let value_dim = head_v * num_v;              // 4096
3037        let conv_dim = key_dim * 2 + value_dim;      // 8192
3038        let eps = cfg.rms_eps;
3039        let scale = 1.0 / (d_state as f32).sqrt();
3040
3041        // projections
3042        // grouped: one f16 activation convert feeds all four projections (matmul_group)
3043        let mut g4 = e.matmul_group(&[&la.wqkv, &la.wqkv_gate, &la.ssm_beta, &la.ssm_alpha], h, t)?;
3044        let alpha = g4.pop().unwrap();                   // [T, num_v]
3045        let beta_raw = g4.pop().unwrap();                // [T, num_v]
3046        let z = g4.pop().unwrap();                       // [T, value_dim]
3047        let qkv_mixed = g4.pop().unwrap();               // [T, conv_dim] token-major
3048
3049        // conv + GDN repack, FUSED (2026-07-03): ssm_conv1d_gdn reads qkv_mixed [T, conv_dim]
3050        // token-major DIRECTLY (causal window rows t-pad..t, rows<0 = zero prefill state), applies
3051        // the 8-tap conv + SiLU, and scatters straight into the GDN [d_state, num_v, T] q/k/v
3052        // layout with the modulo head-repeat. Replaces transpose + zeros + conv_left_pad +
3053        // ssm_conv1d + qkv_to_gdn_repack (5 launches, conv_in/conv_out scratch + a 16MB@T=512
3054        // round-trip). BIT-IDENTICAL accumulation and scatter mapping.
3055        let _ = (head_k, head_v);
3056        let mut q_g = e.uninit(d_state * num_v * t)?;
3057        let mut k_g = e.uninit(d_state * num_v * t)?;
3058        let mut v_g = e.uninit(d_state * num_v * t)?;
3059        e.ssm_conv1d_gdn(&qkv_mixed, la.ssm_conv1d.float_data(), &mut q_g, &mut k_g, &mut v_g,
3060                         conv_dim, t, d_conv, d_state, num_v, num_k, key_dim)?;
3061        // L2-norm q,k per (head_dim) row — rows are contiguous d_state in q_g.
3062        let mut q_l2 = e.uninit(d_state * num_v * t)?;
3063        e.l2_norm(&q_g, &mut q_l2, d_state, num_v * t, eps)?;
3064        let mut k_l2 = e.uninit(d_state * num_v * t)?;
3065        e.l2_norm(&k_g, &mut k_l2, d_state, num_v * t, eps)?;
3066        let v_gd = v_g;
3067
3068        // beta = sigmoid(beta_raw) ; g_log = a * softplus(alpha + dt). Both need [num_v, T] layout
3069        // (g[t*num_v + h]). beta_raw/alpha are [T, num_v] token-major == that layout already.
3070        let mut beta = e.uninit(t * num_v)?;
3071        e.sigmoid(&beta_raw, &mut beta, t * num_v)?;
3072        // gdn_glog expects alpha [H,T] with alpha[t*H+h] and dt_bias/a [H] — matches token-major [T,num_v].
3073        let mut g_log = e.uninit(t * num_v)?;
3074        e.gdn_glog(&alpha, la.ssm_dt.float_data(), la.ssm_a.float_data(), &mut g_log, num_v, t)?;
3075
3076        // GDN scan (A4: gdn_scan_prefill dispatches chunked WY under MEMRA_GDN_CHUNKED)
3077        let state_in = e.zeros(d_state * d_state * num_v)?;  // zero state (prefill)
3078        let mut state_out = e.zeros(d_state * d_state * num_v)?;
3079        let mut o = e.uninit(d_state * num_v * t)?;
3080        e.gdn_scan_prefill(&q_l2, &k_l2, &v_gd, &g_log, &beta, None, None, &state_in, &mut state_out, &mut o, num_v, t, scale, num_v)?;
3081
3082        // gated RMSNorm: dst = RMSNorm(o, ssm_norm[head_v]) * silu(z). o is [d_state, num_v, T];
3083        // rows of head_v=d_state, nrows = num_v*T. z must match row layout: z is [T, value_dim] token-major
3084        // = [T, num_v*head_v]; per (t, vh) the head_v slice is contiguous -> rows align as (t*num_v+vh).
3085        // o rows are (t*num_v+vh) too. Good.
3086        let mut gn = e.uninit(d_state * num_v * t)?;
3087        e.gated_rmsnorm(&o, la.ssm_norm.float_data(), &z, &mut gn, d_state, num_v * t, eps)?;
3088
3089        // ssm_out projection: gn is [d_state, num_v, T] = [value_dim, T] viewed token-major as [T, value_dim]?
3090        // gn layout: (t*num_v+vh)*d_state + i  == token t, then (vh,i) = channel vh*d_state+i. That's
3091        // token-major [T, value_dim]. linear wants [T, in=value_dim]. Good.
3092        let out = e.matmul(&la.ssm_out, &gn, t)?;
3093        Ok(out)
3094    }
3095}
3096
3097impl HybridModel {
3098    /// MoE FFN (EDGE-1). z: [T, n_embd] (already post-attention-normed). Returns moe_out [T, n_embd].
3099    /// Node-for-node vs llama.cpp build_moe_ffn + qwen35moe::build_layer_ffn.
3100    ///
3101    /// `il` is the trunk layer index — the residency-cache key prefix (a gate-expert of layer 3 is a
3102    /// different 860160-byte block than the same expert of layer 7).
3103    ///
3104    /// Routing: host softmax+sort (default) OR the fused router kernel (MEMRA_FUSED_ROUTER).
3105    /// Dispatch: stage-every-token into 3 scratch slots (default) OR the SLRU residency cache
3106    /// (MEMRA_MOE_CACHE). The cache-HIT weight path is bit-identical to stage-every-token (§B.3).
3107    /// Convenience wrapper used by the hybrid trunk/MTP loops: pulls dims + max-block from `self`.
3108    pub fn moe_ffn_il(&self, e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>, t: usize, il: u16)
3109               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3110        Self::moe_ffn_inner(e, m, z, None, t, &self.cfg, il, self.max_moe_block(), false)
3111    }
3112
3113    /// Prefill twin: Step35 promotes expert-grouped dispatch by default while decode/spec callers
3114    /// keep `moe_ffn_il` and therefore retain their existing dispatch class.
3115    pub fn moe_ffn_il_prefill(
3116        &self,
3117        e: &Engine,
3118        m: &MoeWeights,
3119        z: &CudaSlice<f32>,
3120        t: usize,
3121        il: u16,
3122    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3123        Self::moe_ffn_inner(e, m, z, None, t, &self.cfg, il, self.max_moe_block(), true)
3124    }
3125
3126    /// Decode-path twin with a PRE-QUANTIZED z (from add_rms_norm_zq8): threads (zq, zd) into the
3127    /// t=1 dev arm so the per-layer standalone quantize_q8_1 launch folds away. Identical bytes
3128    /// (the fused kernel reproduces quantize_q8_1 exactly); every other path ignores the pair.
3129    pub fn moe_ffn_il_zq8(&self, e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>,
3130                          zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>, t: usize, il: u16)
3131               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3132        Self::moe_ffn_inner(
3133            e, m, z, zq8, t, &self.cfg, il, self.max_moe_block(), false,
3134        )
3135    }
3136
3137    /// MoE FFN (EDGE-1), source-/model-agnostic. z: [T, n_embd] (already post-attention-normed).
3138    /// Returns moe_out [T, n_embd]. Node-for-node vs llama.cpp build_moe_ffn. Shared by the hybrid
3139    /// (qwen35moe, shared expert present) and the dense-attention MoE (OLMoE, no shared expert) paths;
3140    /// `cfg.moe` supplies the dims and the optional shexp fields decide whether step 3 runs.
3141    ///
3142    /// `il` is the layer index — the residency-cache key prefix. `max_block` is the global max expert
3143    /// stride (fixed cache-slot size); pass `self.max_moe_block()`.
3144    pub(crate) fn moe_ffn(e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>, t: usize,
3145                          cfg: &ModelConfig, il: u16, max_block: usize)
3146               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3147        Self::moe_ffn_inner(e, m, z, None, t, cfg, il, max_block, false)
3148    }
3149
3150    #[allow(clippy::too_many_arguments)]
3151    pub(crate) fn moe_ffn_inner(
3152        e: &Engine,
3153        m: &MoeWeights,
3154        z: &CudaSlice<f32>,
3155        zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>,
3156        t: usize,
3157        cfg: &ModelConfig,
3158        il: u16,
3159        max_block: usize,
3160        prefill: bool,
3161    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3162        let worker_io = crate::spill_pread::worker_enabled();
3163        let epoch_lfu = std::env::var_os("MEMRA_MOE_LFU_DECAY").is_some();
3164        if Engine::moe_cache_enabled() && (worker_io || epoch_lfu) {
3165            e.with_moe_cache(max_block, |cache, _| {
3166                cache.begin_forward_epoch(il, t);
3167                if worker_io {
3168                    cache.begin_worker_scope();
3169                }
3170                Ok(())
3171            })?;
3172        }
3173        // Expert-grouped dispatch for prefill (T>1). MEMRA_MOE_GROUPED explicitly opts the
3174        // current caller into this research arm; the naked default stays on the established path.
3175        if t > 1 && moe_grouped_enabled(cfg, prefill) {
3176            let grouped_out = Self::moe_ffn_grouped(e, m, z, t, cfg, il, max_block)?;
3177            // MEMRA_MOE_GATE: byte-identity comparison vs sequential path.
3178            // The grouped q8 path uses the same row-wise quantize/dot/activation/FMA programs as
3179            // sequential dispatch; mixed or q8-disabled layouts fall back to the matching f32
3180            // path. A mismatch is therefore a correctness failure, not an accepted numeric class.
3181            if std::env::var("MEMRA_MOE_GATE").is_ok() {
3182                let seq_out = Self::moe_ffn_sequential(e, m, z, t, cfg, il, max_block)?;
3183                let g_host = e.dtoh(&grouped_out)?;
3184                let s_host = e.dtoh(&seq_out)?;
3185                let g_bytes: &[u8] = unsafe { std::slice::from_raw_parts(g_host.as_ptr() as *const u8, g_host.len() * 4) };
3186                let s_bytes: &[u8] = unsafe { std::slice::from_raw_parts(s_host.as_ptr() as *const u8, s_host.len() * 4) };
3187                if g_bytes == s_bytes {
3188                    println!("moe-gate il={il} t={t} BYTE-IDENTICAL");
3189                } else {
3190                    let diffs = g_host.iter().zip(s_host.iter()).enumerate()
3191                        .filter(|(_, (a, b))| a != b).count();
3192                    let maxdiff = g_host.iter().zip(s_host.iter())
3193                        .map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max);
3194                    panic!("moe-gate il={il} t={t} MISMATCH: {diffs}/{} elems differ, maxdiff={maxdiff:.6e}", g_host.len());
3195                }
3196            }
3197            return Ok(grouped_out);
3198        }
3199        Self::moe_ffn_sequential_zq8(e, m, z, zq8, t, cfg, il, max_block)
3200    }
3201
3202    /// Sequential (per-token) MoE FFN -- the original path. Factored out for the gate comparison.
3203    pub(crate) fn moe_ffn_sequential(e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>, t: usize,
3204                          cfg: &ModelConfig, il: u16, max_block: usize)
3205               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3206        Self::moe_ffn_sequential_zq8(e, m, z, None, t, cfg, il, max_block)
3207    }
3208
3209    /// One router-logit selector shared by sequential and grouped dispatch. Real prefill must use
3210    /// the row-wise GEMV when exact routing is enabled: cuBLASLt's reduction changes with `m`,
3211    /// which makes expert selection depend on the caller's chunk or concat-batch shape.
3212    fn moe_router_logits(
3213        e: &Engine,
3214        m: &MoeWeights,
3215        z: &CudaSlice<f32>,
3216        t: usize,
3217        cfg: &ModelConfig,
3218    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3219        if t < PRIME_MIN_T {
3220            // Decode and speculative verify use one fixed per-row reduction program.
3221            if crate::router_kernel_on() {
3222                e.router_gemv(
3223                    m.gate_inp.float_data(),
3224                    z,
3225                    cfg.n_embd as usize,
3226                    m.gate_exps.n_expert,
3227                    t,
3228                )
3229            } else {
3230                e.matmul_decode_exact(&m.gate_inp, z, t)
3231            }
3232        } else if crate::router_prefill_exact_on() && crate::router_kernel_on() {
3233            e.router_gemv(
3234                m.gate_inp.float_data(),
3235                z,
3236                cfg.n_embd as usize,
3237                m.gate_exps.n_expert,
3238                t,
3239            )
3240        } else {
3241            e.matmul(&m.gate_inp, z, t)
3242        }
3243    }
3244
3245    /// Append the host-visible router selection for one layer/forward when calibration tracing is
3246    /// enabled. Both sequential and expert-grouped prefill must call this after routing so the
3247    /// trace is independent of the dispatch optimization selected for the forward.
3248    fn trace_moe_routes(il: u16, t: usize, sel_all: &[u32], weights: &[f32])
3249                        -> Result<(), Box<dyn std::error::Error>> {
3250        use std::io::Write as _;
3251        if let Ok(path) = std::env::var("MEMRA_MOE_TRACE") {
3252            let mut f = std::fs::OpenOptions::new().create(true).append(true).open(path)?;
3253            let ids: Vec<String> = sel_all.iter().map(|s| s.to_string()).collect();
3254            writeln!(f, "{} {} {}", il, t, ids.join(","))?;
3255        }
3256        if let Ok(path) = std::env::var("MEMRA_MOE_WEIGHT_TRACE") {
3257            let mut f = std::fs::OpenOptions::new().create(true).append(true).open(path)?;
3258            let pairs: Vec<String> = sel_all.iter().zip(weights)
3259                .map(|(expert, weight)| format!("{expert}:{weight:.9}"))
3260                .collect();
3261            writeln!(f, "{} {} {}", il, t, pairs.join(","))?;
3262        }
3263        Ok(())
3264    }
3265
3266    /// Append the f32 input to one MoE layer for offline, layerwise calibration. This diagnostic
3267    /// intentionally performs a DtoH copy and is therefore disabled unless an explicit fresh trace
3268    /// directory is supplied. Each layer owns one payload file; index.jsonl records byte offsets so
3269    /// a validator can prove request/layer coverage before the trace is used for pruning or healing.
3270    fn trace_moe_input(e: &Engine, il: u16, t: usize, n_embd: usize, z: &CudaSlice<f32>)
3271                       -> Result<(), Box<dyn std::error::Error>> {
3272        use std::io::Write as _;
3273        let Ok(dir) = std::env::var("MEMRA_MOE_INPUT_TRACE_DIR") else { return Ok(()) };
3274        let host = e.dtoh(z)?;
3275        if host.len() != t * n_embd {
3276            return Err(format!(
3277                "MoE input trace shape mismatch at layer {il}: got {} values, expected {}x{}",
3278                host.len(), t, n_embd
3279            ).into());
3280        }
3281        let bytes = unsafe {
3282            std::slice::from_raw_parts(
3283                host.as_ptr().cast::<u8>(), host.len() * std::mem::size_of::<f32>()
3284            )
3285        };
3286        let state = MOE_INPUT_TRACE_WRITER.get_or_init(|| std::sync::Mutex::new(None));
3287        let mut state = state.lock().map_err(|_| "MoE input trace writer lock is poisoned")?;
3288        if state.is_none() {
3289            let dir = std::path::PathBuf::from(&dir);
3290            std::fs::create_dir_all(&dir)?;
3291            let index = std::fs::OpenOptions::new().create(true).append(true)
3292                .open(dir.join("index.jsonl"))?;
3293            *state = Some(MoeInputTraceWriter {
3294                dir,
3295                index,
3296                payloads: std::collections::HashMap::new(),
3297            });
3298        }
3299        let writer = state.as_mut().unwrap();
3300        if writer.dir != std::path::Path::new(&dir) {
3301            return Err("MEMRA_MOE_INPUT_TRACE_DIR changed after capture started".into());
3302        }
3303        let file_name = format!("layer-{il:03}.f32");
3304        if !writer.payloads.contains_key(&il) {
3305            let payload = std::fs::OpenOptions::new().create(true).append(true)
3306                .open(writer.dir.join(&file_name))?;
3307            let offset = payload.metadata()?.len();
3308            writer.payloads.insert(il, (payload, offset));
3309        }
3310        let (payload, offset) = writer.payloads.get_mut(&il).unwrap();
3311        let row_offset = *offset;
3312        payload.write_all(bytes)?;
3313        *offset += bytes.len() as u64;
3314        writeln!(
3315            writer.index,
3316            "{{\"format\":\"memra-moe-input-trace-v1\",\"layer\":{il},\"tokens\":{t},\
3317             \"hidden_size\":{n_embd},\"file\":\"{file_name}\",\"offset\":{row_offset},\
3318             \"payload_bytes\":{}}}",
3319            bytes.len()
3320        )?;
3321        Ok(())
3322    }
3323
3324    #[allow(clippy::too_many_arguments)]
3325    pub(crate) fn moe_ffn_sequential_zq8(
3326        e: &Engine,
3327        m: &MoeWeights,
3328        z: &CudaSlice<f32>,
3329        zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>,
3330        t: usize,
3331        cfg: &ModelConfig,
3332        il: u16,
3333        max_block: usize,
3334    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3335        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
3336        let moe = cfg.moe.as_ref().unwrap();
3337        let n_embd = cfg.n_embd as usize;          // 2048 (gate/up in_f, down out_f)
3338        let n_expert = moe.expert_count as usize;  // 256
3339        let n_used = moe.expert_used_count as usize; // 8
3340        let n_ff_exp = moe.expert_ff_length as usize; // 512 (gate/up out_f, down in_f)
3341
3342        // verify the HostExps dims match cfg (catches a wrong-file / transpose mixup)
3343        debug_assert_eq!(m.gate_exps.in_f, n_embd);
3344        debug_assert_eq!(m.gate_exps.out_f, n_ff_exp);
3345        debug_assert_eq!(m.down_exps.in_f, n_ff_exp);  // down is TRANSPOSED: in=512
3346        debug_assert_eq!(m.down_exps.out_f, n_embd);   //                     out=2048
3347        debug_assert_eq!(m.gate_exps.n_expert, n_expert);
3348
3349        // step35 PER-LAYER SwiGLU clamp (None on every other arch and on every unclamped layer).
3350        // Routed experts and the shared expert read SEPARATE arrays — never share one value.
3351        let lim_exp = cfg.clamp_exp_at(il as u32);
3352        let lim_shexp = cfg.clamp_shexp_at(il as u32);
3353        let use_cache = Engine::moe_cache_enabled();
3354        let uniform_experts = m.has_uniform_expert_layout();
3355        let moe_q8 = uniform_experts && moe_q8_enabled()
3356            && q8_expert_supported(m.gate_exps.qtype) && q8_expert_supported(m.up_exps.qtype)
3357            && q8_expert_supported(m.down_exps.qtype);
3358        // Experimental secondary backend: complete experts already resident in the SLRU stay on
3359        // CUDA; any expert missing one or more projections runs from the original host GGUF bytes
3360        // through llama.cpp CPU quant dots. Small-t only keeps decode and speculative verification
3361        // in the same numeric/dispatch class; real prefill remains on the established GPU path and
3362        // seeds the residency cache. An explicit library path is the build/runtime gate, so naked
3363        // commands and CI have no llama.cpp or OpenMP dependency.
3364        let cpu_expert_requested = crate::cpu_experts::configured();
3365        if cpu_expert_requested && (cfg.hy3.is_none() || cfg.m3.is_some()) {
3366            return Err(std::io::Error::other(
3367                "MEMRA_CPU_EXPERT_LIB is experimental and currently gated to Hy3",
3368            )
3369            .into());
3370        }
3371        let cpu_hybrid = cpu_expert_requested && t < PRIME_MIN_T && m.dev_exps.is_none();
3372        // A dynamic SLRU changes which complete experts run on CUDA versus llama.cpp CPU dots.
3373        // Those backends are each deterministic but are different numeric configurations, so a
3374        // later prefill eviction can change greedy output. Freeze after the first real prefill;
3375        // decode/spec reads the fixed resident set, while later prefill misses use transient GPU
3376        // staging below and cannot change backend assignment.
3377        let freeze_cpu_residency = cpu_expert_requested
3378            && std::env::var("MEMRA_CPU_EXPERT_FREEZE_CACHE").as_deref() == Ok("1");
3379        let caller_warms_before_freeze = std::env::var("MEMRA_CPU_EXPERT_FREEZE_WARMUP_TOKENS")
3380            .ok()
3381            .and_then(|value| value.parse::<usize>().ok())
3382            .is_some_and(|tokens| tokens > 0);
3383        if cpu_hybrid && freeze_cpu_residency && !caller_warms_before_freeze {
3384            e.freeze_moe_cache();
3385        }
3386        let cache_frozen = use_cache && e.moe_cache_frozen();
3387        let cache_dispatch = use_cache && (!cache_frozen || cpu_hybrid);
3388
3389        // 1. ROUTER: one selector is shared with expert-grouped prefill so changing dispatch
3390        // cannot change logits, selected expert ids, or routing weights.
3391        let logits = Self::moe_router_logits(e, m, z, t, cfg)?;
3392
3393        // LAUNCH-STRUCTURE STAGE 3 (2026-07-05, MEMRA_MOE_DEV default ON, =0 rollback): ZERO-DtoH
3394        // device-dispatch when this layer's expert blocks are ALL cache-resident. The fused
3395        // router's sel/w stay ON DEVICE; the expert weight pointers come from the per-layer
3396        // device table of fixed slot addresses; gate/up/silu + down/fma run as the same TWO
3397        // launches per token as gdec. Removes the per-layer router DtoH + stream sync — the
3398        // per-token host stall that dominated the 35B decode wall after stages 1+2.
3399        // BIT-IDENTITY: the router kernel is selection-exact vs the host oracle (kernel-check
3400        // tie gate) and the _dev matvec twins reproduce the gdec kernels' exact FP chains; the
3401        // only difference is where sel/w/pointers are READ from (device instead of params).
3402        // Residency: one-shot PREWARM force-admits the layer while free slots cover it
3403        // (MEMRA_MOE_PREWARM=0 -> organic residency, dev path fires when the SLRU fills).
3404        // Any non-resident layer falls through to host routing + the gdec/sequential path.
3405        // FITS-VRAM RESIDENT EXPERTS (2026-07-06): the layer's expert slabs are device-resident
3406        // (load-time decision) — fire the zero-DtoH dev path unconditionally with the prebuilt
3407        // pointer row. No cache, no dispatch, no residency check: the llama full-offload regime
3408        // (it measured 169.55 vs the cache path's 28.5 on the local 35B — the residency-gate
3409        // all-or-nothing fallback was the 6x). BIT-IDENTITY: same _dev kernels, same math; only
3410        // the pointer table's provenance differs (slab base+stride vs SLRU slot addresses).
3411        // MoE PREFILL PAIR-BATCH (2026-07-06, the 16x pp hole): t>1 on resident experts — ONE
3412        // launch per proj covers ALL (token,expert) pairs (grid.y=pair, warp-per-row), replacing
3413        // the per-expert loop (256 experts x 3-4 launches x tiny m_e). Scatter is slot-ordered
3414        // per token (the sequential-axpy bit-identity class). Requires q8-supported qtypes +
3415        // resident slabs. MEMRA_MOE_PAIRS=0 rollback.
3416        // t >= PRIME_MIN_T only (2026-07-06 exactness fix, verify-probe proof): spec VERIFY
3417        // batches (t = 2..K+2) previously rode these pairs kernels while T=1 decode rode the
3418        // dev_q8 loop — different FP chains, verify-T2 logit maxdiff 2.6e-1 vs eager -> greedy
3419        // flips at tight margins -> 35B real-prompt spec self-consistency FAIL (the 27B
3420        // "verify must be kernel-DISPATCH-identical to decode" lesson, MoE edition). Small-t
3421        // now rides the dev loop below (same kernels per token as decode); pairs serves real
3422        // prefill (t >= 16, where spec never verifies).
3423        // sigmoid-router archs (M3, Hy3) must NOT enter the pairs/dev arms: those route via the
3424        // fused SOFTMAX device router (moe_router_topk) — silently wrong experts (the M3
3425        // gate-MISMATCH 74602-vs-92 lesson, 2026-07-07). Host sigmoid routing below is correct.
3426        // Per-expert macro-scales (compressed-tensors NVFP4 ST class, e.g. unsloth 35B-A3B):
3427        // the device-dispatch kernels (pairs/dev) do NOT fold them — those checkpoints must
3428        // ride the macro-aware sequential/staged paths below or every expert output is off by
3429        // its global scale (~3e4x, measured garbage 2026-07-16). GGUF experts: macros None.
3430        let no_exp_macros = m.gate_exps.macros.is_none() && m.up_exps.macros.is_none()
3431            && m.down_exps.macros.is_none();
3432        // A clamped-SwiGLU layer (step35 43/44) also cannot ride pairs: moe_pairs_silu_mul's
3433        // epilogue is plain silu(gate)*up with no clamp form, and moe_ffn_pairs takes no `il`
3434        // so it cannot even see the per-layer limit.
3435        if cfg.sigmoid_router().is_none() && cfg.m3.is_none() && cfg.hy3.is_none()
3436            && !cfg.swiglu_clamped_at(il as u32)
3437            && no_exp_macros
3438            && t >= PRIME_MIN_T && m.dev_exps.is_some() && moe_q8_enabled()
3439            && q8_expert_supported(m.gate_exps.qtype) && q8_expert_supported(m.up_exps.qtype)
3440            && q8_expert_supported(m.down_exps.qtype)
3441            && std::env::var("MEMRA_MOE_PAIRS").map(|v| v != "0").unwrap_or(true)
3442            && std::env::var("MEMRA_MOE_STATS").is_err() {
3443            return Self::moe_ffn_pairs(e, m, z, &logits, t, cfg);
3444        }
3445
3446        // t < PRIME_MIN_T: moe_ffn_dev loops tokens serially (1 launch-pair per token) — the
3447        // decode path AND the spec-verify path (dispatch parity = exactness; see pairs gate
3448        // above). Serial launches are fine at t<=10 (K+2); real prefill never lands here.
3449        // moe_ffn_dev routes via the FUSED SOFTMAX device router (moe_router_topk) — sigmoid
3450        // routing (M3, Hy3: +expert bias) has no device kernel yet, so those arches must NOT
3451        // enter the dev arms: with MOE_CACHE=1 M3 silently routed softmax = wrong experts
3452        // (gate MISMATCH 74602 vs 92, caught 2026-07-07). Host sigmoid path below is correct.
3453        // macro-carrying experts are handled inside the dev path now (epilogue fold + w-scale);
3454        // pairs/gdec/csr keep their macro gates until their kernels grow the fold.
3455        // step35 (2026-08-06) is the third sigmoid-router arch and the deny above was written as
3456        // two arch names, not as the mechanism — so `dev_ok` let it into moe_ffn_dev's
3457        // moe_router_topk (softmax, no exp_probs_b bias, no expert_weights_scale) = silently
3458        // wrong experts, the same failure the pairs gate at :2254 already blocks by predicate.
3459        // Keyed off sigmoid_router() so arch #4 is denied by construction.
3460        // The clamped-SwiGLU layers must also fall through: every moe_ffn_dev kernel's fused
3461        // epilogue is plain silu(gate)*up (moe_gate_up_silu8_dev_*), which has no clamp form.
3462        let dev_ok = uniform_experts && cfg.sigmoid_router().is_none()
3463            && cfg.m3.is_none() && cfg.hy3.is_none()
3464            && !cfg.swiglu_clamped_at(il as u32);
3465        // Observation modes must route through the host-visible selection below. Otherwise a fully
3466        // resident layer returns through device dispatch before its trace/stats row is recorded,
3467        // silently biasing calibration toward only non-resident layers on large-VRAM machines.
3468        let observe_routes = std::env::var("MEMRA_MOE_STATS").is_ok()
3469            || std::env::var("MEMRA_MOE_TRACE").is_ok()
3470            || std::env::var("MEMRA_MOE_WEIGHT_TRACE").is_ok()
3471            || std::env::var("MEMRA_MOE_INPUT_TRACE_DIR").is_ok();
3472        if dev_ok && t < PRIME_MIN_T && m.dev_exps.is_some() && n_used <= 8 && moe_dev_enabled()
3473            && !observe_routes {
3474            return Self::moe_ffn_dev(e, m, z, zq8, &logits, t, cfg, il, max_block);
3475        }
3476        if dev_ok && use_cache && n_used <= 8 && moe_dev_enabled()
3477            && !observe_routes {
3478            let row_ok = e.with_moe_cache(max_block, |c, eng| {
3479                if moe_prewarm_enabled() { c.prewarm_layer(il, m, eng)?; }
3480                Ok(c.layer_dev_row(il, n_expert, eng)?.is_some())
3481            })?;
3482            if row_ok {
3483                return Self::moe_ffn_dev(e, m, z, zq8, &logits, t, cfg, il, max_block);
3484            }
3485        }
3486
3487        // Per-token (sel[8], w[8]) — sigmoid host oracle (M3/Hy3), else fused-router/softmax.
3488        let (sel_all, w_all, routed_cpu_input) = if let Some(sig) = cfg.sigmoid_router() {
3489            if cpu_hybrid {
3490                let (sel, w, input) = Self::moe_route_sigmoid_with_input(
3491                    e,
3492                    &logits,
3493                    z,
3494                    t,
3495                    n_expert,
3496                    n_used,
3497                    m.exp_probs_b.as_deref(),
3498                    sig,
3499                    m.active_experts.as_deref(),
3500                )?;
3501                (sel, w, Some(input))
3502            } else {
3503                let (sel, w) = Self::moe_route_cfg(
3504                    e,
3505                    &logits,
3506                    t,
3507                    n_expert,
3508                    n_used,
3509                    m.exp_probs_b.as_deref(),
3510                    Some(sig),
3511                    m.active_experts.as_deref(),
3512                )?;
3513                (sel, w, None)
3514            }
3515        } else {
3516            let (sel, w) = Self::moe_route_cfg(
3517                e,
3518                &logits,
3519                t,
3520                n_expert,
3521                n_used,
3522                None,
3523                None,
3524                m.active_experts.as_deref(),
3525            )?;
3526            (sel, w, None)
3527        };
3528
3529        // MEMRA_MOE_TRACE=<path>: append one line per (layer, step) with the selected expert ids —
3530        // offline analysis derives the decode working set + step-to-step reuse (the go/no-go
3531        // measurement for resident-expert tiering; see rig5090.jsonl 2026-07-07 pinned-tier row).
3532        Self::trace_moe_routes(il, t, &sel_all, &w_all)?;
3533        Self::trace_moe_input(e, il, t, n_embd, z)?;
3534
3535        // Worker reads ordinarily overlap NVMe with the current expert but leave their H2D copies
3536        // demand-serialized on the compute stream. Hy3 decode already has a safe owner-thread
3537        // boundary here: sigmoid routing read `logits` back to the host, which synchronized all
3538        // earlier-layer compute. Submit the complete selected set first, then reserve only victims
3539        // outside that set and queue its H2D copies on the copy stream. Dispatch inserts an event
3540        // wait for each pending block, so later copies can overlap the earlier expert kernels while
3541        // the selected weights and numerical kernels remain unchanged. Restrict this experiment to
3542        // T=1; batched forwards can have token-local consumers still in flight between selections.
3543        // The CPU backend reads mmap-backed model bytes directly. Positioned-read worker buffers
3544        // are CUDA write-combined staging allocations and are intentionally not CPU-compute inputs;
3545        // do not spend NVMe bandwidth filling them for a layer whose misses go to CPU.
3546        let worker_disk_prefetch =
3547            cache_dispatch && crate::spill_pread::worker_enabled() && !cpu_hybrid;
3548        let promote_worker_h2d =
3549            t == 1 && worker_disk_prefetch && crate::spill_pread::copy_h2d_enabled();
3550        if promote_worker_h2d {
3551            let mut selected_blocks = Vec::with_capacity(n_used * 3);
3552            for &ex in sel_all.iter().take(n_used) {
3553                let ex = ex as u16;
3554                selected_blocks.extend([
3555                    BlockId::new(il, PROJ_GATE, ex),
3556                    BlockId::new(il, PROJ_UP, ex),
3557                    BlockId::new(il, PROJ_DOWN, ex),
3558                ]);
3559            }
3560            for &ex in sel_all.iter().take(n_used) {
3561                Self::moe_prefetch_disk_expert(e, il, ex as usize, m, max_block, &selected_blocks)?;
3562            }
3563            e.with_moe_cache(max_block, |cache, eng| {
3564                cache.promote_worker_reads_at_safe_boundary(
3565                    &selected_blocks,
3566                    &selected_blocks,
3567                    eng,
3568                )?;
3569                Ok(())
3570            })?;
3571        }
3572
3573        // MEMRA_MOE_STATS: per-layer routing stats for the A2 (expert-grouped prefill) baseline —
3574        // per-token expert-id entropy, active-expert coverage, tokens-per-expert group sizes.
3575        if t > 1 && std::env::var("MEMRA_MOE_STATS").is_ok() {
3576            let mut cnt = vec![0u32; n_expert];
3577            for &s in sel_all.iter() { cnt[s as usize] += 1; }
3578            let total = sel_all.len() as f64;
3579            let mut h = 0.0f64;
3580            let mut active = 0usize;
3581            for &c in &cnt { if c > 0 { active += 1; let p = c as f64 / total; h -= p * p.log2(); } }
3582            let maxc = cnt.iter().copied().max().unwrap_or(0);
3583            println!("moe-stats il={} t={} assignments={} active={}/{} entropy={:.3}b (max {:.3}b) mean_tok_per_active={:.2} max_tok_per_expert={}",
3584                     il, t, sel_all.len(), active, n_expert, h, (n_expert as f64).log2(), total / active.max(1) as f64, maxc);
3585        }
3586
3587        // LAUNCH-STRUCTURE STAGE 2 (2026-07-05): moe_out memset elision on the gdec path.
3588        // moe_down8_fma_f32 FULLY overwrites its token row (dst[o] = the in-kernel FMA chain that
3589        // starts at 0.0f — numerically the axpy-into-zeroed-row chain), so when the grouped-decode
3590        // path fires the upfront `e.zeros(t*n_embd)` memset is pure launch churn. Allocate uninit
3591        // when gdec CAN fire (any t — decode t=1 AND the spec verify t=K+1 route here per token)
3592        // and lazily zero ONLY the row of a token that falls through to the sequential axpy loop.
3593        // BIT-IDENTITY: unchanged — every row is either fully overwritten (gdec) or
3594        // zeroed-then-accumulated exactly as before (fallback).
3595        // `!swiglu_clamped_at`: the grouped-decode kernels' fused epilogue is plain
3596        // silu(gate)*up (see the m3 note at the call sites below) — a clamped layer must fall
3597        // through to the sequential loop's ffn_act_lim. Hoisted into the fire predicate so the
3598        // uninit/memset invariant at :2418 stays consistent with what actually dispatches.
3599        let gdec_may_fire = uniform_experts && use_cache && n_used <= 8 && gdec_enabled()
3600            && !cfg.swiglu_clamped_at(il as u32);
3601        // SLAB-LOCAL RESIDENT ARM (lane/pp-leverb 2026-08-08): the fits-VRAM resident slabs
3602        // (`dev_exps`, sized per PP device by cx-503b) were consumed ONLY by the pairs/dev
3603        // arms — every one of which DENIES sigmoid-router archs (step35/M3/Hy3). So on those
3604        // archs the slabs were uploaded but never read, and every expert went through the
3605        // SLRU (37 GB H2D staging per pp4096 prime on the Step SKU; and post-cx-503b the
3606        // dead slabs additionally STARVE the SLRU, which sizes itself on free-VRAM-after-
3607        // residents). This arm reads the slabs through the SAME kernels the SLRU arm runs —
3608        // gdec's fused pair when eligible, the per-expert qmatvec twins otherwise — with the
3609        // slab base + ex*stride as the pointer provenance (the exact bit-identity class
3610        // `moe_ffn_dev` documents for its resident-vs-SLRU arms; bytes are the same HostExps
3611        // bytes either way). LOCALITY GATE: `d.dev == e.ctx().ordinal()` — a slab on another
3612        // device must NOT be dereferenced (m=1 peer reads are the measured 34-150x class,
3613        // strictly worse than staging); under PP-2 without the prime walker this admits
3614        // stage-0 layers on dev0 and leaves stage-1 layers on the SLRU, which is the honest
3615        // interim shape. MEMRA_MOE_SLAB=0 = rollback/A-B seam (read per call).
3616        let slab_local = m.dev_exps.as_ref()
3617            .filter(|d| !d.gu_il && moe_slab_enabled() && d.dev == e.ctx().ordinal());
3618        let slab_bases = slab_local.map(|d| {
3619            use cudarc::driver::DevicePtr;
3620            let s = e.stream();
3621            let (pg, _g0) = d.gate.device_ptr(&s);
3622            let (pu, _g1) = d.up.device_ptr(&s);
3623            let (pd, _g2) = d.down.device_ptr(&s);
3624            (pg as u64, pu as u64, pd as u64)
3625        });
3626        // Fused-pair eligibility mirrors the gdec call sites exactly (plain-SiLU epilogue,
3627        // no macros, m3 clamp excluded, q8-supported qtypes, <=8 experts) — INCLUDING
3628        // `gdec_enabled()`: the pair IS the gdec kernel pair, so MEMRA_MOE_GDEC=0 must
3629        // disable it here too. That seam is also the exactness localizer: with GDEC=0 both
3630        // provenances run the SAME per-expert qmatvec kernels (slab base+stride vs SLRU
3631        // slot) and must be BIT-IDENTICAL — the true provenance-only pair — while GDEC=1
3632        // compares the fused-pair class against the SLRU's hit/miss MIX (gdec for
3633        // all-resident tokens, staged loop for misses), which is a dispatch-class
3634        // comparison, not a provenance one.
3635        let slab_fused_may_fire = slab_bases.is_some() && n_used <= 8 && gdec_enabled()
3636            && !cfg.swiglu_clamped_at(il as u32) && cfg.m3.is_none()
3637            && no_exp_macros && moe_q8;
3638        // moe_out memset elision: BOTH full-row-overwrite arms (gdec + slab fused) allocate
3639        // uninit; a token that falls through to any accumulating loop zeroes its own row.
3640        let mut moe_out = if gdec_may_fire || slab_fused_may_fire {
3641            e.uninit(t * n_embd)?
3642        } else {
3643            e.zeros(t * n_embd)?
3644        };
3645        // The router readback above already established a host boundary. Copy each small-t hidden
3646        // row once so CPU miss experts can start while the owner thread queues resident GPU work.
3647        let cpu_input = if cpu_hybrid {
3648            Some(routed_cpu_input.ok_or("CPU expert routing did not return the MoE input")?)
3649        } else {
3650            None
3651        };
3652
3653        // GPU scratch: one slot per proj, big enough for ONE expert (default stage-every-token path).
3654        // STAGE 2: LAZY — allocated only if the no-cache staging path actually runs (under
3655        // MEMRA_MOE_CACHE they were 3 dead ~1MB alloc_zeros + memset + free per layer per token,
3656        // measured ~123 memsets/token of the decode wall).
3657        let g_len = m.gate_exps.max_expert_bytes();  // 860160 for the uniform 35B gate
3658        let u_len = m.up_exps.max_expert_bytes();    // 860160 for the uniform 35B up
3659        let d_len = m.down_exps.max_expert_bytes();  // 1114112 for the uniform 35B down
3660        let mut scratch_g: Option<CudaSlice<u8>> = None;
3661        let mut scratch_u: Option<CudaSlice<u8>> = None;
3662        let mut scratch_d: Option<CudaSlice<u8>> = None;
3663        // `max_block` (the GLOBAL max expert stride across all layers) is passed in — the cache slots
3664        // are FIXED-ADDRESS and must fit any layer's block (UD/dynamic GGUFs vary quant per layer).
3665
3666        // EDGE-1 §C.2/C.3: the optional pipeline queues the next selected expert's MISS blocks on
3667        // the copy stream before launching the current expert's compute. Pending slots stay invisible
3668        // to cache hits until dispatch inserts the completion-event wait; current gate/up/down ids are
3669        // protected from eviction. This changes scheduling only, never the GGUF bytes or GEMM path.
3670        let page_window = moe_page_prefetch_window();
3671
3672        // 2. PER TOKEN: routed-expert loop. The ONE dispatch change vs Stage-1: a resident slot
3673        //    (cache HIT, no H2D) OR a staged slot (MISS) feeds the SAME unchanged qmatvec_view.
3674        for tok in 0..t {
3675            let sel = &sel_all[tok * n_used..(tok + 1) * n_used];
3676            let w = &w_all[tok * n_used..(tok + 1) * n_used];
3677            let zt = z.slice(tok * n_embd..(tok + 1) * n_embd);  // CudaView<f32>
3678            let mut tok_q8: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
3679
3680            // STAGE-2 GROUPED DECODE (2026-07-04, MEMRA_MOE_GDEC default ON, =0 rollback): fold
3681            // this token's whole routed-expert FFN (8x gate/up/silu + 8x down/axpy = 40 launches)
3682            // into TWO launches via expert-pointer indirection over the fixed-address cache slots.
3683            // Fires only when ALL 3*n_used blocks are ALREADY cache-resident (pure-HIT: zero
3684            // memcpy, zero admission, so no slot can move under the collected pointers) — any
3685            // miss falls through to the sequential loop below, which admits as before. In steady
3686            // state on a fully-resident rig every token-layer takes the grouped path.
3687            // BIT-IDENTITY: each in-kernel dot reproduces qmatvec_f32's exact reduction; SiLU is
3688            // silu_mul_f32's exact expression; the down accumulation is a slot-ordered
3689            // __fmaf_rn chain == the sequential axpy_f32 chain (MEMRA_MOE_GDEC_GATE compares).
3690            // cfg.m3: the grouped kernels' fused epilogues are plain SiLU — M3's swigluoai must
3691            // NOT take them until the kernels grow the clamped variant. NVFP4 experts carry
3692            // per-expert macro-scales the fused kernels don't fold — those fall through too.
3693            let no_macros = m.gate_exps.macros.is_none() && m.up_exps.macros.is_none()
3694                && m.down_exps.macros.is_none();
3695            // SLAB-LOCAL FUSED PAIR (lane/pp-leverb 2026-08-08): the gdec launch pair
3696            // (moe_gate_up_silu8_q8 + moe_down8_fma_q8 — the EXACT kernels, same FP chains)
3697            // with pointers computed from the resident slab base + ex*stride instead of
3698            // collected SLRU slot addresses. No cache lock, no residency predicate — the
3699            // slab holds every expert by construction, so this arm never falls through
3700            // (gdec's P(all 24 resident) ≈ 0.37 coin-flip and the miss path's ~49-launch
3701            // staging both die). Bit-identity class: pointer provenance only, the same
3702            // slab-vs-SLRU equivalence moe_ffn_dev documents. Ordered BEFORE gdec: when a
3703            // slab exists it is strictly better (no lock, no miss).
3704            if slab_fused_may_fire {
3705                let (pg, pu, pd) = slab_bases.unwrap();
3706                let mut gp = [0u64; 8];
3707                let mut up = [0u64; 8];
3708                let mut dp = [0u64; 8];
3709                for (j, &ex) in sel.iter().enumerate() {
3710                    let ex = ex as usize;
3711                    gp[j] = pg + (ex * m.gate_exps.expert_stride) as u64;
3712                    up[j] = pu + (ex * m.up_exps.expert_stride) as u64;
3713                    dp[j] = pd + (ex * m.down_exps.expert_stride) as u64;
3714                }
3715                let mut wv = [0f32; 8];
3716                wv[..n_used].copy_from_slice(w);
3717                if tok_q8.is_none() {
3718                    tok_q8 = Some(e.quantize_q8_1_view(&zt, 1, n_embd)?);
3719                }
3720                let (zq, zd) = tok_q8.as_ref().unwrap();
3721                let act = e.moe_gate_up_silu8_q8(crate::WPtr8(gp), crate::WPtr8(up), zq, zd,
3722                                                 n_embd, n_ff_exp, n_used,
3723                                                 m.gate_exps.qtype, m.up_exps.qtype,
3724                                                 m.gate_exps.row_bytes, m.up_exps.row_bytes)?;
3725                let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
3726                let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
3727                e.moe_down8_fma_q8(crate::WPtr8(dp), crate::F32x8(wv), &aq2, &ad2, &mut dst,
3728                                   n_ff_exp, n_embd, n_used,
3729                                   m.down_exps.qtype, m.down_exps.row_bytes)?;
3730                continue;
3731            }
3732            if gdec_may_fire && moe_q8 && cfg.m3.is_none() && no_macros {
3733                if tok_q8.is_none() {
3734                    tok_q8 = Some(e.quantize_q8_1_view(&zt, 1, n_embd)?);
3735                }
3736                let (zq, zd) = tok_q8.as_ref().unwrap();
3737                if Self::moe_gdec_token_q8(e, m, il, max_block, zq, zd, sel, w,
3738                                           &mut moe_out, tok, n_embd, n_ff_exp, n_used)? {
3739                    continue;
3740                }
3741            } else if gdec_may_fire && cfg.m3.is_none() && no_macros
3742                && Self::moe_gdec_token(e, m, il, max_block, &zt, sel, w,
3743                                        &mut moe_out, tok, n_embd, n_ff_exp, n_used)? {
3744                continue;
3745            }
3746
3747            // STAGE 2 memset-elision invariant: moe_out was allocated UNINIT when gdec or the
3748            // slab pair could fire. This token fell through to a sequential axpy loop, which
3749            // ACCUMULATES — zero its row first (row-sized memset; other rows are owned by the
3750            // full-overwrite arms). slab_fused_may_fire never actually reaches here (its arm
3751            // has no fallible predicate), included for the allocation invariant's symmetry.
3752            if gdec_may_fire || slab_fused_may_fire {
3753                let mut row = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
3754                e.memset_zeros_view(&mut row)?;
3755            }
3756
3757            // Fiddler-style split at whole-expert granularity. Partial residency is deliberately a
3758            // CPU assignment: transferring the missing projection would reintroduce the PCIe/NVMe
3759            // stall this path exists to remove, while mixing projections would require another
3760            // activation round-trip. Weight addresses remain valid until this worker is joined at
3761            // the bottom of the token scope.
3762            let mut cpu_mask = vec![false; sel.len()];
3763            let cpu_worker = if let Some(host_input) = cpu_input.as_ref() {
3764                let gpu_resident = if use_cache {
3765                    e.with_moe_cache(max_block, |cache, _| {
3766                        Ok(sel
3767                            .iter()
3768                            .map(|&expert| {
3769                                let expert = expert as u16;
3770                                [PROJ_GATE, PROJ_UP, PROJ_DOWN]
3771                                    .into_iter()
3772                                    .filter(|&projection| {
3773                                        cache
3774                                            .resident(BlockId::new(il, projection, expert))
3775                                            .is_some()
3776                                    })
3777                                    .count()
3778                            })
3779                            .collect::<Vec<_>>())
3780                    })?
3781                } else {
3782                    vec![0; sel.len()]
3783                };
3784                let mut cpu_selected = Vec::new();
3785                for (index, (&expert, &route_weight)) in sel.iter().zip(w).enumerate() {
3786                    if gpu_resident[index] != 3 {
3787                        cpu_mask[index] = true;
3788                        crate::cpu_experts::record_incomplete_gpu_residency(gpu_resident[index]);
3789                        let expert = expert as usize;
3790                        cpu_selected.push((expert, route_weight));
3791                    }
3792                }
3793                if crate::cpu_experts::predictor_enabled() {
3794                    // Fire-and-forget lookahead: the predictor worker scores layers il+1..
3795                    // from this layer's MoE input and prefetches predicted-and-missing
3796                    // experts into the companion RAM cache. Never blocks this thread.
3797                    let row = &host_input[tok * n_embd..(tok + 1) * n_embd];
3798                    crate::cpu_experts::predictor_submit(il, row);
3799                }
3800                if cpu_selected.is_empty() {
3801                    None
3802                } else {
3803                    let row = &host_input[tok * n_embd..(tok + 1) * n_embd];
3804                    let job = crate::cpu_experts::prepare_job(m, il, &cpu_selected, row)
3805                        .map_err(std::io::Error::other)?;
3806                    Some(crate::cpu_experts::submit(job).map_err(std::io::Error::other)?)
3807                }
3808            } else {
3809                None
3810            };
3811
3812            let worker_window = worker_disk_prefetch
3813                .then(worker_prefetch_window)
3814                .unwrap_or(0);
3815            for (j, &ex) in sel.iter().enumerate() {
3816                if cpu_mask[j] {
3817                    continue;
3818                }
3819                let ex = ex as usize;
3820                // PER-EXPERT SLAB READ (lane/pp-leverb 2026-08-08): the layers the fused
3821                // pair above excludes — step35's CLAMPED layers 43/44 (ffn_act_lim has no
3822                // fused form) and macro-carrying artifacts — still have their bytes in the
3823                // local resident slab. Same kernels as the SLRU arms (`qmatvec_expert_q8` /
3824                // `qmatvec_view`), same ffn_act_lim/macro folds; provenance = slab base +
3825                // ex*stride. No dispatch lock, no admission, no prefetch — nothing to miss.
3826                if let Some(d) = slab_local {
3827                    let gl = m.gate_exps.expert_layout(ex);
3828                    let ul = m.up_exps.expert_layout(ex);
3829                    let dl = m.down_exps.expert_layout(ex);
3830                    let (g0, u0, d0) = (ex * m.gate_exps.expert_stride,
3831                                        ex * m.up_exps.expert_stride,
3832                                        ex * m.down_exps.expert_stride);
3833                    let (gate, up) = if moe_q8 {
3834                        if tok_q8.is_none() {
3835                            tok_q8 = Some(e.quantize_q8_1_view(&zt, 1, n_embd)?);
3836                        }
3837                        let (zq, zd) = tok_q8.as_ref().unwrap();
3838                        (e.qmatvec_expert_q8(&d.gate, g0..g0 + gl.len, zq, zd, 1,
3839                                             m.gate_exps.in_f, m.gate_exps.out_f,
3840                                             gl.qtype, gl.row_bytes)?,
3841                         e.qmatvec_expert_q8(&d.up, u0..u0 + ul.len, zq, zd, 1,
3842                                             m.up_exps.in_f, m.up_exps.out_f,
3843                                             ul.qtype, ul.row_bytes)?)
3844                    } else {
3845                        (e.qmatvec_view(&d.gate, g0..g0 + gl.len, &zt, 1,
3846                                        m.gate_exps.in_f, m.gate_exps.out_f,
3847                                        gl.qtype, gl.row_bytes)?,
3848                         e.qmatvec_view(&d.up, u0..u0 + ul.len, &zt, 1,
3849                                        m.up_exps.in_f, m.up_exps.out_f,
3850                                        ul.qtype, ul.row_bytes)?)
3851                    };
3852                    let mut act = e.uninit(n_ff_exp)?;
3853                    Self::ffn_act_lim(e, cfg, &gate, &up, m.gate_exps.macro_scale(ex),
3854                                      m.up_exps.macro_scale(ex), lim_exp, &mut act, n_ff_exp)?;
3855                    let y = if moe_q8 {
3856                        let (aq2, ad2) = e.quantize_q8_1(&act, 1, n_ff_exp)?;
3857                        e.qmatvec_expert_q8(&d.down, d0..d0 + dl.len, &aq2, &ad2, 1,
3858                                            m.down_exps.in_f, m.down_exps.out_f,
3859                                            dl.qtype, dl.row_bytes)?
3860                    } else {
3861                        let actv = act.slice(0..n_ff_exp);
3862                        e.qmatvec_view(&d.down, d0..d0 + dl.len, &actv, 1,
3863                                       m.down_exps.in_f, m.down_exps.out_f,
3864                                       dl.qtype, dl.row_bytes)?
3865                    };
3866                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
3867                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
3868                    continue;
3869                }
3870                for next in page_prefetch_positions(j, sel.len(), page_window) {
3871                    Self::moe_prefetch_host_expert(sel[next] as usize, m);
3872                }
3873                let keep = [
3874                    crate::moe_cache::BlockId::new(il, crate::moe_cache::PROJ_GATE, ex as u16),
3875                    crate::moe_cache::BlockId::new(il, crate::moe_cache::PROJ_UP, ex as u16),
3876                    crate::moe_cache::BlockId::new(il, crate::moe_cache::PROJ_DOWN, ex as u16),
3877                ];
3878                if worker_disk_prefetch && worker_window > 0 {
3879                    for next in worker_prefetch_positions(j, sel.len(), worker_window) {
3880                        Self::moe_prefetch_disk_expert(
3881                            e,
3882                            il,
3883                            sel[next] as usize,
3884                            m,
3885                            max_block,
3886                            &keep,
3887                        )?;
3888                    }
3889                } else if cache_dispatch
3890                    && !cpu_hybrid
3891                    && moe_prefetch_enabled()
3892                    && j + 1 < sel.len()
3893                {
3894                    let next = sel[j + 1] as usize;
3895                    Self::moe_prefetch_expert(e, il, next, m, max_block, &keep)?;
3896                }
3897                let [gate_q8, up_q8, down_q8] = [moe_q8; 3];
3898                if cache_dispatch && (gate_q8 || up_q8 || down_q8) {
3899                    // dp4a EXPERT PATH (MEMRA_MOE_Q8): quantize z-row once per token. Mixed expert
3900                    // layouts stay on the metadata-aware f32 path.
3901                    if (gate_q8 || up_q8) && tok_q8.is_none() {
3902                        tok_q8 = Some(e.quantize_q8_1_view(&zt, 1, n_embd)?);
3903                    }
3904                    let gate = if gate_q8 {
3905                        let (zq, zd) = tok_q8.as_ref().unwrap();
3906                        Self::moe_cached_gemm_q8(e, il, PROJ_GATE, ex, m, max_block, zq, zd)?
3907                    } else {
3908                        Self::moe_cached_gemm(e, il, PROJ_GATE, ex, m, max_block, &zt)?
3909                    };
3910                    let up = if up_q8 {
3911                        let (zq, zd) = tok_q8.as_ref().unwrap();
3912                        Self::moe_cached_gemm_q8(e, il, PROJ_UP, ex, m, max_block, zq, zd)?
3913                    } else {
3914                        Self::moe_cached_gemm(e, il, PROJ_UP, ex, m, max_block, &zt)?
3915                    };
3916                    let mut act = e.uninit(n_ff_exp)?;
3917                    Self::ffn_act_lim(
3918                        e,
3919                        cfg,
3920                        &gate,
3921                        &up,
3922                        m.gate_exps.macro_scale(ex),
3923                        m.up_exps.macro_scale(ex),
3924                        lim_exp,
3925                        &mut act,
3926                        n_ff_exp,
3927                    )?;
3928                    let y = if down_q8 {
3929                        let (aq2, ad2) = e.quantize_q8_1(&act, 1, n_ff_exp)?;
3930                        Self::moe_cached_gemm_q8(e, il, PROJ_DOWN, ex, m, max_block, &aq2, &ad2)?
3931                    } else {
3932                        let actv = act.slice(0..n_ff_exp);
3933                        Self::moe_cached_gemm(e, il, PROJ_DOWN, ex, m, max_block, &actv)?
3934                    };
3935                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
3936                    // down-proj macro folds into the accumulate weight (1.0 for non-macro archs).
3937                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
3938                } else if cache_dispatch {
3939                    // SLRU residency cache: per-projection, dispatch the block (HIT => resident slot,
3940                    // MISS => staged slot) then run the SAME unchanged qmatvec_view from that slot.
3941                    // The bytes the kernel reads are byte-for-byte the same GGUF block (§B.3); the
3942                    // only difference between HIT and MISS is whether the memcpy_htod ran.
3943                    let gate = Self::moe_cached_gemm(e, il, PROJ_GATE, ex, m, max_block, &zt)?;
3944                    let up   = Self::moe_cached_gemm(e, il, PROJ_UP,   ex, m, max_block, &zt)?;
3945                    let mut act = e.uninit(n_ff_exp)?;  // activation fully overwrites
3946                    Self::ffn_act_lim(e, cfg, &gate, &up, m.gate_exps.macro_scale(ex),
3947                        m.up_exps.macro_scale(ex), lim_exp, &mut act, n_ff_exp)?;
3948                    let actv = act.slice(0..n_ff_exp);
3949                    let y = Self::moe_cached_gemm(e, il, PROJ_DOWN, ex, m, max_block, &actv)?;
3950                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
3951                    // down-proj macro folds into the accumulate weight (post-matmul linear scale).
3952                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
3953                } else if cache_frozen {
3954                    // A later prompt prime must not change the CPU/GPU assignment frozen after the
3955                    // first prime. Reuse every fixed resident projection directly and stage only a
3956                    // true miss through the ordinary scratch slot. This preserves the established
3957                    // f32-dequant numeric path while avoiding a full-bank reread on every prime.
3958                    let gate = Self::moe_frozen_gemm(
3959                        e,
3960                        il,
3961                        PROJ_GATE,
3962                        ex,
3963                        m,
3964                        max_block,
3965                        &zt,
3966                        &mut scratch_g,
3967                        g_len,
3968                    )?;
3969                    let up = Self::moe_frozen_gemm(
3970                        e,
3971                        il,
3972                        PROJ_UP,
3973                        ex,
3974                        m,
3975                        max_block,
3976                        &zt,
3977                        &mut scratch_u,
3978                        u_len,
3979                    )?;
3980                    let mut act = e.uninit(n_ff_exp)?;
3981                    Self::ffn_act_lim(
3982                        e,
3983                        cfg,
3984                        &gate,
3985                        &up,
3986                        m.gate_exps.macro_scale(ex),
3987                        m.up_exps.macro_scale(ex),
3988                        lim_exp,
3989                        &mut act,
3990                        n_ff_exp,
3991                    )?;
3992                    let actv = act.slice(0..n_ff_exp);
3993                    let y = Self::moe_frozen_gemm(
3994                        e,
3995                        il,
3996                        PROJ_DOWN,
3997                        ex,
3998                        m,
3999                        max_block,
4000                        &actv,
4001                        &mut scratch_d,
4002                        d_len,
4003                    )?;
4004                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
4005                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
4006                } else {
4007                    // Stage-1: stage gate/up/down for expert `ex` into the scratch slots, then GEMM.
4008                    // Lazy scratch: first no-cache expert allocates the 3 slots (uninit — stage_expert
4009                    // fully overwrites the byte range the GEMM reads).
4010                    if scratch_g.is_none() {
4011                        scratch_g = Some(e.alloc_u8_uninit(g_len)?);
4012                        scratch_u = Some(e.alloc_u8_uninit(u_len)?);
4013                        scratch_d = Some(e.alloc_u8_uninit(d_len)?);
4014                    }
4015                    let (sg, su, sd) = (scratch_g.as_mut().unwrap(), scratch_u.as_mut().unwrap(),
4016                                        scratch_d.as_mut().unwrap());
4017                    let gl = m.gate_exps.expert_layout(ex);
4018                    let ul = m.up_exps.expert_layout(ex);
4019                    let dl = m.down_exps.expert_layout(ex);
4020                    e.stage_expert(m.gate_exps.expert_bytes(ex), sg, 0)?;
4021                    let gate = e.qmatvec_view(sg, 0..gl.len, &zt, 1,
4022                        m.gate_exps.in_f, m.gate_exps.out_f, gl.qtype, gl.row_bytes)?;
4023
4024                    e.stage_expert(m.up_exps.expert_bytes(ex), su, 0)?;
4025                    let up = e.qmatvec_view(su, 0..ul.len, &zt, 1,
4026                        m.up_exps.in_f, m.up_exps.out_f, ul.qtype, ul.row_bytes)?;
4027
4028                    let mut act = e.uninit(n_ff_exp)?;  // activation fully overwrites
4029                    Self::ffn_act_lim(e, cfg, &gate, &up, m.gate_exps.macro_scale(ex),
4030                        m.up_exps.macro_scale(ex), lim_exp, &mut act, n_ff_exp)?;
4031
4032                    e.stage_expert(m.down_exps.expert_bytes(ex), sd, 0)?;
4033                    let actv = act.slice(0..n_ff_exp);
4034                    let y = e.qmatvec_view(sd, 0..dl.len, &actv, 1,
4035                        m.down_exps.in_f, m.down_exps.out_f, dl.qtype, dl.row_bytes)?;
4036
4037                    let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
4038                    e.axpy_into(&y, w[j] * m.down_exps.macro_scale(ex), &mut dst, n_embd)?;
4039                }
4040            }
4041            if let Some(worker) = cpu_worker {
4042                let cpu_output = worker.wait().map_err(std::io::Error::other)?;
4043                let cpu_output = e.htod(&cpu_output)?;
4044                let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
4045                e.axpy_into(&cpu_output, 1.0, &mut dst, n_embd)?;
4046            }
4047            if cpu_hybrid && !cache_frozen && cpu_expert_profile_admit_enabled() {
4048                for (j, &ex) in sel.iter().enumerate() {
4049                    if cpu_mask[j] {
4050                        Self::moe_profile_admit_expert(e, il, ex as usize, m, max_block)?;
4051                    }
4052                }
4053            }
4054        }
4055
4056        // 3. SHARED EXPERT (ALWAYS-ON, no routing) on the SAME z — qwen35moe only. OLMoE and most
4057        //    vanilla MoE have NO shared expert (the shexp tensors are absent / `None`); skip it then.
4058        // gate_inp_shexp is OPTIONAL: qwen35moe gates the shared expert (sigmoid(gate_inp) x sh);
4059        // MiniMax-M3 (DeepSeek-V3 class) has NO shexp gate — the shared expert adds directly.
4060        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
4061            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
4062        {
4063            let n_ff_sh = gate_shexp.out_features();  // 512
4064            // Q8 TRUNK-FUSION (decode t=1): gate_shexp+up_shexp are Q8_0 same-shape on the 35B —
4065            // ONE fused2 launch (also folds the two per-matmul re-quantizes of z into one).
4066            // Bit-identical per (tensor,row); falls back to the two matmul calls when ineligible.
4067            // Small-t (spec verify 2..15) rides matmul_decode_exact so shexp FP chains match the
4068            // t==1 decode chain per column (cuBLASLt n-dependence + dp4a-vs-mmvq class); real
4069            // prefill keeps the batched matmul. Activation routes through ffn_act (SiLU for
4070            // softmax archs, clamped swigluoai for M3 — identical to silu_mul when cfg.m3 is None).
4071            let verify_t = t > 1 && t < PRIME_MIN_T;
4072            let (sg_gate, sg_up) = if t == 1 {
4073                match e.matmul_q8_fused2_x(gate_shexp, up_shexp, z)? {
4074                    Some(pair) => pair,
4075                    None => (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?),
4076                }
4077            } else if verify_t {
4078                (e.matmul_decode_exact(gate_shexp, z, t)?, e.matmul_decode_exact(up_shexp, z, t)?)
4079            } else {
4080                (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?)   // [T, 512] each
4081            };
4082            let mut sa = e.uninit(t * n_ff_sh)?;  // activation fully overwrites
4083            Self::ffn_act_lim(e, cfg, &sg_gate, &sg_up, 1.0, 1.0, lim_shexp, &mut sa, t * n_ff_sh)?;
4084            let sh = if verify_t { e.matmul_decode_exact(down_shexp, &sa, t)? }
4085                     else { e.matmul(down_shexp, &sa, t)? };     // [T, n_embd]
4086
4087            // shexp gate: qwen35moe sigmoid-gates via ffn_gate_inp_shexp (1-D ne=[n_embd] ->
4088            // out_f=1); M3 has no gate tensor -> weight 1.0. Decode + verify ride the fused
4089            // sigmoid-dot kernel (one fold order for both chains; kills the per-layer
4090            // cuBLASLt m=1 splitK GEMM — 40x/step, ~10% of the H100 q35 decode step).
4091            // SERVE ISOLATION (lane/concat-prime-exact, 2026-08-02): PREFILL rides it too.
4092            // This out_f=1 cuBLASLt GEMV is the SECOND m-dependent op in the trunk (probed:
4093            // rows [0,19) move by 1.07e-4 between m=74 and m=75 while sigmoid_dot_rows is
4094            // BIT-IDENTICAL — allw-shexpgate-o35b.log). The gate multiplies the shared
4095            // expert's contribution into every token's residual, so under cross-request
4096            // concat prefill a session's hidden state depended on its co-arrivals' token
4097            // count. MEMRA_ROUTER_PREFILL_EXACT=0 reverts to the batched cuBLASLt linear.
4098            let g = match &m.gate_inp_shexp {
4099                Some(gate_inp_shexp) => {
4100                    if t < PRIME_MIN_T || crate::router_prefill_exact_on() {
4101                        e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
4102                    } else {
4103                        let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
4104                        let mut g = e.uninit(t)?;  // sigmoid fully overwrites
4105                        e.sigmoid(&gs, &mut g, t)?;
4106                        g
4107                    }
4108                }
4109                None => e.htod(&vec![1.0f32; t])?,
4110            };
4111            // moe_out[r, :] += sh[r, :] * g[r]   (per-token scalar gate; g=1 ungated)
4112            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
4113        }
4114
4115        Ok(moe_out)
4116    }
4117
4118    /// Stage-1 (no-cache) per-DECODE-TOKEN H2D bytes: every routed block re-staged every layer every
4119    /// token = sum over MoE layers of n_used * (gate+up+down expert_stride). The §D.4 PCIe baseline.
4120    pub fn stage1_h2d_per_token(&self) -> u64 {
4121        use crate::hybrid::Ffn;
4122        let n_used = self.cfg.moe.as_ref().map(|m| m.expert_used_count as u64).unwrap_or(0);
4123        let mut bytes = 0u64;
4124        for l in self.layers.iter() {
4125            if let Ffn::Moe(m) = &l.ffn {
4126                bytes += n_used * (m.gate_exps.max_expert_bytes() + m.up_exps.max_expert_bytes()
4127                                   + m.down_exps.max_expert_bytes()) as u64;
4128            }
4129        }
4130        bytes
4131    }
4132
4133    /// Largest expert block (bytes) across ALL MoE layers + the MTP head — the fixed cache slot size.
4134    /// UD/dynamic GGUFs quant different layers differently, so `expert_stride` varies per layer; the
4135    /// residency cache slots are fixed-address and must fit any block, so size to this global max.
4136    pub(crate) fn max_moe_block(&self) -> usize {
4137        use crate::hybrid::Ffn;
4138        let mut mx = 0usize;
4139        let mut scan = |ffn: &Ffn| {
4140            if let Ffn::Moe(m) = ffn {
4141                mx = mx.max(m.gate_exps.max_expert_bytes())
4142                       .max(m.up_exps.max_expert_bytes())
4143                       .max(m.down_exps.max_expert_bytes());
4144            }
4145        };
4146        for l in self.layers.iter() { scan(&l.ffn); }
4147        if let Some(mtp) = self.mtp.as_ref() { scan(&mtp.ffn); }
4148        mx
4149    }
4150
4151    /// Exact retained projection lengths for cache class sizing. Pruned ids keep router positions
4152    /// but have no bytes and therefore consume no residency slot.
4153    pub(crate) fn moe_cache_block_sizes(&self) -> Vec<usize> {
4154        use crate::hybrid::Ffn;
4155        let mut sizes = Vec::new();
4156        let mut scan = |ffn: &Ffn| {
4157            let Ffn::Moe(m) = ffn else { return };
4158            for ex in 0..m.gate_exps.n_expert {
4159                if m.active_experts.as_ref().is_some_and(|active| !active[ex]) {
4160                    continue;
4161                }
4162                for exps in [&m.gate_exps, &m.up_exps, &m.down_exps] {
4163                    let len = exps.expert_layout(ex).len;
4164                    if len > 0 {
4165                        sizes.push(len);
4166                    }
4167                }
4168            }
4169        };
4170        for layer in &self.layers {
4171            scan(&layer.ffn);
4172        }
4173        if let Some(mtp) = &self.mtp {
4174            scan(&mtp.ffn);
4175        }
4176        sizes
4177    }
4178
4179    /// Persist the frozen residency set so a later process can restage it directly and skip
4180    /// the profiling warmup. Plain text: a versioned header binding slot geometry, then one
4181    /// `layer proj ex` triple per line. A mismatched or stale profile is rejected at load
4182    /// (header check) or degrades to fewer restaged blocks (per-id checks); either way the
4183    /// post-freeze argmax gate still validates the serving assignment.
4184    pub fn save_cpu_expert_residency_profile(
4185        &self,
4186        e: &Engine,
4187        path: &std::path::Path,
4188    ) -> Result<(), Box<dyn std::error::Error>> {
4189        let Some(ids) = e.export_moe_residency() else {
4190            return Err("no MoE residency cache to persist".into());
4191        };
4192        let mut body = format!(
4193            "memra-freeze-profile v1 max_block={} blocks={}\n",
4194            self.max_moe_block(),
4195            ids.len()
4196        );
4197        for (layer, proj, ex) in &ids {
4198            body.push_str(&format!("{layer} {proj} {ex}\n"));
4199        }
4200        let tmp = path.with_extension("tmp");
4201        std::fs::write(&tmp, body)?;
4202        std::fs::rename(&tmp, path)?;
4203        println!(
4204            "[moe-cache] freeze profile saved: {} blocks -> {}",
4205            ids.len(),
4206            path.display()
4207        );
4208        Ok(())
4209    }
4210
4211    /// Restage a saved freeze profile and freeze immediately, skipping the profiling warmup.
4212    /// Returns false (leaving the cache untouched for a normal warmup) when the profile is
4213    /// missing or its header does not match this model's slot geometry.
4214    pub fn restore_cpu_expert_residency_profile(
4215        &self,
4216        e: &Engine,
4217        path: &std::path::Path,
4218    ) -> Result<bool, Box<dyn std::error::Error>> {
4219        use crate::hybrid::Ffn;
4220        use crate::moe_cache::BlockId;
4221        let Ok(content) = std::fs::read_to_string(path) else {
4222            return Ok(false);
4223        };
4224        let mut lines = content.lines();
4225        let Some(header) = lines.next() else { return Ok(false) };
4226        let expected = format!("memra-freeze-profile v1 max_block={}", self.max_moe_block());
4227        if !header.starts_with(&expected) {
4228            println!(
4229                "[moe-cache] freeze profile ignored (geometry mismatch): {}",
4230                path.display()
4231            );
4232            return Ok(false);
4233        }
4234        let mut by_layer: std::collections::HashMap<u16, Vec<BlockId>> =
4235            std::collections::HashMap::new();
4236        for line in lines {
4237            let mut fields = line.split_whitespace();
4238            let (Some(layer), Some(proj), Some(ex)) =
4239                (fields.next(), fields.next(), fields.next())
4240            else {
4241                continue;
4242            };
4243            let (Ok(layer), Ok(proj), Ok(ex)) =
4244                (layer.parse::<u16>(), proj.parse::<u8>(), ex.parse::<u16>())
4245            else {
4246                continue;
4247            };
4248            by_layer
4249                .entry(layer)
4250                .or_default()
4251                .push(BlockId::new(layer, proj, ex));
4252        }
4253        let requested: usize = by_layer.values().map(Vec::len).sum();
4254        if requested == 0 {
4255            return Ok(false);
4256        }
4257        let max_block = self.max_moe_block();
4258        let mut restaged = 0usize;
4259        let mut stage_layer = |layer_index: u16,
4260                               ffn: &Ffn|
4261         -> Result<(), Box<dyn std::error::Error>> {
4262            let Ffn::Moe(m) = ffn else { return Ok(()) };
4263            let Some(ids) = by_layer.get(&layer_index) else {
4264                return Ok(());
4265            };
4266            e.with_moe_cache(max_block, |cache, eng| {
4267                for id in ids {
4268                    if cache.restage_block(*id, m, eng)? {
4269                        restaged += 1;
4270                    }
4271                }
4272                Ok(())
4273            })
4274        };
4275        for (index, layer) in self.layers.iter().enumerate() {
4276            stage_layer(index as u16, &layer.ffn)?;
4277        }
4278        if let Some(mtp) = self.mtp.as_ref() {
4279            stage_layer(u16::MAX, &mtp.ffn)?;
4280        }
4281        e.freeze_moe_cache();
4282        println!(
4283            "[moe-cache] freeze profile restored: {restaged}/{requested} blocks restaged from {}",
4284            path.display()
4285        );
4286        Ok(true)
4287    }
4288
4289    /// Freeze the heterogeneous CPU/GPU split after the caller's discarded profile warmup.
4290    pub fn freeze_cpu_expert_residency(
4291        &self,
4292        e: &Engine,
4293    ) -> Result<(), Box<dyn std::error::Error>> {
4294        e.freeze_moe_cache();
4295        Ok(())
4296    }
4297
4298    /// FFN activation dispatch: swigluoai (clamped, alpha/limit) when cfg.m3 says so, else the
4299    /// standard SiLU*up. One seam so every FFN site (dense, routed expert, shared expert) follows
4300    /// the model's activation exactly.
4301    ///
4302    /// NO-`il` FORM: cannot apply step35's PER-LAYER SwiGLU clamp. Only call it from a site whose
4303    /// layer provably has no live limit (dense-FFN layers, MTP blocks) — `ffn_act_lim` is the
4304    /// form for anything that can land on a clamped layer.
4305    pub fn ffn_act(e: &Engine, cfg: &ModelConfig, gate: &CudaSlice<f32>, up: &CudaSlice<f32>,
4306               act: &mut CudaSlice<f32>, n: usize) -> Result<(), Box<dyn std::error::Error>> {
4307        Self::ffn_act_scaled(e, cfg, gate, up, 1.0, 1.0, act, n)
4308    }
4309
4310    /// ffn_act with per-tensor post-matmul macro-scales folded in (gs/us == 1.0 -> identical
4311    /// float ops to ffn_act; used by the ModelOpt NVFP4 expert path where each expert tensor
4312    /// carries a `weight_scale_2`). Same no-`il` contract as `ffn_act`.
4313    #[allow(clippy::too_many_arguments)]
4314    pub(crate) fn ffn_act_scaled(e: &Engine, cfg: &ModelConfig, gate: &CudaSlice<f32>, up: &CudaSlice<f32>,
4315               gs: f32, us: f32, act: &mut CudaSlice<f32>, n: usize)
4316               -> Result<(), Box<dyn std::error::Error>> {
4317        Self::ffn_act_lim(e, cfg, gate, up, gs, us, None, act, n)
4318    }
4319
4320    /// ffn_act_scaled + step35's PER-LAYER clamped SwiGLU. `limit`:
4321    ///   * `None`   -> the unclamped dispatch (every arch except step35's layers 43-44).
4322    ///   * `Some(l)`-> `min(silu(gate*gs), l) * clamp(up*us, +-l)` (llama-graph.cpp:2146/1751,
4323    ///                 non-DEEPSEEK4 branch). Callers source it from `cfg.clamp_exp_at(il)`
4324    ///                 (routed experts) or `cfg.clamp_shexp_at(il)` (shared expert) — the two
4325    ///                 arrays are SEPARATE and a layer can have one without the other.
4326    /// The `> 1e-6` eps gate lives in `clamp_exp_at`/`clamp_shexp_at`, so a `Some` here is
4327    /// already known live.
4328    #[allow(clippy::too_many_arguments)]
4329    pub(crate) fn ffn_act_lim(e: &Engine, cfg: &ModelConfig, gate: &CudaSlice<f32>, up: &CudaSlice<f32>,
4330               gs: f32, us: f32, limit: Option<f32>, act: &mut CudaSlice<f32>, n: usize)
4331               -> Result<(), Box<dyn std::error::Error>> {
4332        if let Some(m3) = cfg.m3.as_ref() {
4333            debug_assert!(limit.is_none(), "m3 swigluoai and step35 clamp are different archs");
4334            return e.swigluoai_mul_scaled(gate, up, gs, us, m3.swiglu_alpha, m3.swiglu_limit, act, n);
4335        }
4336        if let Some(l) = limit {
4337            return e.swiglu_clamped_mul_scaled(gate, up, gs, us, l, act, n);
4338        }
4339        if gs == 1.0 && us == 1.0 { return e.silu_mul(gate, up, act, n); }
4340        e.silu_mul_scaled(gate, up, gs, us, act, n)
4341    }
4342
4343    /// Routing for the whole batch: returns (sel [T*n_used] expert ids, w [T*n_used] renorm weights),
4344    /// token-major. Default = the Stage-1 host path (dtoh logits, softmax-256, stable DESC top-k,
4345    /// renorm). MEMRA_FUSED_ROUTER = the device kernel (§A) which reproduces the same numerics; we
4346    /// still dtoh the tiny [T,n_used] sel/w buffers (64 B/token vs 1 KB/token) — the host loop
4347    /// indexes HostExps.bytes on the CPU to choose the DMA source (§A.2 output staging).
4348    fn moe_route(e: &Engine, logits: &CudaSlice<f32>, t: usize, n_expert: usize, n_used: usize)
4349                 -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
4350        Self::moe_route_cfg(e, logits, t, n_expert, n_used, None, None, None)
4351    }
4352
4353    /// DeepSeek-V3-class sigmoid routing (MiniMax-M3, Hy3), host oracle. Reference:
4354    /// M3/Hy3 modeling code — scores = sigmoid(logits); selection over scores + expert bias
4355    /// (M3 `e_score_correction_bias` / Hy3 `expert_bias`, both surfaced as `exp_probs_b`);
4356    /// weights = un-biased scores of the selected experts, sum-normalized when `route_norm`,
4357    /// x scaling factor (M3 routed_scaling_factor 2.0 / Hy3 router_scaling_factor 2.826).
4358    /// `sig` = (scaling_factor, route_norm) from `cfg.sigmoid_router()`; softmax archs pass
4359    /// None -> the qwen35moe/OLMoE path below.
4360    fn moe_route_cfg(e: &Engine, logits: &CudaSlice<f32>, t: usize, n_expert: usize, n_used: usize,
4361                     bias: Option<&[f32]>, sig: Option<(f32, bool)>, active: Option<&[bool]>)
4362                 -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
4363        if let Some((sf, route_norm)) = sig {
4364            // sigmoid routing. Host path only for now (fused-router kernel is softmax-top-k).
4365            let lg = e.dtoh(logits)?;
4366            return Self::moe_route_sigmoid_host(
4367                &lg, t, n_expert, n_used, bias, sf, route_norm, active,
4368            );
4369        }
4370        // LAUNCH-STRUCTURE STAGE 1 (2026-07-05): fused router DEFAULT ON (MEMRA_FUSED_ROUTER=0
4371        // rollback) via the single-sync pinned readback — softmax arch only; the M3 sigmoid arm
4372        // above returns before this (host path until a sigmoid fused-router kernel exists).
4373        if active.is_none() && !matches!(std::env::var("MEMRA_FUSED_ROUTER").as_deref(), Ok("0")) {
4374            return e.moe_router_topk_host(logits, t, n_expert, n_used);
4375        }
4376        // Host oracle (the §D bit-identity reference).
4377        let lg = e.dtoh(logits)?;   // [T*n_expert] host
4378        let mut sel = vec![0u32; t * n_used];
4379        let mut w_out = vec![0f32; t * n_used];
4380        for tok in 0..t {
4381            let row = &lg[tok * n_expert..(tok + 1) * n_expert];
4382            // softmax over ALL n_expert (stable: subtract max)
4383            let maxl = row.iter().enumerate()
4384                .filter(|(i, _)| active.is_none_or(|mask| mask[*i]))
4385                .map(|(_, &x)| x).fold(f32::NEG_INFINITY, f32::max);
4386            let mut probs = vec![0f32; n_expert];
4387            let mut den = 0f32;
4388            for i in 0..n_expert {
4389                if active.is_some_and(|mask| !mask[i]) { continue; }
4390                let x = (row[i] - maxl).exp(); probs[i] = x; den += x;
4391            }
4392            for p in probs.iter_mut() { *p /= den; }
4393            // stable DESC sort: prob DESC, ascending-index tiebreak.
4394            let mut idx: Vec<usize> = (0..n_expert)
4395                .filter(|&i| active.is_none_or(|mask| mask[i])).collect();
4396            idx.sort_by(|&a, &b| probs[b].total_cmp(&probs[a]).then(a.cmp(&b)));
4397            let sl = &idx[..n_used];
4398            let mut wv: Vec<f32> = sl.iter().map(|&i| probs[i]).collect();
4399            let mut ws: f32 = wv.iter().sum();
4400            ws = ws.max(6.103515625e-5_f32);  // F16 smallest normal, clamp BEFORE divide
4401            for x in wv.iter_mut() { *x /= ws; }
4402            for j in 0..n_used {
4403                sel[tok * n_used + j] = sl[j] as u32;
4404                w_out[tok * n_used + j] = wv[j];
4405            }
4406        }
4407        Ok((sel, w_out))
4408    }
4409
4410    #[allow(clippy::too_many_arguments)]
4411    fn moe_route_sigmoid_with_input(
4412        e: &Engine,
4413        logits: &CudaSlice<f32>,
4414        input: &CudaSlice<f32>,
4415        t: usize,
4416        n_expert: usize,
4417        n_used: usize,
4418        bias: Option<&[f32]>,
4419        (sf, route_norm): (f32, bool),
4420        active: Option<&[bool]>,
4421    ) -> Result<(Vec<u32>, Vec<f32>, Vec<f32>), Box<dyn std::error::Error>> {
4422        let (lg, input) = e.dtoh_pair(logits, input)?;
4423        let (sel, w) =
4424            Self::moe_route_sigmoid_host(&lg, t, n_expert, n_used, bias, sf, route_norm, active)?;
4425        Ok((sel, w, input))
4426    }
4427
4428    /// Start the prediction-guided prefetch worker (MEMRA_MOE_PREFETCH=depth). Call after
4429    /// residency freeze: the worker filters against a static snapshot of the frozen HBM set.
4430    /// Builds a fully-owned per-layer table (host router copies via one-time DtoH, bias,
4431    /// active mask, prebuilt projection descriptors) so no model reference escapes.
4432    pub fn start_moe_prefetch_predictor(
4433        &self,
4434        e: &Engine,
4435        cfg: &ModelConfig,
4436    ) -> Result<(), Box<dyn std::error::Error>> {
4437        use crate::hybrid::Ffn;
4438        let Some(sig) = cfg.sigmoid_router() else {
4439            return Err("prefetch predictor requires a sigmoid-router arch".into());
4440        };
4441        let resident: std::collections::HashSet<(u16, u8, u16)> = e
4442            .export_moe_residency()
4443            .ok_or("prefetch predictor needs the frozen MoE residency cache")?
4444            .into_iter()
4445            .collect();
4446        let mut layers = Vec::new();
4447        for (index, layer) in self.layers.iter().enumerate() {
4448            let Ffn::Moe(m) = &layer.ffn else { continue };
4449            let crate::model::GpuTensor::Float { data, .. } = &m.gate_inp else { continue };
4450            let router = e.dtoh(data)?;
4451            let n_expert = m.gate_exps.n_expert;
4452            let n_embd = m.gate_exps.in_f;
4453            if router.len() != n_embd * n_expert {
4454                continue;
4455            }
4456            let build = |exps: &crate::model::HostExps| {
4457                (0..n_expert)
4458                    .map(|expert| crate::cpu_experts::predictor_projection(exps, expert))
4459                    .collect::<Vec<_>>()
4460            };
4461            layers.push((index as u16, crate::cpu_experts::PredictLayerInit {
4462                router,
4463                bias: m.exp_probs_b.clone(),
4464                active: m.active_experts.clone(),
4465                n_embd,
4466                n_used: cfg
4467                    .moe
4468                    .as_ref()
4469                    .map(|moe| moe.expert_used_count as usize)
4470                    .ok_or("prefetch predictor requires MoE config")?,
4471                sig,
4472                weights_n_expert: n_expert,
4473                gate: build(&m.gate_exps),
4474                up: build(&m.up_exps),
4475                down: build(&m.down_exps),
4476            }));
4477        }
4478        crate::cpu_experts::start_prefetch_predictor(layers, resident)
4479            .map_err(|error| error.into())
4480    }
4481
4482    /// Crate-visible sigmoid-routing oracle for the prefetch predictor: identical selection
4483    /// math to the runtime router, applied to host-computed lookahead logits.
4484    #[allow(clippy::too_many_arguments)]
4485    pub(crate) fn moe_route_sigmoid_host_public(
4486        logits: &[f32],
4487        t: usize,
4488        n_expert: usize,
4489        n_used: usize,
4490        bias: Option<&[f32]>,
4491        sf: f32,
4492        route_norm: bool,
4493        active: Option<&[bool]>,
4494    ) -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
4495        Self::moe_route_sigmoid_host(logits, t, n_expert, n_used, bias, sf, route_norm, active)
4496    }
4497
4498    #[allow(clippy::too_many_arguments)]
4499    fn moe_route_sigmoid_host(
4500        lg: &[f32],
4501        t: usize,
4502        n_expert: usize,
4503        n_used: usize,
4504        bias: Option<&[f32]>,
4505        sf: f32,
4506        route_norm: bool,
4507        active: Option<&[bool]>,
4508    ) -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
4509        if lg.len() != t * n_expert {
4510            return Err(format!(
4511                "sigmoid router logits length mismatch: got {}, expected {}",
4512                lg.len(),
4513                t * n_expert,
4514            )
4515            .into());
4516        }
4517        let mut sel = vec![0u32; t * n_used];
4518        let mut w_out = vec![0f32; t * n_used];
4519        for tok in 0..t {
4520            let row = &lg[tok * n_expert..(tok + 1) * n_expert];
4521            let scores: Vec<f32> = row.iter().map(|&x| 1.0 / (1.0 + (-x).exp())).collect();
4522            // selection score = sigmoid + bias; weight = plain sigmoid.
4523            let selsc: Vec<f32> = match bias {
4524                Some(b) => scores.iter().zip(b).map(|(s, bb)| s + bb).collect(),
4525                None => scores.clone(),
4526            };
4527            let mut idx: Vec<usize> = (0..n_expert)
4528                .filter(|&i| active.is_none_or(|mask| mask[i]))
4529                .collect();
4530            idx.sort_by(|&a, &b| selsc[b].total_cmp(&selsc[a]).then(a.cmp(&b)));
4531            let sl = &idx[..n_used];
4532            let mut wv: Vec<f32> = sl.iter().map(|&i| scores[i]).collect();
4533            if route_norm {
4534                let ws: f32 = wv.iter().sum::<f32>().max(1e-20);
4535                for x in wv.iter_mut() {
4536                    *x = *x / ws * sf;
4537                }
4538            } else {
4539                for x in wv.iter_mut() {
4540                    *x *= sf;
4541                }
4542            }
4543            for j in 0..n_used {
4544                sel[tok * n_used + j] = sl[j] as u32;
4545                w_out[tok * n_used + j] = wv[j];
4546            }
4547        }
4548        Ok((sel, w_out))
4549    }
4550
4551    /// LAUNCH-STRUCTURE STAGE 3: the ZERO-DtoH fully-resident MoE FFN. Caller guarantees the
4552    /// layer's device pointer row exists (checked under the cache lock). Router top-k runs on
4553    /// device; sel/w are consumed by the `_dev` matvec twins directly; NOTHING crosses PCIe.
4554    /// Same numerics as the fused-router + gdec chain (kernel-level bit-identity, see the
4555    /// MoE PREFILL PAIR-BATCH: host routing (sel/w like the sequential path), then 5 launches
4556    /// TOTAL per layer (quantize z, gate-pairs, up-pairs, silu, act-quantize, down-pairs,
4557    /// scatter) regardless of T or expert count. Bit-identity class: per (pair,row) dot =
4558    /// qmatvec_expert_q8 order; per-token accumulation slot-ordered (scatter kernel).
4559    fn moe_ffn_pairs(e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>, logits: &CudaSlice<f32>,
4560                     t: usize, cfg: &ModelConfig)
4561                     -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4562        let moe = cfg.moe.as_ref().unwrap();
4563        let n_embd = cfg.n_embd as usize;
4564        let n_expert = moe.expert_count as usize;
4565        let n_used = moe.expert_used_count as usize;
4566        let n_ff_exp = moe.expert_ff_length as usize;
4567        // This arm has no `il` and every kernel in it fuses PLAIN silu(gate)*up, so a clamped
4568        // model must never reach it. The caller's gate at `moe_ffn_sequential_zq8` denies per
4569        // layer via `swiglu_clamped_at(il)`; assert the whole-model form here so a future caller
4570        // that forgets the gate fails loudly in debug instead of returning wrong logits.
4571        debug_assert!(!cfg.swiglu_clamped_anywhere(),
4572                      "moe_ffn_pairs has no per-layer clamp: fused epilogues are plain SiLU");
4573        let dev = m.dev_exps.as_ref().unwrap();
4574        // WALL-GAP ARC: interleaved gate/up slab strides (see moe_ffn_dev).
4575        let (rbg_d, rbu_d) = if dev.gu_il {
4576            let sxx = m.gate_exps.row_bytes + m.up_exps.row_bytes; (sxx, sxx)
4577        } else { (m.gate_exps.row_bytes, m.up_exps.row_bytes) };
4578
4579        let (sel_all, w_all) = Self::moe_route(e, logits, t, n_expert, n_used)?;
4580        let n_pairs = t * n_used;
4581        // pair arrays: pair p = (token p/n_used, slot p%n_used) — ALREADY slot-ordered per token,
4582        // so the CSR is trivial: tok_pair_off[tok] = tok*n_used, ids identity.
4583        let pair_tok: Vec<i32> = (0..n_pairs).map(|p| (p / n_used) as i32).collect();
4584        let pair_ex:  Vec<i32> = sel_all.iter().map(|&x| x as i32).collect();
4585        let pair_w:   Vec<f32> = w_all.clone();
4586        let tok_off:  Vec<i32> = (0..=t).map(|tok| (tok * n_used) as i32).collect();
4587        let tok_ids:  Vec<i32> = (0..n_pairs as i32).collect();
4588        let pt = e.htod_i32(&pair_tok)?;
4589        let px = e.htod_i32(&pair_ex)?;
4590        let pw = e.htod(&pair_w)?;
4591        let toff = e.htod_i32(&tok_off)?;
4592        let tids = e.htod_i32(&tok_ids)?;
4593
4594        // z quantized ONCE for all tokens; gate/up pair matvecs; silu; act quantize; down; scatter.
4595        // EXPERT-MAJOR CSR (rung 2): pairs grouped by expert -> the kernel reuses each weight
4596        // row across the expert's token group (llama-MMQ's core win). Host grouping is O(pairs).
4597        let mut by_ex: Vec<Vec<i32>> = vec![Vec::new(); n_expert];
4598        for p in 0..n_pairs { by_ex[pair_ex[p] as usize].push(p as i32); }
4599        let mut ex_ids: Vec<i32> = Vec::new();
4600        let mut ex_off: Vec<i32> = vec![0];
4601        let mut ex_pairs: Vec<i32> = Vec::with_capacity(n_pairs);
4602        for (ex, list) in by_ex.iter().enumerate() {
4603            if list.is_empty() { continue; }
4604            ex_ids.push(ex as i32);
4605            ex_pairs.extend_from_slice(list);
4606            ex_off.push(ex_pairs.len() as i32);
4607        }
4608        let n_active = ex_ids.len();
4609        let exi = e.htod_i32(&ex_ids)?;
4610        let exo = e.htod_i32(&ex_off)?;
4611        let exp_d = e.htod_i32(&ex_pairs)?;
4612        let _ = &px;   // pair-major twin keeps it; em path uses CSR
4613
4614        // INT8-MMA EXPERT MMQ (MEMRA_MOE_MMA=1, opt-in): the m16n8k16.s8 tensor-core analog of the
4615        // _dec dp4a kernel (cu/mmq_iq_experts.cu). Same CSR grouping; per-expert matvec runs as a
4616        // 128x128-tile int8 MMA GEMM over the expert's token group. Weight IQ nibbles decode to int8
4617        // at tile-load + per-32 float scale; activation is q8_1_mmq (D4, same quant class as dp4a).
4618        // FP-ORDER differs from dp4a (MMA reduction) — logits SHIFT, gated on argmax/spec/closeness,
4619        // NOT byte-identity (like the W4A8 path). Requires IQ3_S/IQ4_XS + in_f % 256 == 0.
4620        // t >= 16 (GEMM_M-class rule): the MMA tile needs token volume (crossover ~200 tok/expert;
4621        // microbench: dp4a wins at tiny groups). ALSO an exactness requirement — spec verify
4622        // batches (t=2..K+2) must ride the dp4a path whose FP order matches the T=1 decode chain,
4623        // else K=1 self-consistency FAILs (caught 2026-07-06: MMA at T=2 flipped a verify argmax).
4624        // DEFAULT ON (2026-07-06, third flip — this time with the real culprit fixed): the
4625        // "MMA prime breaks spec" failure was the ROUTER's cuBLASLt n-dependence (d994271),
4626        // not MMA's own FP order — both this and the k-quant arms were innocent suspects whose
4627        // margin shifts surfaced the router bug. With the router decode-exact at verify t, the
4628        // full battery is green with MMA on (spec p1/p2/p3 PASS, raw K=1..8 PASS, argmax MATCH,
4629        // pp6257 2862 = 2.1x dec). t>=16 floor still required: verify batches must ride dp4a
4630        // (dispatch parity with the T=1 decode chain). MEMRA_MOE_MMA=0 rollback;
4631        // MEMRA_MOE_MMA_T overrides the floor (bisect seam).
4632        static MMA_T: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
4633        let mma_t = *MMA_T.get_or_init(|| {
4634            std::env::var("MEMRA_MOE_MMA_T").ok().and_then(|v| v.parse().ok()).unwrap_or(16)
4635        });
4636        let use_mma = std::env::var("MEMRA_MOE_MMA").map(|v| v != "0").unwrap_or(true)
4637            && t >= mma_t
4638            && q8_expert_dec_supported(m.gate_exps.qtype) && q8_expert_dec_supported(m.up_exps.qtype)
4639            && q8_expert_dec_supported(m.down_exps.qtype)
4640            && n_embd % 256 == 0 && n_ff_exp % 256 == 0;
4641        // GROUPED f16 LANE admission (MEMRA_MOE_F16G, rounds 46-49): its own door, no longer a
4642        // subset of the MMQ arm — q35's k-quant stragglers (Q6_K/Q4_K down, one Q3_K gate/up
4643        // layer) fail q8_expert_dec_supported but dequant fine to f16, so f16g must be able to
4644        // take a layer the MMQ arm would reject. Same t >= mma_t floor as MMA: decode and
4645        // spec-verify batches must ride the dp4a path whose FP order matches the T=1 chain.
4646        // MODE 2 (sm_120a naked default, lane/f16g-default-rearb 2026-08-02): every layer
4647        // whose three projections pass f16g_proj_ok rides the sk visitor with direct tile
4648        // loaders — with IQ4_XS/IQ3_S direct coverage the sk arm beats the int8-MMA MMQ
4649        // tiles on the IQ-bank models too (q35 board-2048 +33.9%, KAT pp512 +46.7%).
4650        // AUTO-KQUANT (mode 3, MEMRA_MOE_F16G=3, lane/q4k-expert-prefill): admit f16g ONLY
4651        // where the MMA arm can't take the layer's QTYPES — the k-quant expert class whose
4652        // baseline is the per-pair _em fallback (Ornith-35B Q4_K: board-2048 3.14x). Its
4653        // MMA-capable carve-out (IQ3_S/IQ4_XS/Q4_0 banks on MMQ) was priced pre-IQ-direct;
4654        // it survives as the rollback seam. Keyed on qtype capability, NOT use_mma, so
4655        // MEMRA_MOE_MMA=0 stays a pure dp4a rollback seam.
4656        let mma_capable = q8_expert_dec_supported(m.gate_exps.qtype)
4657            && q8_expert_dec_supported(m.up_exps.qtype)
4658            && q8_expert_dec_supported(m.down_exps.qtype)
4659            && n_embd % 256 == 0 && n_ff_exp % 256 == 0;
4660        let f16g_mode = crate::moe_f16g_mode();
4661        let f16g = f16g_mode != 0 && t >= mma_t
4662            && (f16g_mode != 3 || !mma_capable)
4663            && f16g_proj_ok(m.gate_exps.qtype, n_embd)
4664            && f16g_proj_ok(m.up_exps.qtype, n_embd)
4665            && f16g_proj_ok(m.down_exps.qtype, n_ff_exp);
4666        if use_mma || f16g {
4667            // GROUPED f16 LANE (MEMRA_MOE_F16G, rounds 46-49, experimental door): dequant
4668            // the active experts ONCE per projection to f16 and run one grouped f16 GEMM over
4669            // the CSR groups. f16-mirror numeric class — argmax/spec gated before promotion.
4670            // NOTE: the pair-gather kernel needs pair p ordered EXPERT-MAJOR (ex_pairs order);
4671            // the y rows come back in that same CSR order, so gate/up/down all stay pair-major
4672            // in ex_pairs order — but moe_pairs_silu_mul and the scatter consume PAIR-ID order.
4673            // We therefore gather activations per ex_pairs and scatter y back through ex_pairs.
4674            let y_down = if f16g {
4675                // CSR order end-to-end: gather z rows by the pair's TOKEN (pair p's token is
4676                // p / n_used — the trivial CSR above), silu in CSR order (elementwise), one
4677                // permute at the very end back to pair-id order for the scatter.
4678                let csr_tok: Vec<i32> = ex_pairs.iter().map(|&p| p / n_used as i32).collect();
4679                let csr_tok_d = e.htod_i32(&csr_tok)?;
4680                let (z_f16, z_s) = e.moe_f16g_act(z, Some(&csr_tok_d), n_embd, n_pairs)?;
4681                let g_csr = e.moe_f16_grouped(&dev.ptr_row, 0, n_expert, &exi, &ex_off, &exo,
4682                                              &z_f16, &z_s, n_embd, n_ff_exp, n_active, n_pairs,
4683                                              m.gate_exps.qtype, rbg_d)?;
4684                let u_csr = e.moe_f16_grouped(&dev.ptr_row, 1, n_expert, &exi, &ex_off, &exo,
4685                                              &z_f16, &z_s, n_embd, n_ff_exp, n_active, n_pairs,
4686                                              m.up_exps.qtype, rbu_d)?;
4687                let act_csr = e.moe_pairs_silu_mul(&g_csr, &u_csr, n_pairs * n_ff_exp)?;
4688                let (a_f16, a_s) = e.moe_f16g_act(&act_csr, None, n_ff_exp, n_pairs)?;
4689                let d_csr = e.moe_f16_grouped(&dev.ptr_row, 2, n_expert, &exi, &ex_off, &exo,
4690                                              &a_f16, &a_s, n_ff_exp, n_embd, n_active, n_pairs,
4691                                              m.down_exps.qtype, m.down_exps.row_bytes)?;
4692                e.rows_permute(&d_csr, &exp_d, n_pairs, n_embd)?
4693            } else {
4694            // gate/up: activation = z, token-major over t tokens; pair_tok gathers the routed row.
4695            let z_scr = e.mmq_iq_quantize_act(z, n_embd, t)?;
4696            let gate = e.mmq_iq_experts(&dev.ptr_row, 0, n_expert, &exi, &exo, &exp_d, &pt, &z_scr,
4697                                        n_embd, n_ff_exp, n_active, n_pairs, t,
4698                                        m.gate_exps.qtype, rbg_d)?;
4699            let up = e.mmq_iq_experts(&dev.ptr_row, 1, n_expert, &exi, &exo, &exp_d, &pt, &z_scr,
4700                                      n_embd, n_ff_exp, n_active, n_pairs, t,
4701                                      m.up_exps.qtype, rbu_d)?;
4702            // down: activation = silu(gate)*up, pair-major [n_pairs, n_ff_exp]; pair_tok =
4703            // identity. FUSED ACT-EPILOGUE (default on): one launch computes the activation in
4704            // registers and writes ONLY the quantized scratch — the two-pass chain
4705            // (moe_pairs_silu_mul writes act f32, mmq_iq_quantize_act re-reads it) is the
4706            // MEMRA_MOE_FUSE_ACTQ=0 rollback. Scratch bytes are BYTE-IDENTICAL (kernel-check).
4707            let a_scr = if crate::moe_fuse_actq_on() {
4708                e.mmq_iq_fused_act_quant(&gate, &up, n_ff_exp, n_pairs, 0)?
4709            } else {
4710                let act = e.moe_pairs_silu_mul(&gate, &up, n_pairs * n_ff_exp)?;
4711                e.mmq_iq_quantize_act(&act, n_ff_exp, n_pairs)?
4712            };
4713            let pair_self: Vec<i32> = (0..n_pairs as i32).collect();
4714            let pself = e.htod_i32(&pair_self)?;
4715            e.mmq_iq_experts(&dev.ptr_row, 2, n_expert, &exi, &exo, &exp_d, &pself, &a_scr,
4716                             n_ff_exp, n_embd, n_active, n_pairs, n_pairs,
4717                             m.down_exps.qtype, m.down_exps.row_bytes)?
4718            };
4719            let mut moe_out = e.uninit(t * n_embd)?;
4720            e.moe_pairs_scatter(&y_down, &pw, &toff, &tids, &mut moe_out, t, n_embd)?;
4721            if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
4722                (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
4723            {
4724                let n_ff_sh = gate_shexp.out_features();
4725                let sg_gate = e.matmul(gate_shexp, z, t)?;
4726                let sg_up = e.matmul(up_shexp, z, t)?;
4727                let mut sa = e.uninit(t * n_ff_sh)?;
4728                Self::ffn_act(e, cfg, &sg_gate, &sg_up, &mut sa, t * n_ff_sh)?;
4729                let sh = e.matmul(down_shexp, &sa, t)?;
4730                // shexp gate: qwen35moe sigmoid-gates; M3 has no gate tensor -> weight 1.0.
4731                // SERVE ISOLATION (lane/concat-prime-exact): the fused sigmoid-dot is the
4732                // m-INVARIANT form — see the sequential arm's note. This is the PAIRS arm,
4733                // i.e. the one real prefill actually takes on a resident-expert MoE model,
4734                // so the concat-prime isolation fix has to land here as well.
4735                let g = match &m.gate_inp_shexp {
4736                    Some(gate_inp_shexp) if crate::router_prefill_exact_on() => {
4737                        e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
4738                    }
4739                    Some(gate_inp_shexp) => {
4740                        let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
4741                        let mut g = e.uninit(t)?;
4742                        e.sigmoid(&gs, &mut g, t)?;
4743                        g
4744                    }
4745                    None => e.htod(&vec![1.0f32; t])?,
4746                };
4747                e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
4748            }
4749            return Ok(moe_out);
4750        }
4751
4752        // DECODE-ONCE MMQ (rung 3, MEMRA_MOE_DEC=1 default-on): dequant each weight group once per
4753        // (row,group) then dp4a across the expert's tokens. _em re-decoded per token (NEUTRAL).
4754        let dec = std::env::var("MEMRA_MOE_DEC").map(|v| v != "0").unwrap_or(true);
4755        let matvec = |proj, exi: &_, exo: &_, exp_d: &_, pt: &_, aq: &_, ad: &_,
4756                      inf, outf, qtype, rb| -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4757            // _dec's decode-once extractors are IQ-only; k-quant expert layers take the _em dot path.
4758            let dec = dec && q8_expert_dec_supported(qtype);
4759            if dec { e.moe_pairs_matvec_q8_dec(&dev.ptr_row, proj, exi, exo, exp_d, pt, aq, ad,
4760                                               inf, outf, n_expert, n_active, n_pairs, qtype, rb) }
4761            else   { e.moe_pairs_matvec_q8_em (&dev.ptr_row, proj, exi, exo, exp_d, pt, aq, ad,
4762                                               inf, outf, n_expert, n_active, n_pairs, qtype, rb) }
4763        };
4764        let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
4765        let gate = matvec(0, &exi, &exo, &exp_d, &pt, &zq, &zd,
4766                          n_embd, n_ff_exp, m.gate_exps.qtype, rbg_d)?;
4767        let up = matvec(1, &exi, &exo, &exp_d, &pt, &zq, &zd,
4768                        n_embd, n_ff_exp, m.up_exps.qtype, rbu_d)?;
4769        let act = e.moe_pairs_silu_mul(&gate, &up, n_pairs * n_ff_exp)?;
4770        let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
4771        // down consumes PAIR-major activation rows: pair_tok = identity.
4772        let pair_self: Vec<i32> = (0..n_pairs as i32).collect();
4773        let pself = e.htod_i32(&pair_self)?;
4774        let y_down = matvec(2, &exi, &exo, &exp_d, &pself, &aq2, &ad2,
4775                            n_ff_exp, n_embd, m.down_exps.qtype, m.down_exps.row_bytes)?;
4776        let mut moe_out = e.uninit(t * n_embd)?;   // scatter fully overwrites per (token,col)
4777        e.moe_pairs_scatter(&y_down, &pw, &toff, &tids, &mut moe_out, t, n_embd)?;
4778
4779        // SHARED EXPERT epilogue — same as the other paths.
4780        // gate_inp_shexp is OPTIONAL: qwen35moe gates the shared expert (sigmoid(gate_inp) x sh);
4781        // MiniMax-M3 (DeepSeek-V3 class) has NO shexp gate — the shared expert adds directly.
4782        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
4783            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
4784        {
4785            let n_ff_sh = gate_shexp.out_features();
4786            let sg_gate = e.matmul(gate_shexp, z, t)?;
4787            let sg_up = e.matmul(up_shexp, z, t)?;
4788            let mut sa = e.uninit(t * n_ff_sh)?;
4789            e.silu_mul(&sg_gate, &sg_up, &mut sa, t * n_ff_sh)?;
4790            let sh = e.matmul(down_shexp, &sa, t)?;
4791            // shexp gate: qwen35moe sigmoid-gates; M3 has no gate tensor -> weight 1.0.
4792            // m-INVARIANT fused sigmoid-dot under router_prefill_exact (serve isolation,
4793            // lane/concat-prime-exact) — every shexp-gate arm shares the same form so a
4794            // dispatch choice cannot change bits.
4795            let g = match &m.gate_inp_shexp {
4796                Some(gate_inp_shexp) if crate::router_prefill_exact_on() => {
4797                    e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
4798                }
4799                Some(gate_inp_shexp) => {
4800                    let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
4801                    let mut g = e.uninit(t)?;
4802                    e.sigmoid(&gs, &mut g, t)?;
4803                    g
4804                }
4805                None => e.htod(&vec![1.0f32; t])?,
4806            };
4807            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
4808        }
4809        Ok(moe_out)
4810    }
4811
4812    /// kernel headers); the shared-expert epilogue is byte-identical to moe_ffn_sequential's.
4813    #[allow(clippy::too_many_arguments)]
4814    #[allow(clippy::too_many_arguments)]
4815    fn moe_ffn_dev(e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>,
4816                   zq8: Option<&(CudaSlice<i8>, CudaSlice<f32>)>, logits: &CudaSlice<f32>,
4817                   t: usize, cfg: &ModelConfig, il: u16, max_block: usize)
4818                   -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4819        let moe = cfg.moe.as_ref().unwrap();
4820        let n_embd = cfg.n_embd as usize;
4821        let n_expert = moe.expert_count as usize;
4822        let n_used = moe.expert_used_count as usize;
4823        let n_ff_exp = moe.expert_ff_length as usize;
4824        // moe_router_topk is SOFTMAX-only (no exp_probs_b bias, no expert_weights_scale) and every
4825        // gate_up kernel here fuses PLAIN silu(gate)*up. `dev_ok` denies sigmoid-router archs and
4826        // clamped layers; assert both so a future caller that skips the gate fails loudly.
4827        debug_assert!(cfg.sigmoid_router().is_none(),
4828                      "moe_ffn_dev routes SOFTMAX: a sigmoid-router arch would pick wrong experts");
4829        debug_assert!(!cfg.swiglu_clamped_at(il as u32),
4830                      "moe_ffn_dev's fused epilogue is plain SiLU: no clamped form");
4831
4832        // device top-k: sel [t, n_used] i32, w [t, n_used] f32 — stays on device.
4833        let (sel_d, mut w_d) = e.moe_router_topk(logits, t, n_expert, n_used)?;
4834        // Down-projection macro fold (compressed-tensors NVFP4 artifacts): one tiny launch,
4835        // skipped entirely for macro-free experts (every k-quant GGUF).
4836        if m.has_macros {
4837            e.moe_w_scale_by_expert(&mut w_d, &sel_d, &m.dev_macros, n_expert, t * n_used)?;
4838        }
4839
4840        // moe_out rows are FULLY overwritten by moe_down8_fma_dev — uninit (stage-2 rule).
4841        let mut moe_out = e.uninit(t * n_embd)?;
4842
4843        // RESIDENT-EXPERTS arm: the pointer row comes from the load-time slab (no cache, no
4844        // lock). Same kernels/loop as the SLRU arm below — only the row's provenance differs.
4845        if let Some(dev) = m.dev_exps.as_ref() {
4846            // WALL-GAP ARC: interleaved gate/up slab (MEMRA_MOE_GU_IL) -> both projections use
4847            // the combined stride; up's base is offset in the ptr table. Down unchanged.
4848            let (rbg_d, rbu_d) = if dev.gu_il {
4849                let sxx = m.gate_exps.row_bytes + m.up_exps.row_bytes; (sxx, sxx)
4850            } else { (m.gate_exps.row_bytes, m.up_exps.row_bytes) };
4851            let q8 = moe_q8_enabled()
4852                && q8_expert_supported(m.gate_exps.qtype) && q8_expert_supported(m.up_exps.qtype)
4853                && q8_expert_supported(m.down_exps.qtype);
4854            // SMALL-M ROWS ARM (MEMRA_SPEC_M2, lane/spec-m2): batch the verify token loop —
4855            // ONE batched z-quantize + ONE gate_up rows launch + ONE act quantize + ONE down
4856            // rows launch (4 launches/layer, was 4t). BIT-IDENTICAL per token to the serial
4857            // loop below (rows twins = the _v/w8h2v per-token programs on a grid.z token axis;
4858            // quantize_q8_1 is per-32-block row-independent). Gated to the AUTO kernel modes —
4859            // a custom MEMRA_MOE_DEVQ8_GU/DOWN diagnostic run keeps the serial loop so the
4860            // dispatched kernel stays exactly the env-selected one — and to the w8h2v shape
4861            // (n_ff_exp==512, n_used<=8), the same contract the AUTO down dispatch keys on.
4862            let rows_arm = q8 && t > 1 && crate::spec::spec_m2()
4863                && n_ff_exp == 512 && n_used <= 8
4864                && std::env::var("MEMRA_MOE_DEVQ8_GU").map(|v| v.is_empty() || v == "v").unwrap_or(true)
4865                && std::env::var("MEMRA_MOE_DEVQ8_DOWN").map(|v| v.is_empty() || v == "w8h2v").unwrap_or(true);
4866            // CSR EXPERT-DEDUP gate_up (verify-cost target #1, DEFAULT ON 2026-07-10): the
4867            // owner-scan kernel serves every (token, slot) pair of an expert from ONE block,
4868            // deduping the 38-40% duplicated weight-stream+decode the overlap probe measured
4869            // (gate_up 55.0 -> 39.7us/launch; +1.3-2.1% spec e2e p2, +0.6-1.7% p3, all K).
4870            // Bit-identical to the _rows twins (explicit-intrinsic accumulate — the ULP/fmad
4871            // lesson; =2 byte-compare verified zero diffs). down stays on _rows (CSR down
4872            // measured 23.5 -> 37.5us: 16-group rows can't amortize the serial pair loop).
4873            // MEMRA_MOE_CSR=0 rollback; =2 runs BOTH paths and byte-compares (debug).
4874            let csr_mode = std::env::var("MEMRA_MOE_CSR").ok()
4875                .and_then(|v| v.parse::<i32>().ok()).unwrap_or(1);
4876            let csr_qt = |qt: i32| qt == crate::QT_IQ4_XS || qt == crate::QT_IQ3_S;
4877            let csr_arm = rows_arm && csr_mode > 0 && t <= 10
4878                && csr_qt(m.gate_exps.qtype) && csr_qt(m.up_exps.qtype)
4879                && csr_qt(m.down_exps.qtype);
4880            if csr_arm {
4881                if csr_mode == 2 {
4882                    static ENGAGED: std::sync::Once = std::sync::Once::new();
4883                    ENGAGED.call_once(|| eprintln!("[memra] moe CSR byte-compare mode ON (t={t})"));
4884                }
4885                let n_pairs = t * n_used;
4886                let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
4887                let act = e.moe_gate_up_silu8_dev_q8_csr(&dev.ptr_row, &sel_d, &zq, &zd, n_pairs,
4888                                                         n_embd, n_ff_exp, n_used, n_expert,
4889                                                         m.gate_exps.qtype, m.up_exps.qtype,
4890                                                         rbg_d, rbu_d)?;
4891                let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
4892                // down stays on the _rows twin — BOTH CSR down variants measured negative
4893                // (v1 serial pairs 23.5->37.5us; v2 warp-parallel+SMEM -14% e2e, K=8 -37%):
4894                // 16-group rows have too little decode to amortize any dedup structure.
4895                e.moe_down8_fma_dev_q8_rows(&dev.ptr_row, &sel_d, &w_d, &aq2, &ad2, &mut moe_out,
4896                                            t, n_ff_exp, n_embd, n_used, n_expert,
4897                                            m.down_exps.qtype, m.down_exps.row_bytes)?;
4898                if csr_mode == 2 {
4899                    // DEBUG BYTE-COMPARE: run the _rows twins on the same inputs, diff bits.
4900                    let act_r = e.moe_gate_up_silu8_dev_q8_rows(&dev.ptr_row, &sel_d, &zq, &zd, t,
4901                                                                n_embd, n_ff_exp, n_used, n_expert,
4902                                                                m.gate_exps.qtype, m.up_exps.qtype,
4903                                                                rbg_d, rbu_d, &m.dev_macros)?;
4904                    let mut out_r = e.uninit(t * n_embd)?;
4905                    let (aq2r, ad2r) = e.quantize_q8_1(&act_r, n_pairs, n_ff_exp)?;
4906                    e.moe_down8_fma_dev_q8_rows(&dev.ptr_row, &sel_d, &w_d, &aq2r, &ad2r, &mut out_r,
4907                                                t, n_ff_exp, n_embd, n_used, n_expert,
4908                                                m.down_exps.qtype, m.down_exps.row_bytes)?;
4909                    let (a1, a2) = (e.dtoh(&act)?, e.dtoh(&act_r)?);
4910                    let (o1, o2) = (e.dtoh(&moe_out)?, e.dtoh(&out_r)?);
4911                    let ba = a1.iter().zip(&a2).filter(|(x, y)| x.to_bits() != y.to_bits()).count();
4912                    let bo = o1.iter().zip(&o2).filter(|(x, y)| x.to_bits() != y.to_bits()).count();
4913                    if ba + bo > 0 {
4914                        eprintln!("[csr-check] il={il} t={t} ACT diffs={ba}/{} OUT diffs={bo}/{}",
4915                                  a1.len(), o1.len());
4916                        // First 4 differing ACT elements: (pair, o, csr, rows) + that pair's expert
4917                        let sel_h = e.dtoh_i32(&sel_d)?;
4918                        let mut shown = 0;
4919                        for (i, (x, y)) in a1.iter().zip(&a2).enumerate() {
4920                            if x.to_bits() != y.to_bits() && shown < 4 {
4921                                let (p, o) = (i / n_ff_exp, i % n_ff_exp);
4922                                let ex = sel_h[p];
4923                                let npx = sel_h.iter().filter(|&&v| v == ex).count();
4924                                eprintln!("  ACT p={p} ex={ex} np={npx} o={o} csr={x:e} rows={y:e}");
4925                                shown += 1;
4926                            }
4927                        }
4928                        std::process::exit(3);
4929                    }
4930                }
4931            } else if rows_arm {
4932                // TEMP PROBE (MEMRA_MOE_OVERLAP=1): cross-token expert-activation overlap at
4933                // verify — sizes the CSR dedup win (unique experts vs t*n_used pairs).
4934                if std::env::var("MEMRA_MOE_OVERLAP").as_deref() == Ok("1") {
4935                    use std::sync::atomic::{AtomicU64, Ordering};
4936                    static PAIRS: AtomicU64 = AtomicU64::new(0);
4937                    static UNIQ: AtomicU64 = AtomicU64::new(0);
4938                    static CALLS: AtomicU64 = AtomicU64::new(0);
4939                    let sel_h = e.dtoh_i32(&sel_d)?;
4940                    let mut u: Vec<i32> = sel_h.clone(); u.sort_unstable(); u.dedup();
4941                    PAIRS.fetch_add(sel_h.len() as u64, Ordering::Relaxed);
4942                    UNIQ.fetch_add(u.len() as u64, Ordering::Relaxed);
4943                    let c = CALLS.fetch_add(1, Ordering::Relaxed) + 1;
4944                    if c % 480 == 0 {
4945                        let p = PAIRS.load(Ordering::Relaxed); let q = UNIQ.load(Ordering::Relaxed);
4946                        eprintln!("[overlap] calls={c} pairs={p} unique={q} ratio={:.3} (t={t})",
4947                                  q as f64 / p as f64);
4948                    }
4949                }
4950                let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
4951                let act = e.moe_gate_up_silu8_dev_q8_rows(&dev.ptr_row, &sel_d, &zq, &zd, t,
4952                                                          n_embd, n_ff_exp, n_used, n_expert,
4953                                                          m.gate_exps.qtype, m.up_exps.qtype,
4954                                                          rbg_d, rbu_d, &m.dev_macros)?;
4955                let (aq2, ad2) = e.quantize_q8_1(&act, t * n_used, n_ff_exp)?;
4956                e.moe_down8_fma_dev_q8_rows(&dev.ptr_row, &sel_d, &w_d, &aq2, &ad2, &mut moe_out,
4957                                            t, n_ff_exp, n_embd, n_used, n_expert,
4958                                            m.down_exps.qtype, m.down_exps.row_bytes)?;
4959            } else {
4960            for tok in 0..t {
4961                let zt = z.slice(tok * n_embd..(tok + 1) * n_embd);
4962                let selt = sel_d.slice(tok * n_used..(tok + 1) * n_used);
4963                let wt = w_d.slice(tok * n_used..(tok + 1) * n_used);
4964                let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
4965                if q8 {
4966                    let (zq, zd) = match (t, zq8) {
4967                        (1, Some((q, d))) => (q.clone(), d.clone()),
4968                        _ => e.quantize_q8_1_view(&zt, 1, n_embd)?,
4969                    };
4970                    let act = e.moe_gate_up_silu8_dev_q8(&dev.ptr_row, &selt, &zq, &zd,
4971                                                         n_embd, n_ff_exp, n_used, n_expert,
4972                                                         m.gate_exps.qtype, m.up_exps.qtype,
4973                                                         rbg_d, rbu_d, &m.dev_macros)?;
4974                    let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
4975                    e.moe_down8_fma_dev_q8(&dev.ptr_row, &selt, &wt, &aq2, &ad2, &mut dst,
4976                                           n_ff_exp, n_embd, n_used, n_expert,
4977                                           m.down_exps.qtype, m.down_exps.row_bytes)?;
4978                } else {
4979                    let act = e.moe_gate_up_silu8_dev(&dev.ptr_row, &selt, &zt, n_embd, n_ff_exp,
4980                                                      n_used, n_expert,
4981                                                      m.gate_exps.qtype, m.up_exps.qtype,
4982                                                      rbg_d, rbu_d, &m.dev_macros)?;
4983                    e.moe_down8_fma_dev(&dev.ptr_row, &selt, &wt, &act, &mut dst,
4984                                        n_ff_exp, n_embd, n_used, n_expert,
4985                                        m.down_exps.qtype, m.down_exps.row_bytes)?;
4986                }
4987            }
4988            }
4989        } else {
4990        // Launch under the cache lock: the row borrow lives as long as the closure, and the
4991        // lock covers only launch ISSUE (µs), same policy as moe_cached_gemm.
4992        // Q8 ARM PARITY (2026-07-06): the SLRU arm ran the f32-dequant _dev kernels only —
4993        // 80us/launch vs the q8 twins' 15us on the SAME shapes (fixed-build profile: 228
4994        // f32 launches = 36ms of the 64-tok window). Same q8 gate + kernels as the resident
4995        // arm above; MEMRA_MOE_Q8=0 restores the byte-identical f32 path.
4996        let q8 = moe_q8_enabled()
4997            && q8_expert_supported(m.gate_exps.qtype) && q8_expert_supported(m.up_exps.qtype)
4998            && q8_expert_supported(m.down_exps.qtype);
4999        e.with_moe_cache(max_block, |c, eng| {
5000            let row = c.layer_dev_row(il, n_expert, eng)?
5001                .ok_or("moe_ffn_dev: layer row vanished under the lock")?;
5002            for tok in 0..t {
5003                let zt = z.slice(tok * n_embd..(tok + 1) * n_embd);
5004                let selt = sel_d.slice(tok * n_used..(tok + 1) * n_used);
5005                let wt = w_d.slice(tok * n_used..(tok + 1) * n_used);
5006                let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
5007                if q8 {
5008                    let (zq, zd) = match (t, zq8) {
5009                        (1, Some((q, d))) => (q.clone(), d.clone()),
5010                        _ => eng.quantize_q8_1_view(&zt, 1, n_embd)?,
5011                    };
5012                    let act = eng.moe_gate_up_silu8_dev_q8(row, &selt, &zq, &zd,
5013                                                           n_embd, n_ff_exp, n_used, n_expert,
5014                                                           m.gate_exps.qtype, m.up_exps.qtype,
5015                                                           m.gate_exps.row_bytes, m.up_exps.row_bytes,
5016                                                           &m.dev_macros)?;
5017                    let (aq2, ad2) = eng.quantize_q8_1(&act, n_used, n_ff_exp)?;
5018                    eng.moe_down8_fma_dev_q8(row, &selt, &wt, &aq2, &ad2, &mut dst,
5019                                             n_ff_exp, n_embd, n_used, n_expert,
5020                                             m.down_exps.qtype, m.down_exps.row_bytes)?;
5021                } else {
5022                    let act = eng.moe_gate_up_silu8_dev(row, &selt, &zt, n_embd, n_ff_exp,
5023                                                        n_used, n_expert,
5024                                                        m.gate_exps.qtype, m.up_exps.qtype,
5025                                                        m.gate_exps.row_bytes, m.up_exps.row_bytes,
5026                                                        &m.dev_macros)?;
5027                    eng.moe_down8_fma_dev(row, &selt, &wt, &act, &mut dst,
5028                                          n_ff_exp, n_embd, n_used, n_expert,
5029                                          m.down_exps.qtype, m.down_exps.row_bytes)?;
5030                }
5031            }
5032            // instrumentation parity with the host paths (3 blocks/expert-slot, all hits).
5033            c.hits += (t * 3 * n_used) as u64;
5034            Ok(())
5035        })?;
5036        }
5037
5038        // SHARED EXPERT epilogue — byte-identical to moe_ffn_sequential step 3 (incl. its Q8
5039        // TRUNK-FUSION arm: fused2 is bit-identical to the two matmul calls per (tensor,row)).
5040        // gate_inp_shexp is OPTIONAL: qwen35moe gates the shared expert (sigmoid(gate_inp) x sh);
5041        // MiniMax-M3 (DeepSeek-V3 class) has NO shexp gate — the shared expert adds directly.
5042        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
5043            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
5044        {
5045            let n_ff_sh = gate_shexp.out_features();
5046            // verify-t (2..15) decode-exact arm: this fn now serves the spec verify batches
5047            // (pairs gate moved to t>=PRIME_MIN_T), so the shexp chain must match t==1 per col.
5048            let verify_t = t > 1 && t < PRIME_MIN_T;
5049            let (sg_gate, sg_up) = if t == 1 {
5050                match e.matmul_q8_fused2_x(gate_shexp, up_shexp, z)? {
5051                    Some(pair) => pair,
5052                    None => (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?),
5053                }
5054            } else if verify_t {
5055                // VERIFY-TIER TRUNK FUSION (MEMRA_SPEC_FUSED_T, t=2-4): the shexp gate+up pair
5056                // rides one shared quantize + one fused2 batched launch instead of two
5057                // decode-exact calls. Bit-identical per (tensor,token,row) — see spec_fused_t.
5058                let mut fused = None;
5059                if crate::spec::spec_fused_t() && (2..=4).contains(&t)
5060                    && e.uses_q8_1_fast(gate_shexp) && e.uses_q8_1_fast(up_shexp) {
5061                    let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
5062                    fused = e.matmul_q8_fused2_t(gate_shexp, up_shexp, &zq, &zd, t)?;
5063                }
5064                match fused {
5065                    Some(pair) => pair,
5066                    None => (e.matmul_decode_exact(gate_shexp, z, t)?,
5067                             e.matmul_decode_exact(up_shexp, z, t)?),
5068                }
5069            } else {
5070                (e.matmul(gate_shexp, z, t)?, e.matmul(up_shexp, z, t)?)
5071            };
5072            let mut sa = e.uninit(t * n_ff_sh)?;  // silu_mul fully overwrites
5073            e.silu_mul(&sg_gate, &sg_up, &mut sa, t * n_ff_sh)?;
5074            let sh = if verify_t { e.matmul_decode_exact(down_shexp, &sa, t)? }
5075                     else { e.matmul(down_shexp, &sa, t)? };
5076            // shexp gate: qwen35moe sigmoid-gates; M3 has no gate tensor -> weight 1.0.
5077            // Same fused sigmoid-dot as moe_ffn_sequential step 3 (byte-identity contract
5078            // between the two arms; prefill keeps the batched cuBLASLt linear).
5079            let g = match &m.gate_inp_shexp {
5080                Some(gate_inp_shexp) => {
5081                    // router_prefill_exact_on(): the fused sigmoid-dot is m-INVARIANT and
5082                    // serves EVERY t (serve isolation, lane/concat-prime-exact).
5083                    if t < PRIME_MIN_T || crate::router_prefill_exact_on() {
5084                        e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
5085                    } else {
5086                        let gs = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
5087                        let mut g = e.uninit(t)?;
5088                        e.sigmoid(&gs, &mut g, t)?;
5089                        g
5090                    }
5091                }
5092                None => e.htod(&vec![1.0f32; t])?,
5093            };
5094            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, t)?;
5095        }
5096
5097        Ok(moe_out)
5098    }
5099
5100    /// STAGE-2 GROUPED DECODE (2026-07-04): run ONE token's whole routed-expert FFN in TWO
5101    /// launches when every one of its 3*n_used blocks is ALREADY cache-resident. Returns
5102    /// Ok(true) if the grouped path ran (caller skips the sequential loop for this token);
5103    /// Ok(false) on ANY miss (caller falls through — the sequential loop stages/admits as
5104    /// before, so the NEXT occurrence takes the grouped path). Pointer safety: cache slots are
5105    /// fixed-address for the engine's lifetime and the pure-HIT path performs no admission, so
5106    /// the collected raw pointers cannot move between collection and launch (single-threaded
5107    /// decode; the lock is held only for collection, launches are stream-ordered after any
5108    /// prior same-stream staging writes).
5109    #[allow(clippy::too_many_arguments)]
5110    /// q8 twin of moe_gdec_token (dp4a arc): same residency check + 2-launch shape; the fused
5111    /// kernels consume the pre-quantized z-row and re-quantize act per slot batch.
5112    #[allow(clippy::too_many_arguments)]
5113    fn moe_gdec_token_q8(e: &Engine, m: &MoeWeights, il: u16, max_block: usize,
5114                      zq: &CudaSlice<i8>, zd: &CudaSlice<f32>, sel: &[u32], w: &[f32],
5115                      moe_out: &mut CudaSlice<f32>, tok: usize,
5116                      n_embd: usize, n_ff_exp: usize, n_used: usize)
5117                      -> Result<bool, Box<dyn std::error::Error>> {
5118        use crate::moe_cache::{BlockId, PROJ_GATE, PROJ_UP, PROJ_DOWN};
5119        use cudarc::driver::DevicePtr;
5120        let ptrs = e.with_moe_cache(max_block, |c, eng| {
5121            let mut g = [0u64; 8];
5122            let mut u = [0u64; 8];
5123            let mut d = [0u64; 8];
5124            for (j, &ex) in sel.iter().enumerate() {
5125                let ex = ex as u16;
5126                let (Some(sg), Some(su), Some(sd)) = (c.resident(BlockId::new(il, PROJ_GATE, ex)),
5127                                                      c.resident(BlockId::new(il, PROJ_UP,   ex)),
5128                                                      c.resident(BlockId::new(il, PROJ_DOWN, ex)))
5129                else { return Ok(None); };
5130                let __s = eng.stream();
5131                let (pg, _e0) = c.slot(sg).device_ptr(&__s);
5132                let (pu, _e1) = c.slot(su).device_ptr(&__s);
5133                let (pd, _e2) = c.slot(sd).device_ptr(&__s);
5134                g[j] = pg as u64; u[j] = pu as u64; d[j] = pd as u64;
5135            }
5136            if cpu_expert_profile_admit_enabled() && !c.is_frozen() {
5137                for &ex in sel {
5138                    let ex = ex as u16;
5139                    for proj in [PROJ_GATE, PROJ_UP, PROJ_DOWN] {
5140                        c.note_profile_hit(BlockId::new(il, proj, ex));
5141                    }
5142                }
5143            }
5144            c.hits += (3 * n_used) as u64;
5145            Ok(Some((g, u, d)))
5146        })?;
5147        let Some((g, u, d)) = ptrs else { return Ok(false) };
5148        let mut wv = [0f32; 8];
5149        wv[..n_used].copy_from_slice(w);
5150        let act = e.moe_gate_up_silu8_q8(crate::WPtr8(g), crate::WPtr8(u), zq, zd,
5151                                         n_embd, n_ff_exp, n_used,
5152                                         m.gate_exps.qtype, m.up_exps.qtype,
5153                                         m.gate_exps.row_bytes, m.up_exps.row_bytes)?;
5154        // per-slot act quantize: [n_used, n_ff] rows in one quantize launch.
5155        let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
5156        let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
5157        e.moe_down8_fma_q8(crate::WPtr8(d), crate::F32x8(wv), &aq2, &ad2, &mut dst,
5158                           n_ff_exp, n_embd, n_used,
5159                           m.down_exps.qtype, m.down_exps.row_bytes)?;
5160        Ok(true)
5161    }
5162
5163    fn moe_gdec_token(e: &Engine, m: &MoeWeights, il: u16, max_block: usize,
5164                      zt: &cudarc::driver::CudaView<f32>, sel: &[u32], w: &[f32],
5165                      moe_out: &mut CudaSlice<f32>, tok: usize,
5166                      n_embd: usize, n_ff_exp: usize, n_used: usize)
5167                      -> Result<bool, Box<dyn std::error::Error>> {
5168        use crate::moe_cache::{BlockId, PROJ_GATE, PROJ_UP, PROJ_DOWN};
5169        use cudarc::driver::DevicePtr;
5170        // One lock hold: residency-check all 3*n_used blocks, collect raw slot pointers.
5171        let ptrs = e.with_moe_cache(max_block, |c, eng| {
5172            let mut g = [0u64; 8];
5173            let mut u = [0u64; 8];
5174            let mut d = [0u64; 8];
5175            for (j, &ex) in sel.iter().enumerate() {
5176                let ex = ex as u16;
5177                let (Some(sg), Some(su), Some(sd)) = (c.resident(BlockId::new(il, PROJ_GATE, ex)),
5178                                                      c.resident(BlockId::new(il, PROJ_UP,   ex)),
5179                                                      c.resident(BlockId::new(il, PROJ_DOWN, ex)))
5180                else { return Ok(None); };
5181                let __s = eng.stream();
5182                let (pg, _e0) = c.slot(sg).device_ptr(&__s);
5183                let (pu, _e1) = c.slot(su).device_ptr(&__s);
5184                let (pd, _e2) = c.slot(sd).device_ptr(&__s);
5185                g[j] = pg as u64; u[j] = pu as u64; d[j] = pd as u64;
5186            }
5187            if cpu_expert_profile_admit_enabled() && !c.is_frozen() {
5188                for &ex in sel {
5189                    let ex = ex as u16;
5190                    for proj in [PROJ_GATE, PROJ_UP, PROJ_DOWN] {
5191                        c.note_profile_hit(BlockId::new(il, proj, ex));
5192                    }
5193                }
5194            }
5195            c.hits += (3 * n_used) as u64; // instrumentation parity with dispatch()
5196            Ok(Some((g, u, d)))
5197        })?;
5198        let Some((g, u, d)) = ptrs else { return Ok(false) };
5199        let mut wv = [0f32; 8];
5200        wv[..n_used].copy_from_slice(w);
5201        // 2 launches: (gate+up+silu) x8, then (down + slot-ordered FMA accumulate) x8.
5202        let act = e.moe_gate_up_silu8(crate::WPtr8(g), crate::WPtr8(u), zt,
5203                                      n_embd, n_ff_exp, n_used,
5204                                      m.gate_exps.qtype, m.up_exps.qtype,
5205                                      m.gate_exps.row_bytes, m.up_exps.row_bytes)?;
5206        let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
5207        e.moe_down8_fma_into(crate::WPtr8(d), crate::F32x8(wv), &act, &mut dst,
5208                             n_ff_exp, n_embd, n_used,
5209                             m.down_exps.qtype, m.down_exps.row_bytes)?;
5210        Ok(true)
5211    }
5212
5213    /// EDGE-1 §B.3: dispatch one expert projection through the SLRU cache, then run the SAME
5214    /// `qmatvec_view` from whichever slot it landed in (resident HIT or staged MISS). `x` is the
5215    /// sliced activation row. `proj` selects the gate/up/down HostExps tensor. Returns y = W_expert @ x.
5216    /// q8 twin of moe_cached_gemm: same dispatch/slot mechanics, dp4a expert kernel.
5217    fn moe_cached_gemm_q8(e: &Engine, il: u16, proj: u8, ex: usize, m: &MoeWeights,
5218                          max_block: usize, aq: &CudaSlice<i8>, ad: &CudaSlice<f32>)
5219                          -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
5220        use crate::moe_cache::{BlockId, DispatchSlot, PROJ_GATE, PROJ_UP};
5221        let exps = match proj { PROJ_GATE => &m.gate_exps, PROJ_UP => &m.up_exps, _ => &m.down_exps };
5222        let layout = exps.expert_layout(ex);
5223        let id = BlockId::new(il, proj, ex as u16);
5224        let source = exps.expert_source(ex);
5225        e.with_moe_cache(max_block, |c, eng| {
5226            let slot = c.dispatch_source(id, source, eng)?;
5227            let DispatchSlot::Resident(sl) = slot;
5228            let buf = c.slot(sl);
5229            eng.qmatvec_expert_q8(buf, 0..layout.len, aq, ad, 1, exps.in_f, exps.out_f,
5230                                  layout.qtype, layout.row_bytes)
5231        })
5232    }
5233
5234    fn moe_cached_gemm(e: &Engine, il: u16, proj: u8, ex: usize, m: &MoeWeights,
5235                       max_block: usize, x: &cudarc::driver::CudaView<f32>)
5236                       -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
5237        use crate::moe_cache::{BlockId, DispatchSlot, PROJ_GATE, PROJ_UP};
5238        let exps = match proj { PROJ_GATE => &m.gate_exps, PROJ_UP => &m.up_exps, _ => &m.down_exps };
5239        let layout = exps.expert_layout(ex);
5240        let id = BlockId::new(il, proj, ex as u16);
5241        let source = exps.expert_source(ex);
5242        // dispatch under the lock (lookup/admit/memcpy-issue), then resolve the slot and GEMM.
5243        e.with_moe_cache(max_block, |c, eng| {
5244            let slot = c.dispatch_source(id, source, eng)?;
5245            // resolve the device buffer for this slot; the GEMM is enqueued on the compute stream
5246            // (the same stream the memcpy was issued on, so ordering holds without extra sync).
5247            let DispatchSlot::Resident(sl) = slot;
5248            let buf = c.slot(sl);
5249            eng.qmatvec_view(buf, 0..layout.len, x, 1, exps.in_f, exps.out_f,
5250                             layout.qtype, layout.row_bytes)
5251        })
5252    }
5253
5254    /// Populate the warmup cache for an expert whose current-token output intentionally ran on
5255    /// CPU. No GEMM is launched and callers invoke this only after the CPU result has completed,
5256    /// so the current forward's backend assignment and output remain unchanged.
5257    fn moe_profile_admit_expert(
5258        e: &Engine,
5259        il: u16,
5260        ex: usize,
5261        m: &MoeWeights,
5262        max_block: usize,
5263    ) -> Result<(), Box<dyn std::error::Error>> {
5264        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
5265        e.with_moe_cache(max_block, |cache, eng| {
5266            for (proj, exps) in [
5267                (PROJ_GATE, &m.gate_exps),
5268                (PROJ_UP, &m.up_exps),
5269                (PROJ_DOWN, &m.down_exps),
5270            ] {
5271                let id = BlockId::new(il, proj, ex as u16);
5272                let _ = cache.dispatch_source(id, exps.expert_source(ex), eng)?;
5273            }
5274            Ok(())
5275        })
5276    }
5277
5278    /// Read a projection from the immutable residency set when present; otherwise use one
5279    /// transient slot without admitting or evicting anything. Used only by post-freeze prefill.
5280    #[allow(clippy::too_many_arguments)]
5281    fn moe_frozen_gemm(
5282        e: &Engine,
5283        il: u16,
5284        proj: u8,
5285        ex: usize,
5286        m: &MoeWeights,
5287        max_block: usize,
5288        x: &cudarc::driver::CudaView<f32>,
5289        scratch: &mut Option<CudaSlice<u8>>,
5290        scratch_len: usize,
5291    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
5292        use crate::moe_cache::{BlockId, PROJ_GATE, PROJ_UP};
5293        let exps = match proj {
5294            PROJ_GATE => &m.gate_exps,
5295            PROJ_UP => &m.up_exps,
5296            _ => &m.down_exps,
5297        };
5298        let layout = exps.expert_layout(ex);
5299        let id = BlockId::new(il, proj, ex as u16);
5300        if let Some(output) = e.with_moe_cache(max_block, |cache, eng| {
5301            let Some(slot) = cache.resident(id) else {
5302                return Ok(None);
5303            };
5304            let buf = cache.slot(slot);
5305            Ok(Some(eng.qmatvec_view(
5306                buf,
5307                0..layout.len,
5308                x,
5309                1,
5310                exps.in_f,
5311                exps.out_f,
5312                layout.qtype,
5313                layout.row_bytes,
5314            )?))
5315        })? {
5316            return Ok(output);
5317        }
5318        if scratch.is_none() {
5319            *scratch = Some(e.alloc_u8_uninit(scratch_len)?);
5320        }
5321        let scratch = scratch.as_mut().unwrap();
5322        e.stage_expert(exps.expert_bytes(ex), scratch, 0)?;
5323        e.qmatvec_view(
5324            scratch,
5325            0..layout.len,
5326            x,
5327            1,
5328            exps.in_f,
5329            exps.out_f,
5330            layout.qtype,
5331            layout.row_bytes,
5332        )
5333    }
5334
5335    fn moe_prefetch_expert(
5336        e: &Engine,
5337        il: u16,
5338        ex: usize,
5339        m: &MoeWeights,
5340        max_block: usize,
5341        keep: &[crate::moe_cache::BlockId],
5342    ) -> Result<(), Box<dyn std::error::Error>> {
5343        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
5344        e.with_moe_cache(max_block, |c, eng| {
5345            for (proj, exps) in [(PROJ_GATE, &m.gate_exps), (PROJ_UP, &m.up_exps),
5346                                 (PROJ_DOWN, &m.down_exps)] {
5347                let id = BlockId::new(il, proj, ex as u16);
5348                let _ = c.prefetch_source(id, exps.expert_source(ex), keep, eng)?;
5349            }
5350            Ok(())
5351        })
5352    }
5353
5354    /// Worker-mode disk lookahead for grouped prefill. Memory sources are deliberately skipped so
5355    /// selecting `worker` changes only storage scheduling here; all CUDA work stays in dispatch.
5356    fn moe_prefetch_disk_expert(e: &Engine, il: u16, ex: usize, m: &MoeWeights,
5357                                max_block: usize, keep: &[crate::moe_cache::BlockId])
5358                                -> Result<(), Box<dyn std::error::Error>> {
5359        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
5360        e.with_moe_cache(max_block, |c, eng| {
5361            for (proj, exps) in [(PROJ_GATE, &m.gate_exps), (PROJ_UP, &m.up_exps),
5362                                 (PROJ_DOWN, &m.down_exps)] {
5363                let source = exps.expert_source(ex);
5364                if let crate::model::ExpertSource::Disk { .. } = &source {
5365                    let id = BlockId::new(il, proj, ex as u16);
5366                    let _ = c.prefetch_source(id, source, keep, eng)?;
5367                }
5368            }
5369            Ok(())
5370        })
5371    }
5372
5373    #[inline]
5374    fn moe_prefetch_host_expert(ex: usize, m: &MoeWeights) {
5375        let _ = m.gate_exps.prefetch_expert_pages(ex);
5376        let _ = m.up_exps.prefetch_expert_pages(ex);
5377        let _ = m.down_exps.prefetch_expert_pages(ex);
5378    }
5379}
5380
5381// ================================================================================================
5382// A2: EXPERT-GROUPED MoE PREFILL (MEMRA_MOE_GROUPED=1). Resident-case prototype.
5383//
5384// Instead of the per-token loop (T * 8 experts * 3 projections = 12024 individual m=1 matvecs),
5385// this groups tokens by expert and runs ONE matmul per active expert per projection at m=m_e.
5386// On a 501-token prefill with ~170 active experts, that's ~510 matmuls (vs 12024).
5387//
5388// EXACTNESS: per-token accumulation across its 8 experts is reordered (grouped processes experts
5389// in expert-id order, not the router's top-k order). To preserve bit-identity with the sequential
5390// loop, we use an 8-SLOT scheme: expert outputs are scattered into slots keyed by the token's
5391// top-k position (0..7), then reduced in that fixed order. This makes the f32 addition order
5392// identical to the per-token loop regardless of expert processing order.
5393//
5394// Memory: T * 8 * n_embd * 4 = 501 * 8 * 2048 * 4 = ~32 MB (slot buffer). Fine on 96GB.
5395// ================================================================================================
5396
5397impl HybridModel {
5398    /// Resident-slab fast path for host-routed grouped prefill. Unclamped layers batch the
5399    /// sequential fused q8 program over the token axis; clamped layers use the separate
5400    /// expert-major q8 chain so `ffn_act_lim` remains authoritative.
5401    #[allow(clippy::too_many_arguments)]
5402    fn moe_ffn_grouped_resident_q8(
5403        e: &Engine,
5404        m: &MoeWeights,
5405        z: &CudaSlice<f32>,
5406        t: usize,
5407        cfg: &ModelConfig,
5408        il: u16,
5409        sel_all: &[u32],
5410        w_all: &[f32],
5411        table: &CudaSlice<u64>,
5412        gu_il: bool,
5413    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
5414        let moe = cfg.moe.as_ref().unwrap();
5415        let n_embd = cfg.n_embd as usize;
5416        let n_expert = moe.expert_count as usize;
5417        let n_used = moe.expert_used_count as usize;
5418        let n_ff_exp = moe.expert_ff_length as usize;
5419        let n_pairs = t * n_used;
5420        debug_assert_eq!(sel_all.len(), n_pairs);
5421        debug_assert_eq!(w_all.len(), n_pairs);
5422        debug_assert!(
5423            m.gate_exps.macros.is_none()
5424                && m.up_exps.macros.is_none()
5425                && m.down_exps.macros.is_none(),
5426            "resident grouped q8 does not fold per-expert macro scales",
5427        );
5428
5429        // The rows twins run the resident sequential program verbatim on grid.z = token:
5430        // fused gate/up/SiLU per slot, batched activation quantization, then the original
5431        // slot-ordered down/FMA chain. Routing remains the host sigmoid oracle above; these
5432        // kernels consume sel/w only and never enter the softmax device router.
5433        if !cfg.swiglu_clamped_at(il as u32) {
5434            let sel: Vec<i32> = sel_all.iter().map(|&expert| expert as i32).collect();
5435            let sel_d = e.htod_i32(&sel)?;
5436            let w_d = e.htod(w_all)?;
5437            let (gate_row_bytes, up_row_bytes) = if gu_il {
5438                let combined = m.gate_exps.row_bytes + m.up_exps.row_bytes;
5439                (combined, combined)
5440            } else {
5441                (m.gate_exps.row_bytes, m.up_exps.row_bytes)
5442            };
5443            let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
5444            let act = e.moe_gate_up_silu8_dev_q8_rows(
5445                table,
5446                &sel_d,
5447                &zq,
5448                &zd,
5449                t,
5450                n_embd,
5451                n_ff_exp,
5452                n_used,
5453                n_expert,
5454                m.gate_exps.qtype,
5455                m.up_exps.qtype,
5456                gate_row_bytes,
5457                up_row_bytes,
5458                &m.dev_macros,
5459            )?;
5460            let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
5461            let mut moe_out = e.uninit(t * n_embd)?;
5462            e.moe_down8_fma_dev_q8_rows_g(
5463                table,
5464                &sel_d,
5465                &w_d,
5466                &aq2,
5467                &ad2,
5468                &mut moe_out,
5469                t,
5470                n_ff_exp,
5471                n_embd,
5472                n_used,
5473                n_expert,
5474                m.down_exps.qtype,
5475                m.down_exps.row_bytes,
5476            )?;
5477
5478            if std::env::var("MEMRA_MOE_STATS").is_ok() {
5479                let mut counts = vec![0usize; n_expert];
5480                for &expert in sel_all {
5481                    counts[expert as usize] += 1;
5482                }
5483                let mut sizes: Vec<usize> =
5484                    counts.into_iter().filter(|&count| count != 0).collect();
5485                sizes.sort_unstable();
5486                let mean = sizes.iter().sum::<usize>() as f64 / sizes.len().max(1) as f64;
5487                println!(
5488                    "moe-grouped il={il} t={t} dispatch=resident-q8-rows active={}/{} \
5489                     m_e: min={} median={} mean={mean:.1} max={}",
5490                    sizes.len(),
5491                    n_expert,
5492                    sizes.first().copied().unwrap_or(0),
5493                    sizes.get(sizes.len() / 2).copied().unwrap_or(0),
5494                    sizes.last().copied().unwrap_or(0),
5495                );
5496            }
5497            return Ok(moe_out);
5498        }
5499
5500        // Clamped layers keep gate/up, activation, and down as separate stages. The pair-major
5501        // matvec body is qmatvec_expert_q8 verbatim, while one launch covers every routed pair.
5502        // Pair ids stay in router slot order so scatter preserves the sequential FMA chain.
5503        let pair_tok: Vec<i32> = (0..n_pairs).map(|pair| (pair / n_used) as i32).collect();
5504        let pair_ex: Vec<i32> = sel_all.iter().map(|&expert| expert as i32).collect();
5505        let tok_off: Vec<i32> = (0..=t).map(|tok| (tok * n_used) as i32).collect();
5506        let tok_ids: Vec<i32> = (0..n_pairs as i32).collect();
5507
5508        let mut by_expert: Vec<Vec<i32>> = vec![Vec::new(); n_expert];
5509        for (pair, &expert) in pair_ex.iter().enumerate() {
5510            by_expert[expert as usize].push(pair as i32);
5511        }
5512
5513        let pair_tok_d = e.htod_i32(&pair_tok)?;
5514        let pair_ex_d = e.htod_i32(&pair_ex)?;
5515        let pair_w_d = e.htod(w_all)?;
5516        let tok_off_d = e.htod_i32(&tok_off)?;
5517        let tok_ids_d = e.htod_i32(&tok_ids)?;
5518
5519        let matvec = |
5520            proj: i32,
5521            pair_rows: &CudaSlice<i32>,
5522            aq: &CudaSlice<i8>,
5523            ad: &CudaSlice<f32>,
5524            in_f: usize,
5525            out_f: usize,
5526            qtype: i32,
5527            row_bytes: usize,
5528        | -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
5529            e.moe_pairs_matvec_q8(
5530                table,
5531                proj,
5532                pair_rows,
5533                &pair_ex_d,
5534                aq,
5535                ad,
5536                in_f,
5537                out_f,
5538                n_expert,
5539                n_pairs,
5540                qtype,
5541                row_bytes,
5542            )
5543        };
5544
5545        let (gate_row_bytes, up_row_bytes) = if gu_il {
5546            let combined = m.gate_exps.row_bytes + m.up_exps.row_bytes;
5547            (combined, combined)
5548        } else {
5549            (m.gate_exps.row_bytes, m.up_exps.row_bytes)
5550        };
5551        let (zq, zd) = e.quantize_q8_1(z, t, n_embd)?;
5552        let gate = matvec(
5553            0,
5554            &pair_tok_d,
5555            &zq,
5556            &zd,
5557            n_embd,
5558            n_ff_exp,
5559            m.gate_exps.qtype,
5560            gate_row_bytes,
5561        )?;
5562        let up = matvec(
5563            1,
5564            &pair_tok_d,
5565            &zq,
5566            &zd,
5567            n_embd,
5568            n_ff_exp,
5569            m.up_exps.qtype,
5570            up_row_bytes,
5571        )?;
5572        let mut act = e.uninit(n_pairs * n_ff_exp)?;
5573        Self::ffn_act_lim(
5574            e,
5575            cfg,
5576            &gate,
5577            &up,
5578            1.0,
5579            1.0,
5580            cfg.clamp_exp_at(il as u32),
5581            &mut act,
5582            n_pairs * n_ff_exp,
5583        )?;
5584        let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
5585        let pair_self: Vec<i32> = (0..n_pairs as i32).collect();
5586        let pair_self_d = e.htod_i32(&pair_self)?;
5587        let down = matvec(
5588            2,
5589            &pair_self_d,
5590            &aq2,
5591            &ad2,
5592            n_ff_exp,
5593            n_embd,
5594            m.down_exps.qtype,
5595            m.down_exps.row_bytes,
5596        )?;
5597        let mut moe_out = e.uninit(t * n_embd)?;
5598        e.moe_pairs_scatter(
5599            &down,
5600            &pair_w_d,
5601            &tok_off_d,
5602            &tok_ids_d,
5603            &mut moe_out,
5604            t,
5605            n_embd,
5606        )?;
5607
5608        if std::env::var("MEMRA_MOE_STATS").is_ok() {
5609            let mut sizes: Vec<usize> = by_expert
5610                .iter()
5611                .filter_map(|pairs| (!pairs.is_empty()).then_some(pairs.len()))
5612                .collect();
5613            sizes.sort_unstable();
5614            let mean = sizes.iter().sum::<usize>() as f64 / sizes.len().max(1) as f64;
5615            println!(
5616                "moe-grouped il={il} t={t} dispatch=resident-q8-clamped-pairs active={}/{} \
5617                 m_e: min={} median={} mean={mean:.1} max={}",
5618                sizes.len(),
5619                n_expert,
5620                sizes.first().copied().unwrap_or(0),
5621                sizes.get(sizes.len() / 2).copied().unwrap_or(0),
5622                sizes.last().copied().unwrap_or(0),
5623            );
5624        }
5625        Ok(moe_out)
5626    }
5627
5628    fn moe_ffn_grouped_add_shared(
5629        e: &Engine,
5630        m: &MoeWeights,
5631        z: &CudaSlice<f32>,
5632        t: usize,
5633        cfg: &ModelConfig,
5634        il: u16,
5635        moe_out: &mut CudaSlice<f32>,
5636    ) -> Result<(), Box<dyn std::error::Error>> {
5637        let n_embd = cfg.n_embd as usize;
5638        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
5639            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
5640        {
5641            let n_ff_sh = gate_shexp.out_features();
5642            let sg_gate = e.matmul(gate_shexp, z, t)?;
5643            let sg_up = e.matmul(up_shexp, z, t)?;
5644            let mut sa = e.uninit(t * n_ff_sh)?;
5645            Self::ffn_act_lim(
5646                e,
5647                cfg,
5648                &sg_gate,
5649                &sg_up,
5650                1.0,
5651                1.0,
5652                cfg.clamp_shexp_at(il as u32),
5653                &mut sa,
5654                t * n_ff_sh,
5655            )?;
5656            let sh = e.matmul(down_shexp, &sa, t)?;
5657            let gate = match &m.gate_inp_shexp {
5658                Some(gate_inp_shexp) => {
5659                    if t < PRIME_MIN_T || crate::router_prefill_exact_on() {
5660                        e.sigmoid_dot_rows(z, gate_inp_shexp.float_data(), n_embd, t)?
5661                    } else {
5662                        let raw = e.linear(z, gate_inp_shexp.float_data(), t, n_embd, 1)?;
5663                        let mut gate = e.uninit(t)?;
5664                        e.sigmoid(&raw, &mut gate, t)?;
5665                        gate
5666                    }
5667                }
5668                None => e.htod(&vec![1.0f32; t])?,
5669            };
5670            e.add_scaled_rows(&sh, &gate, moe_out, n_embd, t)?;
5671        }
5672        Ok(())
5673    }
5674
5675    /// A2 expert-grouped MoE FFN (prefill path, MEMRA_MOE_GROUPED=1). Same semantics as moe_ffn:
5676    /// z [T, n_embd] -> moe_out [T, n_embd]. BIT-IDENTICAL to moe_ffn when using the slot scheme.
5677    pub(crate) fn moe_ffn_grouped(e: &Engine, m: &MoeWeights, z: &CudaSlice<f32>, t: usize,
5678                                  cfg: &ModelConfig, il: u16, max_block: usize)
5679                   -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
5680        let moe = cfg.moe.as_ref().unwrap();
5681        let n_embd = cfg.n_embd as usize;
5682        let n_expert = moe.expert_count as usize;
5683        let n_used = moe.expert_used_count as usize;
5684        let n_ff_exp = moe.expert_ff_length as usize;
5685        // step35 per-layer SwiGLU clamp; None on every other arch / unclamped layer.
5686        let lim_exp = cfg.clamp_exp_at(il as u32);
5687
5688        // 1. ROUTER: exactly the same m-invariant selector and host sigmoid oracle as the
5689        // sequential path. The grouped dispatch never enters the softmax-only pairs/dev router.
5690        let logits = Self::moe_router_logits(e, m, z, t, cfg)?;
5691        let (sel_all, w_all) = if let Some(sig) = cfg.sigmoid_router() {
5692            Self::moe_route_cfg(e, &logits, t, n_expert, n_used,
5693                                m.exp_probs_b.as_deref(), Some(sig), m.active_experts.as_deref())?
5694        } else {
5695            Self::moe_route_cfg(e, &logits, t, n_expert, n_used,
5696                                None, None, m.active_experts.as_deref())?
5697        };
5698        Self::trace_moe_routes(il, t, &sel_all, &w_all)?;
5699        Self::trace_moe_input(e, il, t, n_embd, z)?;
5700
5701        // Fits-VRAM Step35 under the PP stage split: use the expert-major q8 arithmetic directly
5702        // from the owning device's uniform resident slab. This is not `moe_ffn_pairs`: routing
5703        // already happened above and the activation is clamp-aware. Mixed layouts, remote slabs,
5704        // macro-scaled experts, q8-disabled configs, and spill fall through to metadata-aware A2.
5705        let no_exp_macros = m.gate_exps.macros.is_none()
5706            && m.up_exps.macros.is_none()
5707            && m.down_exps.macros.is_none();
5708        let resident_q8 = m.dev_exps.as_ref().filter(|dev| {
5709            m.has_uniform_expert_layout()
5710                && no_exp_macros
5711                && moe_q8_enabled()
5712                && q8_expert_supported(m.gate_exps.qtype)
5713                && q8_expert_supported(m.up_exps.qtype)
5714                && q8_expert_supported(m.down_exps.qtype)
5715                && moe_slab_enabled()
5716                && dev.dev == e.ctx().ordinal()
5717        });
5718        if let Some(dev) = resident_q8 {
5719            let mut moe_out = Self::moe_ffn_grouped_resident_q8(
5720                e,
5721                m,
5722                z,
5723                t,
5724                cfg,
5725                il,
5726                &sel_all,
5727                &w_all,
5728                &dev.ptr_row,
5729                dev.gu_il,
5730            )?;
5731            Self::moe_ffn_grouped_add_shared(e, m, z, t, cfg, il, &mut moe_out)?;
5732            return Ok(moe_out);
5733        }
5734
5735        // 2. BUILD PER-EXPERT TOKEN LISTS (host-side grouping).
5736        // For each expert e, we need: which tokens use it, their positions in z, their top-k
5737        // slot index (for bit-identical accumulation), and their weights.
5738        struct ExpertGroup {
5739            tok_indices: Vec<i32>,   // indices into z rows (0..T-1)
5740            slot_indices: Vec<i32>,  // top-k slot (0..n_used-1) for that token-expert pair
5741            weights: Vec<f32>,       // renormalized weight for that token-expert pair
5742        }
5743        let mut groups: Vec<ExpertGroup> = (0..n_expert).map(|_| ExpertGroup {
5744            tok_indices: Vec::new(), slot_indices: Vec::new(), weights: Vec::new(),
5745        }).collect();
5746
5747        for tok in 0..t {
5748            for j in 0..n_used {
5749                let ex = sel_all[tok * n_used + j] as usize;
5750                let w = w_all[tok * n_used + j];
5751                groups[ex].tok_indices.push(tok as i32);
5752                groups[ex].slot_indices.push(j as i32);
5753                groups[ex].weights.push(w);
5754            }
5755        }
5756
5757        // 3. ALLOCATE SLOT BUFFER: [T, n_used, n_embd] f32, zero-initialized.
5758        // Each token's 8 expert contributions land in their respective slots.
5759        let mut slot_buf = e.zeros(t * n_used * n_embd)?;
5760        let mut wbuf = e.zeros(t * n_used)?;  // [T, n_used] weight buffer for FMA reduce
5761
5762        // Expert weight dimensions (used in both cache and staging paths).
5763        let g_len = m.gate_exps.max_expert_bytes();
5764        let u_len = m.up_exps.max_expert_bytes();
5765        let d_len = m.down_exps.max_expert_bytes();
5766        let moe_q8 = m.has_uniform_expert_layout()
5767            && moe_q8_enabled()
5768            && q8_expert_supported(m.gate_exps.qtype)
5769            && q8_expert_supported(m.up_exps.qtype)
5770            && q8_expert_supported(m.down_exps.qtype);
5771        // The per-expert slab view is legal only for the ordinary contiguous uniform layout.
5772        // Interleaved GU slabs require the pointer-table fast path above.
5773        let slab_local = m.dev_exps.as_ref().filter(|dev| {
5774            !dev.gu_il && moe_slab_enabled() && dev.dev == e.ctx().ordinal()
5775        });
5776        let use_cache =
5777            slab_local.is_none() && Engine::moe_cache_enabled() && !e.moe_cache_frozen();
5778        // The sequential no-cache/frozen staging oracle is f32. Use q8 only where sequential
5779        // also does: a local resident slab or a live SLRU dispatch.
5780        let grouped_q8 = moe_q8 && (slab_local.is_some() || use_cache);
5781
5782        // GPU scratch for staging (only allocated without a local slab or cache).
5783        let (mut scratch_g, mut scratch_u, mut scratch_d) = if slab_local.is_none() && !use_cache {
5784            (Some(e.alloc_u8(g_len)?), Some(e.alloc_u8(u_len)?), Some(e.alloc_u8(d_len)?))
5785        } else {
5786            (None, None, None)
5787        };
5788
5789        // 4. PER ACTIVE EXPERT: gather, compute, scatter.
5790        // Processing ORDER: DESCENDING m_e (biggest token batches first) — the concluded winner
5791        // (rig5090 2026-07-04, the ascending-id arm and its MEMRA_MOE_ORDER seam removed): desc is
5792        // a first-forward win at partial cache capacity — the hot (big-m_e) experts are admitted
5793        // to the SLRU before the small-m_e tail can pollute it, so residency converges in ONE
5794        // forward instead of several: auto-cache T=501 126.9 -> 169.9 tok/s (1.34x), cap512
5795        // 119.6 -> 160.8 (and kills the rep-to-rep bimodal); wash (<2%) at cap64 pure-spill and
5796        // at long prompts where every expert stages regardless. Order is FREE to change without
5797        // breaking the byte-identity gate: the slot scheme pins each token's accumulation order
5798        // regardless of expert processing order (the whole point of the slots).
5799        let mut order: Vec<usize> =
5800            (0..n_expert).filter(|&ex| !groups[ex].tok_indices.is_empty()).collect();
5801        order.sort_by(|&a, &b| groups[b].tok_indices.len()
5802            .cmp(&groups[a].tok_indices.len()).then(a.cmp(&b)));
5803        let mut m_dist: Vec<usize> = Vec::new();  // for stats
5804        let page_window = moe_page_prefetch_window();
5805        let worker_disk_prefetch = use_cache && crate::spill_pread::worker_enabled();
5806        if worker_disk_prefetch {
5807            if let Some(first) = grouped_worker_prefetch_position(order.len(), None) {
5808                Self::moe_prefetch_disk_expert(e, il, order[first], m, max_block, &[])?;
5809            }
5810        }
5811        for (order_pos, &ex) in order.iter().enumerate() {
5812            for next in page_prefetch_positions(order_pos, order.len(), page_window) {
5813                Self::moe_prefetch_host_expert(order[next], m);
5814            }
5815            if worker_disk_prefetch {
5816                if let Some(next) = grouped_worker_prefetch_position(order.len(), Some(order_pos)) {
5817                    use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
5818                    let keep = [
5819                        BlockId::new(il, PROJ_GATE, ex as u16),
5820                        BlockId::new(il, PROJ_UP, ex as u16),
5821                        BlockId::new(il, PROJ_DOWN, ex as u16),
5822                    ];
5823                    Self::moe_prefetch_disk_expert(e, il, order[next], m, max_block, &keep)?;
5824                }
5825            }
5826            let grp = &groups[ex];
5827            let m_e = grp.tok_indices.len();
5828            m_dist.push(m_e);
5829            let gl = m.gate_exps.expert_layout(ex);
5830            let ul = m.up_exps.expert_layout(ex);
5831            let dl = m.down_exps.expert_layout(ex);
5832
5833            // Upload index/weight arrays to device. The down-proj per-expert macro-scale
5834            // (ModelOpt weight_scale_2) folds into the scatter weights — post-matmul linear,
5835            // same fold as the sequential loop's `w[j] * macro_scale(ex)`. 1.0 for GGUF experts.
5836            let tok_idx_d = e.htod_i32(&grp.tok_indices)?;
5837            let slot_idx_d = e.htod_i32(&grp.slot_indices)?;
5838            let dmac = m.down_exps.macro_scale(ex);
5839            let weight_d = if dmac == 1.0 { e.htod(&grp.weights)? } else {
5840                let scaled: Vec<f32> = grp.weights.iter().map(|&w| w * dmac).collect();
5841                e.htod(&scaled)?
5842            };
5843
5844            // GATHER: collect m_e activation rows from z into a contiguous buffer.
5845            let mut gathered = e.zeros(m_e * n_embd)?;
5846            e.gather_rows(z, &tok_idx_d, &mut gathered, n_embd, m_e)?;
5847            let gv = gathered.slice(0..m_e * n_embd);
5848
5849            // Compute gate/up/down from the local slab, metadata-aware cache, or staging. The q8
5850            // form keeps each gathered row in the sequential arithmetic class at `m=m_e`.
5851            let y = if let Some(dev) = slab_local {
5852                let gate_start = ex * m.gate_exps.expert_stride;
5853                let up_start = ex * m.up_exps.expert_stride;
5854                let down_start = ex * m.down_exps.expert_stride;
5855                if grouped_q8 {
5856                    let (zq, zd) = e.quantize_q8_1(&gathered, m_e, n_embd)?;
5857                    let gate = e.qmatvec_expert_q8(
5858                        &dev.gate,
5859                        gate_start..gate_start + gl.len,
5860                        &zq,
5861                        &zd,
5862                        m_e,
5863                        m.gate_exps.in_f,
5864                        m.gate_exps.out_f,
5865                        gl.qtype,
5866                        gl.row_bytes,
5867                    )?;
5868                    let up = e.qmatvec_expert_q8(
5869                        &dev.up,
5870                        up_start..up_start + ul.len,
5871                        &zq,
5872                        &zd,
5873                        m_e,
5874                        m.up_exps.in_f,
5875                        m.up_exps.out_f,
5876                        ul.qtype,
5877                        ul.row_bytes,
5878                    )?;
5879                    let mut act = e.uninit(m_e * n_ff_exp)?;
5880                    Self::ffn_act_lim(
5881                        e,
5882                        cfg,
5883                        &gate,
5884                        &up,
5885                        m.gate_exps.macro_scale(ex),
5886                        m.up_exps.macro_scale(ex),
5887                        lim_exp,
5888                        &mut act,
5889                        m_e * n_ff_exp,
5890                    )?;
5891                    let (aq2, ad2) = e.quantize_q8_1(&act, m_e, n_ff_exp)?;
5892                    e.qmatvec_expert_q8(
5893                        &dev.down,
5894                        down_start..down_start + dl.len,
5895                        &aq2,
5896                        &ad2,
5897                        m_e,
5898                        m.down_exps.in_f,
5899                        m.down_exps.out_f,
5900                        dl.qtype,
5901                        dl.row_bytes,
5902                    )?
5903                } else {
5904                    let gate = e.qmatvec_view(
5905                        &dev.gate,
5906                        gate_start..gate_start + gl.len,
5907                        &gv,
5908                        m_e,
5909                        m.gate_exps.in_f,
5910                        m.gate_exps.out_f,
5911                        gl.qtype,
5912                        gl.row_bytes,
5913                    )?;
5914                    let up = e.qmatvec_view(
5915                        &dev.up,
5916                        up_start..up_start + ul.len,
5917                        &gv,
5918                        m_e,
5919                        m.up_exps.in_f,
5920                        m.up_exps.out_f,
5921                        ul.qtype,
5922                        ul.row_bytes,
5923                    )?;
5924                    let mut act = e.uninit(m_e * n_ff_exp)?;
5925                    Self::ffn_act_lim(
5926                        e,
5927                        cfg,
5928                        &gate,
5929                        &up,
5930                        m.gate_exps.macro_scale(ex),
5931                        m.up_exps.macro_scale(ex),
5932                        lim_exp,
5933                        &mut act,
5934                        m_e * n_ff_exp,
5935                    )?;
5936                    let actv = act.slice(0..m_e * n_ff_exp);
5937                    e.qmatvec_view(
5938                        &dev.down,
5939                        down_start..down_start + dl.len,
5940                        &actv,
5941                        m_e,
5942                        m.down_exps.in_f,
5943                        m.down_exps.out_f,
5944                        dl.qtype,
5945                        dl.row_bytes,
5946                    )?
5947                }
5948            } else if use_cache {
5949                use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
5950                if grouped_q8 {
5951                    let (zq, zd) = e.quantize_q8_1(&gathered, m_e, n_embd)?;
5952                    let gate = e.with_moe_cache(max_block, |cache, eng| {
5953                        let id = BlockId::new(il, PROJ_GATE, ex as u16);
5954                        let slot =
5955                            cache.dispatch_source(id, m.gate_exps.expert_source(ex), eng)?;
5956                        eng.qmatvec_expert_q8(
5957                            cache.buf(slot),
5958                            0..gl.len,
5959                            &zq,
5960                            &zd,
5961                            m_e,
5962                            m.gate_exps.in_f,
5963                            m.gate_exps.out_f,
5964                            gl.qtype,
5965                            gl.row_bytes,
5966                        )
5967                    })?;
5968                    let up = e.with_moe_cache(max_block, |cache, eng| {
5969                        let id = BlockId::new(il, PROJ_UP, ex as u16);
5970                        let slot =
5971                            cache.dispatch_source(id, m.up_exps.expert_source(ex), eng)?;
5972                        eng.qmatvec_expert_q8(
5973                            cache.buf(slot),
5974                            0..ul.len,
5975                            &zq,
5976                            &zd,
5977                            m_e,
5978                            m.up_exps.in_f,
5979                            m.up_exps.out_f,
5980                            ul.qtype,
5981                            ul.row_bytes,
5982                        )
5983                    })?;
5984                    let mut act = e.uninit(m_e * n_ff_exp)?;
5985                    Self::ffn_act_lim(
5986                        e,
5987                        cfg,
5988                        &gate,
5989                        &up,
5990                        m.gate_exps.macro_scale(ex),
5991                        m.up_exps.macro_scale(ex),
5992                        lim_exp,
5993                        &mut act,
5994                        m_e * n_ff_exp,
5995                    )?;
5996                    let (aq2, ad2) = e.quantize_q8_1(&act, m_e, n_ff_exp)?;
5997                    e.with_moe_cache(max_block, |cache, eng| {
5998                        let id = BlockId::new(il, PROJ_DOWN, ex as u16);
5999                        let slot =
6000                            cache.dispatch_source(id, m.down_exps.expert_source(ex), eng)?;
6001                        eng.qmatvec_expert_q8(
6002                            cache.buf(slot),
6003                            0..dl.len,
6004                            &aq2,
6005                            &ad2,
6006                            m_e,
6007                            m.down_exps.in_f,
6008                            m.down_exps.out_f,
6009                            dl.qtype,
6010                            dl.row_bytes,
6011                        )
6012                    })?
6013                } else {
6014                    let gate = e.with_moe_cache(max_block, |cache, eng| {
6015                        let id = BlockId::new(il, PROJ_GATE, ex as u16);
6016                        let slot =
6017                            cache.dispatch_source(id, m.gate_exps.expert_source(ex), eng)?;
6018                        eng.qmatvec_view(
6019                            cache.buf(slot),
6020                            0..gl.len,
6021                            &gv,
6022                            m_e,
6023                            m.gate_exps.in_f,
6024                            m.gate_exps.out_f,
6025                            gl.qtype,
6026                            gl.row_bytes,
6027                        )
6028                    })?;
6029                    let up = e.with_moe_cache(max_block, |cache, eng| {
6030                        let id = BlockId::new(il, PROJ_UP, ex as u16);
6031                        let slot =
6032                            cache.dispatch_source(id, m.up_exps.expert_source(ex), eng)?;
6033                        eng.qmatvec_view(
6034                            cache.buf(slot),
6035                            0..ul.len,
6036                            &gv,
6037                            m_e,
6038                            m.up_exps.in_f,
6039                            m.up_exps.out_f,
6040                            ul.qtype,
6041                            ul.row_bytes,
6042                        )
6043                    })?;
6044                    let mut act = e.uninit(m_e * n_ff_exp)?;
6045                    Self::ffn_act_lim(
6046                        e,
6047                        cfg,
6048                        &gate,
6049                        &up,
6050                        m.gate_exps.macro_scale(ex),
6051                        m.up_exps.macro_scale(ex),
6052                        lim_exp,
6053                        &mut act,
6054                        m_e * n_ff_exp,
6055                    )?;
6056                    let actv = act.slice(0..m_e * n_ff_exp);
6057                    e.with_moe_cache(max_block, |cache, eng| {
6058                        let id = BlockId::new(il, PROJ_DOWN, ex as u16);
6059                        let slot =
6060                            cache.dispatch_source(id, m.down_exps.expert_source(ex), eng)?;
6061                        eng.qmatvec_view(
6062                            cache.buf(slot),
6063                            0..dl.len,
6064                            &actv,
6065                            m_e,
6066                            m.down_exps.in_f,
6067                            m.down_exps.out_f,
6068                            dl.qtype,
6069                            dl.row_bytes,
6070                        )
6071                    })?
6072                }
6073            } else {
6074                let sg = scratch_g.as_mut().unwrap();
6075                let su = scratch_u.as_mut().unwrap();
6076                let sd = scratch_d.as_mut().unwrap();
6077                e.stage_expert(m.gate_exps.expert_bytes(ex), sg, 0)?;
6078                e.stage_expert(m.up_exps.expert_bytes(ex), su, 0)?;
6079                e.stage_expert(m.down_exps.expert_bytes(ex), sd, 0)?;
6080                if grouped_q8 {
6081                    let (zq, zd) = e.quantize_q8_1(&gathered, m_e, n_embd)?;
6082                    let gate = e.qmatvec_expert_q8(
6083                        sg,
6084                        0..gl.len,
6085                        &zq,
6086                        &zd,
6087                        m_e,
6088                        m.gate_exps.in_f,
6089                        m.gate_exps.out_f,
6090                        gl.qtype,
6091                        gl.row_bytes,
6092                    )?;
6093                    let up = e.qmatvec_expert_q8(
6094                        su,
6095                        0..ul.len,
6096                        &zq,
6097                        &zd,
6098                        m_e,
6099                        m.up_exps.in_f,
6100                        m.up_exps.out_f,
6101                        ul.qtype,
6102                        ul.row_bytes,
6103                    )?;
6104                    let mut act = e.uninit(m_e * n_ff_exp)?;
6105                    Self::ffn_act_lim(
6106                        e,
6107                        cfg,
6108                        &gate,
6109                        &up,
6110                        m.gate_exps.macro_scale(ex),
6111                        m.up_exps.macro_scale(ex),
6112                        lim_exp,
6113                        &mut act,
6114                        m_e * n_ff_exp,
6115                    )?;
6116                    let (aq2, ad2) = e.quantize_q8_1(&act, m_e, n_ff_exp)?;
6117                    e.qmatvec_expert_q8(
6118                        sd,
6119                        0..dl.len,
6120                        &aq2,
6121                        &ad2,
6122                        m_e,
6123                        m.down_exps.in_f,
6124                        m.down_exps.out_f,
6125                        dl.qtype,
6126                        dl.row_bytes,
6127                    )?
6128                } else {
6129                    let gate = e.qmatvec_view(
6130                        sg,
6131                        0..gl.len,
6132                        &gv,
6133                        m_e,
6134                        m.gate_exps.in_f,
6135                        m.gate_exps.out_f,
6136                        gl.qtype,
6137                        gl.row_bytes,
6138                    )?;
6139                    let up = e.qmatvec_view(
6140                        su,
6141                        0..ul.len,
6142                        &gv,
6143                        m_e,
6144                        m.up_exps.in_f,
6145                        m.up_exps.out_f,
6146                        ul.qtype,
6147                        ul.row_bytes,
6148                    )?;
6149                    let mut act = e.uninit(m_e * n_ff_exp)?;
6150                    Self::ffn_act_lim(
6151                        e,
6152                        cfg,
6153                        &gate,
6154                        &up,
6155                        m.gate_exps.macro_scale(ex),
6156                        m.up_exps.macro_scale(ex),
6157                        lim_exp,
6158                        &mut act,
6159                        m_e * n_ff_exp,
6160                    )?;
6161                    let actv = act.slice(0..m_e * n_ff_exp);
6162                    e.qmatvec_view(
6163                        sd,
6164                        0..dl.len,
6165                        &actv,
6166                        m_e,
6167                        m.down_exps.in_f,
6168                        m.down_exps.out_f,
6169                        dl.qtype,
6170                        dl.row_bytes,
6171                    )?
6172                }
6173            };
6174
6175            // SCATTER into slot buffer: each row goes to slot_buf[tok, slot, :].
6176            e.scatter_slot(&y, &tok_idx_d, &slot_idx_d, &weight_d,
6177                           &mut slot_buf, &mut wbuf, n_embd, n_used, m_e)?;
6178        }
6179
6180        // 5. REDUCE SLOTS: sum the 8 slots per token into the final moe_out.
6181        let mut moe_out = e.zeros(t * n_embd)?;
6182        e.reduce_slots(&slot_buf, &wbuf, &mut moe_out, n_embd, n_used, t)?;
6183
6184        // STATS: print m-distribution when MEMRA_MOE_STATS is set.
6185        if std::env::var("MEMRA_MOE_STATS").is_ok() && !m_dist.is_empty() {
6186            m_dist.sort_unstable();
6187            let active = m_dist.len();
6188            let mean = m_dist.iter().sum::<usize>() as f64 / active as f64;
6189            let median = m_dist[active / 2];
6190            let max_m = *m_dist.last().unwrap();
6191            let min_m = m_dist[0];
6192            let above16 = m_dist.iter().filter(|&&x| x >= 16).count();
6193            println!("moe-grouped il={il} t={t} active={active}/{n_expert} \
6194                      m_e: min={min_m} median={median} mean={mean:.1} max={max_m} \
6195                      above_gemm_threshold(>=16)={above16}/{active}");
6196        }
6197
6198        Self::moe_ffn_grouped_add_shared(e, m, z, t, cfg, il, &mut moe_out)?;
6199        Ok(moe_out)
6200    }
6201
6202    /// Lane-3 M2: cross-stream MoE for lockstep decode. Routes all m stream rows in one
6203    /// batch, executes fully-HBM-resident experts through the grouped gather/GEMM/scatter
6204    /// machinery at m_e>1 (weight reads amortized across streams), and assigns any expert
6205    /// with a missing projection to that row's CPU companion call (whole-expert granularity,
6206    /// same rule as the sequential frozen path). Slot-pinned accumulation keeps each row's
6207    /// expert-sum order identical to the sequential path.
6208    pub(crate) fn moe_ffn_lockstep(
6209        &self,
6210        e: &Engine,
6211        m: &MoeWeights,
6212        zbatch: &CudaSlice<f32>,
6213        mrows: usize,
6214        il: u16,
6215        max_block: usize,
6216    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6217        use crate::moe_cache::{BlockId, PROJ_DOWN, PROJ_GATE, PROJ_UP};
6218        let cfg = &self.cfg;
6219        let moe = cfg.moe.as_ref().unwrap();
6220        let n_embd = cfg.n_embd as usize;
6221        let n_expert = moe.expert_count as usize;
6222        let n_used = moe.expert_used_count as usize;
6223        let n_ff_exp = moe.expert_ff_length as usize;
6224        // step35 per-layer SwiGLU clamp; None on every other arch / unclamped layer.
6225        let lim_exp = cfg.clamp_exp_at(il as u32);
6226        let lim_shexp = cfg.clamp_shexp_at(il as u32);
6227
6228        let logits = e.matmul(&m.gate_inp, zbatch, mrows)?;
6229        let (sel_all, w_all) = if let Some(sig) = cfg.sigmoid_router() {
6230            Self::moe_route_cfg(e, &logits, mrows, n_expert, n_used,
6231                                m.exp_probs_b.as_deref(), Some(sig), m.active_experts.as_deref())?
6232        } else {
6233            Self::moe_route_cfg(e, &logits, mrows, n_expert, n_used,
6234                                None, None, m.active_experts.as_deref())?
6235        };
6236        Self::trace_moe_routes(il, mrows, &sel_all, &w_all)?;
6237
6238        // Residency split at whole-expert granularity against the (frozen) cache.
6239        let resident_expert: Vec<bool> = e.with_moe_cache(max_block, |c, _| {
6240            Ok((0..n_expert)
6241                .map(|ex| {
6242                    [PROJ_GATE, PROJ_UP, PROJ_DOWN].into_iter().all(|p| {
6243                        c.resident(BlockId::new(il, p, ex as u16)).is_some()
6244                    })
6245                })
6246                .collect())
6247        })?;
6248
6249        struct Group {
6250            rows: Vec<i32>,
6251            slots: Vec<i32>,
6252            weights: Vec<f32>,
6253        }
6254        let mut groups: std::collections::HashMap<usize, Group> = Default::default();
6255        let mut cpu_rows: Vec<Vec<(usize, f32)>> = vec![Vec::new(); mrows];
6256        let mut cpu_by_expert: std::collections::HashMap<usize, Vec<(usize, f32)>> =
6257            Default::default();
6258        for row in 0..mrows {
6259            for j in 0..n_used {
6260                let ex = sel_all[row * n_used + j] as usize;
6261                let w = w_all[row * n_used + j];
6262                if resident_expert[ex] {
6263                    let group = groups.entry(ex).or_insert_with(|| Group {
6264                        rows: Vec::new(),
6265                        slots: Vec::new(),
6266                        weights: Vec::new(),
6267                    });
6268                    group.rows.push(row as i32);
6269                    group.slots.push(j as i32);
6270                    group.weights.push(w);
6271                } else {
6272                    crate::cpu_experts::record_incomplete_gpu_residency(0);
6273                    cpu_rows[row].push((ex, w));
6274                    cpu_by_expert.entry(ex).or_default().push((row, w));
6275                }
6276            }
6277        }
6278
6279        // CPU tickets first: reads/compute overlap the GPU grouped work below. Experts routed
6280        // by >=2 streams go through the multi-row ABI (weight decode amortized across rows);
6281        // each row's remaining experts stay one ordinary per-row call. Contribution FP-sum
6282        // order per row differs from the sequential single-call chunk — part of the
6283        // documented lockstep numeric class.
6284        let host_rows = e.dtoh(zbatch)?;
6285        let rows_ok = crate::cpu_experts::rows_supported();
6286        enum CpuPart {
6287            Single { row: usize },
6288            Rows { rows: Vec<usize> },
6289        }
6290        let mut tickets: Vec<(CpuPart, crate::cpu_experts::CpuExpertTicket)> = Vec::new();
6291        let mut rows_served: std::collections::HashSet<(usize, usize)> = Default::default();
6292        if rows_ok {
6293            let mut shared: Vec<(usize, Vec<(usize, f32)>)> = cpu_by_expert
6294                .into_iter()
6295                .filter(|(_, rows)| rows.len() >= 2)
6296                .collect();
6297            shared.sort_by_key(|(ex, _)| *ex);
6298            for (ex, mut row_weights) in shared {
6299                row_weights.sort_by_key(|(row, _)| *row);
6300                let inputs: Vec<(&[f32], f32)> = row_weights
6301                    .iter()
6302                    .map(|&(row, w)| (&host_rows[row * n_embd..(row + 1) * n_embd], w))
6303                    .collect();
6304                let job = crate::cpu_experts::prepare_rows_job(m, ex, &inputs)
6305                    .map_err(std::io::Error::other)?;
6306                for &(row, _) in &row_weights {
6307                    rows_served.insert((row, ex));
6308                }
6309                tickets.push((
6310                    CpuPart::Rows {
6311                        rows: row_weights.iter().map(|&(row, _)| row).collect(),
6312                    },
6313                    crate::cpu_experts::submit_rows(job).map_err(std::io::Error::other)?,
6314                ));
6315            }
6316        }
6317        for (row, selected) in cpu_rows.iter().enumerate() {
6318            let leftover: Vec<(usize, f32)> = selected
6319                .iter()
6320                .copied()
6321                .filter(|&(ex, _)| !rows_served.contains(&(row, ex)))
6322                .collect();
6323            if leftover.is_empty() {
6324                continue;
6325            }
6326            let host_row = &host_rows[row * n_embd..(row + 1) * n_embd];
6327            let job = crate::cpu_experts::prepare_job(m, il, &leftover, host_row)
6328                .map_err(std::io::Error::other)?;
6329            tickets.push((
6330                CpuPart::Single { row },
6331                crate::cpu_experts::submit(job).map_err(std::io::Error::other)?,
6332            ));
6333        }
6334
6335        let mut slot_buf = e.zeros(mrows * n_used * n_embd)?;
6336        let mut wbuf = e.zeros(mrows * n_used)?;
6337        let mut order: Vec<usize> = groups.keys().copied().collect();
6338        order.sort_by(|&a, &b| {
6339            groups[&b].rows.len().cmp(&groups[&a].rows.len()).then(a.cmp(&b))
6340        });
6341        for &ex in &order {
6342            let group = &groups[&ex];
6343            let m_e = group.rows.len();
6344            let gl = m.gate_exps.expert_layout(ex);
6345            let ul = m.up_exps.expert_layout(ex);
6346            let dl = m.down_exps.expert_layout(ex);
6347            let row_idx_d = e.htod_i32(&group.rows)?;
6348            let slot_idx_d = e.htod_i32(&group.slots)?;
6349            let dmac = m.down_exps.macro_scale(ex);
6350            let weight_d = if dmac == 1.0 {
6351                e.htod(&group.weights)?
6352            } else {
6353                let scaled: Vec<f32> = group.weights.iter().map(|&w| w * dmac).collect();
6354                e.htod(&scaled)?
6355            };
6356            let mut gathered = e.zeros(m_e * n_embd)?;
6357            e.gather_rows(zbatch, &row_idx_d, &mut gathered, n_embd, m_e)?;
6358            let gv = gathered.slice(0..m_e * n_embd);
6359            let gate = e.with_moe_cache(max_block, |c, eng| {
6360                let slot = c
6361                    .resident(BlockId::new(il, PROJ_GATE, ex as u16))
6362                    .ok_or("lockstep resident expert vanished (cache not frozen?)")?;
6363                eng.qmatvec_view(c.buf(crate::moe_cache::DispatchSlot::Resident(slot)), 0..gl.len, &gv, m_e,
6364                    m.gate_exps.in_f, m.gate_exps.out_f, gl.qtype, gl.row_bytes)
6365            })?;
6366            let up = e.with_moe_cache(max_block, |c, eng| {
6367                let slot = c
6368                    .resident(BlockId::new(il, PROJ_UP, ex as u16))
6369                    .ok_or("lockstep resident expert vanished (cache not frozen?)")?;
6370                eng.qmatvec_view(c.buf(crate::moe_cache::DispatchSlot::Resident(slot)), 0..ul.len, &gv, m_e,
6371                    m.up_exps.in_f, m.up_exps.out_f, ul.qtype, ul.row_bytes)
6372            })?;
6373            let mut act = e.zeros(m_e * n_ff_exp)?;
6374            Self::ffn_act_lim(e, cfg, &gate, &up, m.gate_exps.macro_scale(ex),
6375                m.up_exps.macro_scale(ex), lim_exp, &mut act, m_e * n_ff_exp)?;
6376            let actv = act.slice(0..m_e * n_ff_exp);
6377            let y = e.with_moe_cache(max_block, |c, eng| {
6378                let slot = c
6379                    .resident(BlockId::new(il, PROJ_DOWN, ex as u16))
6380                    .ok_or("lockstep resident expert vanished (cache not frozen?)")?;
6381                eng.qmatvec_view(c.buf(crate::moe_cache::DispatchSlot::Resident(slot)), 0..dl.len, &actv, m_e,
6382                    m.down_exps.in_f, m.down_exps.out_f, dl.qtype, dl.row_bytes)
6383            })?;
6384            e.scatter_slot(&y, &row_idx_d, &slot_idx_d, &weight_d,
6385                           &mut slot_buf, &mut wbuf, n_embd, n_used, m_e)?;
6386        }
6387        let mut moe_out = e.zeros(mrows * n_embd)?;
6388        e.reduce_slots(&slot_buf, &wbuf, &mut moe_out, n_embd, n_used, mrows)?;
6389
6390        // CPU contributions join BEFORE the shared expert (the sequential path's placement).
6391        let mut row_sums: Vec<Option<Vec<f32>>> = vec![None; mrows];
6392        for (part, ticket) in tickets {
6393            let cpu_output = ticket.wait().map_err(std::io::Error::other)?;
6394            let mut add_row = |row: usize, chunk: &[f32]| {
6395                let sum = row_sums[row].get_or_insert_with(|| vec![0.0f32; n_embd]);
6396                for (accumulator, value) in sum.iter_mut().zip(chunk) {
6397                    *accumulator += value;
6398                }
6399            };
6400            match part {
6401                CpuPart::Single { row } => add_row(row, &cpu_output),
6402                CpuPart::Rows { rows } => {
6403                    for (slot, row) in rows.into_iter().enumerate() {
6404                        add_row(row, &cpu_output[slot * n_embd..(slot + 1) * n_embd]);
6405                    }
6406                }
6407            }
6408        }
6409        for (row, sum) in row_sums.into_iter().enumerate() {
6410            let Some(sum) = sum else { continue };
6411            let cpu_output = e.htod(&sum)?;
6412            let mut dst = moe_out.slice_mut(row * n_embd..(row + 1) * n_embd);
6413            e.axpy_into(&cpu_output, 1.0, &mut dst, n_embd)?;
6414        }
6415
6416        if let (Some(gate_shexp), Some(up_shexp), Some(down_shexp)) =
6417            (&m.gate_shexp, &m.up_shexp, &m.down_shexp)
6418        {
6419            let n_ff_sh = gate_shexp.out_features();
6420            let sg_gate = e.matmul(gate_shexp, zbatch, mrows)?;
6421            let sg_up = e.matmul(up_shexp, zbatch, mrows)?;
6422            let mut sa = e.zeros(mrows * n_ff_sh)?;
6423            Self::ffn_act_lim(e, cfg, &sg_gate, &sg_up, 1.0, 1.0, lim_shexp,
6424                              &mut sa, mrows * n_ff_sh)?;
6425            let sh = e.matmul(down_shexp, &sa, mrows)?;
6426            // lockstep rows ARE decode tokens: fused sigmoid-dot per row so batched serving
6427            // decode matches the single-sequence decode chain bit-for-bit.
6428            let g = match &m.gate_inp_shexp {
6429                Some(gate_inp_shexp) => {
6430                    e.sigmoid_dot_rows(zbatch, gate_inp_shexp.float_data(), n_embd, mrows)?
6431                }
6432                None => e.htod(&vec![1.0f32; mrows])?,
6433            };
6434            e.add_scaled_rows(&sh, &g, &mut moe_out, n_embd, mrows)?;
6435        }
6436
6437        Ok(moe_out)
6438    }
6439}
6440
6441// ============================ gemma4 (R8 verified wiring) ==================================
6442// Node-for-node vs llama.cpp src/models/gemma4.cpp:180-405 (HANDOVER "R8 VERIFIED WIRING").
6443// v0 bring-up: full attention everywhere (exact for prompts < sliding_window 1024 — R6 masking
6444// later), sdpa_naive (hd 512 has no FA stamp), sequential host-staged MoE (the perf arms grow
6445// gemma variants after the correctness gate).
6446impl HybridModel {
6447    /// Per-layer attention geometry (R5): (head_dim, n_kv, n_head, rope_base, scale, is_swa).
6448    pub(crate) fn gemma4_geom(&self, il: usize) -> (usize, usize, usize, f32, f32, bool) {
6449        let g = self.cfg.gemma4.as_ref().unwrap();
6450        let swa = g.swa_pattern[il];
6451        let hd = if swa { g.key_length_swa } else { g.key_length_global } as usize;
6452        // attention scale = 1.0 (llama gemma4.cpp:11 "Gemma4 uses self.scaling = 1.0" — q/k are
6453        // per-head rms-normed; NOT the 1/sqrt(hd) default. Bring-up bug: 1/sqrt(hd) left token-0
6454        // rows exact (softmax over one element) while every later position drifted).
6455        (hd, g.head_count_kv[il] as usize, self.cfg.n_head as usize,
6456         if swa { g.rope_base_swa } else { g.rope_base_global },
6457         1.0, swa)
6458    }
6459
6460    /// Suppress-token mask over t logits rows (tokenizer.ggml.suppress_tokens; no-op when the
6461    /// model ships none). NOT monotonic like softcap — must run before every argmax/sample, so
6462    /// every gemma4 logits tail (forward/prime/decode/dc/verify/e4b) calls this on device ld.
6463    fn gemma4_suppress(&self, e: &Engine, ld: &mut CudaSlice<f32>, t: usize)
6464                       -> Result<(), Box<dyn std::error::Error>> {
6465        if let Some((ids, n)) = self.gemma4_aux.as_ref().and_then(|a| a.suppress_d.as_ref()) {
6466            e.mask_ids_rows(ld, ids, *n, self.output.out_features(), t)?;
6467        }
6468        Ok(())
6469    }
6470
6471    /// gemma4 attention (R5 geometry, R7 weightless V-norm on the RAW K projection, R9 dual rope).
6472    /// `cache`: Some => PRIME mode — append the T post-rope K / normed V rows into the quantized
6473    /// KV cache (same per-row quantize math as the decode append) and advance len. Fresh-prompt
6474    /// only (v0): attends within `tokens` via the f32 sdpa.
6475    fn gemma4_attn_prime(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer, il: usize,
6476                         h: &CudaSlice<f32>, pos_d: &CudaSlice<i32>, t: usize,
6477                         cache: Option<&mut Cache>)
6478                         -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6479        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
6480        let eps = self.cfg.rms_eps;
6481        let aux = self.gemma4_aux.as_ref().unwrap();
6482
6483        // quantize-once window: q/k/v share `h` — the MMQ D4 activation quantizes once
6484        // (h stays borrowed across the triple, so the cache key can't go stale).
6485        e.mmq_act_begin();
6486        let q0 = e.matmul(&fa.wq, h, t)?;   // [t, nh*hd]
6487        let k0 = e.matmul(&fa.wk, h, t)?;   // [t, nkv*hd]
6488        // globals ship no v_proj (wv := wk at load) — V input is the SAME projection output;
6489        // reuse k0 instead of re-running the identical matmul (K=V dedup, 5 layers).
6490        let v0 = if swa { e.matmul(&fa.wv, h, t)? } else { e.clone_dtod(&k0)? };
6491
6492        let mut q = e.uninit(t * nh * hd)?;
6493        let mut k = e.uninit(t * nkv * hd)?;
6494        // R7: V = weightless rms_norm of the raw projection; NEVER roped.
6495        let mut v = e.uninit(t * nkv * hd)?;
6496        // 31B glue lane: producers emit the bf16 FA operands (norm emits vb; rope emits qb/kb
6497        // post-rope) — kills 3 f32->bf16 converts + re-reads per layer. Bit-identical operands
6498        // (same __float2bfloat16); MEMRA_FA_EMIT=0 reverts to the convert-in-FA path.
6499        static EMIT: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
6500        let emit = t >= 16 && crate::Engine::qkvnorm_w_on_prefill(nh * t + 2 * nkv * t, hd)
6501            && *EMIT.get_or_init(|| std::env::var("MEMRA_FA_EMIT").map(|s| s != "0").unwrap_or(true));
6502        let mut qb = e.alloc_uninit::<u8>(if emit { t * nh * hd * 2 } else { 1 })?;
6503        let mut kb = e.alloc_uninit::<u8>(if emit { t * nkv * hd * 2 } else { 1 })?;
6504        let mut vb = e.alloc_uninit::<u8>(if emit { t * nkv * hd * 2 } else { 1 })?;
6505        // f16-P/V door: emit V as f16 straight from the norm when this layer's FA consumer
6506        // reads f16 — kills the per-layer bf16->f16 re-encode (1.4GB/pass on 31B SWA).
6507        let v_f16 = emit && crate::fa_f16pv_on() && match hd {
6508            512 => true,
6509            256 => swa && crate::faw_hp_on() && nh % 2 == 0 && (nh / nkv) % 2 == 0,
6510            _ => false,
6511        };
6512        if emit {
6513            e.rms_norm_qkv_w4b(&q0, &k0, &v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
6514                               &aux.ones, &mut q, &mut k, &mut v, &mut vb,
6515                               hd, nh * t, nkv * t, eps, v_f16)?;
6516        } else {
6517            e.rms_norm_qkv(&q0, &k0, &v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
6518                           &aux.ones, &mut q, &mut k, &mut v, hd, nh * t, nkv * t, eps)?;
6519        }
6520
6521        let ff = if swa { None } else {
6522            Some(aux.rope_freqs.as_ref().expect("gemma4 global rope needs rope_freqs.weight"))
6523        };
6524        if emit {
6525            e.rope_neox2_bf16e(&mut q, &mut k, &mut qb, &mut kb, pos_d, hd, hd, nh, nkv, t,
6526                               base, 1.0, ff)?;
6527        } else {
6528            e.rope_neox2(&mut q, &mut k, pos_d, hd, hd, nh, nkv, t, base, 1.0, ff)?;
6529        }
6530
6531        if let Some(cache) = cache {
6532            let kvl = cache.kv[il].as_mut().unwrap();
6533            assert_eq!(kvl.len, 0, "gemma4 prime is fresh-prompt only (v0)");
6534            e.append_kv_quantized_rows(&k, &v, &mut kvl.k, &mut kvl.v, kvl.len, t,
6535                                       kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes, (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on()))?;
6536            kvl.len += t;
6537        }
6538        let mut attn = e.zeros(t * nh * hd)?;
6539        // R6: SWA layers mask keys older than sliding_window once the prompt exceeds it
6540        // (windowed naive twin; fa windowed stamps later). Under the window, full attention
6541        // is exact — SWA rides fa_prefill (hd-256 stamp), the hd-512 globals stay naive.
6542        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
6543        if swa && t > win {
6544            if hd == 256 && std::env::var("MEMRA_NOFA").is_err() {
6545                if emit { e.fa_prefill_w_pre(&qb, &kb, &vb, &mut attn, hd, nh, nkv, t, t,
6546                                             scale, true, win, v_f16)?; }
6547                else { e.fa_prefill_w(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true,
6548                                      win)?; }
6549            } else {
6550                e.sdpa_naive_w(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true, win)?;
6551            }
6552        } else if hd == 256 && std::env::var("MEMRA_NOFA").is_err() {
6553            e.fa_prefill(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?;
6554        } else if hd == 512 && std::env::var("MEMRA_NOFA").is_err() {
6555            if emit { e.fa_prefill_hd512_pre(&qb, &kb, &vb, &mut attn, hd, nh, nkv, t, t,
6556                                             scale, true, v_f16)?; }
6557            else { e.fa_prefill_hd512(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?; }
6558        } else {
6559            e.sdpa_naive(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?;
6560        }
6561        Ok(e.matmul(&fa.wo, &attn, t)?)
6562    }
6563
6564    /// Back-compat wrapper (pure prefill, no cache).
6565    fn gemma4_attn(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer, il: usize,
6566                   h: &CudaSlice<f32>, pos_d: &CudaSlice<i32>, t: usize)
6567                   -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6568        self.gemma4_attn_prime(e, fa, il, h, pos_d, t, None)
6569    }
6570
6571    /// gemma4 MoE with the expert input PRE-QUANTIZED (the q8z tail fusion). Caller guarantees
6572    /// the fast-arm conditions (resident dev slabs + dp4a qtypes) — decode t=1 and verify rows
6573    /// arms only, per-token kernel chains identical to the f32-input path (same quantize bytes:
6574    /// the q8z epilogue is quantize_q8_1 verbatim).
6575    fn gemma4_moe_q8(&self, e: &Engine, m: &crate::hybrid::MoeWeights,
6576                     bits: &crate::hybrid::Gemma4MoeBits,
6577                     mq: &(CudaSlice<i8>, CudaSlice<f32>),
6578                     router_in: &CudaSlice<f32>, t: usize)
6579                     -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6580        let cfg = &self.cfg;
6581        let moe = cfg.moe.as_ref().unwrap();
6582        let n_embd = cfg.n_embd as usize;
6583        let n_expert = moe.expert_count as usize;
6584        let n_used = moe.expert_used_count as usize;
6585        let n_ff_exp = moe.expert_ff_length as usize;
6586        // router stays the two-launch pair: THREE fuse variants measured worse (serial-dot
6587        // -50%, 1024-thread warp-parallel -12% on topk sync overhead — jsonl 2026-07-11);
6588        // the pair's 12us is kernel time, not launch gaps.
6589        let logits = if crate::router_kernel_on() {
6590            e.router_gemv(m.gate_inp.float_data(), router_in, n_embd, n_expert, t)?
6591        } else {
6592            e.matmul(&m.gate_inp, router_in, t)?
6593        };
6594        let dev = m.dev_exps.as_ref().unwrap();
6595        let (sel_d, w_d) = e.moe_router_topk_scaled(&logits, t, n_expert, n_used,
6596                                                    &bits.per_expert_scale_d)?;
6597        let (zq, zd) = mq;
6598        if t == 1 {
6599            let selv = sel_d.slice(0..n_used);
6600            let wv = w_d.slice(0..n_used);
6601            let act = e.moe_gate_up_gelu8_dev_q8(&dev.ptr_row, &selv, zq, zd,
6602                                                 n_embd, n_ff_exp, n_used, n_expert,
6603                                                 m.gate_exps.qtype, m.up_exps.qtype,
6604                                                 m.gate_exps.row_bytes, m.up_exps.row_bytes)?;
6605            let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
6606            let mut moe_out = e.uninit(n_embd)?;
6607            e.moe_down8_fma_dev_q8(&dev.ptr_row, &selv, &wv, &aq2, &ad2,
6608                                   &mut moe_out.slice_mut(0..n_embd), n_ff_exp, n_embd,
6609                                   n_used, n_expert, m.down_exps.qtype, m.down_exps.row_bytes)?;
6610            return Ok(moe_out);
6611        }
6612        let csr = t <= 10 && std::env::var("MEMRA_GEMMA_CSR").as_deref() != Ok("0");
6613        let act = if csr {
6614            e.moe_gate_up_gelu8_dev_q8_csr(&dev.ptr_row, &sel_d, zq, zd, t * n_used,
6615                                           n_embd, n_ff_exp, n_used, n_expert,
6616                                           m.gate_exps.qtype, m.up_exps.qtype,
6617                                           m.gate_exps.row_bytes, m.up_exps.row_bytes)?
6618        } else {
6619            e.moe_gate_up_gelu8_dev_q8_rows(&dev.ptr_row, &sel_d, zq, zd, t,
6620                                            n_embd, n_ff_exp, n_used, n_expert,
6621                                            m.gate_exps.qtype, m.up_exps.qtype,
6622                                            m.gate_exps.row_bytes, m.up_exps.row_bytes)?
6623        };
6624        let (aq2, ad2) = e.quantize_q8_1(&act, t * n_used, n_ff_exp)?;
6625        let mut moe_out = e.uninit(t * n_embd)?;
6626        // down stays rows_g: the CSR dedup twin measured NEGATIVE at nsb=22 too (189.4 vs
6627        // 207.0 depth spec, bitwise-exact — jsonl 2026-07-10; qwen nsb=16 same verdict).
6628        e.moe_down8_fma_dev_q8_rows_g(&dev.ptr_row, &sel_d, &w_d, &aq2, &ad2, &mut moe_out, t,
6629                                      n_ff_exp, n_embd, n_used, n_expert,
6630                                      m.down_exps.qtype, m.down_exps.row_bytes)?;
6631        Ok(moe_out)
6632    }
6633
6634    /// gemma4 MoE (R2 router prologue input supplied by caller, R3 per-expert output scale).
6635    /// Sequential host-staged v0 — softmax gating + renorm (moe_route, the qwen recipe), GELU
6636    /// experts, scale folded into the accumulate weight (post-matmul linear scale, exact fold).
6637    fn gemma4_moe(&self, e: &Engine, m: &crate::hybrid::MoeWeights,
6638                  bits: &crate::hybrid::Gemma4MoeBits, moe_in: &CudaSlice<f32>,
6639                  router_in: &CudaSlice<f32>, t: usize)
6640                  -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6641        let cfg = &self.cfg;
6642        let moe = cfg.moe.as_ref().unwrap();
6643        let n_embd = cfg.n_embd as usize;
6644        let n_expert = moe.expert_count as usize;
6645        let n_used = moe.expert_used_count as usize;
6646        let n_ff_exp = moe.expert_ff_length as usize;
6647
6648        // Router: the in-house GEMV for ALL small t (decode AND verify ride the same per-column
6649        // kernel — the cuBLASLt n-dependence flipped top-k at verify t on the 27B, d994271);
6650        // batched matmul only at real prefill.
6651        let logits = if t < PRIME_MIN_T && crate::router_kernel_on() {
6652            e.router_gemv(m.gate_inp.float_data(), router_in, n_embd, n_expert, t)?
6653        } else {
6654            e.matmul(&m.gate_inp, router_in, t)?
6655        };
6656
6657        // FAST SMALL-T ARM (decode t=1 AND spec verify t=2..15): device softmax-topk router,
6658        // then PER TOKEN the same fused gate_up GELU + down8 FMA launch pair over the resident
6659        // dev slabs — verify rides the EXACT decode kernel chain per token (dispatch-parity
6660        // law; the qwen "verify must be kernel-dispatch-identical to decode" lesson).
6661        if t < PRIME_MIN_T && m.dev_exps.as_ref().is_some_and(|d| !d.gu_il)
6662            && expert_dp4a_supported(m.gate_exps.qtype) && expert_dp4a_supported(m.up_exps.qtype)
6663            && expert_dp4a_supported(m.down_exps.qtype)
6664            && std::env::var("MEMRA_GEMMA_MOE_FAST").as_deref() != Ok("0") {
6665            let dev = m.dev_exps.as_ref().unwrap();
6666            let (sel_d, w_d) = e.moe_router_topk_scaled(&logits, t, n_expert, n_used,
6667                                                        &bits.per_expert_scale_d)?;
6668            if t == 1 {
6669                let (zq, zd) = e.quantize_q8_1(moe_in, 1, n_embd)?;
6670                let selv = sel_d.slice(0..n_used);
6671                let wv = w_d.slice(0..n_used);
6672                let act = e.moe_gate_up_gelu8_dev_q8(&dev.ptr_row, &selv, &zq, &zd,
6673                                                     n_embd, n_ff_exp, n_used, n_expert,
6674                                                     m.gate_exps.qtype, m.up_exps.qtype,
6675                                                     m.gate_exps.row_bytes, m.up_exps.row_bytes)?;
6676                let (aq2, ad2) = e.quantize_q8_1(&act, n_used, n_ff_exp)?;
6677                let mut moe_out = e.uninit(n_embd)?;
6678                e.moe_down8_fma_dev_q8(&dev.ptr_row, &selv, &wv, &aq2, &ad2,
6679                                       &mut moe_out.slice_mut(0..n_embd), n_ff_exp, n_embd,
6680                                       n_used, n_expert, m.down_exps.qtype, m.down_exps.row_bytes)?;
6681                return Ok(moe_out);
6682            }
6683            // VERIFY ROWS TWINS (t=2..15): ONE launch pair for all tokens; per (token,row,slot)
6684            // bodies are the t=1 kernels VERBATIM (bit-identical to the per-token loop).
6685            // gate_up rides the CSR owner-scan dedup when t <= 10 (each duplicated expert's
6686            // weight stream decoded once; the qwen CSR class). MEMRA_GEMMA_CSR=0 -> rows.
6687            let (zq, zd) = e.quantize_q8_1(moe_in, t, n_embd)?;
6688            let csr = t <= 10 && std::env::var("MEMRA_GEMMA_CSR").as_deref() != Ok("0");
6689            let act = if csr {
6690                e.moe_gate_up_gelu8_dev_q8_csr(&dev.ptr_row, &sel_d, &zq, &zd, t * n_used,
6691                                               n_embd, n_ff_exp, n_used, n_expert,
6692                                               m.gate_exps.qtype, m.up_exps.qtype,
6693                                               m.gate_exps.row_bytes, m.up_exps.row_bytes)?
6694            } else {
6695                e.moe_gate_up_gelu8_dev_q8_rows(&dev.ptr_row, &sel_d, &zq, &zd, t,
6696                                                n_embd, n_ff_exp, n_used, n_expert,
6697                                                m.gate_exps.qtype, m.up_exps.qtype,
6698                                                m.gate_exps.row_bytes, m.up_exps.row_bytes)?
6699            };
6700            let (aq2, ad2) = e.quantize_q8_1(&act, t * n_used, n_ff_exp)?;
6701            let mut moe_out = e.uninit(t * n_embd)?;
6702            e.moe_down8_fma_dev_q8_rows_g(&dev.ptr_row, &sel_d, &w_d, &aq2, &ad2, &mut moe_out, t,
6703                                          n_ff_exp, n_embd, n_used, n_expert,
6704                                          m.down_exps.qtype, m.down_exps.row_bytes)?;
6705            return Ok(moe_out);
6706        }
6707
6708        let (sel_all, mut w_all) = Self::moe_route(e, &logits, t, n_expert, n_used)?;
6709        for (i, &sx) in sel_all.iter().enumerate() {
6710            w_all[i] *= bits.per_expert_scale[sx as usize];
6711        }
6712
6713        // PREFILL PAIRS ARM (t >= 16): expert-major CSR over the resident slabs — ONE launch per
6714        // projection covers ALL (token,expert) pairs (the qwen pairs recipe, _em dot = expert_dot_g
6715        // which has the Q4_0 body; GELU pairs epilogue; R3 scale folded into pair_w).
6716        if t >= PRIME_MIN_T && m.dev_exps.as_ref().is_some_and(|d| !d.gu_il)
6717            && expert_dp4a_supported(m.gate_exps.qtype) && expert_dp4a_supported(m.up_exps.qtype)
6718            && expert_dp4a_supported(m.down_exps.qtype)
6719            && std::env::var("MEMRA_GEMMA_MOE_PAIRS").as_deref() != Ok("0") {
6720            let dev = m.dev_exps.as_ref().unwrap();
6721            let n_pairs = t * n_used;
6722            let pair_ex: Vec<i32> = sel_all.iter().map(|&x| x as i32).collect();
6723            let pair_tok: Vec<i32> = (0..n_pairs).map(|p| (p / n_used) as i32).collect();
6724            let tok_off: Vec<i32> = (0..=t).map(|tok| (tok * n_used) as i32).collect();
6725            let tok_ids: Vec<i32> = (0..n_pairs as i32).collect();
6726            let pt = e.htod_i32(&pair_tok)?;
6727            let pw = e.htod(&w_all)?;
6728            let toff = e.htod_i32(&tok_off)?;
6729            let tids = e.htod_i32(&tok_ids)?;
6730            let mut by_ex: Vec<Vec<i32>> = vec![Vec::new(); n_expert];
6731            for p in 0..n_pairs { by_ex[pair_ex[p] as usize].push(p as i32); }
6732            let mut ex_ids: Vec<i32> = Vec::new();
6733            let mut ex_off: Vec<i32> = vec![0];
6734            let mut ex_pairs: Vec<i32> = Vec::with_capacity(n_pairs);
6735            for (ex, list) in by_ex.iter().enumerate() {
6736                if list.is_empty() { continue; }
6737                ex_ids.push(ex as i32);
6738                ex_pairs.extend_from_slice(list);
6739                ex_off.push(ex_pairs.len() as i32);
6740            }
6741            let n_active = ex_ids.len();
6742            let exi = e.htod_i32(&ex_ids)?;
6743            let exo = e.htod_i32(&ex_off)?;
6744            let exp_d = e.htod_i32(&ex_pairs)?;
6745            // GROUPED f16 LANE (MEMRA_MOE_F16G=1, round 46 arc 2): dequant active experts to
6746            // f16 once per projection + one grouped f16 GEMM over the CSR groups; CSR order
6747            // end-to-end (gelu is elementwise), one row permute before the scatter. The
6748            // ragged down k (704) needs no padding here — cublas takes any k.
6749            // f16-mirror numeric class, argmax/spec gated. PER-MODEL default OFF (round 50):
6750            // the gelu class regressed g26 board-2048 prefill -8.3% under the round-49
6751            // Hopper default — see moe_f16g_gemma_on.
6752            if crate::moe_f16g_gemma_on()
6753                && f16g_proj_ok(m.gate_exps.qtype, n_embd)
6754                && f16g_proj_ok(m.up_exps.qtype, n_embd)
6755                && f16g_proj_ok(m.down_exps.qtype, n_ff_exp) {
6756                let csr_tok: Vec<i32> = ex_pairs.iter().map(|&p| p / n_used as i32).collect();
6757                let csr_tok_d = e.htod_i32(&csr_tok)?;
6758                let (z_f16, z_s) = e.moe_f16g_act(moe_in, Some(&csr_tok_d), n_embd, n_pairs)?;
6759                let g_csr = e.moe_f16_grouped(&dev.ptr_row, 0, n_expert, &exi, &ex_off, &exo,
6760                                              &z_f16, &z_s, n_embd, n_ff_exp, n_active, n_pairs,
6761                                              m.gate_exps.qtype, m.gate_exps.row_bytes)?;
6762                let u_csr = e.moe_f16_grouped(&dev.ptr_row, 1, n_expert, &exi, &ex_off, &exo,
6763                                              &z_f16, &z_s, n_embd, n_ff_exp, n_active, n_pairs,
6764                                              m.up_exps.qtype, m.up_exps.row_bytes)?;
6765                let act_csr = e.moe_pairs_gelu_mul(&g_csr, &u_csr, n_pairs * n_ff_exp)?;
6766                let (a_f16, a_s) = e.moe_f16g_act(&act_csr, None, n_ff_exp, n_pairs)?;
6767                let d_csr = e.moe_f16_grouped(&dev.ptr_row, 2, n_expert, &exi, &ex_off, &exo,
6768                                              &a_f16, &a_s, n_ff_exp, n_embd, n_active, n_pairs,
6769                                              m.down_exps.qtype, m.down_exps.row_bytes)?;
6770                let y_down = e.rows_permute(&d_csr, &exp_d, n_pairs, n_embd)?;
6771                let mut moe_out = e.uninit(t * n_embd)?;
6772                e.moe_pairs_scatter(&y_down, &pw, &toff, &tids, &mut moe_out, t, n_embd)?;
6773                if std::env::var("MEMRA_F16G_DEBUG").is_ok() {
6774                    let scan = |v: &[f32]| v.iter().filter(|x| !x.is_finite()).count();
6775                    let (yd, mo) = (e.dtoh(&y_down)?, e.dtoh(&moe_out)?);
6776                    eprintln!("[f16g-debug] post-permute bad={} post-scatter bad={}",
6777                              scan(&yd), scan(&mo));
6778                }
6779                return Ok(moe_out);
6780            }
6781            // gate/up: int8-MMA expert GEMM (in_f = n_embd 2816 = 11x256 tiles ok); down keeps
6782            // the decode-once dp4a (in_f 704 fails the 256-superblock tiling).
6783            let mma = n_embd % 256 == 0
6784                && std::env::var("MEMRA_GEMMA_MOE_MMA").as_deref() != Ok("0");
6785            let (gate, up) = if mma {
6786                let z_scr = e.mmq_iq_quantize_act(moe_in, n_embd, t)?;
6787                (e.mmq_iq_experts(&dev.ptr_row, 0, n_expert, &exi, &exo, &exp_d, &pt, &z_scr,
6788                                  n_embd, n_ff_exp, n_active, n_pairs, t,
6789                                  m.gate_exps.qtype, m.gate_exps.row_bytes)?,
6790                 e.mmq_iq_experts(&dev.ptr_row, 1, n_expert, &exi, &exo, &exp_d, &pt, &z_scr,
6791                                  n_embd, n_ff_exp, n_active, n_pairs, t,
6792                                  m.up_exps.qtype, m.up_exps.row_bytes)?)
6793            } else {
6794                let (zq, zd) = e.quantize_q8_1(moe_in, t, n_embd)?;
6795                (e.moe_pairs_matvec_q8_dec(&dev.ptr_row, 0, &exi, &exo, &exp_d, &pt, &zq, &zd,
6796                                           n_embd, n_ff_exp, n_expert, n_active, n_pairs,
6797                                           m.gate_exps.qtype, m.gate_exps.row_bytes)?,
6798                 e.moe_pairs_matvec_q8_dec(&dev.ptr_row, 1, &exi, &exo, &exp_d, &pt, &zq, &zd,
6799                                           n_embd, n_ff_exp, n_expert, n_active, n_pairs,
6800                                           m.up_exps.qtype, m.up_exps.row_bytes)?)
6801            };
6802            let pair_self: Vec<i32> = (0..n_pairs as i32).collect();
6803            let pself = e.htod_i32(&pair_self)?;
6804            // DOWN through the int8-MMA expert GEMM (2026-07-31, g26 prefill lever): the
6805            // ragged k (n_ff_exp=704 on the 26B) rides a PADDED k-walk — in_f rounds up
6806            // to the 256-val superblock (768) while the act quantizer's zero padding
6807            // makes every padded-k product exactly zero (weight overread bytes multiply
6808            // zero int8 act values; the dev slab carries 144B tail slack for the OOB).
6809            // The old dp4a matvec was 11.3ms/call at m=T (the 0.07x prefill wall).
6810            // MEMRA_GEMMA_MOE_MMA=0 reverts down together with gate/up.
6811            // FUSED ACT-EPILOGUE (default on): gelu_tanh(gate)*up + D4 quantize in one
6812            // launch — no f32 act buffer (the fused kernel zero-pads the ragged tail
6813            // exactly like the two-pass quantizer). MEMRA_MOE_FUSE_ACTQ=0 rollback.
6814            // Scratch bytes are BYTE-IDENTICAL (kernel-check gated).
6815            let y_down = if mma {
6816                let in_pad = n_ff_exp.div_ceil(256) * 256;
6817                let a_scr = if crate::moe_fuse_actq_on() {
6818                    e.mmq_iq_fused_act_quant(&gate, &up, n_ff_exp, n_pairs, 1)?
6819                } else {
6820                    let act = e.moe_pairs_gelu_mul(&gate, &up, n_pairs * n_ff_exp)?;
6821                    e.mmq_iq_quantize_act(&act, n_ff_exp, n_pairs)?
6822                };
6823                e.mmq_iq_experts(&dev.ptr_row, 2, n_expert, &exi, &exo, &exp_d, &pself, &a_scr,
6824                                 in_pad, n_embd, n_active, n_pairs, n_pairs,
6825                                 m.down_exps.qtype, m.down_exps.row_bytes)?
6826            } else {
6827                let act = e.moe_pairs_gelu_mul(&gate, &up, n_pairs * n_ff_exp)?;
6828                let (aq2, ad2) = e.quantize_q8_1(&act, n_pairs, n_ff_exp)?;
6829                e.moe_pairs_matvec_q8_dec(&dev.ptr_row, 2, &exi, &exo, &exp_d, &pself, &aq2, &ad2,
6830                                          n_ff_exp, n_embd, n_expert, n_active, n_pairs,
6831                                          m.down_exps.qtype, m.down_exps.row_bytes)?
6832            };
6833            let mut moe_out = e.uninit(t * n_embd)?;
6834            e.moe_pairs_scatter(&y_down, &pw, &toff, &tids, &mut moe_out, t, n_embd)?;
6835            return Ok(moe_out);
6836        }
6837
6838        let g_len = m.gate_exps.expert_stride;
6839        let u_len = m.up_exps.expert_stride;
6840        let d_len = m.down_exps.expert_stride;
6841        // Resident dev slabs (fits-VRAM regime): read each expert straight from the device slab
6842        // at ex*stride — zero H2D, SAME qmatvec_view kernel/bytes as the staged path. Staging is
6843        // the spill fallback.
6844        let dev = m.dev_exps.as_ref().filter(|d| !d.gu_il);
6845        let (mut sg, mut su, mut sd) = if dev.is_some() { (None, None, None) } else {
6846            (Some(e.alloc_u8_uninit(g_len)?), Some(e.alloc_u8_uninit(u_len)?), Some(e.alloc_u8_uninit(d_len)?))
6847        };
6848        let mut moe_out = e.zeros(t * n_embd)?;
6849        for tok in 0..t {
6850            let sel = &sel_all[tok * n_used..(tok + 1) * n_used];
6851            let w = &w_all[tok * n_used..(tok + 1) * n_used];
6852            let zt = moe_in.slice(tok * n_embd..(tok + 1) * n_embd);
6853            for (j, &ex) in sel.iter().enumerate() {
6854                let ex = ex as usize;
6855                let gate = match dev {
6856                    Some(d) => e.qmatvec_view(&d.gate, ex * g_len..(ex + 1) * g_len, &zt, 1,
6857                        m.gate_exps.in_f, m.gate_exps.out_f, m.gate_exps.qtype, m.gate_exps.row_bytes)?,
6858                    None => {
6859                        let sg = sg.as_mut().unwrap();
6860                        e.stage_expert(m.gate_exps.expert_bytes(ex), sg, 0)?;
6861                        e.qmatvec_view(sg, 0..g_len, &zt, 1,
6862                            m.gate_exps.in_f, m.gate_exps.out_f, m.gate_exps.qtype, m.gate_exps.row_bytes)?
6863                    }
6864                };
6865                let up = match dev {
6866                    Some(d) => e.qmatvec_view(&d.up, ex * u_len..(ex + 1) * u_len, &zt, 1,
6867                        m.up_exps.in_f, m.up_exps.out_f, m.up_exps.qtype, m.up_exps.row_bytes)?,
6868                    None => {
6869                        let su = su.as_mut().unwrap();
6870                        e.stage_expert(m.up_exps.expert_bytes(ex), su, 0)?;
6871                        e.qmatvec_view(su, 0..u_len, &zt, 1,
6872                            m.up_exps.in_f, m.up_exps.out_f, m.up_exps.qtype, m.up_exps.row_bytes)?
6873                    }
6874                };
6875                let mut act = e.uninit(n_ff_exp)?;
6876                e.gelu_tanh_mul(&gate, &up, &mut act, n_ff_exp)?;
6877                let actv = act.slice(0..n_ff_exp);
6878                let y = match dev {
6879                    Some(d) => e.qmatvec_view(&d.down, ex * d_len..(ex + 1) * d_len, &actv, 1,
6880                        m.down_exps.in_f, m.down_exps.out_f, m.down_exps.qtype, m.down_exps.row_bytes)?,
6881                    None => {
6882                        let sd = sd.as_mut().unwrap();
6883                        e.stage_expert(m.down_exps.expert_bytes(ex), sd, 0)?;
6884                        e.qmatvec_view(sd, 0..d_len, &actv, 1,
6885                            m.down_exps.in_f, m.down_exps.out_f, m.down_exps.qtype, m.down_exps.row_bytes)?
6886                    }
6887                };
6888                let mut dst = moe_out.slice_mut(tok * n_embd..(tok + 1) * n_embd);
6889                e.axpy_into(&y, w[j], &mut dst, n_embd)?;
6890            }
6891        }
6892        Ok(moe_out)
6893    }
6894
6895    /// One gemma4 trunk layer (R8): x -> x_next.
6896    fn gemma4_layer(&self, e: &Engine, il: usize, layer: &crate::hybrid::HybridLayer,
6897                    x: &CudaSlice<f32>, pos_d: &CudaSlice<i32>, t: usize)
6898                    -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6899        let n_embd = self.cfg.n_embd as usize;
6900        let eps = self.cfg.rms_eps;
6901
6902        let mut h = e.zeros(t * n_embd)?;
6903        e.rms_norm(x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
6904        let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
6905        let o = self.gemma4_attn(e, fa, il, &h, pos_d, t)?;
6906        // gemma order: post_attention_norm applies to the ATTENTION OUTPUT, then the residual.
6907        let mut cur = e.zeros(t * n_embd)?;
6908        e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, t, eps)?;
6909        self.gemma4_layer_tail_add(e, layer, &cur, x, t)
6910    }
6911
6912    /// Everything after the attention output in a gemma4 layer: the residual add (cur + x ->
6913    /// attn_out) FUSED with the three attn_out norms, then shared FFN + router + MoE + combine +
6914    /// layer scale — shared verbatim by the prefill, decode and verify paths.
6915    fn gemma4_layer_tail_add(&self, e: &Engine, layer: &crate::hybrid::HybridLayer,
6916                             cur: &CudaSlice<f32>, x: &CudaSlice<f32>, t: usize)
6917                             -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6918        Ok(self.gemma4_layer_tail_add_n(e, layer, cur, x, t, None)?.0)
6919    }
6920
6921    /// tail_add with the NEXT layer's attn_norm fused into the closing add+scale (one launch
6922    /// produces both x_next and h_next — the cross-layer fusion). `next_norm` None = last layer.
6923    fn gemma4_layer_tail_add_n(&self, e: &Engine, layer: &crate::hybrid::HybridLayer,
6924                               cur: &CudaSlice<f32>, x: &CudaSlice<f32>, t: usize,
6925                               next_norm: Option<&CudaSlice<f32>>)
6926                               -> Result<(CudaSlice<f32>, Option<CudaSlice<f32>>), Box<dyn std::error::Error>> {
6927        let n_embd = self.cfg.n_embd as usize;
6928        let bits = layer.gemma4.as_ref().unwrap();
6929        let (sn, attn_out) = self.gemma4_layer_tail_core(e, layer, cur, x, t)?;
6930        let mut xn = e.uninit(t * n_embd)?;
6931        match next_norm {
6932            Some(w) => {
6933                let mut hn = e.uninit(t * n_embd)?;
6934                e.add_scale_rms_norm(&sn, &attn_out, bits.layer_scale, w, &mut xn, &mut hn,
6935                                     n_embd, t, self.cfg.rms_eps)?;
6936                Ok((xn, Some(hn)))
6937            }
6938            None => {
6939                e.add_scale(&sn, &attn_out, bits.layer_scale, &mut xn, t * n_embd)?;
6940                Ok((xn, None))
6941            }
6942        }
6943    }
6944
6945    /// Tail core: attn_out = cur+x (fused with the 3 norms), both FFN branches, the combine
6946    /// norm — returns (sn, attn_out) for the closing add+scale variants.
6947    fn gemma4_layer_tail_core(&self, e: &Engine, layer: &crate::hybrid::HybridLayer,
6948                              cur: &CudaSlice<f32>, x: &CudaSlice<f32>, t: usize)
6949                              -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6950        self.gemma4_layer_tail_core_pn(e, layer, cur, x, t, None, false)
6951    }
6952
6953    /// tail_core with an optional PRE-NORM fold (glue-fusion lane): `pre_norm = Some(wa)`
6954    /// means `cur` is the RAW attention output and the dense entry runs
6955    /// rms(cur, wa) + residual-add + ffn_norm as ONE launch (E4B's post-attn norm fold).
6956    /// `defer_post_norm`: dense-arm exit returns RAW f0 (ffn_down output) instead of
6957    /// sn = rms(f0, post_ffw) — the caller fuses the post-norm into its residual emit
6958    /// (rms_pre_add_q8_1, E4B glue wave 5). MoE arm ignores it.
6959    fn gemma4_layer_tail_core_pn(&self, e: &Engine, layer: &crate::hybrid::HybridLayer,
6960                                 cur: &CudaSlice<f32>, x: &CudaSlice<f32>, t: usize,
6961                                 pre_norm: Option<&CudaSlice<f32>>, defer_post_norm: bool)
6962                                 -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6963        let n_embd = self.cfg.n_embd as usize;
6964        let eps = self.cfg.rms_eps;
6965        let bits = layer.gemma4.as_ref().unwrap();
6966
6967        // DENSE gemma4 variants (31B/E4B — no MoE, no parallel branch): attn_out = cur + x
6968        // fused with the single ffn_norm; GELU_PAR ffn; post_ffw_norm.
6969        let Some(mbits) = bits.moe_bits.as_ref() else {
6970            let crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } = &layer.ffn
6971            else { panic!("gemma4 dense layer without Dense ffn") };
6972            let mut attn_out = e.uninit(t * n_embd)?;
6973            let mut zsh = e.uninit(t * n_embd)?;
6974            // wave-2: with the pre-norm fold active the entry ALSO emits zsh q8_1 — the
6975            // t=1 fused2 gate/up consume the pair with no standalone quantize launch.
6976            let mut zpair: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
6977            match pre_norm {
6978                Some(wa) if t == 1 => {
6979                    zpair = Some(e.rms_pre_add_rms_norm_q8z(cur, wa, x,
6980                                                            bits.ffn_norm.float_data(),
6981                                                            &mut attn_out, &mut zsh,
6982                                                            n_embd, t, eps)?);
6983                }
6984                Some(wa) => e.rms_pre_add_rms_norm(cur, wa, x, bits.ffn_norm.float_data(),
6985                                                   &mut attn_out, &mut zsh, n_embd, t, eps)?,
6986                None => e.add_rms_norm(cur, x, bits.ffn_norm.float_data(), &mut attn_out,
6987                                       &mut zsh, n_embd, t, eps)?,
6988            }
6989            let n_ff = ffn_gate.out_features();
6990            // FFN persistent slab (counter-barrier form) FALSIFIED here 2026-07-14
6991            // (falsification #7, jsonl row): PDL glue already hides the launch boundaries
6992            // it fused; barrier + worst-segment occupancy net −0.3% (31B depth) / −2.3%
6993            // (E4B spec). Down's act dependency is all-to-all, so sentinel sync cannot
6994            // rescue segment C — the megakernel front is closed for the dense tail.
6995            let (gate, up) = if t == 1 {
6996                let (zq, zd) = match zpair {
6997                    Some(p) => p,
6998                    None => e.quantize_q8_1(&zsh, 1, n_embd)?,
6999                };
7000                match e.matmul_q4_fused2(ffn_gate, ffn_up, &zq, &zd)? {
7001                    Some(p) => p,
7002                    None => (e.matmul_pre(ffn_gate, &zq, &zd, &zsh, 1)?,
7003                             e.matmul_pre(ffn_up, &zq, &zd, &zsh, 1)?),
7004                }
7005            } else {
7006                // BATCHED FUSED2 (DEFAULT ON 2026-07-13, MEMRA_F2B=0 seam): one segmented
7007                // launch for the verify's gate+up — the up segment's blocks fill SMs as
7008                // the gate segment drains (the launch-tail mechanism behind the b-tier
7009                // plateau; first positive after six falsified in-kernel variants).
7010                static F2B: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
7011                let f2b = *F2B.get_or_init(|| std::env::var("MEMRA_F2B").as_deref() != Ok("0"));
7012                let fused = if f2b {
7013                    let (zq, zd) = e.quantize_q8_1(&zsh, t, n_embd)?;
7014                    e.matmul_q4_fused2_batched(ffn_gate, ffn_up, &zq, &zd, t)?
7015                } else { None };
7016                match fused {
7017                    Some(p) => p,
7018                    None => {
7019                        // quantize-once window: gate/up share `zsh` (borrowed across the pair).
7020                        e.mmq_act_begin();
7021                        (e.matmul(ffn_gate, &zsh, t)?, e.matmul(ffn_up, &zsh, t)?)
7022                    }
7023                }
7024            };
7025            let mut act = e.uninit(t * n_ff)?;
7026            // act quantize folds into the GELU epilogue (bit-identical q8_1 rounding);
7027            // ffn_down rides matmul_pre — one quantize launch fewer per layer.
7028            let f0 = if e.uses_q8_1_fast(ffn_down) {
7029                let upv = e.view(&up, t * n_ff);
7030                let up_all = upv.slice(0..t * n_ff);
7031                let (aq, ad) = e.gelu_tanh_mul_q8_1(&gate, &up_all, &mut act, n_ff, t)?;
7032                e.matmul_pre(ffn_down, &aq, &ad, &act, t)?
7033            } else {
7034                e.gelu_tanh_mul(&gate, &up, &mut act, t * n_ff)?;
7035                e.matmul(ffn_down, &act, t)?
7036            };
7037            if defer_post_norm { return Ok((f0, attn_out)); }
7038            let mut sn = e.uninit(t * n_embd)?;
7039            e.rms_norm(&f0, bits.post_ffw_norm.float_data(), &mut sn, n_embd, t, eps)?;
7040            return Ok((sn, attn_out));
7041        };
7042
7043        assert!(pre_norm.is_none(), "pre-norm fold is dense-entry only");
7044        // MoE variant (26B): attn_out = cur + x fused with the three attn_out norms
7045        // (ffn_norm + router-scale + pre_ffw_norm_2): ONE launch, chains verbatim. At small t
7046        // (decode + verify) the zsh and moe_in outputs are EMITTED q8_1 (both consumers are
7047        // quantized matmuls — two quantize launches + two f32 round-trips fold away).
7048        let mut attn_out = e.uninit(t * n_embd)?;
7049        let mut router_in = e.uninit(t * n_embd)?;
7050        let fast_moe = match &layer.ffn {
7051            crate::hybrid::Ffn::Moe(m) => m.dev_exps.as_ref().is_some_and(|d| !d.gu_il)
7052                && expert_dp4a_supported(m.gate_exps.qtype)
7053                && expert_dp4a_supported(m.up_exps.qtype)
7054                && expert_dp4a_supported(m.down_exps.qtype)
7055                && std::env::var("MEMRA_GEMMA_MOE_FAST").as_deref() != Ok("0"),
7056            _ => false,
7057        };
7058        let q8z = t < PRIME_MIN_T && fast_moe;
7059        let (zsh_f32, zsh_q8, moe_q8) = if q8z {
7060            let (z0, m2) = e.add_rms_norm3_q8z(cur, x, bits.ffn_norm.float_data(),
7061                                               &mbits.router_scale_pre,
7062                                               mbits.pre_ffw_norm_2.float_data(),
7063                                               &mut attn_out, &mut router_in, n_embd, t, eps)?;
7064            (None, Some(z0), Some(m2))
7065        } else {
7066            let mut zsh = e.uninit(t * n_embd)?;
7067            let mut moe_in = e.uninit(t * n_embd)?;
7068            e.add_rms_norm3(cur, x, bits.ffn_norm.float_data(), &mbits.router_scale_pre,
7069                            mbits.pre_ffw_norm_2.float_data(), &mut attn_out, &mut zsh,
7070                            &mut router_in, &mut moe_in, n_embd, t, eps)?;
7071            (Some((zsh, moe_in)), None, None)
7072        };
7073        let attn_out2 = attn_out;
7074        #[allow(unused_variables)]
7075        let attn_out = &attn_out2;
7076        let n_ff = mbits.shared_gate.out_features();
7077        let (gate, up) = if let Some((zq, zd)) = zsh_q8.as_ref() {
7078            if t == 1 {
7079                match e.matmul_q4_fused2(&mbits.shared_gate, &mbits.shared_up, zq, zd)? {
7080                    Some(p) => p,
7081                    None => {
7082                        let h0 = e.zeros(0)?;
7083                        (e.matmul_pre(&mbits.shared_gate, zq, zd, &h0, 1)?,
7084                         e.matmul_pre(&mbits.shared_up, zq, zd, &h0, 1)?)
7085                    }
7086                }
7087            } else {
7088                // verify t 2..15: the batched mmvq twins consume the pre-quantized pair.
7089                let h0 = e.zeros(0)?;
7090                (e.matmul_pre(&mbits.shared_gate, zq, zd, &h0, t)?,
7091                 e.matmul_pre(&mbits.shared_up, zq, zd, &h0, t)?)
7092            }
7093        } else {
7094            let (zsh, _) = zsh_f32.as_ref().unwrap();
7095            (e.matmul(&mbits.shared_gate, zsh, t)?, e.matmul(&mbits.shared_up, zsh, t)?)
7096        };
7097        let mut act = e.uninit(t * n_ff)?;
7098        e.gelu_tanh_mul(&gate, &up, &mut act, t * n_ff)?;
7099        let mlp0 = e.matmul(&mbits.shared_down, &act, t)?;
7100        let crate::hybrid::Ffn::Moe(m) = &layer.ffn else { panic!("gemma4 layer not MoE") };
7101        let moe0 = match (&moe_q8, &zsh_f32) {
7102            (Some(mq), _) => self.gemma4_moe_q8(e, m, mbits, mq, &router_in, t)?,
7103            (None, Some((_, moe_in))) => self.gemma4_moe(e, m, mbits, moe_in, &router_in, t)?,
7104            _ => unreachable!(),
7105        };
7106        // post_ffw_norm_1(mlp0) + post_ffw_norm_2(moe0): one fused launch, per-row verbatim.
7107        let mut mlp = e.uninit(t * n_embd)?;
7108        let mut moe = e.uninit(t * n_embd)?;
7109        e.rms_norm2x(&mlp0, &moe0, mbits.post_ffw_norm_1.float_data(),
7110                     mbits.post_ffw_norm_2.float_data(), &mut mlp, &mut moe, n_embd, t, eps)?;
7111
7112        // combine: rms_norm(mlp + moe, post_ffw_norm) + attn_out, then the layer output scalar.
7113        // add+norm fused (add_rms_norm == add then rms_norm, kernel-check-pinned identity).
7114        let mut sum = e.uninit(t * n_embd)?;
7115        let mut sn = e.uninit(t * n_embd)?;
7116        e.add_rms_norm(&mlp, &moe, bits.post_ffw_norm.float_data(), &mut sum, &mut sn,
7117                       n_embd, t, eps)?;
7118        Ok((sn, attn_out2))
7119    }
7120
7121    /// tail_add with the next attn_norm emitted PRE-QUANTIZED q8_1 (decode/verify loops).
7122    fn gemma4_layer_tail_add_nq(&self, e: &Engine, layer: &crate::hybrid::HybridLayer,
7123                                cur: &CudaSlice<f32>, x: &CudaSlice<f32>, t: usize,
7124                                next_norm: Option<&CudaSlice<f32>>)
7125                                -> Result<(CudaSlice<f32>, Option<(CudaSlice<i8>, CudaSlice<f32>)>), Box<dyn std::error::Error>> {
7126        let n_embd = self.cfg.n_embd as usize;
7127        let bits = layer.gemma4.as_ref().unwrap();
7128        let (sn, attn_out) = self.gemma4_layer_tail_core(e, layer, cur, x, t)?;
7129        let mut xn = e.uninit(t * n_embd)?;
7130        match next_norm {
7131            Some(w) => {
7132                let pair = e.add_scale_rms_norm_q8_1(&sn, &attn_out, bits.layer_scale, w, &mut xn,
7133                                                     n_embd, t, self.cfg.rms_eps)?;
7134                Ok((xn, Some(pair)))
7135            }
7136            None => {
7137                e.add_scale(&sn, &attn_out, bits.layer_scale, &mut xn, t * n_embd)?;
7138                Ok((xn, None))
7139            }
7140        }
7141    }
7142
7143    /// gemma4 prefill: `last_only` = forward_last semantics (lm_head on the final row only).
7144    /// R4: final logits softcapped 30*tanh(l/30) on host (monotonic — argmax unaffected).
7145    fn gemma4_forward(&self, e: &Engine, tokens: &[u32], last_only: bool)
7146                      -> Result<Vec<f32>, Box<dyn std::error::Error>> {
7147        // E4B routes to its own forward regardless of the caller's entry point (forward /
7148        // forward_last / prime paths all funnel here for gemma4).
7149        if self.is_gemma4_e4b() { return self.gemma4_e4b_forward(e, tokens, last_only); }
7150        let n_embd = self.cfg.n_embd as usize;
7151        let t = tokens.len();
7152        let pos: Vec<i32> = (0..t as i32).collect();
7153        let pos_d = e.htod_i32(&pos)?;
7154
7155        let mut x = self.embed(e, tokens)?;
7156        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
7157        // MEMRA_GEMMA_PROBE=1: per-layer trunk stats (host rms + max of the LAST token row) —
7158        // the bring-up bisect vs llama-eval-callback node stats.
7159        let probe = std::env::var("MEMRA_GEMMA_PROBE").is_ok();
7160        let stat = |e: &Engine, x: &CudaSlice<f32>, tag: &str| -> Result<(), Box<dyn std::error::Error>> {
7161            let h = e.dtoh(x)?;
7162            let bad = h.iter().filter(|v| !v.is_finite()).count();
7163            let mx = h.iter().filter(|v| v.is_finite()).fold(0.0f32, |m, v| m.max(v.abs()));
7164            eprintln!("[gemma-probe] {tag}: tok0_first3={:?} bad={bad} max={mx:.3e}", &h[..3]);
7165            Ok(())
7166        };
7167        if probe { stat(e, &x, "embed")?; }
7168        for (il, layer) in self.layers.iter().enumerate() {
7169            x = self.gemma4_layer(e, il, layer, &x, &pos_d, t)?;
7170            if probe { stat(e, &x, &format!("L{il}"))?; }
7171        }
7172        let mut hn = e.zeros(t * n_embd)?;
7173        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, self.cfg.rms_eps)?;
7174        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
7175        let n_vocab = self.output.out_features();
7176        let logits = if last_only {
7177            let hv = e.view(&hn, t * n_embd);
7178            let last_row = hv.slice((t - 1) * n_embd..t * n_embd);
7179            let mut hlast = e.zeros(n_embd)?;
7180            e.copy_view_into(&mut hlast, 0, &last_row, n_embd)?;
7181            let mut ld = e.matmul(&self.output, &hlast, 1)?;
7182            e.softcap(&mut ld, cap, n_vocab)?;
7183            self.gemma4_suppress(e, &mut ld, 1)?;
7184            e.dtoh(&ld)?
7185        } else {
7186            let mut ld = e.matmul(&self.output, &hn, t)?;
7187            e.softcap(&mut ld, cap, t * n_vocab)?;
7188            self.gemma4_suppress(e, &mut ld, t)?;
7189            e.dtoh(&ld)?
7190        };
7191        Ok(logits)
7192    }
7193
7194    /// gemma4 BATCHED PROMPT PRIME v0 (fresh cache only): the prefill graph over the whole
7195    /// prompt with each layer's post-rope K / weightless-normed V appended into the quantized
7196    /// KV cache (decode-append row math). Returns (last-row softcapped logits, h_seed = last
7197    /// pre-output_norm hidden, hiddens = full pre-output_norm stack [T, n_embd]).
7198    pub(crate) fn gemma4_prime(&self, e: &Engine, tokens: &[u32], cache: &mut Cache)
7199                               -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7200        // Err, not assert (2026-08-07, lane/gemma4-serve-gaps): a served gemma4 prompt longer
7201        // than the worker's prefill tick used to chunk here, and chunk 2 (pos > 0) killed the
7202        // whole worker process on this line. The worker now primes gemma4 monolithically and
7203        // routes continuation suffixes tokenwise; this is the per-request backstop.
7204        if cache.pos != 0 {
7205            return Err("gemma4 prime v0 is fresh-prompt only (no continuation/chunked prime) \
7206                        — prime the full prompt in one call or decode tokenwise".into());
7207        }
7208        let n_embd = self.cfg.n_embd as usize;
7209        let eps = self.cfg.rms_eps;
7210        let t = tokens.len();
7211        let pos: Vec<i32> = (0..t as i32).collect();
7212        let pos_d = e.htod_i32(&pos)?;
7213        let mut x = self.embed(e, tokens)?;
7214        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
7215        for (il, layer) in self.layers.iter().enumerate() {
7216            let mut h = e.zeros(t * n_embd)?;
7217            e.rms_norm(&x, layer.attn_norm.float_data(), &mut h, n_embd, t, eps)?;
7218            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer not full-attn") };
7219            let o = self.gemma4_attn_prime(e, fa, il, &h, &pos_d, t, Some(cache))?;
7220            let mut cur = e.zeros(t * n_embd)?;
7221            e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, t, eps)?;
7222            x = self.gemma4_layer_tail_add(e, layer, &cur, &x, t)?;
7223            self.dflash_tap(e, cache, il, &x, t)?;
7224        }
7225        cache.pos += t;
7226        let hiddens = e.clone_dtod(&x)?;
7227        let xv = e.view(&x, t * n_embd);
7228        let last_row = xv.slice((t - 1) * n_embd..t * n_embd);
7229        let mut h_seed = e.zeros(n_embd)?;
7230        e.copy_view_into(&mut h_seed, 0, &last_row, n_embd)?;
7231        let mut hn = e.uninit(n_embd)?;
7232        e.rms_norm(&h_seed, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
7233        let mut ld = e.matmul(&self.output, &hn, 1)?;
7234        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
7235        e.softcap(&mut ld, cap, self.output.out_features())?;
7236        self.gemma4_suppress(e, &mut ld, 1)?;
7237        let logits = e.dtoh(&ld)?;
7238        Ok((logits, h_seed, hiddens))
7239    }
7240
7241    /// gemma4 T=1 decode attention: per-layer geometry, quantized-KV append + fa_decode
7242    /// (vec kernels at hd 256, generic scalar at the globals' hd 512), weightless V-norm,
7243    /// dual rope, scale 1.0. Takes the attn-normed input PRE-QUANTIZED (the cross-layer
7244    /// fused norm emits q8 directly — the f32 h never materializes).
7245    fn gemma4_decode_attn(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer, il: usize,
7246                          hq: &CudaSlice<i8>, hdq: &CudaSlice<f32>,
7247                          pos_d: &CudaSlice<i32>, cache: &mut Cache)
7248                          -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7249        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
7250        let eps = self.cfg.rms_eps;
7251        let aux = self.gemma4_aux.as_ref().unwrap();
7252        let (hq, hdq) = (hq, hdq);
7253        let h0 = e.zeros(0)?;
7254        let h = &h0;
7255        let (q0, k0, v0) = if swa {
7256            match e.matmul_q4_fused3(&fa.wq, &fa.wk, &fa.wv, &hq, &hdq)? {
7257                Some(t3) => t3,
7258                None => (e.matmul_pre(&fa.wq, &hq, &hdq, h, 1)?,
7259                         e.matmul_pre(&fa.wk, &hq, &hdq, h, 1)?,
7260                         e.matmul_pre(&fa.wv, &hq, &hdq, h, 1)?),
7261            }
7262        } else {
7263            let (q0, k0) = match e.matmul_q4_fused2(&fa.wq, &fa.wk, &hq, &hdq)? {
7264                Some(p) => p,
7265                None => (e.matmul_pre(&fa.wq, &hq, &hdq, h, 1)?,
7266                         e.matmul_pre(&fa.wk, &hq, &hdq, h, 1)?),
7267            };
7268            let v0 = e.clone_dtod(&k0)?;
7269            (q0, k0, v0)
7270        };
7271        let mut q = e.uninit(nh * hd)?;
7272        let mut k = e.uninit(nkv * hd)?;
7273        let mut v = e.uninit(nkv * hd)?;
7274        // E4B wave-3 fold, m=1 completion (2026-07-23): the rows arms took this fold at
7275        // 550fcfa5; the decode-step trio kept 2 launches/layer it doesn't need.
7276        let ff = if swa { None } else {
7277            Some(aux.rope_freqs.as_ref().expect("gemma4 global rope needs rope_freqs.weight"))
7278        };
7279        e.rms_norm_qkv_rope(&q0, &k0, &v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
7280                            &aux.ones, &mut q, &mut k, &mut v, hd, nh, nkv,
7281                            pos_d, nh, nkv, base, 1.0, ff, eps)?;
7282        let kvl = cache.kv[il].as_mut().unwrap();
7283        e.append_kv_quantized(&k, &v, &mut kvl.k, &mut kvl.v, kvl.len,
7284                              kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes, (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on()))?;
7285        kvl.len += 1;
7286        // R6 decode: SWA layers attend only the last `sliding_window` keys — a token-aligned
7287        // VIEW OFFSET into the quantized cache (keys carry absolute rope; the mask is purely
7288        // positional). Globals attend the full history.
7289        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
7290        let mut attn = e.uninit(nh * hd)?;
7291        // global layers: SAME hd512 rows twin as verify with t=1 (parity law).
7292        if !swa && hd == 512 && kvl.len >= crate::fa512_min_tkv()
7293            && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
7294            let kp = e.view_u8(&kvl.k, kvl.len * kvl.k_tok_bytes);
7295            let vp = e.view_u8(&kvl.v, kvl.len * kvl.v_tok_bytes);
7296            // device-len: eager syncs the counter (async arg-store), dc keeps it live.
7297            let base = kvl.len as i32;
7298            e.i32_set_k(&mut kvl.len_d, base)?;
7299            e.fa_decode_rows(&q, &kp, &vp, &mut attn, hd, nh, nkv, kvl.len - 1, 1, scale,
7300                             kvl.k_tok_bytes, kvl.v_tok_bytes, Some((&kvl.len_d, -1)), false,
7301                             false, None)?;
7302            return Ok(e.matmul(&fa.wo, &attn, 1)?);
7303        }
7304        // windowed regime: SAME rows_w kernel as verify with t=1 (parity law — see verify_attn).
7305        if swa && kvl.len > win && hd == 256
7306            && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
7307            let kp = e.view_u8(&kvl.k, kvl.len * kvl.k_tok_bytes);
7308            let vp = e.view_u8(&kvl.v, kvl.len * kvl.v_tok_bytes);
7309            let base = kvl.len as i32;
7310            e.i32_set_k(&mut kvl.len_d, base)?;
7311            e.fa_decode_rows_w(&q, &kp, &vp, &mut attn, hd, nh, nkv, &kvl.len_d, -1, 1, scale,
7312                               win, kvl.k_tok_bytes, kvl.v_tok_bytes, None)?;
7313            return Ok(e.matmul(&fa.wo, &attn, 1)?);
7314        }
7315        let (off_tok, t_kv) = if swa && kvl.len > win { (kvl.len - win, win) } else { (0, kvl.len) };
7316        let k_view = e.view_u8_range(&kvl.k, off_tok * kvl.k_tok_bytes,
7317                                     (off_tok + t_kv) * kvl.k_tok_bytes);
7318        let v_view = e.view_u8_range(&kvl.v, off_tok * kvl.v_tok_bytes,
7319                                     (off_tok + t_kv) * kvl.v_tok_bytes);
7320        e.fa_decode_kvmod(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, t_kv, scale,
7321                    kvl.k_tok_bytes, kvl.v_tok_bytes, swa && crate::Engine::wkv_on())?;
7322        Ok(e.matmul(&fa.wo, &attn, 1)?)
7323    }
7324
7325    /// gemma4 DEVICE-COUNTER decode step (graph arc): token id + rope pos + KV lengths live in
7326    /// device counters; ZERO varying host kernel args. `cap_bucket_max` = Some(bucket) for graph
7327    /// capture (host mirrors untouched, n_splits from bucket, full-buffer KV views) / None for
7328    /// the eager-dc gate path (host mirrors advanced, live geometry — bit-identical target =
7329    /// gemma4_decode_step_h's token stream). V1 scope: t_kv <= sliding_window (no window views
7330    /// in-graph; the driver gates).
7331    #[allow(clippy::too_many_arguments)]
7332    pub fn gemma4_decode_step_dc(&self, e: &Engine, token_d: &CudaSlice<u32>,
7333                                 pos_d: &mut CudaSlice<i32>, embd_gpu: &CudaSlice<u8>,
7334                                 embd_qt: i32, embd_rb: usize, cache: &mut Cache,
7335                                 n_vocab: usize, cap_bucket_max: Option<(usize, usize)>)
7336                                 -> Result<CudaSlice<u32>, Box<dyn std::error::Error>> {
7337        let mut tok_out = e.stream().alloc_zeros::<u32>(1)?;
7338        self.gemma4_decode_step_dc_into(e, token_d, pos_d, embd_gpu, embd_qt, embd_rb, cache,
7339                                        n_vocab, cap_bucket_max, &mut tok_out)?;
7340        Ok(tok_out)
7341    }
7342
7343    /// CAPTURE body: argmax lands in the PERSISTENT `tok_out` (same buffer = same address on
7344    /// every replay; pass `token_d` itself for the self-feeding graph loop).
7345    #[allow(clippy::too_many_arguments)]
7346    pub fn gemma4_decode_step_dc_into(&self, e: &Engine, token_d: &CudaSlice<u32>,
7347                                      pos_d: &mut CudaSlice<i32>, embd_gpu: &CudaSlice<u8>,
7348                                      embd_qt: i32, embd_rb: usize, cache: &mut Cache,
7349                                      n_vocab: usize, cap_bucket_max: Option<(usize, usize)>,
7350                                      tok_out: &mut CudaSlice<u32>)
7351                                      -> Result<(), Box<dyn std::error::Error>> {
7352        let n_embd = self.cfg.n_embd as usize;
7353        let eps = self.cfg.rms_eps;
7354        let mut x = e.embed_gather_device(embd_gpu, token_d, n_embd, embd_qt, embd_rb)?;
7355        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
7356        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
7357        let n_layers = self.layers.len();
7358        for (il, layer) in self.layers.iter().enumerate() {
7359            let (hq, hdq) = match h_carry.take() {
7360                Some(p) => p,
7361                None => e.rms_norm_q8_1(&x, self.layers[0].attn_norm.float_data(), n_embd, 1, eps)?,
7362            };
7363            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
7364            let o = self.gemma4_decode_attn_dc(e, fa, il, &hq, &hdq, pos_d, cache, cap_bucket_max)?;
7365            let mut cur = e.uninit(n_embd)?;
7366            e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, 1, eps)?;
7367            let next_norm = if il + 1 < n_layers {
7368                Some(self.layers[il + 1].attn_norm.float_data())
7369            } else { None };
7370            let (xn, hn) = self.gemma4_layer_tail_add_nq(e, layer, &cur, &x, 1, next_norm)?;
7371            x = xn;
7372            h_carry = hn;
7373        }
7374        let mut hn = e.uninit(n_embd)?;
7375        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
7376        let mut logits = e.matmul(&self.output, &hn, 1)?;
7377        self.gemma4_suppress(e, &mut logits, 1)?;   // cap skipped (monotonic); the mask is not
7378        e.argmax_token_device_into(&logits, tok_out, n_vocab)?;
7379        e.inc_seqlen(pos_d)?;
7380        if cap_bucket_max.is_none() { cache.pos += 1; }
7381        Ok(())
7382    }
7383
7384    /// Persistent transient slots for the ALLOC-FREE captured dc step (the graph door):
7385    /// every buffer the step produces per token lives here, allocated ONCE pre-capture, so
7386    /// the captured graph carries zero cuMemAllocAsync/Free nodes (the 226us/launch tax,
7387    /// osrt 2026-07-23). Sized for the model's max per-layer shapes.
7388
7389    /// Build the slot set (call OUTSIDE any capture).
7390    pub fn g4_dc_slots(&self, e: &Engine) -> Result<G4DcSlots, Box<dyn std::error::Error>> {
7391        let n_embd = self.cfg.n_embd as usize;
7392        let n_vocab = self.output.out_features();
7393        let n_layers = self.layers.len();
7394        let (mut qmax, mut kvmax, mut ffmax) = (0usize, 0usize, 0usize);
7395        for il in 0..n_layers {
7396            let (hd, nkv, nh, _b, _s, _w) = self.gemma4_geom(il);
7397            qmax = qmax.max(nh * hd);
7398            kvmax = kvmax.max(nkv * hd);
7399            if let crate::hybrid::Ffn::Dense { ffn_gate, .. } = &self.layers[il].ffn {
7400                ffmax = ffmax.max(ffn_gate.out_features());
7401            }
7402        }
7403        Ok(G4DcSlots {
7404            x: e.uninit(n_embd)?, xn: e.uninit(n_embd)?, cur: e.uninit(n_embd)?,
7405            hq: e.alloc_i8_uninit(n_embd)?, hd_: e.uninit(n_embd / 32)?,
7406            q0: e.uninit(qmax)?, k0: e.uninit(kvmax)?, v0: e.uninit(kvmax)?,
7407            q: e.uninit(qmax)?, k: e.uninit(kvmax)?, v: e.uninit(kvmax)?,
7408            attn: e.uninit(qmax)?, o: e.uninit(n_embd)?,
7409            attn_out: e.uninit(n_embd)?, zsh: e.uninit(n_embd)?,
7410            // zq/zd feed the wo matvec (nh*hd rows — 4096 on hd512 globals > n_embd),
7411            // the ffn entry (n_embd) and the lm_head (n_embd): size for the max.
7412            zq: e.alloc_i8_uninit(n_embd.max(qmax))?, zd: e.uninit(n_embd.max(qmax) / 32)?,
7413            gate: e.uninit(ffmax)?, up: e.uninit(ffmax)?,
7414            act: e.uninit(ffmax)?, actq: e.alloc_i8_uninit(ffmax)?, actd: e.uninit(ffmax / 32)?,
7415            f0: e.uninit(n_embd)?, sn: e.uninit(n_embd)?,
7416            hn: e.uninit(n_embd)?, logits: e.uninit(n_vocab)?,
7417        })
7418    }
7419
7420    /// m=1 pre-quantized matvec into a slot — mirrors matmul_pre's m=1 mmvq route exactly
7421    /// (rp4-mirror bytes; mmvq_supports guaranteed for gemma4 q4_0/q6_K).
7422    fn g4_matvec_m1_into(&self, e: &Engine, w: &crate::model::GpuTensor,
7423                         aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, y: &mut CudaSlice<f32>)
7424                         -> Result<(), Box<dyn std::error::Error>> {
7425        use crate::model::GpuTensor;
7426        let (bytes, qtype, row_bytes, scale, rp) = match w {
7427            GpuTensor::Quant { bytes, qtype, row_bytes, scale, rp, .. } =>
7428                (bytes, *qtype, *row_bytes, *scale, *rp),
7429            _ => return Err("g4_matvec_m1_into: non-quant tensor".into()),
7430        };
7431        let (mbytes, mrp) = match w {
7432            GpuTensor::Quant { rp4: Some(m4), .. } => (m4, true),
7433            _ => (bytes, rp),
7434        };
7435        e.qmatvec_mmvq_into(mbytes, aq, ad, 1, w.in_features(), w.out_features(),
7436                            qtype, row_bytes, scale, mrp, y)
7437    }
7438
7439    /// ALLOC-FREE dc step (capture body): kernel-for-kernel mirror of
7440    /// `gemma4_decode_step_dc_into` at t=1 with every transient slot-fed. Dense gemma4 only
7441    /// (12B/31B; uniform q4_0 trunk guarantees the fused2/3 arms).
7442    #[allow(clippy::too_many_arguments)]
7443    pub fn gemma4_decode_step_dc_slotted(&self, e: &Engine, token_d: &CudaSlice<u32>,
7444                                         pos_d: &mut CudaSlice<i32>, embd_gpu: &CudaSlice<u8>,
7445                                         embd_qt: i32, embd_rb: usize, cache: &mut Cache,
7446                                         n_vocab: usize, cap_bucket_max: Option<(usize, usize)>,
7447                                         sl: &mut G4DcSlots, tok_out: &mut CudaSlice<u32>,
7448                                         ring: Option<(&mut CudaSlice<u32>, usize)>)
7449                                         -> Result<(), Box<dyn std::error::Error>> {
7450        let n_embd = self.cfg.n_embd as usize;
7451        let eps = self.cfg.rms_eps;
7452        e.embed_gather_device_into(embd_gpu, token_d, &mut sl.x, n_embd, embd_qt, embd_rb)?;
7453        e.scale_inplace(&mut sl.x, (n_embd as f32).sqrt(), n_embd)?;
7454        let n_layers = self.layers.len();
7455        let mut has_carry = false;
7456        for il in 0..n_layers {
7457            if !has_carry {
7458                e.rms_norm_q8_1_into(&sl.x, self.layers[il].attn_norm.float_data(), n_embd, 1,
7459                                     eps, &mut sl.hq, &mut sl.hd_)?;
7460            }
7461            has_carry = true;
7462            let layer = &self.layers[il];
7463            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
7464            self.gemma4_decode_attn_dc_slotted(e, fa, il, pos_d, cache, cap_bucket_max, sl)?;
7465            e.rms_norm(&sl.o, layer.post_attn_norm.float_data(), &mut sl.cur, n_embd, 1, eps)?;
7466            let next_norm = if il + 1 < n_layers {
7467                Some(self.layers[il + 1].attn_norm.float_data())
7468            } else { None };
7469            self.gemma4_layer_tail_slotted(e, layer, next_norm, sl)?;
7470            std::mem::swap(&mut sl.x, &mut sl.xn);
7471        }
7472        e.rms_norm(&sl.x, self.output_norm.float_data(), &mut sl.hn, n_embd, 1, eps)?;
7473        e.quantize_q8_1_into(&sl.hn, 1, n_embd, &mut sl.zq, &mut sl.zd)?;
7474        // lm_head via the same m=1 mmvq route as matmul (q6_K on the gemma4 family).
7475        {
7476            let (zq, zd) = (&sl.zq, &sl.zd);
7477            let zq = unsafe { &*(zq as *const CudaSlice<i8>) };
7478            let zd = unsafe { &*(zd as *const CudaSlice<f32>) };
7479            self.g4_matvec_m1_into(e, &self.output, zq, zd, &mut sl.logits)?;
7480        }
7481        self.gemma4_suppress(e, &mut sl.logits, 1)?;
7482        e.argmax_token_device_into(&sl.logits, tok_out, n_vocab)?;
7483        if let Some((ring, base)) = ring {
7484            // in-graph token parking: slot = (pos - base) % ring.len(); the door drains
7485            // with ONE sync per chunk instead of a per-token dtoh (the launch-serialization
7486            // fix — llama's 885us/launch overlaps GPU work because nothing waits per token).
7487            e.plain_tok_ring(tok_out, pos_d, base, ring)?;
7488        }
7489        e.inc_seqlen(pos_d)?;
7490        if cap_bucket_max.is_none() { cache.pos += 1; }
7491        Ok(())
7492    }
7493
7494    /// Slot-fed dc attention: mirrors gemma4_decode_attn_dc's CAPTURE arm kernel-for-kernel.
7495    #[allow(clippy::too_many_arguments)]
7496    fn gemma4_decode_attn_dc_slotted(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer,
7497                                     il: usize, pos_d: &CudaSlice<i32>, cache: &mut Cache,
7498                                     cap_bucket_max: Option<(usize, usize)>, sl: &mut G4DcSlots)
7499                                     -> Result<(), Box<dyn std::error::Error>> {
7500        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
7501        let eps = self.cfg.rms_eps;
7502        let aux = self.gemma4_aux.as_ref().unwrap();
7503        {
7504            let hq = unsafe { &*(&sl.hq as *const CudaSlice<i8>) };
7505            let hdq = unsafe { &*(&sl.hd_ as *const CudaSlice<f32>) };
7506            if swa {
7507                if !e.matmul_q4_fused3_into(&fa.wq, &fa.wk, &fa.wv, hq, hdq,
7508                                            &mut sl.q0, &mut sl.k0, &mut sl.v0)? {
7509                    return Err("slotted step: fused3 unavailable (non-uniform trunk)".into());
7510                }
7511            } else {
7512                if !e.matmul_q4_fused2_into(&fa.wq, &fa.wk, hq, hdq, &mut sl.q0, &mut sl.k0)? {
7513                    return Err("slotted step: fused2 unavailable".into());
7514                }
7515                let k0r = unsafe { &*(&sl.k0 as *const CudaSlice<f32>) };
7516                e.copy_into(&mut sl.v0, 0, k0r, nkv * hd)?;
7517            }
7518        }
7519        // E4B wave-3 fold, m=1 completion (2026-07-23) — MUST mirror the dc_into arm
7520        // kernel-for-kernel (graph stream-identity gate).
7521        let ff = if swa { None } else {
7522            Some(aux.rope_freqs.as_ref().expect("gemma4 global rope needs rope_freqs.weight"))
7523        };
7524        let kvl = cache.kv[il].as_mut().unwrap();
7525        let kv_fp8 = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
7526        if crate::Engine::qkv_append_on() {
7527            // append fold (2026-07-23): mirrors dc_into.
7528            e.rms_norm_qkv_rope_append_dc(&sl.q0, &sl.k0, &sl.v0, fa.q_norm.float_data(),
7529                fa.k_norm.float_data(), &aux.ones, &mut sl.q, &mut sl.k, &mut sl.v, hd, nh, nkv,
7530                pos_d, nh, nkv, base, 1.0, ff, eps,
7531                &mut kvl.k, &mut kvl.v, &kvl.len_d, kvl.k_tok_bytes, kvl.v_tok_bytes, kv_fp8)?;
7532        } else {
7533            e.rms_norm_qkv_rope(&sl.q0, &sl.k0, &sl.v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
7534                                &aux.ones, &mut sl.q, &mut sl.k, &mut sl.v, hd, nh, nkv,
7535                                pos_d, nh, nkv, base, 1.0, ff, eps)?;
7536            e.append_kv_quantized_dc(&sl.k, &sl.v, &mut kvl.k, &mut kvl.v, &kvl.len_d,
7537                                     kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes,
7538                                     kv_fp8)?;
7539        }
7540        e.inc_seqlen(&mut kvl.len_d)?;
7541        let (b_swa, b_glob) = cap_bucket_max.expect("slotted step is capture-only");
7542        let k_view = e.view_u8(&kvl.k, kvl.k.len());
7543        let v_view = e.view_u8(&kvl.v, kvl.v.len());
7544        let rows_on = std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0");
7545        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
7546        // combine-q8 emit (wave-5b m=1 port): the rows arms quantize inside the combine —
7547        // the standalone quantize launch runs only on the non-rows fallback. MUST mirror
7548        // the dc_into arm branch-for-branch (stream gate).
7549        let mut fa_q8 = false;
7550        if !swa && hd == 512 && b_glob >= crate::fa512_min_tkv() && rows_on {
7551            e.fa_decode_rows(&sl.q, &k_view, &v_view, &mut sl.attn, hd, nh, nkv, b_glob - 1,
7552                             1, scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
7553                             Some((&kvl.len_d, -1)), false, false,
7554                             Some((&mut sl.zq, &mut sl.zd)))?;
7555            fa_q8 = true;
7556        } else if swa && b_swa > win && hd == 256 && rows_on {
7557            e.fa_decode_rows_w(&sl.q, &k_view, &v_view, &mut sl.attn, hd, nh, nkv,
7558                               &kvl.len_d, -1, 1, scale, win,
7559                               kvl.k_tok_bytes, kvl.v_tok_bytes,
7560                               Some((&mut sl.zq, &mut sl.zd)))?;
7561            fa_q8 = true;
7562        } else {
7563            let b = if swa { b_swa } else { b_glob };
7564            e.fa_decode_dc(&sl.q, &k_view, &v_view, &mut sl.attn, hd, nh, nkv, &kvl.len_d, b,
7565                           scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
7566                           swa && crate::Engine::wkv_on())?;
7567        }
7568        if !fa_q8 {
7569            let aq = unsafe { &*(&sl.attn as *const CudaSlice<f32>) };
7570            e.quantize_q8_1_into(aq, 1, nh * hd, &mut sl.zq, &mut sl.zd)?;
7571        }
7572        {
7573            let zq = unsafe { &*(&sl.zq as *const CudaSlice<i8>) };
7574            let zd = unsafe { &*(&sl.zd as *const CudaSlice<f32>) };
7575            self.g4_matvec_m1_into(e, &fa.wo, zq, zd, &mut sl.o)?;
7576        }
7577        Ok(())
7578    }
7579
7580    /// Slot-fed dense layer tail: mirrors gemma4_layer_tail_core (t=1, no pre-norm fold) +
7581    /// tail_add_nq kernel-for-kernel; the next layer's (hq, hd_) carry lands in the slots.
7582    fn gemma4_layer_tail_slotted(&self, e: &Engine, layer: &crate::hybrid::HybridLayer,
7583                                 next_norm: Option<&CudaSlice<f32>>, sl: &mut G4DcSlots)
7584                                 -> Result<(), Box<dyn std::error::Error>> {
7585        let n_embd = self.cfg.n_embd as usize;
7586        let eps = self.cfg.rms_eps;
7587        let bits = layer.gemma4.as_ref().unwrap();
7588        let crate::hybrid::Ffn::Dense { ffn_gate, ffn_up, ffn_down } = &layer.ffn
7589        else { return Err("slotted tail: dense ffn only".into()) };
7590        e.add_rms_norm(&sl.cur, &sl.x, bits.ffn_norm.float_data(), &mut sl.attn_out,
7591                       &mut sl.zsh, n_embd, 1, eps)?;
7592        let n_ff = ffn_gate.out_features();
7593        {
7594            let zshr = unsafe { &*(&sl.zsh as *const CudaSlice<f32>) };
7595            e.quantize_q8_1_into(zshr, 1, n_embd, &mut sl.zq, &mut sl.zd)?;
7596        }
7597        {
7598            let zq = unsafe { &*(&sl.zq as *const CudaSlice<i8>) };
7599            let zd = unsafe { &*(&sl.zd as *const CudaSlice<f32>) };
7600            if !e.matmul_q4_fused2_into(ffn_gate, ffn_up, zq, zd, &mut sl.gate, &mut sl.up)? {
7601                return Err("slotted tail: ffn fused2 unavailable".into());
7602            }
7603        }
7604        debug_assert!(e.uses_q8_1_fast(ffn_down));
7605        {
7606            let upr = unsafe { &*(&sl.up as *const CudaSlice<f32>) };
7607            let upv = e.view(upr, n_ff);
7608            let up_all = upv.slice(0..n_ff);
7609            let gr = unsafe { &*(&sl.gate as *const CudaSlice<f32>) };
7610            e.gelu_tanh_mul_q8_1_into(gr, &up_all, &mut sl.act, n_ff, 1,
7611                                      &mut sl.actq, &mut sl.actd)?;
7612        }
7613        {
7614            let aq = unsafe { &*(&sl.actq as *const CudaSlice<i8>) };
7615            let ad = unsafe { &*(&sl.actd as *const CudaSlice<f32>) };
7616            self.g4_matvec_m1_into(e, ffn_down, aq, ad, &mut sl.f0)?;
7617        }
7618        e.rms_norm(&sl.f0, bits.post_ffw_norm.float_data(), &mut sl.sn, n_embd, 1, eps)?;
7619        match next_norm {
7620            Some(w) => {
7621                e.add_scale_rms_norm_q8_1_into(&sl.sn, &sl.attn_out, bits.layer_scale, w,
7622                                               &mut sl.xn, n_embd, 1, eps,
7623                                               &mut sl.hq, &mut sl.hd_)?;
7624            }
7625            None => {
7626                e.add_scale(&sl.sn, &sl.attn_out, bits.layer_scale, &mut sl.xn, n_embd)?;
7627            }
7628        }
7629        Ok(())
7630    }
7631
7632    /// dc attention: same math as gemma4_decode_attn, KV slot/lengths from device counters.
7633    #[allow(clippy::too_many_arguments)]
7634    fn gemma4_decode_attn_dc(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer, il: usize,
7635                             hq: &CudaSlice<i8>, hdq: &CudaSlice<f32>,
7636                             pos_d: &CudaSlice<i32>, cache: &mut Cache,
7637                             cap_bucket_max: Option<(usize, usize)>)
7638                             -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7639        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
7640        let eps = self.cfg.rms_eps;
7641        let aux = self.gemma4_aux.as_ref().unwrap();
7642        let (q0, k0, v0) = if swa {
7643            match e.matmul_q4_fused3(&fa.wq, &fa.wk, &fa.wv, hq, hdq)? {
7644                Some(t3) => t3,
7645                None => {
7646                    let h0 = e.zeros(0)?;
7647                    (e.matmul_pre(&fa.wq, hq, hdq, &h0, 1)?,
7648                     e.matmul_pre(&fa.wk, hq, hdq, &h0, 1)?,
7649                     e.matmul_pre(&fa.wv, hq, hdq, &h0, 1)?)
7650                }
7651            }
7652        } else {
7653            let (q0, k0) = match e.matmul_q4_fused2(&fa.wq, &fa.wk, hq, hdq)? {
7654                Some(p) => p,
7655                None => {
7656                    let h0 = e.zeros(0)?;
7657                    (e.matmul_pre(&fa.wq, hq, hdq, &h0, 1)?,
7658                     e.matmul_pre(&fa.wk, hq, hdq, &h0, 1)?)
7659                }
7660            };
7661            let v0 = e.clone_dtod(&k0)?;
7662            (q0, k0, v0)
7663        };
7664        let mut q = e.uninit(nh * hd)?;
7665        let mut k = e.uninit(nkv * hd)?;
7666        let mut v = e.uninit(nkv * hd)?;
7667        // E4B wave-3 fold, m=1 completion (2026-07-23): mirrored by the slotted arm.
7668        let ff = if swa { None } else {
7669            Some(aux.rope_freqs.as_ref().expect("gemma4 global rope needs rope_freqs.weight"))
7670        };
7671        let kvl = cache.kv[il].as_mut().unwrap();
7672        let kv_fp8 = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
7673        if crate::Engine::qkv_append_on() {
7674            // append fold (2026-07-23): norm+rope+cache-append in ONE launch.
7675            e.rms_norm_qkv_rope_append_dc(&q0, &k0, &v0, fa.q_norm.float_data(),
7676                fa.k_norm.float_data(), &aux.ones, &mut q, &mut k, &mut v, hd, nh, nkv,
7677                pos_d, nh, nkv, base, 1.0, ff, eps,
7678                &mut kvl.k, &mut kvl.v, &kvl.len_d, kvl.k_tok_bytes, kvl.v_tok_bytes, kv_fp8)?;
7679        } else {
7680            e.rms_norm_qkv_rope(&q0, &k0, &v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
7681                                &aux.ones, &mut q, &mut k, &mut v, hd, nh, nkv,
7682                                pos_d, nh, nkv, base, 1.0, ff, eps)?;
7683            e.append_kv_quantized_dc(&k, &v, &mut kvl.k, &mut kvl.v, &kvl.len_d,
7684                                     kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes, kv_fp8)?;
7685        }
7686        e.inc_seqlen(&mut kvl.len_d)?;
7687        let mut attn = e.uninit(nh * hd)?;
7688        // combine-q8 emit carrier (wave-5b m=1 port): rows arms fill this; the tail then
7689        // rides g4_matvec_m1_into instead of matmul's internal quantize.
7690        let mut fa_q8: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
7691        // Weight prefetch NOT wired here (26B/31B/qwen probes 2026-07-13: 26B flat
7692        // 196.6/196.0 vs 196.4/196.1 — MoE ffn dilutes wo; 31B −0.2% — dense decode sits
7693        // at the DRAM wall, no idle window to front-load into). E4B keeps the arm
7694        // (gemma4_e4b_attn, +0.65% valid window).
7695        match cap_bucket_max {
7696            None => {
7697                // dc-EAGER: host knows the length — R6 window views EXACTLY like the eager
7698                // decode (SWA layers attend the last `sliding_window` keys); the device
7699                // counters carry only the append slot + the graph seam.
7700                kvl.len += 1;
7701                let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
7702                if !swa && hd == 512 && kvl.len >= crate::fa512_min_tkv()
7703                    && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
7704                    // global layers: SAME hd512 rows twin as verify, t=1 (parity law).
7705                    // dc: len_d is live (inc_seqlen) — device-len rides it, plus=-1.
7706                    let kp = e.view_u8(&kvl.k, kvl.len * kvl.k_tok_bytes);
7707                    let vp = e.view_u8(&kvl.v, kvl.len * kvl.v_tok_bytes);
7708                    let (mut aq8, mut ad8) = e.uninit_q8_pair(nh * hd)?;
7709                    e.fa_decode_rows(&q, &kp, &vp, &mut attn, hd, nh, nkv, kvl.len - 1, 1,
7710                                     scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
7711                                     Some((&kvl.len_d, -1)), false, false,
7712                                     Some((&mut aq8, &mut ad8)))?;
7713                    fa_q8 = Some((aq8, ad8));
7714                } else if swa && kvl.len > win && hd == 256
7715                    && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
7716                    // windowed regime: SAME rows_w kernel as verify, t=1 (parity law).
7717                    let kp = e.view_u8(&kvl.k, kvl.len * kvl.k_tok_bytes);
7718                    let vp = e.view_u8(&kvl.v, kvl.len * kvl.v_tok_bytes);
7719                    let (mut aq8, mut ad8) = e.uninit_q8_pair(nh * hd)?;
7720                    e.fa_decode_rows_w(&q, &kp, &vp, &mut attn, hd, nh, nkv, &kvl.len_d, -1,
7721                                       1, scale, win, kvl.k_tok_bytes, kvl.v_tok_bytes,
7722                                       Some((&mut aq8, &mut ad8)))?;
7723                    fa_q8 = Some((aq8, ad8));
7724                } else {
7725                    let (off_tok, t_kv) = if swa && kvl.len > win { (kvl.len - win, win) }
7726                                          else { (0, kvl.len) };
7727                    let k_view = e.view_u8_range(&kvl.k, off_tok * kvl.k_tok_bytes,
7728                                                 (off_tok + t_kv) * kvl.k_tok_bytes);
7729                    let v_view = e.view_u8_range(&kvl.v, off_tok * kvl.v_tok_bytes,
7730                                                 (off_tok + t_kv) * kvl.v_tok_bytes);
7731                    e.fa_decode_kvmod(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, t_kv, scale,
7732                                kvl.k_tok_bytes, kvl.v_tok_bytes, swa && crate::Engine::wkv_on())?;
7733                }
7734            }
7735            Some((b_swa, b_glob)) => {
7736                // capture: full-buffer views + device length. PARITY LAW port (graph arc step
7737                // 3): the SAME regime branches as dc-eager, pinned per arm — b_swa is the
7738                // exact-key t_kv for the dc family (n_splits constant per bucket), b_glob is
7739                // the RUNG max for the rows family (kernels derive per-replay splits from
7740                // kvl.len_d; grid/partials sized for the rung end, excess splits exit).
7741                let k_view = e.view_u8(&kvl.k, kvl.k.len());
7742                let v_view = e.view_u8(&kvl.v, kvl.v.len());
7743                let rows_on = std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0");
7744                let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
7745                if !swa && hd == 512 && b_glob >= crate::fa512_min_tkv() && rows_on {
7746                    let (mut aq8, mut ad8) = e.uninit_q8_pair(nh * hd)?;
7747                    e.fa_decode_rows(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, b_glob - 1,
7748                                     1, scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
7749                                     Some((&kvl.len_d, -1)), false, false,
7750                                     Some((&mut aq8, &mut ad8)))?;
7751                    fa_q8 = Some((aq8, ad8));
7752                } else if swa && b_swa > win && hd == 256 && rows_on {
7753                    let (mut aq8, mut ad8) = e.uninit_q8_pair(nh * hd)?;
7754                    e.fa_decode_rows_w(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
7755                                       &kvl.len_d, -1, 1, scale, win,
7756                                       kvl.k_tok_bytes, kvl.v_tok_bytes,
7757                                       Some((&mut aq8, &mut ad8)))?;
7758                    fa_q8 = Some((aq8, ad8));
7759                } else {
7760                    let b = if swa { b_swa } else { b_glob };
7761                    e.fa_decode_dc(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, &kvl.len_d, b,
7762                                   scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
7763                                   swa && crate::Engine::wkv_on())?;
7764                }
7765            }
7766        }
7767        // combine-q8 emit (wave-5b m=1 port): the rows arms produced the wo activation pair
7768        // in-combine — ride the slotted arm's exact matvec route (parity by construction).
7769        if let Some((aq8, ad8)) = fa_q8 {
7770            let mut y = e.uninit(fa.wo.out_features())?;
7771            self.g4_matvec_m1_into(e, &fa.wo, &aq8, &ad8, &mut y)?;
7772            return Ok(y);
7773        }
7774        Ok(e.matmul(&fa.wo, &attn, 1)?)
7775    }
7776
7777    /// gemma4 GRAPH-REPLAY greedy loop: per (swa-key, global-key) fa bucket, capture ONE full
7778    /// dc step (self-feeding: argmax writes token_d in-graph) and replay it — one graph launch
7779    /// per token, one 4B dtoh. V1 scope: whole generation under the sliding window (no window
7780    /// views in-graph); caller gates and falls back to the dc-eager loop.
7781    pub fn gemma4_generate_graph(&self, e: &Engine, prompt_pos: usize, first_token: u32,
7782                                 cache: &mut Cache, max_new: usize, eos: &[u32],
7783                                 mut on_token: impl FnMut(u32) -> bool)
7784                                 -> Result<(Vec<u32>, crate::decode::StopReason), Box<dyn std::error::Error>> {
7785        if self.is_gemma4_e4b() {
7786            return Err("E4B graph serving is unwired (HANDOVER-E4B.md) — dc-eager is the serving arm".into());
7787        }
7788        use crate::decode::StopReason;
7789        let n_vocab = self.output.out_features();
7790        let n_embd = self.cfg.n_embd as usize;
7791        let embd_gpu = self.embd_gpu.get_or_init(|| {
7792            e.upload_u8(&self.embd.raw).expect("embed table upload")
7793        });
7794        let (qt, rb) = self.embd.qt_and_row_bytes(n_embd);
7795        for kvl in cache.kv.iter_mut().flatten() {
7796            e.set_i32_one(&mut kvl.len_d, kvl.len as i32)?;
7797        }
7798        let mut token_d = e.stream().clone_htod(&[first_token])?;
7799        let mut pos_d = e.htod_i32(&[prompt_pos as i32])?;
7800        let g4 = self.cfg.gemma4.as_ref().unwrap();
7801        let (hd_s, hd_g) = (g4.key_length_swa as usize, g4.key_length_global as usize);
7802        // per-layer nkv: swa vs global counts from the pattern (uniform within class).
7803        let nkv_s = g4.head_count_kv.iter().zip(g4.swa_pattern.iter())
7804            .find(|p| *p.1).map(|p| *p.0 as usize).unwrap_or(8);
7805        let nkv_g = g4.head_count_kv.iter().zip(g4.swa_pattern.iter())
7806            .find(|p| !*p.1).map(|p| *p.0 as usize).unwrap_or(2);
7807        let mut graphs: std::collections::HashMap<((bool, usize), (bool, usize), bool, bool),
7808                                                  (cudarc::driver::CudaGraph,
7809                                                   Vec<Box<dyn std::any::Any + Send>>)> = Default::default();
7810        // ALLOC-FREE capture: persistent transient slots — the captured graph carries zero
7811        // mem nodes (the 226us/launch tax). Slots must outlive every cached graph.
7812        let mut slots = self.g4_dc_slots(e)?;
7813        // Chunked replay ring: tokens park on-device; ONE drain sync per chunk. ring_base is
7814        // baked at the door entry (the modulo keeps every capture valid indefinitely).
7815        const RING: usize = 64;
7816        // DRAIN pinned at 1 (2026-07-23): relaunching the SAME graph exec before its prior
7817        // launch completes is ILLEGAL (chunk=4 -> ILLEGAL_ADDRESS; chunk=1 clean). Pipelining
7818        // needs alternating exec instances — and the measured payoff was ~0 (the ~200us
7819        // cuGraphLaunch host cost already overlaps its own launch's GPU work; llama pays
7820        // 885us/launch the same way). MEMRA_GRAPH_DRAIN raises it only for experiments.
7821        const DRAIN: usize = 1;
7822        let mut ring = e.stream().alloc_zeros::<u32>(RING)?;
7823        let ring_base = prompt_pos;
7824        let mut out = Vec::with_capacity(max_new);
7825        let mut reason = StopReason::MaxNew;
7826        let mut next = first_token;
7827        let mut captures = 0usize;
7828        for _ in 0..max_new {
7829            out.push(next);
7830            if eos.contains(&next) { reason = StopReason::Eos; break; }
7831            if !on_token(next) { reason = StopReason::Callback; break; }
7832            let t_kv = cache.pos + 1;
7833            // Bucket key per ARM (graph arc step 3):
7834            //  - swa component: dc family under the window (exact (fa_vec, n_splits) key —
7835            //    n_splits must be constant per bucket); rows_w above it (window-constant, so
7836            //    the component collapses to a single marker).
7837            //  - global component: dc family under the fa512 floor (exact key); rows_dpl16
7838            //    at/above it — the kernel derives splits from len_d per replay, so buckets
7839            //    are power-of-2 RUNGS (one capture per doubling; grid sized for the rung end).
7840            let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
7841            let f512 = crate::fa512_min_tkv();
7842            let key_s = if t_kv > win { (true, usize::MAX) }
7843                        else { e.fa_bucket_key(t_kv, hd_s, nkv_s, crate::Engine::wkv_on()) };
7844            let (key_g, rung_end) = if t_kv >= f512 {
7845                // strict upper bound: the rung must ROLL at exact powers (t_kv==1024 starts
7846                // the [1024,2048) bucket) — sizing covers every replayed T_kv < end.
7847                let end = (t_kv + 1).next_power_of_two().max(f512 * 2);
7848                ((true, end), end)
7849            } else { (e.fa_bucket_key(t_kv, hd_g, nkv_g, false), t_kv) };
7850            let key = (key_s, key_g, t_kv >= f512, t_kv > win);
7851            if !graphs.contains_key(&key) {
7852                let bucket_max = (t_kv, rung_end);
7853                // snapshot device+host state (the 3 capture-warmup runs leave no residue).
7854                let snap = cache.snapshot(e)?;
7855                let pos_save = e.dtoh_i32_one(&pos_d)?;
7856                let len_save: Vec<Option<i32>> = cache.kv.iter()
7857                    .map(|k| k.as_ref().map(|kvl| e.dtoh_i32_one(&kvl.len_d).unwrap())).collect();
7858                let tok_save = e.dtoh_u32_one(&token_d)?;
7859                // RETAINED capture (2026-07-23): the plain capture_graph left pool-transient
7860                // clones as dead COPY NODES replayed every launch — the E4B 0.74ms/token
7861                // regression class, and this door's measured -8.8%. The keeper pins warmup
7862                // transients so the captured graph holds kernel nodes only.
7863                let graph = {
7864                    let tok_ref = &mut token_d;
7865                    let pos_ref = &mut pos_d;
7866                    let cache_ref = &mut *cache;
7867                    let slots_ref = &mut slots;
7868                    let ring_ref = &mut ring;
7869                    e.capture_graph_retained_flags(
7870                        cudarc::driver::sys::CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_USE_NODE_PRIORITY,
7871                        |e| {
7872                        // self-feeding: the argmax writes token_d itself.
7873                        let tok_in = unsafe { &*(tok_ref as *const CudaSlice<u32>) };
7874                        let sl = unsafe { &mut *(slots_ref as *mut G4DcSlots) };
7875                        let rg = unsafe { &mut *(ring_ref as *mut CudaSlice<u32>) };
7876                        self.gemma4_decode_step_dc_slotted(e, tok_in, pos_ref, embd_gpu, qt, rb,
7877                                                           cache_ref, n_vocab, Some(bucket_max),
7878                                                           sl, tok_ref, Some((rg, ring_base)))
7879                    })?
7880                };
7881                cache.rollback(e, &snap, 0)?;
7882                e.set_i32_one(&mut pos_d, pos_save)?;
7883                for (il, ls) in len_save.iter().enumerate() {
7884                    if let (Some(kvl), Some(v)) = (cache.kv[il].as_mut(), ls) {
7885                        e.set_i32_one(&mut kvl.len_d, *v)?;
7886                    }
7887                }
7888                e.set_u32_one(&mut token_d, tok_save)?;
7889                if std::env::var("MEMRA_GRAPH_CENSUS").as_deref() == Ok("1") {
7890                    if let Ok(c) = crate::graph_update::node_census(&graph.0) {
7891                        eprintln!("[graph-census] {c:?}");
7892                    }
7893                }
7894                graphs.insert(key, graph);
7895                captures += 1;
7896            }
7897            // CHUNKED REPLAY: enqueue up to DRAIN launches back-to-back (the ~200us host
7898            // cost of each cuGraphLaunch overlaps the PREVIOUS launch's ~11ms GPU work),
7899            // then ONE sync + ring drain. The chunk must stay inside this bucket key and
7900            // the budget; capture warmups already emitted their tokens through the ring.
7901            let mut chunk = 1usize;
7902            let drain_cap: usize = std::env::var("MEMRA_GRAPH_DRAIN").ok()
7903                .and_then(|v| v.parse().ok()).unwrap_or(DRAIN);
7904            while chunk < drain_cap && out.len() + chunk < max_new {
7905                let t_next = cache.pos + 1 + chunk;
7906                let key_s2 = if t_next > win { (true, usize::MAX) }
7907                             else { e.fa_bucket_key(t_next, hd_s, nkv_s, crate::Engine::wkv_on()) };
7908                let key_g2 = if t_next >= f512 {
7909                    (true, (t_next + 1).next_power_of_two().max(f512 * 2))
7910                } else { e.fa_bucket_key(t_next, hd_g, nkv_g, false) };
7911                if (key_s2, key_g2, t_next >= f512, t_next > win) != key { break; }
7912                chunk += 1;
7913            }
7914            let g = &graphs.get(&key).unwrap().0;
7915            for _ in 0..chunk { g.launch()?; }
7916            e.stream().synchronize()?;
7917            let ringh = e.dtoh_u32(&ring)?;
7918            for j in 0..chunk {
7919                let pos_j = cache.pos + j;
7920                let tok_j = ringh[(pos_j - ring_base) % RING];
7921                cache.pos += 0; // advanced below in one shot
7922                if j + 1 == chunk { next = tok_j; }
7923                else {
7924                    out.push(tok_j);
7925                    if eos.contains(&tok_j) || !on_token(tok_j) {
7926                        reason = if eos.contains(&tok_j) { StopReason::Eos }
7927                                 else { StopReason::Callback };
7928                        // roll device/host state back to the stop point.
7929                        let keep = cache.pos + j + 1;
7930                        e.set_i32_one(&mut pos_d, keep as i32)?;
7931                        for kvl in cache.kv.iter_mut().filter_map(|k| k.as_mut()) {
7932                            e.set_i32_one(&mut kvl.len_d, keep as i32)?;
7933                            kvl.len = keep;
7934                        }
7935                        cache.pos = keep;
7936                        if std::env::var("MEMRA_GRAPH_STATS").is_ok() {
7937                            eprintln!("[gemma-graph] captures={captures} buckets={}", graphs.len());
7938                        }
7939                        return Ok((out, reason));
7940                    }
7941                }
7942            }
7943            cache.pos += chunk;
7944            for kvl in cache.kv.iter_mut().filter_map(|k| k.as_mut()) { kvl.len += chunk; }
7945        }
7946        if std::env::var("MEMRA_GRAPH_STATS").is_ok() {
7947            eprintln!("[gemma-graph] captures={captures} buckets={}", graphs.len());
7948        }
7949        Ok((out, reason))
7950    }
7951
7952    /// gemma4 VERIFY step (spec decode): t tokens batched through the trunk at positions
7953    /// pos0..pos0+t-1, K/V rows appended to the quantized cache (caller rolls back rejected
7954    /// rows via kvl.len), per-token causal windowed attend (fa_decode per token — the same
7955    /// kernel family as T=1 decode at each token's t_kv). Returns [t, n_vocab] softcapped
7956    /// logits (host) + advances cache.pos by t.
7957    pub(crate) fn gemma4_decode_step_t(&self, e: &Engine, tokens: &[u32], pos0: usize,
7958                                       cache: &mut Cache)
7959                                       -> Result<Vec<f32>, Box<dyn std::error::Error>> {
7960        Ok(self.gemma4_decode_step_t_h(e, tokens, pos0, cache)?.0)
7961    }
7962
7963    /// GREEDY verify: per-row DEVICE argmax (t x 4B host traffic instead of the t x 1MB logits
7964    /// stack — softcap skipped: tanh is monotonic, per-row argmax unaffected). Returns
7965    /// (argmax ids [t], post-output_norm hidden stack [t, n_embd]).
7966    pub(crate) fn gemma4_decode_step_t_am(&self, e: &Engine, tokens: &[u32], pos0: usize,
7967                                          cache: &mut Cache)
7968                                          -> Result<(Vec<u32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7969        let (ld, hn) = self.gemma4_verify_trunk(e, tokens, pos0, cache, None)?;
7970        let t = tokens.len();
7971        let n_vocab = self.output.out_features();
7972        let mut toks = e.stream().alloc_zeros::<u32>(t)?;
7973        for i in 0..t {
7974            e.argmax_token_device_col(&ld, i, n_vocab, &mut toks, i)?;
7975        }
7976        Ok((e.dtoh_u32(&toks)?, hn))
7977    }
7978
7979    /// Device-token verify (async spec round): tokens live in tok_d[0..t]; per-row argmax
7980    /// lands in a DEVICE buffer (no host logits). Returns (vam_d [t] u32 device, hn stack).
7981    pub(crate) fn gemma4_decode_step_t_am_dev(&self, e: &Engine, tok_d: &CudaSlice<u32>, t: usize,
7982                                              pos0: usize, cache: &mut Cache)
7983                                              -> Result<(CudaSlice<u32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7984        let (ld, hn) = self.gemma4_verify_trunk(e, &vec![0u32; t], pos0, cache, Some(tok_d))?;
7985        let n_vocab = self.output.out_features();
7986        let mut vam = e.stream().alloc_zeros::<u32>(t)?;
7987        for i in 0..t {
7988            e.argmax_token_device_col(&ld, i, n_vocab, &mut vam, i)?;
7989        }
7990        Ok((vam, hn))
7991    }
7992
7993    /// gemma4 verify + the POST-output_norm hidden stack [t, n_embd] (the drafter's h input —
7994    /// llama's h_nextn convention).
7995    pub(crate) fn gemma4_decode_step_t_h(&self, e: &Engine, tokens: &[u32], pos0: usize,
7996                                         cache: &mut Cache)
7997                                         -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7998        let (mut ld, hn) = self.gemma4_verify_trunk(e, tokens, pos0, cache, None)?;
7999        let t = tokens.len();
8000        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
8001        e.softcap(&mut ld, cap, t * self.output.out_features())?;
8002        Ok((e.dtoh(&ld)?, hn))
8003    }
8004
8005    /// Persistent device scratch for the BURST verify stream (one per generation): rope pos
8006    /// rows [cap] + per-row t_kv counters (device-filled from the round counter, zero H2D).
8007    pub(crate) fn verify_stream_scratch(&self, e: &Engine, cap: usize)
8008                                        -> Result<VerifyStreamScratch, Box<dyn std::error::Error>> {
8009        Ok(VerifyStreamScratch {
8010            pos_d: e.htod_i32(&vec![0i32; cap])?,
8011            row_ctrs: (0..cap).map(|_| e.htod_i32(&[0])).collect::<Result<_, _>>()?,
8012        })
8013    }
8014
8015    /// BURST verify trunk (device-slot twin): tokens from a device buffer, rope positions
8016    /// iota'd from `ctr`, appends/attention at the layers' len_d counters (verify_attn_stream),
8017    /// NO host cache.pos/len advance. Returns (per-row device argmaxes [t], post-norm hidden
8018    /// stack [t, n_embd]) — the burst's accept/seed inputs, zero host readbacks.
8019    /// `scr` = PERSISTENT per-gen scratch (pos rows + per-row t_kv counters): the first cut
8020    /// htod_i32-allocated these per call — 9 pageable H2D copies per round, each a stream
8021    /// sync, exactly the turnaround the burst exists to remove.
8022    pub(crate) fn gemma4_verify_t_am_stream(&self, e: &Engine, tok_d: &CudaSlice<u32>, t: usize,
8023                                            ctr: &CudaSlice<i32>, hint: usize,
8024                                            cache: &mut Cache,
8025                                            scr: &mut VerifyStreamScratch)
8026                                            -> Result<(CudaSlice<u32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
8027        let n_embd = self.cfg.n_embd as usize;
8028        let eps = self.cfg.rms_eps;
8029        assert!(t <= scr.row_ctrs.len() && t <= 64);
8030        e.i32_iota_from(ctr, &mut scr.pos_d, t)?;
8031        for i in 0..t {
8032            e.i32_copy_add(ctr, &mut scr.row_ctrs[i], (i + 1) as i32)?;
8033        }
8034        let (pos_d, row_ctrs) = (&scr.pos_d, &scr.row_ctrs);
8035        let embd_gpu = self.embd_gpu.get_or_init(|| {
8036            e.upload_u8(&self.embd.raw).expect("embed table upload")
8037        });
8038        let (qt, rb) = self.embd.qt_and_row_bytes(n_embd);
8039        let mut x = e.embed_gather_device_td(embd_gpu, tok_d, t, n_embd, qt, rb)?;
8040        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
8041        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
8042        let n_layers = self.layers.len();
8043        for (il, layer) in self.layers.iter().enumerate() {
8044            let (hq, hdq) = match h_carry.take() {
8045                Some(p) => p,
8046                None => e.rms_norm_q8_1(&x, self.layers[0].attn_norm.float_data(), n_embd, t, eps)?,
8047            };
8048            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
8049            let o = self.gemma4_verify_attn_stream(e, fa, il, &hq, &hdq, pos_d, t, cache,
8050                                                    hint, row_ctrs)?;
8051            let mut cur = e.uninit(t * n_embd)?;
8052            e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, t, eps)?;
8053            let next_norm = if il + 1 < n_layers {
8054                Some(self.layers[il + 1].attn_norm.float_data())
8055            } else { None };
8056            let (xn, hn) = self.gemma4_layer_tail_add_nq(e, layer, &cur, &x, t, next_norm)?;
8057            x = xn;
8058            h_carry = hn;
8059            self.dflash_tap(e, cache, il, &x, t)?;
8060        }
8061        let mut hn = e.uninit(t * n_embd)?;
8062        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
8063        let ld = e.matmul(&self.output, &hn, t)?;
8064        let n_vocab = self.output.out_features();
8065        let mut vam = e.stream().alloc_zeros::<u32>(t)?;
8066        for i in 0..t {
8067            e.argmax_token_device_col(&ld, i, n_vocab, &mut vam, i)?;
8068        }
8069        Ok((vam, hn))
8070    }
8071
8072    /// Verify trunk core: returns (UN-softcapped logits device [t, n_vocab], post-output_norm
8073    /// hidden stack [t, n_embd]); appends KV rows + advances cache.pos.
8074    /// DFlash tap write (dflash lane): copy the post-layer residual rows of `x` into the
8075    /// armed sink at the tap slot for `il` (row-major [t, n_taps*hidden]). Per-row D2D
8076    /// copies — t <= block_size on verify; prime pays t*n_taps once per prompt (dedicated
8077    /// kernel later if it shows in the profile).
8078    fn dflash_tap(&self, e: &Engine, cache: &mut Cache, il: usize, x: &CudaSlice<f32>, t: usize)
8079                  -> Result<(), Box<dyn std::error::Error>> {
8080        let Some(taps) = cache.dflash_taps.as_mut() else { return Ok(()) };
8081        let Some(slot) = taps.layer_ids.iter().position(|&l| l == il) else { return Ok(()) };
8082        let h = taps.hidden;
8083        let n_taps = taps.layer_ids.len();
8084        debug_assert_eq!(taps.t, t);
8085        let xv = e.view(x, t * h);
8086        for r in 0..t {
8087            let row = xv.slice(r * h..(r + 1) * h);
8088            e.copy_view_into(&mut taps.buf, r * n_taps * h + slot * h, &row, h)?;
8089        }
8090        Ok(())
8091    }
8092
8093    fn gemma4_verify_trunk(&self, e: &Engine, tokens: &[u32], pos0: usize, cache: &mut Cache,
8094                           tok_dev: Option<&CudaSlice<u32>>)
8095                           -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
8096        let n_embd = self.cfg.n_embd as usize;
8097        let eps = self.cfg.rms_eps;
8098        let t = tokens.len();
8099        let pos: Vec<i32> = (0..t).map(|i| (pos0 + i) as i32).collect();
8100        let pos_d = e.htod_i32(&pos)?;
8101        let mut x = match tok_dev {
8102            Some(td) => {
8103                let embd_gpu = self.embd_gpu.get_or_init(|| {
8104                    e.upload_u8(&self.embd.raw).expect("embed table upload")
8105                });
8106                let (qt, rb) = self.embd.qt_and_row_bytes(n_embd);
8107                e.embed_gather_device_td(embd_gpu, td, t, n_embd, qt, rb)?
8108            }
8109            None => e.htod(&self.embd.gather(n_embd, tokens))?,
8110        };
8111        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
8112        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
8113        let n_layers = self.layers.len();
8114        for (il, layer) in self.layers.iter().enumerate() {
8115            let (hq, hdq) = match h_carry.take() {
8116                Some(p) => p,
8117                None => e.rms_norm_q8_1(&x, self.layers[0].attn_norm.float_data(), n_embd, t, eps)?,
8118            };
8119            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
8120            let o = self.gemma4_verify_attn(e, fa, il, &hq, &hdq, &pos_d, t, cache)?;
8121            let mut cur = e.uninit(t * n_embd)?;
8122            e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, t, eps)?;
8123            let next_norm = if il + 1 < n_layers {
8124                Some(self.layers[il + 1].attn_norm.float_data())
8125            } else { None };
8126            let (xn, hn) = self.gemma4_layer_tail_add_nq(e, layer, &cur, &x, t, next_norm)?;
8127            x = xn;
8128            h_carry = hn;
8129            self.dflash_tap(e, cache, il, &x, t)?;
8130        }
8131        let mut hn = e.uninit(t * n_embd)?;
8132        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
8133        let mut ld = e.matmul(&self.output, &hn, t)?;
8134        self.gemma4_suppress(e, &mut ld, t)?;   // before the per-row argmax consumers
8135        cache.pos += t;
8136        Ok((ld, hn))
8137    }
8138
8139    /// Verify attention: project/norm/rope t rows, append them to the cache, then attend each
8140    /// row causally over [win_off_i .. base+i] via the SAME fa_decode dispatch as T=1 decode.
8141    /// BURST verify attention (device-slot twin of `gemma4_verify_attn`): the append lands
8142    /// at the layer's len_d counter (rows_dc), attention bases ride the counter (rows /
8143    /// rows_w with base_dev), and NO host len is read or bumped — `hint` is a host UPPER
8144    /// bound on base_len used only for split sizing and arm gating (the burst loop passes
8145    /// pos0 + burst slack). Host len mirrors re-sync at the burst drain.
8146    #[allow(clippy::too_many_arguments)]
8147    fn gemma4_verify_attn_stream(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer, il: usize,
8148                                 hq: &CudaSlice<i8>, hdq: &CudaSlice<f32>,
8149                                 pos_d: &CudaSlice<i32>, t: usize,
8150                                 cache: &mut Cache, hint: usize,
8151                                 row_ctrs: &[CudaSlice<i32>])
8152                                 -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8153        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
8154        let eps = self.cfg.rms_eps;
8155        let aux = self.gemma4_aux.as_ref().unwrap();
8156        let h0 = e.zeros(0)?;
8157        let h = &h0;
8158        // BATCHED FUSED qkv (MEMRA_F2B=1, megakernel microcosm): swa layers fuse all three,
8159        // globals fuse q,k (v := k clone). Bit-identical per row; segments tail-fill.
8160        static F2B_QKV: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
8161        let f2b = *F2B_QKV.get_or_init(|| std::env::var("MEMRA_F2B").as_deref() != Ok("0"));
8162        let fused_qkv = if f2b {
8163            if swa {
8164                e.matmul_q4_fused3_batched(&fa.wq, &fa.wk, &fa.wv, hq, hdq, t)?
8165                    .map(|(a, b, c)| (a, b, Some(c)))
8166            } else {
8167                e.matmul_q4_fused2_batched(&fa.wq, &fa.wk, hq, hdq, t)?
8168                    .map(|(a, b)| (a, b, None))
8169            }
8170        } else { None };
8171        let (q0, k0, v0) = match fused_qkv {
8172            Some((a, b, cv)) => {
8173                let v = match cv { Some(c) => c, None => e.clone_dtod(&b)? };
8174                (a, b, v)
8175            }
8176            None => {
8177                let q0 = e.matmul_pre(&fa.wq, hq, hdq, h, t)?;
8178                let k0 = e.matmul_pre(&fa.wk, hq, hdq, h, t)?;
8179                let v0 = if swa { e.matmul_pre(&fa.wv, hq, hdq, h, t)? }
8180                         else { e.clone_dtod(&k0)? };
8181                (q0, k0, v0)
8182            }
8183        };
8184        let mut q = e.uninit(t * nh * hd)?;
8185        let mut k = e.uninit(t * nkv * hd)?;
8186        let mut v = e.uninit(t * nkv * hd)?;
8187        // E4B wave-3 fold (2026-07-23 decode-dust port): q/k/v norms + q/k rope in ONE
8188        // launch — rope math verbatim on the normed rows; V ones-rms, never roped.
8189        let ff = if swa { None } else {
8190            Some(aux.rope_freqs.as_ref().expect("gemma4 global rope needs rope_freqs.weight"))
8191        };
8192        e.rms_norm_qkv_rope(&q0, &k0, &v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
8193                            &aux.ones, &mut q, &mut k, &mut v, hd, nh * t, nkv * t,
8194                            pos_d, nh, nkv, base, 1.0, ff, eps)?;
8195        let kvl = cache.kv[il].as_mut().unwrap();
8196        // append at the DEVICE slot; the counter advances by t on-device.
8197        e.append_kv_quantized_rows_dc(&k, &v, &mut kvl.k, &mut kvl.v, &kvl.len_d, t,
8198                                      kvl.kv_dim_k, kvl.kv_dim_v,
8199                                      kvl.k_tok_bytes, kvl.v_tok_bytes,
8200                                      (!swa && crate::Engine::gkv_on())
8201                                          || (swa && crate::Engine::wkv_on()))?;
8202        // len_d is NOT advanced here: the burst's device rollback (spec_rollback_stream) is
8203        // the sole len writer after this round's attention (base stays = old len, plus = 0).
8204        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
8205        let mut attn = e.uninit(t * nh * hd)?;
8206        let k_view = e.view_u8(&kvl.k, kvl.k.len());
8207        let v_view = e.view_u8(&kvl.v, kvl.v.len());
8208        // arm gating on the host HINT (upper bound; burst entry guards hint >= the vec floor
8209        // and a stable window regime — the same rung/regime keys as the draft graph).
8210        if swa && hint + 1 >= win {
8211            // fully-windowed rows: per-row window geometry from the counter (plus = 0: len_d
8212            // still holds the pre-append len; row r's T_kv = ctr + r + 1).
8213            e.fa_decode_rows_w(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
8214                               &kvl.len_d, 0, t, scale, win,
8215                               kvl.k_tok_bytes, kvl.v_tok_bytes, None)?;
8216        } else if hd == 512 && hint + t < crate::fa512_min_tkv() {
8217            // globals UNDER the fa512 crossover: eager runs the per-row fa_decode_kvmod
8218            // fallback there (parity with t=1 decode) — mirror it with per-row fa_decode_dc
8219            // (bit-correct for any t_kv <= bucket; the drafter dc arc proved the pairing).
8220            // Burst entry gates the horizon onto one side of the crossover, so hint decides
8221            // for every row.
8222            // NO .max(512): fa_decode_dc gates its hd512 dpl16-vs-scalar pick on bucket_max
8223            // (mirroring eager's fa512 floor) — forcing 512 here flipped the arm to dpl16
8224            // while eager ran scalar (il=5 KV drift, the burst's 4/128). SAME LAW capped
8225            // from above (2026-07-13): a regime-pinned hint near the floor (round-graph
8226            // captures use f512-1) pow2-rounds PAST it — clamp under the floor, or the arm
8227            // re-flips to dpl16 (the round-graph 4/64). The scalar unified self-splits, so
8228            // any bucket >= the live length is exact.
8229            let bucket = (hint + t + 2).next_power_of_two()
8230                .min(crate::fa512_min_tkv().saturating_sub(1));
8231            let qv = e.view(&q, t * nh * hd);
8232            for i in 0..t {
8233                let q_row = qv.slice(i * nh * hd..(i + 1) * nh * hd);
8234                let mut q_one = e.uninit(nh * hd)?;
8235                e.copy_view_into(&mut q_one, 0, &q_row, nh * hd)?;
8236                let mut a_one = e.uninit(nh * hd)?;
8237                e.fa_decode_dc(&q_one, &k_view, &v_view, &mut a_one, hd, nh, nkv,
8238                               &row_ctrs[i], bucket, scale,
8239                               kvl.k_tok_bytes, kvl.v_tok_bytes, false)?;
8240                e.copy_into(&mut attn, i * nh * hd, &a_one, nh * hd)?;
8241            }
8242        } else if hd == 512 {
8243            // globals past the crossover: dpl16 dc twin via the shared rows wrapper (hint
8244            // sizes splits — upper bound; splits beyond the device len exit in-kernel).
8245            e.fa_decode_rows(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, hint, t, scale,
8246                             kvl.k_tok_bytes, kvl.v_tok_bytes,
8247                             Some((&kvl.len_d, 0)), false, false, None)?;
8248        } else {
8249            // hd256 under-window: v4 device-len rows twin.
8250            e.fa_decode_rows_dc(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
8251                                &kvl.len_d, hint + t, t, scale,
8252                                kvl.k_tok_bytes, kvl.v_tok_bytes, 0,
8253                                swa && crate::Engine::wkv_on())?;
8254        }
8255        Ok(e.matmul(&fa.wo, &attn, t)?)
8256    }
8257
8258    fn gemma4_verify_attn(&self, e: &Engine, fa: &crate::hybrid::FullAttnLayer, il: usize,
8259                          hq: &CudaSlice<i8>, hdq: &CudaSlice<f32>,
8260                          pos_d: &CudaSlice<i32>, t: usize,
8261                          cache: &mut Cache)
8262                          -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8263        let (hd, nkv, nh, base, scale, swa) = self.gemma4_geom(il);
8264        let eps = self.cfg.rms_eps;
8265        let aux = self.gemma4_aux.as_ref().unwrap();
8266        let n_embd = self.cfg.n_embd as usize;
8267        let _ = n_embd;
8268
8269        let h0 = e.zeros(0)?;
8270        let h = &h0;
8271        // BATCHED FUSED qkv (MEMRA_F2B=1, megakernel microcosm): swa layers fuse all three,
8272        // globals fuse q,k (v := k clone). Bit-identical per row; segments tail-fill.
8273        static F2B_QKV: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
8274        let f2b = *F2B_QKV.get_or_init(|| std::env::var("MEMRA_F2B").as_deref() != Ok("0"));
8275        let fused_qkv = if f2b {
8276            if swa {
8277                e.matmul_q4_fused3_batched(&fa.wq, &fa.wk, &fa.wv, hq, hdq, t)?
8278                    .map(|(a, b, c)| (a, b, Some(c)))
8279            } else {
8280                e.matmul_q4_fused2_batched(&fa.wq, &fa.wk, hq, hdq, t)?
8281                    .map(|(a, b)| (a, b, None))
8282            }
8283        } else { None };
8284        let (q0, k0, v0) = match fused_qkv {
8285            Some((a, b, cv)) => {
8286                let v = match cv { Some(c) => c, None => e.clone_dtod(&b)? };
8287                (a, b, v)
8288            }
8289            None => {
8290                let q0 = e.matmul_pre(&fa.wq, hq, hdq, h, t)?;
8291                let k0 = e.matmul_pre(&fa.wk, hq, hdq, h, t)?;
8292                let v0 = if swa { e.matmul_pre(&fa.wv, hq, hdq, h, t)? }
8293                         else { e.clone_dtod(&k0)? };
8294                (q0, k0, v0)
8295            }
8296        };
8297        let mut q = e.uninit(t * nh * hd)?;
8298        let mut k = e.uninit(t * nkv * hd)?;
8299        let mut v = e.uninit(t * nkv * hd)?;
8300        // E4B wave-3 fold (2026-07-23 decode-dust port): q/k/v norms + q/k rope in ONE
8301        // launch — rope math verbatim on the normed rows; V ones-rms, never roped.
8302        let ff = if swa { None } else {
8303            Some(aux.rope_freqs.as_ref().expect("gemma4 global rope needs rope_freqs.weight"))
8304        };
8305        e.rms_norm_qkv_rope(&q0, &k0, &v0, fa.q_norm.float_data(), fa.k_norm.float_data(),
8306                            &aux.ones, &mut q, &mut k, &mut v, hd, nh * t, nkv * t,
8307                            pos_d, nh, nkv, base, 1.0, ff, eps)?;
8308        let kvl = cache.kv[il].as_mut().unwrap();
8309        let base_len = kvl.len;
8310        e.append_kv_quantized_rows(&k, &v, &mut kvl.k, &mut kvl.v, base_len, t,
8311                                   kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes, (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on()))?;
8312        kvl.len += t;
8313        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
8314        let mut attn = e.uninit(t * nh * hd)?;
8315        // ROWS twin when no window offset is in play (globals are hd512-ineligible; SWA rows
8316        // under the window need no offset): ONE launch, per-row causal == the per-token loop.
8317        let rows_ok = (hd == 256 && base_len + 1 >= crate::fa_vec_min_tkv())
8318            // gemma globals: hd512 rows twin in the dpl16 vec regime (row 0 gates the batch);
8319            // decode rides the SAME symbol at t=1 (parity law).
8320            || (hd == 512 && !swa && base_len + 1 >= crate::fa512_min_tkv());
8321        if rows_ok && (!swa || base_len + t <= win) {
8322            let k_view = e.view_u8(&kvl.k, (base_len + t) * kvl.k_tok_bytes);
8323            let v_view = e.view_u8(&kvl.v, (base_len + t) * kvl.v_tok_bytes);
8324            if hd == 512 {
8325                // device-len twin: sync the counter to the verify base (async arg-store).
8326                e.i32_set_k(&mut kvl.len_d, base_len as i32)?;
8327                e.fa_decode_rows(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, base_len, t,
8328                                 scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
8329                                 Some((&kvl.len_d, 0)), false,
8330                                 swa && crate::Engine::wkv_on(), None)?;
8331            } else {
8332                // hd256: the SAME v4_dc symbol the burst verify launches (PARITY LAW — the
8333                // host-base rows_v4 twin compiles apart from v4_dc and the two-symbol split
8334                // drifted the burst's persisted KV at il=8; one symbol, counter arg-stored).
8335                e.i32_set_k(&mut kvl.len_d, base_len as i32)?;
8336                e.fa_decode_rows_dc(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
8337                                    &kvl.len_d, base_len + t, t, scale,
8338                                    kvl.k_tok_bytes, kvl.v_tok_bytes, 0,
8339                                    swa && crate::Engine::wkv_on())?;
8340            }
8341            return Ok(e.matmul(&fa.wo, &attn, t)?);
8342        }
8343        // WINDOWED rows twin (deep ctx, every row fully windowed): one launch, ABSOLUTE-index
8344        // per-row geometry over the prefix view. PARITY LAW (2026-07-10 root-cause): textually
8345        // identical kernels do NOT compile bit-identically (nvcc unrolls fa_decode_vec_q 2x vs
8346        // its rows_w clone — SASS-proven; the unpinned score `+=` chain then rounds apart), so
8347        // decode-vs-verify parity comes from BOTH sides launching THIS SAME rows_w kernel
8348        // (decode passes t=1) — bitwise equal per position by symbol identity, any lane.
8349        // MEMRA_GEMMA_ROWS_W=0 -> per-token loop (decode falls back to fa_decode views too).
8350        if hd == 256 && swa && base_len + 1 >= win
8351            && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
8352            let k_view = e.view_u8(&kvl.k, (base_len + t) * kvl.k_tok_bytes);
8353            let v_view = e.view_u8(&kvl.v, (base_len + t) * kvl.v_tok_bytes);
8354            e.i32_set_k(&mut kvl.len_d, base_len as i32)?;
8355            e.fa_decode_rows_w(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, &kvl.len_d, 0,
8356                               t, scale, win, kvl.k_tok_bytes, kvl.v_tok_bytes, None)?;
8357            return Ok(e.matmul(&fa.wo, &attn, t)?);
8358        }
8359        for i in 0..t {
8360            let avail = base_len + i + 1;
8361            let (off_tok, t_kv) = if swa && avail > win { (avail - win, win) } else { (0, avail) };
8362            let k_view = e.view_u8_range(&kvl.k, off_tok * kvl.k_tok_bytes,
8363                                         (off_tok + t_kv) * kvl.k_tok_bytes);
8364            let v_view = e.view_u8_range(&kvl.v, off_tok * kvl.v_tok_bytes,
8365                                         (off_tok + t_kv) * kvl.v_tok_bytes);
8366            let qi = e.view(&q, t * nh * hd);
8367            let q_row = qi.slice(i * nh * hd..(i + 1) * nh * hd);
8368            let mut q_one = e.uninit(nh * hd)?;
8369            e.copy_view_into(&mut q_one, 0, &q_row, nh * hd)?;
8370            let mut a_one = e.uninit(nh * hd)?;
8371            // straddle rounds: windowed rows must ride the SAME rows_w kernel decode uses
8372            // (parity law above); global hd512 rows past the fa512 floor likewise ride the
8373            // rows_dpl16 twin; remaining rows keep the gated fa_decode pair.
8374            if swa && avail > win && hd == 256
8375                && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
8376                let kp = e.view_u8(&kvl.k, avail * kvl.k_tok_bytes);
8377                let vp = e.view_u8(&kvl.v, avail * kvl.v_tok_bytes);
8378                e.i32_set_k(&mut kvl.len_d, (avail - 1) as i32)?;
8379                e.fa_decode_rows_w(&q_one, &kp, &vp, &mut a_one, hd, nh, nkv, &kvl.len_d, 0,
8380                                   1, scale, win, kvl.k_tok_bytes, kvl.v_tok_bytes, None)?;
8381            } else if !swa && hd == 512 && avail >= crate::fa512_min_tkv()
8382                && std::env::var("MEMRA_GEMMA_ROWS_W").as_deref() != Ok("0") {
8383                let kp = e.view_u8(&kvl.k, avail * kvl.k_tok_bytes);
8384                let vp = e.view_u8(&kvl.v, avail * kvl.v_tok_bytes);
8385                e.i32_set_k(&mut kvl.len_d, (avail - 1) as i32)?;
8386                e.fa_decode_rows(&q_one, &kp, &vp, &mut a_one, hd, nh, nkv, avail - 1, 1,
8387                                 scale, kvl.k_tok_bytes, kvl.v_tok_bytes,
8388                                 Some((&kvl.len_d, 0)), false, false, None)?;
8389            } else {
8390                e.fa_decode_kvmod(&q_one, &k_view, &v_view, &mut a_one, hd, nh, nkv, t_kv, scale,
8391                            kvl.k_tok_bytes, kvl.v_tok_bytes, swa && crate::Engine::wkv_on())?;
8392            }
8393            e.copy_into(&mut attn, i * nh * hd, &a_one, nh * hd)?;
8394        }
8395        Ok(e.matmul(&fa.wo, &attn, t)?)
8396    }
8397
8398    /// gemma4 T=1 decode step: R8 layer graph over the cache; returns (softcapped logits host,
8399    /// h_seed = pre-output_norm hidden). Advances cache.pos.
8400    pub(crate) fn gemma4_decode_step_h(&self, e: &Engine, token: u32, cache: &mut Cache)
8401                                       -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
8402        // M1-PP2 door (crate::pp): 2-stage split with an explicit activation handoff.
8403        // Default OFF — unset env means this branch is never taken. The gemma4 arm stays
8404        // 2-stage in M2 (the N-stage gate model is the generic-arm 9B); N>2 warns + runs
8405        // unsplit rather than guessing a fence.
8406        if let Some(split) = crate::pp::pp2_split(self.layers.len()) {
8407            return self.gemma4_decode_step_h_pp2(e, token, cache, split);
8408        }
8409        if crate::pp::pp_cuts(self.layers.len()).is_some() {
8410            crate::pp::warn_unwired_once("gemma4 eager decode (N>2)");
8411        }
8412        let n_embd = self.cfg.n_embd as usize;
8413        let eps = self.cfg.rms_eps;
8414        let pos_d = e.htod_i32(&[cache.pos as i32])?;
8415        let mut x = e.htod(&self.embd.gather(n_embd, &[token]))?;
8416        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
8417        // cross-layer fusion: each tail's closing add+scale also EMITS the next layer's
8418        // attn-normed input pre-quantized q8_1 (the mixer consumes only quantized matmuls).
8419        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
8420        let n_layers = self.layers.len();
8421        for (il, layer) in self.layers.iter().enumerate() {
8422            let (hq, hdq) = match h_carry.take() {
8423                Some(p) => p,
8424                None => e.rms_norm_q8_1(&x, self.layers[0].attn_norm.float_data(), n_embd, 1, eps)?,
8425            };
8426            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
8427            let o = self.gemma4_decode_attn(e, fa, il, &hq, &hdq, &pos_d, cache)?;
8428            let mut cur = e.uninit(n_embd)?;
8429            e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, 1, eps)?;
8430            let next_norm = if il + 1 < n_layers {
8431                Some(self.layers[il + 1].attn_norm.float_data())
8432            } else { None };
8433            let (xn, hn) = self.gemma4_layer_tail_add_nq(e, layer, &cur, &x, 1, next_norm)?;
8434            x = xn;
8435            h_carry = hn;
8436        }
8437        let mut hn = e.uninit(n_embd)?;
8438        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
8439        let h_seed = e.clone_dtod(&x)?;
8440        let mut ld = e.matmul(&self.output, &hn, 1)?;
8441        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
8442        e.softcap(&mut ld, cap, self.output.out_features())?;   // R4 on device (262k host tanh ~ms/step)
8443        self.gemma4_suppress(e, &mut ld, 1)?;
8444        let logits = e.dtoh(&ld)?;
8445        cache.pos += 1;
8446        Ok((logits, h_seed))
8447    }
8448
8449    /// M1-PP2 stage subgraph (gemma4 arm): layers [lo, hi) of the gemma4 T=1 walk with the
8450    /// pre-quantized next-norm carry LOCAL to the range. Enters with a materialized residual
8451    /// `x` (the range head runs its own `rms_norm_q8_1` against ITS layer's attn_norm — for
8452    /// lo == 0 that is exactly the unsplit loop's il==0 arm); exits with the residual
8453    /// materialized (range tail passes next_norm = None, exactly the unsplit last-layer arm).
8454    /// Bit-identity of the cut relies on the kernel-check-pinned `add_scale_rms_norm_q8_1 ==
8455    /// add_scale then rms_norm_q8_1` identity (`pp2-gate` verifies end-to-end).
8456    fn gemma4_decode_layers(&self, e: &Engine, mut x: CudaSlice<f32>, lo: usize, hi: usize,
8457                            pos_d: &CudaSlice<i32>, cache: &mut Cache)
8458                            -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8459        let n_embd = self.cfg.n_embd as usize;
8460        let eps = self.cfg.rms_eps;
8461        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
8462        for il in lo..hi {
8463            let layer = &self.layers[il];
8464            let (hq, hdq) = match h_carry.take() {
8465                Some(p) => p,
8466                // range head: il == lo — norm against THIS layer's attn_norm.
8467                None => e.rms_norm_q8_1(&x, self.layers[il].attn_norm.float_data(), n_embd, 1, eps)?,
8468            };
8469            let Mixer::Full(fa) = &layer.mixer else { panic!("gemma4 layer {il} not full-attn") };
8470            let o = self.gemma4_decode_attn(e, fa, il, &hq, &hdq, pos_d, cache)?;
8471            let mut cur = e.uninit(n_embd)?;
8472            e.rms_norm(&o, layer.post_attn_norm.float_data(), &mut cur, n_embd, 1, eps)?;
8473            let next_norm = if il + 1 < hi {
8474                Some(self.layers[il + 1].attn_norm.float_data())
8475            } else { None };
8476            let (xn, hn) = self.gemma4_layer_tail_add_nq(e, layer, &cur, &x, 1, next_norm)?;
8477            x = xn;
8478            h_carry = hn;
8479        }
8480        Ok(x)
8481    }
8482
8483    /// M1-PP2 (increment 2): `gemma4_decode_step_h` as TWO stage subgraphs, each on its
8484    /// own stream (and device, under MEMRA_PP_DEVICES) with the transport-selected
8485    /// boundary handoff — same choreography as the generic arm (decode.rs), same
8486    /// ownership contract (crate::pp). Stage 0 = embed+scale + layers [0, split);
8487    /// stage 1 = layers [split, n) + output_norm + softcapped head.
8488    /// MEMRA_PP_STREAMS=0 = the increment-1 same-stream seam. Gate: `pp2-gate`.
8489    fn gemma4_decode_step_h_pp2(&self, e: &Engine, token: u32, cache: &mut Cache, split: usize)
8490                                -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
8491        if crate::pp::pp_host_bounce_active() {
8492            return Err(
8493                "gemma4_decode_step_h_pp2: refused with MEMRA_PP_HOST_BOUNCE=1 because stage 1 \
8494                 still peer-reads stage 0's position buffer; add a stage-local position upload \
8495                 before enabling host bounce for gemma4"
8496                    .into(),
8497            );
8498        }
8499        if crate::pp::pp2_streams_off() {
8500            return self.gemma4_decode_step_h_pp2_samestream(e, token, cache, split);
8501        }
8502        let rt = crate::pp::Pp2Rt::get(e)?;
8503        let e0 = rt.engine(0, e);
8504        let e1 = rt.engine(1, e);
8505        let n_embd = self.cfg.n_embd as usize;
8506        let eps = self.cfg.rms_eps;
8507
8508        // ---- STAGE 0 (its own stream): embed + sqrt(n_embd) scale + layers [0, split) ----
8509        let (pos_d, slot) = {
8510            let _st0 = rt.enter(0);
8511            let pos_d = e0.htod_i32(&[cache.pos as i32])?;
8512            let mut x = e0.htod(&self.embd.gather(n_embd, &[token]))?;
8513            e0.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
8514            let x = self.gemma4_decode_layers(e0, x, 0, split, &pos_d, cache)?;
8515            let slot = rt.tx(0, &x, n_embd)?;
8516            (pos_d, slot)
8517        };
8518
8519        // ---- STAGE 1 (its own stream): RX + layers [split, n) + softcapped head ----
8520        let _st1 = rt.enter(1);
8521        let x = rt.rx(0, slot, n_embd)?;
8522        let x = self.gemma4_decode_layers(e1, x, split, self.layers.len(), &pos_d, cache)?;
8523
8524        let mut hn = e1.uninit(n_embd)?;
8525        e1.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
8526        let h_seed = e1.clone_dtod(&x)?;
8527        let mut ld = e1.matmul(&self.output, &hn, 1)?;
8528        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
8529        e1.softcap(&mut ld, cap, self.output.out_features())?;
8530        self.gemma4_suppress(e1, &mut ld, 1)?;
8531        let logits = e1.dtoh(&ld)?;
8532        cache.pos += 1;
8533        Ok((logits, h_seed))
8534    }
8535
8536    /// MEMRA_PP_STREAMS=0 rollback seam: the increment-1 gemma4 pp2 body verbatim — both
8537    /// stage subgraphs on the ambient compute stream, boundary = two plain dtod copies.
8538    fn gemma4_decode_step_h_pp2_samestream(&self, e: &Engine, token: u32, cache: &mut Cache,
8539                                           split: usize)
8540                                           -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
8541        let n_embd = self.cfg.n_embd as usize;
8542        let eps = self.cfg.rms_eps;
8543        let pos_d = e.htod_i32(&[cache.pos as i32])?;
8544
8545        // ---- STAGE 0: embed + sqrt(n_embd) scale + layers [0, split) ----
8546        let mut x = e.htod(&self.embd.gather(n_embd, &[token]))?;
8547        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
8548        let x = self.gemma4_decode_layers(e, x, 0, split, &pos_d, cache)?;
8549
8550        // ---- STAGE BOUNDARY: explicit [n_embd] activation handoff (TX copy, RX copy) ----
8551        let boundary_tx = e.clone_dtod(&x)?;
8552        let boundary_rx = e.clone_dtod(&boundary_tx)?;
8553
8554        // ---- STAGE 1: layers [split, n) + output_norm + softcapped head ----
8555        let x = self.gemma4_decode_layers(e, boundary_rx, split, self.layers.len(), &pos_d, cache)?;
8556
8557        let mut hn = e.uninit(n_embd)?;
8558        e.rms_norm(&x, self.output_norm.float_data(), &mut hn, n_embd, 1, eps)?;
8559        let h_seed = e.clone_dtod(&x)?;
8560        let mut ld = e.matmul(&self.output, &hn, 1)?;
8561        let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
8562        e.softcap(&mut ld, cap, self.output.out_features())?;
8563        self.gemma4_suppress(e, &mut ld, 1)?;
8564        let logits = e.dtoh(&ld)?;
8565        cache.pos += 1;
8566        Ok((logits, h_seed))
8567    }
8568}
8569
8570// ============================ step35 (Step-3.7-Flash) ==================================
8571// Node-for-node vs llama.cpp src/models/step35.cpp:216-300. WHY THIS IS A DEDICATED MIXER
8572// FAMILY and not a few branches inside the generic `full_attn*` chain:
8573//
8574//   1. `n_head` IS PER LAYER (`step35.attention.head_count` is an ARRAY: 64 on full-attn
8575//      layers, 96 on SWA). Every generic site reads the `cfg.n_head` scalar, so wq/wo/attn_gate
8576//      shapes and the FA head counts would be wrong on 33 of 45 layers.
8577//   2. `cfg.rope_dim_count` is 128 for this arch, but FULL-attn layers rotate only 64 dims
8578//      (upstream halves `n_rot_full` after the generic loader defaults it). A generic mixer
8579//      would rotate 128 dims on the full layers — silently wrong, plausible-looking logits.
8580//   3. Dual rope base (5e6 full / 1e4 SWA) + `rope_freqs.weight` on FULL layers ONLY.
8581//   4. SWA 3:1 (`sliding_window` 512, pattern [false,true,true,true]).
8582//   5. The head-wise gate is a SEPARATE `attn_gate.weight [n_embd, n_head_l]` (one pre-sigmoid
8583//      scalar per head, broadcast over head_dim) applied to the attention output BEFORE wo —
8584//      not the qwen35 fused-in-wq per-DIM gate that `attn_out_gate()`/`q_gate_split` handle.
8585//      Its input is the POST-attn_norm hidden (`cur`), not the residual.
8586//
8587// Attention scale is the DEFAULT 1/sqrt(n_embd_head_k) (step35.cpp:255) — NOT gemma4's 1.0.
8588impl HybridModel {
8589    /// Step35's declarative geometry row. Missing rows are artifact/config errors; never
8590    /// synthesize a drafter or trunk layer from a neighboring class.
8591    pub(crate) fn step35_geom(&self, il: usize) -> memra_gguf::config::LayerGeometry {
8592        let geometry = self.cfg.layer_geometry(il as u32)
8593            .unwrap_or_else(|| panic!("step35 layer {il} has no geometry-table row"));
8594        debug_assert_eq!(
8595            geometry.attention_gate,
8596            memra_gguf::config::AttentionGateKind::SeparateHead
8597        );
8598        geometry
8599    }
8600
8601    /// step35 attention core: everything from the q/k/v projection outputs through the head-wise
8602    /// gate, returning the GATED attention output PRE-`wo` (the caller runs wo, so this composes
8603    /// with both the plain `matmul(&fa.wo, ..)` sites and the f16/into-slab GEMM sites).
8604    ///
8605    /// `hg` = the POST-attn_norm hidden state (upstream's `cur`) — the gate projection's input
8606    /// on single-sequence paths. `gt_pre` supplies the already-projected per-head gate for a
8607    /// cross-request batch; exactly one of `hg` or `gt_pre` must be present.
8608    /// `cache`:
8609    ///   * `Some` => PRIME mode: append this chunk's post-rope K / raw V rows into the resident
8610    ///     quantized cache and attend THROUGH the cache view, exactly like the generic
8611    ///     `full_attn_prime_fa_dispatch` (quantize-then-attend on chunk 0 too, so chunk size
8612    ///     cannot decide where a precision edge falls — the grain-free chunk-invariance
8613    ///     contract, lane/chunkinv-flip).
8614    ///   * `None` => pure prefill (forward/forward_last/t2probe): attend over this batch's f32
8615    ///     q/k/v, no cache side effect.
8616    ///
8617    /// SWA WINDOW, and why prefill needs `sdpa_naive_w_quantized_view`: the view is trimmed to
8618    /// the oldest key ANY query in this chunk can reach (`off = base_len - (win-1)`), which
8619    /// bounds the view at `win-1+t` rows, but the mask is still required — inside the chunk,
8620    /// query `qt` may only see view keys `[qt, qt+win-1]`, so the earlier keys the trimmed view
8621    /// still contains must be masked per query. memra's window convention
8622    /// (`sdpa_naive_w_f32`: mask `t < q_pos-(window-1)`, `q_pos = (T_kv-T)+qt`) is bit-for-bit
8623    /// upstream's `LLAMA_SWA_TYPE_STANDARD` (`llama-hparams.h:359`: mask `p1-p0 >= n_swa`), and
8624    /// step35.cpp:6 sets exactly that swa_type — verified verbatim on both sides.
8625    ///
8626    /// Note the f32 `sdpa_naive_w` floor's shared memory is `t_kv*4` bytes, so the SWA arm needs
8627    /// `t_kv = win-1+t <= 12287` — i.e. it relies on chunked prefill (MEMRA_PRIME_CHUNK, default
8628    /// 4096). A monolithic 32k prime would exceed the 48 KiB dynamic-smem default.
8629    ///
8630    /// SWA ARM SELECTION IS KEYED ON `seq_end`, NOT ON THE CHUNK'S OWN `t_kv` (lane/step35-chunkfix,
8631    /// 2026-08-07). `seq_end` = the ABSOLUTE end position of the whole prime call
8632    /// (`cache.pos + prompt_len` at entry; `t` on the cacheless prefill path) — a property of the
8633    /// REQUEST, identical at every MEMRA_PRIME_CHUNK value. The old predicate `swa && t_kv > win`
8634    /// read the chunk's own extent, which made kernel selection — and therefore the logits, the
8635    /// hidden rows, and the generated text — a function of the chunk size:
8636    ///   a chunk [b,e) has off = max(0, b-(win-1)), t_kv = e-off, so b <= win-1 => off=0 => FA iff
8637    ///   e <= win, while every later chunk has t_kv = t+(win-1) > win. The FA rows were therefore a
8638    ///   contiguous PREFIX [0,P) with P = c*floor(win/c) for c <= win (else 0), and the output
8639    ///   depended only on P. Measured (research/step37-p2-20260806, closed-form receipt
8640    ///   `raw/chunkinv-step35-GAP2-CONFIRMED-20260807.txt`): at T=4883, c=512 and c=64 (P=512) are
8641    ///   byte-identical to each other but DIFFER from the c=4096 default (P=0) by maxdiff 1.813e0
8642    ///   with greedy text diverging at step 6; c=513 (P=0) is bit-identical to the default. A
8643    ///   one-token change in a documented machine-config knob changed the answer.
8644    /// So the pre-fix comment "same cache bytes, same numeric class" was false as written: same
8645    /// bytes, DIFFERENT numeric class — swapping `fa_prefill_view_ws` for the f32 windowed floor on
8646    /// the same rows moves the logits by ~1.8.
8647    ///
8648    /// Keying on `seq_end` makes P identically 0 for every chunk size, so MEMRA_PRIME_CHUNK is a
8649    /// pure memory/transient knob again (research/step35-chunkfix-20260807). It is
8650    /// correct-by-construction rather than a tolerance argument — the windowed kernel computes the
8651    /// same masked attention the FA arm computed (for e <= win the window mask is a no-op under
8652    /// causal, which is exactly why FA was legal there), it is now simply the ONLY arm used once
8653    /// the request passes the window. It also cannot move the shipped default: at chunk=4096 every
8654    /// chunk of a `seq_end > win` prime ALREADY had t_kv > win (chunk 0 has t_kv = min(chunk,
8655    /// seq_end) > win; every later chunk has t_kv >= t+win-1 > win since `PRIME_MIN_T` keeps
8656    /// t >= 16), and a `seq_end <= win` prime keeps every chunk on FA exactly as before — so this
8657    /// is a no-op on both default regimes and only removes the FA prefix at small chunk values.
8658    /// The `t_kv <= 12287` smem ceiling is respected: the chunks whose arm changes are precisely
8659    /// those with t_kv <= win = 512.
8660    #[allow(clippy::too_many_arguments)]
8661    fn step35_attn_pre_wo(&self, e: &Engine, fa: &FullAttnLayer, mut g3: Vec<CudaSlice<f32>>,
8662                          hg: Option<&CudaSlice<f32>>, gt_pre: Option<&CudaSlice<f32>>,
8663                          pos_d: &CudaSlice<i32>, t: usize,
8664                          cache: Option<&mut Cache>, il: usize, seq_end: usize)
8665                          -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8666        let geometry = self.step35_geom(il);
8667        let hd = geometry.head_dim_k as usize;
8668        let nkv = geometry.n_head_kv as usize;
8669        let nh = geometry.n_head as usize;
8670        let rbase = geometry.rope_base;
8671        let scale = geometry.attention_scale();
8672        let swa = geometry.window.is_some();
8673        let eps = self.cfg.rms_eps;
8674        let win = geometry.window.unwrap_or(0) as usize;
8675        let n_rot = geometry.n_rot as usize;
8676
8677        let v = g3.pop().unwrap();
8678        let k0 = g3.pop().unwrap();
8679        let q0 = g3.pop().unwrap();
8680
8681        // q/k RMSNorm over head_dim rows (attn_q_norm/attn_k_norm [128] F32), then the
8682        // per-layer PARTIAL NEOX rope: n_rot = 64 on full layers (dims 64..127 pass through
8683        // unrotated), 128 on SWA. rope_freqs (llama3-style per-dim factors) on FULL only.
8684        let mut q = e.uninit(t * nh * hd)?;
8685        e.rms_norm(&q0, fa.q_norm.float_data(), &mut q, hd, nh * t, eps)?;
8686        let mut k = e.uninit(t * nkv * hd)?;
8687        e.rms_norm(&k0, fa.k_norm.float_data(), &mut k, hd, nkv * t, eps)?;
8688        let ff = if geometry.rope_factors {
8689            self.step35_aux.as_ref().and_then(|a| a.rope_freqs(e))
8690        } else {
8691            None
8692        };
8693        e.rope_neox2(&mut q, &mut k, pos_d, hd, n_rot, nh, nkv, t, rbase, 1.0, ff)?;
8694
8695        let mut attn = e.uninit(t * nh * hd)?;
8696        match cache {
8697            Some(cache) => {
8698                let base_len = cache.kv[il].as_ref().unwrap().len;
8699                // Read per layer call, never in a measured default.
8700                let legacy_tkv = std::env::var("MEMRA_STEP35_SWA_TKV").as_deref() == Ok("1");
8701                let legacy_calllocal =
8702                    std::env::var("MEMRA_PRIME_CALLLOCAL").as_deref() == Ok("1");
8703                let off = if swa {
8704                    let raw = base_len.saturating_sub(win - 1);
8705                    if legacy_tkv || legacy_calllocal { raw } else { raw & !31usize }
8706                } else {
8707                    0
8708                };
8709                {
8710                    let kvl = cache.kv[il].as_mut().unwrap();
8711                    assert!(kvl.len + t <= cache.max_ctx, "step35 prime: KV overflow");
8712                    let write_row = e.prepare_kv_append(kvl, off, t)?;
8713                    e.append_kv_quantized_rows(&k, &v, &mut kvl.k, &mut kvl.v, write_row, t,
8714                                               kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes,
8715                                               kvl.v_tok_bytes, crate::Engine::kv_fp8_on())?;
8716                    kvl.len += t;
8717                    let new_len = kvl.len as i32;
8718                    e.set_i32_one(&mut kvl.len_d, new_len)?;
8719                }
8720                let kvl = cache.kv[il].as_ref().unwrap();
8721                // Both segmentation seams restore the FULL pre-fix arithmetic: their legacy
8722                // arm predicate (`t_kv` below, per-call `seq_end` in prime_cache) AND the
8723                // unaligned view offset here. Both halves are load-bearing for the canaries:
8724                // on the FA default the predicate arms agree bitwise wherever they can differ
8725                // (a t_kv<=win view has no maskable key, so windowed==unwindowed FA bit-for-bit).
8726                // That made predicate-only seams INERT under Lever A — first chunkinv35c, then
8727                // tickinv35c. The raw offset restores the live segmentation-variant mechanism
8728                // on the current FA path: its tile grid starts at the chunk/call boundary.
8729                // SWA: trim the view to the oldest key any query in this chunk can reach —
8730                // ALIGNED DOWN to the FA tile size (BK=32). The raw trim is chunk-DEPENDENT
8731                // (off = base_len-(win-1), and base_len is a chunk boundary), and the FA
8732                // kernel's online-softmax recurrence groups keys into BK tiles relative to
8733                // the VIEW START — so an unaligned off regroups the same absolute keys into
8734                // different tiles at different chunk sizes = different (m,l) rounding =
8735                // chunk-dependent bits. chunkinv35 caught exactly this on the first FA-arm
8736                // battery (first_div == chunk size; raw/leverA-gates-20260807T135541Z.log).
8737                // Aligning off to 32 pins tiles to ABSOLUTE key positions for every chunk
8738                // size; the <=31 extra leading keys are older than EVERY query's window
8739                // (all queries sit at >= base_len, so keys < base_len-(win-1) are masked
8740                // for all of them) and a fully-masked key is an exact-0.0 no-op in both
8741                // kernels (NEG_INF -> p=0.0; l+=0.0 and O+=0.0 are bitwise identity), so
8742                // the floor arm's bits do not move either (gated: G2f, battery 2).
8743                let t_kv = base_len + t - off;
8744                let physical = kvl.physical_rows(off, off + t_kv)?;
8745                let k_view = e.view_u8_range(&kvl.k, physical.start * kvl.k_tok_bytes,
8746                                             physical.end * kvl.k_tok_bytes);
8747                let v_view = e.view_u8_range(&kvl.v, physical.start * kvl.v_tok_bytes,
8748                                             physical.end * kvl.v_tok_bytes);
8749                // CHUNK-INVARIANT PREDICATE: `seq_end` (the request's absolute end position), not
8750                // this chunk's `t_kv`. See the doc note — keying on t_kv made P = c*floor(win/c)
8751                // rows take FA and the output a function of MEMRA_PRIME_CHUNK.
8752                // MEMRA_STEP35_SWA_TKV=1 is the ROLLBACK SEAM to the pre-fix arithmetic — BOTH
8753                // halves: this predicate AND the unaligned view offset above (`legacy_tkv`).
8754                // It is what gives the step35 chunkinv gate its canary teeth: chunk-VARIANT by
8755                // construction, so the invariance assertion MUST break under it (the seam whose
8756                // absence made the original canary inert — GAP 1 in research/step37-p2-20260806;
8757                // and a predicate-only seam went inert AGAIN under the FA default, battery 2's
8758                // CANARY UNEXPECTEDLY MATCHED — see the offset comment). Read per call, not
8759                // cached (probes flip it in-process). Never on in a measured default run.
8760                let swa_naive = if legacy_tkv { t_kv > win } else { seq_end > win };
8761                if swa && swa_naive {
8762                    // Windowed mask needed (see the doc note). DEFAULT since lane/pp-prefill
8763                    // 2026-08-07: the windowed hd128 FA stamp (`fa_prefill_view_ws_w_hd128`) —
8764                    // the anatomy profile measured the f32 floor at 565 ms/layer on a pp4096
8765                    // (41% of the whole prime) while the unwindowed hd128 FA family did the
8766                    // strictly harder causal-4096 in 3.3 ms. NOTE t_kv can be <= win here
8767                    // (a chunk-0 view on a small chunk); the windowed kernel handles that
8768                    // identically to the unwindowed one modulo the mask, which is the point.
8769                    // NEW NUMERIC CLASS vs the floor (bf16-MMA online softmax vs f32 serial),
8770                    // selected on `seq_end` like every arm here, so the class is uniform for
8771                    // the whole request at every MEMRA_PRIME_CHUNK — chunkinv holds by the
8772                    // same construction as the chunkfix. MEMRA_STEP35_SWA_FA=0 = rollback to
8773                    // the f32 floor (the previous numeric config, kept as the A/B seam).
8774                    if std::env::var("MEMRA_STEP35_SWA_FA").as_deref() == Ok("0") {
8775                        e.sdpa_naive_w_quantized_view(&q, &k_view, &v_view, &mut attn, hd, nh,
8776                                                      nkv, t, t_kv, scale, true, win,
8777                                                      kvl.k_tok_bytes, kvl.v_tok_bytes)?;
8778                    } else {
8779                        e.fa_prefill_view_ws_w_hd128(&q, &k_view, &v_view, &mut attn, hd, nh,
8780                                                     nkv, t, t_kv, scale, true, win,
8781                                                     kvl.k_tok_bytes, kvl.v_tok_bytes)?;
8782                    }
8783                } else if std::env::var("MEMRA_NOFA").is_ok() {
8784                    e.sdpa_naive_quantized_view(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
8785                                                t, t_kv, scale, true,
8786                                                kvl.k_tok_bytes, kvl.v_tok_bytes)?;
8787                } else {
8788                    // seq_end <= win (or a full-attn layer): no query in the WHOLE request can
8789                    // reach past the window, so the window mask is a no-op under causal and every
8790                    // chunk rides the hd128 dequant-once FA prefill — one arm for the whole
8791                    // request either way, which is what makes the chunk size arithmetic-free.
8792                    e.fa_prefill_view_ws(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
8793                                         t, t_kv, scale, true,
8794                                         kvl.k_tok_bytes, kvl.v_tok_bytes,
8795                                         crate::Engine::kv_fp8_on())?;
8796                }
8797            }
8798            None => {
8799                // Cacheless prefill (forward/forward_last/t2probe) is MONOLITHIC — there is no
8800                // chunk loop on this path, so seq_end == t and the predicate is unchanged. The
8801                // assert pins that: if a chunked cacheless prefill is ever added, it must thread
8802                // seq_end here too or it re-opens the same door.
8803                debug_assert_eq!(seq_end, t, "step35 cacheless prefill is monolithic (seq_end == t)");
8804                if swa && seq_end > win {
8805                    e.sdpa_naive_w(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true, win)?;
8806                } else if std::env::var("MEMRA_NOFA").is_ok() {
8807                    e.sdpa_naive(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?;
8808                } else {
8809                    e.fa_prefill(&q, &k, &v, &mut attn, hd, nh, nkv, t, t, scale, true)?;
8810                }
8811            }
8812        }
8813
8814        // HEAD-WISE GATE (step35.cpp:267-285): gate = attn_gate(cur) -> [t, n_head_l];
8815        // attn *= sigmoid(gate) broadcast over head_dim. BEFORE wo.
8816        let gw = fa.attn_gate.as_ref()
8817            .ok_or("step35 layer is missing attn_gate.weight (head-wise attention gate)")?;
8818        let gt_owned = if gt_pre.is_none() {
8819            Some(e.matmul(
8820                gw,
8821                hg.ok_or("step35 attention needs hg when gt_pre is absent")?,
8822                t,
8823            )?)
8824        } else {
8825            None
8826        };
8827        let gt = gt_pre.or(gt_owned.as_ref()).unwrap();
8828        let mut ag = e.uninit(t * nh * hd)?;
8829        e.attn_head_gate(&attn, gt, &mut ag, None, hd, nh, t)?;
8830        Ok(ag)
8831    }
8832
8833    /// step35 PREFILL mixer, no cache side effect (the `full_attn` contract: `forward`,
8834    /// `forward_last`, t2probe). Post-`wo`.
8835    pub(crate) fn step35_attn(&self, e: &Engine, fa: &FullAttnLayer, h: &CudaSlice<f32>,
8836                              pos_d: &CudaSlice<i32>, t: usize, il: usize)
8837                              -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8838        let g3 = e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?;
8839        // Monolithic path: the request ends at t (no chunk loop). See step35_attn_pre_wo's note.
8840        let ag = self.step35_attn_pre_wo(e, fa, g3, Some(h), None, pos_d, t, None, il, t)?;
8841        Ok(e.matmul(&fa.wo, &ag, t)?)
8842    }
8843
8844    /// step35 PRIME mixer (the `full_attn_prime` contract: append this chunk's K/V into the
8845    /// resident quantized cache, attend through the cache view). Post-`wo`.
8846    ///
8847    /// `seq_end` = the ABSOLUTE end position of the whole prime request (chunk-invariant); it
8848    /// selects the SWA arm. See `step35_attn_pre_wo`'s doc note for why it cannot be this chunk's
8849    /// own extent.
8850    #[allow(clippy::too_many_arguments)]
8851    pub(crate) fn step35_attn_prime(&self, e: &Engine, fa: &FullAttnLayer, h: &CudaSlice<f32>,
8852                                    hx: Option<&CudaSlice<u8>>, pos_d: &CudaSlice<i32>, t: usize,
8853                                    cache: &mut Cache, il: usize, seq_end: usize)
8854                                    -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8855        let g3 = match hx {
8856            Some(xh) => e.matmul_group_xh(&[&fa.wq, &fa.wk, &fa.wv], h, xh, t)?,
8857            None => e.matmul_group(&[&fa.wq, &fa.wk, &fa.wv], h, t)?,
8858        };
8859        let ag = self.step35_attn_pre_wo(
8860            e,
8861            fa,
8862            g3,
8863            Some(h),
8864            None,
8865            pos_d,
8866            t,
8867            Some(cache),
8868            il,
8869            seq_end,
8870        )?;
8871        Ok(e.matmul(&fa.wo, &ag, t)?)
8872    }
8873
8874    /// step35 T=1 decode attention (post-`wo`, matching `full_attn_decode_pre`'s contract).
8875    /// `pre_q` = the attn-input norm's q8_1 pair when the caller took the norm-fusion lever
8876    /// (then `h` is a zero-length placeholder and EVERY projection here — including the gate —
8877    /// must be on the q8_1 fast path; `mixer_in_q8_1_fast` enforces that for step35 by also
8878    /// requiring `attn_gate`).
8879    ///
8880    /// SWA decode is a token-aligned VIEW OFFSET into the quantized cache (the gemma4 R6
8881    /// pattern): keys carry absolute rope and the mask is purely positional, so the single
8882    /// query at `len-1` attending the last `win` rows IS the windowed result — no mask kernel.
8883    #[allow(clippy::too_many_arguments)]
8884    pub(crate) fn step35_decode_attn(&self, e: &Engine, fa: &FullAttnLayer, il: usize,
8885                          h: &CudaSlice<f32>,
8886                          pre_q: Option<(&CudaSlice<i8>, &CudaSlice<f32>)>,
8887                          pos_d: &CudaSlice<i32>, cache: &mut Cache)
8888                          -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8889        let geometry = self.step35_geom(il);
8890        let hd = geometry.head_dim_k as usize;
8891        let nkv = geometry.n_head_kv as usize;
8892        let nh = geometry.n_head as usize;
8893        let rbase = geometry.rope_base;
8894        let scale = geometry.attention_scale();
8895        let swa = geometry.window.is_some();
8896        let eps = self.cfg.rms_eps;
8897        let win = geometry.window.unwrap_or(0) as usize;
8898        let n_rot = geometry.n_rot as usize;
8899        let n_embd = self.cfg.n_embd as usize;
8900        let gw = fa.attn_gate.as_ref()
8901            .ok_or("step35 layer is missing attn_gate.weight (head-wise attention gate)")?;
8902
8903        let (q0, k0, v0, gt) = match pre_q {
8904            Some((hq, hdq)) => {
8905                debug_assert!(e.uses_q8_1_fast(gw),
8906                    "step35 pre-quantized decode requires attn_gate on the q8_1 fast path \
8907                     (h is a zero-length placeholder here) — see mixer_in_q8_1_fast");
8908                let (a, b, c) = match e.matmul_q8_fused3(&fa.wq, &fa.wk, &fa.wv, hq, hdq)? {
8909                    Some(t3) => t3,
8910                    None => (e.matmul_pre(&fa.wq, hq, hdq, h, 1)?,
8911                             e.matmul_pre(&fa.wk, hq, hdq, h, 1)?,
8912                             e.matmul_pre(&fa.wv, hq, hdq, h, 1)?),
8913                };
8914                let gt = e.matmul_pre(gw, hq, hdq, h, 1)?;
8915                (a, b, c, gt)
8916            }
8917            None => {
8918                if e.uses_q8_1_fast(&fa.wq) && e.uses_q8_1_fast(&fa.wk)
8919                    && e.uses_q8_1_fast(&fa.wv) && e.uses_q8_1_fast(gw) {
8920                    let (hq, hdq) = e.quantize_q8_1(h, 1, n_embd)?;
8921                    let (a, b, c) = match e.matmul_q8_fused3(&fa.wq, &fa.wk, &fa.wv, &hq, &hdq)? {
8922                        Some(t3) => t3,
8923                        None => (e.matmul_pre(&fa.wq, &hq, &hdq, h, 1)?,
8924                                 e.matmul_pre(&fa.wk, &hq, &hdq, h, 1)?,
8925                                 e.matmul_pre(&fa.wv, &hq, &hdq, h, 1)?),
8926                    };
8927                    let gt = e.matmul_pre(gw, &hq, &hdq, h, 1)?;
8928                    (a, b, c, gt)
8929                } else {
8930                    (e.matmul(&fa.wq, h, 1)?, e.matmul(&fa.wk, h, 1)?,
8931                     e.matmul(&fa.wv, h, 1)?, e.matmul(gw, h, 1)?)
8932                }
8933            }
8934        };
8935
8936        let mut q = e.uninit(nh * hd)?;
8937        e.rms_norm(&q0, fa.q_norm.float_data(), &mut q, hd, nh, eps)?;
8938        let mut k = e.uninit(nkv * hd)?;
8939        e.rms_norm(&k0, fa.k_norm.float_data(), &mut k, hd, nkv, eps)?;
8940        let ff = if swa { None } else {
8941            self.step35_aux.as_ref().and_then(|a| a.rope_freqs(e))
8942        };
8943        e.rope_neox2(&mut q, &mut k, pos_d, hd, n_rot, nh, nkv, 1, rbase, 1.0, ff)?;
8944
8945        if std::env::var("MEMRA_NOFA").is_ok() {
8946            return Err("MEMRA_NOFA (naive f32 SDPA) is incompatible with the quantized KV \
8947                        cache; unset MEMRA_NOFA to use fa_decode".into());
8948        }
8949        let kvl = cache.kv[il].as_mut().unwrap();
8950        let next_len = kvl.len + 1;
8951        let (off, t_kv) = if swa && next_len > win { (next_len - win, win) } else { (0, next_len) };
8952        let write_row = e.prepare_kv_append(kvl, off & !31usize, 1)?;
8953        e.append_kv_quantized(&k, &v0, &mut kvl.k, &mut kvl.v, write_row,
8954                              kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes, kvl.v_tok_bytes,
8955                              crate::Engine::kv_fp8_on())?;
8956        kvl.len = next_len;
8957        let physical = kvl.physical_rows(off, off + t_kv)?;
8958        let k_view = e.view_u8_range(&kvl.k, physical.start * kvl.k_tok_bytes,
8959                                     physical.end * kvl.k_tok_bytes);
8960        let v_view = e.view_u8_range(&kvl.v, physical.start * kvl.v_tok_bytes,
8961                                     physical.end * kvl.v_tok_bytes);
8962        let mut attn = e.uninit(nh * hd)?;
8963        e.fa_decode_kvmod(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, t_kv, scale,
8964                          kvl.k_tok_bytes, kvl.v_tok_bytes, crate::Engine::kv_fp8_on())?;
8965
8966        let mut ag = e.uninit(nh * hd)?;
8967        e.attn_head_gate(&attn, &gt, &mut ag, None, hd, nh, 1)?;
8968        Ok(e.matmul(&fa.wo, &ag, 1)?)
8969    }
8970}
8971
8972// ===================================================================================== //
8973//  gemma-4 E4B (per-layer embeddings + KV-sharing) — FIRST-LIGHT forward.               //
8974//  Dedicated simple path (Stage-B matmuls, per-row causal attention over the quantized  //
8975//  cache) so the tuned 26B/31B paths stay untouched. Arch: research/gemma4-bringup/     //
8976//  e4b-arch-map.md; llama reference: src/models/gemma4.cpp (E4B arms). Wired: forward   //
8977//  (prefill logits), prime (tokenwise-equivalent batched trunk), eager decode_step_h.   //
8978//  NOT wired: dc/graph serving arms, verify/spec, chunked prime (HANDOVER-E4B.md).      //
8979// ===================================================================================== //
8980impl HybridModel {
8981    pub fn is_gemma4_e4b(&self) -> bool {
8982        self.gemma4_aux.as_ref().is_some_and(|a| a.e4b.is_some())
8983    }
8984
8985    /// E4B per-layer geometry: nh/nkv derive from the layer's OWN tensor shapes (the E4B GGUF
8986    /// ships a scalar head_count_kv; swa q 8x256 / kv 2x256, global q 4x512 / kv 1x512).
8987    /// KV-shared layers report the SHARE TARGET's kv count (their wk IS the target's tensor).
8988    fn gemma4_e4b_geom(&self, il: usize) -> (usize, usize, usize, f32, f32, bool) {
8989        let g = self.cfg.gemma4.as_ref().unwrap();
8990        let swa = g.swa_pattern[il];
8991        let hd = if swa { g.key_length_swa } else { g.key_length_global } as usize;
8992        let Mixer::Full(fa) = &self.layers[il].mixer else { panic!("e4b layer {il} not full-attn") };
8993        let nh = fa.wq.out_features() / hd;
8994        let nkv = fa.wk.out_features() / hd;
8995        (hd, nkv, nh, if swa { g.rope_base_swa } else { g.rope_base_global }, 1.0, swa)
8996    }
8997
8998    /// E4B KV-share target: Some(target) for the trailing shared layers, None = own cache.
8999    fn gemma4_e4b_kv_target(&self, il: usize) -> Option<usize> {
9000        self.layers[il].gemma4.as_ref()
9001            .and_then(|b| b.e4b.as_ref())
9002            .and_then(|e4| e4.kv_share.map(|t| t as usize))
9003    }
9004
9005    /// E4B prologue: inp_pl [t][n_layer][n_epl] =
9006    ///   ( gather(per_layer_tok_embd, tok)*sqrt(n_epl)
9007    ///   + rms_norm(model_proj . x_scaled * 1/sqrt(n_embd), proj_norm) ) * 1/sqrt(2)
9008    /// (llama gemma4.cpp build_inp_per_layer + project_per_layer_inputs, exact order).
9009    fn gemma4_e4b_inp_pl(&self, e: &Engine, tokens: &[u32], x_scaled: &CudaSlice<f32>, t: usize)
9010                         -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
9011        let tok_d = e.stream().clone_htod(&tokens.to_vec())?;
9012        self.gemma4_e4b_inp_pl_dev(e, &tok_d, x_scaled, t)
9013    }
9014
9015    /// Device-token prologue core (dc arm shares it: token ids never touch the host).
9016    fn gemma4_e4b_inp_pl_dev(&self, e: &Engine, tok_d: &CudaSlice<u32>,
9017                             x_scaled: &CudaSlice<f32>, t: usize)
9018                             -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
9019        let aux = self.gemma4_aux.as_ref().unwrap();
9020        let m = aux.e4b.as_ref().unwrap();
9021        let n_embd = self.cfg.n_embd as usize;
9022        let n_layer = self.layers.len();
9023        let width = m.n_epl * n_layer;
9024        let tbl = m.tok_tbl_gpu.get_or_init(|| {
9025            e.upload_u8(&m.tok_embd_bytes).expect("e4b per-layer token table upload")
9026        });
9027        let mut a = e.embed_gather_device_td(tbl, tok_d, t, width, m.tok_embd_qt,
9028                                             m.tok_embd_row_bytes)?;
9029        e.scale_inplace(&mut a, (m.n_epl as f32).sqrt(), t * width)?;
9030        let mut p = e.matmul(&m.model_proj, x_scaled, t)?;
9031        e.scale_inplace(&mut p, 1.0 / (n_embd as f32).sqrt(), t * width)?;
9032        let mut pn = e.uninit(t * width)?;
9033        e.rms_norm(&p, m.proj_norm.float_data(), &mut pn, m.n_epl, t * n_layer,
9034                   self.cfg.rms_eps)?;
9035        let mut out = e.uninit(t * width)?;
9036        e.add_scale(&a, &pn, 1.0 / 2f32.sqrt(), &mut out, t * width)?;
9037        Ok(out)
9038    }
9039
9040    /// E4B attention (t-wide, causal, per-row fa over the QUANTIZED cache — first-light
9041    /// correctness path; the fa rows arms come later). Own-KV layers project+norm+rope k/v
9042    /// and append t rows; KV-shared layers are Q-only over the target layer's cache (which
9043    /// already holds this forward's rows — the target runs earlier in the stack).
9044    #[allow(clippy::too_many_arguments)]
9045    fn gemma4_e4b_attn(&self, e: &Engine, il: usize,
9046                       hq: &CudaSlice<i8>, hdq: &CudaSlice<f32>,
9047                       pos_d: &CudaSlice<i32>, t: usize, cache: &mut Cache,
9048                       dc_bucket: Option<usize>)
9049                       -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
9050        let (hd, nkv, nh, base, scale, swa) = self.gemma4_e4b_geom(il);
9051        let eps = self.cfg.rms_eps;
9052        let aux = self.gemma4_aux.as_ref().unwrap();
9053        let Mixer::Full(fa) = &self.layers[il].mixer else { unreachable!() };
9054        // pre-quantized layer input (E4B fusion port, 2026-07-12): ONE norm+quant feeds
9055        // wq/wk/wv via matmul_pre — the first-light path quantized the same h three times
9056        // (the profile's 342 quantize_q8_1/token; glue = 26% of the 6.1ms token).
9057        let h0 = e.zeros(0)?;
9058        let h = &h0;
9059
9060        let ff = if swa { None } else {
9061            Some(aux.rope_freqs.as_ref().expect("e4b global rope needs rope_freqs.weight"))
9062        };
9063        let share = self.gemma4_e4b_kv_target(il);
9064        // Own-KV arms keep the f32 (k, v) alive for the prime fa arm below.
9065        let mut kv_f32: Option<(CudaSlice<f32>, CudaSlice<f32>)> = None;
9066        let mut q;
9067        if let Some(_tgt) = share {
9068            let q0 = e.matmul_pre(&fa.wq, hq, hdq, h, t)?;
9069            q = e.uninit(t * nh * hd)?;
9070            // Q-only through the same fused norm+rope kernel (rk = 0: the k/v segments are
9071            // empty; q0 stands in for the unused k/v pointers).
9072            let mut kdummy = e.uninit(1)?;
9073            let mut vdummy = e.uninit(1)?;
9074            e.rms_norm_qkv_rope(&q0, &q0, &q0, fa.q_norm.float_data(),
9075                                fa.q_norm.float_data(), &aux.ones,
9076                                &mut q, &mut kdummy, &mut vdummy, hd, nh * t, 0,
9077                                pos_d, nh, 1, base, 1.0, ff, eps)?;
9078        } else {
9079            // wave-4b: ONE concat matvec (wq|wk|wv) at t == 1 when the cat tensor exists;
9080            // else the fused3 grid launch; else per-matvec. The cat output is contiguous
9081            // q|k|v rows — the cat norm+rope twin consumes it directly.
9082            let e4bits = self.layers[il].gemma4.as_ref().and_then(|g| g.e4b.as_ref());
9083            let cat = e4bits.and_then(|e4| e4.qkv_cat.as_ref());
9084            q = e.uninit(t * nh * hd)?;
9085            let mut k = e.uninit(t * nkv * hd)?;
9086            let mut v = e.uninit(t * nkv * hd)?;
9087            if t == 1 && cat.is_some() {
9088                let qkv0 = e.matmul_pre(cat.unwrap(), hq, hdq, h, 1)?;
9089                e.rms_norm_qkv_rope_cat(&qkv0, fa.q_norm.float_data(), fa.k_norm.float_data(),
9090                                        &aux.ones, &mut q, &mut k, &mut v, hd, nh, nkv,
9091                                        pos_d, nh, nkv, base, 1.0, ff, eps)?;
9092            } else {
9093                let (q0, k0, v0) = match if t == 1 {
9094                    e.matmul_q4_fused3(&fa.wq, &fa.wk, &fa.wv, hq, hdq)?
9095                } else {
9096                    // E4B verify f3 port (2026-07-14): the 31B segmented-grid batched qkv
9097                    // on E4B's real-V triple — same MEMRA_F2B seam, bit-identical per row.
9098                    static F2B_QKV: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
9099                    if *F2B_QKV.get_or_init(|| std::env::var("MEMRA_F2B").as_deref() != Ok("0")) {
9100                        e.matmul_q4_fused3_batched(&fa.wq, &fa.wk, &fa.wv, hq, hdq, t)?
9101                    } else { None }
9102                } {
9103                    Some(triple) => triple,
9104                    None => (e.matmul_pre(&fa.wq, hq, hdq, h, t)?,
9105                             e.matmul_pre(&fa.wk, hq, hdq, h, t)?,
9106                             e.matmul_pre(&fa.wv, hq, hdq, h, t)?),   // E4B: real v (K != V)
9107                };
9108                // wave-3 fold: q/k/v norms + q/k rope in ONE launch (rope math verbatim on
9109                // the normed rows; V ones-rms, never roped).
9110                e.rms_norm_qkv_rope(&q0, &k0, &v0, fa.q_norm.float_data(),
9111                                    fa.k_norm.float_data(), &aux.ones, &mut q, &mut k, &mut v,
9112                                    hd, nh * t, nkv * t, pos_d, nh, nkv, base, 1.0, ff, eps)?;
9113            }
9114            let kvl = cache.kv[il].as_mut().unwrap();
9115            // class flag must match the cache dims (the g-threading sweep hardcoded `false`
9116            // here and the wkv default corrupted E4B: q8_0 bytes into an e4m3 cache — the
9117            // degenerate tok-0 stream, 2026-07-12).
9118            let cls = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
9119            if dc_bucket.is_some() {
9120                // DC arm (graph serving): append at the len_d slot, advance the counter
9121                // in-stream — replay-correct, no host len in the launch args. Host mirrors
9122                // are NOT touched here (the replay loop owns them; a bump at capture-record
9123                // time would double-count the capture iteration).
9124                debug_assert!(t == 1);
9125                // wave 5c: append + len_d inc fused (one launch; single-block ordering).
9126                e.append_kv_quantized_row_dc_inc(&k, &v, &mut kvl.k, &mut kvl.v,
9127                                                 &mut kvl.len_d, kvl.kv_dim_k, kvl.kv_dim_v,
9128                                                 kvl.k_tok_bytes, kvl.v_tok_bytes, cls)?;
9129            } else {
9130                e.append_kv_quantized_rows(&k, &v, &mut kvl.k, &mut kvl.v, kvl.len, t,
9131                                           kvl.kv_dim_k, kvl.kv_dim_v, kvl.k_tok_bytes,
9132                                           kvl.v_tok_bytes, cls)?;
9133                kvl.len += t;
9134            }
9135            kv_f32 = Some((k, v));
9136        }
9137        // attention: per-row causal fa over the (own or target) quantized cache. The cache
9138        // already contains this forward's rows in both arms; row i attends [.., base+i].
9139        let kvl_idx = share.unwrap_or(il);
9140        let kvl = cache.kv[kvl_idx].as_ref().unwrap();
9141        let base_len = kvl.len - t;   // pre-append length (target appended this forward too)
9142        let win = self.cfg.gemma4.as_ref().unwrap().sliding_window as usize;
9143        let mut attn = e.uninit(t * nh * hd)?;
9144        // PRIME FA ARMS (perf lane 3, completed 2026-07-31 — the H100 board pinned E4B
9145        // prefill at 482 tok/s: the per-row loop ran the DECODE kernel once per token per
9146        // layer). Fresh-prompt (base_len == 0) batched attention, one launch per layer:
9147        //   - own-KV hd256 under the window: f32 fa_prefill (full causal exact — 26B pattern)
9148        //   - own-KV hd256 above the window (swa): fa_prefill_w windowed twin (12B pattern)
9149        //   - own-KV hd512 globals: fa_prefill_hd512 (12B/31B globals pattern)
9150        //   - KV-shared layers (no f32 k/v): fa_prefill_view over the target's QUANTIZED
9151        //     rows (the T=K verify kernel; the target appended this forward's rows already).
9152        //     swa-shared above the window keeps the per-row loop (no windowed view twin).
9153        // Same numeric class split as the 26B prime (f32/batched prefill vs per-row
9154        // quantized decode); run-gen argmax + chat gates arbitrate. MEMRA_NOFA=1 reverts all.
9155        if t > 1 && base_len == 0 && std::env::var("MEMRA_NOFA").is_err() {
9156            if let Some((kf, vf)) = &kv_f32 {
9157                if hd == 256 && t <= win {
9158                    e.fa_prefill(&q, kf, vf, &mut attn, hd, nh, nkv, t, t, scale, true)?;
9159                    return Ok(e.matmul(&fa.wo, &attn, t)?);
9160                }
9161                if hd == 256 && swa && t > win {
9162                    e.fa_prefill_w(&q, kf, vf, &mut attn, hd, nh, nkv, t, t, scale, true,
9163                                   win)?;
9164                    return Ok(e.matmul(&fa.wo, &attn, t)?);
9165                }
9166                if hd == 512 && !swa {
9167                    e.fa_prefill_hd512(&q, kf, vf, &mut attn, hd, nh, nkv, t, t, scale,
9168                                       true)?;
9169                    return Ok(e.matmul(&fa.wo, &attn, t)?);
9170                }
9171            } else if share.is_some() {
9172                let g = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
9173                let k_view = e.view_u8(&kvl.k, kvl.k.len());
9174                let v_view = e.view_u8(&kvl.v, kvl.v.len());
9175                if hd == 256 && (!swa || t <= win) {
9176                    // inline-dequant quantized-view prefill (fa_prefill_q stamps 256/128)
9177                    e.fa_prefill_view(&q, &k_view, &v_view, &mut attn, hd, nh, nkv, t, t,
9178                                      scale, true, kvl.k_tok_bytes, kvl.v_tok_bytes, g)?;
9179                    return Ok(e.matmul(&fa.wo, &attn, t)?);
9180                }
9181                // remaining shared classes (swa above the window; hd512 globals): dequant
9182                // the target's t rows ONCE to f32, then the same f32 twins as own-KV.
9183                let kv_dim = nkv * hd;
9184                let mut kf = e.uninit(t * kv_dim)?;
9185                let mut vf = e.uninit(t * kv_dim)?;
9186                e.fa_dequant_kv_view_f32(&k_view, &v_view, &mut kf, &mut vf, kv_dim, kv_dim,
9187                                         t, kvl.k_tok_bytes, kvl.v_tok_bytes, g)?;
9188                if hd == 512 {
9189                    e.fa_prefill_hd512(&q, &kf, &vf, &mut attn, hd, nh, nkv, t, t, scale,
9190                                       true)?;
9191                } else {
9192                    e.fa_prefill_w(&q, &kf, &vf, &mut attn, hd, nh, nkv, t, t, scale, true,
9193                                   win)?;
9194                }
9195                return Ok(e.matmul(&fa.wo, &attn, t)?);
9196            }
9197        }
9198        if let Some(bucket) = dc_bucket {
9199            // DC arm (t == 1, under-window regime — the generate gate enforces it): ONE
9200            // fa_decode_dc over the live counter. len_d already advanced past this token
9201            // (t_kv = len_d[0], the cache.rs contract). KV-shared layers read the target's
9202            // counter (advanced when the target ran earlier in the stack).
9203            assert!(t == 1);
9204            // hd512 globals: eager (kvmod) picks the SCALAR unified below the fa512 floor,
9205            // and under the window every live t_kv sits below it — cap the capture bucket
9206            // under the floor so fa_decode_dc bakes the same scalar symbol, or the graph
9207            // rides the dpl16 twin and its numeric class diverges from the dc-eager stream
9208            // (E4B-GRAPH-GATE 2/64, 2026-07-12).
9209            let bucket = if hd == 512 && win <= crate::fa512_min_tkv() {
9210                bucket.min(crate::fa512_min_tkv().saturating_sub(1))
9211            } else { bucket };
9212            let k_view = e.view_u8(&kvl.k, kvl.k.len());
9213            let v_view = e.view_u8(&kvl.v, kvl.v.len());
9214            let g = (!swa && crate::Engine::gkv_on()) || (swa && crate::Engine::wkv_on());
9215            // Weight prefetch (SOTA item 3, 2026-07-13): wo's decode plane prefetched into
9216            // L2 across the fa window — fa reads KV only, the weight DRAM lanes are idle
9217            // there (E4B valid window +0.65%: 196.8 vs 195.6). Value-free scheduling op,
9218            // captured into the dc graph like any other launch. Extending the cascade to
9219            // the ffn gate/up planes measured NEGATIVE (193.9 vs 195.8 — 29MB/layer floods
9220            // the fill path and evicts still-hot lines); wo-only is the shipped shape.
9221            // MEMRA_WPF=0 rollback seam.
9222            if crate::Engine::wpf_level() >= 1 {
9223                e.prefetch_weight_l2(&fa.wo)?;
9224            }
9225            // wave 5b: the combine emits the wo input q8 pair directly — the standalone
9226            // quantize launch + the f32 attn round-trip fold away (t=1 fast path only).
9227            if e.uses_q8_1_fast(&fa.wo) {
9228                let mut oq = e.alloc_i8_uninit(nh * hd)?;
9229                let mut od = e.zeros(nh * hd / 32)?;
9230                e.fa_decode_dc_q8(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
9231                                  &kvl.len_d, bucket, scale,
9232                                  kvl.k_tok_bytes, kvl.v_tok_bytes, g,
9233                                  Some((&mut oq, &mut od)))?;
9234                return Ok(e.matmul_pre(&fa.wo, &oq, &od, &attn, t)?);
9235            }
9236            e.fa_decode_dc(&q, &k_view, &v_view, &mut attn, hd, nh, nkv,
9237                           &kvl.len_d, bucket, scale,
9238                           kvl.k_tok_bytes, kvl.v_tok_bytes, g)?;
9239            return Ok(e.matmul(&fa.wo, &attn, t)?);
9240        }
9241        for i in 0..t {
9242            let avail = base_len + i + 1;
9243            let (off_tok, t_kv) = if swa && avail > win { (avail - win, win) } else { (0, avail) };
9244            let k_view = e.view_u8_range(&kvl.k, off_tok * kvl.k_tok_bytes,
9245                                         (off_tok + t_kv) * kvl.k_tok_bytes);
9246            let v_view = e.view_u8_range(&kvl.v, off_tok * kvl.v_tok_bytes,
9247                                         (off_tok + t_kv) * kvl.v_tok_bytes);
9248            let qv = e.view(&q, t * nh * hd);
9249            let q_row = qv.slice(i * nh * hd..(i + 1) * nh * hd);
9250            let mut q_one = e.uninit(nh * hd)?;
9251            e.copy_view_into(&mut q_one, 0, &q_row, nh * hd)?;
9252            let mut a_one = e.uninit(nh * hd)?;
9253            // read class MUST match the append class (globals are e4m3 under gkv): the
9254            // swa-only flag decoded global-layer e4m3 bytes as q8_0/q5_1 — attention over
9255            // garbage on EVERY E4B global layer, the cross-mode maxdiff-30 root (2026-07-12).
9256            e.fa_decode_kvmod(&q_one, &k_view, &v_view, &mut a_one, hd, nh, nkv, t_kv, scale,
9257                        kvl.k_tok_bytes, kvl.v_tok_bytes,
9258                        (!swa && crate::Engine::gkv_on())
9259                            || (swa && crate::Engine::wkv_on()))?;
9260            e.copy_into(&mut attn, i * nh * hd, &a_one, nh * hd)?;
9261        }
9262        Ok(e.matmul(&fa.wo, &attn, t)?)
9263    }
9264
9265    /// E4B trunk: embed -> prologue -> layers (attn + dense ffn via the 31B tail_core + the
9266    /// per-layer-embedding tail + layer scale) -> output_norm. Returns (softcapped logits
9267    /// device [t, n_vocab], pre-output_norm hidden [t, n_embd]). Appends t rows per own-KV
9268    /// layer; does NOT advance cache.pos (caller owns pos).
9269    fn gemma4_e4b_trunk(&self, e: &Engine, tokens: &[u32], pos0: usize, cache: &mut Cache,
9270                        head_last: bool)
9271                        -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
9272        let n_embd = self.cfg.n_embd as usize;
9273        let t = tokens.len();
9274        let pos: Vec<i32> = (0..t).map(|i| (pos0 + i) as i32).collect();
9275        let pos_d = e.htod_i32(&pos)?;
9276        let mut x = e.htod(&self.embd.gather(n_embd, tokens))?;
9277        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
9278        let inp_pl = self.gemma4_e4b_inp_pl(e, tokens, &x, t)?;
9279        self.gemma4_e4b_trunk_core(e, x, inp_pl, &pos_d, t, cache, None, true, head_last)
9280    }
9281
9282    /// Layer stack + head over prebuilt (x_scaled, inp_pl, device pos) — everything below
9283    /// here is device-driven, so the dc arm shares it verbatim (stream identity with the
9284    /// eager chain by construction: SAME functions, not twins).
9285    fn gemma4_e4b_trunk_core(&self, e: &Engine, x_in: CudaSlice<f32>, inp_pl: CudaSlice<f32>,
9286                             pos_d: &CudaSlice<i32>, t: usize, cache: &mut Cache,
9287                             dc_bucket: Option<usize>, cap_logits: bool, head_last: bool)
9288                             -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
9289        let n_embd = self.cfg.n_embd as usize;
9290        let eps = self.cfg.rms_eps;
9291        let n_layer = self.layers.len();
9292        let mut x = x_in;
9293        let aux_e4b = self.gemma4_aux.as_ref().unwrap().e4b.as_ref().unwrap();
9294        let n_epl = aux_e4b.n_epl;
9295
9296        // cross-layer fusion (2026-07-12 port of the 26B/31B trunk structure): each layer's
9297        // closing add_scale also EMITS the next layer's attn-normed input pre-quantized
9298        // q8_1 (add_scale_rms_norm_q8_1); the LAST layer emits through output_norm so the
9299        // head rides matmul_pre too. First layer's pair comes from a standalone fused
9300        // norm+quant.
9301        let mut h_carry: Option<(CudaSlice<i8>, CudaSlice<f32>)> = None;
9302        for il in 0..n_layer {
9303            let layer = &self.layers[il];
9304            let (hq, hdq) = match h_carry.take() {
9305                Some(p) => p,
9306                None => e.rms_norm_q8_1(&x, layer.attn_norm.float_data(), n_embd, t, eps)?,
9307            };
9308            let o = self.gemma4_e4b_attn(e, il, &hq, &hdq, pos_d, t, cache, dc_bucket)?;
9309            // dense ffn tail with the post-attn norm FOLDED into its entry (one launch for
9310            // rms(o, post_attn_norm) + residual add + ffn_norm — glue-fusion lane).
9311            let bits = layer.gemma4.as_ref().unwrap();
9312            let e4b = bits.e4b.as_ref().expect("e4b layer bits");
9313            // glue wave 5: the tail DEFERS its post_ffw norm — the FFN exit fuses
9314            // rms(f0, post_ffw) + residual add + q8 emit into ONE launch (rms_pre_add_q8_1),
9315            // killing the rms_norm + add_q8_1 pair per layer. T-GENERIC since 2026-07-13:
9316            // the fused single-phase reduction is NOT FP-order-identical to the unfused
9317            // rms_norm+add pair (the original "bit-identical chain" claim was FALSE — the
9318            // t==1 gate left the batched VERIFY on the unfused chain and split E4B verify
9319            // from decode by logit maxdiff ~0.45, the greedy tie-flip at depth: gate 135/256.
9320            // NOFUSE bisect: disabling ONLY this fusion -> verify maxdiff 0.000e0). With the
9321            // gate dropped, decode AND verify ride the same fused chain — parity by
9322            // construction, VERIFY-GATE 0.000e0.
9323            let fuse_exit = e.uses_q8_1_fast(&e4b.inp_gate);
9324            let (sn, attn_out) = self.gemma4_layer_tail_core_pn(
9325                e, layer, &o, &x, t, Some(layer.post_attn_norm.float_data()), fuse_exit)?;
9326            let mut resid = e.uninit(t * n_embd)?;
9327            // per-layer-embedding tail: resid += rms(proj . (gelu(inp_gate . resid) * inp_pl[il]))
9328            // wave-2: the residual add emits q8_1 alongside — inp_gate rides matmul_pre.
9329            // (PLE one-block mega-fusion PROBED NEGATIVE 2026-07-13: argmax-correct but
9330            // 126 vs 189 tok/s — one SM pulling 0.74MB of weights loses to the multi-block
9331            // launch chain it replaced; jsonl row. Kernel deleted per doctrine.)
9332            let g = if fuse_exit {
9333                // sn here = RAW f0 (post_ffw deferred).
9334                let (rq, rd) = e.rms_pre_add_q8_1(&sn, bits.post_ffw_norm.float_data(),
9335                                                  &attn_out, &mut resid, n_embd, t,
9336                                                  self.cfg.rms_eps)?;
9337                e.matmul_pre(&e4b.inp_gate, &rq, &rd, &resid, t)?
9338            } else {
9339                e.add(&sn, &attn_out, &mut resid, t * n_embd)?;
9340                e.matmul(&e4b.inp_gate, &resid, t)?
9341            };
9342            let mut act = e.uninit(t * n_epl)?;
9343            let y = if t == 1 && e.uses_q8_1_fast(&e4b.proj) {
9344                let ipv = e.view(&inp_pl, n_epl * n_layer);
9345                let row = ipv.slice(il * n_epl..(il + 1) * n_epl);
9346                let (aq, ad) = e.gelu_tanh_mul_q8_1(&g, &row, &mut act, n_epl, 1)?;
9347                e.matmul_pre(&e4b.proj, &aq, &ad, &act, t)?
9348            } else {
9349                let mut inp_this = e.uninit(t * n_epl)?;
9350                e.copy_rows_strided(&inp_pl, &mut inp_this, n_epl, t, n_epl * n_layer,
9351                                    il * n_epl)?;
9352                e.gelu_tanh_mul(&g, &inp_this, &mut act, t * n_epl)?;
9353                e.matmul(&e4b.proj, &act, t)?
9354            };
9355            // rms(y, post_norm) + (yn + resid)*layer_scale + next-layer norm+quant emit,
9356            // ONE launch (glue-fusion lane; last layer emits through output_norm).
9357            let next_norm = if il + 1 < n_layer {
9358                self.layers[il + 1].attn_norm.float_data()
9359            } else {
9360                self.output_norm.float_data()
9361            };
9362            let mut xn = e.uninit(t * n_embd)?;
9363            let pair = e.rms_pre_add_scale_rms_norm_q8_1(&y, e4b.post_norm.float_data(),
9364                                                         &resid, bits.layer_scale, next_norm,
9365                                                         &mut xn, n_embd, t, eps)?;
9366            h_carry = Some(pair);
9367            x = xn;
9368        }
9369        // the head consumes the last layer's fused (output_norm) emit. head_last callers
9370        // (prime, last_only forward) need only the final row's logits — the all-T head is
9371        // t*n_vocab of discarded work (E4B prime: ~134ms GEMM + a 2.26GB dtoh kept 1 row).
9372        let (oq, odq) = h_carry.take().unwrap();
9373        let h0 = e.zeros(0)?;
9374        let hm = if head_last { 1 } else { t };
9375        let (hq, hd) = if head_last && t > 1 {
9376            let mut q1 = e.uninit_i8(n_embd)?;
9377            e.dtod_copy_view_i8(&oq.slice((t - 1) * n_embd..t * n_embd), &mut q1)?;
9378            let nb = n_embd / 32;
9379            let mut d1 = e.uninit(nb)?;
9380            e.dtod_copy_view(&odq.slice((t - 1) * nb..t * nb), &mut d1)?;
9381            (q1, d1)
9382        } else {
9383            (oq, odq)
9384        };
9385        let mut ld = e.matmul_pre(&self.output, &hq, &hd, &h0, hm)?;
9386        // softcap is strictly monotonic — greedy (argmax-only) consumers skip it, matching
9387        // the 26B/31B dc precedent (their dc head goes matmul -> argmax with no cap).
9388        // Logit-returning callers (host logits / spec prime) keep the capped emit.
9389        if cap_logits {
9390            let cap = self.cfg.gemma4.as_ref().unwrap().final_logit_softcapping;
9391            e.softcap(&mut ld, cap, hm * self.output.out_features())?;
9392        }
9393        self.gemma4_suppress(e, &mut ld, hm)?;  // mask both capped and argmax-only consumers
9394        Ok((ld, x))
9395    }
9396
9397    /// E4B batched VERIFY (device tokens, the spec round's t=K+1 step): t rows through the
9398    /// e4b trunk (per-row causal attention; own-KV layers append t rows host-len, KV-shared
9399    /// layers ride their targets), per-row device argmax + the POST-output_norm hidden
9400    /// stack (the drafter's h convention). Advances cache.pos/kvl.len by t — the spec
9401    /// round rolls back rejected rows (shared layers have no KvLayer, so the plain rewind
9402    /// covers exactly the layers that appended).
9403    pub fn gemma4_e4b_decode_step_t_am_dev(&self, e: &Engine, tok_d: &CudaSlice<u32>,
9404                                                  t: usize, pos0: usize, cache: &mut Cache)
9405                                                  -> Result<(CudaSlice<u32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
9406        let n_embd = self.cfg.n_embd as usize;
9407        let eps = self.cfg.rms_eps;
9408        let pos: Vec<i32> = (0..t).map(|i| (pos0 + i) as i32).collect();
9409        let pos_d = e.htod_i32(&pos)?;
9410        let embd_gpu = self.embd_gpu.get_or_init(|| {
9411            e.upload_u8(&self.embd.raw).expect("embed table upload")
9412        });
9413        let (qt, rb) = self.embd.qt_and_row_bytes(n_embd);
9414        let mut x = e.embed_gather_device_td(embd_gpu, tok_d, t, n_embd, qt, rb)?;
9415        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), t * n_embd)?;
9416        let inp_pl = self.gemma4_e4b_inp_pl_dev(e, tok_d, &x, t)?;
9417        let (ld, xp) = self.gemma4_e4b_trunk_core(e, x, inp_pl, &pos_d, t, cache, None, true,
9418                                                  false)?;
9419        // softcap is monotonic — the per-row argmax is invariant to it (the trunk's head
9420        // emit is already capped, matching the eager chain bit-for-bit).
9421        let n_vocab = self.output.out_features();
9422        let mut vam = e.stream().alloc_zeros::<u32>(t)?;
9423        for i in 0..t {
9424            e.argmax_token_device_col(&ld, i, n_vocab, &mut vam, i)?;
9425        }
9426        let mut hn = e.uninit(t * n_embd)?;
9427        e.rms_norm(&xp, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
9428        cache.pos += t;
9429        Ok((vam, hn))
9430    }
9431
9432    /// E4B verify + host logits + POST-output_norm hidden stack (the short-prompt spec
9433    /// prime path — mirror of `gemma4_decode_step_t_h`).
9434    pub(crate) fn gemma4_e4b_decode_step_t_h(&self, e: &Engine, tokens: &[u32], pos0: usize,
9435                                             cache: &mut Cache)
9436                                             -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
9437        let n_embd = self.cfg.n_embd as usize;
9438        let eps = self.cfg.rms_eps;
9439        let t = tokens.len();
9440        let (ld, xp) = self.gemma4_e4b_trunk(e, tokens, pos0, cache, false)?;
9441        let mut hn = e.uninit(t * n_embd)?;
9442        e.rms_norm(&xp, self.output_norm.float_data(), &mut hn, n_embd, t, eps)?;
9443        cache.pos += t;
9444        Ok((e.dtoh(&ld)?, hn))
9445    }
9446
9447    /// E4B GRAPH-CAPTURABLE dc step: same trunk as the dc step but token_d is updated IN
9448    /// PLACE (self-feeding replay) and every launch arg is a device counter — pos from
9449    /// pos_d (inc'd in-stream), KV slots from len_d (advanced in-stream), attention from
9450    /// fa_decode_dc at `bucket`. Host mirrors (cache.pos / kvl.len) advance in the caller's
9451    /// replay loop. UNDER-WINDOW regime only (the caller gates pos + budget < window).
9452    pub fn gemma4_e4b_decode_step_dcg(&self, e: &Engine, token_d: &mut CudaSlice<u32>,
9453                                      pos_d: &mut CudaSlice<i32>, embd_gpu: &CudaSlice<u8>,
9454                                      embd_qt: i32, embd_rb: usize, cache: &mut Cache,
9455                                      n_vocab: usize, bucket: usize)
9456                                      -> Result<(), Box<dyn std::error::Error>> {
9457        let n_embd = self.cfg.n_embd as usize;
9458        let mut x = e.embed_gather_device(embd_gpu, token_d, n_embd, embd_qt, embd_rb)?;
9459        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
9460        let inp_pl = self.gemma4_e4b_inp_pl_dev(e, token_d, &x, 1)?;
9461        let (ld, _x) = self.gemma4_e4b_trunk_core(e, x, inp_pl, pos_d, 1, cache, Some(bucket),
9462                                                  false, false)?;
9463        e.argmax_token_device_into(&ld, token_d, n_vocab)?;
9464        e.inc_seqlen(pos_d)?;
9465        Ok(())
9466    }
9467
9468    /// E4B DEVICE-COUNTER decode step (the dc serving arm): token id rides `token_d`, the
9469    /// greedy argmax lands in the returned device buffer — 4B/token host traffic. The layer
9470    /// stack is gemma4_e4b_trunk_core, i.e. the SAME functions the eager chain runs (stream
9471    /// identity by construction, not by twin-kernel parity). Host KV mirrors advance like the
9472    /// 26B dc-eager arm (window views are host math); len_d stays synced by the caller's
9473    /// entry sync + the appends here don't read it. Graph capture is NOT wired (no
9474    /// cap_bucket_max) — the E4B graph arc comes after the perf gates.
9475    #[allow(clippy::too_many_arguments)]
9476    pub fn gemma4_e4b_decode_step_dc(&self, e: &Engine, token_d: &CudaSlice<u32>,
9477                                     pos_d: &mut CudaSlice<i32>, embd_gpu: &CudaSlice<u8>,
9478                                     embd_qt: i32, embd_rb: usize, cache: &mut Cache,
9479                                     n_vocab: usize)
9480                                     -> Result<CudaSlice<u32>, Box<dyn std::error::Error>> {
9481        let n_embd = self.cfg.n_embd as usize;
9482        let eps = self.cfg.rms_eps;
9483        let mut x = e.embed_gather_device(embd_gpu, token_d, n_embd, embd_qt, embd_rb)?;
9484        e.scale_inplace(&mut x, (n_embd as f32).sqrt(), n_embd)?;
9485        let inp_pl = self.gemma4_e4b_inp_pl_dev(e, token_d, &x, 1)?;
9486        let (ld, _x) = self.gemma4_e4b_trunk_core(e, x, inp_pl, pos_d, 1, cache, None, false,
9487                                                  false)?;
9488        let mut tok_out = e.stream().alloc_zeros::<u32>(1)?;
9489        e.argmax_token_device_into(&ld, &mut tok_out, n_vocab)?;
9490        e.inc_seqlen(pos_d)?;
9491        cache.pos += 1;
9492        let _ = eps;
9493        Ok(tok_out)
9494    }
9495
9496    /// E4B eager T=1 decode step (decode_step_h contract): returns (softcapped logits host,
9497    /// pre-output_norm hidden). Advances cache.pos.
9498    pub(crate) fn gemma4_e4b_decode_step_h(&self, e: &Engine, token: u32, cache: &mut Cache)
9499                                           -> Result<(Vec<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
9500        let (ld, x) = self.gemma4_e4b_trunk(e, &[token], cache.pos, cache, false)?;
9501        let logits = e.dtoh(&ld)?;
9502        cache.pos += 1;
9503        Ok((logits, x))
9504    }
9505
9506    /// E4B batched prime (prime_cache contract: (last-row logits host, h_seed pre-norm last
9507    /// row, hidden stack)). First-light: the t-wide trunk (per-row attention) — correct, not
9508    /// fast; the prefill fa arms come later.
9509    pub(crate) fn gemma4_e4b_prime(&self, e: &Engine, tokens: &[u32], cache: &mut Cache)
9510                                   -> Result<(Vec<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
9511        // Err, not assert (2026-08-07, lane/gemma4-serve-gaps): same served-chunked-prompt
9512        // process-kill as gemma4_prime — refuse per-request.
9513        if cache.pos != 0 {
9514            return Err("e4b prime is fresh-prompt only (v0) — prime the full prompt in one \
9515                        call or decode tokenwise".into());
9516        }
9517        let n_embd = self.cfg.n_embd as usize;
9518        let t = tokens.len();
9519        let (ld, x) = self.gemma4_e4b_trunk(e, tokens, 0, cache, true)?;
9520        cache.pos += t;
9521        let last = e.dtoh(&ld)?;   // head_last: ld is already the final row only
9522        let xv = e.view(&x, t * n_embd);
9523        let row = xv.slice((t - 1) * n_embd..t * n_embd);
9524        let mut h_seed = e.uninit(n_embd)?;
9525        e.copy_view_into(&mut h_seed, 0, &row, n_embd)?;
9526        Ok((last, h_seed, x))
9527    }
9528
9529    /// E4B prefill logits (forward contract — no persistent cache; scratch cache internally).
9530    pub(crate) fn gemma4_e4b_forward(&self, e: &Engine, tokens: &[u32], last_only: bool)
9531                                     -> Result<Vec<f32>, Box<dyn std::error::Error>> {
9532        let mut cache = Cache::new(e, &self.cfg, tokens.len() + 8)?;
9533        let (ld, _x) = self.gemma4_e4b_trunk(e, tokens, 0, &mut cache, last_only)?;
9534        Ok(e.dtoh(&ld)?)   // head_last already reduced to the final row when last_only
9535    }
9536}
9537
9538#[cfg(test)]
9539mod prime_chunk_schedule_tests {
9540    use super::{
9541        dynamic_prime_chunk_ranges, fixed_prime_chunk_ranges, fixed_prime_chunk_ranges_for_ring,
9542        PRIME_MIN_T,
9543        PRIME_PIPE_MIN_CHUNK,
9544    };
9545
9546    fn sizes(ranges: &[(usize, usize)]) -> Vec<usize> {
9547        ranges.iter().map(|(start, end)| end - start).collect()
9548    }
9549
9550    fn auto_chunk(t: usize) -> usize {
9551        t.div_ceil(8).max(PRIME_PIPE_MIN_CHUNK).min(4096)
9552    }
9553
9554    #[test]
9555    fn fixed_schedule_retains_measured_geometry() {
9556        assert_eq!(
9557            sizes(&fixed_prime_chunk_ranges(461, 128)),
9558            vec![128, 128, 128, 77]
9559        );
9560        assert_eq!(
9561            sizes(&fixed_prime_chunk_ranges(1833, 230)),
9562            vec![230, 230, 230, 230, 230, 230, 230, 223]
9563        );
9564        assert_eq!(
9565            sizes(&fixed_prime_chunk_ranges(4096, 512)),
9566            vec![512; 8]
9567        );
9568        let capped = sizes(&fixed_prime_chunk_ranges_for_ring(8200, 4096, true));
9569        assert_eq!(capped, vec![4096, 4088, 16]);
9570        assert!(capped.iter().all(|&rows| rows <= 4096));
9571        assert_eq!(
9572            sizes(&fixed_prime_chunk_ranges_for_ring(4100, 4096, false)),
9573            vec![4100],
9574            "flag-off schedule remains byte-for-byte the legacy monolithic tail",
9575        );
9576    }
9577
9578    #[test]
9579    fn dynamic_schedule_matches_registered_shapes() {
9580        let cases = [
9581            (461, vec![64, 141, 132, 124]),
9582            (1833, vec![115, 269, 260, 252, 244, 237, 231, 225]),
9583            (4096, vec![256, 602, 580, 563, 545, 531, 516, 503]),
9584        ];
9585        for (t, expected) in cases {
9586            let chunk = auto_chunk(t);
9587            let fixed = fixed_prime_chunk_ranges(t, chunk);
9588            assert_eq!(
9589                sizes(&dynamic_prime_chunk_ranges(t, chunk, &fixed)),
9590                expected
9591            );
9592        }
9593    }
9594
9595    #[test]
9596    fn dynamic_schedule_covers_exactly_and_shrinks_after_fill() {
9597        for t in 256..=8192 {
9598            let chunk = auto_chunk(t);
9599            let fixed = fixed_prime_chunk_ranges(t, chunk);
9600            let dynamic = dynamic_prime_chunk_ranges(t, chunk, &fixed);
9601            assert_eq!(dynamic.len(), fixed.len(), "T={t}");
9602            assert_eq!(dynamic.first().unwrap().0, 0, "T={t}");
9603            assert_eq!(dynamic.last().unwrap().1, t, "T={t}");
9604            for pair in dynamic.windows(2) {
9605                assert_eq!(pair[0].1, pair[1].0, "T={t}");
9606            }
9607            assert!(
9608                dynamic
9609                    .iter()
9610                    .all(|(start, end)| end - start >= PRIME_MIN_T),
9611                "T={t} sizes={:?}",
9612                sizes(&dynamic)
9613            );
9614            if dynamic.len() >= 3 {
9615                let chunk_sizes = sizes(&dynamic);
9616                assert!(
9617                    chunk_sizes[0] < chunk_sizes[1],
9618                    "T={t} sizes={chunk_sizes:?}"
9619                );
9620                assert!(
9621                    chunk_sizes[1..].windows(2).all(|pair| pair[0] >= pair[1]),
9622                    "T={t} sizes={chunk_sizes:?}"
9623                );
9624            }
9625        }
9626    }
9627}
9628
9629#[cfg(test)]
9630mod page_prefetch_tests {
9631    use super::{
9632        grouped_worker_prefetch_position, page_prefetch_positions,
9633        page_prefetch_window_from_values, worker_prefetch_positions,
9634    };
9635
9636    #[test]
9637    fn page_prefetch_window_keeps_existing_opt_in_default() {
9638        assert_eq!(page_prefetch_window_from_values(false, None), 0);
9639        assert_eq!(page_prefetch_window_from_values(false, Some("8")), 0);
9640        assert_eq!(page_prefetch_window_from_values(true, None), 1);
9641        assert_eq!(page_prefetch_window_from_values(true, Some("bad")), 1);
9642        assert_eq!(page_prefetch_window_from_values(true, Some("0")), 0);
9643        assert_eq!(page_prefetch_window_from_values(true, Some("8")), 8);
9644    }
9645
9646    #[test]
9647    fn rolling_page_prefetch_advises_each_future_expert_once() {
9648        let advised: Vec<_> = (0..7)
9649            .flat_map(|position| page_prefetch_positions(position, 7, 3))
9650            .collect();
9651        assert_eq!(advised, vec![1, 2, 3, 4, 5, 6]);
9652
9653        let one_ahead: Vec<_> = (0..4)
9654            .flat_map(|position| page_prefetch_positions(position, 4, 1))
9655            .collect();
9656        assert_eq!(one_ahead, vec![1, 2, 3]);
9657        assert!(page_prefetch_positions(0, 4, 0).is_empty());
9658    }
9659
9660    #[test]
9661    fn grouped_worker_prefetch_primes_first_then_each_known_next_once() {
9662        assert_eq!(grouped_worker_prefetch_position(0, None), None);
9663        let positions: Vec<_> = std::iter::once(grouped_worker_prefetch_position(4, None).unwrap())
9664            .chain((0..4).filter_map(|position| {
9665                grouped_worker_prefetch_position(4, Some(position))
9666            }))
9667            .collect();
9668        assert_eq!(positions, vec![0, 1, 2, 3]);
9669        assert_eq!(grouped_worker_prefetch_position(1, Some(0)), None);
9670    }
9671
9672    #[test]
9673    fn rolling_worker_prefetch_primes_current_and_each_future_expert_once() {
9674        let queued: Vec<_> = (0..8)
9675            .flat_map(|position| worker_prefetch_positions(position, 8, 5))
9676            .collect();
9677        assert_eq!(queued, (0..8).collect::<Vec<_>>());
9678
9679        let one_at_a_time: Vec<_> = (0..4)
9680            .flat_map(|position| worker_prefetch_positions(position, 4, 1))
9681            .collect();
9682        assert_eq!(one_at_a_time, vec![0, 1, 2, 3]);
9683        assert!(worker_prefetch_positions(0, 4, 0).is_empty());
9684    }
9685}
9686
9687pub struct G4DcSlots {
9688    x: CudaSlice<f32>, xn: CudaSlice<f32>, cur: CudaSlice<f32>,
9689    hq: CudaSlice<i8>, hd_: CudaSlice<f32>,
9690    q0: CudaSlice<f32>, k0: CudaSlice<f32>, v0: CudaSlice<f32>,
9691    q: CudaSlice<f32>, k: CudaSlice<f32>, v: CudaSlice<f32>,
9692    attn: CudaSlice<f32>, o: CudaSlice<f32>,
9693    attn_out: CudaSlice<f32>, zsh: CudaSlice<f32>,
9694    zq: CudaSlice<i8>, zd: CudaSlice<f32>,
9695    gate: CudaSlice<f32>, up: CudaSlice<f32>,
9696    act: CudaSlice<f32>, actq: CudaSlice<i8>, actd: CudaSlice<f32>,
9697    f0: CudaSlice<f32>, sn: CudaSlice<f32>,
9698    hn: CudaSlice<f32>, logits: CudaSlice<f32>,
9699}