Skip to main content

memra_engine/
lib.rs

1//! memra engine: Stage-1 correctness-first forward-pass kernels + ops, on sm_120 via cudarc.
2
3use std::sync::{Arc, Mutex};
4use cudarc::driver::{CudaContext, CudaStream, CudaModule, CudaFunction, CudaSlice, LaunchConfig, PushKernelArg};
5use cudarc::nvrtc::Ptx;
6
7#[cfg(debug_assertions)]
8pub(crate) fn debug_assert_tensor_stream_device<T>(
9    tensor: &CudaSlice<T>,
10    stream: &CudaStream,
11    site: &str,
12) {
13    let tensor_dev = tensor.ordinal();
14    let stream_dev = stream.context().ordinal();
15    assert_eq!(
16        tensor_dev, stream_dev,
17        "PP cross-device tensor read at {site}: tensor on dev{tensor_dev}, stream on dev{stream_dev}"
18    );
19}
20
21pub use memra_gguf;
22pub use memra_runtime;
23
24pub mod model;
25pub mod forward;
26pub mod hybrid;
27pub mod hybrid_forward;
28pub mod sigrouter_contract;
29/// The dual cache lives in the shared `memra-kv` crate (Phase D extraction); this
30/// re-export keeps every `crate::cache::` / `memra_engine::cache::` path unchanged.
31pub mod cache {
32    pub use memra_kv::*;
33}
34pub mod decode;
35pub mod decode_batch;
36/// MLA (multi-head latent attention) CPU f32 reference — GLM-5.2 bring-up lane increment 1.
37/// Naive vs absorbed decode forms + NORM/NEOX rope permutation, unit-tested; the permanent
38/// oracle for the MLA kernel family (`research/mla-bringup-20260801/DESIGN.md`). No CUDA deps.
39pub mod mla;
40pub mod pp;
41pub mod spec;
42pub mod gemma_spec;
43pub mod round_stream;
44pub mod graph_update;
45pub mod dflash;
46pub mod eagle;
47pub use memra_sampling as sampler;
48
49/// In-house MoE router GEMV on the spec-verify small-t path (DEFAULT ON since 2026-07-10:
50/// battery green on 35B p2/p3 K=1..8, acceptance bit-identical, +2-4% spec e2e — replaces
51/// ~240 per-column cuBLAS gemv launches/round). MEMRA_ROUTER_KERNEL=0 is the rollback seam.
52/// MoE grouped f16 GEMM door (experimental until gated), f16-mirror numeric class:
53/// per-layer expert dequant to f16 + one grouped f16 GEMM over the CSR groups.
54///   MEMRA_MOE_F16G=1  cublasGemmGroupedBatchedEx (round 46 arc 2). The grouped API issues
55///                     through cublas-internal streams NOT ordered with ours — v1 pays a full
56///                     stream sync per projection (round-47 ledgered defect).
57///   MEMRA_MOE_F16G=2  single-kernel grouped GEMM on the engine stream (round 49): ordered by
58///                     construction, zero syncs, f32 C with the act row-scale folded in.
59/// DEFAULT (2026-08-01, round 49 promotion): mode 1 on the Hopper lane — with the 41/41
60/// dequant coverage fix the q35 board-2048 prime measured 5490 (MMQ) / 8380 (mode 1,
61/// +53%) / 7990 (mode 2) x3 interleaved on the H100, argmax MATCH — the last board loss
62/// flips. The 5090 measured FLAT (858GB/s makes the dequant-workspace traffic cancel the
63/// GEMM win) — but that verdict is for expert banks the int8-MMA MMQ arm can take
64/// (IQ3_S/IQ4_XS/Q4_0). MEMRA_MOE_F16G=0 kills anywhere.
65///
66/// HOPPER RE-VERDICT (2026-08-02, lane/h100-flip-full): mode 2 with full direct coverage
67/// (Q4_K/Q6_K/IQ4_XS/IQ3_S tile loaders, lane/iq-direct-loaders) + the deep tail
68/// (lane/sk-tail-form) FLIPS past cublas mode 1 on the H100 — q35 board-2048 prime
69/// 13163.6 (mode 2, cross=32) vs 8626.5 (mode 1) vs 8073.4 (round-51 sk form), +52.6%,
70/// interleaved x5 zero overlap, argmax MATCH 30/30. The round-54 NO-FLIP (8547 vs 8112)
71/// was coverage-priced at 5.2% direct; ~100% coverage kills the workspace pass and the
72/// verdict inverts. Hopper naked default -> mode 2 (this arm); the gemma (gelu) site
73/// stays env-explicit-only via moe_f16g_gemma_on (Err => closed, unaffected by this arm).
74///
75/// MODE-2 DEFAULT (sm_120a naked, 2026-08-02, lane/f16g-default-rearb): with the direct
76/// tile loaders covering Q4_K/Q6_K/IQ4_XS/IQ3_S, the sk visitor beats the int8-MMA MMQ
77/// tiles on the IQ-bank models too (q35 board-2048 +33.9%, KAT pp512 +46.7% / pp2048
78/// +30.6% — research/iq-direct-loaders-20260802 §3-5, confirmed + full battery in
79/// research/f16g-default-rearb-20260802/), so every f16g-admitted expert layer rides
80/// mode 2 naked. Decode/verify stay on dp4a (t >= 16 floor). f16-mirror numeric class
81/// for naked q35/KAT prefill+prime — new token-sha anchors stamped in the rearb lane.
82///
83/// AUTO-KQUANT (mode 3, 2026-08-02, lane/q4k-expert-prefill): the previous sm_120a
84/// default, kept reachable via MEMRA_MOE_F16G=3. The mode-2 sk form is admitted ONLY for
85/// layers the MMA MMQ arm rejects (k-quant expert projections — Q3_K/Q4_K/Q6_K), i.e.
86/// exactly where the baseline is the per-pair moe_pairs_matvec_q8_em fallback with zero
87/// token reuse (Ornith-35B Q4_K_M board-2048 1098.2 -> 3453.7, 3.14x,
88/// research/q4k-expert-prefill-20260802/). Its "IQ banks keep their measured-faster MMQ
89/// tiles" ruling was priced BEFORE the IQ direct loaders and is refuted on the 5090 —
90/// the k-quant-only admission survives as the rollback seam, not the default.
91/// The gemma (gelu) site stays env-explicit-only (moe_f16g_gemma_on).
92pub fn moe_f16g_mode() -> u8 {
93    static M: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
94    *M.get_or_init(|| match std::env::var("MEMRA_MOE_F16G").as_deref() {
95        Ok("0") => 0,
96        Ok("2") => 2,
97        Ok("3") => 3,
98        Ok(_) => 1,
99        // Both arches independently re-arbitrated to mode 2 on 2026-08-02
100        // (5090: lane/f16g-default-rearb; H100: lane/h100-flip-full) — unset = 2 everywhere.
101        Err(_) => 2,
102    })
103}
104/// Mode-2 sk kernel form policy (round 51, lane/sk-bm128): the single-kernel grouped GEMM runs
105/// as a persistent problem-visitor over the real CSR tiles with two tile forms. Returns
106/// (shape_sel, cross) for the FFI:
107///   MEMRA_F16G_SK=0    -> (-1, _): the round-49 grid-scan kernel (rollback seam).
108///   MEMRA_F16G_SK=32   -> all groups on the 32x64x32 2-stage form (cross = i32::MAX).
109///   MEMRA_F16G_SK=128  -> all groups on the 128x64x64 3-stage form (cross = 1; groups fall
110///                         back to 32x64 in-launcher when the device/in_f can't take it).
111///   unset              -> hybrid split: groups with m_e >= MEMRA_F16G_SK_CROSS ride the 128
112///                         form. Default cross = 64 (5090 sweep 2026-08-01, receipts
113///                         research/sk-bm128-20260801/; H100 re-swept on the direct+tail
114///                         form 2026-08-02, lane/h100-flip-full: {16,32,64} ->
115///                         12868/13192/13225 — 64 wins there too, the pre-direct 32
116///                         verdict was stale).
117pub fn moe_f16g_sk_params() -> (i32, i32) {
118    static P: std::sync::OnceLock<(i32, i32)> = std::sync::OnceLock::new();
119    *P.get_or_init(|| match std::env::var("MEMRA_F16G_SK").as_deref() {
120        Ok("0") => (-1, 0),
121        Ok("32") => (0, i32::MAX),
122        Ok("128") => (0, 1),
123        _ => {
124            let cross = std::env::var("MEMRA_F16G_SK_CROSS").ok()
125                .and_then(|v| v.parse().ok()).unwrap_or(64);
126            (0, cross)
127        }
128    })
129}
130/// DIRECT-FROM-QUANT sk tile loaders (lane/kquant-tile-loaders, 2026-08-02; IQ classes added
131/// by lane/iq-direct-loaders): Q4_K/Q6_K/IQ4_XS/IQ3_S expert projections on the mode-2/3 sk
132/// visitor forms dequant their weight tiles in-register from the quant superblocks instead of
133/// running the per-(layer,projection) dequant pass into an f16 workspace (41.8% of Ornith-35B
134/// t=512 kernel time — the pp512 wall, research/q4k-expert-prefill-20260802 §5; the IQ classes
135/// are 94.8% of q35's bank bytes — the h100-sk-direct coverage pricing). Bit-identical to the
136/// workspace path by construction (kernel-check "f16g-kq-direct" gates it bitwise) — a
137/// data-movement change, not a numeric-class change. Default ON; MEMRA_F16G_DIRECT=0 reverts
138/// to the workspace path everywhere; MEMRA_F16G_DIRECT=kq keeps the k-quant loaders and
139/// reverts only the IQ classes (the iq-direct-loaders A/B seam — the pre-lane shipped config).
140pub fn moe_f16g_direct_on(qtype: i32) -> bool {
141    static M: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
142    let m = *M.get_or_init(|| match std::env::var("MEMRA_F16G_DIRECT").as_deref() {
143        Ok("0") => 0,
144        Ok("kq") => 1,
145        _ => 2,
146    });
147    match m {
148        0 => false,
149        1 => qtype == QT_Q4_K || qtype == QT_Q6_K,
150        _ => true,
151    }
152}
153/// DEEP-TAIL sk form (lane/sk-tail-form, 2026-08-02): groups below the visitor crossover ride
154/// a 32x64x64 3-STAGE cp.async tile instead of the round-51 32x64x32 2-stage — the same 32-row
155/// tile (zero extra padding), 2 k-blocks in flight instead of 1 and half the syncs per k. The
156/// H100 ncu pricing (research/sk-bm128-20260801) put the 2-stage tail at 31% of the sk GEMM
157/// stage under q35's routing skew. Bit-identical to every other sk form by construction
158/// (kernel-check "f16g-sk" gates all tail arms maxdiff==0); exists in both the workspace-f16
159/// and direct-from-quant variants. Default ON; MEMRA_F16G_TAIL=0 = rollback to the 2-stage
160/// tail. in_f % 64 != 0 falls back in-launcher.
161pub fn moe_f16g_tail_on() -> bool {
162    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
163    *ON.get_or_init(|| std::env::var("MEMRA_F16G_TAIL").as_deref() != Ok("0"))
164}
165
166/// Per-model door for the gemma-MoE (gelu) grouped path: round 49's Hopper default
167/// REGRESSED g26 board-2048 prefill -8.3% interleaved x5 on-box (def median 10380,
168/// wild 8.9k-11.7k spread; off 11317, ±0.13%) — the +6-15% probe verdict didn't
169/// survive the board workload (stale-verdict law, round 50). The silu/qwen class
170/// keeps the round-49 default (q35 +53% board-2048). Explicit MEMRA_MOE_F16G=1/2
171/// still opens this door for A/B.
172pub fn moe_f16g_gemma_on() -> bool {
173    static M: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
174    *M.get_or_init(|| !matches!(std::env::var("MEMRA_MOE_F16G").as_deref(), Ok("0") | Err(_)))
175}
176
177/// Fused act-epilogue (silu/gelu-mul + q8_1_mmq quantize in one launch) for the MoE prefill
178/// MMA arms. Byte-identical to the two-pass path (kernel-check gated) — default ON.
179/// MEMRA_MOE_FUSE_ACTQ=0 is the rollback/A-B seam.
180pub fn moe_fuse_actq_on() -> bool {
181    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
182    *ON.get_or_init(|| std::env::var("MEMRA_MOE_FUSE_ACTQ").as_deref() != Ok("0"))
183}
184
185/// PREFILL router m-invariance (lane/concat-prime-exact, 2026-08-02). The batched cuBLASLt
186/// router GEMM changes a row's logits when OTHER rows join the call (probed: first change at
187/// m=65 on the Ornith-35B router, 3.9e-3 — while the MMQ/f16 trunk GEMMs are bit-identical
188/// across m). Feeding a top-k discontinuity, that made a served request's expert selection a
189/// function of its CO-ARRIVALS under cross-request prime batching. The in-house router GEMV
190/// is m-invariant, so prefill uses it too and routing depends on a session's own tokens only.
191/// DEFAULT ON: it is the serving isolation contract, and it is the same kernel decode and spec
192/// verify already use (dispatch parity, one router kernel for every t).
193/// MEMRA_ROUTER_PREFILL_EXACT=0 reverts to the batched GEMM.
194pub fn router_prefill_exact_on() -> bool {
195    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
196    *ON.get_or_init(|| std::env::var("MEMRA_ROUTER_PREFILL_EXACT").as_deref() != Ok("0"))
197}
198
199pub fn router_kernel_on() -> bool {
200    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
201    *ON.get_or_init(|| {
202        let on = std::env::var("MEMRA_ROUTER_KERNEL").as_deref() != Ok("0");
203        if !on { eprintln!("[memra] router kernel OFF (rollback: per-column cuBLAS gemv)"); }
204        on
205    })
206}
207
208/// FAST-ROUTER batch twin (lane/fast-router, 2026-08-02). The concat-prime exactness fix
209/// (router_prefill_exact_on) routes prefill through router_gemv — m-invariant, but a
210/// per-(expert,token) GEMV program with zero operand reuse, so q35 board-2048 prefill paid
211/// -10% on the 5090. router_gemv_f32_w8_batch register-tiles (8x8 expert-x-token) the same
212/// per-row FP chains (BIT-IDENTICAL per row — kernel-check sweeps m=1..2048 on real router
213/// weights), so the t crossover below is pure perf, not a numeric config. Swept on-box
214/// (research/fast-router-20260802/crossover-router*.jsonl): plain wins t<=4, batch +7-9%
215/// at t=8, 1.9x at t=16 rising to 3.45x at t=2048 — MIN_T=8. Decode t=1 and spec verify
216/// t<8 keep the plain w8 form. MEMRA_ROUTER_BATCH=0 forces plain at every t (rollback
217/// seam, perf-only: bits are equal by the kernel-check gate).
218/// Killed arms (same sweep, JSONL is the record): the 8x16 tile lost to 8x8 at every t
219/// (128-accumulator register pressure beats the halved w-traffic), and the same-shape
220/// sigmoid_dot_rows twin (out_f=1) measured 0.62-0.89x at every prefill t
221/// (launch-latency-bound, ~7us/layer at m=2048) — both bit-identity-PASSED before dying.
222pub const ROUTER_BATCH_MIN_T: usize = 8;
223pub fn router_batch_on() -> bool {
224    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
225    *ON.get_or_init(|| std::env::var("MEMRA_ROUTER_BATCH").as_deref() != Ok("0"))
226}
227mod cpu_experts;
228pub mod moe_cache;
229pub mod spill;
230mod spill_pread;
231#[cfg(memra_cutlass)]
232pub mod cutlass_ffi;
233pub mod mmq_ffi;
234pub mod f16_ffi;
235pub mod prime_graph;
236pub mod fp8_ffi;
237
238// Fatbins are EMBEDDED (crates-release lane, 2026-08-04): build.rs still writes them to
239// OUT_DIR, but the bytes ship inside the binary via include_bytes! and load through
240// cuModuleLoadData. Distribution contract: a prebuilt or cargo-installed binary must be
241// self-contained — the old baked OUT_DIR *paths* pointed at the builder's temp dir and
242// broke every machine that wasn't the build machine. Same bytes, same module image;
243// the runtime MEMRA_GEMM_FATBIN tune-seam override below is preserved.
244const FATBIN: &[u8] = include_bytes!(env!("MEMRA_ENGINE_FATBIN"));
245const HYBRID_FATBIN: &[u8] = include_bytes!(env!("MEMRA_HYBRID_FATBIN"));
246const QMATVEC_FATBIN: &[u8] = include_bytes!(env!("MEMRA_QMATVEC_FATBIN"));
247const FLASH_FATBIN: &[u8] = include_bytes!(env!("MEMRA_FLASH_FATBIN"));
248const GEMM_FATBIN: &[u8] = include_bytes!(env!("MEMRA_GEMM_FATBIN"));
249const ROUTER_FATBIN: &[u8] = include_bytes!(env!("MEMRA_ROUTER_FATBIN"));
250/// spec_sample.cu: sampled-spec primitives (Philox Gumbel-max / softmax gather / residual sampler).
251const SAMPLE_FATBIN: &[u8] = include_bytes!(env!("MEMRA_SAMPLE_FATBIN"));
252
253/// TUNE SEAM (tools/sweep): a RUNTIME `MEMRA_GEMM_FATBIN=<path>` overrides the baked-in
254/// qmatvec_gemm.cu fatbin path (build.rs bakes the same name at COMPILE time via
255/// cargo:rustc-env — that constant is the default). Lets the sweep harness swap in a
256/// `-D`-tuned fatbin per process with NO rust rebuild. Unset at runtime => the
257/// compile-time default (zero behavior change).
258fn gemm_fatbin_bytes() -> std::borrow::Cow<'static, [u8]> {
259    assert!(!(portable_mma_gated() && std::env::var_os("MEMRA_GEMM_FATBIN").is_some()),
260            "MEMRA_GEMM_FATBIN overrides are not allowed in the portable CUDA lane");
261    match std::env::var("MEMRA_GEMM_FATBIN") {
262        Ok(path) => std::borrow::Cow::Owned(
263            std::fs::read(&path).unwrap_or_else(|e| panic!("MEMRA_GEMM_FATBIN read {path}: {e}"))),
264        Err(_) => std::borrow::Cow::Borrowed(GEMM_FATBIN),
265    }
266}
267
268/// Phase A (ARCHITECTURE-H100.md): sm_90a re-enables the portable-PTX tensor-core paths
269/// (int8 mma.m16n8k32/k16.s8, bf16 m16n8k16, ldmatrix, cp.async — all sm_80-class, native
270/// on Hopper) that the portable boot lane gates off. Dispatch guards that used to test
271/// `cfg!(memra_portable_cuda)` test this instead; sm_89 keeps the pure-portable behavior.
272/// The sm_120a/sm_100a-only MMA kinds (mxf4nvf4, kind::f8f6f4) are NOT covered — their
273/// launchers stay fail-closed stubs on 90a and their dispatch arms stay arch-gated.
274pub(crate) const fn portable_mma_gated() -> bool {
275    cfg!(memra_portable_cuda) && !cfg!(memra_hopper_mma)
276}
277
278/// The legacy quantized prefill GEMMs are tuned and validated for sm_120a; sm_90a re-admits
279/// them through the Hopper-MMA lane (int8 m16n8k32.s8 is sm_80-class PTX).  Keep the policy
280/// in a pure helper so the dispatch guard can be regression-tested without constructing an
281/// Engine or allocating a GPU tensor.
282const fn legacy_quant_gemm_allowed(portable_cuda: bool, hopper_mma: bool, no_gemm: bool) -> bool {
283    (!portable_cuda || hopper_mma) && !no_gemm
284}
285
286// ---- KV-cache format selection (kvbytes lane, 2026-07-08; default OFF = daily config) ----
287// `MEMRA_KV_K` = q8_0 (default, 34 B/32elem) | fp8 (raw e4m3, 32 B — the -6% K-bytes arm)
288// `MEMRA_KV_V` = q5_1 (default, 24 B/32elem) | q4_0 (18 B, -25% V bytes) | fp8 (32 B, +33%)
289// A non-default format is a NEW NUMERIC CONFIG: its own run-gen argmax baseline is legal,
290// but the gate battery (kernel-check, run-spec self-consistency) must pass WITHIN it and
291// the choice is explicit env, never silent. flash_attn.cu is compiled once per format pair
292// (build.rs); the kernels keep their names — Engine::new just loads the matching fatbin.
293const FLASH_FATBIN_VQ4: &[u8] = include_bytes!(env!("MEMRA_FLASH_FATBIN_VQ4"));
294const FLASH_FATBIN_VF8: &[u8] = include_bytes!(env!("MEMRA_FLASH_FATBIN_VF8"));
295const FLASH_FATBIN_KF8: &[u8] = include_bytes!(env!("MEMRA_FLASH_FATBIN_KF8"));
296const FLASH_FATBIN_KF8VQ4: &[u8] = include_bytes!(env!("MEMRA_FLASH_FATBIN_KF8VQ4"));
297const FLASH_FATBIN_KF8VF8: &[u8] = include_bytes!(env!("MEMRA_FLASH_FATBIN_KF8VF8"));
298
299/// KV format policy moved to the shared `memra-kv` crate (Phase D); re-exported so the
300/// fatbin router below and every existing `crate::kv_blk_bytes()` call site is unchanged.
301pub use memra_kv::{kv_blk_bytes, kv_cache_formats};
302
303/// The flash_attn fatbin matching the selected KV formats.
304fn flash_fatbin_bytes() -> &'static [u8] {
305    match kv_cache_formats() {
306        ("q8_0", "q5_1") => FLASH_FATBIN,
307        ("q8_0", "q4_0") => FLASH_FATBIN_VQ4,
308        ("q8_0", "fp8")  => FLASH_FATBIN_VF8,
309        ("fp8",  "q5_1") => FLASH_FATBIN_KF8,
310        ("fp8",  "q4_0") => FLASH_FATBIN_KF8VQ4,
311        ("fp8",  "fp8")  => FLASH_FATBIN_KF8VF8,
312        other => unreachable!("kv_cache_formats returned {other:?}"),
313    }
314}
315
316/// TUNE SEAM (tools/sweep): kernel1 (Q8_0/Q4_K/Q5_K) launch-tile override,
317/// `MEMRA_GEMM_K1_LAUNCH="BM,BN,NWARP"`. MUST match the `-D K1_BM/K1_BN/NWARP` the swept
318/// fatbin was compiled with (the .cu tile and the host launch grid/block have to agree —
319/// the hardcoded (128,128,8) in qmatvec_gemm/qmatvec_gemm_raw is the shipped default).
320/// Kernel2 (Q6_K/NVFP4) launch is untouched. Unset or malformed => None => shipped
321/// defaults (zero behavior change).
322fn k1_launch_override() -> Option<(u32, u32, u32)> {
323    static K1: std::sync::OnceLock<Option<(u32, u32, u32)>> = std::sync::OnceLock::new();
324    *K1.get_or_init(|| {
325        let v = std::env::var("MEMRA_GEMM_K1_LAUNCH").ok()?;
326        let p: Vec<u32> = v.split(',').filter_map(|s| s.trim().parse().ok()).collect();
327        match p.as_slice() { [bm, bn, w] => Some((*bm, *bn, *w)), _ => None }
328    })
329}
330
331/// H100 wgmma prefill-GEMM seam (task 8, ARCHITECTURE-H100.md): OPT-IN (MEMRA_WGMMA=1).
332/// v0 verdict (2026-07-26, N=5 pp512 9B-Q8_0): wgmma 3845 tok/s vs MMQ 8692 — the
333/// standalone harness's "688us MMQ ref" was a pp2048-shape figure, so v0 (unpipelined,
334/// 64x64 tile, wait_group<0> every 32-K step) is ~3x SLOWER per launch at m=512 model
335/// shapes. Default stays MMQ until the pipelined version beats it N=5 (repo law).
336/// Correctness stays pinned regardless: kernel-check's wgmma case is cfg-gated, not env-gated.
337pub(crate) fn wgmma_gemm_enabled() -> bool {
338    static V: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
339    *V.get_or_init(|| std::env::var("MEMRA_WGMMA").as_deref() == Ok("1"))
340}
341
342/// TUNE SEAM: keys per FA-decode split (`MEMRA_FA_SPLIT` forces a fixed size; default 64). Smaller
343/// splits raise grid.y so grid = n_head_kv * n_splits fills the 82 SMs at short/mid ctx (vec path
344/// launches only n_head_kv=8 CTAs per split). Swept clock-locked 2026-07-03 (graph tg128): 32 beat
345/// 64 at ctx 128/512 (+0.5/+1.2%) and lost at 2048 (-3%) — BUT the adaptive 32/64 default BROKE the
346/// MTP spec-decode exact-match gate (run-spec K=1/2 self-consistency FAIL with 32; PASS with 64):
347/// the split count changes the combine's FP summation order, and the spec verify's batched forward
348/// only argmax-matches single-step decode under the 64-split order on real prompts. Spec exactness
349/// (the bigger lever) outranks a <=1.2% decode win -> default stays FIXED 64; sweeps use the env.
350/// Takes t_kv so eager, _dc capture, and fa_geom_eager stay signature-compatible for future
351/// adaptive retries (any retry MUST pass run-spec self-consistency first).
352/// Minimum t_kv for the warp-per-token vec FA path (below it the scalar path's 4x-more-blocks
353/// hides latency better — measured crossover, see `fa_decode`). Shared by fa_decode / fa_decode_dc /
354/// fa_geom_eager / fa_decode_rows-eligibility (spec verify) so the kernel pick NEVER diverges
355/// between eager decode and the verify (the spec-exactness law).
356pub const FA_VEC_MIN_TKV: usize = 96;
357/// Env-overridable crossover (MEMRA_FA_VEC_MIN, default FA_VEC_MIN_TKV). The 96 floor was
358/// measured on the qwen geometry (nkv=2); gemma4 SWA layers run nkv=8 = 4x the vec grid,
359/// which moves the crossover — sweep per model, adopt per the battery.
360pub fn fa_vec_min_tkv() -> usize {
361    static V: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
362    *V.get_or_init(|| std::env::var("MEMRA_FA_VEC_MIN").ok()
363        .and_then(|v| v.parse().ok())
364        .unwrap_or_else(|| FA_VEC_MIN_DEFAULT.load(std::sync::atomic::Ordering::Relaxed)))
365}
366
367/// f16-P/V class (DEFAULT since 2026-07-23 stamp v4; MEMRA_FA_F16PV=0 = f32-class rollback):
368/// llama-fa=1-style f16 P + f16 P@V accumulation on the hd512/SWA prefill stamps
369/// (KQ/softmax/normalize stay f32). Laptop stamp: 12B 1.045x, 31B 0.979x vs llama.
370///
371/// SPEC-SERVING FLIP (2026-07-26, the wkv acceptance-law pattern): with MEMRA_DRAFT set the
372/// default is OFF. f16 P/V shifts the PRIME's hidden states/KV in the sub-argmax logit
373/// space the drafter feeds on — argmax gates stay MATCH while depth acceptance falls off a
374/// cliff (26B d1736 0.883 -> 0.405, -40% e2e; f16pv-off alone restores 0.846/314 tok/s —
375/// the perf-ci acceptance battery is the only gate that sees this class). Explicit
376/// MEMRA_FA_F16PV always wins; plain serving keeps the f16 prefill win.
377pub fn fa_f16pv_on() -> bool {
378    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
379    *ON.get_or_init(|| std::env::var("MEMRA_FA_F16PV").map(|v| v != "0")
380        .unwrap_or_else(|_| std::env::var("MEMRA_DRAFT").is_err()))
381}
382
383/// hd512 head-pair arm (DEFAULT since stamp v4; MEMRA_FA512_HP=0 reverts to sp16): GQA
384/// ncols2=2 — 2 heads per CTA share each staged K/V tile, Q register-resident. Engages
385/// when n_head is even and the GQA group (n_head/n_head_kv) is even.
386pub fn fa512_hp_on() -> bool {
387    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
388    *ON.get_or_init(|| std::env::var("MEMRA_FA512_HP").as_deref() != Ok("0"))
389}
390
391/// SWA head-pair arm (DEFAULT since stamp v4; MEMRA_FAW_HP=0 reverts to p1): llama-class
392/// windowed geometry — 32 q-rows x 2 heads per CTA sharing staged K/V, f16 P@V
393/// accumulation. Even n_head and even GQA group required (guarded per call).
394pub fn faw_hp_on() -> bool {
395    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
396    *ON.get_or_init(|| std::env::var("MEMRA_FAW_HP").as_deref() != Ok("0"))
397}
398
399/// 4-warp sp16 experiment arm (MEMRA_FA512_W4=1, requires the f16pv door): GEMM0 split-K
400/// 4-way + GEMM1 4x128 O-dims. Own partial-sum order — oracle-band gated. Returns warp
401/// count (2 = base sp16). 8-warp arm measured NEGATIVE 2026-07-23 (jsonl) and removed.
402pub fn fa512_wide_warps() -> usize {
403    static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
404    *N.get_or_init(|| match std::env::var("MEMRA_FA512_W4").as_deref() {
405        Ok("1") => 4, _ => 2,
406    })
407}
408
409/// hd-512 vec crossover floor (MEMRA_FA512_MIN, default 512) — shared by fa_decode dispatch
410/// and the gemma global-layer rows/parity call sites.
411pub fn fa512_min_tkv() -> usize {
412    static FA512_MIN: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
413    *FA512_MIN.get_or_init(|| std::env::var("MEMRA_FA512_MIN").ok()
414        .and_then(|v| v.parse().ok()).unwrap_or(512))
415}
416/// Per-model crossover default, set at model load BEFORE the first decode (per-model
417/// numeric-config adoption law). qwen keeps the measured 96; gemma4 (nkv=8 SWA) measured
418/// vec-always fastest: 119.9 (96) / 130.0 (48) / 133.2 (1) tok/s tg128-regime, 2026-07-10.
419pub static FA_VEC_MIN_DEFAULT: std::sync::atomic::AtomicUsize =
420    std::sync::atomic::AtomicUsize::new(FA_VEC_MIN_TKV);
421/// Per-model windowed-split default (MEMRA_FA_SPW overrides): gemma MoE (26B, nkv=8) measured
422/// 32 (grid-limited t=1 under the raw-e4m3 sV ceiling, 2026-07-12); dense gemma (31B)
423/// measured 64 (37.13/37.12 vs 36.87/36.86 at 1.7k, N=2 — different attention geometry).
424pub static FA_SPW_DEFAULT: std::sync::atomic::AtomicUsize =
425    std::sync::atomic::AtomicUsize::new(32);
426/// Per-model hd512 (gemma globals) split default (MEMRA_FA_SP512 overrides): 26B measured 16
427/// (2026-07-11 N=2), dense 31B measured 32 (36.86/36.93 vs 36.73/36.73 at 1.7k, 2026-07-12).
428/// fused t=1 q4_0 pair/triple row mapping: true = mr1 (one row/warp). Per-model default
429/// (dense gemma wins +1.1% short / +0.6% depth on the 31B; MoE 26B REGRESSES −1.2% —
430/// its shared-expert fused2 shapes lose to the finer grid). MEMRA_Q40_MR env still wins.
431pub static FUSED_MR1_DEFAULT: std::sync::atomic::AtomicBool =
432    std::sync::atomic::AtomicBool::new(false);
433/// Per-model router-GEMV form (2026-07-31): the 8-warp twin is +8.8% on the H100 q35
434/// decode step (router was 14.8% of it) with argmax + spec self-consistency green on
435/// qwen-class MoE both rigs. The gemma-4 26B knife-edge block (2026-07-31, single
436/// synthetic prompt) was RE-ARBITRATED 2026-08-01 on 6 real prompts — gate outcomes
437/// identical to the lone-warp arm, +13% g26 decode — so gemma4 rides the default too
438/// (research/g26-decode-20260801/). MEMRA_ROUTER_V2 env overrides either way.
439pub static ROUTER_W8_DEFAULT: std::sync::atomic::AtomicBool =
440    std::sync::atomic::AtomicBool::new(true);
441pub static FA_SP512_DEFAULT: std::sync::atomic::AtomicUsize =
442    std::sync::atomic::AtomicUsize::new(16);
443/// Per-model rms_norm block size (per-model numeric-config law: the per-thread partial-sum
444/// split changes with blockDim -> different FP order -> battery-arbitrated per model).
445/// qwen keeps the shipped 256; gemma4 adopts 1024 (single-row 2816-col norms are one-block
446/// latency-bound at 256 threads — 7us/launch measured).
447pub static RMS_BLOCK_DEFAULT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(256);
448/// gemma4 fa split ladder switch (set at model load; see fa_split_keys).
449pub static FA_SP_GEMMA: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
450/// Per-model stream-k override for SPEC serving (-1 = unset → env/default; 0 = force
451/// tiling; 1 = force sk). Set by generate_spec_gemma per model tier — the sk autotune's
452/// per-process kernel coin made 12B-class spec cells bimodal, while the 26B's drafter
453/// measures BETTER under sk's fold order (2026-07-27). mmq_ffi reads this before the env.
454pub static MMQ_SK_FORCE: std::sync::atomic::AtomicI8 = std::sync::atomic::AtomicI8::new(-1);
455/// Per-model FP8-KV door — lives in memra-kv next to the format policy it drives
456/// (re-export keeps `crate::KV_FP8_FORCE` setters in model.rs/hybrid.rs working).
457pub use memra_kv::KV_FP8_FORCE;
458pub(crate) fn rms_block() -> u32 {
459    static V: std::sync::OnceLock<u32> = std::sync::OnceLock::new();
460    *V.get_or_init(|| std::env::var("MEMRA_RMS_BLOCK").ok()
461        .and_then(|v| v.parse().ok())
462        .unwrap_or_else(|| RMS_BLOCK_DEFAULT.load(std::sync::atomic::Ordering::Relaxed)))
463}
464
465pub(crate) fn fa_split_keys(t_kv: usize, n_head_kv: usize) -> usize {
466    static S: std::sync::OnceLock<Option<usize>> = std::sync::OnceLock::new();
467    if let Some(forced) = *S.get_or_init(|| {
468        std::env::var("MEMRA_FA_SPLIT").ok().and_then(|v| v.parse().ok())
469            .filter(|&s: &usize| s >= 8 && s % 8 == 0)
470    }) { return forced; }
471    // CTX-ADAPTIVE default (2026-07-05 40k sweep: sp32 24.5 vs sp128 26.0 tok/s = +5.8% — at
472    // deep ctx the n_splits count explodes (40k/32 = 1265 splits x 8 kv-heads) and the combine
473    // + partial-buffer cost dominates; at short ctx small splits fill the SMs). Exactness: split
474    // size only changes the PARTITION of keys; the rows/combine order per split is fixed and the
475    // gate battery (kernel-check + run-spec K=1..8) arbitrates every default change.
476    //
477    // SM-AWARE SHORT-CTX RUNG (2026-07-06 g7e): the 32-key rung was tuned on the 82-SM 5090.
478    // On 188 SMs the vec grid (n_head_kv x n_splits CTAs) starves at short ctx — the 35B has
479    // n_head_kv=2, so ctx128/split32 = 8 CTAs on 188 SMs. Measured on g7e (N=1 sweep + N=3
480    // interleaved confirm): 35B ctx128 sp16 179 vs sp32 161 (+11%), ctx512 178 vs 158, ctx2048
481    // flat, ctx>=4096 sp64 edges sp16 by ~3%; 27B ctx128 70.9 vs 66.3 (+7%); 9B 177 vs 163
482    // (+9%). Rigs <=100 SMs keep the validated 5090 ladder EXACTLY (default unchanged there —
483    // rig-divergence law: this branch is measured on 188 SMs only).
484    // gemma4 all-16 ladder probe REVERTED (2026-07-10): +1.3 plain at d1736 (157.5 vs 156.2)
485    // but depth VERIFY collapsed (spec 203.5 -> 169 — the windowed rows' per-row combine over
486    // 64 splits). The mixed default (swa nkv=8 -> 32, globals nkv=2 -> 8-ladder) stays; a
487    // caller-split policy would break row-vs-decode split parity. FA_SP_GEMMA kept as a seam.
488    if FA_SP_GEMMA.load(std::sync::atomic::Ordering::Relaxed)
489        && std::env::var("MEMRA_FA_SP16").as_deref() == Ok("1") {
490        return if t_kv <= 8192 { 16 } else if t_kv <= 16384 { 64 } else { 128 };
491    }
492    let big_rig = fa_sm_count() >= 128;
493    if big_rig {
494        let _ = n_head_kv;
495        if t_kv <= 2048 { 16 } else if t_kv <= 16384 { 64 } else { 128 }
496    } else if n_head_kv <= 4 {
497        // KV-HEAD-AWARE RUNG (2026-07-08, 5090): the 8192->32 rung was validated on kv=8 models
498        // (27B/9B: 8 heads x n_splits fills 82 SMs). The 35B has n_head_kv=2 — at ctx512/sp32
499        // the vec grid is 2 x 20 = 40 CTAs on 82 SMs (half idle). Measured (35B, run-gen 128tok
500        // N=1 sweep + N=3 confirm): sp8 162.1 / sp16 161.3 / sp32 159.4 at short ctx.
501        // DEPTH TAPER (same day, the deep-ctx lesson re-learned on this rung): sp8 at d6257 =
502        // 782 splits -> combine + partial-buffer cost dominates (141.2 tok/s); the d6257 sweep
503        // says sp64 = 153.0 (sp16/32 147, sp96 147.6, sp128 141). Few-kv-head models need the
504        // taper EARLIER than kv=8 (per-split grid 4x thinner, same per-split combine cost).
505        // Crossover hunt: sp8 vs sp64 = 156.7/155.9 at d3072, 151.7/155.6 at d4096 -> boundary 3072.
506        // RUNG RE-SWEPT UNDER THE DEEP KERNEL (2026-08-02, lane/ladder-3072 — the stale-verdict
507        // law: the 3072 boundary was calibrated on the conflicted v4 core; the deep rewrite cut
508        // vec cost ~1.2-1.4x while combine scales with n_splits, so sp8's combine bill
509        // dominates far earlier). Kernel receipts (quiet-rig nsys, deep vec + combine us):
510        // d1024 sp8 17.1 vs sp64 10.6; d2048 31.0 vs 12.2; d3072 44.0 vs 18.3. e2e run-gen
511        // tg128 N=3 interleaved (KAT + q35, research/ladder-3072-20260802/): sp8 loses at
512        // EVERY depth >= 1024 (KAT d2048 182.6 vs 188.0 = -2.9%, d3072 175.9 vs 186.4 =
513        // -5.6%; q35 d4096 169.2 vs 182.6 = -7.4%); d512 flat (+-0.2%, inside noise). sp32
514        // ties sp64 within noise in the mid band and loses at d4096 -> no extra rung.
515        // Boundary 3072 -> 512: sp8 keeps only the short-ctx band it was validated on
516        // (ctx128-512); sp64 takes over where the deep kernel made combine the bill.
517        if t_kv <= 512 { 8 } else if t_kv <= 16384 { 64 } else { 128 }
518    } else {
519        if t_kv <= 8192 { 32 } else if t_kv <= 16384 { 64 } else { 128 }
520    }
521}
522
523/// SM count of device 0, cached (used by fa_split_keys' rig-size rung; primary-context query,
524/// same attribute Engine::batched_variant reads).
525fn fa_sm_count() -> i32 {
526    static N: std::sync::OnceLock<i32> = std::sync::OnceLock::new();
527    *N.get_or_init(|| {
528        cudarc::driver::result::init().ok();
529        cudarc::driver::result::device::get(0)
530            .and_then(|d| unsafe { cudarc::driver::result::device::get_attribute(
531                d, cudarc::driver::sys::CUdevice_attribute_enum::CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT) })
532            .unwrap_or(82)
533    })
534}
535
536/// FA-prefill kernel-name suffix for a head_dim (the template-stamped twins in flash_attn.cu):
537/// 256 = the original names (qwen35 class, dispatch unchanged), 128 = `_hd128` (MiniMax-M3).
538/// Any other dim errors — callers gate to sdpa_naive before dispatching FA.
539fn fa_hd_suffix(head_dim: usize) -> Result<&'static str, Box<dyn std::error::Error>> {
540    match head_dim {
541        256 => Ok(""),
542        128 => Ok("_hd128"),
543        d => Err(format!("fa_prefill: no kernel stamped for head_dim={d} (only 256/128); \
544                          callers must gate to sdpa_naive").into()),
545    }
546}
547
548/// Quant type codes matching qmatvec.cu QType enum.
549pub const QT_Q8_0: i32 = 0;
550pub const QT_Q4_K: i32 = 1;
551pub const QT_Q6_K: i32 = 2;
552pub const QT_Q5_K: i32 = 3;
553pub const QT_Q3_K: i32 = 4;
554pub const QT_IQ4_XS: i32 = 5;
555pub const QT_IQ3_S: i32 = 6;
556pub const QT_NVFP4: i32 = 7;
557/// Checkpoint-native FP8-E4M3 (MEMRA_ST_E4M3, lane e4m3dec): raw safetensors e4m3 weight bytes
558/// [out_f, in_f] row-major (row_bytes == in_f), per-tensor f32 weight_scale in GpuTensor `scale`
559/// (fused at the mmvq write / post-matmul scale_inplace). Decode = qmatvec_e4m3_mmvq (+ _b2/_b4/_b8
560/// batched twins); prefill (m>=16) = the cuBLASLt FP8 GEMM on the SAME resident bytes (fp8_ffi.rs)
561/// — ONE weight copy total, no Q8_0 re-encode duplicate.
562pub const QT_F8_E4M3: i32 = 10;
563/// Device-side tag for the A6 SPLIT-PLANE repacked NVFP4 layout (Stage-A generic kernel only;
564/// GpuTensor keeps qtype=QT_NVFP4 + an `rp` flag — this tag never lives in a GpuTensor).
565pub const QT_NVFP4_RP: i32 = 9;
566/// Unquantized f32 weight (safetensors MoE Path A: experts dequantized to f32 host-resident).
567pub const QT_F32: i32 = 8;
568pub const QT_BF16: i32 = 11;
569pub const QT_Q4_0: i32 = 12; // gemma-4 QAT GGUF weight format (18B/32: fp16 d + nibbles)
570/// GGUF Q2_K. Appended after the existing Q4_0 code so kernel ABI values do not move.
571/// Mixed-expert artifacts use the generic f32-dequant staged kernel until a target-rig-gated
572/// dp4a/MMQ implementation exists.
573pub const QT_Q2_K: i32 = 13;
574/// Checkpoint-native FP8-E4M3 with a BLOCK-128 weight-scale GRID (lane/fp8-blk128-decode,
575/// 2026-08-05) — the Qwen-official FP8 / DeepSeek-V3 scale class. Same raw e4m3 bytes as
576/// `QT_F8_E4M3` ([out_f, in_f] row-major, row_bytes == in_f), but the dequant scale is
577/// `GpuTensor::Quant.blk` (`Fp8BlockScales`, [ceil(out_f/128), ceil(in_f/128)] f32) and the
578/// scalar `scale` field is 1.0 by the layout contract.
579///
580/// WHY A DISTINCT CODE rather than `QT_F8_E4M3` + a `blk` flag: every existing QT_F8_E4M3
581/// consumer (qmatvec_e4m3_mmvq and its batched/fused twins, e4m3_fused_params,
582/// matmul_pre_dual_noscale's F8 arm, try_fp8_gemm) threads exactly ONE scalar weight scale. Under
583/// a shared code, any consumer that was not taught the grid would still MATCH and would dequant
584/// every tile at scale 1.0 — a silent numeric corruption. Under a distinct code every untaught
585/// consumer refuses loudly instead (`mmvq_supports`/`gemm_supports`/`mmq_supports` return false;
586/// the mmvq name match panics), so a missed dispatch site is a crash or a refusal receipt, never
587/// wrong numbers. Decode = `qmatvec_e4m3_blk_mmvq`; prefill (m>=16) = the per-block FP8 MMQ tile
588/// on the SAME resident bytes+grid (fp8_ffi::try_fp8_blk_mmq) — ONE weight copy total.
589pub const QT_F8_E4M3_BLK: i32 = 14;
590
591/// Engine device context: CUDA context, stream, loaded kernel modules, cuBLASLt (via runtime::Gpu).
592pub struct Engine {
593    pub gpu: memra_runtime::Gpu,
594    module: Arc<CudaModule>,
595    hybrid: Arc<CudaModule>,
596    qmatvec: Arc<CudaModule>,
597    flash: Arc<CudaModule>,
598    /// FP8-GLOBALS module (2026-07-11): the kf8vf8 fatbin loaded ALONGSIDE the default —
599    /// gemma GLOBAL layers (hd512) append + attend in e4m3 (dequant-latency arc, HANDOVER).
600    /// Lazy: loaded on first global-format use; None until then.
601    flash_g: std::sync::OnceLock<Arc<CudaModule>>,
602    gemm: Arc<CudaModule>,
603    router: Arc<CudaModule>,
604    /// Sampled-spec kernels (research/sampled-spec-impl-map.md piece A).
605    sample: Arc<CudaModule>,
606    /// EDGE-1 §B: one shared SLRU expert-residency cache, lazily built on first MoE dispatch under
607        /// MEMRA_MOE_CACHE. `Mutex` makes it multi-agent safe (§E.2); the lock covers only lookup/admit/
608    /// memcpy-issue (µs), NOT the GEMM, so streams still overlap. `None` => cache disabled.
609    moe_cache: Mutex<Option<crate::moe_cache::MoeSlotCache>>,
610    /// Exact retained expert-block lengths collected after model load. Mixed-layout models use
611    /// this inventory to preallocate fixed-address size classes instead of sizing every slot to
612    /// the single largest block. The cache still owns every address for its full lifetime.
613    moe_cache_layout: Mutex<Option<Vec<usize>>>,
614    /// CAPTURE-RETAIN mode (graph arc, 2026-07-12): while a graph capture (and its allocator
615    /// warmups) runs, every Engine allocation is ALSO kept alive here — a captured graph's
616    /// transient buffers must never return to the pool, or later allocations (e.g. the spec
617    /// verify between replays) reuse their addresses and the replay reads/writes live memory
618    /// (the draft-graph corruption root cause). Fast-path cost when off: one relaxed atomic.
619    capture_keep_on: std::sync::atomic::AtomicBool,
620    /// VERIFY-EXACT scope (dflash lane, 2026-07-13): when set, matmul/matmul_pre skip the
621    /// m>=16 prefill-GEMM branches so a t>=16 batched VERIFY rides the decode-exact b-tier
622    /// class (the parity law). The t=16 dflash verify tripped the GEMM threshold — 770us/
623    /// matmul (54% of the round) AND a different FP order than decode (issue-10 landmine).
624    verify_exact: std::sync::atomic::AtomicBool,
625    capture_keep: Mutex<Vec<Box<dyn std::any::Any + Send>>>,
626    /// EDGE-1 §C.2: dedicated H2D copy stream for async prefetch (event-synced to the compute stream).
627    pub copy_stream: Arc<CudaStream>,
628    /// Resident CUTLASS NVFP4 prefill scratch (workspace + a_packed + sfa_linear + sfa_sw + y + alpha),
629    /// allocated ONCE and grown to the largest prefill GEMM shape, then reused per-call. Removes the
630    /// 6 fresh allocations + alpha htod that `cutlass_fp4_gemm` did every prefill matmul (~200/prefill).
631    /// Safe as a single shared buffer because all GPU compute serializes on the one `gpu.stream` worker
632    /// thread (the server runs one GPU worker; no concurrent CUTLASS GEMMs share this scratch). `None`
633    /// until the first CUTLASS FP4 GEMM. Mutex guards lazy build/grow only (matches `moe_cache`).
634    #[cfg(memra_cutlass)]
635    cutlass_scratch: Mutex<Option<crate::cutlass_ffi::CutlassScratch>>,
636    /// FP8-ACT PREFILL scratch (MEMRA_PP_FP8): quantized-activation buffer + scale block + cuBLASLt
637    /// workspace, allocated once and grown to the largest prefill m*k (see fp8_ffi.rs). `None`
638    /// until the first FP8 prefill GEMM; Mutex guards lazy build/grow only (matches cutlass_scratch).
639    fp8_scratch: Mutex<Option<crate::fp8_ffi::Fp8Scratch>>,
640    /// f16-P/V door: pooled V re-encode buffer (bf16->f16) for the hd512 _pre path. Lazy-grow;
641    /// per-call cudaMalloc was a laptop-regression suspect (VRAM pressure, 31B nkv=4 = 4x bytes).
642    fa_vf16_scratch: Mutex<Option<CudaSlice<u8>>>,
643    /// Pooled fa-decode split partials (part_o, part_m, part_l): per-call zeros() was 3
644    /// alloc+memset pairs per fa launch (~144 mem nodes per decode token — the graph door's
645    /// residual launch tax) — lazy-grow, memset-prefix per use, stream-ordered reuse.
646    fa_part_pool: Mutex<Option<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>)>>,
647    /// Retired fa-part pool generations (#68): old buffers whose addresses captured graphs may
648    /// have baked — kept alive for the Engine's lifetime instead of returning to the async pool
649    /// (see the RETIRE-ON-GROW comment at the realloc sites). Doubling growth bounds the total.
650    fa_part_retired: Mutex<Vec<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>)>>,
651    /// name -> resolved CudaFunction (capture-safe lookups; see `func`).
652    fn_cache: Mutex<std::collections::HashMap<String, CudaFunction>>,
653    f16_scratch: Mutex<Option<crate::f16_ffi::F16Scratch>>,
654    /// RANK1 LEVER (parallel argmax): resident pass-1 partials scratch (part_v[NB] f32, part_i[NB] i32),
655    /// allocated ONCE on first parallel-argmax call and reused. Stable pointers so the 2-pass argmax
656    /// is CUDA-graph-capturable (the buffer is referenced by both captured passes; lazy-allocated
657    /// before capture under the generate_graph tracking-off window so it carries no events).
658    argmax_partials: Mutex<Option<(CudaSlice<f32>, CudaSlice<i32>)>>,
659    /// ARC B (chunk-prime dequant-once): resident bf16 K/V workspace for `fa_prefill_view_ws`
660    /// ((K bytes, V bytes) u8 buffers holding [t_kv, kv_dim] bf16). Grown lazily to the largest
661    /// (t_kv, kv_dim) seen, REUSED across layers/chunks/calls (contents rewritten per launch —
662    /// safe because all compute serializes on the one gpu.stream). ~82MB at 40k ctx on the 27B.
663    prime_deqw_ws: Mutex<Option<(CudaSlice<u8>, CudaSlice<u8>)>>,
664    /// LAUNCH-STRUCTURE STAGE 1: persistent PINNED (cacheable, flags=0) host staging buffer for the
665    /// fused-router sel/w readback — one async DtoH pair + ONE sync instead of two synced dtohs.
666    /// Grown lazily; reused every MoE layer (single-threaded decode serializes on the sync).
667    router_stage: Mutex<Option<PinnedStage>>,
668}
669
670/// FAVENDOR lane env gate (2026-07-08): MEMRA_FA_V2=1 dispatches the llama-fattn-vec-mechanism
671/// decode kernels (fa_decode_vec_q_v2 / fa_decode_vec_q_rows_v2 / fa_decode_vec_q_v2_dc):
672/// tile-batched online softmax (one alpha rescale per 32-key tile instead of per key) + wide-load
673/// block dequant in the staging phase. NOTE rev2: llama's register streaming (no smem) was ALSO
674/// tried and measured 2x WORSE at depth in our gqa-warps frame — the smem KV-tile broadcast stays
675/// (see the kernel comment). NEW NUMERIC CONFIG (tile-level softmax regrouping changes FP order vs
676/// the per-key twins) — own argmax baseline; eager decode, the spec-verify rows path AND the
677/// graph _dc path switch TOGETHER (the spec-exactness law). Default OFF. Read per call (not
678/// OnceLock) so the gate battery can A/B within one process, matching the MEMRA_NO_FA_VEC pattern.
679fn fa_v2_on() -> bool {
680    // DEFAULT ON since 2026-07-08 (MEMRA_FA_V2=0 reverts): tile-batched online softmax, e2e
681    // measured across every model x depth — 35B 168.7->173.4 (d512) / 153.1->158.5 (d6257),
682    // 9B 131.2->132.7 / 108.4->124.5 (+15% — the engine-wide depth-slope fix), 27B 47.2->47.7 /
683    // 42.2->44.9. One-time numeric-config change; kernel-check + argmax + spec self-consistency
684    // + graph bit-identity green on all three models.
685    std::env::var("MEMRA_FA_V2").map(|v| v != "0").unwrap_or(true)
686}
687
688/// FA v3 gate (default ON since 2026-07-09; MEMRA_FA_V3=0 reverts to v2 — research/fa/fa_v3_design.md):
689/// HYBRID decode twins (fa_decode_vec_q_v3 / _rows_v3 / _v3_dc): llama's int8-dp4a K.Q with
690/// register-quantized Q (no K dequant, no K smem) + OUR CTA-shared staged bf16 V tile + OUR
691/// split partition/combine. NEW NUMERIC CONFIG (int8 Q quantization changes the K.Q accumulation
692/// vs the bf16-roundtrip FMA chain) — own argmax baseline; eager decode, the spec-verify rows
693/// path AND the graph _dc path switch TOGETHER (the spec-exactness law). Read per call so the
694/// gate battery can A/B within one process (the MEMRA_FA_V2 pattern).
695fn fa_v3_on() -> bool {
696    // DEFAULT ON since 2026-07-09 (MEMRA_FA_V3=0 reverts to v2): dp4a-K hybrid FA decode —
697    // fa kernel -21-23% at depth (micro), 35B spec p3 +5% (190->200, the last spec cell),
698    // d6257 +1.7%. Own numeric config; full battery green on 35B+9B incl graph bit-identity.
699    std::env::var("MEMRA_FA_V3").map(|v| v != "0").unwrap_or(true)
700}
701
702/// The v3 dp4a K path reads RAW q8_0 bytes (34B blocks) and stages q5_1 V verbatim — it is only
703/// correct on the DEFAULT KV formats — and needs dpl % 4 == 0 consecutive quants per lane
704/// (head_dim % 128 == 0; both daily models are hd256). All three dispatch sites share this
705/// predicate so the twins can never diverge.
706fn fa_v4_mode() -> &'static str {
707    static M: std::sync::OnceLock<String> = std::sync::OnceLock::new();
708    M.get_or_init(|| std::env::var("MEMRA_FA_V4").unwrap_or_default())
709}
710fn fa_v4_on() -> bool { fa_v4_mode() != "0" }   // DEFAULT ON 2026-07-10 (MEMRA_FA_V4=0 rollback)
711/// t_kv-conditional v4 pick (gemma depth lesson 2026-07-10: v4's key-per-lane pipeline starves
712/// at the 1024-window with short splits — MEMRA_FA_V4=0 measured depth plain 158.0 vs 156.7).
713/// Threshold MEMRA_FA_V4_MAX (default usize::MAX = unchanged behavior; gemma sets 1024 at load
714/// via FA_V4_MAX_DEFAULT). Applied at EVERY dispatch site (eager, rows, rows_w, dc) so verify
715/// stays kernel-family-identical to decode at the same t_kv.
716/// Per-model deep-ctx smem floor default (MEMRA_FA_SMEM_TKV env overrides): gemma pushes it
717/// above the 1024 window so the windowed decode + verify rows share the REGISTER family.
718pub static FA_SMEM_TKV_DEFAULT: std::sync::atomic::AtomicUsize =
719    std::sync::atomic::AtomicUsize::new(1024);
720pub static FA_V4_MAX_DEFAULT: std::sync::atomic::AtomicUsize =
721    std::sync::atomic::AtomicUsize::new(usize::MAX);
722pub fn fa_v4_at_pub(t_kv: usize) -> bool { fa_v4_at(t_kv) }
723fn fa_v4_at(t_kv: usize) -> bool {
724    static M: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
725    let mx = *M.get_or_init(|| std::env::var("MEMRA_FA_V4_MAX").ok()
726        .and_then(|v| v.parse().ok())
727        .unwrap_or_else(|| FA_V4_MAX_DEFAULT.load(std::sync::atomic::Ordering::Relaxed)));
728    fa_v4_on() && t_kv < mx
729}
730/// FA-DEEP gate (2026-08-02, lane fa-decode-deep): deep-ctx v4 twins
731/// (fa_decode_vec_q_v4_deep / _deep_dc) — the depth-decode lane's priced fix. Unlike
732/// v2/v3/v4 this is NOT a numeric config: the deep twins run the v4 program VERBATIM
733/// (same split partition, same softmax/accumulation order, same partials/combine) and only
734/// move the smem physical layout (bank de-conflict row pads) + the load schedule (next-tile
735/// L2 prefetch) — kernel-check pins bitdiff==0 vs the v4 twins across depths, so eager /
736/// rows-verify / graph / seqs stay mutually bit-identical wherever the threshold falls.
737/// Engages at t_kv >= MEMRA_FA_DEEP_MIN. The swept floor is 0 = ALWAYS ON where v4 ran
738/// (fa-deep-bench fine grid 96..6144, 2026-08-02: deep flat-or-better at EVERY depth,
739/// 1.01-1.26x, no losing cell — so there is no engagement boundary and no new
740/// capture-recapture edge; the env stays as a sweep/diagnostic seam only).
741/// MEMRA_FA_DEEP=0 is the rollback seam. Read per call so the battery + bench can A/B
742/// within one process (the v2/v3 pattern).
743pub const FA_DEEP_MIN_DEFAULT: usize = 0;
744fn fa_deep_at(t_kv: usize) -> bool {
745    if std::env::var("MEMRA_FA_DEEP").as_deref() == Ok("0") { return false; }
746    let min = std::env::var("MEMRA_FA_DEEP_MIN").ok().and_then(|v| v.parse().ok())
747        .unwrap_or(FA_DEEP_MIN_DEFAULT);
748    t_kv >= min
749}
750/// Public twin (kernel-check builds the deep-vs-v4 bit pin; bench sweeps the floor).
751pub fn fa_deep_at_pub(t_kv: usize) -> bool { fa_deep_at(t_kv) }
752
753fn fa_v3_active(head_dim: usize) -> bool {
754    // v3's dp4a-K walk reads raw q8_0 K bytes — no e4m3 arm; the fp8-KV arm (MEMRA_KV_FP8)
755    // must fall back like any non-default KV format (the rows_dc stream path asserts on it).
756    fa_v3_on() && head_dim % 128 == 0 && kv_cache_formats() == ("q8_0", "q5_1")
757        && !Engine::kv_fp8_on()
758}
759
760/// BATCHED-TICK increment 2 (2026-08-01): true iff a row at this t_kv would take the v4
761/// eager arm in `fa_decode_kvmod`'s dispatch — the exact precondition for the z-batched
762/// `fa_decode_vec_q_seqs_v4` twin to reproduce its per-seq program bit-identically.
763/// Mirrors the kvmod predicates: vec on + above the vec floor + hd256 + inside the v4
764/// window + the PRODUCTION v4 body (the noB3/stage phase probes are wrong-output) + the
765/// default flash module (no fp8-KV g-module). Callers must ALSO group rows on one
766/// `fa_split_keys` rung (the rows-twins' straddle law) before batching.
767pub fn fa_seqs_eligible(t_kv: usize, head_dim: usize) -> bool {
768    std::env::var("MEMRA_NO_FA_VEC").is_err()
769        && t_kv >= fa_vec_min_tkv()
770        && head_dim == 256
771        && fa_v4_at(t_kv)
772        && !matches!(fa_v4_mode(), "noB3" | "stage")
773        && !Engine::kv_fp8_on()
774}
775/// Public twin of the crate-private split ladder (kernel-check builds the seqs-vs-loop pin).
776pub fn fa_split_keys_pub(t_kv: usize, n_head_kv: usize) -> usize { fa_split_keys(t_kv, n_head_kv) }
777
778/// A raw pinned (page-locked, CACHEABLE — flags=0, not write-combined) host allocation for
779/// DtoH staging. cudarc's `alloc_pinned` uses CU_MEMHOSTALLOC_WRITECOMBINED, which is right for
780/// HtoD streams but pathologically slow for host READS — the router readback is host-read-heavy,
781/// so we allocate through `result::malloc_host` with flags=0 directly.
782struct PinnedStage {
783    ptr: *mut u8,
784    cap: usize,
785}
786unsafe impl Send for PinnedStage {}
787impl PinnedStage {
788    fn new(cap: usize) -> Result<Self, Box<dyn std::error::Error>> {
789        let ptr = unsafe { cudarc::driver::result::malloc_host(cap, 0)? } as *mut u8;
790        Ok(PinnedStage { ptr, cap })
791    }
792}
793impl Drop for PinnedStage {
794    fn drop(&mut self) {
795        let _ = unsafe { cudarc::driver::result::free_host(self.ptr as _) };
796    }
797}
798
799/// Number of pass-1 blocks for the parallel argmax (fan-out across SMs to saturate HBM). 256 blocks
800/// x 256 threads = 65536 threads covering the 248K-vocab scan in ~4 strided loads/thread.
801pub const ARGMAX_NB: usize = 256;
802
803/// crate-visible alias for the batched FA3 shim entry (hybrid_forward's batch arm).
804pub(crate) use memra_fa3_vl as fa3_vl_raw;
805
806unsafe extern "C" {
807    /// FA3 v10 shim (cu/fa3_prefill.cu): TMA-swizzled wgmma FA, fresh causal hd256.
808    fn memra_fa3_prefill(q16: *const core::ffi::c_void, k16: *const core::ffi::c_void,
809                        v16: *const core::ffi::c_void, o: *mut f32,
810                        t: i32, h: i32, hkv: i32, d: i32, scale: f32,
811                        stream: *mut core::ffi::c_void) -> i32;
812    /// batched varlen twin: host arrays of device pointers per seq (B <= 8).
813    pub(crate) fn memra_fa3_vl(q16s: *const *const core::ffi::c_void, k16s: *const *const core::ffi::c_void,
814                   v16s: *const *const core::ffi::c_void, os: *const *mut f32,
815                   ts: *const i32, b: i32, h: i32, hkv: i32, d: i32, scale: f32,
816                   stream: *mut core::ffi::c_void) -> i32;
817}
818
819/// STAGE-2 GROUPED DECODE: 8 expert weight-block device pointers passed BY VALUE as one kernel
820/// param (matches the CUDA `wptr8_t` struct: 8x 64-bit pointers, `#[repr(C)]` => identical
821/// layout). The pointers are SLRU cache-slot base addresses — fixed for the engine's lifetime
822/// (slots are never re-allocated), so passing raw values is stable across the launch.
823#[repr(C)]
824#[derive(Clone, Copy)]
825pub struct WPtr8(pub [u64; 8]);
826unsafe impl cudarc::driver::DeviceRepr for WPtr8 {}
827
828/// task #18 varlen GDN: per-seq args for gdn_chunk_{state,output}_mma_vl — one launch
829/// runs all B<=8 sequences' K4/K5 (CUDA `gdnseq_t`/`gdnvl_t`, layout-identical repr(C)).
830/// Raw addresses are valid for the launch: every referenced buffer outlives the call and
831/// all work is on the single compute stream (same discipline as the f16 GEMM FFI).
832#[repr(C)]
833#[derive(Clone, Copy, Default)]
834pub struct GdnSeqVl {
835    pub kb16: u64, pub gcum: u64, pub beta: u64, pub u: u64, pub wb16: u64,
836    pub y: u64, pub ssnap: u64, pub state_in: u64, pub state_out: u64,
837    pub q: u64, pub p: u64, pub o: u64,
838    pub k: u64, pub v: u64, pub g: u64, pub a: u64, pub w: u64,
839    pub t: i32, pub nc: i32,
840}
841unsafe impl cudarc::driver::DeviceRepr for GdnSeqVl {}
842#[repr(C)]
843#[derive(Clone, Copy)]
844pub struct GdnVl8(pub [GdnSeqVl; 8]);
845unsafe impl cudarc::driver::DeviceRepr for GdnVl8 {}
846
847/// task #22: per-seq wgmma-fused extras (CUDA `gdnw_t`/`gdnwvl_t`) — qb16 mirror +
848/// pre-masked Pb16, riding NEXT TO GdnSeqVl so the base struct stays untouched.
849#[repr(C)]
850#[derive(Clone, Copy, Default)]
851pub struct GdnWVl { pub qb16: u64, pub pb16: u64 }
852unsafe impl cudarc::driver::DeviceRepr for GdnWVl {}
853#[repr(C)]
854#[derive(Clone, Copy)]
855pub struct GdnWVl8(pub [GdnWVl; 8]);
856unsafe impl cudarc::driver::DeviceRepr for GdnWVl8 {}
857
858/// task #18 increment 3: per-seq PREP/TAIL args (CUDA `gdnprep_t`/`gdnprepvl_t`).
859#[repr(C)]
860#[derive(Clone, Copy, Default)]
861pub struct GdnPrepVl {
862    pub qkv: u64, pub conv_state: u64, pub conv_out: u64,
863    pub q_g: u64, pub k_g: u64, pub v_g: u64,
864    pub q_l2: u64, pub k_l2: u64,
865    pub beta_raw: u64, pub alpha: u64, pub beta: u64, pub g_log: u64,
866    pub o: u64, pub z: u64, pub gn: u64, pub gn16: u64,
867    pub kb16: u64,
868    pub qb16: u64,
869    pub t: i32, pub pad: i32,
870}
871unsafe impl cudarc::driver::DeviceRepr for GdnPrepVl {}
872#[repr(C)]
873#[derive(Clone, Copy)]
874pub struct GdnPrepVl8(pub [GdnPrepVl; 8]);
875unsafe impl cudarc::driver::DeviceRepr for GdnPrepVl8 {}
876
877/// task #18 (attn side): per-seq varlen FA args (CUDA `faseq_t`/`favl_t`).
878#[repr(C)]
879#[derive(Clone, Copy, Default)]
880pub struct FaSeqVl {
881    pub q: u64, pub k16: u64, pub v16: u64, pub o: u64, pub kf: u64, pub vf: u64,
882    pub t: i32, pub pad: i32,
883}
884unsafe impl cudarc::driver::DeviceRepr for FaSeqVl {}
885#[repr(C)]
886#[derive(Clone, Copy)]
887pub struct FaVl8(pub [FaSeqVl; 8]);
888unsafe impl cudarc::driver::DeviceRepr for FaVl8 {}
889
890/// task #18 (attn pre-FA): per-seq split/norm/rope/append args (CUDA `attnpre_t`).
891#[repr(C)]
892#[derive(Clone, Copy, Default)]
893pub struct AttnPreVl {
894    pub qf: u64, pub kf: u64, pub vf: u64,
895    pub q: u64, pub gate: u64, pub qn: u64, pub kn: u64,
896    pub kc: u64, pub vc: u64,
897    pub t: i32, pub pad: i32,
898}
899unsafe impl cudarc::driver::DeviceRepr for AttnPreVl {}
900#[repr(C)]
901#[derive(Clone, Copy)]
902pub struct AttnPreVl8(pub [AttnPreVl; 8]);
903unsafe impl cudarc::driver::DeviceRepr for AttnPreVl8 {}
904
905/// task #18 increment 2: one sequence's FULL chunk-buffer set (alloc-only; the
906/// varlen K1-K5 chain fills them).
907pub struct GdnChunkBufs {
908    pub gcum: CudaSlice<f32>,
909    pub a: CudaSlice<f32>,
910    pub p: CudaSlice<f32>,
911    pub u: CudaSlice<f32>,
912    pub w: CudaSlice<f32>,
913    pub kb16: CudaSlice<u8>,
914    pub wb16: CudaSlice<u8>,
915    pub y16: CudaSlice<u8>,
916    pub ssnap16: CudaSlice<u8>,
917    pub qb16: CudaSlice<u8>,
918    pub pb16: CudaSlice<u8>,
919    pub o: CudaSlice<f32>,
920    pub t: usize,
921    pub nc: usize,
922}
923
924/// STAGE-2 GROUPED DECODE: the 8 routed-expert weights by value (CUDA `f32x8_t`).
925#[repr(C)]
926#[derive(Clone, Copy)]
927pub struct F32x8(pub [f32; 8]);
928unsafe impl cudarc::driver::DeviceRepr for F32x8 {}
929
930/// Harness timing contract: wall nanos of the LAST generate/generate_spec prompt prime on this
931/// process. Bench binaries read it right after the call to print gen-only throughput without the
932/// prime-subtraction hack (which amplifies prime jitter into the gen number at long prompts).
933pub static PRIME_NANOS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
934
935impl Engine {
936    pub fn new(ordinal: usize) -> Result<Self, Box<dyn std::error::Error>> {
937        let gpu = memra_runtime::Gpu::new(ordinal)?;
938        // ARCH GUARD (unified dual-arch engine): the fatbins carry single-arch SASS, so a
939        // binary/device mismatch otherwise dies at first module load with an opaque CUDA
940        // error. Fail early with the rebuild hint instead. MEMRA_ARCH_CHECK=0 skips.
941        if std::env::var("MEMRA_ARCH_CHECK").as_deref() != Ok("0") {
942            use cudarc::driver::sys::CUdevice_attribute_enum as A;
943            let (maj, min) = cudarc::driver::result::device::get(ordinal as i32)
944                .and_then(|d| unsafe { Ok((
945                    cudarc::driver::result::device::get_attribute(d, A::CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR)?,
946                    cudarc::driver::result::device::get_attribute(d, A::CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR)?)) })
947                .unwrap_or((0, 0));
948            let built = env!("MEMRA_BUILT_CUDA_ARCH");
949            let ok = matches!((built, maj, min),
950                ("120a", 12, 0) | ("120a", 12, 1) | ("100a", 10, 0) | ("90a", 9, 0) | ("89", 8, 9));
951            if !ok {
952                return Err(format!(
953                    "memra was built for sm_{built} but device {ordinal} reports compute \
954                     capability {maj}.{min}. Rebuild on this machine (MEMRA_CUDA_ARCH \
955                     auto-detects the GPU) or set MEMRA_ARCH_CHECK=0 to bypass.").into());
956            }
957        }
958        // Default async-pool RELEASE_THRESHOLD is 0: freed blocks return to the OS at every
959        // sync, so cuMemAllocAsync NODES inside captured graphs re-map memory on EVERY
960        // cuGraphLaunch (measured 226us/launch on the gemma graph door, 2026-07-23 osrt).
961        // Pinning the threshold keeps the pool cached -> alloc nodes become pointer bumps.
962        unsafe {
963            use cudarc::driver::sys;
964            let dev: sys::CUdevice = ordinal as sys::CUdevice;
965            let mut pool: sys::CUmemoryPool = std::ptr::null_mut();
966            if sys::cuDeviceGetDefaultMemPool(&mut pool, dev) == sys::CUresult::CUDA_SUCCESS {
967                let mut thresh: u64 = u64::MAX;
968                let _ = sys::cuMemPoolSetAttribute(
969                    pool,
970                    sys::CUmemPool_attribute_enum::CU_MEMPOOL_ATTR_RELEASE_THRESHOLD,
971                    &mut thresh as *mut u64 as *mut core::ffi::c_void,
972                );
973            }
974        }
975        let module = gpu.ctx.load_module(Ptx::from_binary(FATBIN.to_vec()))?;
976        let hybrid = gpu.ctx.load_module(Ptx::from_binary(HYBRID_FATBIN.to_vec()))?;
977        let qmatvec = gpu.ctx.load_module(Ptx::from_binary(QMATVEC_FATBIN.to_vec()))?;
978        let flash = gpu.ctx.load_module(Ptx::from_binary(flash_fatbin_bytes().to_vec()))?;
979        let gemm = gpu.ctx.load_module(Ptx::from_binary(gemm_fatbin_bytes().into_owned()))?;
980        let router = gpu.ctx.load_module(Ptx::from_binary(ROUTER_FATBIN.to_vec()))?;
981        let sample = gpu.ctx.load_module(Ptx::from_binary(SAMPLE_FATBIN.to_vec()))?;
982        let copy_stream = gpu.ctx.new_stream()?;
983        // DECODE EVENT-TRACKING ELISION — DEFAULT ON (2026-07-05; MEMRA_EVT=1 = escape hatch).
984        // cudarc is in multi-stream mode (main stream +
985        // copy_stream are both created streams), so with tracking on EVERY launch arg records a
986        // read/write CudaEvent and inserts cuStreamWaitEvent on prior events. On the 35B MoE decode
987        // that is ~19k cuStreamWaitEvent + ~9k cuEventRecord + ~6k event create/destroy per token
988        // (~7 ms/tok host time, measured nsys 2026-07-04 g7e), and +4.6% measured on 27B decode —
989        // protecting NOTHING: every hot-path kernel/memcpy runs on the ONE gpu.stream.
990        // CROSS-STREAM HAZARD AUDIT: MoeSlotCache in-memory prefetch uses copy_stream. Every
991        // overwrite explicitly records the prior compute point and makes copy_stream wait; every
992        // consumer explicitly waits for the copy completion event. The opt-in positioned-read
993        // proof stays on gpu.stream and retains an explicit event solely to guard pinned-source
994        // reuse. Graph-capture sites use only gpu.stream, so these handoffs never rely on cudarc's
995        // implicit event tracking.
996        // SAFETY: single-stream ordering is total; the runtime mem-pool is configured with
997        // internal-dependency reuse (memra-runtime), so alloc reuse is stream-ordered too.
998        if std::env::var("MEMRA_EVT").map(|v| v == "1").unwrap_or(false) {
999            // escape hatch: keep cudarc's implicit cross-stream event tracking.
1000        } else {
1001            unsafe { gpu.ctx.disable_event_tracking(); }
1002        }
1003        Ok(Self { gpu, module, hybrid, qmatvec, flash, flash_g: std::sync::OnceLock::new(), gemm, router, sample,
1004                  moe_cache: Mutex::new(None),
1005                  moe_cache_layout: Mutex::new(None),
1006                  copy_stream,
1007                  capture_keep_on: std::sync::atomic::AtomicBool::new(false),
1008                  verify_exact: std::sync::atomic::AtomicBool::new(false),
1009                  capture_keep: Mutex::new(Vec::new()),
1010                  argmax_partials: Mutex::new(None),
1011                  prime_deqw_ws: Mutex::new(None),
1012                  router_stage: Mutex::new(None),
1013                  fp8_scratch: Mutex::new(None),
1014                  fa_vf16_scratch: Mutex::new(None),
1015                  fa_part_pool: Mutex::new(None),
1016                  fa_part_retired: Mutex::new(Vec::new()),
1017                  fn_cache: Mutex::new(Default::default()),
1018                  f16_scratch: Mutex::new(None),
1019                  #[cfg(memra_cutlass)]
1020                  cutlass_scratch: Mutex::new(None) })
1021    }
1022
1023    pub fn ctx(&self) -> &Arc<CudaContext> { &self.gpu.ctx }
1024
1025    /// Bytes the async pool holds MAPPED but NOT LIVE (reserved - used), i.e. freed blocks
1026    /// parked in the pool because `Engine::new` pins RELEASE_THRESHOLD to u64::MAX above.
1027    ///
1028    /// Why this is a public engine surface: `mem_get_info`'s `free` DOES NOT SEE these bytes —
1029    /// they are mapped to this process, so `free` counts them as gone, yet the very next
1030    /// `alloc_u8` is satisfied from them without touching `free` at all. Any admission or
1031    /// budget decision that reads `free` alone therefore under-counts real headroom by exactly
1032    /// this amount. Effective allocatable headroom is `free + pool_cached_bytes()`.
1033    ///
1034    /// MEASURED SIZE (c=64 serve burst, 9B NVFP4 + draft, 24GB card, 2026-08-06): 34-89 MB
1035    /// during the burst — SMALL. The admission gate adds it because a term that can only ever
1036    /// under-count headroom does not belong in a gate that queues real work, but the honest
1037    /// reading of this number is that pool caching is NOT where a long-running server's VRAM
1038    /// hides on this path: reserved ~= used throughout, so the memory the driver reports as
1039    /// gone is genuinely LIVE (see `pool_reserved_used` for the diagnostic pair).
1040    ///
1041    /// Returns 0 if the pool cannot be queried (never a false-positive headroom claim).
1042    pub fn pool_cached_bytes(&self) -> usize {
1043        let (reserved, used) = self.pool_reserved_used();
1044        reserved.saturating_sub(used)
1045    }
1046
1047    /// Raw async-pool occupancy: (RESERVED_MEM_CURRENT, USED_MEM_CURRENT) in bytes. Reserved is
1048    /// what the pool has mapped from the driver; used is what is live inside it. Exposed for
1049    /// admission/VRAM diagnostics — the pair distinguishes "memory is parked in the pool and
1050    /// `free` cannot see it" (reserved >> used) from "memory is genuinely held live by some
1051    /// owner" (reserved ~= used), which are opposite bugs with opposite fixes.
1052    /// (0, 0) if the pool cannot be queried.
1053    pub fn pool_reserved_used(&self) -> (usize, usize) {
1054        use cudarc::driver::sys;
1055        unsafe {
1056            let mut pool: sys::CUmemoryPool = std::ptr::null_mut();
1057            if sys::cuDeviceGetDefaultMemPool(&mut pool, self.gpu.ctx.ordinal() as sys::CUdevice)
1058                != sys::CUresult::CUDA_SUCCESS
1059            {
1060                return (0, 0);
1061            }
1062            let (mut reserved, mut used) = (0u64, 0u64);
1063            if sys::cuMemPoolGetAttribute(
1064                pool,
1065                sys::CUmemPool_attribute_enum::CU_MEMPOOL_ATTR_RESERVED_MEM_CURRENT,
1066                &mut reserved as *mut u64 as *mut core::ffi::c_void,
1067            ) != sys::CUresult::CUDA_SUCCESS {
1068                return (0, 0);
1069            }
1070            if sys::cuMemPoolGetAttribute(
1071                pool,
1072                sys::CUmemPool_attribute_enum::CU_MEMPOOL_ATTR_USED_MEM_CURRENT,
1073                &mut used as *mut u64 as *mut core::ffi::c_void,
1074            ) != sys::CUresult::CUDA_SUCCESS {
1075                return (0, 0);
1076            }
1077            (reserved as usize, used as usize)
1078        }
1079    }
1080
1081    /// Ambient stream (by value since M1-PP2 increment 2): the thread's pp2 stage stream
1082    /// when a stage scope is active, else the main compute stream — see `Gpu::stream`.
1083    pub fn stream(&self) -> Arc<CudaStream> { self.gpu.stream() }
1084    /// FP8-GLOBALS switch (MEMRA_GEMMA_GKV, default ON): gemma global (hd512) layers keep
1085    /// their KV in e4m3 — the dequant-latency arc (HANDOVER). Windowed layers stay q8_0/q5_1.
1086    pub fn gkv_on() -> bool {
1087        memra_kv::gkv_on()
1088    }
1089
1090    /// FP8-WINDOWED switch (MEMRA_GEMMA_WKV — measured 2026-07-12 in a validity-gated
1091    /// window: 1.7k 174.1-174.4 vs 168.6-169.4 default (+3%), 4.9k 158.7-160.4; vs llama
1092    /// same-window 159.5-160.2 / 140.6 = 1.09x / 1.13x): gemma windowed (hd256 SWA)
1093    /// layers hold e4m3 KV and ride the format-aware v4 lane from the kf8vf8 module.
1094    /// SERVING-MODE DEFAULT (2026-07-12, the 31B spec unlock): fp8-windowed KV GUTS the
1095    /// MTP drafter's acceptance — its single swa attention reads the windowed cache and
1096    /// e4m3 noise flips its argmaxes (31B short accept .758 -> 1.000 with q8/q5, spec 88
1097    /// -> 122.7 vs llama-mtp 112; depth .59 -> .78; 26B depth .57 -> .89). So the default
1098    /// keys on serving intent: SPEC serving (MEMRA_DRAFT set) -> OFF, plain -> ON (its
1099    /// depth-plain +3% stands). Explicit MEMRA_GEMMA_WKV always wins. GKV (globals) stays
1100    /// ON for both — no acceptance cost measured.
1101    pub fn wkv_on() -> bool {
1102        memra_kv::wkv_on()
1103    }
1104
1105    /// QWEN FP8-KV switch (MEMRA_KV_FP8 explicit; else the per-model KV_FP8_FORCE door set
1106    /// at model load; else OFF). Non-gemma full-attn layers hold e4m3 K/V via the kf8vf8
1107    /// module. Per-model verdict 2026-07-12: 9B +0.7-4% scaling with depth, 27B flat,
1108    /// 35B −2% (fp8 format-gates its v3 dp4a lane) — so the 9B class defaults ON
1109    /// (adopted 2026-07-28 with the deferred acceptance battery), others stay OFF.
1110    pub fn kv_fp8_on() -> bool {
1111        memra_kv::kv_fp8_on()
1112    }
1113
1114    /// fa kernel routed by head_dim: hd512 (gemma globals) resolves from the kf8vf8 module
1115    /// when the fp8-globals arm is on; everything else from the default flash module.
1116    fn fa_func(&self, name: &str, head_dim: usize) -> CudaFunction {
1117        if head_dim == 512 && Self::gkv_on() { self.func_g(name) } else { self.func(name) }
1118    }
1119
1120    /// Kernel from the FP8-GLOBALS (kf8vf8) flash module — gemma global-layer arm only.
1121    /// Format-AGNOSTIC kernels (e.g. fa_decode_combine_f32) are not compiled into the
1122    /// per-format fatbins; fall back to the base modules for those.
1123    fn func_g(&self, name: &str) -> CudaFunction {
1124        let m = self.flash_g.get_or_init(|| {
1125            self.gpu.ctx.load_module(cudarc::nvrtc::Ptx::from_binary(FLASH_FATBIN_KF8VF8.to_vec()))
1126                .expect("load kf8vf8 flash fatbin (fp8-globals arm)")
1127        });
1128        let key = format!("g:{name}");
1129        if let Some(f) = self.fn_cache.lock().unwrap().get(&key) { return f.clone(); }
1130        let f = match m.load_function(name) {
1131            Ok(f) => f,
1132            Err(_) => self.func(name),
1133        };
1134        self.fn_cache.lock().unwrap().insert(key, f.clone());
1135        f
1136    }
1137
1138    fn func(&self, name: &str) -> CudaFunction {
1139        // Resolution cache: cuModuleGetFunction fails inside a CUDA-graph capture region,
1140        // so capture-time lookups MUST be host-memory hits (warmups populate the cache).
1141        if let Some(f) = self.fn_cache.lock().unwrap().get(name) { return f.clone(); }
1142        let f = self.module.load_function(name)
1143            .or_else(|_| self.hybrid.load_function(name))
1144            .or_else(|_| self.qmatvec.load_function(name))
1145            .or_else(|_| self.flash.load_function(name))
1146            .or_else(|_| self.gemm.load_function(name))
1147            .or_else(|_| self.router.load_function(name))
1148            .or_else(|_| self.sample.load_function(name))
1149            .unwrap_or_else(|_| panic!("kernel {name} not in any fatbin"));
1150        self.fn_cache.lock().unwrap().insert(name.to_string(), f.clone());
1151        f
1152    }
1153
1154    /// Scatter trimmed draft logits into full-vocab space: dst = -inf everywhere, then
1155    /// dst[d2t[i]] = src[i]. Two launches (fill, scatter) — no grid-wide sync needed.
1156    pub fn scatter_trim_logits(&self, src: &CudaSlice<f32>, d2t: &CudaSlice<u32>,
1157                               dst: &mut CudaSlice<f32>, d_vocab: usize, n_vocab: usize)
1158                               -> Result<(), Box<dyn std::error::Error>> {
1159        let f1 = self.func("scatter_trim_logits_f32");
1160        let f2 = self.func("scatter_trim_logits_pass2_f32");
1161        let (dv, nv) = (d_vocab as i32, n_vocab as i32);
1162        let cfg1 = LaunchConfig { grid_dim: (256, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
1163        let __s_b1 = self.gpu.stream();
1164        let mut b1 = __s_b1.launch_builder(&f1);
1165        b1.arg(src).arg(d2t).arg(&mut *dst).arg(&dv).arg(&nv);
1166        unsafe { b1.launch(cfg1)?; }
1167        let cfg2 = LaunchConfig { grid_dim: (d_vocab.div_ceil(256) as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
1168        let __s_b2 = self.gpu.stream();
1169        let mut b2 = __s_b2.launch_builder(&f2);
1170        b2.arg(src).arg(d2t).arg(&mut *dst).arg(&dv);
1171        unsafe { b2.launch(cfg2)?; }
1172        Ok(())
1173    }
1174
1175    // ---- FILTERED-SPEC (feat/filtered-spec): top-k/p/min-p transforms applied symmetrically
1176    // to p and q — rejection sampling stays distribution-exact for the filtered target. ----
1177
1178    /// Per-row filtered-softmax stats: out[r] = (threshold_e, renorm_mass_e, row_max) for the
1179    /// filter (top_k, top_p, min_p) at `temp`. Rows index into x with row_stride f32s.
1180    #[allow(clippy::too_many_arguments)]
1181    pub fn filter_stats(&self, x: &CudaSlice<f32>, row_stride: usize, rows: &CudaSlice<i32>,
1182                        out_th: &mut CudaSlice<f32>, out_z: &mut CudaSlice<f32>,
1183                        out_max: &mut CudaSlice<f32>, n: usize, nrow: usize,
1184                        temp: f32, top_k: i32, top_p: f32, min_p: f32)
1185                        -> Result<(), Box<dyn std::error::Error>> {
1186        let f = self.func("filter_stats_f32");
1187        let (ni, nr, rs) = (n as i32, nrow as i32, row_stride as i64);
1188        let cfg = LaunchConfig { grid_dim: (nrow as u32, 1, 1), block_dim: (1024, 1, 1), shared_mem_bytes: 0 };
1189        let __s_b = self.gpu.stream();
1190        let mut b = __s_b.launch_builder(&f);
1191        b.arg(x).arg(&rs).arg(rows).arg(&mut *out_th).arg(&mut *out_z).arg(&mut *out_max)
1192         .arg(&ni).arg(&nr).arg(&temp).arg(&top_k).arg(&top_p).arg(&min_p);
1193        unsafe { b.launch(cfg)?; }
1194        Ok(())
1195    }
1196
1197    /// out[pair] = filtered-softmax prob of ids[pair] in row rows[pair] (th/z per PAIR).
1198    #[allow(clippy::too_many_arguments)]
1199    pub fn softmax_gather_filtered(&self, x: &CudaSlice<f32>, row_stride: usize,
1200                                   ids: &CudaSlice<u32>, rows: &CudaSlice<i32>,
1201                                   th: &CudaSlice<f32>, z: &CudaSlice<f32>,
1202                                   out: &mut CudaSlice<f32>, n: usize, npair: usize, temp: f32)
1203                                   -> Result<(), Box<dyn std::error::Error>> {
1204        let f = self.func("softmax_gather_filtered_f32");
1205        let (ni, np, rs) = (n as i32, npair as i32, row_stride as i64);
1206        let cfg = LaunchConfig { grid_dim: (npair as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
1207        let __s_b = self.gpu.stream();
1208        let mut b = __s_b.launch_builder(&f);
1209        b.arg(x).arg(&rs).arg(ids).arg(rows).arg(th).arg(z).arg(&mut *out).arg(&ni).arg(&np).arg(&temp);
1210        unsafe { b.launch(cfg)?; }
1211        Ok(())
1212    }
1213
1214    /// Filtered residual sample: token ~ norm(max(0, fp - fq)) with fp/fq the filtered softmaxes.
1215    #[allow(clippy::too_many_arguments)]
1216    pub fn residual_sample_filtered(&self, p: &CudaSlice<f32>, q: Option<&CudaSlice<f32>>, n: usize,
1217                                    temp: f32, seed: u64, stream_pos: u32,
1218                                    p_stats: (f32, f32, f32), q_stats: (f32, f32, f32),
1219                                    out_tok: &mut CudaSlice<u32>)
1220                                    -> Result<(), Box<dyn std::error::Error>> {
1221        let f = self.func("residual_sample_filtered_f32");
1222        let (ni, slo, shi) = (n as i32, (seed & 0xFFFF_FFFF) as u32, (seed >> 32) as u32);
1223        let has_q: i32 = q.is_some() as i32;
1224        let qbuf = q.unwrap_or(p);
1225        let (pm, pth, pz) = p_stats; let (qm, qth, qz) = q_stats;
1226        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (1024, 1, 1), shared_mem_bytes: 0 };
1227        let __s_b = self.gpu.stream();
1228        let mut b = __s_b.launch_builder(&f);
1229        b.arg(p).arg(qbuf).arg(&has_q).arg(&ni).arg(&temp).arg(&slo).arg(&shi).arg(&stream_pos)
1230         .arg(&pm).arg(&pth).arg(&pz).arg(&qm).arg(&qth).arg(&qz).arg(&mut *out_tok);
1231        unsafe { b.launch(cfg)?; }
1232        Ok(())
1233    }
1234
1235    /// Gumbel-max draw from the FILTERED distribution (masked perturb; argmax after).
1236    #[allow(clippy::too_many_arguments)]
1237    pub fn gumbel_perturb_filtered(&self, x: &CudaSlice<f32>, y: &mut CudaSlice<f32>, n: usize,
1238                                   seed: u64, stream_pos: u32, temp: f32, row_max: f32, th: f32)
1239                                   -> Result<(), Box<dyn std::error::Error>> {
1240        let f = self.func("gumbel_perturb_filtered_f32");
1241        let (ni, slo, shi) = (n as i32, (seed & 0xFFFF_FFFF) as u32, (seed >> 32) as u32);
1242        let cfg = LaunchConfig { grid_dim: (n.div_ceil(256) as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
1243        let __s_b = self.gpu.stream();
1244        let mut b = __s_b.launch_builder(&f);
1245        b.arg(x).arg(&mut *y).arg(&ni).arg(&slo).arg(&shi).arg(&stream_pos).arg(&temp).arg(&row_max).arg(&th);
1246        unsafe { b.launch(cfg)?; }
1247        Ok(())
1248    }
1249
1250    /// Keskar penalties applied IN PLACE to a logits buffer: history token ids get
1251    /// rep-divided/multiplied + freq*count + presence subtracted. Symmetric p/q usage keeps
1252    /// filtered rejection sampling exact for the penalized target.
1253    #[allow(clippy::too_many_arguments)]
1254    pub fn penalize_logits(&self, x: &mut CudaSlice<f32>, hist: &CudaSlice<u32>, n_hist: usize,
1255                           rep: f32, freq: f32, present: f32, n: usize)
1256                           -> Result<(), Box<dyn std::error::Error>> {
1257        if n_hist == 0 { return Ok(()); }
1258        let f = self.func("penalize_logits_f32");
1259        let (nh, ni) = (n_hist as i32, n as i32);
1260        let cfg = LaunchConfig { grid_dim: (n_hist.div_ceil(128) as u32, 1, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
1261        let __s_b = self.gpu.stream();
1262        let mut b = __s_b.launch_builder(&f);
1263        b.arg(&mut *x).arg(hist).arg(&nh).arg(&rep).arg(&freq).arg(&present).arg(&ni);
1264        unsafe { b.launch(cfg)?; }
1265        Ok(())
1266    }
1267
1268    /// Rows variant: penalize `nrow` contiguous rows of length n in one launch.
1269    #[allow(clippy::too_many_arguments)]
1270    pub fn penalize_logits_rows(&self, x: &mut CudaSlice<f32>, hist: &CudaSlice<u32>, n_hist: usize,
1271                                rep: f32, freq: f32, present: f32, n: usize, nrow: usize)
1272                                -> Result<(), Box<dyn std::error::Error>> {
1273        if n_hist == 0 || nrow == 0 { return Ok(()); }
1274        let f = self.func("penalize_logits_rows_f32");
1275        let (nh, ni, nr) = (n_hist as i32, n as i32, nrow as i32);
1276        let cfg = LaunchConfig { grid_dim: (n_hist.div_ceil(128) as u32, nrow as u32, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
1277        let __s_b = self.gpu.stream();
1278        let mut b = __s_b.launch_builder(&f);
1279        b.arg(&mut *x).arg(hist).arg(&nh).arg(&rep).arg(&freq).arg(&present).arg(&ni).arg(&nr);
1280        unsafe { b.launch(cfg)?; }
1281        Ok(())
1282    }
1283
1284    /// WEIGHT PREFETCH (SOTA item 3, 2026-07-13, DEFAULT ON): during a bandwidth-idle
1285    /// window (the fa launch reads KV, not weights) prefetch the NEXT matvec's
1286    /// decode-plane bytes into L2 so it reads L2-warm. Value-free scheduling op — same
1287    /// class as prefetch_l2 (numerics untouched by construction). Wired only where it
1288    /// measured positive: the E4B dc attn arm (+0.65%). 26B (flat — MoE ffn dominates),
1289    /// 31B (−0.2% — decode at the DRAM wall) and the ffn gate/up cascade (−1% — 29MB/layer
1290    /// floods the fill path) all probed and NOT wired. MEMRA_WPF=0 rollback seam.
1291    pub fn wpf_level() -> u32 {
1292        static ON: std::sync::OnceLock<u32> = std::sync::OnceLock::new();
1293        *ON.get_or_init(|| std::env::var("MEMRA_WPF").ok()
1294            .and_then(|v| v.parse().ok()).unwrap_or(1))
1295    }
1296
1297    /// PDL launch arm (SOTA item 2, 2026-07-13, DEFAULT ON): the six MEMRA_PDL_ENTRY glue
1298    /// kernels launch through cuLaunchKernelEx with PROGRAMMATIC_STREAM_SERIALIZATION — the
1299    /// grid launches while the predecessor drains (~120ns/kernel back, pdl_probe), the
1300    /// kernels' entry grid-dep sync restores read order (SASS-audited: ACQBULK precedes
1301    /// every LDG in all six). Valid windows: E4B +1.0-1.2% (128 AND 384-tok gens);
1302    /// 26B/31B/qwen flat no-harm. Battery: kernel-check GREEN, run-gen tokens IDENTICAL x3
1303    /// gemma, spec 64/64 E4B K=1/4/8 + 26B/31B K=4 + qwen PASS. Works eager AND under
1304    /// capture (capture encodes native programmatic edges — the post-capture edge-REWRITE
1305    /// arm died: engine graphs hold cuMemAllocAsync alloc nodes, edge edits on those return
1306    /// CUDA_ERROR_NOT_SUPPORTED). MEMRA_PDL=0 rollback seam.
1307    /// See the `verify_exact` field. Scoped by the dflash round around its t=16 verify.
1308    pub fn set_verify_exact(&self, on: bool) {
1309        self.verify_exact.store(on, std::sync::atomic::Ordering::Relaxed);
1310    }
1311    pub(crate) fn verify_exact_on(&self) -> bool {
1312        self.verify_exact.load(std::sync::atomic::Ordering::Relaxed)
1313    }
1314
1315    /// m=1 norm+rope+append fold seam (2026-07-23): MEMRA_QKV_APPEND=0 reverts to the
1316    /// fused-norm-rope + standalone-append pair (the exact-oracle bisect arm).
1317    pub fn qkv_append_on() -> bool {
1318        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
1319        *ON.get_or_init(|| std::env::var("MEMRA_QKV_APPEND").map(|v| v != "0").unwrap_or(true))
1320    }
1321
1322    /// PDL wave-B1a seam: the four dense-glue kernels (rms_norm_f32, add_rms_norm_f32,
1323    /// add_scale_rms_norm_q8_1, quantize_q8_1). MEMRA_PDL_WB=0 reverts alone.
1324    pub fn pdl_wb_on() -> bool {
1325        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
1326        *ON.get_or_init(|| std::env::var("MEMRA_PDL_WB").map(|v| v != "0").unwrap_or(true))
1327    }
1328
1329    /// PDL wave-A seam: the mmvq matvec PDL launches only (the six glue kernels keep
1330    /// their own MEMRA_PDL master seam). MEMRA_PDL_MMVQ=0 reverts wave-A alone — the
1331    /// per-model no-harm bisect knob.
1332    pub fn pdl_mmvq_on() -> bool {
1333        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
1334        *ON.get_or_init(|| std::env::var("MEMRA_PDL_MMVQ").map(|v| v != "0").unwrap_or(true))
1335    }
1336
1337    pub fn pdl_on() -> bool {
1338        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
1339        *ON.get_or_init(|| std::env::var("MEMRA_PDL").map(|v| v != "0").unwrap_or(true))
1340    }
1341
1342    /// Raw CUfunction for a PDL-attributed launch: the SAME kernels.fatbin loaded once more
1343    /// through the raw driver API (cudarc hides its CUfunction handles; a duplicate module
1344    /// of tiny glue kernels is free). Resolved lazily per name, cached process-wide.
1345    /// Fused t=1 q4_0 mr policy: env MEMRA_Q40_MR wins (1/2); else the per-model
1346    /// FUSED_MR1_DEFAULT (dense gemma = mr1, MoE = mr2 — see the static's doc).
1347    fn q40_mr1_on() -> bool {
1348        static Q40MR: std::sync::OnceLock<Option<u32>> = std::sync::OnceLock::new();
1349        match *Q40MR.get_or_init(|| std::env::var("MEMRA_Q40_MR").ok()
1350            .and_then(|v| v.parse().ok())) {
1351            Some(v) => v == 1,
1352            None => crate::FUSED_MR1_DEFAULT.load(std::sync::atomic::Ordering::Relaxed),
1353        }
1354    }
1355
1356    /// PDL wave-B2: flash-module PDL functions. `g` selects the kf8vf8 flavor — the
1357    /// caller MUST pass the SAME flavor its builder launch would resolve (fa_func/func_g
1358    /// mirror); the flavors differ semantically (KV byte formats), a wrong-module launch
1359    /// writes wrong bytes silently.
1360    fn pdl_func_flash(&self, g: bool, name: &'static str)
1361        -> Result<cudarc::driver::sys::CUfunction, Box<dyn std::error::Error>> {
1362        use cudarc::driver::sys as cu;
1363        // PER-CONTEXT caches (M1-PP2 cross-device fix, 8x box 2026-08-02): CUmodule and
1364        // CUfunction handles are CONTEXT-scoped, and a remote-stage Engine
1365        // (MEMRA_PP_DEVICES=a,b) lives in the other device's primary context. The old
1366        // process-wide OnceLock cache handed stage 1 the dev-a handles, so every stage-1
1367        // launch_pdl* died CUDA_ERROR_INVALID_HANDLE. Key module + function caches by
1368        // this engine's CUcontext; single-context runs behave exactly as before.
1369        static MODS: std::sync::Mutex<Option<std::collections::HashMap<(usize, bool), usize>>> =
1370            std::sync::Mutex::new(None);
1371        static FNS: std::sync::Mutex<Option<std::collections::HashMap<(usize, bool, &'static str), usize>>> =
1372            std::sync::Mutex::new(None);
1373        let ctx_key = self.ctx().cu_ctx() as usize;
1374        if let Some(&f) = FNS.lock().unwrap().get_or_insert_with(Default::default)
1375            .get(&(ctx_key, g, name)) { return Ok(f as cu::CUfunction); }
1376        let module = {
1377            let mut mods = MODS.lock().unwrap();
1378            let map = mods.get_or_insert_with(Default::default);
1379            match map.get(&(ctx_key, g)) {
1380                Some(&m) => m,
1381                None => {
1382                    let m = self.pdl_load_module_in_ctx(
1383                        if g { FLASH_FATBIN_KF8VF8 } else { FLASH_FATBIN })?;
1384                    map.insert((ctx_key, g), m);
1385                    m
1386                }
1387            }
1388        };
1389        let cname = std::ffi::CString::new(name)?;
1390        let mut f: cu::CUfunction = std::ptr::null_mut();
1391        let r = unsafe { cu::cuModuleGetFunction(&mut f, module as cu::CUmodule, cname.as_ptr()) };
1392        if r != cu::CUresult::CUDA_SUCCESS { return Err(format!("pdl_func_flash {name} (g={g}): {r:?}").into()); }
1393        FNS.lock().unwrap().get_or_insert_with(Default::default)
1394            .insert((ctx_key, g, name), f as usize);
1395        Ok(f)
1396    }
1397
1398    /// Load a fatbin as a raw CUmodule IN THIS ENGINE'S CONTEXT. `cuModuleLoadData` binds
1399    /// the module to the thread's CURRENT context — a remote-stage engine must not
1400    /// inherit the primary's (the INVALID_HANDLE class above). Restores the caller's
1401    /// current context before returning.
1402    fn pdl_load_module_in_ctx(&self, bytes: &[u8]) -> Result<usize, Box<dyn std::error::Error>> {
1403        use cudarc::driver::sys as cu;
1404        let mut prev: cu::CUcontext = std::ptr::null_mut();
1405        unsafe { cu::cuCtxGetCurrent(&mut prev).result()?; }
1406        self.ctx().bind_to_thread()?;
1407        let mut m: cu::CUmodule = std::ptr::null_mut();
1408        let r = unsafe { cu::cuModuleLoadData(&mut m, bytes.as_ptr() as *const std::ffi::c_void) };
1409        let restore = if prev.is_null() { cu::CUresult::CUDA_SUCCESS }
1410                      else { unsafe { cu::cuCtxSetCurrent(prev) } };
1411        if r != cu::CUresult::CUDA_SUCCESS {
1412            return Err(format!("pdl module load: {r:?}").into());
1413        }
1414        if restore != cu::CUresult::CUDA_SUCCESS {
1415            return Err(format!("pdl module load: ctx restore {restore:?}").into());
1416        }
1417        Ok(m as usize)
1418    }
1419
1420    fn pdl_func(&self, name: &'static str) -> Result<cudarc::driver::sys::CUfunction, Box<dyn std::error::Error>> {
1421        use cudarc::driver::sys as cu;
1422        // PER-CONTEXT caches — same M1-PP2 cross-device fix as pdl_func_flash (handles
1423        // are context-scoped; key everything by this engine's CUcontext).
1424        static MODULES: std::sync::Mutex<Option<std::collections::HashMap<usize, usize>>> =
1425            std::sync::Mutex::new(None);
1426        // PDL wave-A: the mmvq kernels live in the qmatvec fatbin, not kernels.cu — second
1427        // duplicate module, loaded lazily on the first kernels-module miss.
1428        static QMODULES: std::sync::Mutex<Option<std::collections::HashMap<usize, usize>>> =
1429            std::sync::Mutex::new(None);
1430        static FNS: std::sync::Mutex<Option<std::collections::HashMap<(usize, &'static str), usize>>> =
1431            std::sync::Mutex::new(None);
1432        let ctx_key = self.ctx().cu_ctx() as usize;
1433        if let Some(&f) = FNS.lock().unwrap().get_or_insert_with(Default::default)
1434            .get(&(ctx_key, name)) { return Ok(f as cu::CUfunction); }
1435        let module = {
1436            let mut mods = MODULES.lock().unwrap();
1437            let map = mods.get_or_insert_with(Default::default);
1438            match map.get(&ctx_key) {
1439                Some(&m) => m,
1440                None => {
1441                    let m = self.pdl_load_module_in_ctx(FATBIN)?;
1442                    map.insert(ctx_key, m);
1443                    m
1444                }
1445            }
1446        };
1447        let cname = std::ffi::CString::new(name)?;
1448        let mut f: cu::CUfunction = std::ptr::null_mut();
1449        let mut r = unsafe { cu::cuModuleGetFunction(&mut f, module as cu::CUmodule, cname.as_ptr()) };
1450        if r == cu::CUresult::CUDA_ERROR_NOT_FOUND {
1451            let qmodule = {
1452                let mut mods = QMODULES.lock().unwrap();
1453                let map = mods.get_or_insert_with(Default::default);
1454                match map.get(&ctx_key) {
1455                    Some(&m) => m,
1456                    None => {
1457                        let m = self.pdl_load_module_in_ctx(QMATVEC_FATBIN)?;
1458                        map.insert(ctx_key, m);
1459                        m
1460                    }
1461                }
1462            };
1463            r = unsafe { cu::cuModuleGetFunction(&mut f, qmodule as cu::CUmodule, cname.as_ptr()) };
1464        }
1465        if r != cu::CUresult::CUDA_SUCCESS { return Err(format!("pdl_func {name}: {r:?}").into()); }
1466        FNS.lock().unwrap().get_or_insert_with(Default::default)
1467            .insert((ctx_key, name), f as usize);
1468        Ok(f)
1469    }
1470
1471    /// cuLaunchKernelEx with CU_LAUNCH_ATTRIBUTE_PROGRAMMATIC_STREAM_SERIALIZATION on the
1472    /// compute stream. ONLY legal for kernels whose entry carries MEMRA_PDL_ENTRY.
1473    ///
1474    /// # Safety
1475    /// `params` must match the kernel's exact parameter list (order, types, count) —
1476    /// a mismatch corrupts the launch silently.
1477    /// Flash-module twin of `launch_pdl` — `g` picks the kf8vf8 flavor (must mirror the
1478    /// builder path's fa_func/func_g choice exactly).
1479    ///
1480    /// # Safety
1481    /// Same contract as `launch_pdl`.
1482    unsafe fn launch_pdl_flash(&self, g: bool, name: &'static str, grid: (u32, u32, u32),
1483                               block: (u32, u32, u32), smem: u32,
1484                               params: &mut [*mut std::ffi::c_void])
1485                               -> Result<(), Box<dyn std::error::Error>> {
1486        use cudarc::driver::sys as cu;
1487        let f = self.pdl_func_flash(g, name)?;
1488        if smem > 0 {
1489            // mirror the builder path's opt-in ceiling (idempotent host-side set).
1490            let r = unsafe { cu::cuFuncSetAttribute(f,
1491                cu::CUfunction_attribute_enum::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES,
1492                smem as i32) };
1493            if r != cu::CUresult::CUDA_SUCCESS {
1494                return Err(format!("pdl smem attr {name}: {r:?}").into());
1495            }
1496        }
1497        let mut attr = cu::CUlaunchAttribute {
1498            id: cu::CUlaunchAttributeID::CU_LAUNCH_ATTRIBUTE_PROGRAMMATIC_STREAM_SERIALIZATION,
1499            pad: [0; 4],
1500            value: cu::CUlaunchAttributeValue { programmaticStreamSerializationAllowed: 1 },
1501        };
1502        let cfg = cu::CUlaunchConfig {
1503            gridDimX: grid.0, gridDimY: grid.1, gridDimZ: grid.2,
1504            blockDimX: block.0, blockDimY: block.1, blockDimZ: block.2,
1505            sharedMemBytes: smem, hStream: self.gpu.stream().cu_stream(),
1506            attrs: &mut attr, numAttrs: 1,
1507        };
1508        let r = unsafe { cu::cuLaunchKernelEx(&cfg, f, params.as_mut_ptr(), std::ptr::null_mut()) };
1509        if r != cu::CUresult::CUDA_SUCCESS { return Err(format!("launch_pdl_flash {name}: {r:?}").into()); }
1510        Ok(())
1511    }
1512
1513    unsafe fn launch_pdl(&self, name: &'static str, grid: (u32, u32, u32), block: (u32, u32, u32),
1514                         params: &mut [*mut std::ffi::c_void])
1515                         -> Result<(), Box<dyn std::error::Error>> {
1516        use cudarc::driver::sys as cu;
1517        let f = self.pdl_func(name)?;
1518        let mut attr = cu::CUlaunchAttribute {
1519            id: cu::CUlaunchAttributeID::CU_LAUNCH_ATTRIBUTE_PROGRAMMATIC_STREAM_SERIALIZATION,
1520            pad: [0; 4],
1521            value: cu::CUlaunchAttributeValue { programmaticStreamSerializationAllowed: 1 },
1522        };
1523        let cfg = cu::CUlaunchConfig {
1524            gridDimX: grid.0, gridDimY: grid.1, gridDimZ: grid.2,
1525            blockDimX: block.0, blockDimY: block.1, blockDimZ: block.2,
1526            sharedMemBytes: 0, hStream: self.gpu.stream().cu_stream(),
1527            attrs: &mut attr, numAttrs: 1,
1528        };
1529        let r = unsafe { cu::cuLaunchKernelEx(&cfg, f, params.as_mut_ptr(), std::ptr::null_mut()) };
1530        if r != cu::CUresult::CUDA_SUCCESS { return Err(format!("launch_pdl {name}: {r:?}").into()); }
1531        Ok(())
1532    }
1533
1534    /// L2-prefetch a quant weight's DECODE plane (the rp4 split-plane mirror when present —
1535    /// that is what the m<=8 dispatch reads — else the raw block bytes). No-op on float arms.
1536    pub fn prefetch_weight_l2(&self, w: &crate::model::GpuTensor)
1537                              -> Result<(), Box<dyn std::error::Error>> {
1538        if let crate::model::GpuTensor::Quant { bytes, rp4, .. } = w {
1539            let p = rp4.as_ref().unwrap_or(bytes);
1540            self.prefetch_l2(p, p.len())?;
1541        }
1542        Ok(())
1543    }
1544
1545    /// DSpark markov chain ops (dflash lane): gather one bf16 row of a [V, rank] table
1546    /// by the DEVICE token id at tok[idx] into f32.
1547    pub fn gather_row_bf16(&self, table: &CudaSlice<u8>, tok: &CudaSlice<u32>, idx: usize,
1548                           dst: &mut CudaSlice<f32>, ncols: usize)
1549                           -> Result<(), Box<dyn std::error::Error>> {
1550        let f = self.func("gather_row_bf16_f32");
1551        let cfg = LaunchConfig { grid_dim: (ncols.div_ceil(256) as u32, 1, 1),
1552                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
1553        let (nc, ix) = (ncols as i32, idx as i32);
1554        let __s_b = self.gpu.stream();
1555        let mut b = __s_b.launch_builder(&f);
1556        b.arg(table).arg(tok).arg(&ix).arg(dst).arg(&nc);
1557        unsafe { b.launch(cfg)?; }
1558        Ok(())
1559    }
1560
1561    /// logits[row_off .. row_off+n] += bias[0..n] (in place, one row).
1562    pub fn add_row_inplace(&self, logits: &mut CudaSlice<f32>, bias: &CudaSlice<f32>,
1563                           n: usize, row_off: usize)
1564                           -> Result<(), Box<dyn std::error::Error>> {
1565        let f = self.func("add_row_inplace_f32");
1566        let cfg = LaunchConfig { grid_dim: (n.div_ceil(256) as u32, 1, 1),
1567                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
1568        let (ni, off) = (n as i32, row_off as i64);
1569        let __s_b = self.gpu.stream();
1570        let mut b = __s_b.launch_builder(&f);
1571        b.arg(logits).arg(bias).arg(&ni).arg(&off);
1572        unsafe { b.launch(cfg)?; }
1573        Ok(())
1574    }
1575
1576    /// L2 prefetch of a device byte range (latency-hiding arc; value-free scheduling op).
1577    pub fn prefetch_l2(&self, p: &CudaSlice<u8>, n: usize) -> Result<(), Box<dyn std::error::Error>> {
1578        let f = self.func("prefetch_l2_bytes");
1579        let lines = n.div_ceil(128);
1580        let ni = n as i64;
1581        let cfg = LaunchConfig { grid_dim: (lines.div_ceil(256) as u32, 1, 1),
1582                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
1583        let __s_b = self.gpu.stream();
1584        let mut b = __s_b.launch_builder(&f);
1585        b.arg(p).arg(&ni);
1586        unsafe { b.launch(cfg)?; }
1587        Ok(())
1588    }
1589
1590    /// MoE router GEMV (MEMRA_ROUTER_KERNEL): deterministic warp-per-(expert,token) f32 dot.
1591    /// Different FP order than the cuBLAS path it replaces — battery-gated numeric config.
1592    pub fn router_gemv(&self, w: &CudaSlice<f32>, x: &CudaSlice<f32>, n_embd: usize,
1593                       n_experts: usize, t: usize)
1594                       -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1595        // float4 v2 probed 2026-07-14: +0.25% but flips near-tie routing (new FP order,
1596        // stream differs) — too small to justify a numeric config change; deleted.
1597        // w8 twin (2026-07-31): on the 132-SM H100 the lone-warp form is 14.8% of the q35
1598        // decode step (latency-bound) — the calculus flipped. MEMRA_ROUTER_V2=0 reverts to
1599        // the warp form (rollback seam; new FP order, battery-arbitrated per model).
1600        let w8 = match std::env::var("MEMRA_ROUTER_V2").as_deref() {
1601            Ok("0") => false,
1602            Ok(_) => true,
1603            Err(_) => ROUTER_W8_DEFAULT.load(std::sync::atomic::Ordering::Relaxed),
1604        };
1605        // FAST-ROUTER batch twin (lane/fast-router, 2026-08-02): at prefill m the per-(e,tok)
1606        // w8 form re-streams both operand rows per output (GEMV program at GEMM shape — the
1607        // concat-prime exactness fix paid -10% q35 board-2048 prefill through it). The batch
1608        // twin (8x8 expert-x-token register tile) is BIT-IDENTICAL per row (same k order,
1609        // same tree, same fold — kernel-check sweeps m=1..2048 on real router weights), so
1610        // the crossover is pure perf, not a numeric config. MIN_T from the on-box sweep
1611        // (research/fast-router-20260802/crossover-router*.jsonl); decode t=1 and small-t
1612        // spec verify keep the plain w8 form. MEMRA_ROUTER_BATCH=0: rollback seam
1613        // (perf-only, bits equal).
1614        let batch = w8 && t >= ROUTER_BATCH_MIN_T && router_batch_on();
1615        self.router_gemv_form(w, x, n_embd, n_experts, t, w8, batch)
1616    }
1617
1618    /// Form-explicit router GEMV launch (kernel-check bit-identity gate + crossover bench
1619    /// force both forms; `batch` requires `w8`).
1620    pub fn router_gemv_form(&self, w: &CudaSlice<f32>, x: &CudaSlice<f32>, n_embd: usize,
1621                            n_experts: usize, t: usize, w8: bool, batch: bool)
1622                            -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1623        debug_assert!(!batch || w8, "batch twin exists for the w8 form only");
1624        let mut y = self.alloc_uninit::<f32>(t * n_experts)?;
1625        let f = if batch { self.func("router_gemv_f32_w8_batch") }
1626                else if w8 { self.func("router_gemv_f32_w8") }
1627                else { self.func("router_gemv_f32") };
1628        let (ne, nx, ti) = (n_embd as i32, n_experts as i32, t as i32);
1629        let cfg = if batch {
1630            LaunchConfig { grid_dim: (n_experts.div_ceil(8) as u32, t.div_ceil(8) as u32, 1),
1631                           block_dim: (32, 8, 1), shared_mem_bytes: 0 }
1632        } else {
1633            LaunchConfig { grid_dim: (n_experts as u32, t as u32, 1),
1634                           block_dim: (32, if w8 { 8 } else { 1 }, 1), shared_mem_bytes: 0 }
1635        };
1636        let __s_b = self.gpu.stream();
1637        let mut b = __s_b.launch_builder(&f);
1638        b.arg(w).arg(x).arg(&mut y).arg(&ne).arg(&nx).arg(&ti);
1639        unsafe { b.launch(cfg)?; }
1640        Ok(y)
1641    }
1642
1643    /// f32 row permute: dst[idx[i], :] = src[i, :] (grouped-GEMM CSR -> pair-id reorder).
1644    pub fn rows_permute(&self, src: &CudaSlice<f32>, idx: &CudaSlice<i32>, nrows: usize,
1645                        ncols: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1646        let mut dst = self.alloc_uninit::<f32>(nrows * ncols)?;
1647        let f = self.func("rows_permute_f32");
1648        let (nc, nr) = (ncols as i32, nrows as i32);
1649        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (256, 1, 1),
1650                                 shared_mem_bytes: 0 };
1651        let __s_b = self.gpu.stream();
1652        let mut b = __s_b.launch_builder(&f);
1653        b.arg(src).arg(idx).arg(&mut dst).arg(&nc).arg(&nr);
1654        unsafe { b.launch(cfg)?; }
1655        Ok(dst)
1656    }
1657
1658    /// shexp gate fused dot: g[tok] = sigmoid(dot(x[tok,:], w)) — replaces the per-layer
1659    /// cuBLASLt m=1 GEMM + separate sigmoid launch on the qwen35moe decode path (the
1660    /// splitKreduce x40/step dig, 2026-07-31). One fold order for every t, so the t=1
1661    /// decode chain and the small-t spec-verify chain match per row by construction.
1662    pub fn sigmoid_dot_rows(&self, x: &CudaSlice<f32>, w: &CudaSlice<f32>, n_embd: usize,
1663                            t: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1664        // MEMRA_SHEXP_DOT=0: rollback seam to the cuBLASLt linear + sigmoid pair (numeric
1665        // config; same class as MEMRA_ROUTER_V2).
1666        static OFF: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
1667        if *OFF.get_or_init(|| std::env::var("MEMRA_SHEXP_DOT").as_deref() == Ok("0")) {
1668            let gs = self.linear(x, w, t, n_embd, 1)?;
1669            let mut g = self.uninit(t)?;
1670            self.sigmoid(&gs, &mut g, t)?;
1671            return Ok(g);
1672        }
1673        // FAST-ROUTER lane note (2026-08-02): a register-tiled 8-token batch twin of this
1674        // kernel was built, proven bit-identical, and measured SLOWER at every prefill t on
1675        // the 5090 (0.62-0.89x — launch-latency-bound op, ~7us/layer at m=2048;
1676        // research/fast-router-20260802/crossover-router.jsonl). Dispatch arm killed per
1677        // flags doctrine; this per-token form serves every t.
1678        let mut g = self.alloc_uninit::<f32>(t)?;
1679        let f = self.func("sigmoid_dot_rows_f32");
1680        let (ne, ti) = (n_embd as i32, t as i32);
1681        let cfg = LaunchConfig { grid_dim: (t as u32, 1, 1), block_dim: (32, 8, 1),
1682                                 shared_mem_bytes: 0 };
1683        let __s_b = self.gpu.stream();
1684        let mut b = __s_b.launch_builder(&f);
1685        b.arg(x).arg(w).arg(&mut g).arg(&ne).arg(&ti);
1686        unsafe { b.launch(cfg)?; }
1687        Ok(g)
1688    }
1689
1690    /// ROUND-STREAM stream rollback: all counters <- pos_start + base + n_acc.
1691    pub fn spec_rollback_stream(&self, len_ptrs: &CudaSlice<u64>, pos_start: &CudaSlice<i32>,
1692                                acc: &CudaSlice<u32>, base: usize, n_rows: usize)
1693                                -> Result<(), Box<dyn std::error::Error>> {
1694        let f = self.func("spec_rollback_stream");
1695        let (b, nr) = (base as i32, n_rows as i32);
1696        let cfg = LaunchConfig { grid_dim: (n_rows.div_ceil(64) as u32, 1, 1),
1697                                 block_dim: (64, 1, 1), shared_mem_bytes: 0 };
1698        let __s_bl = self.gpu.stream();
1699        let mut bl = __s_bl.launch_builder(&f);
1700        bl.arg(len_ptrs).arg(pos_start).arg(acc).arg(&b).arg(&nr);
1701        unsafe { bl.launch(cfg)?; }
1702        Ok(())
1703    }
1704
1705    /// PLAIN-DECODE GRAPH ring store: ring[(pos_start - base) % cap] = vam[0].
1706    pub fn plain_tok_ring(&self, vam: &CudaSlice<u32>, pos_start: &CudaSlice<i32>,
1707                          base: usize, ring: &mut CudaSlice<u32>)
1708                          -> Result<(), Box<dyn std::error::Error>> {
1709        let f = self.func("plain_tok_ring");
1710        let (b, cap) = (base as i32, ring.len() as i32);
1711        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
1712        let __s_bl = self.gpu.stream();
1713        let mut bl = __s_bl.launch_builder(&f);
1714        bl.arg(vam).arg(pos_start).arg(&b).arg(&mut *ring).arg(&cap);
1715        unsafe { bl.launch(cfg)?; }
1716        Ok(())
1717    }
1718
1719    /// ROUND-STREAM stage (c) 4 epilogue: ring commit + tiny counter copies.
1720    pub fn spec_ring_commit(&self, vtok: &CudaSlice<u32>, acc: &CudaSlice<u32>,
1721                            brk: &CudaSlice<u32>, ring: &mut CudaSlice<u32>,
1722                            pend: &mut CudaSlice<u32>)
1723                            -> Result<(), Box<dyn std::error::Error>> {
1724        let f = self.func("spec_ring_commit");
1725        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
1726        let __s_b = self.gpu.stream();
1727        let mut b = __s_b.launch_builder(&f);
1728        b.arg(vtok).arg(acc).arg(brk).arg(ring).arg(pend);
1729        unsafe { b.launch(cfg)?; }
1730        Ok(())
1731    }
1732    pub fn i32_copy_add(&self, src: &CudaSlice<i32>, dst: &mut CudaSlice<i32>, delta: i32)
1733                        -> Result<(), Box<dyn std::error::Error>> {
1734        let f = self.func("i32_copy_add");
1735        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
1736        let __s_b = self.gpu.stream();
1737        let mut b = __s_b.launch_builder(&f);
1738        b.arg(src).arg(dst).arg(&delta);
1739        unsafe { b.launch(cfg)?; }
1740        Ok(())
1741    }
1742    pub fn u32_copy(&self, src: &CudaSlice<u32>, dst: &mut CudaSlice<u32>)
1743                    -> Result<(), Box<dyn std::error::Error>> {
1744        let f = self.func("u32_copy");
1745        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
1746        let __s_b = self.gpu.stream();
1747        let mut b = __s_b.launch_builder(&f);
1748        b.arg(src).arg(dst);
1749        unsafe { b.launch(cfg)?; }
1750        Ok(())
1751    }
1752
1753    /// ROUND-GRAPH adaptive depth: brk[0] <- clamp(acc[0] + 1, floor, cap) — the host
1754    /// adaptive policy as a captured device op (policy-identical: the accept walk depth
1755    /// caps acceptance exactly like drafting fewer tokens).
1756    pub fn spec_adapt_k(&self, acc: &CudaSlice<u32>, brk: &mut CudaSlice<u32>,
1757                        floor: usize, cap: usize)
1758                        -> Result<(), Box<dyn std::error::Error>> {
1759        let f = self.func("spec_adapt_k");
1760        let (fl, cp) = (floor as i32, cap as i32);
1761        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
1762        let __s_b = self.gpu.stream();
1763        let mut b = __s_b.launch_builder(&f);
1764        b.arg(acc).arg(brk).arg(&fl).arg(&cp);
1765        unsafe { b.launch(cfg)?; }
1766        Ok(())
1767    }
1768
1769    /// ROUND-STREAM stage (c) 3: accept walk fully device-driven (brk + assembled vtok).
1770    pub fn spec_accept_greedy_dc(&self, preds: &CudaSlice<u32>, vtok: &CudaSlice<u32>,
1771                                 last_pred: &CudaSlice<u32>, brk: &CudaSlice<u32>,
1772                                 out: &mut CudaSlice<u32>)
1773                                 -> Result<(), Box<dyn std::error::Error>> {
1774        let f = self.func("spec_accept_greedy_dc");
1775        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
1776        let __s_b = self.gpu.stream();
1777        let mut b = __s_b.launch_builder(&f);
1778        b.arg(preds).arg(vtok).arg(last_pred).arg(brk).arg(out);
1779        unsafe { b.launch(cfg)?; }
1780        Ok(())
1781    }
1782
1783    /// ROUND-STREAM stage (c) 2: verify-chain device-pos entries.
1784    pub fn pos_iota(&self, pos0: &CudaSlice<i32>, out: &mut CudaSlice<i32>, t: usize)
1785                    -> Result<(), Box<dyn std::error::Error>> {
1786        let f = self.func("pos_iota_i32");
1787        let ti = t as i32;
1788        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (t.max(1) as u32, 1, 1),
1789                                 shared_mem_bytes: 0 };
1790        let __s_b = self.gpu.stream();
1791        let mut b = __s_b.launch_builder(&f);
1792        b.arg(pos0).arg(out).arg(&ti);
1793        unsafe { b.launch(cfg)?; }
1794        Ok(())
1795    }
1796    #[allow(clippy::too_many_arguments)]
1797    pub fn append_kv_quantized_rows_dc(&self, k_rows: &CudaSlice<f32>, v_rows: &CudaSlice<f32>,
1798                                       kc: &mut CudaSlice<u8>, vc: &mut CudaSlice<u8>,
1799                                       t0_dev: &CudaSlice<i32>, t: usize,
1800                                       kv_dim_k: usize, kv_dim_v: usize,
1801                                       k_tok_bytes: usize, v_tok_bytes: usize, g: bool)
1802                                       -> Result<(), Box<dyn std::error::Error>> {
1803        let f = if g { self.func_g("append_quantize_kv_q8_0_q5_1_rows_dc") }
1804                else { self.func("append_quantize_kv_q8_0_q5_1_rows_dc") };
1805        let nblk = (kv_dim_k.max(kv_dim_v) / 32) as u32;
1806        let cfg = LaunchConfig { grid_dim: (nblk, t as u32, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
1807        let (kdk, kdv) = (kv_dim_k as i32, kv_dim_v as i32);
1808        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
1809        let __s_b = self.gpu.stream();
1810        let mut b = __s_b.launch_builder(&f);
1811        b.arg(k_rows).arg(v_rows).arg(kc).arg(vc).arg(t0_dev).arg(&kdk).arg(&kdv).arg(&ktb).arg(&vtb);
1812        unsafe { b.launch(cfg)?; }
1813        Ok(())
1814    }
1815
1816    /// t=1 dc append with a FUSED len_d increment (wave 5c) — one launch replaces
1817    /// append_rows_dc + inc_seqlen. Single block (read-before-inc ordering).
1818    #[allow(clippy::too_many_arguments)]
1819    pub fn append_kv_quantized_row_dc_inc(&self, k_row: &CudaSlice<f32>, v_row: &CudaSlice<f32>,
1820                                          kc: &mut CudaSlice<u8>, vc: &mut CudaSlice<u8>,
1821                                          t0_dev: &mut CudaSlice<i32>,
1822                                          kv_dim_k: usize, kv_dim_v: usize,
1823                                          k_tok_bytes: usize, v_tok_bytes: usize, g: bool)
1824                                          -> Result<(), Box<dyn std::error::Error>> {
1825        let f = if g { self.func_g("append_quantize_kv_q8_0_q5_1_dc_inc") }
1826                else { self.func("append_quantize_kv_q8_0_q5_1_dc_inc") };
1827        let nthreads = ((kv_dim_k.max(kv_dim_v) / 32) * 32).min(1024) as u32;
1828        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (nthreads, 1, 1),
1829                                 shared_mem_bytes: 0 };
1830        let (kdk, kdv) = (kv_dim_k as i32, kv_dim_v as i32);
1831        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
1832        let __s_b = self.gpu.stream();
1833        let mut b = __s_b.launch_builder(&f);
1834        b.arg(k_row).arg(v_row).arg(kc).arg(vc).arg(t0_dev).arg(&kdk).arg(&kdv).arg(&ktb).arg(&vtb);
1835        unsafe { b.launch(cfg)?; }
1836        Ok(())
1837    }
1838
1839    /// ROUND-STREAM: draft-chain pack + in-graph d2t remap (see kernels.cu headers).
1840    pub fn pack_tok_p(&self, tok: &CudaSlice<u32>, p: &CudaSlice<f32>, out: &mut CudaSlice<u32>,
1841                      slot: usize) -> Result<(), Box<dyn std::error::Error>> {
1842        let f = self.func("pack_tok_p");
1843        let sl = slot as i32;
1844        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
1845        let __s_b = self.gpu.stream();
1846        let mut b = __s_b.launch_builder(&f);
1847        b.arg(tok).arg(p).arg(out).arg(&sl);
1848        unsafe { b.launch(cfg)?; }
1849        Ok(())
1850    }
1851    pub fn tok_map_u32(&self, tok: &mut CudaSlice<u32>, map: &CudaSlice<u32>)
1852                       -> Result<(), Box<dyn std::error::Error>> {
1853        let f = self.func("tok_map_u32");
1854        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
1855        let __s_b = self.gpu.stream();
1856        let mut b = __s_b.launch_builder(&f);
1857        b.arg(tok).arg(map);
1858        unsafe { b.launch(cfg)?; }
1859        Ok(())
1860    }
1861
1862    /// ROUND-STREAM stage (c) 1: device verify-token assembly + p-min break derivation.
1863    #[allow(clippy::too_many_arguments)]
1864    pub fn spec_assemble_verify(&self, tokp: &CudaSlice<u32>, pend: &CudaSlice<u32>,
1865                                d2t: Option<&CudaSlice<u32>>, vtok: &mut CudaSlice<u32>,
1866                                brk: &mut CudaSlice<u32>, p_min: f32, k: usize, pmin0: bool)
1867                                -> Result<(), Box<dyn std::error::Error>> {
1868        let f = self.func("spec_assemble_verify");
1869        let (ki, pm) = (k as i32, if pmin0 { 1i32 } else { 0i32 });
1870        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
1871        let __s_b = self.gpu.stream();
1872        let mut b = __s_b.launch_builder(&f);
1873        match d2t {
1874            Some(m) => { b.arg(tokp).arg(pend).arg(m).arg(vtok).arg(brk).arg(&p_min).arg(&ki).arg(&pm);
1875                         unsafe { b.launch(cfg)?; } }
1876            None => { let null: u64 = 0;
1877                      b.arg(tokp).arg(pend).arg(&null).arg(vtok).arg(brk).arg(&p_min).arg(&ki).arg(&pm);
1878                      unsafe { b.launch(cfg)?; } }
1879        }
1880        Ok(())
1881    }
1882
1883    /// ROUND-STREAM stage (b) 3b: recur-restore twins with device-j (see hybrid.cu headers).
1884    #[allow(clippy::too_many_arguments)]
1885    pub fn ssm_conv_ring_rebuild_dc(&self, qkv_tm: &CudaSlice<f32>, ring_old: &CudaSlice<f32>,
1886                                    conv_state: &mut CudaSlice<f32>, conv_dim: usize,
1887                                    acc: &CudaSlice<u32>, base: usize, t_v: usize, d_conv: usize)
1888                                    -> Result<(), Box<dyn std::error::Error>> {
1889        let f = self.func("ssm_conv_ring_rebuild_f32_dc");
1890        let n = conv_dim * (d_conv - 1);
1891        let cfg = LaunchConfig::for_num_elems(n as u32);
1892        let (cd, b0, tv, dc) = (conv_dim as i32, base as i32, t_v as i32, d_conv as i32);
1893        let __s_b = self.gpu.stream();
1894        let mut b = __s_b.launch_builder(&f);
1895        b.arg(qkv_tm).arg(ring_old).arg(conv_state).arg(&cd).arg(acc).arg(&b0).arg(&tv).arg(&dc);
1896        unsafe { b.launch(cfg)?; }
1897        Ok(())
1898    }
1899    #[allow(clippy::too_many_arguments)]
1900    pub fn gdn_scan_s128_dc(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
1901                            g: &CudaSlice<f32>, beta: &CudaSlice<f32>, state_in: &CudaSlice<f32>,
1902                            state_out: &mut CudaSlice<f32>, o: &mut CudaSlice<f32>,
1903                            n_head: usize, acc: &CudaSlice<u32>, base: usize, t_v: usize,
1904                            scale: f32)
1905                            -> Result<(), Box<dyn std::error::Error>> {
1906        let f = self.func("gdn_scan_s128_dc");
1907        const S_V: u32 = 128; const WARP: u32 = 32; const COLS_PER_BLOCK: u32 = 4;
1908        let cfg = LaunchConfig {
1909            grid_dim: (n_head as u32, 1, S_V / COLS_PER_BLOCK),
1910            block_dim: (WARP, COLS_PER_BLOCK, 1),
1911            shared_mem_bytes: 0,
1912        };
1913        let (h, b0, tv) = (n_head as i32, base as i32, t_v as i32);
1914        let __s_b = self.gpu.stream();
1915        let mut b = __s_b.launch_builder(&f);
1916        b.arg(q).arg(k).arg(v).arg(g).arg(beta).arg(state_in).arg(state_out).arg(o)
1917         .arg(&h).arg(acc).arg(&b0).arg(&tv).arg(&scale);
1918        unsafe { b.launch(cfg)?; }
1919        Ok(())
1920    }
1921
1922    /// ROUND-STREAM stage (b) 3a: device per-layer KV-len rollback (see spec_rollback_kv).
1923    pub fn spec_rollback_kv(&self, len_ptrs: &CudaSlice<u64>, saved: &CudaSlice<i32>,
1924                            acc: &CudaSlice<u32>, base: usize, n_layer: usize)
1925                            -> Result<(), Box<dyn std::error::Error>> {
1926        let f = self.func("spec_rollback_kv");
1927        let (b, nl) = (base as i32, n_layer as i32);
1928        let cfg = LaunchConfig { grid_dim: (n_layer.div_ceil(64) as u32, 1, 1),
1929                                 block_dim: (64, 1, 1), shared_mem_bytes: 0 };
1930        let __s_bl = self.gpu.stream();
1931        let mut bl = __s_bl.launch_builder(&f);
1932        bl.arg(len_ptrs).arg(saved).arg(acc).arg(&b).arg(&nl);
1933        unsafe { bl.launch(cfg)?; }
1934        Ok(())
1935    }
1936
1937    /// OPTIPIPE increment 1: derive the K=1 successor-valid bit on device.
1938    pub fn spec_fork_valid(&self, acc: &CudaSlice<u32>, optimistic_pending: u32,
1939                           valid: &mut CudaSlice<u32>)
1940                           -> Result<(), Box<dyn std::error::Error>> {
1941        let f = self.func("spec_fork_valid");
1942        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (1, 1, 1),
1943                                 shared_mem_bytes: 0 };
1944        let __s_bl = self.gpu.stream();
1945        let mut bl = __s_bl.launch_builder(&f);
1946        bl.arg(acc).arg(&optimistic_pending).arg(valid);
1947        unsafe { bl.launch(cfg)?; }
1948        Ok(())
1949    }
1950
1951    /// OPTIPIPE increment 1: leave stage-local KV lengths on hit, restore them on miss.
1952    pub fn spec_fork_reconcile_kv(&self, len_ptrs: &CudaSlice<u64>, saved: &CudaSlice<i32>,
1953                                  acc: &CudaSlice<u32>, valid: &CudaSlice<u32>, base: usize,
1954                                  n_layer: usize)
1955                                  -> Result<(), Box<dyn std::error::Error>> {
1956        let f = self.func("spec_fork_reconcile_kv");
1957        let (b, nl) = (base as i32, n_layer as i32);
1958        let cfg = LaunchConfig { grid_dim: (n_layer.div_ceil(64) as u32, 1, 1),
1959                                 block_dim: (64, 1, 1), shared_mem_bytes: 0 };
1960        let __s_bl = self.gpu.stream();
1961        let mut bl = __s_bl.launch_builder(&f);
1962        bl.arg(len_ptrs).arg(saved).arg(acc).arg(valid).arg(&b).arg(&nl);
1963        unsafe { bl.launch(cfg)?; }
1964        Ok(())
1965    }
1966
1967    /// OPTIPIPE increment 1: conditionally restore one stage-owned recurrent-state buffer.
1968    pub fn spec_fork_restore_f32(&self, snapshot: &CudaSlice<f32>, state: &mut CudaSlice<f32>,
1969                                 valid: &CudaSlice<u32>)
1970                                 -> Result<(), Box<dyn std::error::Error>> {
1971        assert_eq!(snapshot.len(), state.len(), "fork recurrent snapshot shape mismatch");
1972        let f = self.func("spec_fork_restore_f32");
1973        let n = state.len() as i32;
1974        let blocks = state.len().div_ceil(256).min(65535).max(1) as u32;
1975        let cfg = LaunchConfig { grid_dim: (blocks, 1, 1), block_dim: (256, 1, 1),
1976                                 shared_mem_bytes: 0 };
1977        let __s_bl = self.gpu.stream();
1978        let mut bl = __s_bl.launch_builder(&f);
1979        bl.arg(snapshot).arg(state).arg(valid).arg(&n);
1980        unsafe { bl.launch(cfg)?; }
1981        Ok(())
1982    }
1983
1984    /// ROUND-STREAM stage (b): device next-round seed gather (see spec_seed_gather header).
1985    /// Caller D2Ds h_seed into fill_prev after (both slots carry the same value in every arm).
1986    pub fn spec_seed_gather(&self, vx: &CudaSlice<f32>, fill_prev: &CudaSlice<f32>,
1987                            acc: &CudaSlice<u32>, h_seed: &mut CudaSlice<f32>,
1988                            base: usize, n_embd: usize)
1989                            -> Result<(), Box<dyn std::error::Error>> {
1990        let f = self.func("spec_seed_gather");
1991        let (b, ne) = (base as i32, n_embd as i32);
1992        let cfg = LaunchConfig { grid_dim: (n_embd.div_ceil(256) as u32, 1, 1),
1993                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
1994        let __s_bl = self.gpu.stream();
1995        let mut bl = __s_bl.launch_builder(&f);
1996        bl.arg(vx).arg(fill_prev).arg(acc).arg(h_seed).arg(&b).arg(&ne);
1997        unsafe { bl.launch(cfg)?; }
1998        Ok(())
1999    }
2000
2001
2002    /// ROUND-STREAM stage (a): device greedy accept walk (see spec_accept_greedy header).
2003    pub fn spec_accept_greedy(&self, preds: &CudaSlice<u32>, draft: &CudaSlice<u32>,
2004                              last_pred: u32, base: usize, k_round: usize,
2005                              out: &mut CudaSlice<u32>)
2006                              -> Result<(), Box<dyn std::error::Error>> {
2007        let f = self.func("spec_accept_greedy");
2008        let (b, k) = (base as i32, k_round as i32);
2009        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
2010        let __s_bl = self.gpu.stream();
2011        let mut bl = __s_bl.launch_builder(&f);
2012        bl.arg(preds).arg(draft).arg(&last_pred).arg(&b).arg(&k).arg(out);
2013        unsafe { bl.launch(cfg)?; }
2014        Ok(())
2015    }
2016
2017    // ================= SAMPLED-SPEC PRIMITIVES (spec_sample.cu, piece A) =================
2018    // Counter-based randomness: every call takes (seed, stream_pos) — the caller owns the
2019    // event counter (one per sampled token). temp <= 0 arms are exact greedy limits.
2020
2021    /// y = x/temp + Gumbel(Philox(seed, stream_pos)) over n logits (then run device argmax on y
2022    /// = one categorical sample at temperature `temp`). temp<=0: y = x (pure copy).
2023    pub fn gumbel_perturb(&self, x: &CudaSlice<f32>, y: &mut CudaSlice<f32>, n: usize,
2024                          seed: u64, stream_pos: u32, temp: f32)
2025                          -> Result<(), Box<dyn std::error::Error>> {
2026        let f = self.func("gumbel_perturb_f32");
2027        let (ni, slo, shi) = (n as i32, (seed & 0xFFFF_FFFF) as u32, (seed >> 32) as u32);
2028        let cfg = LaunchConfig { grid_dim: (n.div_ceil(256) as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
2029        let __s_b = self.gpu.stream();
2030        let mut b = __s_b.launch_builder(&f);
2031        b.arg(x).arg(&mut *y).arg(&ni).arg(&slo).arg(&shi).arg(&stream_pos).arg(&temp);
2032        unsafe { b.launch(cfg)?; }
2033        Ok(())
2034    }
2035
2036    /// GRAMMAR TOKEN MASK (constrained decoding, lane/constrained-full): ban every vocab id
2037    /// whose bit is unset in the packed llguidance bitset, IN PLACE on row `col` of a stacked
2038    /// [B, n_vocab] logits buffer. `mask` = the SimpleVob u32 words H2D'd verbatim
2039    /// (~n_vocab/8 bytes/step — trivial on PCIe); ids >= 32*mask_words (padded lm_head tail)
2040    /// are banned too, the device twin of constrained::apply_mask. Banned value -FLT_MAX ==
2041    /// the argmax/gumbel kernels' init sentinel, so a fully-banned tail can never win and
2042    /// ordering matches the host -inf mask bit-for-bit for every finite logit.
2043    pub fn mask_logits_col(&self, logits: &mut CudaSlice<f32>, mask: &CudaSlice<u32>,
2044                           col: usize, n: usize, mask_words: usize)
2045                           -> Result<(), Box<dyn std::error::Error>> {
2046        let f = self.func("mask_logits_f32");
2047        let (ci, ni, mw) = (col as i32, n as i32, mask_words as i32);
2048        let cfg = LaunchConfig { grid_dim: (n.div_ceil(256).min(1024) as u32, 1, 1),
2049                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
2050        let __s_b = self.gpu.stream();
2051        let mut b = __s_b.launch_builder(&f);
2052        b.arg(&mut *logits).arg(mask).arg(&ci).arg(&ni).arg(&mw);
2053        unsafe { b.launch(cfg)?; }
2054        Ok(())
2055    }
2056
2057    /// Column-`col` twin of `gumbel_perturb` over stacked logits [B, n_vocab] (the batched
2058    /// serving tick's device sampler): y = x[col]/temp + gumbel(seed, stream_pos, lane).
2059    /// SAME kernel/Philox mapping as `gumbel_perturb` — bit-identical perturbation for the
2060    /// same (seed, stream_pos, temp) regardless of which batch column the row sits in
2061    /// (the lane index is the in-row position; `col` only moves the input pointer). That
2062    /// pointer-invariance IS the serving isolation contract for sampled rows.
2063    pub fn gumbel_perturb_col(&self, x: &CudaSlice<f32>, col: usize, y: &mut CudaSlice<f32>,
2064                              n: usize, seed: u64, stream_pos: u32, temp: f32)
2065                              -> Result<(), Box<dyn std::error::Error>> {
2066        let f = self.func("gumbel_perturb_f32");
2067        let (ni, slo, shi) = (n as i32, (seed & 0xFFFF_FFFF) as u32, (seed >> 32) as u32);
2068        let col_view = x.slice(col * n..(col + 1) * n);
2069        let cfg = LaunchConfig { grid_dim: (n.div_ceil(256) as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
2070        let __s_b = self.gpu.stream();
2071        let mut b = __s_b.launch_builder(&f);
2072        b.arg(&col_view).arg(&mut *y).arg(&ni).arg(&slo).arg(&shi).arg(&stream_pos).arg(&temp);
2073        unsafe { b.launch(cfg)?; }
2074        Ok(())
2075    }
2076
2077    /// In-graph sampling-event counter bump (spec_sample.cu kernel 5): ctr[0] += 1. The sampled
2078    /// graph-draft chain replays with FIXED kernel args, so the Philox event counter must be
2079    /// DEVICE data — the host seeds it once per round; every replay bumps it before the perturb
2080    /// reads it (counter is data, not state — graph-replay-safe).
2081    pub fn sctr_inc(&self, ctr: &mut CudaSlice<u32>) -> Result<(), Box<dyn std::error::Error>> {
2082        let f = self.func("memra_sctr_inc");
2083        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (1, 1, 1), shared_mem_bytes: 0 };
2084        let __s_b = self.gpu.stream();
2085        let mut b = __s_b.launch_builder(&f);
2086        b.arg(&mut *ctr);
2087        unsafe { b.launch(cfg)?; }
2088        Ok(())
2089    }
2090
2091    /// Graph-capturable `gumbel_perturb`: the sampling-event counter comes from DEVICE memory
2092    /// (`ctr[0]`) instead of a host scalar. Identical math to `gumbel_perturb` at
2093    /// stream_pos == ctr[0] (same Philox call, same lane mapping) — the eager and graph sampled
2094    /// chains produce bit-identical perturbations for the same (seed, counter, temp).
2095    pub fn gumbel_perturb_ctr(&self, x: &CudaSlice<f32>, y: &mut CudaSlice<f32>, n: usize,
2096                              seed: u64, ctr: &CudaSlice<u32>, temp: f32)
2097                              -> Result<(), Box<dyn std::error::Error>> {
2098        let f = self.func("gumbel_perturb_ctr_f32");
2099        let (ni, slo, shi) = (n as i32, (seed & 0xFFFF_FFFF) as u32, (seed >> 32) as u32);
2100        let cfg = LaunchConfig { grid_dim: (n.div_ceil(256) as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
2101        let __s_b = self.gpu.stream();
2102        let mut b = __s_b.launch_builder(&f);
2103        b.arg(x).arg(&mut *y).arg(&ni).arg(&slo).arg(&shi).arg(ctr).arg(&temp);
2104        unsafe { b.launch(cfg)?; }
2105        Ok(())
2106    }
2107
2108    /// out[pair] = softmax_temp(x[rows[pair]])[ids[pair]] for npair (row, id) pairs; rows index
2109    /// into x with `row_stride` f32s per row. temp<=0: out = 1.0 iff id is the row argmax
2110    /// (smallest-index tie-break — matches the argmax-gate contract).
2111    pub fn softmax_gather(&self, x: &CudaSlice<f32>, row_stride: usize,
2112                          ids: &CudaSlice<u32>, rows: &CudaSlice<i32>,
2113                          out: &mut CudaSlice<f32>, n: usize, npair: usize, temp: f32)
2114                          -> Result<(), Box<dyn std::error::Error>> {
2115        let f = self.func("softmax_gather_f32");
2116        let (ni, rs) = (n as i32, row_stride as i64);
2117        let np = npair as i32;
2118        let cfg = LaunchConfig { grid_dim: (npair as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
2119        let __s_b = self.gpu.stream();
2120        let mut b = __s_b.launch_builder(&f);
2121        b.arg(x).arg(&rs).arg(ids).arg(rows).arg(&mut *out).arg(&ni).arg(&np).arg(&temp);
2122        unsafe { b.launch(cfg)?; }
2123        Ok(())
2124    }
2125
2126    /// Sample token from norm(max(0, softmax_temp(p) - softmax_temp(q))) (q = None -> plain
2127    /// categorical from softmax_temp(p)). Row stats (max, sumexp at temp) must be precomputed
2128    /// (softmax_gather's pass-1 values; see spec.rs caller). Deterministic fixed-order CDF walk.
2129    pub fn residual_sample(&self, p: &CudaSlice<f32>, q: Option<&CudaSlice<f32>>, n: usize,
2130                           temp: f32, seed: u64, stream_pos: u32,
2131                           out_tok: &mut CudaSlice<u32>)
2132                           -> Result<(), Box<dyn std::error::Error>> {
2133        let f = self.func("residual_sample_f32");
2134        let (ni, slo, shi) = (n as i32, (seed & 0xFFFF_FFFF) as u32, (seed >> 32) as u32);
2135        let nth = 1024u32;
2136        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (nth, 1, 1), shared_mem_bytes: 0 };
2137        let has_q: i32 = q.is_some() as i32;
2138        let qbuf = q.unwrap_or(p);   // dummy when absent; kernel gates on has_q
2139        let __s_b = self.gpu.stream();
2140        let mut b = __s_b.launch_builder(&f);
2141        b.arg(p).arg(qbuf).arg(&has_q).arg(&ni).arg(&temp).arg(&slo).arg(&shi).arg(&stream_pos)
2142         .arg(&mut *out_tok);
2143        unsafe { b.launch(cfg)?; }
2144        Ok(())
2145    }
2146
2147    /// Access the shared MoE residency cache (EDGE-1 §B), building it on first use under
2148    /// MEMRA_MOE_CACHE. The closure runs while the lock is held — keep it to lookup/admit/issue, not
2149    /// the GEMM. `max_block_bytes` sizes the slots (largest of gate/up/down). Returns the closure's
2150    /// result. If MEMRA_MOE_CACHE is unset this is never called (the caller checks the env first).
2151    pub fn with_moe_cache<R>(&self, max_block_bytes: usize,
2152                             f: impl FnOnce(&mut crate::moe_cache::MoeSlotCache, &Engine) -> Result<R, Box<dyn std::error::Error>>)
2153                             -> Result<R, Box<dyn std::error::Error>> {
2154        let mut guard = self.moe_cache.lock().unwrap();
2155        if guard.is_none() {
2156            *guard = Some(crate::moe_cache::MoeSlotCache::new(self, max_block_bytes)?);
2157        }
2158        let cache = guard.as_mut().unwrap();
2159                f(cache, self)
2160    }
2161
2162    /// Freeze the already-built MoE residency set. This never constructs a cache: callers use it
2163    /// only after a real prefill has populated the machine-specific CPU/GPU working set.
2164    pub fn freeze_moe_cache(&self) {
2165        if let Some(cache) = self.moe_cache.lock().unwrap().as_mut() {
2166            cache.freeze();
2167        }
2168    }
2169
2170    /// The current residency set as (layer, proj, ex) triples, or None if no cache was built.
2171    /// Never constructs a cache.
2172    pub fn export_moe_residency(&self) -> Option<Vec<(u16, u8, u16)>> {
2173        self.moe_cache
2174            .lock()
2175            .unwrap()
2176            .as_ref()
2177            .map(crate::moe_cache::MoeSlotCache::export_residency)
2178    }
2179
2180    pub(crate) fn moe_cache_frozen(&self) -> bool {
2181        self.moe_cache
2182            .lock()
2183            .unwrap()
2184            .as_ref()
2185            .is_some_and(crate::moe_cache::MoeSlotCache::is_frozen)
2186    }
2187
2188    /// A frozen heterogeneous CPU/GPU expert split cannot use Hy3's ordinary batched prefill
2189    /// efficiently: T>=PRIME_MIN_T bypasses the CPU backend and transiently rereads every missing
2190    /// expert through the GPU spill path. Replay the short prompt through decode after freezing,
2191    /// while leaving the profiling warmup's established batched behavior untouched.
2192    /// (`pub`: run-gen's #46 batched-prime gate skips itself when generation will take the
2193    /// tokenwise arm anyway.)
2194    pub fn frozen_cpu_experts_prefer_tokenwise_prime(&self) -> bool {
2195        crate::cpu_experts::configured()
2196            && self.moe_cache_frozen()
2197            && std::env::var("MEMRA_CPU_EXPERT_BATCHED_PRIME").as_deref() != Ok("1")
2198    }
2199
2200    /// Install the loaded model's exact retained expert-block inventory before lazy cache build.
2201    pub(crate) fn configure_moe_cache_layout(&self, block_bytes: Vec<usize>) {
2202        assert!(
2203            self.moe_cache.lock().unwrap().is_none(),
2204            "MoE cache layout configured after cache construction"
2205        );
2206        *self.moe_cache_layout.lock().unwrap() = Some(block_bytes);
2207    }
2208
2209    pub(crate) fn moe_cache_layout(&self) -> Option<Vec<usize>> {
2210        self.moe_cache_layout.lock().unwrap().clone()
2211    }
2212
2213    /// True if the MoE residency cache is enabled (MEMRA_MOE_CACHE set).
2214    pub fn moe_cache_enabled() -> bool {
2215        std::env::var("MEMRA_MOE_CACHE").as_deref() != Ok("0")
2216 }
2217
2218    /// Snapshot the MoE cache counters (hits, misses, staged_bytes, n_slots) for the §D.4 PCIe gate.
2219    /// Returns None if the cache was never built (disabled or no MoE forward ran).
2220    pub fn moe_cache_stats(&self) -> Option<(u64, u64, u64, usize)> {
2221        let guard = self.moe_cache.lock().unwrap();
2222        guard.as_ref()            .map(|c| (c.hits, c.misses, c.staged_bytes, c.n_slots()))
2223    }
2224
2225    /// Experimental CPU expert backend counters: completed layer calls, experts served, and the
2226    /// sum of backend wall nanoseconds. The timer includes explicit disk->RAM fills on cache misses;
2227    /// callers compare a before/after snapshot around a decode window.
2228    pub fn cpu_expert_stats(
2229        &self,
2230    ) -> Option<(u64, u64, u64, u64, u64, u64, u64, u64, u64, u64, u64)> {
2231        crate::cpu_experts::configured().then(crate::cpu_experts::stats)
2232    }
2233
2234    /// Caller-blocked nanoseconds at CPU expert joins. Compare before/after snapshots to measure
2235    /// the backend tail that resident-GPU expert work did not hide.
2236    pub fn cpu_expert_predictor_stats(&self) -> (u64, u64) {
2237        crate::cpu_experts::predictor_stats()
2238    }
2239
2240    pub fn cpu_expert_exposed_wait_ns(&self) -> Option<u64> {
2241        crate::cpu_experts::configured().then(crate::cpu_experts::exposed_wait_ns)
2242    }
2243
2244    /// CPU-routed expert selections grouped by how many of their three projections were already
2245    /// resident in HBM. This makes otherwise-stranded partial residency visible to tuning runs.
2246    pub fn cpu_expert_gpu_residency_stats(&self) -> Option<(u64, u64, u64)> {
2247        crate::cpu_experts::configured().then(crate::cpu_experts::incomplete_gpu_residency_stats)
2248    }
2249
2250    /// Positioned-read proof-backend counters:
2251    /// `(reads, bytes, read_errors, short_reads, mmap_fallbacks, buffer_waits, ring_full)`.
2252    pub fn moe_pread_stats(&self) -> Option<(u64, u64, u64, u64, u64, u64, u64)> {
2253
2254        let guard = self.moe_cache.lock().unwrap();
2255        guard.as_ref().and_then(|cache| cache.pread_stats()).map(|stats| (
2256            stats.reads,
2257            stats.bytes,
2258            stats.read_errors,
2259            stats.short_reads,
2260            stats.fallbacks,
2261            stats.buffer_waits,
2262            stats.ring_full,
2263        ))
2264    }
2265
2266    /// Reset the MoE cache perf counters (to separate warmup from steady-state windows).
2267    pub fn moe_cache_reset_counters(&self) {
2268        if let Some(c) = self.moe_cache.lock().unwrap().as_mut() { c.reset_counters(); }
2269    }
2270
2271    pub fn htod_bytes(&self, v: &[u8]) -> Result<CudaSlice<u8>, Box<dyn std::error::Error>> {
2272        Ok(self.gpu.stream().clone_htod(v)?)
2273    }
2274
2275    /// `htod_bytes` with a mapped (uninit) tail pad: the wide-load expert dots read up to 6B
2276    /// past the final q4_0 block through their aligned window — the bytes never reach a
2277    /// result (funnelshift discards them) but must be mapped memory.
2278    pub fn htod_bytes_padded(&self, v: &[u8], pad: usize)
2279                             -> Result<CudaSlice<u8>, Box<dyn std::error::Error>> {
2280        let mut d = self.alloc_u8_uninit(v.len() + pad)?;
2281        {
2282            let mut view = d.slice_mut(0..v.len());
2283            self.gpu.stream().memcpy_htod(v, &mut view)?;
2284        }
2285        Ok(d)
2286    }
2287
2288    /// Device-to-device copy of `src` into `dst[off..off+len]` (f32). For in-place KV append.
2289    pub fn copy_into(&self, dst: &mut CudaSlice<f32>, off: usize, src: &CudaSlice<f32>, len: usize)
2290                     -> Result<(), Box<dyn std::error::Error>> {
2291        let mut view = dst.slice_mut(off..off + len);
2292        self.gpu.stream().memcpy_dtod(&src.slice(0..len), &mut view)?;
2293        Ok(())
2294    }
2295
2296    /// View a sub-range of a device buffer (for attending over [0..len) of a KV cache).
2297    /// u8 twin of copy_into (D2D byte-range copy at an offset).
2298    pub fn copy_u8_into(&self, dst: &mut CudaSlice<u8>, off: usize, src: &CudaSlice<u8>, len: usize)
2299                        -> Result<(), Box<dyn std::error::Error>> {
2300        let mut view = dst.slice_mut(off..off + len);
2301        self.gpu.stream().memcpy_dtod(&src.slice(0..len), &mut view)?;
2302        Ok(())
2303    }
2304
2305    /// D2D byte-range copy with explicit source and destination offsets.
2306    pub fn copy_u8_range_into(
2307        &self,
2308        dst: &mut CudaSlice<u8>,
2309        dst_off: usize,
2310        src: &CudaSlice<u8>,
2311        src_off: usize,
2312        len: usize,
2313    ) -> Result<(), Box<dyn std::error::Error>> {
2314        let mut dst_view = dst.slice_mut(dst_off..dst_off + len);
2315        self.gpu
2316            .stream()
2317            .memcpy_dtod(&src.slice(src_off..src_off + len), &mut dst_view)?;
2318        Ok(())
2319    }
2320
2321    /// Resolve an absolute append slot to the Step35 SWA layer's physical rows. At wrap, copy
2322    /// only the aligned live prefix through temporary device storage and rebase it at row zero,
2323    /// keeping the audited attention range contiguous without changing its absolute start.
2324    pub fn prepare_kv_append(
2325        &self,
2326        kv: &mut crate::cache::KvLayer,
2327        retain_from: usize,
2328        append_rows: usize,
2329    ) -> Result<usize, Box<dyn std::error::Error>> {
2330        let Some(plan) = kv
2331            .ring
2332            .as_ref()
2333            .map(|ring| ring.append_plan(kv.len, retain_from, append_rows))
2334            .transpose()?
2335        else {
2336            return Ok(kv.len);
2337        };
2338        match plan {
2339            crate::cache::KvRingAppend::Contiguous { write_row } => Ok(write_row),
2340            crate::cache::KvRingAppend::Rebase {
2341                src_row,
2342                keep_rows,
2343                new_base,
2344                write_row,
2345            } => {
2346                if keep_rows > 0 {
2347                    let k_len = keep_rows * kv.k_tok_bytes;
2348                    let v_len = keep_rows * kv.v_tok_bytes;
2349                    let mut k_tmp = self.alloc_u8_uninit(k_len)?;
2350                    let mut v_tmp = self.alloc_u8_uninit(v_len)?;
2351                    self.copy_u8_range_into(
2352                        &mut k_tmp,
2353                        0,
2354                        &kv.k,
2355                        src_row * kv.k_tok_bytes,
2356                        k_len,
2357                    )?;
2358                    self.copy_u8_range_into(
2359                        &mut v_tmp,
2360                        0,
2361                        &kv.v,
2362                        src_row * kv.v_tok_bytes,
2363                        v_len,
2364                    )?;
2365                    self.copy_u8_into(&mut kv.k, 0, &k_tmp, k_len)?;
2366                    self.copy_u8_into(&mut kv.v, 0, &v_tmp, v_len)?;
2367                }
2368                kv.ring.as_mut().unwrap().apply_rebase(new_base);
2369                Ok(write_row)
2370            }
2371        }
2372    }
2373
2374    /// H2D write of `src` into `dst[off..off+src.len()]` (u8). In-place row updates for the
2375    /// adaptive trim head: no realloc, so captured graphs keep their baked addresses.
2376    pub fn htod_u8_into(&self, dst: &mut CudaSlice<u8>, off: usize, src: &[u8])
2377                        -> Result<(), Box<dyn std::error::Error>> {
2378        let mut view = dst.slice_mut(off..off + src.len());
2379        self.gpu.stream().memcpy_htod(src, &mut view)?;
2380        Ok(())
2381    }
2382
2383    pub fn view<'a>(&self, b: &'a CudaSlice<f32>, len: usize) -> cudarc::driver::CudaView<'a, f32> {
2384        b.slice(0..len)
2385    }
2386
2387    /// View the first `len` BYTES of a u8 device buffer (quantized KV cache: [0..t_kv*tok_bytes)).
2388    /// Byte-range view (gemma4 R6 window offset into the quantized KV stream).
2389    pub fn view_u8_range<'a>(&self, b: &'a CudaSlice<u8>, start: usize, end: usize)
2390                             -> cudarc::driver::CudaView<'a, u8> {
2391        b.slice(start..end)
2392    }
2393    pub fn view_u8<'a>(&self, b: &'a CudaSlice<u8>, len: usize) -> cudarc::driver::CudaView<'a, u8> {
2394        b.slice(0..len)
2395    }
2396
2397    /// Append-quantize ONE token's post-RoPE K (q8_0) and V (q5_1) into the resident byte caches at
2398    /// token index `t` (KVQUANT-PLAN §C). One CTA (one warp) per 32-element block; the kernel writes
2399    /// the f16 scale(s) + packed quants for K and V. k_row/v_row are f32 [kv_dim_k]/[kv_dim_v].
2400    pub fn append_kv_quantized(&self, k_row: &CudaSlice<f32>, v_row: &CudaSlice<f32>,
2401                               kc: &mut CudaSlice<u8>, vc: &mut CudaSlice<u8>, t: usize,
2402                               kv_dim_k: usize, kv_dim_v: usize,
2403                               k_tok_bytes: usize, v_tok_bytes: usize, g: bool)
2404                               -> Result<(), Box<dyn std::error::Error>> {
2405        let f = if g { self.func_g("append_quantize_kv_q8_0_q5_1") } else { self.func("append_quantize_kv_q8_0_q5_1") };
2406        let nblk = (kv_dim_k.max(kv_dim_v) / 32) as u32;
2407        let cfg = LaunchConfig { grid_dim: (nblk, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
2408        let (ti, kdk, kdv) = (t as i32, kv_dim_k as i32, kv_dim_v as i32);
2409        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
2410        let __s_b = self.gpu.stream();
2411        let mut b = __s_b.launch_builder(&f);
2412        b.arg(k_row).arg(v_row).arg(kc).arg(vc).arg(&ti).arg(&kdk).arg(&kdv).arg(&ktb).arg(&vtb);
2413        unsafe { b.launch(cfg)?; }
2414        Ok(())
2415    }
2416
2417    /// Device-counter variant of `append_kv_quantized` (CUDA-GRAPH-PLAN Phase 2): the write slot
2418    /// `t` is read from `t_dev[0]` (a resident device i32[1]) instead of a host int arg, so the
2419    /// launch args are FIXED across decode steps (graph-capturable). Identical quant math.
2420    pub fn append_kv_quantized_dc(&self, k_row: &CudaSlice<f32>, v_row: &CudaSlice<f32>,
2421                                  kc: &mut CudaSlice<u8>, vc: &mut CudaSlice<u8>, t_dev: &CudaSlice<i32>,
2422                                  kv_dim_k: usize, kv_dim_v: usize,
2423                                  k_tok_bytes: usize, v_tok_bytes: usize, g: bool)
2424                               -> Result<(), Box<dyn std::error::Error>> {
2425        let nblk = (kv_dim_k.max(kv_dim_v) / 32) as u32;
2426        let (kdk, kdv) = (kv_dim_k as i32, kv_dim_v as i32);
2427        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
2428        // PDL wave-B2: flash-module flavor mirrors the builder path's g flag exactly.
2429        if Self::pdl_on() && Self::pdl_wb_on() {
2430            use cudarc::driver::{DevicePtr, DevicePtrMut};
2431            let s = &self.gpu.stream();
2432            let (pk, _g0) = k_row.device_ptr(s); let (pv, _g1) = v_row.device_ptr(s);
2433            let (pkc, _g2) = kc.device_ptr_mut(s); let (pvc, _g3) = vc.device_ptr_mut(s);
2434            let (pt, _g4) = t_dev.device_ptr(s);
2435            let mut ps = [
2436                &pk as *const _ as *mut std::ffi::c_void, &pv as *const _ as *mut _,
2437                &pkc as *const _ as *mut _, &pvc as *const _ as *mut _,
2438                &pt as *const _ as *mut _, &kdk as *const _ as *mut _,
2439                &kdv as *const _ as *mut _, &ktb as *const _ as *mut _,
2440                &vtb as *const _ as *mut _,
2441            ];
2442            unsafe { self.launch_pdl_flash(g, "append_quantize_kv_q8_0_q5_1_dc",
2443                                           (nblk, 1, 1), (32, 1, 1), 0, &mut ps)?; }
2444            return Ok(());
2445        }
2446        let f = if g { self.func_g("append_quantize_kv_q8_0_q5_1_dc") } else { self.func("append_quantize_kv_q8_0_q5_1_dc") };
2447        let cfg = LaunchConfig { grid_dim: (nblk, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
2448        let __s_b = self.gpu.stream();
2449        let mut b = __s_b.launch_builder(&f);
2450        b.arg(k_row).arg(v_row).arg(kc).arg(vc).arg(t_dev).arg(&kdk).arg(&kdv).arg(&ktb).arg(&vtb);
2451        unsafe { b.launch(cfg)?; }
2452        Ok(())
2453    }
2454
2455    /// Append-quantize T token rows in one shot (BATCHED PROMPT PRIME). k_rows/v_rows are
2456    /// token-major [T, kv_dim] post-RoPE f32; rows land at cache slots t0..t0+T. Default = the
2457    /// batched `_rows` kernel: one (nblk, T) launch whose per-(block,token) warp program is the
2458    /// per-token append kernel verbatim -> every written row is BIT-IDENTICAL to T sequential
2459    /// `append_kv_quantized_view` calls (kernel_check pins the bytes). MEMRA_PRIME_APPEND_LOOP=1
2460    /// forces the T-launch per-row loop (the A/B seam that measured the launch overhead).
2461    #[allow(clippy::too_many_arguments)]
2462    pub fn append_kv_quantized_rows(&self, k_rows: &CudaSlice<f32>, v_rows: &CudaSlice<f32>,
2463                                    kc: &mut CudaSlice<u8>, vc: &mut CudaSlice<u8>,
2464                                    t0: usize, t: usize, kv_dim_k: usize, kv_dim_v: usize,
2465                                    k_tok_bytes: usize, v_tok_bytes: usize, g: bool)
2466                               -> Result<(), Box<dyn std::error::Error>> {
2467        if std::env::var("MEMRA_PRIME_APPEND_LOOP").is_ok() {
2468            for i in 0..t {
2469                let k_row = k_rows.slice(i * kv_dim_k..(i + 1) * kv_dim_k);
2470                let v_row = v_rows.slice(i * kv_dim_v..(i + 1) * kv_dim_v);
2471                self.append_kv_quantized_view(&k_row, &v_row, kc, vc, t0 + i,
2472                                              kv_dim_k, kv_dim_v, k_tok_bytes, v_tok_bytes, g)?;
2473            }
2474            return Ok(());
2475        }
2476        let f = if g { self.func_g("append_quantize_kv_q8_0_q5_1_rows") } else { self.func("append_quantize_kv_q8_0_q5_1_rows") };
2477        let nblk = (kv_dim_k.max(kv_dim_v) / 32) as u32;
2478        let cfg = LaunchConfig { grid_dim: (nblk, t as u32, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
2479        let (t0i, kdk, kdv) = (t0 as i32, kv_dim_k as i32, kv_dim_v as i32);
2480        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
2481        let __s_b = self.gpu.stream();
2482        let mut b = __s_b.launch_builder(&f);
2483        b.arg(k_rows).arg(v_rows).arg(kc).arg(vc).arg(&t0i).arg(&kdk).arg(&kdv).arg(&ktb).arg(&vtb);
2484        unsafe { b.launch(cfg)?; }
2485        Ok(())
2486    }
2487
2488    /// Increment a device i32[1] counter in place (p[0] += 1) via the resident `inc_i32` kernel.
2489    /// Used to advance the device-resident seqlen/pos counters inside the decode-dc path (and,
2490    /// later, inside a captured graph) without a host round-trip.
2491    pub fn inc_seqlen(&self, p: &mut CudaSlice<i32>) -> Result<(), Box<dyn std::error::Error>> {
2492        let f = self.func("inc_i32");
2493        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (1, 1, 1), shared_mem_bytes: 0 };
2494        let __s_b = self.gpu.stream();
2495        let mut b = __s_b.launch_builder(&f);
2496        b.arg(p);
2497        unsafe { b.launch(cfg)?; }
2498        Ok(())
2499    }
2500
2501    /// Like `append_kv_quantized` but k_row/v_row are CudaViews (one token's row sliced out of a
2502    /// token-major [T, kv_dim] activation buffer — the MTP verify path appends T tokens).
2503    pub fn append_kv_quantized_view(&self, k_row: &cudarc::driver::CudaView<f32>,
2504                                    v_row: &cudarc::driver::CudaView<f32>,
2505                                    kc: &mut CudaSlice<u8>, vc: &mut CudaSlice<u8>, t: usize,
2506                                    kv_dim_k: usize, kv_dim_v: usize,
2507                                    k_tok_bytes: usize, v_tok_bytes: usize, g: bool)
2508                                    -> Result<(), Box<dyn std::error::Error>> {
2509        let f = if g { self.func_g("append_quantize_kv_q8_0_q5_1") }
2510                else { self.func("append_quantize_kv_q8_0_q5_1") };
2511        let nblk = (kv_dim_k.max(kv_dim_v) / 32) as u32;
2512        let cfg = LaunchConfig { grid_dim: (nblk, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
2513        let (ti, kdk, kdv) = (t as i32, kv_dim_k as i32, kv_dim_v as i32);
2514        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
2515        let __s_b = self.gpu.stream();
2516        let mut b = __s_b.launch_builder(&f);
2517        b.arg(k_row).arg(v_row).arg(kc).arg(vc).arg(&ti).arg(&kdk).arg(&kdv).arg(&ktb).arg(&vtb);
2518        unsafe { b.launch(cfg)?; }
2519        Ok(())
2520    }
2521
2522    /// Device-to-device copy of a CudaView `src` into `dst[off..off+len]` (f32). Like `copy_into`
2523    /// but the source is a sub-view (e.g. one column of a token-major activation buffer).
2524    pub fn copy_view_into(&self, dst: &mut CudaSlice<f32>, off: usize,
2525                          src: &cudarc::driver::CudaView<f32>, len: usize)
2526                          -> Result<(), Box<dyn std::error::Error>> {
2527        let mut view = dst.slice_mut(off..off + len);
2528        self.gpu.stream().memcpy_dtod(&src.slice(0..len), &mut view)?;
2529        Ok(())
2530    }
2531
2532    /// Real device-to-device COPY of `src` into a freshly allocated buffer (NOT an Arc clone).
2533    /// Used for cache snapshots (MTP-PLAN §D.4): `CudaSlice::clone()` only bumps a refcount and
2534    /// would alias the live buffer; this allocs new device memory and memcpy_dtod's the contents.
2535    pub fn clone_dtod(&self, src: &CudaSlice<f32>) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2536        let mut dst = self.gpu.stream().alloc_zeros::<f32>(src.len())?;
2537        self.gpu.stream().memcpy_dtod(src, &mut dst)?;
2538        Ok(dst)
2539    }
2540
2541    /// D2D row extraction: copy a view (e.g. one row of a [B, n] batch buffer) into `dst`.
2542    /// Stream-ordered, async — decode_batch's per-sequence row plumbing.
2543    pub fn dtod_copy_view(&self, src: &cudarc::driver::CudaView<f32>, dst: &mut CudaSlice<f32>)
2544                          -> Result<(), Box<dyn std::error::Error>> {
2545        self.gpu.stream().memcpy_dtod(src, dst)?;
2546        Ok(())
2547    }
2548
2549    /// D2D i8 twin of `dtod_copy_view` (q8_1 activation rows).
2550    pub fn dtod_copy_view_i8(&self, src: &cudarc::driver::CudaView<i8>, dst: &mut CudaSlice<i8>)
2551                             -> Result<(), Box<dyn std::error::Error>> {
2552        self.gpu.stream().memcpy_dtod(src, dst)?;
2553        Ok(())
2554    }
2555
2556    /// D2D row placement: copy `src` into `dst[offset .. offset+src.len()]`.
2557    pub fn dtod_copy_into(&self, src: &CudaSlice<f32>, dst: &mut CudaSlice<f32>, offset: usize)
2558                          -> Result<(), Box<dyn std::error::Error>> {
2559        let n = src.len();
2560        let mut dv = dst.slice_mut(offset..offset + n);
2561        self.gpu.stream().memcpy_dtod(src, &mut dv)?;
2562        Ok(())
2563    }
2564
2565    /// Uninitialized i8 device buffer (decode_batch q8_1 row scratch).
2566    pub fn uninit_i8(&self, n: usize) -> Result<CudaSlice<i8>, Box<dyn std::error::Error>> {
2567        self.alloc_uninit::<i8>(n)
2568    }
2569
2570    /// Resident-quantized linear (Stage-A: f32 dequant-in-kernel). y[m,out]=x[m,in]@W[out,in]^T.
2571    pub fn qmatvec(&self, w: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize, in_f: usize, out_f: usize,
2572                   qtype: i32, row_bytes: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2573        let f = self.func("qmatvec_f32");
2574        let mut y = self.alloc_uninit::<f32>(m * out_f)?;  // full-overwrite output: skip memset
2575        let cfg = LaunchConfig { grid_dim: (out_f as u32, m as u32, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
2576        let (inf, outf, mi, qt, rb) = (in_f as i32, out_f as i32, m as i32, qtype, row_bytes as i64);
2577        let __s_b = self.gpu.stream();
2578        let mut b = __s_b.launch_builder(&f);
2579        b.arg(w).arg(x).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&qt).arg(&rb);
2580        unsafe { b.launch(cfg)?; }
2581        Ok(y)
2582    }
2583
2584    /// Allocate a reusable u8 GPU scratch buffer (for staged expert weights).
2585    pub fn alloc_u8(&self, n: usize) -> Result<CudaSlice<u8>, Box<dyn std::error::Error>> {
2586        let s = self.gpu.stream().alloc_zeros::<u8>(n)?;
2587        self.keep_if_capturing(&s);
2588        Ok(s)
2589    }
2590
2591    /// Uninitialized u8 scratch — skips alloc_zeros' memset. ONLY for staging buffers whose read
2592    /// range is fully overwritten by a stage_expert H2D before any kernel reads it (LAUNCH-STRUCTURE
2593    /// STAGE 2: the per-layer MoE scratch trio was 3 dead ~1MB memsets per layer per decode token).
2594    pub fn alloc_u8_uninit(&self, n: usize) -> Result<CudaSlice<u8>, Box<dyn std::error::Error>> {
2595        let s = unsafe { self.gpu.stream().alloc::<u8>(n)? };
2596        self.keep_if_capturing(&s);
2597        Ok(s)
2598    }
2599
2600    /// Zero a SUB-RANGE of an f32 buffer (CudaViewMut) — the row-sized memset the moe_out
2601    /// memset-elision uses for tokens that fall off the gdec fast path (LAUNCH-STRUCTURE STAGE 2).
2602    pub fn memset_zeros_view(&self, dst: &mut cudarc::driver::CudaViewMut<f32>)
2603                             -> Result<(), Box<dyn std::error::Error>> {
2604        self.gpu.stream().memset_zeros(dst)?;
2605        Ok(())
2606    }
2607
2608    /// EDGE-1 staging: copy `host_bytes` (a sub-slice of a HostExps buffer) into `scratch`
2609    /// at byte offset `off` (async H2D on the default stream). Length is host_bytes.len().
2610    /// The qmatvec_view that reads `scratch[off..]` is enqueued on the SAME stream after this,
2611    /// so ordering is guaranteed without an explicit sync (Stage-1; Stage-2 prefetch on a 2nd
2612    /// stream would require an event).
2613    pub fn stage_expert(&self, host_bytes: &[u8], scratch: &mut CudaSlice<u8>, off: usize)
2614                        -> Result<(), Box<dyn std::error::Error>> {
2615        let mut dst = scratch.slice_mut(off..off + host_bytes.len());  // CudaViewMut<u8>
2616        self.gpu.stream().memcpy_htod(host_bytes, &mut dst)?;            // accepts &[u8] HostSlice src
2617        Ok(())
2618    }
2619
2620    /// EDGE-1 §A: fused MoE router. `logits` is the router output [t, n_expert] (device, f32, the
2621    /// `gate_inp @ z` result). Returns (sel_idx [t, n_used] i32, sel_w [t, n_used] f32): the top-k
2622    /// expert ids (DESC by prob, ascending-index tiebreak) and renormalized weights. Replaces the
2623    /// host dtoh + softmax-256 + stable DESC top-8 sort + renorm (hybrid_forward.rs ~281-298).
2624    /// One CTA per token row, 256 threads (one per expert).
2625    pub fn moe_router_topk(&self, logits: &CudaSlice<f32>, t: usize, n_expert: usize, n_used: usize)
2626                           -> Result<(CudaSlice<i32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
2627        let f = self.func("moe_router_topk_f32");
2628        let mut sel_idx = self.alloc_uninit::<i32>(t * n_used)?;  // kernel fully overwrites
2629        let mut sel_w = self.alloc_uninit::<f32>(t * n_used)?;    // kernel fully overwrites
2630        let cfg = LaunchConfig { grid_dim: (t as u32, 1, 1), block_dim: (n_expert as u32, 1, 1),
2631                                 shared_mem_bytes: 0 };
2632        let (ne, nu) = (n_expert as i32, n_used as i32);
2633        let __s_b = self.gpu.stream();
2634        let mut b = __s_b.launch_builder(&f);
2635        b.arg(logits).arg(&mut sel_idx).arg(&mut sel_w).arg(&ne).arg(&nu);
2636        unsafe { b.launch(cfg)?; }
2637        Ok((sel_idx, sel_w))
2638    }
2639
2640    /// gemma4 twin: per-expert output scale folded into the topk renorm write (replaces the
2641    /// separate moe_w_exscale launch; value chain identical: (w/ws) * s[sel]).
2642    pub fn moe_router_topk_scaled(&self, logits: &CudaSlice<f32>, t: usize, n_expert: usize,
2643                                  n_used: usize, ex_scale: &CudaSlice<f32>)
2644                                  -> Result<(CudaSlice<i32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
2645        // barrier-lean v2 twin (per-warp top-k + one-warp merge) FALSIFIED 2026-07-14:
2646        // bit-identical streams but −1.4% (26B plain N=3 interleaved) — at t=1 the grid is
2647        // ONE block, so the 6.6us is launch/dependency overhead, not the barrier chain;
2648        // fewer barriers bought nothing and the merge structure cost. jsonl is the record.
2649        let f = self.func("moe_router_topk_scaled_f32");
2650        let mut sel_idx = self.alloc_uninit::<i32>(t * n_used)?;
2651        let mut sel_w = self.alloc_uninit::<f32>(t * n_used)?;
2652        let cfg = LaunchConfig { grid_dim: (t as u32, 1, 1), block_dim: (n_expert as u32, 1, 1),
2653                                 shared_mem_bytes: 0 };
2654        let (ne, nu) = (n_expert as i32, n_used as i32);
2655        let __s_b = self.gpu.stream();
2656        let mut b = __s_b.launch_builder(&f);
2657        b.arg(logits).arg(&mut sel_idx).arg(&mut sel_w).arg(&ne).arg(&nu).arg(ex_scale);
2658        unsafe { b.launch(cfg)?; }
2659        Ok((sel_idx, sel_w))
2660    }
2661
2662    /// LAUNCH-STRUCTURE STAGE 1 (2026-07-05): fused router + SINGLE-SYNC host readback. The old
2663    /// MEMRA_FUSED_ROUTER path lost 2% at t=1 because it paid TWO full stream syncs (dtoh_i32 then
2664    /// dtoh, each = clone_dtoh + synchronize) + two alloc_zeros memsets per MoE layer, where the
2665    /// host route pays ONE sync on the 1KB logits dtoh. This variant: uninit outputs (kernel fully
2666    /// overwrites), both DtoH copies issued ASYNC into a persistent PINNED host staging buffer
2667    /// (flags=0 — cacheable, NOT cudarc's WRITECOMBINED default, so the host-side reads of sel/w
2668    /// stay cached), then ONE synchronize. Numerics identical to `moe_router_topk` (same kernel).
2669    pub fn moe_router_topk_host(&self, logits: &CudaSlice<f32>, t: usize, n_expert: usize, n_used: usize)
2670                                -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
2671        let f = self.func("moe_router_topk_f32");
2672        let n = t * n_used;
2673        let mut sel_idx = self.alloc_uninit::<i32>(n)?;
2674        let mut sel_w = self.alloc_uninit::<f32>(n)?;
2675        let cfg = LaunchConfig { grid_dim: (t as u32, 1, 1), block_dim: (n_expert as u32, 1, 1),
2676                                 shared_mem_bytes: 0 };
2677        let (ne, nu) = (n_expert as i32, n_used as i32);
2678        let __s_b = self.gpu.stream();
2679        let mut b = __s_b.launch_builder(&f);
2680        b.arg(logits).arg(&mut sel_idx).arg(&mut sel_w).arg(&ne).arg(&nu);
2681        unsafe { b.launch(cfg)?; }
2682        // single-sync readback: sel (i32) at offset 0, w (f32) at offset n*4 of the pinned stage.
2683        let bytes = n * 8;
2684        let mut guard = self.router_stage.lock().unwrap();
2685        if guard.as_ref().map(|p| p.cap < bytes).unwrap_or(true) {
2686            *guard = Some(PinnedStage::new(bytes.max(4096))?);
2687        }
2688        let stage = guard.as_mut().unwrap();
2689        let (si, sw) = unsafe {
2690            (std::slice::from_raw_parts_mut(stage.ptr as *mut i32, n),
2691             std::slice::from_raw_parts_mut(stage.ptr.add(n * 4) as *mut f32, n))
2692        };
2693        self.gpu.stream().memcpy_dtoh(&sel_idx, si)?;   // async (pinned dst)
2694        self.gpu.stream().memcpy_dtoh(&sel_w, sw)?;     // async (pinned dst)
2695        self.gpu.stream().synchronize()?;               // ONE sync for both
2696        Ok((si.iter().map(|&i| i as u32).collect(), sw.to_vec()))
2697    }
2698
2699    /// Device sigmoid router for Step-3.7 / DeepSeek-V3-class MoEs. `correction_bias` is added
2700    /// only to the top-k key; returned weights use the un-biased sigmoid score. `active` masks
2701    /// original expert ids before top-k. Exact key ties choose the smaller original id.
2702    #[allow(clippy::too_many_arguments)]
2703    pub fn moe_router_sigmoid_topk(&self, logits: &CudaSlice<f32>, t: usize, n_expert: usize,
2704                                    n_used: usize, active_count: usize,
2705                                    correction_bias: &CudaSlice<f32>,
2706                                    active: &CudaSlice<u8>, scaling_factor: f32, route_norm: bool)
2707                                    -> Result<(CudaSlice<i32>, CudaSlice<f32>),
2708                                              Box<dyn std::error::Error>> {
2709        crate::sigrouter_contract::validate_active_count(n_used, active_count)?;
2710        if n_expert == 0 || n_expert > 1024 || n_used == 0 || n_used > n_expert {
2711            return Err(format!(
2712                "sigmoid router shape unsupported: n_expert={n_expert}, n_used={n_used}",
2713            ).into());
2714        }
2715        if logits.len() < t * n_expert || correction_bias.len() != n_expert
2716            || active.len() != n_expert {
2717            return Err(format!(
2718                "sigmoid router buffer mismatch: logits={} bias={} active={} expected logits>={} row={}",
2719                logits.len(), correction_bias.len(), active.len(), t * n_expert, n_expert,
2720            ).into());
2721        }
2722        let f = self.func("moe_router_sigmoid_topk_f32");
2723        let mut sel_idx = self.alloc_uninit::<i32>(t * n_used)?;
2724        let mut sel_w = self.alloc_uninit::<f32>(t * n_used)?;
2725        let threads = n_expert.div_ceil(32) * 32;
2726        let cfg = LaunchConfig { grid_dim: (t as u32, 1, 1), block_dim: (threads as u32, 1, 1),
2727                                 shared_mem_bytes: 0 };
2728        let (ne, nu, rn) = (n_expert as i32, n_used as i32, i32::from(route_norm));
2729        let __s_b = self.gpu.stream();
2730        let mut b = __s_b.launch_builder(&f);
2731        b.arg(logits).arg(correction_bias).arg(active).arg(&mut sel_idx).arg(&mut sel_w)
2732         .arg(&ne).arg(&nu).arg(&scaling_factor).arg(&rn);
2733        unsafe { b.launch(cfg)?; }
2734        Ok((sel_idx, sel_w))
2735    }
2736
2737    /// Single-sync pinned readback twin of `moe_router_sigmoid_topk`. This preserves the existing
2738    /// grouped/staged dispatch contract while replacing the full-logit DtoH plus host sigmoid/sort.
2739    #[allow(clippy::too_many_arguments)]
2740    pub fn moe_router_sigmoid_topk_host(
2741        &self,
2742        logits: &CudaSlice<f32>,
2743        t: usize,
2744        n_expert: usize,
2745        n_used: usize,
2746        active_count: usize,
2747        correction_bias: &CudaSlice<f32>,
2748        active: &CudaSlice<u8>,
2749        scaling_factor: f32,
2750        route_norm: bool,
2751    ) -> Result<(Vec<u32>, Vec<f32>), Box<dyn std::error::Error>> {
2752        let (sel_idx, sel_w) = self.moe_router_sigmoid_topk(
2753            logits, t, n_expert, n_used, active_count, correction_bias, active, scaling_factor,
2754            route_norm,
2755        )?;
2756        let n = t * n_used;
2757        let bytes = n * 8;
2758        let mut guard = self.router_stage.lock().unwrap();
2759        if guard.as_ref().map(|p| p.cap < bytes).unwrap_or(true) {
2760            *guard = Some(PinnedStage::new(bytes.max(4096))?);
2761        }
2762        let stage = guard.as_mut().unwrap();
2763        let (si, sw) = unsafe {
2764            (std::slice::from_raw_parts_mut(stage.ptr as *mut i32, n),
2765             std::slice::from_raw_parts_mut(stage.ptr.add(n * 4) as *mut f32, n))
2766        };
2767        self.gpu.stream().memcpy_dtoh(&sel_idx, si)?;
2768        self.gpu.stream().memcpy_dtoh(&sel_w, sw)?;
2769        self.gpu.stream().synchronize()?;
2770        Ok((si.iter().map(|&i| i as u32).collect(), sw.to_vec()))
2771    }
2772
2773    /// EDGE-1 §C.2: async H2D of `host_bytes` into `scratch[off..]` on the COPY stream, returning a
2774    /// recorded event the compute stream can `wait` on before the dependent GEMM. Used for in-token
2775    /// expert prefetch (pipeline by one). `host_bytes` should be pinned for a true DMA (§C.1).
2776    pub fn stage_expert_async(&self, host_bytes: &[u8], scratch: &mut CudaSlice<u8>, off: usize)
2777                              -> Result<cudarc::driver::CudaEvent, Box<dyn std::error::Error>> {
2778        let mut dst = scratch.slice_mut(off..off + host_bytes.len());
2779        self.copy_stream.memcpy_htod(host_bytes, &mut dst)?;
2780        Ok(self.copy_stream.record_event(None)?)
2781    }
2782
2783    /// Make the compute stream wait for an async copy event (the consumer side of `stage_expert_async`).
2784    pub fn compute_wait(&self, ev: &cudarc::driver::CudaEvent) -> Result<(), Box<dyn std::error::Error>> {
2785        self.gpu.stream().wait(ev)?;
2786        Ok(())
2787    }
2788
2789    /// qmatvec over a byte sub-range of a (resident/scratch) CudaSlice<u8> holding ONE expert
2790    /// matrix. x is a CudaView<f32> (a sliced row of z, or a sliced activation). Reuses the
2791    /// validated qmatvec_f32 dequant path (NOT a fast path — the correctness gate). The
2792    /// CudaView base+offset pointer is honored by the launch arg.
2793    pub fn qmatvec_view(&self, w: &CudaSlice<u8>, range: std::ops::Range<usize>,
2794                        x: &cudarc::driver::CudaView<f32>, m: usize, in_f: usize, out_f: usize,
2795                        qtype: i32, row_bytes: usize)
2796                        -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2797        let f = self.func("qmatvec_f32");
2798        let wv = w.slice(range);  // CudaView<u8>, offset honored
2799        let mut y = self.alloc_uninit::<f32>(m * out_f)?;  // full-overwrite output: skip memset
2800        let cfg = LaunchConfig { grid_dim: (out_f as u32, m as u32, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
2801        let (inf, outf, mi, qt, rb) = (in_f as i32, out_f as i32, m as i32, qtype, row_bytes as i64);
2802        let __s_b = self.gpu.stream();
2803        let mut b = __s_b.launch_builder(&f);
2804        b.arg(&wv).arg(x).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&qt).arg(&rb);
2805        unsafe { b.launch(cfg)?; }
2806        Ok(y)
2807    }
2808
2809    /// STAGE-2 GROUPED DECODE (2026-07-04): one MoE layer's gate+up+SiLU for all `n_used` routed
2810    /// experts of ONE token in ONE launch (replaces 8x qmatvec(gate) + 8x qmatvec(up) + 8x
2811    /// silu_mul = 24 launches). `gp`/`up` are the 8 expert weight-block device pointers (SLRU
2812    /// cache slots — fixed-address, stable for the launch). Returns act [n_used, n_ff].
2813    /// BIT-IDENTICAL to the sequential chain: each dot reproduces qmatvec_f32's exact 256-thread
2814    /// reduction; the SiLU epilogue is silu_mul_f32's exact expression (see kernel header).
2815    #[allow(clippy::too_many_arguments)]
2816    /// dp4a q8 twins (MoE expert dp4a arc, 2026-07-06): same contract as the _f32 versions but
2817    /// consume a PRE-QUANTIZED q8_1 activation. FP-order differs from _f32 (int dot + warp tree)
2818    /// — the argmax/stream-identity battery arbitrates; MEMRA_MOE_Q8=0 restores f32.
2819    pub fn moe_gate_up_silu8_q8(&self, gp: WPtr8, up: WPtr8,
2820                                aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
2821                                in_f: usize, n_ff: usize, n_used: usize, qt_g: i32, qt_u: i32,
2822                                rb_g: usize, rb_u: usize)
2823                                -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2824        let f = self.func("moe_gate_up_silu8_q8");
2825        let mut act = self.alloc_uninit::<f32>(n_used * n_ff)?;
2826        let cfg = LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
2827                                 block_dim: (32, 1, 1), shared_mem_bytes: 0 };
2828        let (inf, nff, rbg, rbu) = (in_f as i32, n_ff as i32, rb_g as i64, rb_u as i64);
2829        let __s_b = self.gpu.stream();
2830        let mut b = __s_b.launch_builder(&f);
2831        b.arg(&gp).arg(&up).arg(aq).arg(ad).arg(&mut act)
2832         .arg(&inf).arg(&nff).arg(&qt_g).arg(&qt_u).arg(&rbg).arg(&rbu);
2833        unsafe { b.launch(cfg)?; }
2834        Ok(act)
2835    }
2836
2837    #[allow(clippy::too_many_arguments)]
2838    pub fn moe_down8_fma_q8(&self, dp: WPtr8, w: F32x8,
2839                            aq2: &CudaSlice<i8>, ad2: &CudaSlice<f32>,
2840                            dst: &mut cudarc::driver::CudaViewMut<f32>,
2841                            in_f: usize, out_f: usize, n_used: usize, qt: i32, rb: usize)
2842                            -> Result<(), Box<dyn std::error::Error>> {
2843        let f = self.func("moe_down8_fma_q8");
2844        let cfg = LaunchConfig { grid_dim: (out_f as u32, 1, 1),
2845                                 block_dim: (32, 1, 1), shared_mem_bytes: 0 };
2846        let (inf, outf, nu, rbi) = (in_f as i32, out_f as i32, n_used as i32, rb as i64);
2847        let __s_b = self.gpu.stream();
2848        let mut b = __s_b.launch_builder(&f);
2849        b.arg(&dp).arg(&w).arg(aq2).arg(ad2).arg(dst)
2850         .arg(&inf).arg(&outf).arg(&nu).arg(&qt).arg(&rbi);
2851        unsafe { b.launch(cfg)?; }
2852        Ok(())
2853    }
2854
2855    /// q8 sequential expert matvec (staged path twin of qmatvec_view for IQ3_S/IQ4_XS).
2856    pub fn qmatvec_expert_q8(&self, w: &CudaSlice<u8>, range: std::ops::Range<usize>,
2857                             aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, m: usize,
2858                             in_f: usize, out_f: usize, qtype: i32, row_bytes: usize)
2859                             -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2860        let f = self.func("qmatvec_expert_q8");
2861        let wv = w.slice(range);
2862        let mut y = self.alloc_uninit::<f32>(m * out_f)?;
2863        const ROWS: u32 = 4;   // MEMRA_MMVQ_ROWS
2864        let cfg = LaunchConfig { grid_dim: ((out_f as u32 + ROWS - 1) / ROWS, m as u32, 1),
2865                                 block_dim: (32, ROWS, 1), shared_mem_bytes: 0 };
2866        let (inf, outf, mi, rbi) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
2867        let __s_b = self.gpu.stream();
2868        let mut b = __s_b.launch_builder(&f);
2869        b.arg(&wv).arg(aq).arg(ad).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&qtype).arg(&rbi);
2870        unsafe { b.launch(cfg)?; }
2871        Ok(y)
2872    }
2873
2874    pub fn moe_gate_up_silu8(&self, gp: WPtr8, up: WPtr8, x: &cudarc::driver::CudaView<f32>,
2875                             in_f: usize, n_ff: usize, n_used: usize, qt_g: i32, qt_u: i32,
2876                             rb_g: usize, rb_u: usize)
2877                             -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2878        let f = self.func("moe_gate_up_silu8_f32");
2879        let mut act = self.alloc_uninit::<f32>(n_used * n_ff)?;  // fully overwritten
2880        let cfg = LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
2881                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
2882        let (inf, nff, rbg, rbu) = (in_f as i32, n_ff as i32, rb_g as i64, rb_u as i64);
2883        let __s_b = self.gpu.stream();
2884        let mut b = __s_b.launch_builder(&f);
2885        b.arg(&gp).arg(&up).arg(x).arg(&mut act)
2886         .arg(&inf).arg(&nff).arg(&qt_g).arg(&qt_u).arg(&rbg).arg(&rbu);
2887        unsafe { b.launch(cfg)?; }
2888        Ok(act)
2889    }
2890
2891    /// STAGE-2 GROUPED DECODE: one MoE layer's down-proj + weighted accumulation for all `n_used`
2892    /// routed experts in ONE launch (replaces 8x qmatvec(down) + 8x axpy = 16 launches), writing
2893    /// the token's moe_out row DIRECTLY (`dst` is the zeroed row; the in-kernel slot-ordered
2894    /// __fmaf_rn chain starting at 0.0f reproduces the sequential axpy_f32 accumulation into the
2895    /// zeroed row bit-for-bit — the A2 byte-identity scheme at m=1).
2896    #[allow(clippy::too_many_arguments)]
2897    pub fn moe_down8_fma_into(&self, dp: WPtr8, w: F32x8, act: &CudaSlice<f32>,
2898                              dst: &mut cudarc::driver::CudaViewMut<f32>,
2899                              in_f: usize, out_f: usize, n_used: usize, qt: i32, rb: usize)
2900                              -> Result<(), Box<dyn std::error::Error>> {
2901        let f = self.func("moe_down8_fma_f32");
2902        let cfg = LaunchConfig { grid_dim: (out_f as u32, 1, 1),
2903                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
2904        let (inf, outf, nu, rbv) = (in_f as i32, out_f as i32, n_used as i32, rb as i64);
2905        let __s_b = self.gpu.stream();
2906        let mut b = __s_b.launch_builder(&f);
2907        b.arg(&dp).arg(&w).arg(act).arg(dst).arg(&inf).arg(&outf).arg(&nu).arg(&qt).arg(&rbv);
2908        unsafe { b.launch(cfg)?; }
2909        Ok(())
2910    }
2911
2912    /// LAUNCH-STRUCTURE STAGE 3: device-dispatch twin of `moe_gate_up_silu8` for FULLY-RESIDENT
2913    /// layers. The expert ids come from the router kernel's DEVICE `sel` output (no DtoH) and the
2914    /// weight pointers from the per-layer device table `[3, n_expert]` of slot base addresses.
2915    /// BIT-IDENTICAL math (same grid/block/reduction; only the pointer/id source differs).
2916    #[allow(clippy::too_many_arguments)]
2917    /// dp4a q8 twin of the _dev pair (resident-experts arc).
2918    ///
2919    /// GEOMETRY VARIANTS (multirow/occupancy arc 2026-07-05): all outputs are BIT-IDENTICAL to
2920    /// the base one-warp-per-(row,slot) kernel (same expert_dot_g g-order + warp tree per row;
2921    /// down's FMA chain stays slot-ordered serial). Seams:
2922    ///   MEMRA_MOE_DEVQ8_GU   = 0(base) | 1 | 2 | 4 -> _r{1,2,4} multirow twin (RPW rows/warp)
2923    ///                       | s2 (gate/up warp split) | s2z (s2 + WPB rows packed per block)
2924    ///                       | gs4 (gate/up x low/high-group 4-warp split, nsb==64 only)
2925    ///                       | u64 (nsb==64 unrolled ILP twin, geometry unchanged)
2926    ///   MEMRA_MOE_DEVQ8_WPB  = warps per block for _r twins / z-rows for s2z (default 4)
2927    ///   MEMRA_MOE_DEVQ8_DOWN = auto(default: w8h2 when in_f==512 & n_used<=8 — measured +3.8%
2928    ///                       decode on 35B/G7e) | 0 (base one-warp serial-slot) | 1 | 2 | 4 ->
2929    ///                       _w8r{1,2,4} slot-parallel twin | h2 (half-warp dual-row, nsb==16
2930    ///                       only) | w8h2 (h2 x slot-parallel)
2931    #[allow(clippy::too_many_arguments)]
2932    /// MoE PREFILL pair-batch matvec: one launch covers all (token,expert) pairs for one proj.
2933    #[allow(clippy::too_many_arguments)]
2934    pub fn moe_pairs_matvec_q8(&self, table: &CudaSlice<u64>, proj: i32,
2935                               pair_tok: &CudaSlice<i32>, pair_ex: &CudaSlice<i32>,
2936                               aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
2937                               in_f: usize, out_f: usize, n_expert: usize, n_pairs: usize,
2938                               qtype: i32, row_bytes: usize)
2939                               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2940        let f = self.func("moe_pairs_matvec_q8");
2941        let mut y = self.alloc_uninit::<f32>(n_pairs * out_f)?;
2942        const ROWS: u32 = 4;
2943        let cfg = LaunchConfig { grid_dim: ((out_f as u32 + ROWS - 1) / ROWS, n_pairs as u32, 1),
2944                                 block_dim: (32, ROWS, 1), shared_mem_bytes: 0 };
2945        let (inf, outf, ne, np, rbi) = (in_f as i32, out_f as i32, n_expert as i32,
2946                                        n_pairs as i32, row_bytes as i64);
2947        let __s_b = self.gpu.stream();
2948        let mut b = __s_b.launch_builder(&f);
2949        b.arg(table).arg(&proj).arg(pair_tok).arg(pair_ex).arg(aq).arg(ad).arg(&mut y)
2950         .arg(&inf).arg(&outf).arg(&ne).arg(&np).arg(&qtype).arg(&rbi);
2951        unsafe { b.launch(cfg)?; }
2952        Ok(y)
2953    }
2954
2955    /// Expert-major pair matvec (weight-reuse across each expert's token group).
2956    #[allow(clippy::too_many_arguments)]
2957    pub fn moe_pairs_matvec_q8_em(&self, table: &CudaSlice<u64>, proj: i32,
2958                                  ex_ids: &CudaSlice<i32>, ex_off: &CudaSlice<i32>,
2959                                  ex_pairs: &CudaSlice<i32>, pair_tok: &CudaSlice<i32>,
2960                                  aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
2961                                  in_f: usize, out_f: usize, n_expert: usize, n_active: usize,
2962                                  n_pairs: usize, qtype: i32, row_bytes: usize)
2963                                  -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2964        let f = self.func("moe_pairs_matvec_q8_em");
2965        let mut y = self.alloc_uninit::<f32>(n_pairs * out_f)?;
2966        const ROWS: u32 = 4;
2967        let cfg = LaunchConfig { grid_dim: ((out_f as u32 + ROWS - 1) / ROWS, n_active as u32, 1),
2968                                 block_dim: (32, ROWS, 1), shared_mem_bytes: 0 };
2969        let (inf, outf, ne, na, rbi) = (in_f as i32, out_f as i32, n_expert as i32,
2970                                        n_active as i32, row_bytes as i64);
2971        let __s_b = self.gpu.stream();
2972        let mut b = __s_b.launch_builder(&f);
2973        b.arg(table).arg(&proj).arg(ex_ids).arg(ex_off).arg(ex_pairs).arg(pair_tok)
2974         .arg(aq).arg(ad).arg(&mut y)
2975         .arg(&inf).arg(&outf).arg(&ne).arg(&na).arg(&qtype).arg(&rbi);
2976        unsafe { b.launch(cfg)?; }
2977        Ok(y)
2978    }
2979
2980    // Decode-once expert-major MMQ (rung 3). Same CSR inputs/geometry as _em; kernel dequants each
2981    // weight group once per (row,group) then dp4a's across the expert's token group.
2982    #[allow(clippy::too_many_arguments)]
2983    pub fn moe_pairs_matvec_q8_dec(&self, table: &CudaSlice<u64>, proj: i32,
2984                                   ex_ids: &CudaSlice<i32>, ex_off: &CudaSlice<i32>,
2985                                   ex_pairs: &CudaSlice<i32>, pair_tok: &CudaSlice<i32>,
2986                                   aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
2987                                   in_f: usize, out_f: usize, n_expert: usize, n_active: usize,
2988                                   n_pairs: usize, qtype: i32, row_bytes: usize)
2989                                   -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
2990        let f = self.func("moe_pairs_matvec_q8_dec");
2991        let mut y = self.alloc_uninit::<f32>(n_pairs * out_f)?;
2992        const ROWS: u32 = 4;
2993        let cfg = LaunchConfig { grid_dim: ((out_f as u32 + ROWS - 1) / ROWS, n_active as u32, 1),
2994                                 block_dim: (32, ROWS, 1), shared_mem_bytes: 0 };
2995        let (inf, outf, ne, na, rbi) = (in_f as i32, out_f as i32, n_expert as i32,
2996                                        n_active as i32, row_bytes as i64);
2997        let __s_b = self.gpu.stream();
2998        let mut b = __s_b.launch_builder(&f);
2999        b.arg(table).arg(&proj).arg(ex_ids).arg(ex_off).arg(ex_pairs).arg(pair_tok)
3000         .arg(aq).arg(ad).arg(&mut y)
3001         .arg(&inf).arg(&outf).arg(&ne).arg(&na).arg(&qtype).arg(&rbi);
3002        unsafe { b.launch(cfg)?; }
3003        Ok(y)
3004    }
3005
3006    pub fn moe_pairs_gelu_mul(&self, gate: &CudaSlice<f32>, up: &CudaSlice<f32>, n: usize)
3007                              -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3008        let f = self.func("moe_pairs_gelu_mul");
3009        let mut act = self.alloc_uninit::<f32>(n)?;
3010        let cfg = LaunchConfig::for_num_elems(n as u32);
3011        let nl = n as i64;
3012        let __s_b = self.gpu.stream();
3013        let mut b = __s_b.launch_builder(&f);
3014        b.arg(gate).arg(up).arg(&mut act).arg(&nl);
3015        unsafe { b.launch(cfg)?; }
3016        Ok(act)
3017    }
3018
3019    pub fn moe_pairs_silu_mul(&self, gate: &CudaSlice<f32>, up: &CudaSlice<f32>, n: usize)
3020                              -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3021        let f = self.func("moe_pairs_silu_mul");
3022        let mut act = self.alloc_uninit::<f32>(n)?;
3023        let cfg = LaunchConfig::for_num_elems(n as u32);
3024        let nl = n as i64;
3025        let __s_b = self.gpu.stream();
3026        let mut b = __s_b.launch_builder(&f);
3027        b.arg(gate).arg(up).arg(&mut act).arg(&nl);
3028        unsafe { b.launch(cfg)?; }
3029        Ok(act)
3030    }
3031
3032    #[allow(clippy::too_many_arguments)]
3033    pub fn moe_pairs_scatter(&self, y_down: &CudaSlice<f32>, pair_w: &CudaSlice<f32>,
3034                             tok_pair_off: &CudaSlice<i32>, tok_pair_ids: &CudaSlice<i32>,
3035                             moe_out: &mut CudaSlice<f32>, t: usize, n_embd: usize)
3036                             -> Result<(), Box<dyn std::error::Error>> {
3037        let f = self.func("moe_pairs_scatter");
3038        let cfg = LaunchConfig { grid_dim: (((n_embd + 255) / 256) as u32, t as u32, 1),
3039                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
3040        let ne = n_embd as i32;
3041        let __s_b = self.gpu.stream();
3042        let mut b = __s_b.launch_builder(&f);
3043        b.arg(y_down).arg(pair_w).arg(tok_pair_off).arg(tok_pair_ids).arg(moe_out).arg(&ne);
3044        unsafe { b.launch(cfg)?; }
3045        Ok(())
3046    }
3047
3048    /// gemma4 GELU twin of moe_gate_up_silu8_dev_q8 (base geometry — slot-packed j8/j8r2
3049    /// twins probed 2026-08-01 g26 decode dig: bit-identical rows, -2.5%/-2.9% whole-model
3050    /// decode x3 interleaved -> refuted and killed; research/g26-decode-20260801/receipts.md).
3051    #[allow(clippy::too_many_arguments)]
3052    pub fn moe_gate_up_gelu8_dev_q8(&self, table: &CudaSlice<u64>, sel: &cudarc::driver::CudaView<i32>,
3053                                    aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
3054                                    in_f: usize, n_ff: usize, n_used: usize, n_expert: usize,
3055                                    qt_g: i32, qt_u: i32, rb_g: usize, rb_u: usize)
3056                                    -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3057        let mut act = self.alloc_uninit::<f32>(n_used * n_ff)?;
3058        let (inf, nff, ne, rbg, rbu) = (in_f as i32, n_ff as i32, n_expert as i32,
3059                                        rb_g as i64, rb_u as i64);
3060        let f = self.func("moe_gate_up_gelu8_dev_q8");
3061        let cfg = LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
3062                                 block_dim: (32, 1, 1), shared_mem_bytes: 0 };
3063        let __s_b = self.gpu.stream();
3064        let mut b = __s_b.launch_builder(&f);
3065        b.arg(table).arg(sel).arg(aq).arg(ad).arg(&mut act)
3066         .arg(&inf).arg(&nff).arg(&ne).arg(&qt_g).arg(&qt_u).arg(&rbg).arg(&rbu);
3067        unsafe { b.launch(cfg)?; }
3068        Ok(act)
3069    }
3070
3071    /// gemma4 GELU rows twin (verify): one launch over (n_ff, n_used, t).
3072    #[allow(clippy::too_many_arguments)]
3073    pub fn moe_gate_up_gelu8_dev_q8_rows(&self, table: &CudaSlice<u64>, sel: &CudaSlice<i32>,
3074                                         aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, t: usize,
3075                                         in_f: usize, n_ff: usize, n_used: usize, n_expert: usize,
3076                                         qt_g: i32, qt_u: i32, rb_g: usize, rb_u: usize)
3077                                         -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3078        let mut act = self.alloc_uninit::<f32>(t * n_used * n_ff)?;
3079        let (inf, nff, ne, rbg, rbu, nu) = (in_f as i32, n_ff as i32, n_expert as i32,
3080                                            rb_g as i64, rb_u as i64, n_used as i32);
3081        let f = self.func("moe_gate_up_gelu8_dev_q8_rows");
3082        let cfg = LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, t as u32),
3083                                 block_dim: (32, 1, 1), shared_mem_bytes: 0 };
3084        let __s_b = self.gpu.stream();
3085        let mut b = __s_b.launch_builder(&f);
3086        b.arg(table).arg(sel).arg(aq).arg(ad).arg(&mut act)
3087         .arg(&inf).arg(&nff).arg(&ne).arg(&qt_g).arg(&qt_u).arg(&rbg).arg(&rbu).arg(&nu);
3088        unsafe { b.launch(cfg)?; }
3089        Ok(act)
3090    }
3091
3092    /// gemma4 GELU CSR twin (verify dedup: owner block serves every pair of its expert).
3093    #[allow(clippy::too_many_arguments)]
3094    pub fn moe_gate_up_gelu8_dev_q8_csr(&self, table: &CudaSlice<u64>, sel: &CudaSlice<i32>,
3095                                        aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, n_pairs: usize,
3096                                        in_f: usize, n_ff: usize, n_used: usize, n_expert: usize,
3097                                        qt_g: i32, qt_u: i32, rb_g: usize, rb_u: usize)
3098                                        -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3099        let mut act = self.alloc_uninit::<f32>(n_pairs * n_ff)?;
3100        let (inf, nff, ne, rbg, rbu, nu, npi) = (in_f as i32, n_ff as i32, n_expert as i32,
3101                                                 rb_g as i64, rb_u as i64, n_used as i32,
3102                                                 n_pairs as i32);
3103        let f = self.func("moe_gate_up_gelu8_dev_q8_csr");
3104        let cfg = LaunchConfig { grid_dim: (n_ff as u32, n_pairs as u32, 1),
3105                                 block_dim: (32, 1, 1), shared_mem_bytes: 0 };
3106        let __s_b = self.gpu.stream();
3107        let mut b = __s_b.launch_builder(&f);
3108        b.arg(table).arg(sel).arg(aq).arg(ad).arg(&mut act)
3109         .arg(&inf).arg(&nff).arg(&ne).arg(&qt_g).arg(&qt_u).arg(&rbg).arg(&rbu).arg(&nu).arg(&npi);
3110        unsafe { b.launch(cfg)?; }
3111        Ok(act)
3112    }
3113
3114    /// gemma4 generic down rows twin (verify): one launch over (out_f, 1, t).
3115    #[allow(clippy::too_many_arguments)]
3116    pub fn moe_down8_fma_dev_q8_rows_g(&self, table: &CudaSlice<u64>, sel: &CudaSlice<i32>,
3117                                       w: &CudaSlice<f32>, aq2: &CudaSlice<i8>, ad2: &CudaSlice<f32>,
3118                                       dst: &mut CudaSlice<f32>, t: usize,
3119                                       in_f: usize, out_f: usize, n_used: usize, n_expert: usize,
3120                                       qt: i32, rb: usize)
3121                                       -> Result<(), Box<dyn std::error::Error>> {
3122        let (inf, outf, nu, ne, rbi) = (in_f as i32, out_f as i32, n_used as i32,
3123                                        n_expert as i32, rb as i64);
3124        let f = self.func("moe_down8_fma_dev_q8_rows_g");
3125        let cfg = LaunchConfig { grid_dim: (out_f as u32, 1, t as u32),
3126                                 block_dim: (32, 1, 1), shared_mem_bytes: 0 };
3127        let __s_b = self.gpu.stream();
3128        let mut b = __s_b.launch_builder(&f);
3129        b.arg(table).arg(sel).arg(w).arg(aq2).arg(ad2).arg(dst)
3130         .arg(&inf).arg(&outf).arg(&nu).arg(&ne).arg(&qt).arg(&rbi);
3131        unsafe { b.launch(cfg)?; }
3132        Ok(())
3133    }
3134
3135    /// rp_q4 microprobe (2026-07-10 verify-trunk lever): b4 GGUF-block layout vs the Q4_0
3136    /// split-plane twin on the wq-class shape. Returns (blk_us, rp_us) after asserting bitwise
3137    /// identity. Bench-only surface (rp_q4_probe bin); no production dispatch reads this.
3138    pub fn rp_probe_q4(&self, m: usize) -> Result<(f64, f64), Box<dyn std::error::Error>> {
3139        let (out_f, in_f) = (2048usize, 2816usize);
3140        let nblk = in_f / 32;
3141        let mut seed = 0x9E3779B97F4A7C15u64;
3142        let mut rng = move || { seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); (seed >> 33) as u8 };
3143        let mut w = vec![0u8; out_f * nblk * 18];
3144        for b in w.iter_mut() { *b = rng(); }
3145        for r in 0..out_f {
3146            for g in 0..nblk {
3147                let off = (r * nblk + g) * 18;
3148                w[off] = 0x00; w[off + 1] = 0x2C;   // sane half d
3149            }
3150        }
3151        let qplane = out_f * nblk * 16;
3152        let mut wrp = vec![0u8; w.len()];
3153        for r in 0..out_f {
3154            for g in 0..nblk {
3155                let src = &w[(r * nblk + g) * 18..(r * nblk + g) * 18 + 18];
3156                wrp[qplane + (r * nblk + g) * 2..qplane + (r * nblk + g) * 2 + 2]
3157                    .copy_from_slice(&src[0..2]);
3158                wrp[(r * nblk + g) * 16..(r * nblk + g) * 16 + 16].copy_from_slice(&src[2..18]);
3159            }
3160        }
3161        let w_d = self.htod_bytes(&w)?;
3162        let wrp_d = self.htod_bytes(&wrp)?;
3163        let mut aq = vec![0i8; m * in_f];
3164        for v in aq.iter_mut() { *v = rng() as i8; }
3165        let aq_d = self.htod_i8(&aq)?;
3166        let ad_d = self.htod(&vec![0.03125f32; m * nblk])?;
3167        let mut y0 = self.alloc_uninit::<f32>(m * out_f)?;
3168        let mut y1 = self.alloc_uninit::<f32>(m * out_f)?;
3169        const RPB: u32 = 4;
3170        let cfg = LaunchConfig { grid_dim: ((out_f as u32).div_ceil(RPB), 1, 1),
3171                                 block_dim: (32, RPB, 1), shared_mem_bytes: 0 };
3172        let (inf, outf, mi) = (in_f as i32, out_f as i32, m as i32);
3173        let (rb, qp) = ((nblk * 18) as i64, qplane as i64);
3174        let fb = self.func("qmatvec_q4_0_mmvq_b4");
3175        let fr = self.func("qmatvec_q4_0_mmvq_b4_rp");
3176        {
3177            let __s_b = self.gpu.stream();
3178            let mut b = __s_b.launch_builder(&fb);
3179            b.arg(&w_d).arg(&aq_d).arg(&ad_d).arg(&mut y0).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
3180            unsafe { b.launch(cfg)?; }
3181            let __s_b = self.gpu.stream();
3182            let mut b = __s_b.launch_builder(&fr);
3183            b.arg(&wrp_d).arg(&aq_d).arg(&ad_d).arg(&mut y1).arg(&inf).arg(&outf).arg(&mi).arg(&qp);
3184            unsafe { b.launch(cfg)?; }
3185        }
3186        self.gpu.stream().synchronize()?;
3187        let (h0, h1) = (self.dtoh(&y0)?, self.dtoh(&y1)?);
3188        let nd = h0.iter().zip(&h1).filter(|(a, b)| a.to_bits() != b.to_bits()).count();
3189        if nd != 0 { return Err(format!("rp twin not bitwise: {nd}/{} diffs", h0.len()).into()); }
3190        let mut time = |rp: bool| -> Result<f64, Box<dyn std::error::Error>> {
3191            self.gpu.stream().synchronize()?;
3192            let t0 = std::time::Instant::now();
3193            for _ in 0..500 {
3194                if rp {
3195                    let __s_b = self.gpu.stream();
3196                    let mut b = __s_b.launch_builder(&fr);
3197                    b.arg(&wrp_d).arg(&aq_d).arg(&ad_d).arg(&mut y1)
3198                     .arg(&inf).arg(&outf).arg(&mi).arg(&qp);
3199                    unsafe { b.launch(cfg)?; }
3200                } else {
3201                    let __s_b = self.gpu.stream();
3202                    let mut b = __s_b.launch_builder(&fb);
3203                    b.arg(&w_d).arg(&aq_d).arg(&ad_d).arg(&mut y0)
3204                     .arg(&inf).arg(&outf).arg(&mi).arg(&rb);
3205                    unsafe { b.launch(cfg)?; }
3206                }
3207            }
3208            self.gpu.stream().synchronize()?;
3209            Ok(t0.elapsed().as_secs_f64() * 1e6 / 500.0)
3210        };
3211        let _ = time(false)?; let _ = time(true)?;   // warm
3212        Ok((time(false)?, time(true)?))
3213    }
3214
3215    /// Build the Q4_0 split-plane decode mirror for a 2D Quant tensor (device-side permutation,
3216    /// q4_0_split_rp_build). Raw bytes stay resident (prefill/gemm/Stage-A); the m<=8 decode
3217    /// dispatch prefers the mirror (_rp twins). No-op unless (Q4_0, 2D, mirror absent).
3218    /// VRAM cost == the tensor's weight size. MEMRA_Q4RP=0 disables at the call sites.
3219    pub fn build_q4_rp4(&self, t: &mut crate::model::GpuTensor)
3220                        -> Result<(), Box<dyn std::error::Error>> {
3221        use crate::model::GpuTensor;
3222        let GpuTensor::Quant { bytes, qtype, row_bytes, ne, rp4, .. } = t else { return Ok(()) };
3223        if *qtype != QT_Q4_0 || rp4.is_some() || ne.len() != 2 { return Ok(()); }
3224        let (in_f, out_f) = (ne[0] as usize, ne[1] as usize);
3225        if in_f % 32 != 0 || *row_bytes != (in_f / 32) * 18 { return Ok(()); }
3226        let nblk = in_f / 32;
3227        let mut dst = self.alloc_uninit::<u8>(out_f * nblk * 18)?;
3228        let f = self.func("q4_0_split_rp_build");
3229        let n = (out_f * nblk) as i32;
3230        let cfg = LaunchConfig { grid_dim: (((out_f * nblk) as u32).div_ceil(256), 1, 1),
3231                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
3232        let (of, nb) = (out_f as i32, nblk as i32);
3233        let _ = n;
3234        let __s_b = self.gpu.stream();
3235        let mut b = __s_b.launch_builder(&f);
3236        b.arg(&*bytes).arg(&mut dst).arg(&of).arg(&nb);
3237        unsafe { b.launch(cfg)?; }
3238        *rp4 = Some(dst);
3239        Ok(())
3240    }
3241
3242    /// Q8_0 twin of `build_q4_rp4` (H100 coalescing fix, 2026-07-26 ncu: GGUF 34B-stride
3243    /// weight loads hold Max Bandwidth at 41-46%; the split mirror makes them aligned 16B
3244    /// ldcs). Raw bytes stay resident (prefill GEMM/MMQ/fused m=1 launches read GGUF layout);
3245    /// the mmvq/batched decode arms prefer the mirror via `rp4`. Bit-identical outputs.
3246    pub fn build_q8_rp4(&self, t: &mut crate::model::GpuTensor)
3247                        -> Result<(), Box<dyn std::error::Error>> {
3248        use crate::model::GpuTensor;
3249        let GpuTensor::Quant { bytes, qtype, row_bytes, ne, rp4, .. } = t else { return Ok(()) };
3250        if *qtype != QT_Q8_0 || rp4.is_some() || ne.len() != 2 { return Ok(()); }
3251        let (in_f, out_f) = (ne[0] as usize, ne[1] as usize);
3252        if in_f % 32 != 0 || *row_bytes != (in_f / 32) * 34 { return Ok(()); }
3253        *rp4 = Some(self.build_q8_rp4_raw(bytes, in_f, out_f)?);
3254        Ok(())
3255    }
3256
3257    /// Raw rp-mirror build for gates/benches: split GGUF Q8_0 bytes into the qplane+dplane
3258    /// mirror without a GpuTensor (same kernel the loader path above uses).
3259    pub fn build_q8_rp4_raw(&self, bytes: &CudaSlice<u8>, in_f: usize, out_f: usize)
3260                            -> Result<CudaSlice<u8>, Box<dyn std::error::Error>> {
3261        assert!(in_f % 32 == 0);
3262        let nblk = in_f / 32;
3263        let mut dst = self.alloc_uninit::<u8>(out_f * nblk * 34)?;
3264        let f = self.func("q8_0_split_rp_build");
3265        let cfg = LaunchConfig { grid_dim: (((out_f * nblk) as u32).div_ceil(256), 1, 1),
3266                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
3267        let (of, nb) = (out_f as i32, nblk as i32);
3268        let __s_b = self.gpu.stream();
3269        let mut b = __s_b.launch_builder(&f);
3270        b.arg(&*bytes).arg(&mut dst).arg(&of).arg(&nb);
3271        unsafe { b.launch(cfg)?; }
3272        Ok(dst)
3273    }
3274
3275    /// K-quant twins of `build_q8_rp4` (H100 K-quant coalescing fix, 2026-08-01 ncu on the
3276    /// q27 Q4_K_M decode: q4_K mmvq DRAM 41-54% with 65% excessive sectors, q6_K 40% with
3277    /// 78% — the 144B/210B superblock strides land every 4B weight load off-sector). The
3278    /// mirror re-packs each tensor into planes (q4_K: qs ++ 16B meta; q6_K: ql ++ qh ++
3279    /// scales ++ d — same total bytes) so every quant fetch is an aligned 16B ldcs. Raw
3280    /// bytes stay resident (prefill GEMM/dequant/Stage-A read GGUF layout); the mmvq/batched
3281    /// decode arms prefer the mirror via `rp4`. Bit-identical outputs.
3282    pub fn build_q4k_rp4(&self, t: &mut crate::model::GpuTensor)
3283                         -> Result<(), Box<dyn std::error::Error>> {
3284        use crate::model::GpuTensor;
3285        let GpuTensor::Quant { bytes, qtype, row_bytes, ne, rp4, .. } = t else { return Ok(()) };
3286        if *qtype != QT_Q4_K || rp4.is_some() || ne.len() != 2 { return Ok(()); }
3287        let (in_f, out_f) = (ne[0] as usize, ne[1] as usize);
3288        if in_f % 256 != 0 || *row_bytes != (in_f / 256) * 144 { return Ok(()); }
3289        *rp4 = Some(self.build_kq_rp4_raw(bytes, in_f, out_f, QT_Q4_K)?);
3290        Ok(())
3291    }
3292
3293    pub fn build_q6k_rp4(&self, t: &mut crate::model::GpuTensor)
3294                         -> Result<(), Box<dyn std::error::Error>> {
3295        use crate::model::GpuTensor;
3296        let GpuTensor::Quant { bytes, qtype, row_bytes, ne, rp4, .. } = t else { return Ok(()) };
3297        if *qtype != QT_Q6_K || rp4.is_some() || ne.len() != 2 { return Ok(()); }
3298        let (in_f, out_f) = (ne[0] as usize, ne[1] as usize);
3299        if in_f % 256 != 0 || *row_bytes != (in_f / 256) * 210 { return Ok(()); }
3300        *rp4 = Some(self.build_kq_rp4_raw(bytes, in_f, out_f, QT_Q6_K)?);
3301        Ok(())
3302    }
3303
3304    /// Raw K-quant rp-mirror build for gates/benches (same kernels the loader path uses).
3305    pub fn build_kq_rp4_raw(&self, bytes: &CudaSlice<u8>, in_f: usize, out_f: usize, qtype: i32)
3306                            -> Result<CudaSlice<u8>, Box<dyn std::error::Error>> {
3307        assert!(in_f % 256 == 0);
3308        let nsbk = in_f / 256;
3309        let (sb_bytes, kname) = match qtype {
3310            QT_Q4_K => (144usize, "q4_K_split_rp_build"),
3311            QT_Q6_K => (210usize, "q6_K_split_rp_build"),
3312            _ => return Err(format!("build_kq_rp4_raw: qtype {qtype} has no rp mirror").into()),
3313        };
3314        let mut dst = self.alloc_uninit::<u8>(out_f * nsbk * sb_bytes)?;
3315        let f = self.func(kname);
3316        let cfg = LaunchConfig { grid_dim: (((out_f * nsbk) as u32).div_ceil(256), 1, 1),
3317                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
3318        let (of, nb) = (out_f as i32, nsbk as i32);
3319        let __s_b = self.gpu.stream();
3320        let mut b = __s_b.launch_builder(&f);
3321        b.arg(&*bytes).arg(&mut dst).arg(&of).arg(&nb);
3322        unsafe { b.launch(cfg)?; }
3323        Ok(dst)
3324    }
3325
3326    /// MEMRA_KQRP seam: the K-quant (q4_K/q6_K) split-plane decode mirrors at model load.
3327    /// Default follows the Q8RP convention — ON on the Hopper lane (80GB pays the mirror
3328    /// VRAM), OFF elsewhere (a 24GB card cannot hold model + mirror + KV for the big trunks).
3329    pub fn kqrp_enabled() -> bool {
3330        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
3331        *ON.get_or_init(|| match std::env::var("MEMRA_KQRP").as_deref() {
3332            Ok("0") => false,
3333            Ok(_) => true,
3334            Err(_) => cfg!(memra_hopper_mma),
3335        })
3336    }
3337
3338    /// IN-PLACE split-plane swap (the 31B dense arc): build the split layout and REPLACE the
3339    /// GGUF bytes (zero extra steady-state VRAM — the transient peak is one tensor's size).
3340    /// The tensor's `rp` flag then routes every consumer (mmvq/batched `_rp` twins, the
3341    /// `qmatvec_gemm_q4_0_rp` prefill kernel). Callers gate on the fast path being active —
3342    /// the Stage-A f32 oracle (`MEMRA_FAST=0`) reads GGUF layout and must never see a swap.
3343    pub fn build_q4_rp_swap(&self, t: &mut crate::model::GpuTensor)
3344                            -> Result<bool, Box<dyn std::error::Error>> {
3345        self.build_q4_rp4(t)?;
3346        self.gpu.stream().synchronize()?;   // build kernel reads the GGUF bytes — drain BEFORE dropping them
3347        use crate::model::GpuTensor;
3348        let GpuTensor::Quant { bytes, rp4, rp, .. } = t else { return Ok(false) };
3349        match rp4.take() {
3350            Some(split) => {
3351                *bytes = split;   // the GGUF-layout buffer drops here
3352                *rp = true;
3353                Ok(true)
3354            }
3355            None => Ok(false),
3356        }
3357    }
3358
3359    /// MEMRA_Q4RP seam (default ON): the Q4_0 split-plane decode mirror at model load.
3360    pub fn q4rp_enabled() -> bool {
3361        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
3362        *ON.get_or_init(|| std::env::var("MEMRA_Q4RP").map(|v| v != "0").unwrap_or(true))
3363    }
3364
3365    /// gemma4-E4B: dense [t][row_elems] gather of layer il's rows from the strided prologue
3366    /// buffer ([t][n_layer][n_epl]; off = il*n_epl, stride = n_layer*n_epl).
3367    pub fn copy_rows_strided(&self, src: &CudaSlice<f32>, dst: &mut CudaSlice<f32>,
3368                             row_elems: usize, n_rows: usize, src_stride: usize, src_off: usize)
3369                             -> Result<(), Box<dyn std::error::Error>> {
3370        let f = self.func("copy_rows_strided_f32");
3371        let cfg = LaunchConfig { grid_dim: (((row_elems as u32 + 255) / 256).max(1), n_rows as u32, 1),
3372                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
3373        let (re, nr) = (row_elems as i32, n_rows as i32);
3374        let (st, off) = (src_stride as i64, src_off as i64);
3375        let __s_b = self.gpu.stream();
3376        let mut b = __s_b.launch_builder(&f);
3377        b.arg(src).arg(&mut *dst).arg(&re).arg(&nr).arg(&st).arg(&off);
3378        unsafe { b.launch(cfg)?; }
3379        Ok(())
3380    }
3381
3382    /// Async device u32 store (value rides the kernel ARG — no host-memory transfer/sync).
3383    pub fn u32_set_k(&self, dst: &mut CudaSlice<u32>, v: u32, idx: usize)
3384                     -> Result<(), Box<dyn std::error::Error>> {
3385        let f = self.func("u32_set_k");
3386        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (1, 1, 1), shared_mem_bytes: 0 };
3387        let ii = idx as i32;
3388        let __s_b = self.gpu.stream();
3389        let mut b = __s_b.launch_builder(&f);
3390        b.arg(dst).arg(&v).arg(&ii);
3391        unsafe { b.launch(cfg)?; }
3392        Ok(())
3393    }
3394
3395    /// counter += v (device-slot append advance; the +1 twin is `inc_seqlen`).
3396    pub fn i32_add_k(&self, d: &mut CudaSlice<i32>, v: i32) -> Result<(), Box<dyn std::error::Error>> {
3397        let f = self.func("i32_add_k");
3398        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
3399        let __s_b = self.gpu.stream();
3400        let mut b = __s_b.launch_builder(&f);
3401        b.arg(d).arg(&v);
3402        unsafe { b.launch(cfg)?; }
3403        Ok(())
3404    }
3405
3406    /// pos rows from a device counter: dst[i] = ctr[0] + i (verify-stream rope positions).
3407    pub fn i32_iota_from(&self, ctr: &CudaSlice<i32>, dst: &mut CudaSlice<i32>, n: usize)
3408                         -> Result<(), Box<dyn std::error::Error>> {
3409        let f = self.func("i32_iota_from");
3410        let cfg = LaunchConfig::for_num_elems(n as u32);
3411        let ni = n as i32;
3412        let __s_b = self.gpu.stream();
3413        let mut b = __s_b.launch_builder(&f);
3414        b.arg(ctr).arg(dst).arg(&ni);
3415        unsafe { b.launch(cfg)?; }
3416        Ok(())
3417    }
3418
3419    /// In-place trim-id translate: buf[idx] = map[buf[idx]] (FR-Spec d2t, async single-slot).
3420    pub fn u32_map_k(&self, buf: &mut CudaSlice<u32>, map: &CudaSlice<u32>, idx: usize)
3421                     -> Result<(), Box<dyn std::error::Error>> {
3422        let f = self.func("u32_map_k");
3423        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (1, 1, 1), shared_mem_bytes: 0 };
3424        let ii = idx as i32;
3425        let __s_b = self.gpu.stream();
3426        let mut b = __s_b.launch_builder(&f);
3427        b.arg(buf).arg(map).arg(&ii);
3428        unsafe { b.launch(cfg)?; }
3429        Ok(())
3430    }
3431
3432    /// Pack a[off..off+n1] ++ b[0..n2] into one buffer (single dtoh follows).
3433    #[allow(clippy::too_many_arguments)]
3434    pub fn u32_pack2(&self, a: &CudaSlice<u32>, off_a: usize, n1: usize,
3435                     b_in: &CudaSlice<u32>, n2: usize, out: &mut CudaSlice<u32>)
3436                     -> Result<(), Box<dyn std::error::Error>> {
3437        let f = self.func("u32_pack2");
3438        let cfg = LaunchConfig::for_num_elems((n1 + n2) as u32);
3439        let (oa, i1, i2) = (off_a as i32, n1 as i32, n2 as i32);
3440        let __s_b = self.gpu.stream();
3441        let mut b = __s_b.launch_builder(&f);
3442        b.arg(a).arg(&oa).arg(&i1).arg(b_in).arg(&i2).arg(out);
3443        unsafe { b.launch(cfg)?; }
3444        Ok(())
3445    }
3446
3447    /// gemma4 R3 device fold: w[i] *= s[sel[i]] over the router's [n] (sel, w) pair.
3448    pub fn moe_w_exscale(&self, w: &mut CudaSlice<f32>, sel: &CudaSlice<i32>,
3449                         s: &CudaSlice<f32>, n: usize) -> Result<(), Box<dyn std::error::Error>> {
3450        let f = self.func("moe_w_exscale");
3451        let cfg = LaunchConfig::for_num_elems(n as u32);
3452        let ni = n as i32;
3453        let __s_b = self.gpu.stream();
3454        let mut b = __s_b.launch_builder(&f);
3455        b.arg(w).arg(sel).arg(s).arg(&ni);
3456        unsafe { b.launch(cfg)?; }
3457        Ok(())
3458    }
3459
3460    /// Down-projection macro fold: w[i] *= macros[2*n_expert + sel[i]] on the device router
3461    /// weights (one launch per MoE layer, only for macro-carrying artifacts — see MoeWeights).
3462    pub fn moe_w_scale_by_expert(&self, w: &mut CudaSlice<f32>, sel: &CudaSlice<i32>,
3463                                 macros: &CudaSlice<f32>, n_expert: usize, n: usize)
3464                                 -> Result<(), Box<dyn std::error::Error>> {
3465        let f = self.func("moe_w_scale_by_expert");
3466        let cfg = LaunchConfig { grid_dim: (n.div_ceil(64) as u32, 1, 1),
3467                                 block_dim: (64, 1, 1), shared_mem_bytes: 0 };
3468        let (ne, nn) = (n_expert as i32, n as i32);
3469        let __s_b = self.gpu.stream();
3470        let mut b = __s_b.launch_builder(&f);
3471        b.arg(w).arg(sel).arg(macros).arg(&ne).arg(&nn);
3472        unsafe { b.launch(cfg)?; }
3473        Ok(())
3474    }
3475
3476    pub fn moe_gate_up_silu8_dev_q8(&self, table: &CudaSlice<u64>, sel: &cudarc::driver::CudaView<i32>,
3477                                    aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
3478                                    in_f: usize, n_ff: usize, n_used: usize, n_expert: usize,
3479                                    qt_g: i32, qt_u: i32, rb_g: usize, rb_u: usize,
3480                                    macros: &CudaSlice<f32>)
3481                                    -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3482        static GU: std::sync::OnceLock<(String, u32)> = std::sync::OnceLock::new();
3483        let (mode, wpb) = GU.get_or_init(|| {
3484            let mode = std::env::var("MEMRA_MOE_DEVQ8_GU").unwrap_or_default();
3485            let wpb = std::env::var("MEMRA_MOE_DEVQ8_WPB").ok()
3486                .and_then(|v| v.parse().ok()).unwrap_or(4u32).clamp(1, 16);
3487            (mode, wpb)
3488        });
3489        let (mode, wpb) = (mode.as_str(), *wpb);
3490        let mut act = self.alloc_uninit::<f32>(n_used * n_ff)?;
3491        let (inf, nff, ne, rbg, rbu) = (in_f as i32, n_ff as i32, n_expert as i32,
3492                                        rb_g as i64, rb_u as i64);
3493        let (f, cfg) = match mode {
3494            "1" | "2" | "4" => {
3495                let rpw: u32 = mode.parse().unwrap();
3496                let f = self.func(match rpw { 1 => "moe_gate_up_silu8_dev_q8_r1",
3497                                              2 => "moe_gate_up_silu8_dev_q8_r2",
3498                                              _ => "moe_gate_up_silu8_dev_q8_r4" });
3499                let rows_per_block = (rpw * wpb) as usize;
3500                let gx = n_ff.div_ceil(rows_per_block) as u32;
3501                (f, LaunchConfig { grid_dim: (gx, n_used as u32, 1),
3502                                   block_dim: (32, wpb, 1), shared_mem_bytes: 0 })
3503            }
3504            "j8" if n_used <= 32 => (self.func("moe_gate_up_silu8_dev_q8_j8"),
3505                     LaunchConfig { grid_dim: (n_ff as u32, 1, 1),
3506                                    block_dim: (32, n_used as u32, 1), shared_mem_bytes: 0 }),
3507            // SMEM-GRID twins (IQ3_S 2KB grid copied to shared, static smem — bit-identical dots)
3508            "vsm2" => {
3509                let f = self.func("moe_gate_up_silu8_dev_q8_vsm2");
3510                let sh = (rb_g + rb_u) as u32;
3511                use cudarc::driver::sys::CUfunction_attribute_enum as A;
3512                f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, sh as i32)?;
3513                (f, LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
3514                                   block_dim: (32, 1, 1), shared_mem_bytes: sh })
3515            }
3516            "vsm" => {
3517                let f = self.func("moe_gate_up_silu8_dev_q8_vsm");
3518                let sh = (rb_g + rb_u) as u32;
3519                use cudarc::driver::sys::CUfunction_attribute_enum as A;
3520                f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, sh as i32)?;
3521                (f, LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
3522                                   block_dim: (32, 1, 1), shared_mem_bytes: sh })
3523            }
3524            "sg" => (self.func("moe_gate_up_silu8_dev_q8_sg"),
3525                     LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
3526                                    block_dim: (32, 1, 1), shared_mem_bytes: 0 }),
3527            "j8sg" if n_used <= 32 => (self.func("moe_gate_up_silu8_dev_q8_j8sg"),
3528                     LaunchConfig { grid_dim: (n_ff as u32, 1, 1),
3529                                    block_dim: (32, n_used as u32, 1), shared_mem_bytes: 0 }),
3530            "u64" if in_f == 2048 => (self.func("moe_gate_up_silu8_dev_q8_u64"),
3531                     LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
3532                                    block_dim: (32, 1, 1), shared_mem_bytes: 0 }),
3533            "gs4" if in_f == 2048 => (self.func("moe_gate_up_silu8_dev_q8_gs4"),
3534                     LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
3535                                    block_dim: (32, 4, 1), shared_mem_bytes: 0 }),
3536            // _v twin (down8 lane 2026-07-08): wide-load IQ4_XS dot, base geometry, bit-identical.
3537            "v" | "" => (self.func("moe_gate_up_silu8_dev_q8_v"),
3538                    LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
3539                                   block_dim: (32, 1, 1), shared_mem_bytes: 0 }),
3540            "s2" => (self.func("moe_gate_up_silu8_dev_q8_s2"),
3541                     LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
3542                                    block_dim: (32, 2, 1), shared_mem_bytes: 0 }),
3543            "s2z" => {
3544                let rz = wpb.min(16);        // s2z smem tile is [16][2]
3545                (self.func("moe_gate_up_silu8_dev_q8_s2z"),
3546                 LaunchConfig { grid_dim: (n_ff.div_ceil(rz as usize) as u32, n_used as u32, 1),
3547                                block_dim: (32, 2, rz), shared_mem_bytes: 0 })
3548            }
3549            _ => (self.func("moe_gate_up_silu8_dev_q8"),
3550                  LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
3551                                 block_dim: (32, 1, 1), shared_mem_bytes: 0 }),
3552        };
3553        let __s_b = self.gpu.stream();
3554        let mut b = __s_b.launch_builder(&f);
3555        b.arg(table).arg(sel).arg(aq).arg(ad).arg(&mut act)
3556         .arg(&inf).arg(&nff).arg(&ne).arg(&qt_g).arg(&qt_u).arg(&rbg).arg(&rbu).arg(macros);
3557        unsafe { b.launch(cfg)?; }
3558        Ok(act)
3559    }
3560
3561    #[allow(clippy::too_many_arguments)]
3562    pub fn moe_down8_fma_dev_q8(&self, table: &CudaSlice<u64>, sel: &cudarc::driver::CudaView<i32>,
3563                                w: &cudarc::driver::CudaView<f32>,
3564                                aq2: &CudaSlice<i8>, ad2: &CudaSlice<f32>,
3565                                dst: &mut cudarc::driver::CudaViewMut<f32>,
3566                                in_f: usize, out_f: usize, n_used: usize, n_expert: usize,
3567                                qt: i32, rb: usize)
3568                                -> Result<(), Box<dyn std::error::Error>> {
3569        static DOWN: std::sync::OnceLock<String> = std::sync::OnceLock::new();
3570        let mode = DOWN.get_or_init(|| std::env::var("MEMRA_MOE_DEVQ8_DOWN").unwrap_or_default());
3571        let (inf, outf, nu, ne, rbi) = (in_f as i32, out_f as i32, n_used as i32,
3572                                        n_expert as i32, rb as i64);
3573        // the w8 twins' smem tile is [RPW][8] — n_used must fit the 8-slot tile;
3574        // the h2 twins are nsb==16 (in_f==512) shape-gated.
3575        let (f, cfg) = match mode.as_str() {
3576            m @ ("1" | "2" | "4") if n_used <= 8 => {
3577                let rpw: usize = m.parse().unwrap();
3578                let f = self.func(match rpw { 1 => "moe_down8_fma_dev_q8_w8r1",
3579                                              2 => "moe_down8_fma_dev_q8_w8r2",
3580                                              _ => "moe_down8_fma_dev_q8_w8r4" });
3581                (f, LaunchConfig { grid_dim: (out_f.div_ceil(rpw) as u32, 1, 1),
3582                                   block_dim: (32, n_used as u32, 1), shared_mem_bytes: 0 })
3583            }
3584            "h2" if in_f == 512 => (self.func("moe_down8_fma_dev_q8_h2"),
3585                LaunchConfig { grid_dim: (out_f.div_ceil(2) as u32, 1, 1),
3586                               block_dim: (32, 1, 1), shared_mem_bytes: 0 }),
3587            // "" = AUTO gemma shape (in_f==704): w8r2 measured +1 tok/s vs base (sweep
3588            // 1/2/4 -> 133.6/134.2/133.6, 2026-07-10); slot-ordered chain preserved.
3589            "" if in_f == 704 && n_used <= 8 =>
3590                (self.func("moe_down8_fma_dev_q8_w8r2"),
3591                 LaunchConfig { grid_dim: (out_f.div_ceil(2) as u32, 1, 1),
3592                                block_dim: (32, n_used as u32, 1), shared_mem_bytes: 0 }),
3593            // "" = AUTO: the measured winner for the 35B expert shape (arc 2026-07-05, +3.8%);
3594            // any shape the h2 kernels can't take (nsb!=16 / n_used>8) falls to base via `_`.
3595            // _v twins (down8 lane 2026-07-08): wide-load IQ4_XS dot, bit-identical outputs.
3596            "w8h2v" | "" if in_f == 512 && n_used <= 8 =>
3597                (self.func("moe_down8_fma_dev_q8_w8h2v"),
3598                 LaunchConfig { grid_dim: (out_f.div_ceil(2) as u32, 1, 1),
3599                                block_dim: (32, n_used as u32, 1), shared_mem_bytes: 0 }),
3600            "w8h2r2v" if in_f == 512 && n_used <= 8 =>
3601                (self.func("moe_down8_fma_dev_q8_w8h2r2v"),
3602                 LaunchConfig { grid_dim: (out_f.div_ceil(4) as u32, 1, 1),
3603                                block_dim: (32, n_used as u32, 1), shared_mem_bytes: 0 }),
3604            "w8h2r2" if in_f == 512 && n_used <= 8 =>
3605                (self.func("moe_down8_fma_dev_q8_w8h2r2"),
3606                 LaunchConfig { grid_dim: (out_f.div_ceil(4) as u32, 1, 1),
3607                                block_dim: (32, n_used as u32, 1), shared_mem_bytes: 0 }),
3608            "w8h2" if in_f == 512 && n_used <= 8 =>
3609                (self.func("moe_down8_fma_dev_q8_w8h2"),
3610                 LaunchConfig { grid_dim: (out_f.div_ceil(2) as u32, 1, 1),
3611                                block_dim: (32, n_used as u32, 1), shared_mem_bytes: 0 }),
3612            _ => (self.func("moe_down8_fma_dev_q8"),
3613                  LaunchConfig { grid_dim: (out_f as u32, 1, 1),
3614                                 block_dim: (32, 1, 1), shared_mem_bytes: 0 }),
3615        };
3616        let __s_b = self.gpu.stream();
3617        let mut b = __s_b.launch_builder(&f);
3618        b.arg(table).arg(sel).arg(w).arg(aq2).arg(ad2).arg(dst)
3619         .arg(&inf).arg(&outf).arg(&nu).arg(&ne).arg(&qt).arg(&rbi);
3620        unsafe { b.launch(cfg)?; }
3621        Ok(())
3622    }
3623
3624    /// SMALL-M VERIFY rows twin (MEMRA_SPEC_M2, lane/spec-m2): ONE launch covers all `t` tokens
3625    /// of the spec verify's MoE dev gate/up (grid.z = token) — the _v geometry per token, with
3626    /// tok-offset sel/aq/ad/act pointers matching the serial loop's slices. BIT-IDENTICAL per
3627    /// token (see the kernel header). aq/ad are the BATCHED z-quantize ([t, in_f] rows —
3628    /// quantize_q8_1's per-32-block program is row-independent, so batched rows == the serial
3629    /// loop's per-token quantize_q8_1_view bytes). Returns act [t, n_used, n_ff].
3630    #[allow(clippy::too_many_arguments)]
3631    pub fn moe_gate_up_silu8_dev_q8_rows(&self, table: &CudaSlice<u64>, sel: &CudaSlice<i32>,
3632                                         aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, t: usize,
3633                                         in_f: usize, n_ff: usize, n_used: usize, n_expert: usize,
3634                                         qt_g: i32, qt_u: i32, rb_g: usize, rb_u: usize,
3635                                         macros: &CudaSlice<f32>)
3636                                         -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3637        let f = self.func("moe_gate_up_silu8_dev_q8_v_rows");
3638        let mut act = self.alloc_uninit::<f32>(t * n_used * n_ff)?;
3639        let cfg = LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, t as u32),
3640                                 block_dim: (32, 1, 1), shared_mem_bytes: 0 };
3641        let (inf, nff, ne, nu, rbg, rbu) = (in_f as i32, n_ff as i32, n_expert as i32,
3642                                            n_used as i32, rb_g as i64, rb_u as i64);
3643        let __s_b = self.gpu.stream();
3644        let mut b = __s_b.launch_builder(&f);
3645        b.arg(table).arg(sel).arg(aq).arg(ad).arg(&mut act)
3646         .arg(&inf).arg(&nff).arg(&ne).arg(&qt_g).arg(&qt_u).arg(&rbg).arg(&rbu).arg(&nu).arg(macros);
3647        unsafe { b.launch(cfg)?; }
3648        Ok(act)
3649    }
3650
3651    /// SMALL-M VERIFY rows twin of the down proj: w8h2v geometry per token on a grid.z token
3652    /// axis. Caller gates the w8h2v shape contract (in_f == 512, n_used <= 8) — same gate as
3653    /// the AUTO dispatch in `moe_down8_fma_dev_q8`. aq2/ad2 = batched act quantize
3654    /// ([t*n_used, in_f] rows). dst rows are FULLY overwritten per token.
3655    #[allow(clippy::too_many_arguments)]
3656    pub fn moe_down8_fma_dev_q8_rows(&self, table: &CudaSlice<u64>, sel: &CudaSlice<i32>,
3657                                     w: &CudaSlice<f32>, aq2: &CudaSlice<i8>, ad2: &CudaSlice<f32>,
3658                                     dst: &mut CudaSlice<f32>, t: usize,
3659                                     in_f: usize, out_f: usize, n_used: usize, n_expert: usize,
3660                                     qt: i32, rb: usize)
3661                                     -> Result<(), Box<dyn std::error::Error>> {
3662        assert!(in_f == 512 && n_used <= 8, "down rows twin is w8h2v shape-gated");
3663        let f = self.func("moe_down8_fma_dev_q8_w8h2v_rows");
3664        let cfg = LaunchConfig { grid_dim: (out_f.div_ceil(2) as u32, 1, t as u32),
3665                                 block_dim: (32, n_used as u32, 1), shared_mem_bytes: 0 };
3666        let (inf, outf, nu, ne, rbi) = (in_f as i32, out_f as i32, n_used as i32,
3667                                        n_expert as i32, rb as i64);
3668        let __s_b = self.gpu.stream();
3669        let mut b = __s_b.launch_builder(&f);
3670        b.arg(table).arg(sel).arg(w).arg(aq2).arg(ad2).arg(dst)
3671         .arg(&inf).arg(&outf).arg(&nu).arg(&ne).arg(&qt).arg(&rbi);
3672        unsafe { b.launch(cfg)?; }
3673        Ok(())
3674    }
3675
3676    /// CSR gate/up v3 (owner-scan dedup, no build kernel): qtypes {IQ4_XS, IQ3_S} (caller
3677    /// gates), grid.y = pair index; the first pair of each expert serves all its pairs.
3678    /// Bit-identical to moe_gate_up_silu8_dev_q8_v_rows (explicit-intrinsic accumulate).
3679    #[allow(clippy::too_many_arguments)]
3680    pub fn moe_gate_up_silu8_dev_q8_csr(&self, table: &CudaSlice<u64>, sel: &CudaSlice<i32>,
3681                                        aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
3682                                        n_pairs: usize, in_f: usize, n_ff: usize, n_used: usize,
3683                                        n_expert: usize, qt_g: i32, qt_u: i32, rb_g: usize, rb_u: usize)
3684                                        -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3685        let f = self.func("moe_gate_up_silu8_dev_q8_csr_iq4");
3686        let mut act = self.alloc_uninit::<f32>(n_pairs * n_ff)?;
3687        let cfg = LaunchConfig { grid_dim: (n_ff as u32, n_pairs as u32, 1),
3688                                 block_dim: (32, 1, 1), shared_mem_bytes: 0 };
3689        let (inf, nff, ne, nu, npi, rbg, rbu) = (in_f as i32, n_ff as i32, n_expert as i32,
3690                                                 n_used as i32, n_pairs as i32, rb_g as i64, rb_u as i64);
3691        let __s_b = self.gpu.stream();
3692        let mut b = __s_b.launch_builder(&f);
3693        b.arg(table).arg(sel).arg(aq).arg(ad).arg(&mut act)
3694         .arg(&inf).arg(&nff).arg(&ne).arg(&qt_g).arg(&qt_u).arg(&rbg).arg(&rbu).arg(&nu).arg(&npi);
3695        unsafe { b.launch(cfg)?; }
3696        Ok(act)
3697    }
3698
3699
3700    /// TEST SEAM (down8 lane 2026-07-08): launch a down dev_q8 variant BY NAME with its
3701    /// canonical geometry, bypassing the env-cached dispatch so moe-devq8-check can byte-
3702    /// compare variants in one process. Variants: "base", "w8h2", "w8h2r2", "w8h2v", "w8h2r2v".
3703    #[allow(clippy::too_many_arguments)]
3704    pub fn moe_down8_fma_dev_q8_variant(&self, variant: &str, table: &CudaSlice<u64>,
3705                                        sel: &cudarc::driver::CudaView<i32>,
3706                                        w: &cudarc::driver::CudaView<f32>,
3707                                        aq2: &CudaSlice<i8>, ad2: &CudaSlice<f32>,
3708                                        dst: &mut cudarc::driver::CudaViewMut<f32>,
3709                                        in_f: usize, out_f: usize, n_used: usize, n_expert: usize,
3710                                        qt: i32, rb: usize)
3711                                        -> Result<(), Box<dyn std::error::Error>> {
3712        let (inf, outf, nu, ne, rbi) = (in_f as i32, out_f as i32, n_used as i32,
3713                                        n_expert as i32, rb as i64);
3714        let (f, cfg) = match variant {
3715            "w8h2" | "w8h2v" => {
3716                (self.func(if variant == "w8h2" { "moe_down8_fma_dev_q8_w8h2" }
3717                           else { "moe_down8_fma_dev_q8_w8h2v" }),
3718                 LaunchConfig { grid_dim: (out_f.div_ceil(2) as u32, 1, 1),
3719                                block_dim: (32, n_used as u32, 1), shared_mem_bytes: 0 })
3720            }
3721            "w8h2r2" | "w8h2r2v" => {
3722                (self.func(if variant == "w8h2r2" { "moe_down8_fma_dev_q8_w8h2r2" }
3723                           else { "moe_down8_fma_dev_q8_w8h2r2v" }),
3724                 LaunchConfig { grid_dim: (out_f.div_ceil(4) as u32, 1, 1),
3725                                block_dim: (32, n_used as u32, 1), shared_mem_bytes: 0 })
3726            }
3727            _ => (self.func("moe_down8_fma_dev_q8"),
3728                  LaunchConfig { grid_dim: (out_f as u32, 1, 1),
3729                                 block_dim: (32, 1, 1), shared_mem_bytes: 0 }),
3730        };
3731        let __s_b = self.gpu.stream();
3732        let mut b = __s_b.launch_builder(&f);
3733        b.arg(table).arg(sel).arg(w).arg(aq2).arg(ad2).arg(dst)
3734         .arg(&inf).arg(&outf).arg(&nu).arg(&ne).arg(&qt).arg(&rbi);
3735        unsafe { b.launch(cfg)?; }
3736        Ok(())
3737    }
3738
3739    /// TEST SEAM (down8 lane): gate_up twin of the above. Variants: "base", "v".
3740    #[allow(clippy::too_many_arguments)]
3741    pub fn moe_gate_up_silu8_dev_q8_variant(&self, variant: &str, table: &CudaSlice<u64>,
3742                                            sel: &cudarc::driver::CudaView<i32>,
3743                                            aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
3744                                            in_f: usize, n_ff: usize, n_used: usize,
3745                                            n_expert: usize, qt_g: i32, qt_u: i32,
3746                                            rb_g: usize, rb_u: usize)
3747                                            -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3748        let mut act = self.alloc_uninit::<f32>(n_used * n_ff)?;
3749        let (inf, nff, ne, rbg, rbu) = (in_f as i32, n_ff as i32, n_expert as i32,
3750                                        rb_g as i64, rb_u as i64);
3751        let f = self.func(if variant == "v" { "moe_gate_up_silu8_dev_q8_v" }
3752                          else { "moe_gate_up_silu8_dev_q8" });
3753        let cfg = LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
3754                                 block_dim: (32, 1, 1), shared_mem_bytes: 0 };
3755        let __s_b = self.gpu.stream();
3756        let mut b = __s_b.launch_builder(&f);
3757        b.arg(table).arg(sel).arg(aq).arg(ad).arg(&mut act)
3758         .arg(&inf).arg(&nff).arg(&ne).arg(&qt_g).arg(&qt_u).arg(&rbg).arg(&rbu);
3759        unsafe { b.launch(cfg)?; }
3760        Ok(act)
3761    }
3762
3763    pub fn moe_gate_up_silu8_dev(&self, table: &CudaSlice<u64>, sel: &cudarc::driver::CudaView<i32>,
3764                                 x: &cudarc::driver::CudaView<f32>,
3765                                 in_f: usize, n_ff: usize, n_used: usize, n_expert: usize,
3766                                 qt_g: i32, qt_u: i32, rb_g: usize, rb_u: usize,
3767                                 macros: &CudaSlice<f32>)
3768                                 -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3769        let f = self.func("moe_gate_up_silu8_dev");
3770        let mut act = self.alloc_uninit::<f32>(n_used * n_ff)?;  // fully overwritten
3771        let cfg = LaunchConfig { grid_dim: (n_ff as u32, n_used as u32, 1),
3772                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
3773        let (inf, nff, ne, rbg, rbu) = (in_f as i32, n_ff as i32, n_expert as i32,
3774                                        rb_g as i64, rb_u as i64);
3775        let __s_b = self.gpu.stream();
3776        let mut b = __s_b.launch_builder(&f);
3777        b.arg(table).arg(sel).arg(x).arg(&mut act)
3778         .arg(&inf).arg(&nff).arg(&ne).arg(&qt_g).arg(&qt_u).arg(&rbg).arg(&rbu).arg(macros);
3779        unsafe { b.launch(cfg)?; }
3780        Ok(act)
3781    }
3782
3783    /// LAUNCH-STRUCTURE STAGE 3: device-dispatch twin of `moe_down8_fma_into` — expert ids AND
3784    /// renormalized weights read from the router kernel's device output. BIT-IDENTICAL chain.
3785    #[allow(clippy::too_many_arguments)]
3786    pub fn moe_down8_fma_dev(&self, table: &CudaSlice<u64>, sel: &cudarc::driver::CudaView<i32>,
3787                             w: &cudarc::driver::CudaView<f32>, act: &CudaSlice<f32>,
3788                             dst: &mut cudarc::driver::CudaViewMut<f32>,
3789                             in_f: usize, out_f: usize, n_used: usize, n_expert: usize,
3790                             qt: i32, rb: usize)
3791                             -> Result<(), Box<dyn std::error::Error>> {
3792        let f = self.func("moe_down8_fma_dev");
3793        let cfg = LaunchConfig { grid_dim: (out_f as u32, 1, 1),
3794                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
3795        let (inf, outf, nu, ne, rbv) = (in_f as i32, out_f as i32, n_used as i32,
3796                                        n_expert as i32, rb as i64);
3797        let __s_b = self.gpu.stream();
3798        let mut b = __s_b.launch_builder(&f);
3799        b.arg(table).arg(sel).arg(w).arg(act).arg(dst)
3800         .arg(&inf).arg(&outf).arg(&nu).arg(&ne).arg(&qt).arg(&rbv);
3801        unsafe { b.launch(cfg)?; }
3802        Ok(())
3803    }
3804
3805    /// dst[i] += alpha * src[i], i in 0..n. dst is a CudaViewMut (a row of moe_out).
3806    pub fn axpy_into(&self, src: &CudaSlice<f32>, alpha: f32,
3807                     dst: &mut cudarc::driver::CudaViewMut<f32>, n: usize)
3808                     -> Result<(), Box<dyn std::error::Error>> {
3809        let f = self.func("axpy_f32");
3810        let cfg = LaunchConfig::for_num_elems(n as u32);
3811        let (a, ni) = (alpha, n as i32);
3812        let __s_b = self.gpu.stream();
3813        let mut b = __s_b.launch_builder(&f);
3814        b.arg(src).arg(dst).arg(&a).arg(&ni);
3815        unsafe { b.launch(cfg)?; }
3816        Ok(())
3817    }
3818
3819    /// dst[r*ncols + c] += src[r*ncols + c] * scale[r]. Per-row scalar accumulate (shared expert).
3820    pub fn add_scaled_rows(&self, src: &CudaSlice<f32>, scale: &CudaSlice<f32>,
3821                           dst: &mut CudaSlice<f32>, ncols: usize, nrows: usize)
3822                           -> Result<(), Box<dyn std::error::Error>> {
3823        let f = self.func("add_scaled_rows_f32");
3824        let cfg = LaunchConfig::for_num_elems((ncols * nrows) as u32);
3825        let (nc, nr) = (ncols as i32, nrows as i32);
3826        let __s_b = self.gpu.stream();
3827        let mut b = __s_b.launch_builder(&f);
3828        b.arg(src).arg(scale).arg(dst).arg(&nc).arg(&nr);
3829        unsafe { b.launch(cfg)?; }
3830        Ok(())
3831    }
3832
3833    // ======== A2 GROUPED MoE PREFILL KERNELS ========
3834
3835    /// Gather m_e rows from src[T, ncols] into dst[m_e, ncols] using index array idx[m_e].
3836    pub fn gather_rows(&self, src: &CudaSlice<f32>, idx: &CudaSlice<i32>,
3837                       dst: &mut CudaSlice<f32>, ncols: usize, m_e: usize)
3838                       -> Result<(), Box<dyn std::error::Error>> {
3839        let f = self.func("gather_rows_f32");
3840        let cfg = LaunchConfig::for_num_elems((m_e * ncols) as u32);
3841        let (nc, me) = (ncols as i32, m_e as i32);
3842        let __s_b = self.gpu.stream();
3843        let mut b = __s_b.launch_builder(&f);
3844        b.arg(src).arg(idx).arg(dst).arg(&nc).arg(&me);
3845        unsafe { b.launch(cfg)?; }
3846        Ok(())
3847    }
3848
3849    /// Scatter expert outputs into per-token slots: dst[tok_idx[r], slot_idx[r], :] = src[r, :] * weight[r].
3850    /// dst is [T, n_used, ncols], zero-initialized. Each (expert, token) pair maps to a unique slot.
3851    /// Scatter expert outputs into per-token slots (raw copy, no weight multiply).
3852    /// Weight stored into wbuf[tok*n_used + slot] for FMA in reduce step.
3853    pub fn scatter_slot(&self, src: &CudaSlice<f32>, tok_idx: &CudaSlice<i32>,
3854                        slot_idx: &CudaSlice<i32>, weight: &CudaSlice<f32>,
3855                        dst: &mut CudaSlice<f32>, wbuf: &mut CudaSlice<f32>,
3856                        ncols: usize, n_used: usize, m_e: usize)
3857                        -> Result<(), Box<dyn std::error::Error>> {
3858        let f = self.func("scatter_add_slot_f32");
3859        let cfg = LaunchConfig::for_num_elems((m_e * ncols) as u32);
3860        let (nc, nu, me) = (ncols as i32, n_used as i32, m_e as i32);
3861        let __s_b = self.gpu.stream();
3862        let mut b = __s_b.launch_builder(&f);
3863        b.arg(src).arg(tok_idx).arg(slot_idx).arg(weight).arg(dst).arg(wbuf).arg(&nc).arg(&nu).arg(&me);
3864        unsafe { b.launch(cfg)?; }
3865        Ok(())
3866    }
3867
3868    /// Reduce n_used slots per token: dst[t, col] = sum_s slots[t, s, col].
3869    /// Reduce n_used slots per token: dst[t, col] = sum_s FMA(wbuf[t,s], slots[t,s,col], acc).
3870    /// Uses FMA for bit-identity with the sequential axpy path.
3871    pub fn reduce_slots(&self, slots: &CudaSlice<f32>, wbuf: &CudaSlice<f32>,
3872                        dst: &mut CudaSlice<f32>, ncols: usize, n_used: usize, t: usize)
3873                        -> Result<(), Box<dyn std::error::Error>> {
3874        let f = self.func("reduce_slots_f32");
3875        let cfg = LaunchConfig::for_num_elems((t * ncols) as u32);
3876        let (nc, nu, ti) = (ncols as i32, n_used as i32, t as i32);
3877        let __s_b = self.gpu.stream();
3878        let mut b = __s_b.launch_builder(&f);
3879        b.arg(slots).arg(wbuf).arg(dst).arg(&nc).arg(&nu).arg(&ti);
3880        unsafe { b.launch(cfg)?; }
3881        Ok(())
3882    }
3883
3884    /// Stage-B: quantize activation [m,in] f32 -> q8_1 (int8 qs + per-block f32 scale).
3885    /// Quantize an activation [m, in_f] to q8_1 (int8 qs + per-32 f32 scale). Public so the
3886    /// forward can quantize a SHARED activation ONCE and feed it to several matmuls (gate+up
3887    /// share `z`; q/k/v and wqkv/gate/beta/alpha share `h`) — quantize_q8_1 was 13.5% of decode
3888    /// GPU time, ~half of it redundant re-quantization of the same row.
3889    /// quantize_q8_1 over a CudaView (a sliced z-row) — same kernel, offset-honoring arg.
3890    pub fn quantize_q8_1_view(&self, x: &cudarc::driver::CudaView<f32>, m: usize, in_f: usize)
3891                     -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
3892        let f = self.func("quantize_q8_1");
3893        let nblk = in_f / 32;
3894        let mut q = self.alloc_uninit::<i8>(m * in_f)?;
3895        let mut d = self.alloc_uninit::<f32>(m * nblk)?;
3896        let cfg = LaunchConfig::for_num_elems((m * in_f) as u32);
3897        let (inf, mi) = (in_f as i32, m as i32);
3898        let __s_b = self.gpu.stream();
3899        let mut b = __s_b.launch_builder(&f);
3900        b.arg(x).arg(&mut q).arg(&mut d).arg(&inf).arg(&mi);
3901        unsafe { b.launch(cfg)?; }
3902        Ok((q, d))
3903    }
3904
3905    pub fn quantize_q8_1(&self, x: &CudaSlice<f32>, m: usize, in_f: usize)
3906                     -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
3907        let nblk = in_f / 32;
3908        let mut q = self.alloc_uninit::<i8>(m * in_f)?;  // full-overwrite output: skip memset
3909        let mut d = self.alloc_uninit::<f32>(m * nblk)?;  // full-overwrite output: skip memset
3910        // WARP-PER-BLOCK kernel: one warp per 32-block -> m*in_f threads total.
3911        let cfg = LaunchConfig::for_num_elems((m * in_f) as u32);
3912        let (inf, mi) = (in_f as i32, m as i32);
3913        if Self::pdl_on() && Self::pdl_wb_on() {
3914            {
3915            use cudarc::driver::{DevicePtr, DevicePtrMut};
3916            let s = &self.gpu.stream();
3917            let (px, _g0) = x.device_ptr(s);
3918            let (pq, _g1) = q.device_ptr_mut(s); let (pd, _g2) = d.device_ptr_mut(s);
3919            let mut ps = [
3920                &px as *const _ as *mut std::ffi::c_void, &pq as *const _ as *mut _,
3921                &pd as *const _ as *mut _, &inf as *const _ as *mut _,
3922                &mi as *const _ as *mut _,
3923            ];
3924            unsafe { self.launch_pdl("quantize_q8_1", cfg.grid_dim, cfg.block_dim, &mut ps)?; }
3925            }
3926            return Ok((q, d));
3927        }
3928        let f = self.func("quantize_q8_1");
3929        let __s_b = self.gpu.stream();
3930        let mut b = __s_b.launch_builder(&f);
3931        b.arg(x).arg(&mut q).arg(&mut d).arg(&inf).arg(&mi);
3932        unsafe { b.launch(cfg)?; }
3933        Ok((q, d))
3934    }
3935
3936    /// Stage-C FP4: quantize activation [m,in] f32 -> e2m1 nibbles (aq4: u32 [m, in/8]) + per-16
3937    /// UE4M3 scale (ad4: u8 [m, in/16]), the layout the mxf4nvf4 block-scale GEMM B-operand wants.
3938    /// in_f must be a multiple of 64 (one NVFP4 K-block). One thread per (token, 16-block).
3939    pub fn quantize_fp4_act(&self, x: &CudaSlice<f32>, m: usize, in_f: usize)
3940                     -> Result<(CudaSlice<u32>, CudaSlice<u8>), Box<dyn std::error::Error>> {
3941        let f = self.func("quantize_fp4_act");
3942        let nb16 = in_f / 16;
3943        let mut aq4 = self.alloc_uninit::<u32>(m * (in_f / 8))?;  // full-overwrite output: skip memset
3944        let mut ad4 = self.alloc_uninit::<u8>(m * nb16)?;  // full-overwrite output: skip memset
3945        let cfg = LaunchConfig::for_num_elems((m * nb16) as u32);
3946        let (inf, mi) = (in_f as i32, m as i32);
3947        let __s_b = self.gpu.stream();
3948        let mut b = __s_b.launch_builder(&f);
3949        b.arg(x).arg(&mut aq4).arg(&mut ad4).arg(&inf).arg(&mi);
3950        unsafe { b.launch(cfg)?; }
3951        Ok((aq4, ad4))
3952    }
3953
3954    /// Stage-C FP4 GEMM (NVFP4 weights): native mxf4nvf4 block-scale tensor-core matmul. Feeds raw
3955    /// e2m1 weight nibbles + raw UE4M3 micro-scales directly to mma.sync.m16n8k64 (762 TFLOP/s peak,
3956    /// 3.5x int8). Activation `x` is quantized to FP4 e2m1 here. NVFP4 per-tensor macro-scale applied
3957    /// post (scale==1.0 -> no-op). `bytes` = raw NVFP4 weight rows. Used by the MEMRA_FP4 prefill path.
3958    pub fn qmatvec_gemm_nvfp4_fp4(&self, bytes: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize,
3959                                  in_f: usize, out_f: usize, row_bytes: usize, scale: f32)
3960                                  -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3961        assert!(in_f % 64 == 0, "FP4 GEMM requires in_f % 64 == 0, got {in_f}");
3962        let (aq4, ad4) = self.quantize_fp4_act(x, m, in_f)?;
3963        let mut y = self.fp4_gemm_launch(bytes, &aq4, &ad4, m, in_f, out_f, row_bytes)?;
3964        if scale != 1.0 { self.scale_inplace(&mut y, scale, m * out_f)?; }
3965        Ok(y)
3966    }
3967
3968    /// Shared mxf4 GEMM launch (pre-quantized FP4 activation aq4/ad4). Same CTA tile as the int8 GEMM
3969    /// (BM=64 rows x BN=128 tokens, 4 warps). No macro-scale applied here.
3970    fn fp4_gemm_launch(&self, bytes: &CudaSlice<u8>, aq4: &CudaSlice<u32>, ad4: &CudaSlice<u8>,
3971                       m: usize, in_f: usize, out_f: usize, row_bytes: usize)
3972                       -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3973        let f = self.func("qmatvec_gemm_nvfp4_fp4");
3974        let mut y = self.alloc_uninit::<f32>(m * out_f)?;  // full-overwrite output: skip memset
3975        const BM: u32 = 64; const BN: u32 = 256;
3976        let cfg = LaunchConfig {
3977            grid_dim: ((out_f as u32 + BM - 1) / BM, (m as u32 + BN - 1) / BN, 1),
3978            block_dim: (32, 4, 1), shared_mem_bytes: 0,
3979        };
3980        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
3981        let __s_b = self.gpu.stream();
3982        let mut b = __s_b.launch_builder(&f);
3983        b.arg(bytes).arg(aq4).arg(ad4).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
3984        unsafe { b.launch(cfg)?; }
3985        Ok(y)
3986    }
3987
3988    /// Test entry (kernel_check): run the FP4 GEMM from raw bytes; NO macro-scale (caller compares bare).
3989    pub fn qmatvec_gemm_nvfp4_fp4_raw(&self, bytes: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize,
3990                                      in_f: usize, out_f: usize, row_bytes: usize)
3991                                      -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
3992        assert!(in_f % 64 == 0, "FP4 GEMM requires in_f % 64 == 0, got {in_f}");
3993        let (aq4, ad4) = self.quantize_fp4_act(x, m, in_f)?;
3994        self.fp4_gemm_launch(bytes, &aq4, &ad4, m, in_f, out_f, row_bytes)
3995    }
3996
3997    /// Stage-B: Q8_0 weight x q8_1 activation int8 dp4a matmul. y[m,out]=x@W^T.
3998    pub fn qmatvec_q8_0_fast(&self, w: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize, in_f: usize,
3999                             out_f: usize, row_bytes: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4000        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
4001        let f = self.func("qmatvec_q8_0_dp4a");
4002        let mut y = self.alloc_uninit::<f32>(m * out_f)?;  // full-overwrite output: skip memset
4003        let cfg = LaunchConfig { grid_dim: (out_f as u32, m as u32, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
4004        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
4005        let __s_b = self.gpu.stream();
4006        let mut b = __s_b.launch_builder(&f);
4007        b.arg(w).arg(&aq).arg(&ad).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
4008        unsafe { b.launch(cfg)?; }
4009        Ok(y)
4010    }
4011
4012    /// Stage-B: Q4_K weight x q8_1 activation int8 dp4a (decode). Min-offset via q8_1 sum term.
4013    #[allow(non_snake_case)] // Keep GGUF qtype spelling visible at the public kernel boundary.
4014    pub fn qmatvec_q4_K_fast(&self, w: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize, in_f: usize,
4015                             out_f: usize, row_bytes: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4016        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
4017        let f = self.func("qmatvec_q4_K_dp4a");
4018        let mut y = self.alloc_uninit::<f32>(m * out_f)?;  // full-overwrite output: skip memset
4019        let cfg = LaunchConfig { grid_dim: (out_f as u32, m as u32, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
4020        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
4021        let __s_b = self.gpu.stream();
4022        let mut b = __s_b.launch_builder(&f);
4023        b.arg(w).arg(&aq).arg(&ad).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
4024        unsafe { b.launch(cfg)?; }
4025        Ok(y)
4026    }
4027
4028    /// Stage-B: Q6_K weight x q8_1 activation int8 dp4a (decode, symmetric).
4029    #[allow(non_snake_case)] // Keep GGUF qtype spelling visible at the public kernel boundary.
4030    pub fn qmatvec_q6_K_fast(&self, w: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize, in_f: usize,
4031                             out_f: usize, row_bytes: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4032        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
4033        let f = self.func("qmatvec_q6_K_dp4a");
4034        let mut y = self.alloc_uninit::<f32>(m * out_f)?;  // full-overwrite output: skip memset
4035        let cfg = LaunchConfig { grid_dim: (out_f as u32, m as u32, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
4036        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
4037        let __s_b = self.gpu.stream();
4038        let mut b = __s_b.launch_builder(&f);
4039        b.arg(w).arg(&aq).arg(&ad).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
4040        unsafe { b.launch(cfg)?; }
4041        Ok(y)
4042    }
4043
4044    /// Stage-B: Q5_K weight x q8_1 activation int8 dp4a (decode). Min-offset via q8_1 sum term.
4045    #[allow(non_snake_case)] // Keep GGUF qtype spelling visible at the public kernel boundary.
4046    pub fn qmatvec_q5_K_fast(&self, w: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize, in_f: usize,
4047                             out_f: usize, row_bytes: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4048        self.qmatvec_dp4a_named("qmatvec_q5_K_dp4a", w, x, m, in_f, out_f, row_bytes)
4049    }
4050    /// Stage-B: Q3_K weight x q8_1 activation int8 dp4a (decode, symmetric).
4051    #[allow(non_snake_case)] // Keep GGUF qtype spelling visible at the public kernel boundary.
4052    pub fn qmatvec_q3_K_fast(&self, w: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize, in_f: usize,
4053                             out_f: usize, row_bytes: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4054        self.qmatvec_dp4a_named("qmatvec_q3_K_dp4a", w, x, m, in_f, out_f, row_bytes)
4055    }
4056    /// A6 split-plane twin of `qmatvec_nvfp4_fast` (weights repacked; used by the rp gates).
4057    pub fn qmatvec_nvfp4_fast_rp(&self, w: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize, in_f: usize,
4058                                 out_f: usize, row_bytes: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4059        assert!(in_f % 64 == 0, "NVFP4 dp4a requires in_f % 64 == 0, got {in_f}");
4060        self.qmatvec_dp4a_named("qmatvec_nvfp4_dp4a_rp", w, x, m, in_f, out_f, row_bytes)
4061    }
4062    /// Stage-B: NVFP4 weight x q8_1 activation int8 dp4a (decode, symmetric, codebook lookup).
4063    pub fn qmatvec_nvfp4_fast(&self, w: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize, in_f: usize,
4064                              out_f: usize, row_bytes: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4065        // B1: the NVFP4 dp4a kernel maps two 32-elem q8_1 blocks onto one 64-elem block_nvfp4
4066        // (sblk = g >> 1). in_f must be a multiple of 64 or the last block reads a partial superblock.
4067        assert!(in_f % 64 == 0, "NVFP4 dp4a requires in_f % 64 == 0, got {in_f}");
4068        self.qmatvec_dp4a_named("qmatvec_nvfp4_dp4a", w, x, m, in_f, out_f, row_bytes)
4069    }
4070    /// Stage-B (optional perf): IQ4_XS codebook int8 dp4a.
4071    #[allow(non_snake_case)] // Keep GGUF qtype spelling visible at the public kernel boundary.
4072    pub fn qmatvec_iq4_XS_fast(&self, w: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize, in_f: usize,
4073                               out_f: usize, row_bytes: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4074        self.qmatvec_dp4a_named("qmatvec_iq4_XS_dp4a", w, x, m, in_f, out_f, row_bytes)
4075    }
4076
4077    /// Shared dp4a launcher: quantize_q8_1 then call the named kernel (grid (out,m), block 64).
4078    fn qmatvec_dp4a_named(&self, name: &str, w: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize,
4079                          in_f: usize, out_f: usize, row_bytes: usize)
4080                          -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4081        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
4082        let f = self.func(name);
4083        let mut y = self.alloc_uninit::<f32>(m * out_f)?;  // full-overwrite output: skip memset
4084        let cfg = LaunchConfig { grid_dim: (out_f as u32, m as u32, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
4085        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
4086        let __s_b = self.gpu.stream();
4087        let mut b = __s_b.launch_builder(&f);
4088        b.arg(w).arg(&aq).arg(&ad).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
4089        unsafe { b.launch(cfg)?; }
4090        Ok(y)
4091    }
4092
4093    pub fn htod(&self, v: &[f32]) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4094        Ok(self.gpu.stream().clone_htod(v)?)
4095    }
4096    pub fn htod_i32(&self, v: &[i32]) -> Result<CudaSlice<i32>, Box<dyn std::error::Error>> {
4097        Ok(self.gpu.stream().clone_htod(v)?)
4098    }
4099    /// i8 upload (moe-devq8-check: synthetic q8_1 activation bytes).
4100    pub fn htod_i8(&self, v: &[i8]) -> Result<CudaSlice<i8>, Box<dyn std::error::Error>> {
4101        Ok(self.gpu.stream().clone_htod(v)?)
4102    }
4103    pub fn htod_u64(&self, v: &[u64]) -> Result<CudaSlice<u64>, Box<dyn std::error::Error>> {
4104        Ok(self.gpu.stream().clone_htod(v)?)
4105    }
4106    /// View twin of `dtoh` (lean-logits component 3: D2H one row of a [B, n_vocab] stack).
4107    pub fn dtoh_view(&self, d: &cudarc::driver::CudaView<f32>)
4108                     -> Result<Vec<f32>, Box<dyn std::error::Error>> {
4109        let v = self.gpu.stream().clone_dtoh(d)?;
4110        self.gpu.stream().synchronize()?;
4111        Ok(v)
4112    }
4113    pub fn dtoh(&self, d: &CudaSlice<f32>) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
4114        let v = self.gpu.stream().clone_dtoh(d)?;
4115                self.gpu.stream().synchronize()?;
4116        Ok(v)
4117    }
4118    /// Queue two f32 device-to-host copies on the compute stream, then establish one host
4119    /// boundary for both. Hy3's CPU/GPU expert split needs the router logits and the MoE input;
4120    /// issuing them together avoids a second stream synchronization in every trunk layer.
4121    pub fn dtoh_pair(
4122        &self,
4123        a: &CudaSlice<f32>,
4124        b: &CudaSlice<f32>,
4125    ) -> Result<(Vec<f32>, Vec<f32>), Box<dyn std::error::Error>> {
4126        let av = self.gpu.stream().clone_dtoh(a)?;
4127        let bv = self.gpu.stream().clone_dtoh(b)?;
4128        self.gpu.stream().synchronize()?;
4129        Ok((av, bv))
4130    }
4131    /// Device-to-host copy of an i32 buffer (fused-router sel_idx readback).
4132    pub fn dtoh_i32(&self, d: &CudaSlice<i32>) -> Result<Vec<i32>, Box<dyn std::error::Error>> {
4133        let v = self.gpu.stream().clone_dtoh(d)?;
4134        self.gpu.stream().synchronize()?;
4135        Ok(v)
4136    }
4137    /// Device-to-host copy of a u8 buffer (used to read back the quantized KV cache for validation).
4138    pub fn dtoh_u8(&self, d: &CudaSlice<u8>) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
4139        let v = self.gpu.stream().clone_dtoh(d)?;
4140        self.gpu.stream().synchronize()?;
4141        Ok(v)
4142    }
4143    pub fn dtoh_u8_view(&self, d: &cudarc::driver::CudaView<u8>)
4144                        -> Result<Vec<u8>, Box<dyn std::error::Error>> {
4145        let v = self.gpu.stream().clone_dtoh(d)?;
4146        self.gpu.stream().synchronize()?;
4147        Ok(v)
4148    }
4149    pub fn zeros(&self, n: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4150        let s = self.gpu.stream().alloc_zeros::<f32>(n)?;
4151        self.keep_if_capturing(&s);
4152        Ok(s)
4153    }
4154
4155    /// GPU-resident greedy argmax (CUDA-GRAPH-PLAN Phase 1): logits[n_vocab] -> token id in a
4156    /// resident device u32 [1]. PARALLEL 2-pass (RANK1 LEVER): the old single-CTA scan (one 256-thread
4157    /// block on one SM over 248K logits) was memory-starved at ~426us/token. Now pass 1 fans NB=256
4158    /// blocks across the SMs to saturate HBM, pass 2 reduces the NB partials. Bit-identical to host
4159    /// `argmax` (smallest index on tie). The whole point is NOT to dtoh logits — only a [1] u32 is read
4160    /// back (or kept resident for graph replay). Returns the device token buffer.
4161    /// Softmax probability of the (already-argmaxed) token `tok` under `logits` — the spec-decode
4162    /// p-min confidence signal. 2-pass like the parallel argmax; returns a device [1] f32.
4163    pub fn prob_of_token_device(&self, logits: &CudaSlice<f32>, tok: &CudaSlice<u32>, n_vocab: usize)
4164                                -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4165        let nb = ARGMAX_NB;
4166        let mut part = self.alloc_uninit::<f32>(nb)?;
4167        let mut p = self.alloc_uninit::<f32>(1)?;
4168        let f1 = self.func("prob_of_token_partial_f32");
4169        let cfg1 = LaunchConfig { grid_dim: (nb as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4170        let nv = n_vocab as i32;
4171        let __s_b1 = self.gpu.stream();
4172        let mut b1 = __s_b1.launch_builder(&f1);
4173        b1.arg(logits).arg(tok).arg(&mut part).arg(&nv);
4174        unsafe { b1.launch(cfg1)?; }
4175        let f2 = self.func("prob_of_token_final_f32");
4176        let cfg2 = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4177        let nbi = nb as i32;
4178        let __s_b2 = self.gpu.stream();
4179        let mut b2 = __s_b2.launch_builder(&f2);
4180        b2.arg(&part).arg(&mut p).arg(&nbi);
4181        unsafe { b2.launch(cfg2)?; }
4182        Ok(p)
4183    }
4184
4185    /// Like `prob_of_token_device` but writes into a PERSISTENT `p_out` buffer (stable pointer).
4186    /// Required for CUDA-graph capture of the draft chain: the captured prob kernels must write
4187    /// where the host reads the p-min confidence between replays. Same kernels, same math.
4188    /// Slot-addressed twin of `prob_of_token_device_into`: token read from `tok_all[tok_idx]`
4189    /// (a view at the slot), probability written to `p_out[p_idx]` — same two kernels, the
4190    /// pointers just land mid-buffer. Zero-sync (gemma confidence-adaptive draft depth).
4191    pub fn prob_of_token_device_col(&self, logits: &CudaSlice<f32>,
4192                                    tok_all: &CudaSlice<u32>, tok_idx: usize,
4193                                    p_out: &mut CudaSlice<f32>, p_idx: usize, n_vocab: usize)
4194                                    -> Result<(), Box<dyn std::error::Error>> {
4195        let tok_v = tok_all.slice(tok_idx..tok_idx + 1);
4196        let mut p_v = p_out.slice_mut(p_idx..p_idx + 1);
4197        let nb = ARGMAX_NB;
4198        let mut part = self.alloc_uninit::<f32>(nb)?;
4199        let f1 = self.func("prob_of_token_partial_f32");
4200        let cfg1 = LaunchConfig { grid_dim: (nb as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4201        let nv = n_vocab as i32;
4202        let __s_b1 = self.gpu.stream();
4203        let mut b1 = __s_b1.launch_builder(&f1);
4204        b1.arg(logits).arg(&tok_v).arg(&mut part).arg(&nv);
4205        unsafe { b1.launch(cfg1)?; }
4206        let f2 = self.func("prob_of_token_final_f32");
4207        let cfg2 = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4208        let nbi = nb as i32;
4209        let __s_b2 = self.gpu.stream();
4210        let mut b2 = __s_b2.launch_builder(&f2);
4211        b2.arg(&part).arg(&mut p_v).arg(&nbi);
4212        unsafe { b2.launch(cfg2)?; }
4213        Ok(())
4214    }
4215
4216    pub fn prob_of_token_device_into(&self, logits: &CudaSlice<f32>, tok: &CudaSlice<u32>,
4217                                     p_out: &mut CudaSlice<f32>, n_vocab: usize)
4218                                     -> Result<(), Box<dyn std::error::Error>> {
4219        let nb = ARGMAX_NB;
4220        let mut part = self.alloc_uninit::<f32>(nb)?;
4221        let f1 = self.func("prob_of_token_partial_f32");
4222        let cfg1 = LaunchConfig { grid_dim: (nb as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4223        let nv = n_vocab as i32;
4224        let __s_b1 = self.gpu.stream();
4225        let mut b1 = __s_b1.launch_builder(&f1);
4226        b1.arg(logits).arg(tok).arg(&mut part).arg(&nv);
4227        unsafe { b1.launch(cfg1)?; }
4228        let f2 = self.func("prob_of_token_final_f32");
4229        let cfg2 = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4230        let nbi = nb as i32;
4231        let __s_b2 = self.gpu.stream();
4232        let mut b2 = __s_b2.launch_builder(&f2);
4233        b2.arg(&part).arg(p_out).arg(&nbi);
4234        unsafe { b2.launch(cfg2)?; }
4235        Ok(())
4236    }
4237
4238    pub fn argmax_token_device(&self, logits: &CudaSlice<f32>, n_vocab: usize)
4239                               -> Result<CudaSlice<u32>, Box<dyn std::error::Error>> {
4240        let mut tok = unsafe { self.gpu.stream().alloc::<u32>(1)? };
4241        self.argmax_token_device_into(logits, &mut tok, n_vocab)?;
4242        Ok(tok)
4243    }
4244    /// Like `argmax_token_device` but writes into a PERSISTENT `tok` buffer (stable pointer) instead
4245    /// of allocating a fresh one. Required for CUDA-graph capture: the captured argmax must write the
4246    /// next token into the SAME device buffer the next replay's embed_gather reads, so the buffer
4247    /// pointer is baked once and the token id never round-trips to host inside steady state. The
4248    /// pass-1 partials scratch (`argmax_partials`) is also a resident stable-pointer buffer so both
4249    /// captured passes bake fixed addresses.
4250    pub fn argmax_token_device_into(&self, logits: &CudaSlice<f32>, tok: &mut CudaSlice<u32>,
4251                                    n_vocab: usize) -> Result<(), Box<dyn std::error::Error>> {
4252        let nb = ARGMAX_NB;
4253        let f1 = self.func("argmax_partial_f32");
4254        let f2 = self.func("argmax_final_f32");
4255        let mut guard = self.argmax_partials.lock().unwrap();
4256        if guard.is_none() {
4257            // allocate ONCE; under generate_graph this runs in the tracking-off prime window so the
4258            // buffers carry no cudarc events (illegal inside capture).
4259            let pv = self.gpu.stream().alloc_zeros::<f32>(nb)?;
4260            let pi = self.gpu.stream().alloc_zeros::<i32>(nb)?;
4261            *guard = Some((pv, pi));
4262        }
4263        let (part_v, part_i) = guard.as_mut().unwrap();
4264        let nv = n_vocab as i32;
4265        let nbi = nb as i32;
4266        // pass 1: NB blocks x 256 threads grid-stride scan -> per-block (val, idx) partials.
4267        let cfg1 = LaunchConfig { grid_dim: (nb as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4268        let __s_b1 = self.gpu.stream();
4269        let mut b1 = __s_b1.launch_builder(&f1);
4270        b1.arg(logits).arg(&mut *part_v).arg(&mut *part_i).arg(&nv);
4271        unsafe { b1.launch(cfg1)?; }
4272        // pass 2: one block reduces NB partials -> token_out[0].
4273        let cfg2 = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4274        let __s_b2 = self.gpu.stream();
4275        let mut b2 = __s_b2.launch_builder(&f2);
4276        b2.arg(&*part_v).arg(&*part_i).arg(tok).arg(&nbi);
4277        unsafe { b2.launch(cfg2)?; }
4278        Ok(())
4279    }
4280    /// Column-`col` device argmax over a stacked verify-logits buffer [t, n_vocab] (spec accept
4281    /// walk): toks[out_idx] = argmax(logits[col*n_vocab .. (col+1)*n_vocab]). SAME 2-pass kernels
4282    /// and tie-break contract as `argmax_token_device_into` (bit-identical to host argmax,
4283    /// argmax_gate-validated) — only the input pointer (a column view) and the output slot differ.
4284    /// Lets the accept walk read ONE [t] u32 instead of dtoh'ing the full [t, n_vocab] logits.
4285    pub fn argmax_token_device_col(&self, logits: &CudaSlice<f32>, col: usize, n_vocab: usize,
4286                                   toks: &mut CudaSlice<u32>, out_idx: usize)
4287                                   -> Result<(), Box<dyn std::error::Error>> {
4288        let nb = ARGMAX_NB;
4289        let f1 = self.func("argmax_partial_f32");
4290        let f2 = self.func("argmax_final_f32");
4291        let mut guard = self.argmax_partials.lock().unwrap();
4292        if guard.is_none() {
4293            let pv = self.gpu.stream().alloc_zeros::<f32>(nb)?;
4294            let pi = self.gpu.stream().alloc_zeros::<i32>(nb)?;
4295            *guard = Some((pv, pi));
4296        }
4297        let (part_v, part_i) = guard.as_mut().unwrap();
4298        let col_view = logits.slice(col * n_vocab..(col + 1) * n_vocab);
4299        let nv = n_vocab as i32;
4300        let nbi = nb as i32;
4301        let cfg1 = LaunchConfig { grid_dim: (nb as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4302        let __s_b1 = self.gpu.stream();
4303        let mut b1 = __s_b1.launch_builder(&f1);
4304        b1.arg(&col_view).arg(&mut *part_v).arg(&mut *part_i).arg(&nv);
4305        unsafe { b1.launch(cfg1)?; }
4306        let mut tok_view = toks.slice_mut(out_idx..out_idx + 1);
4307        let cfg2 = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4308        let __s_b2 = self.gpu.stream();
4309        let mut b2 = __s_b2.launch_builder(&f2);
4310        b2.arg(&*part_v).arg(&*part_i).arg(&mut tok_view).arg(&nbi);
4311        unsafe { b2.launch(cfg2)?; }
4312        Ok(())
4313    }
4314    /// Read back a device u32 buffer (the spec accept walk's [t] per-column argmax tokens).
4315    pub fn htod_u32_v(&self, v: &[u32]) -> Result<CudaSlice<u32>, Box<dyn std::error::Error>> {
4316        Ok(self.gpu.stream().clone_htod(v)?)
4317    }
4318    pub fn dtoh_u32(&self, d: &CudaSlice<u32>) -> Result<Vec<u32>, Box<dyn std::error::Error>> {
4319        let v = self.gpu.stream().clone_dtoh(d)?;
4320        self.gpu.stream().synchronize()?;
4321        Ok(v)
4322    }
4323    /// Allocate a zeroed device u32 buffer (persistent spec-loop prediction slots).
4324    /// H2D into an EXISTING u32 buffer (stable pointer — the per-step grammar-mask upload:
4325    /// contents change every step, the address must not, so a captured graph can read it).
4326    pub fn htod_u32_into(&self, dst: &mut CudaSlice<u32>, src: &[u32])
4327                         -> Result<(), Box<dyn std::error::Error>> {
4328        let mut view = dst.slice_mut(0..src.len());
4329        self.gpu.stream().memcpy_htod(src, &mut view)?;
4330        Ok(())
4331    }
4332
4333    /// H2D into an existing i32 buffer. OPTIPIPE uses this to refresh a stage-local saved-len
4334    /// table without changing the device address its reconcile kernel consumes.
4335    pub fn htod_i32_into(&self, dst: &mut CudaSlice<i32>, src: &[i32])
4336                         -> Result<(), Box<dyn std::error::Error>> {
4337        let mut view = dst.slice_mut(0..src.len());
4338        self.gpu.stream().memcpy_htod(src, &mut view)?;
4339        Ok(())
4340    }
4341
4342    pub fn alloc_u32_zeroed(&self, n: usize) -> Result<CudaSlice<u32>, Box<dyn std::error::Error>> {
4343        let s = self.gpu.stream().alloc_zeros::<u32>(n)?;
4344        self.keep_if_capturing(&s);
4345        Ok(s)
4346    }
4347    /// embed_gather into a PERSISTENT `x_out` buffer (stable pointer) for CUDA-graph capture (the
4348    /// embed output starts the per-step kernel chain and must be at a fixed address across replays).
4349    pub fn embed_gather_device_into(&self, embd: &CudaSlice<u8>, token_d: &CudaSlice<u32>,
4350                                    x_out: &mut CudaSlice<f32>, n_embd: usize, qtype: i32,
4351                                    row_bytes: usize) -> Result<(), Box<dyn std::error::Error>> {
4352        let f = self.func("embed_gather_u32");
4353        let cfg = LaunchConfig { grid_dim: (((n_embd as u32 + 255) / 256).max(1), 1, 1),
4354                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4355        let (ne, qt, rb) = (n_embd as i32, qtype, row_bytes as i64);
4356        let __s_b = self.gpu.stream();
4357        let mut b = __s_b.launch_builder(&f);
4358        b.arg(embd).arg(token_d).arg(x_out).arg(&ne).arg(&qt).arg(&rb);
4359        unsafe { b.launch(cfg)?; }
4360        Ok(())
4361    }
4362    /// Read a [1] i32 device counter (pos / seqlen) back to host. Tiny D2H + sync.
4363    pub fn dtoh_i32_one(&self, d: &CudaSlice<i32>) -> Result<i32, Box<dyn std::error::Error>> {
4364        let v = self.gpu.stream().clone_dtoh(d)?;
4365        self.gpu.stream().synchronize()?;
4366        Ok(v[0])
4367    }
4368    /// Set a [1] i32 device counter IN PLACE (keeps the buffer pointer stable — required for the
4369    /// graph-resident pos/seqlen counters whose addresses are baked into captured graphs). Restores
4370    /// the counter value after the throwaway capture warmups corrupt it.
4371    /// ASYNC i32 single-slot store (value rides the kernel arg — no host-memory transfer/sync).
4372    /// The graph-arc device-len counters use this; set_i32_one below is the SYNCING pageable
4373    /// copy (fine at stream-idle boundaries, poison mid-round).
4374    pub fn i32_set_k(&self, dst: &mut CudaSlice<i32>, v: i32)
4375                     -> Result<(), Box<dyn std::error::Error>> {
4376        let f = self.func("i32_set_k");
4377        let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (1, 1, 1), shared_mem_bytes: 0 };
4378        let idx = 0i32;
4379        let __s_b = self.gpu.stream();
4380        let mut b = __s_b.launch_builder(&f);
4381        b.arg(dst).arg(&v).arg(&idx);
4382        unsafe { b.launch(cfg)?; }
4383        Ok(())
4384    }
4385
4386    pub fn set_i32_one(&self, d: &mut CudaSlice<i32>, v: i32) -> Result<(), Box<dyn std::error::Error>> {
4387        self.gpu.stream().memcpy_htod(&[v], d)?;
4388        Ok(())
4389    }
4390    /// Set a [1] u32 device buffer IN PLACE (stable pointer) — for the resident `token_d` counter
4391    /// during priming / capture-state restore.
4392    pub fn set_u32_one(&self, d: &mut CudaSlice<u32>, v: u32) -> Result<(), Box<dyn std::error::Error>> {
4393        self.gpu.stream().memcpy_htod(&[v], d)?;
4394        Ok(())
4395    }
4396    /// Read back a [1] u32 device buffer (the argmax token). One tiny D2H + sync.
4397    pub fn dtoh_u32_one(&self, d: &CudaSlice<u32>) -> Result<u32, Box<dyn std::error::Error>> {
4398        let v = self.gpu.stream().clone_dtoh(d)?;
4399        self.gpu.stream().synchronize()?;
4400        Ok(v[0])
4401    }
4402    /// Upload raw bytes to a resident device u8 buffer (e.g. the embed table for device gather).
4403    pub fn upload_u8(&self, bytes: &[u8]) -> Result<CudaSlice<u8>, Box<dyn std::error::Error>> {
4404        Ok(self.gpu.stream().clone_htod(bytes)?)
4405    }
4406    /// Embed-from-device (CUDA-GRAPH-PLAN Phase 1): gather+dequant the row for the token id in
4407    /// `token_d[0]` from the resident embed table `embd` -> x_out[n_embd]. Bit-identical to host
4408    /// EmbedHost::gather (same per-dtype `deq`). No host round-trip of the token id.
4409    pub fn embed_gather_device(&self, embd: &CudaSlice<u8>, token_d: &CudaSlice<u32>,
4410                               n_embd: usize, qtype: i32, row_bytes: usize)
4411                               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4412        let f = self.func("embed_gather_u32");
4413        let mut x = self.alloc_uninit::<f32>(n_embd)?;
4414        let cfg = LaunchConfig { grid_dim: (((n_embd as u32 + 255) / 256).max(1), 1, 1),
4415                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4416        let (ne, qt, rb) = (n_embd as i32, qtype, row_bytes as i64);
4417        let __s_b = self.gpu.stream();
4418        let mut b = __s_b.launch_builder(&f);
4419        b.arg(embd).arg(token_d).arg(&mut x).arg(&ne).arg(&qt).arg(&rb);
4420        unsafe { b.launch(cfg)?; }
4421        Ok(x)
4422    }
4423
4424
4425    /// T-token device embed gather (spec verify/replay): tokens uploaded as a tiny [T] u32 htod,
4426    /// rows dequanted on-device -> x[T, n_embd]. Replaces host per-row dequant + T*n_embd*4B htod
4427    /// (nsys: 84% of spec API time was HtoD). Bit-identical rows (same per-dtype deq).
4428    pub fn embed_gather_device_t(&self, embd: &CudaSlice<u8>, tokens: &[u32],
4429                                 n_embd: usize, qtype: i32, row_bytes: usize)
4430                                 -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4431        let t = tokens.len();
4432        let tok_d = self.gpu.stream().clone_htod(tokens)?;
4433        let f = self.func("embed_gather_u32_t");
4434        let mut x = self.alloc_uninit::<f32>(t * n_embd)?;
4435        let cfg = LaunchConfig { grid_dim: (((n_embd as u32 + 255) / 256).max(1), t as u32, 1),
4436                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4437        let (ne, qt, rb, ti) = (n_embd as i32, qtype, row_bytes as i64, t as i32);
4438        let __s_b = self.gpu.stream();
4439        let mut b = __s_b.launch_builder(&f);
4440        b.arg(embd).arg(&tok_d).arg(&mut x).arg(&ne).arg(&qt).arg(&rb).arg(&ti);
4441        unsafe { b.launch(cfg)?; }
4442        Ok(x)
4443    }
4444
4445    /// T-token embed gather from a DEVICE token buffer (round-stream stage c: the verify tokens
4446    /// are assembled on-device from the draft-chain pack slots; no host round trip). Same kernel
4447    /// as embed_gather_device_t — bit-identical rows.
4448    /// embed_gather over a token VIEW (spec round: tokens live in the round's batch buffer).
4449    pub fn embed_gather_device_tv(&self, embd: &CudaSlice<u8>, tok_v: &cudarc::driver::CudaView<u32>,
4450                                  t: usize, n_embd: usize, qtype: i32, row_bytes: usize)
4451                                  -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4452        let f = self.func("embed_gather_u32_t");
4453        let mut x = self.alloc_uninit::<f32>(t * n_embd)?;
4454        let cfg = LaunchConfig { grid_dim: (((n_embd as u32 + 255) / 256).max(1), t as u32, 1),
4455                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4456        let (ne, qt, rb, ti) = (n_embd as i32, qtype, row_bytes as i64, t as i32);
4457        let __s_b = self.gpu.stream();
4458        let mut b = __s_b.launch_builder(&f);
4459        b.arg(embd).arg(tok_v).arg(&mut x).arg(&ne).arg(&qt).arg(&rb).arg(&ti);
4460        unsafe { b.launch(cfg)?; }
4461        Ok(x)
4462    }
4463
4464    pub fn embed_gather_device_td(&self, embd: &CudaSlice<u8>, tok_d: &CudaSlice<u32>, t: usize,
4465                                  n_embd: usize, qtype: i32, row_bytes: usize)
4466                                  -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4467        let f = self.func("embed_gather_u32_t");
4468        let mut x = self.alloc_uninit::<f32>(t * n_embd)?;
4469        let cfg = LaunchConfig { grid_dim: (((n_embd as u32 + 255) / 256).max(1), t as u32, 1),
4470                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
4471        let (ne, qt, rb, ti) = (n_embd as i32, qtype, row_bytes as i64, t as i32);
4472        let __s_b = self.gpu.stream();
4473        let mut b = __s_b.launch_builder(&f);
4474        b.arg(embd).arg(tok_d).arg(&mut x).arg(&ne).arg(&qt).arg(&rb).arg(&ti);
4475        unsafe { b.launch(cfg)?; }
4476        Ok(x)
4477    }
4478
4479    /// Uninitialized device buffer — SKIPS the memset that `alloc_zeros` always issues. Decode
4480    /// profile (nsys): ~1050 memsets/token = 6.5% of decode GPU time + ~half the launch count, the
4481    /// dominant contributor to the 19% inter-kernel idle gap and a blocker for clean CUDA-graph
4482    /// capture. Use ONLY for buffers a kernel FULLY overwrites (every element written, no `+=`).
4483    /// SAFETY: caller guarantees the producing kernel writes every element before any read.
4484    #[inline]
4485    /// Keep an allocation alive for the current capture (no-op when retain mode is off).
4486    fn keep_if_capturing<T: cudarc::driver::DeviceRepr + Send + 'static>(&self, s: &CudaSlice<T>) {
4487        if self.capture_keep_on.load(std::sync::atomic::Ordering::Relaxed) {
4488            self.capture_keep.lock().unwrap().push(Box::new(s.clone()));
4489        }
4490    }
4491
4492    fn alloc_uninit<T: cudarc::driver::DeviceRepr + Send + 'static>(&self, n: usize)
4493            -> Result<CudaSlice<T>, Box<dyn std::error::Error>> {
4494        let mut s = unsafe { self.gpu.stream().alloc::<T>(n)? };
4495        // MEMRA_DEBUG_ZERO_ALLOCS=1 (task #14 defect hunt): memset EVERY engine allocation —
4496        // the global uninit-read discriminator (the prime-fn-scoped zeroing experiment could
4497        // not cover engine-internal buffers). Debug-only: massive launch overhead.
4498        {
4499            static Z: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
4500            if *Z.get_or_init(|| std::env::var("MEMRA_DEBUG_ZERO_ALLOCS").as_deref() == Ok("1")) {
4501                // raw D8 memset (T lacks ValidAsZeroBits in the generic bound)
4502                use cudarc::driver::DevicePtrMut;
4503                let n_bytes = s.len() * std::mem::size_of::<T>();
4504                let stream = self.gpu.stream();
4505                let (p_, _g) = s.device_ptr_mut(&stream);
4506                unsafe {
4507                    cudarc::driver::sys::cuMemsetD8Async(p_, 0, n_bytes, stream.cu_stream())
4508                        .result()?;
4509                }
4510            }
4511        }
4512        self.keep_if_capturing(&s);
4513        Ok(s)
4514    }
4515
4516    /// Public f32 uninitialized scratch (see `alloc_uninit`). For decode/forward scratch a kernel
4517    /// fully overwrites. SAFETY: producing kernel must write every element before any read.
4518    /// Uninitialized q8_1 activation pair (int8 + per-32 scales) — the fa combine q8-emit
4519    /// consumers alloc through this (m=1 decode arms).
4520    pub fn uninit_q8_pair(&self, n: usize)
4521        -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
4522        Ok((self.alloc_uninit::<i8>(n)?, self.alloc_uninit::<f32>(n / 32)?))
4523    }
4524
4525    pub fn uninit(&self, n: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
4526        self.alloc_uninit::<f32>(n)
4527    }
4528
4529    /// i8 uninitialized scratch (same contract as `uninit`).
4530    pub fn alloc_i8_uninit(&self, n: usize) -> Result<CudaSlice<i8>, Box<dyn std::error::Error>> {
4531        self.alloc_uninit::<i8>(n)
4532    }
4533
4534    /// RMSNorm: x[ncols,nrows] row-major, weight[ncols] -> dst. One block/row, 256 threads.
4535    /// gemma4: 3 rms_norms of the SAME input in one launch (one reduction, three weights).
4536    /// Per-output bit-identical to three rms_norm calls (verbatim reduction/scale chain).
4537    #[allow(clippy::too_many_arguments)]
4538    pub fn rms_norm3(&self, x: &CudaSlice<f32>, w0: &CudaSlice<f32>, w1: &CudaSlice<f32>,
4539                     w2: &CudaSlice<f32>, d0: &mut CudaSlice<f32>, d1: &mut CudaSlice<f32>,
4540                     d2: &mut CudaSlice<f32>, ncols: usize, nrows: usize, eps: f32)
4541                     -> Result<(), Box<dyn std::error::Error>> {
4542        let f = self.func("rms_norm3_f32");
4543        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
4544        let (nc, e) = (ncols as i32, eps);
4545        let __s_b = self.gpu.stream();
4546        let mut b = __s_b.launch_builder(&f);
4547        b.arg(x).arg(w0).arg(w1).arg(w2).arg(d0).arg(d1).arg(d2).arg(&nc).arg(&e);
4548        unsafe { b.launch(cfg)?; }
4549        Ok(())
4550    }
4551
4552    /// gemma4 fused q/k/v head norms (one launch, per-row rms_norm_f32-verbatim).
4553    #[allow(clippy::too_many_arguments)]
4554    /// True when the warp-per-row qkv norm would engage for (rows, ncols) — the emit lane
4555    /// piggybacks on the same conditions.
4556    pub fn qkvnorm_w_on_prefill(rows: usize, ncols: usize) -> bool {
4557        static WARP_ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
4558        *WARP_ON.get_or_init(|| {
4559            std::env::var("MEMRA_QKVNORM_W").map(|v| v != "0").unwrap_or(true)
4560        }) && ncols % 4 == 0 && rows >= 64
4561    }
4562
4563    /// w4 norm with bf16 V EMIT (31B glue lane): the v segment also writes its normed rows as
4564    /// bf16 (the FA V operand — bit-identical to a post-hoc f32_to_bf16). Prefill-depth only.
4565    #[allow(clippy::too_many_arguments)]
4566    pub fn rms_norm_qkv_w4b(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
4567                        wq: &CudaSlice<f32>, wk: &CudaSlice<f32>, wv: &CudaSlice<f32>,
4568                        dq: &mut CudaSlice<f32>, dk: &mut CudaSlice<f32>, dv: &mut CudaSlice<f32>,
4569                        dvb: &mut CudaSlice<u8>,
4570                        ncols: usize, rq: usize, rk: usize, eps: f32, vf16: bool)
4571                        -> Result<(), Box<dyn std::error::Error>> {
4572        assert!(ncols % 4 == 0 && rq + 2 * rk >= 64);
4573        let f = self.func("rms_norm_qkv_w4b_f32");
4574        let rows = (rq + 2 * rk) as u32;
4575        let cfg = LaunchConfig {
4576            grid_dim: (rows.div_ceil(8), 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0,
4577        };
4578        let (nc, rqi, rki, rvi, e) = (ncols as i32, rq as i32, rk as i32, rk as i32, eps);
4579        let vf = vf16 as i32;
4580        let __s_b = self.gpu.stream();
4581        let mut b = __s_b.launch_builder(&f);
4582        b.arg(q).arg(k).arg(v).arg(wq).arg(wk).arg(wv).arg(dq).arg(dk).arg(dv).arg(&mut *dvb)
4583         .arg(&nc).arg(&rqi).arg(&rki).arg(&rvi).arg(&e).arg(&vf);
4584        unsafe { b.launch(cfg)?; }
4585        Ok(())
4586    }
4587
4588    pub fn rms_norm_qkv(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
4589                        wq: &CudaSlice<f32>, wk: &CudaSlice<f32>, wv: &CudaSlice<f32>,
4590                        dq: &mut CudaSlice<f32>, dk: &mut CudaSlice<f32>, dv: &mut CudaSlice<f32>,
4591                        ncols: usize, rq: usize, rk: usize, eps: f32)
4592                        -> Result<(), Box<dyn std::error::Error>> {
4593        // Warp-per-row float4 twin (default; MEMRA_QKVNORM_W=0 reverts): the block-per-row form
4594        // spends 767us/launch on 17k+ 2KB rows at prefill depth (launch/reduce latency-bound,
4595        // ~92GB/s). Own numeric config (reduce order differs) — battery-gated.
4596        static WARP_ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
4597        let warp_on = *WARP_ON.get_or_init(|| {
4598            std::env::var("MEMRA_QKVNORM_W").map(|v| v != "0").unwrap_or(true)
4599        });
4600        // rows >= 64 keeps decode (nh + 2*nkv rows) on the block-tree kernel — decode/verify/
4601        // replay numerics are untouched on every model; only prefill depth takes the new config.
4602        if warp_on && ncols % 4 == 0 && rq + 2 * rk >= 64 {
4603            let f = self.func("rms_norm_qkv_w4_f32");
4604            let rows = (rq + 2 * rk) as u32;
4605            let cfg = LaunchConfig {
4606                grid_dim: (rows.div_ceil(8), 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0,
4607            };
4608            let (nc, rqi, rki, rvi, e) = (ncols as i32, rq as i32, rk as i32, rk as i32, eps);
4609            let __s_b = self.gpu.stream();
4610            let mut b = __s_b.launch_builder(&f);
4611            b.arg(q).arg(k).arg(v).arg(wq).arg(wk).arg(wv).arg(dq).arg(dk).arg(dv)
4612             .arg(&nc).arg(&rqi).arg(&rki).arg(&rvi).arg(&e);
4613            unsafe { b.launch(cfg)?; }
4614            return Ok(());
4615        }
4616        let f = self.func("rms_norm_qkv_f32");
4617        let grid = (rq + 2 * rk) as u32;
4618        let cfg = LaunchConfig { grid_dim: (grid, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
4619        let (nc, rqi, rki, e) = (ncols as i32, rq as i32, rk as i32, eps);
4620        let __s_b = self.gpu.stream();
4621        let mut b = __s_b.launch_builder(&f);
4622        b.arg(q).arg(k).arg(v).arg(wq).arg(wk).arg(wv).arg(dq).arg(dk).arg(dv)
4623         .arg(&nc).arg(&rqi).arg(&rki).arg(&e);
4624        unsafe { b.launch(cfg)?; }
4625        Ok(())
4626    }
4627
4628    /// gemma4 fused pair of rms_norms over two different inputs (same width).
4629    #[allow(clippy::too_many_arguments)]
4630    pub fn rms_norm2x(&self, a: &CudaSlice<f32>, bb: &CudaSlice<f32>, wa: &CudaSlice<f32>,
4631                      wb: &CudaSlice<f32>, da: &mut CudaSlice<f32>, db: &mut CudaSlice<f32>,
4632                      ncols: usize, nrows: usize, eps: f32)
4633                      -> Result<(), Box<dyn std::error::Error>> {
4634        let f = self.func("rms_norm2x_f32");
4635        let cfg = LaunchConfig { grid_dim: (2 * nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
4636        let (nc, nr, e) = (ncols as i32, nrows as i32, eps);
4637        let __s_b = self.gpu.stream();
4638        let mut b = __s_b.launch_builder(&f);
4639        b.arg(a).arg(bb).arg(wa).arg(wb).arg(da).arg(db).arg(&nc).arg(&nr).arg(&e);
4640        unsafe { b.launch(cfg)?; }
4641        Ok(())
4642    }
4643
4644    /// gemma4 R4: in-place final-logit softcap y = cap*tanh(y/cap).
4645    pub fn softcap(&self, y: &mut CudaSlice<f32>, cap: f32, n: usize)
4646                   -> Result<(), Box<dyn std::error::Error>> {
4647        let f = self.func("softcap_f32");
4648        let cfg = LaunchConfig::for_num_elems(n as u32);
4649        let ni = n as i32;
4650        let __s_b = self.gpu.stream();
4651        let mut b = __s_b.launch_builder(&f);
4652        b.arg(y).arg(&cap).arg(&ni);
4653        unsafe { b.launch(cfg)?; }
4654        Ok(())
4655    }
4656
4657    /// gemma4 suppress-token mask: y[row][ids[j]] = -inf over t logits rows (fixed-arg launch —
4658    /// graph-capture safe; NOT monotonic like softcap, so it must run before any argmax).
4659    pub fn mask_ids_rows(&self, y: &mut CudaSlice<f32>, ids: &CudaSlice<i32>, n_ids: usize,
4660                         n_vocab: usize, t: usize)
4661                         -> Result<(), Box<dyn std::error::Error>> {
4662        let f = self.func("mask_ids_rows_f32");
4663        let cfg = LaunchConfig::for_num_elems((n_ids * t) as u32);
4664        let (ni, nv, ti) = (n_ids as i32, n_vocab as i32, t as i32);
4665        let __s_b = self.gpu.stream();
4666        let mut b = __s_b.launch_builder(&f);
4667        b.arg(y).arg(ids).arg(&ni).arg(&nv).arg(&ti);
4668        unsafe { b.launch(cfg)?; }
4669        Ok(())
4670    }
4671
4672    /// gemma4: res = (a+b)*c AND dst = rms_norm(res, w) in one launch.
4673    #[allow(clippy::too_many_arguments)]
4674    pub fn add_scale_rms_norm(&self, a: &CudaSlice<f32>, b_in: &CudaSlice<f32>, c: f32,
4675                              w: &CudaSlice<f32>, res: &mut CudaSlice<f32>, dst: &mut CudaSlice<f32>,
4676                              ncols: usize, nrows: usize, eps: f32)
4677                              -> Result<(), Box<dyn std::error::Error>> {
4678        let f = self.func("add_scale_rms_norm_f32");
4679        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
4680        let (nc, e2) = (ncols as i32, eps);
4681        let __s_b = self.gpu.stream();
4682        let mut b = __s_b.launch_builder(&f);
4683        b.arg(a).arg(b_in).arg(&c).arg(w).arg(res).arg(dst).arg(&nc).arg(&e2);
4684        unsafe { b.launch(cfg)?; }
4685        Ok(())
4686    }
4687
4688    /// gemma4: res = (a+b)*c AND the next layer's attn_norm EMITTED q8_1 in one launch.
4689    /// Quantize epilogue bit-identical to quantize_q8_1 (the rms_norm_q8_1 form).
4690    #[allow(clippy::too_many_arguments)]
4691    pub fn add_scale_rms_norm_q8_1(&self, a: &CudaSlice<f32>, b_in: &CudaSlice<f32>, c: f32,
4692                                   w: &CudaSlice<f32>, res: &mut CudaSlice<f32>,
4693                                   ncols: usize, nrows: usize, eps: f32)
4694                                   -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
4695        let mut out_q = self.alloc_uninit::<i8>(nrows * ncols)?;
4696        let mut out_d = self.alloc_uninit::<f32>(nrows * (ncols / 32))?;
4697        let (nc, e2) = (ncols as i32, eps);
4698        if Self::pdl_on() && Self::pdl_wb_on() {
4699            {
4700            use cudarc::driver::{DevicePtr, DevicePtrMut};
4701            let s = &self.gpu.stream();
4702            let (pa, _g0) = a.device_ptr(s); let (pb, _g1) = b_in.device_ptr(s);
4703            let (pw, _g2) = w.device_ptr(s); let (pr, _g3) = res.device_ptr_mut(s);
4704            let (pq, _g4) = out_q.device_ptr_mut(s); let (pd, _g5) = out_d.device_ptr_mut(s);
4705            let mut ps = [
4706                &pa as *const _ as *mut std::ffi::c_void, &pb as *const _ as *mut _,
4707                &c as *const _ as *mut _, &pw as *const _ as *mut _,
4708                &pr as *const _ as *mut _, &pq as *const _ as *mut _,
4709                &pd as *const _ as *mut _, &nc as *const _ as *mut _,
4710                &e2 as *const _ as *mut _,
4711            ];
4712            unsafe { self.launch_pdl("add_scale_rms_norm_q8_1", (nrows as u32, 1, 1),
4713                                     (rms_block(), 1, 1), &mut ps)?; }
4714            }
4715            return Ok((out_q, out_d));
4716        }
4717        let f = self.func("add_scale_rms_norm_q8_1");
4718        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
4719        let __s_b = self.gpu.stream();
4720        let mut b = __s_b.launch_builder(&f);
4721        b.arg(a).arg(b_in).arg(&c).arg(w).arg(res).arg(&mut out_q).arg(&mut out_d).arg(&nc).arg(&e2);
4722        unsafe { b.launch(cfg)?; }
4723        Ok((out_q, out_d))
4724    }
4725
4726    /// Slot-fed add_scale_rms_norm_q8_1 twin (alloc-free capture lane).
4727    #[allow(clippy::too_many_arguments)]
4728    pub fn add_scale_rms_norm_q8_1_into(&self, a: &CudaSlice<f32>, b_in: &CudaSlice<f32>, c: f32,
4729                                        w: &CudaSlice<f32>, res: &mut CudaSlice<f32>,
4730                                        ncols: usize, nrows: usize, eps: f32,
4731                                        out_q: &mut CudaSlice<i8>, out_d: &mut CudaSlice<f32>)
4732                                        -> Result<(), Box<dyn std::error::Error>> {
4733        debug_assert!(out_q.len() >= nrows * ncols && out_d.len() >= nrows * (ncols / 32));
4734        let (nc, e2) = (ncols as i32, eps);
4735        if Self::pdl_on() && Self::pdl_wb_on() {
4736            use cudarc::driver::{DevicePtr, DevicePtrMut};
4737            let s = &self.gpu.stream();
4738            let (pa, _g0) = a.device_ptr(s); let (pb, _g1) = b_in.device_ptr(s);
4739            let (pw, _g2) = w.device_ptr(s); let (pr, _g3) = res.device_ptr_mut(s);
4740            let (pq, _g4) = out_q.device_ptr_mut(s); let (pd, _g5) = out_d.device_ptr_mut(s);
4741            let mut ps = [
4742                &pa as *const _ as *mut std::ffi::c_void, &pb as *const _ as *mut _,
4743                &c as *const _ as *mut _, &pw as *const _ as *mut _,
4744                &pr as *const _ as *mut _, &pq as *const _ as *mut _,
4745                &pd as *const _ as *mut _, &nc as *const _ as *mut _,
4746                &e2 as *const _ as *mut _,
4747            ];
4748            unsafe { self.launch_pdl("add_scale_rms_norm_q8_1", (nrows as u32, 1, 1),
4749                                     (rms_block(), 1, 1), &mut ps)?; }
4750            return Ok(());
4751        }
4752        let f = self.func("add_scale_rms_norm_q8_1");
4753        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
4754        let __s_b = self.gpu.stream();
4755        let mut b = __s_b.launch_builder(&f);
4756        b.arg(a).arg(b_in).arg(&c).arg(w).arg(res).arg(&mut *out_q).arg(&mut *out_d).arg(&nc).arg(&e2);
4757        unsafe { b.launch(cfg)?; }
4758        Ok(())
4759    }
4760
4761    /// E4B glue fusion: rms(a, wa) prologue + the add_scale_rms_norm_q8_1 program — one launch
4762    /// replaces the per-layer rms_norm_f32(y) + emit pair in the PLE tail.
4763    #[allow(clippy::too_many_arguments)]
4764    pub fn rms_pre_add_scale_rms_norm_q8_1(&self, a: &CudaSlice<f32>, wa: &CudaSlice<f32>,
4765                                           b_in: &CudaSlice<f32>, c: f32,
4766                                           w: &CudaSlice<f32>, res: &mut CudaSlice<f32>,
4767                                           ncols: usize, nrows: usize, eps: f32)
4768                                           -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
4769        let mut out_q = self.alloc_uninit::<i8>(nrows * ncols)?;
4770        let mut out_d = self.alloc_uninit::<f32>(nrows * (ncols / 32))?;
4771        let (nc, e2) = (ncols as i32, eps);
4772        if Self::pdl_on() {
4773            {
4774            use cudarc::driver::{DevicePtr, DevicePtrMut};
4775            let s = &self.gpu.stream();
4776            let (pa, _g0) = a.device_ptr(s); let (pwa, _g1) = wa.device_ptr(s);
4777            let (pb, _g2) = b_in.device_ptr(s); let (pw, _g3) = w.device_ptr(s);
4778            let (pr, _g4) = res.device_ptr_mut(s);
4779            let (pq, _g5) = out_q.device_ptr_mut(s); let (pd, _g6) = out_d.device_ptr_mut(s);
4780            let mut ps = [
4781                &pa as *const _ as *mut std::ffi::c_void, &pwa as *const _ as *mut _,
4782                &pb as *const _ as *mut _, &c as *const _ as *mut _,
4783                &pw as *const _ as *mut _, &pr as *const _ as *mut _,
4784                &pq as *const _ as *mut _, &pd as *const _ as *mut _,
4785                &nc as *const _ as *mut _, &e2 as *const _ as *mut _,
4786            ];
4787            unsafe { self.launch_pdl("rms_pre_add_scale_rms_norm_q8_1", (nrows as u32, 1, 1),
4788                                     (rms_block(), 1, 1), &mut ps)?; }
4789            }
4790            return Ok((out_q, out_d));
4791        }
4792        let f = self.func("rms_pre_add_scale_rms_norm_q8_1");
4793        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
4794        let __s_b = self.gpu.stream();
4795        let mut b = __s_b.launch_builder(&f);
4796        b.arg(a).arg(wa).arg(b_in).arg(&c).arg(w).arg(res).arg(&mut out_q).arg(&mut out_d).arg(&nc).arg(&e2);
4797        unsafe { b.launch(cfg)?; }
4798        Ok((out_q, out_d))
4799    }
4800
4801    /// GELU(tanh)*up with the activation emitted q8_1 alongside f32 (glue-fusion lane): the
4802    /// consumer matmul rides matmul_pre, killing its standalone quantize_q8_1 launch.
4803    pub fn gelu_tanh_mul_q8_1(&self, gate: &CudaSlice<f32>, up: &cudarc::driver::CudaView<f32>,
4804                              act: &mut CudaSlice<f32>, ncols: usize, nrows: usize)
4805                              -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
4806        debug_assert!(ncols % 128 == 0);
4807        let mut out_q = self.alloc_uninit::<i8>(nrows * ncols)?;
4808        let mut out_d = self.alloc_uninit::<f32>(nrows * (ncols / 32))?;
4809        let nc = ncols as i32;
4810        if Self::pdl_on() {
4811            {
4812            use cudarc::driver::{DevicePtr, DevicePtrMut};
4813            let s = &self.gpu.stream();
4814            let (pg, _g0) = gate.device_ptr(s); let (pu, _g1) = up.device_ptr(s);
4815            let (pact, _g2) = act.device_ptr_mut(s);
4816            let (pq, _g3) = out_q.device_ptr_mut(s); let (pd, _g4) = out_d.device_ptr_mut(s);
4817            let mut ps = [
4818                &pg as *const _ as *mut std::ffi::c_void, &pu as *const _ as *mut _,
4819                &pact as *const _ as *mut _, &pq as *const _ as *mut _,
4820                &pd as *const _ as *mut _, &nc as *const _ as *mut _,
4821            ];
4822            unsafe { self.launch_pdl("gelu_tanh_mul_q8_1", (nrows as u32, 1, 1),
4823                                     (rms_block(), 1, 1), &mut ps)?; }
4824            }
4825            return Ok((out_q, out_d));
4826        }
4827        let f = self.func("gelu_tanh_mul_q8_1");
4828        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
4829        let __s_b = self.gpu.stream();
4830        let mut b = __s_b.launch_builder(&f);
4831        b.arg(gate).arg(up).arg(act).arg(&mut out_q).arg(&mut out_d).arg(&nc);
4832        unsafe { b.launch(cfg)?; }
4833        Ok((out_q, out_d))
4834    }
4835
4836    /// Slot-fed gelu_tanh_mul_q8_1 twin (alloc-free capture lane; incl. the PDL arm).
4837    #[allow(clippy::too_many_arguments)]
4838    pub fn gelu_tanh_mul_q8_1_into(&self, gate: &CudaSlice<f32>, up: &cudarc::driver::CudaView<f32>,
4839                                   act: &mut CudaSlice<f32>, ncols: usize, nrows: usize,
4840                                   out_q: &mut CudaSlice<i8>, out_d: &mut CudaSlice<f32>)
4841                                   -> Result<(), Box<dyn std::error::Error>> {
4842        debug_assert!(ncols % 128 == 0);
4843        debug_assert!(out_q.len() >= nrows * ncols && out_d.len() >= nrows * (ncols / 32));
4844        let nc = ncols as i32;
4845        if Self::pdl_on() {
4846            use cudarc::driver::{DevicePtr, DevicePtrMut};
4847            let s = &self.gpu.stream();
4848            let (pg, _g0) = gate.device_ptr(s); let (pu, _g1) = up.device_ptr(s);
4849            let (pact, _g2) = act.device_ptr_mut(s);
4850            let (pq, _g3) = out_q.device_ptr_mut(s); let (pd, _g4) = out_d.device_ptr_mut(s);
4851            let mut ps = [
4852                &pg as *const _ as *mut std::ffi::c_void, &pu as *const _ as *mut _,
4853                &pact as *const _ as *mut _, &pq as *const _ as *mut _,
4854                &pd as *const _ as *mut _, &nc as *const _ as *mut _,
4855            ];
4856            unsafe { self.launch_pdl("gelu_tanh_mul_q8_1", (nrows as u32, 1, 1),
4857                                     (rms_block(), 1, 1), &mut ps)?; }
4858            return Ok(());
4859        }
4860        let f = self.func("gelu_tanh_mul_q8_1");
4861        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
4862        let __s_b = self.gpu.stream();
4863        let mut b = __s_b.launch_builder(&f);
4864        b.arg(gate).arg(up).arg(&mut *act).arg(&mut *out_q).arg(&mut *out_d).arg(&nc);
4865        unsafe { b.launch(cfg)?; }
4866        Ok(())
4867    }
4868
4869    /// gemma4: add + rms_norm3 with outputs 0/2 emitted q8_1 (zsh + moe_in) and 1 f32 (router).
4870    #[allow(clippy::too_many_arguments)]
4871    pub fn add_rms_norm3_q8z(&self, a: &CudaSlice<f32>, b_in: &CudaSlice<f32>,
4872                             w0: &CudaSlice<f32>, w1: &CudaSlice<f32>, w2: &CudaSlice<f32>,
4873                             res: &mut CudaSlice<f32>, out1: &mut CudaSlice<f32>,
4874                             ncols: usize, nrows: usize, eps: f32)
4875                             -> Result<((CudaSlice<i8>, CudaSlice<f32>), (CudaSlice<i8>, CudaSlice<f32>)), Box<dyn std::error::Error>> {
4876        let mut q0 = self.alloc_uninit::<i8>(nrows * ncols)?;
4877        let mut d0 = self.alloc_uninit::<f32>(nrows * (ncols / 32))?;
4878        let mut q2 = self.alloc_uninit::<i8>(nrows * ncols)?;
4879        let mut d2 = self.alloc_uninit::<f32>(nrows * (ncols / 32))?;
4880        let f = self.func("add_rms_norm3_q8z_f32");
4881        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
4882        let (nc, e2) = (ncols as i32, eps);
4883        let __s_b = self.gpu.stream();
4884        let mut b = __s_b.launch_builder(&f);
4885        b.arg(a).arg(b_in).arg(w0).arg(w1).arg(w2).arg(res)
4886         .arg(&mut q0).arg(&mut d0).arg(out1).arg(&mut q2).arg(&mut d2).arg(&nc).arg(&e2);
4887        unsafe { b.launch(cfg)?; }
4888        Ok(((q0, d0), (q2, d2)))
4889    }
4890
4891    /// gemma4: res = a+b AND the three rms_norms of res in one launch.
4892    #[allow(clippy::too_many_arguments)]
4893    pub fn add_rms_norm3(&self, a: &CudaSlice<f32>, b_in: &CudaSlice<f32>,
4894                         w0: &CudaSlice<f32>, w1: &CudaSlice<f32>, w2: &CudaSlice<f32>,
4895                         res: &mut CudaSlice<f32>, d0: &mut CudaSlice<f32>, d1: &mut CudaSlice<f32>,
4896                         d2: &mut CudaSlice<f32>, ncols: usize, nrows: usize, eps: f32)
4897                         -> Result<(), Box<dyn std::error::Error>> {
4898        let f = self.func("add_rms_norm3_f32");
4899        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
4900        let (nc, e2) = (ncols as i32, eps);
4901        let __s_b = self.gpu.stream();
4902        let mut b = __s_b.launch_builder(&f);
4903        b.arg(a).arg(b_in).arg(w0).arg(w1).arg(w2).arg(res).arg(d0).arg(d1).arg(d2).arg(&nc).arg(&e2);
4904        unsafe { b.launch(cfg)?; }
4905        Ok(())
4906    }
4907
4908    /// dst = (a + b) * c (residual add + layer scale, one launch).
4909    pub fn add_scale(&self, a: &CudaSlice<f32>, b_in: &CudaSlice<f32>, c: f32,
4910                     dst: &mut CudaSlice<f32>, n: usize) -> Result<(), Box<dyn std::error::Error>> {
4911        let f = self.func("add_scale_f32");
4912        let cfg = LaunchConfig::for_num_elems(n as u32);
4913        let ni = n as i32;
4914        let __s_b = self.gpu.stream();
4915        let mut b = __s_b.launch_builder(&f);
4916        b.arg(a).arg(b_in).arg(&c).arg(dst).arg(&ni);
4917        unsafe { b.launch(cfg)?; }
4918        Ok(())
4919    }
4920
4921    pub fn rms_norm(&self, x: &CudaSlice<f32>, w: &CudaSlice<f32>, dst: &mut CudaSlice<f32>,
4922                    ncols: usize, nrows: usize, eps: f32) -> Result<(), Box<dyn std::error::Error>> {
4923        let (nc, e) = (ncols as i32, eps);
4924        if Self::pdl_on() && Self::pdl_wb_on() {
4925            use cudarc::driver::{DevicePtr, DevicePtrMut};
4926            let s = &self.gpu.stream();
4927            let (px, _g0) = x.device_ptr(s); let (pw, _g1) = w.device_ptr(s);
4928            let (pd, _g2) = dst.device_ptr_mut(s);
4929            let mut ps = [
4930                &px as *const _ as *mut std::ffi::c_void, &pw as *const _ as *mut _,
4931                &pd as *const _ as *mut _, &nc as *const _ as *mut _,
4932                &e as *const _ as *mut _,
4933            ];
4934            unsafe { self.launch_pdl("rms_norm_f32", (nrows as u32, 1, 1),
4935                                     (rms_block(), 1, 1), &mut ps)?; }
4936            return Ok(());
4937        }
4938        let f = self.func("rms_norm_f32");
4939        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
4940        let __s_b = self.gpu.stream();
4941        let mut b = __s_b.launch_builder(&f);
4942        b.arg(x).arg(w).arg(dst).arg(&nc).arg(&e);
4943        unsafe { b.launch(cfg)?; }
4944        Ok(())
4945    }
4946
4947    /// RMS-norm with blockDim=1024 — BIT-IDENTICAL to the fused `rms_norm_q8_1` and
4948    /// `add_rms_norm_q8_1` kernels' sum-of-squares reduction. The spec verify path MUST use this
4949    /// to match decode's FP accumulation order: the standard `rms_norm` at blockDim=256 has a
4950    /// different per-thread stride (ncols/256 partials vs ncols/1024 partials) and therefore a
4951    /// different shfl-tree reduction that can shift `scale = rsqrt(sum/n + eps)` by ULPs, causing
4952    /// divergence through the GDN scan and argmax flips on the 9B text prompt. The underlying
4953    /// `rms_norm_f32` kernel supports any blockDim (generic reduce with shared[32]).
4954    pub fn rms_norm_decode(&self, x: &CudaSlice<f32>, w: &CudaSlice<f32>, dst: &mut CudaSlice<f32>,
4955                           ncols: usize, nrows: usize, eps: f32) -> Result<(), Box<dyn std::error::Error>> {
4956        let f = self.func("rms_norm_f32");
4957        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (1024, 1, 1), shared_mem_bytes: 0 };
4958        let (nc, e) = (ncols as i32, eps);
4959        let __s_b = self.gpu.stream();
4960        let mut b = __s_b.launch_builder(&f);
4961        b.arg(x).arg(w).arg(dst).arg(&nc).arg(&e);
4962        unsafe { b.launch(cfg)?; }
4963        Ok(())
4964    }
4965
4966    /// DECODE GLUE-FUSION LEVER: `z = rms_norm(x)*w` emitted DIRECTLY as q8_1 (no f32 `z` materialized,
4967    /// no standalone quantize_q8_1 launch). Returns (out_q [nrows*ncols i8], out_d [nrows*nblk f32])
4968    /// ready to feed matmul_pre. BIT-IDENTICAL to rms_norm + quantize_q8_1. ncols % 32 == 0.
4969    pub fn rms_norm_q8_1(&self, x: &CudaSlice<f32>, w: &CudaSlice<f32>, ncols: usize, nrows: usize,
4970                         eps: f32) -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
4971        let nblk = ncols / 32;
4972        let mut q = self.alloc_uninit::<i8>(nrows * ncols)?;
4973        let mut d = self.alloc_uninit::<f32>(nrows * nblk)?;
4974        let (nc, e) = (ncols as i32, eps);
4975        if Self::pdl_on() {
4976            {
4977            use cudarc::driver::{DevicePtr, DevicePtrMut};
4978            let s = &self.gpu.stream();
4979            let (px, _g0) = x.device_ptr(s); let (pw, _g1) = w.device_ptr(s);
4980            let (pq, _g2) = q.device_ptr_mut(s); let (pd, _g3) = d.device_ptr_mut(s);
4981            let mut ps = [
4982                &px as *const _ as *mut std::ffi::c_void, &pw as *const _ as *mut _,
4983                &pq as *const _ as *mut _, &pd as *const _ as *mut _,
4984                &nc as *const _ as *mut _, &e as *const _ as *mut _,
4985            ];
4986            unsafe { self.launch_pdl("rms_norm_q8_1", (nrows as u32, 1, 1), (1024, 1, 1),
4987                                     &mut ps)?; }
4988            }
4989            return Ok((q, d));
4990        }
4991        let f = self.func("rms_norm_q8_1");
4992        // 1024 threads: decode is nrows=1 -> ONE CTA; 32 warps hide the pass1->pass2 latency
4993        // (s[32] reduce already sized for 32 warps). Same shape math at any blockDim.
4994        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (1024, 1, 1), shared_mem_bytes: 0 };
4995        let __s_b = self.gpu.stream();
4996        let mut b = __s_b.launch_builder(&f);
4997        b.arg(x).arg(w).arg(&mut q).arg(&mut d).arg(&nc).arg(&e);
4998        unsafe { b.launch(cfg)?; }
4999        Ok((q, d))
5000    }
5001
5002    /// Slot-fed rms_norm_q8_1 twin (alloc-free capture lane): identical launch (incl. the
5003    /// PDL arm), caller-owned outputs.
5004    pub fn rms_norm_q8_1_into(&self, x: &CudaSlice<f32>, w: &CudaSlice<f32>, ncols: usize,
5005                              nrows: usize, eps: f32,
5006                              q: &mut CudaSlice<i8>, d: &mut CudaSlice<f32>)
5007                              -> Result<(), Box<dyn std::error::Error>> {
5008        let nblk = ncols / 32;
5009        debug_assert!(q.len() >= nrows * ncols && d.len() >= nrows * nblk);
5010        let (nc, e) = (ncols as i32, eps);
5011        if Self::pdl_on() {
5012            use cudarc::driver::{DevicePtr, DevicePtrMut};
5013            let s = &self.gpu.stream();
5014            let (px, _g0) = x.device_ptr(s); let (pw, _g1) = w.device_ptr(s);
5015            let (pq, _g2) = q.device_ptr_mut(s); let (pd, _g3) = d.device_ptr_mut(s);
5016            let mut ps = [
5017                &px as *const _ as *mut std::ffi::c_void, &pw as *const _ as *mut _,
5018                &pq as *const _ as *mut _, &pd as *const _ as *mut _,
5019                &nc as *const _ as *mut _, &e as *const _ as *mut _,
5020            ];
5021            unsafe { self.launch_pdl("rms_norm_q8_1", (nrows as u32, 1, 1), (1024, 1, 1),
5022                                     &mut ps)?; }
5023            return Ok(());
5024        }
5025        let f = self.func("rms_norm_q8_1");
5026        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (1024, 1, 1), shared_mem_bytes: 0 };
5027        let __s_b = self.gpu.stream();
5028        let mut b = __s_b.launch_builder(&f);
5029        b.arg(x).arg(w).arg(&mut *q).arg(&mut *d).arg(&nc).arg(&e);
5030        unsafe { b.launch(cfg)?; }
5031        Ok(())
5032    }
5033
5034    /// Slot-fed quantize_q8_1 twin (alloc-free capture lane).
5035    pub fn quantize_q8_1_into(&self, x: &CudaSlice<f32>, m: usize, in_f: usize,
5036                              q: &mut CudaSlice<i8>, d: &mut CudaSlice<f32>)
5037                              -> Result<(), Box<dyn std::error::Error>> {
5038        let nblk = in_f / 32;
5039        debug_assert!(q.len() >= m * in_f && d.len() >= m * nblk);
5040        let cfg = LaunchConfig::for_num_elems((m * in_f) as u32);
5041        let (inf, mi) = (in_f as i32, m as i32);
5042        if Self::pdl_on() && Self::pdl_wb_on() {
5043            use cudarc::driver::{DevicePtr, DevicePtrMut};
5044            let s = &self.gpu.stream();
5045            let (px, _g0) = x.device_ptr(s);
5046            let (pq, _g1) = q.device_ptr_mut(s); let (pd, _g2) = d.device_ptr_mut(s);
5047            let mut ps = [
5048                &px as *const _ as *mut std::ffi::c_void, &pq as *const _ as *mut _,
5049                &pd as *const _ as *mut _, &inf as *const _ as *mut _,
5050                &mi as *const _ as *mut _,
5051            ];
5052            unsafe { self.launch_pdl("quantize_q8_1", cfg.grid_dim, cfg.block_dim, &mut ps)?; }
5053            return Ok(());
5054        }
5055        let f = self.func("quantize_q8_1");
5056        let __s_b = self.gpu.stream();
5057        let mut b = __s_b.launch_builder(&f);
5058        b.arg(x).arg(&mut *q).arg(&mut *d).arg(&inf).arg(&mi);
5059        unsafe { b.launch(cfg)?; }
5060        Ok(())
5061    }
5062
5063    /// DECODE GLUE-FUSION LEVER: `res = a+b; z = rms_norm(res)*w` with z emitted as q8_1. `res` is
5064    /// still written (the post-ffn residual add reads it). Fuses add_rms_norm + quantize_q8_1.
5065    /// Returns (out_q, out_d) for matmul_pre. BIT-IDENTICAL. ncols % 32 == 0.
5066    pub fn add_rms_norm_q8_1(&self, a: &CudaSlice<f32>, b_in: &CudaSlice<f32>, w: &CudaSlice<f32>,
5067                             res: &mut CudaSlice<f32>, ncols: usize, nrows: usize, eps: f32)
5068                             -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
5069        let nblk = ncols / 32;
5070        let mut q = self.alloc_uninit::<i8>(nrows * ncols)?;
5071        let mut d = self.alloc_uninit::<f32>(nrows * nblk)?;
5072        let f = self.func("add_rms_norm_q8_1");
5073        // 1024 threads: same single-CTA-at-decode reasoning as rms_norm_q8_1.
5074        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (1024, 1, 1), shared_mem_bytes: 0 };
5075        let (nc, e) = (ncols as i32, eps);
5076        let __s_bld = self.gpu.stream();
5077        let mut bld = __s_bld.launch_builder(&f);
5078        bld.arg(a).arg(b_in).arg(w).arg(res).arg(&mut q).arg(&mut d).arg(&nc).arg(&e);
5079        unsafe { bld.launch(cfg)?; }
5080        Ok((q, d))
5081    }
5082
5083    /// RANK3 LEVER (add+rmsnorm fuse): `res = a + b; dst = rms_norm(res) * w` in ONE launch. Fuses
5084    /// e.add(a,b,res) + e.rms_norm(res,w,dst), removing one launch + one HBM read of the residual per
5085    /// residual+norm pair. BIT-IDENTICAL to the two-kernel sequence (same IEEE add, same reduction).
5086    pub fn add_rms_norm(&self, a: &CudaSlice<f32>, b: &CudaSlice<f32>, w: &CudaSlice<f32>,
5087                        res: &mut CudaSlice<f32>, dst: &mut CudaSlice<f32>, ncols: usize, nrows: usize,
5088                        eps: f32) -> Result<(), Box<dyn std::error::Error>> {
5089        let (nc, e) = (ncols as i32, eps);
5090        if Self::pdl_on() && Self::pdl_wb_on() {
5091            use cudarc::driver::{DevicePtr, DevicePtrMut};
5092            let s = &self.gpu.stream();
5093            let (pa, _g0) = a.device_ptr(s); let (pb, _g1) = b.device_ptr(s);
5094            let (pw, _g2) = w.device_ptr(s);
5095            let (pr, _g3) = res.device_ptr_mut(s); let (pd, _g4) = dst.device_ptr_mut(s);
5096            let mut ps = [
5097                &pa as *const _ as *mut std::ffi::c_void, &pb as *const _ as *mut _,
5098                &pw as *const _ as *mut _, &pr as *const _ as *mut _,
5099                &pd as *const _ as *mut _, &nc as *const _ as *mut _,
5100                &e as *const _ as *mut _,
5101            ];
5102            unsafe { self.launch_pdl("add_rms_norm_f32", (nrows as u32, 1, 1),
5103                                     (rms_block(), 1, 1), &mut ps)?; }
5104            return Ok(());
5105        }
5106        let f = self.func("add_rms_norm_f32");
5107        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
5108        let __s_b2 = self.gpu.stream();
5109        let mut b2 = __s_b2.launch_builder(&f);
5110        b2.arg(a).arg(b).arg(w).arg(&mut *res).arg(&mut *dst).arg(&nc).arg(&e);
5111        unsafe { b2.launch(cfg)?; }
5112        Ok(())
5113    }
5114
5115    /// E4B glue fusion: rms(a, wa) prologue + add_rms_norm — folds the post-attn norm into
5116    /// the tail entry (res = rms(a)*wa + b; dst = rms(res)*w).
5117    #[allow(clippy::too_many_arguments)]
5118    pub fn rms_pre_add_rms_norm(&self, a: &CudaSlice<f32>, wa: &CudaSlice<f32>,
5119                                b: &CudaSlice<f32>, w: &CudaSlice<f32>,
5120                                res: &mut CudaSlice<f32>, dst: &mut CudaSlice<f32>,
5121                                ncols: usize, nrows: usize, eps: f32)
5122                                -> Result<(), Box<dyn std::error::Error>> {
5123        let f = self.func("rms_pre_add_rms_norm_f32");
5124        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
5125        let (nc, e) = (ncols as i32, eps);
5126        let __s_b2 = self.gpu.stream();
5127        let mut b2 = __s_b2.launch_builder(&f);
5128        b2.arg(a).arg(wa).arg(b).arg(w).arg(&mut *res).arg(&mut *dst).arg(&nc).arg(&e);
5129        unsafe { b2.launch(cfg)?; }
5130        Ok(())
5131    }
5132
5133    /// wave-2 fold: rms(a,wa) + add + ffn-norm with zsh EMITTED q8_1 (fused2 consumes it).
5134    #[allow(clippy::too_many_arguments)]
5135    pub fn rms_pre_add_rms_norm_q8z(&self, a: &CudaSlice<f32>, wa: &CudaSlice<f32>,
5136                                    b: &CudaSlice<f32>, w: &CudaSlice<f32>,
5137                                    res: &mut CudaSlice<f32>, dst: &mut CudaSlice<f32>,
5138                                    ncols: usize, nrows: usize, eps: f32)
5139                                    -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
5140        debug_assert!(ncols % 128 == 0);
5141        let mut out_q = self.alloc_uninit::<i8>(nrows * ncols)?;
5142        let mut out_d = self.alloc_uninit::<f32>(nrows * (ncols / 32))?;
5143        let (nc, e) = (ncols as i32, eps);
5144        if Self::pdl_on() {
5145            {
5146            use cudarc::driver::{DevicePtr, DevicePtrMut};
5147            let s = &self.gpu.stream();
5148            let (pa, _g0) = a.device_ptr(s); let (pwa, _g1) = wa.device_ptr(s);
5149            let (pb, _g2) = b.device_ptr(s); let (pw, _g3) = w.device_ptr(s);
5150            let (pr, _g4) = res.device_ptr_mut(s); let (pdst, _g5) = dst.device_ptr_mut(s);
5151            let (pq, _g6) = out_q.device_ptr_mut(s); let (pd, _g7) = out_d.device_ptr_mut(s);
5152            let mut ps = [
5153                &pa as *const _ as *mut std::ffi::c_void, &pwa as *const _ as *mut _,
5154                &pb as *const _ as *mut _, &pw as *const _ as *mut _,
5155                &pr as *const _ as *mut _, &pdst as *const _ as *mut _,
5156                &pq as *const _ as *mut _, &pd as *const _ as *mut _,
5157                &nc as *const _ as *mut _, &e as *const _ as *mut _,
5158            ];
5159            unsafe { self.launch_pdl("rms_pre_add_rms_norm_q8z_f32", (nrows as u32, 1, 1),
5160                                     (rms_block(), 1, 1), &mut ps)?; }
5161            }
5162            return Ok((out_q, out_d));
5163        }
5164        let f = self.func("rms_pre_add_rms_norm_q8z_f32");
5165        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
5166        let __s_b2 = self.gpu.stream();
5167        let mut b2 = __s_b2.launch_builder(&f);
5168        b2.arg(a).arg(wa).arg(b).arg(w).arg(&mut *res).arg(&mut *dst)
5169          .arg(&mut out_q).arg(&mut out_d).arg(&nc).arg(&e);
5170        unsafe { b2.launch(cfg)?; }
5171        Ok((out_q, out_d))
5172    }
5173
5174    /// wave-4b: OUT-dim concat of three Q4_0 tensors (same in_features; rows are independent
5175    /// blocks, so the concat is a D2D byte concat of the GGUF-layout planes). Returns None
5176    /// off-class (non-Q4_0, mismatched widths, or any tensor already rp-swapped in place).
5177    pub fn build_q4_out_concat3(&self, w0: &crate::model::GpuTensor, w1: &crate::model::GpuTensor,
5178                                w2: &crate::model::GpuTensor)
5179                                -> Result<Option<crate::model::GpuTensor>, Box<dyn std::error::Error>> {
5180        use crate::model::GpuTensor;
5181        let part = |w: &GpuTensor| -> Option<(usize, usize)> {
5182            match w {
5183                GpuTensor::Quant { qtype, row_bytes, rp, .. }
5184                    if *qtype == QT_Q4_0 && !*rp => Some((*row_bytes, w.out_features())),
5185                _ => None,
5186            }
5187        };
5188        let (Some((rb0, o0)), Some((rb1, o1)), Some((rb2, o2))) = (part(w0), part(w1), part(w2))
5189        else { return Ok(None) };
5190        if rb0 != rb1 || rb0 != rb2
5191            || w0.in_features() != w1.in_features() || w0.in_features() != w2.in_features() {
5192            return Ok(None);
5193        }
5194        fn bytes_of(w: &crate::model::GpuTensor) -> &CudaSlice<u8> {
5195            match w { crate::model::GpuTensor::Quant { bytes, .. } => bytes, _ => unreachable!() }
5196        }
5197        let (b0, b1, b2) = (bytes_of(w0), bytes_of(w1), bytes_of(w2));
5198        let total = rb0 * (o0 + o1 + o2);
5199        let mut cat = self.alloc_u8(total)?;
5200        self.copy_u8_into(&mut cat, 0, b0, rb0 * o0)?;
5201        self.copy_u8_into(&mut cat, rb0 * o0, b1, rb1 * o1)?;
5202        self.copy_u8_into(&mut cat, rb0 * (o0 + o1), b2, rb2 * o2)?;
5203        Ok(Some(GpuTensor::Quant {
5204            bytes: cat, qtype: QT_Q4_0, row_bytes: rb0,
5205            ne: vec![w0.in_features() as u64, (o0 + o1 + o2) as u64], scale: 1.0, rp: false,
5206            #[cfg(memra_cutlass)]
5207            cutlass: None,
5208            fp8: None, blk: None, rp4: None, f16: None,
5209        }))
5210    }
5211
5212    /// wave-4b: the qkv-cat twin — one contiguous [rq+2*rk, hd] input from the concat matvec.
5213    #[allow(clippy::too_many_arguments)]
5214    pub fn rms_norm_qkv_rope_cat(&self, qkv: &CudaSlice<f32>,
5215                                 wq: &CudaSlice<f32>, wk: &CudaSlice<f32>, wv: &CudaSlice<f32>,
5216                                 q: &mut CudaSlice<f32>, k: &mut CudaSlice<f32>, v: &mut CudaSlice<f32>,
5217                                 head_dim: usize, rq: usize, rk: usize,
5218                                 pos: &CudaSlice<i32>, nh_q: usize, nh_k: usize,
5219                                 base: f32, freq_scale: f32, ff: Option<&CudaSlice<f32>>, eps: f32)
5220                                 -> Result<(), Box<dyn std::error::Error>> {
5221        let rows = rq + rk + rk;
5222        let theta_scale = base.powf(-2.0 / head_dim as f32);
5223        let (nc, rqi, rki, nhq, nhk) = (head_dim as i32, rq as i32, rk as i32, nh_q as i32, nh_k as i32);
5224        if Self::pdl_on() {
5225            use cudarc::driver::{DevicePtr, DevicePtrMut};
5226            let s = &self.gpu.stream();
5227            let (pqkv, _g0) = qkv.device_ptr(s);
5228            let (pwq, _g1) = wq.device_ptr(s); let (pwk, _g2) = wk.device_ptr(s);
5229            let (pwv, _g3) = wv.device_ptr(s);
5230            let (pq, _g4) = q.device_ptr_mut(s); let (pk, _g5) = k.device_ptr_mut(s);
5231            let (pv, _g6) = v.device_ptr_mut(s);
5232            let (ppos, _g7) = pos.device_ptr(s);
5233            let (pff, _g8) = match ff {
5234                Some(t) => { let (p, g) = t.device_ptr(s); (p, Some(g)) }
5235                None => (0, None),
5236            };
5237            let mut ps = [
5238                &pqkv as *const _ as *mut std::ffi::c_void,
5239                &pwq as *const _ as *mut _, &pwk as *const _ as *mut _,
5240                &pwv as *const _ as *mut _,
5241                &pq as *const _ as *mut _, &pk as *const _ as *mut _,
5242                &pv as *const _ as *mut _,
5243                &nc as *const _ as *mut _, &rqi as *const _ as *mut _,
5244                &rki as *const _ as *mut _, &ppos as *const _ as *mut _,
5245                &nhq as *const _ as *mut _, &nhk as *const _ as *mut _,
5246                &theta_scale as *const _ as *mut _, &freq_scale as *const _ as *mut _,
5247                &pff as *const _ as *mut _, &eps as *const _ as *mut _,
5248            ];
5249            unsafe { self.launch_pdl("rms_norm_qkv_rope_cat_f32", (rows as u32, 1, 1),
5250                                     (rms_block(), 1, 1), &mut ps)?; }
5251            return Ok(());
5252        }
5253        let f = self.func("rms_norm_qkv_rope_cat_f32");
5254        let cfg = LaunchConfig { grid_dim: (rows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
5255        let __s_b = self.gpu.stream();
5256        let mut b = __s_b.launch_builder(&f);
5257        match ff {
5258            Some(t) => { b.arg(qkv).arg(wq).arg(wk).arg(wv)
5259                          .arg(&mut *q).arg(&mut *k).arg(&mut *v)
5260                          .arg(&nc).arg(&rqi).arg(&rki).arg(pos).arg(&nhq).arg(&nhk)
5261                          .arg(&theta_scale).arg(&freq_scale).arg(t).arg(&eps);
5262                         unsafe { b.launch(cfg)?; } }
5263            None => { let null: u64 = 0;
5264                      b.arg(qkv).arg(wq).arg(wk).arg(wv)
5265                       .arg(&mut *q).arg(&mut *k).arg(&mut *v)
5266                       .arg(&nc).arg(&rqi).arg(&rki).arg(pos).arg(&nhq).arg(&nhk)
5267                       .arg(&theta_scale).arg(&freq_scale).arg(&null).arg(&eps);
5268                      unsafe { b.launch(cfg)?; } }
5269        }
5270        Ok(())
5271    }
5272
5273    /// wave-3 fold: rms_norm_qkv + rope_neox2 in ONE launch (n_dims == head_dim; ff nullable).
5274    #[allow(clippy::too_many_arguments)]
5275    pub fn rms_norm_qkv_rope(&self, q0: &CudaSlice<f32>, k0: &CudaSlice<f32>, v0: &CudaSlice<f32>,
5276                             wq: &CudaSlice<f32>, wk: &CudaSlice<f32>, wv: &CudaSlice<f32>,
5277                             q: &mut CudaSlice<f32>, k: &mut CudaSlice<f32>, v: &mut CudaSlice<f32>,
5278                             head_dim: usize, rq: usize, rk: usize,
5279                             pos: &CudaSlice<i32>, nh_q: usize, nh_k: usize,
5280                             base: f32, freq_scale: f32, ff: Option<&CudaSlice<f32>>, eps: f32)
5281                             -> Result<(), Box<dyn std::error::Error>> {
5282        let f = self.func("rms_norm_qkv_rope_f32");
5283        let rows = rq + rk + rk;   // q rows + k rows + v rows (rk == rv)
5284        let cfg = LaunchConfig { grid_dim: (rows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
5285        let theta_scale = base.powf(-2.0 / head_dim as f32);
5286        let (nc, rqi, rki, nhq, nhk) = (head_dim as i32, rq as i32, rk as i32, nh_q as i32, nh_k as i32);
5287        let __s_b = self.gpu.stream();
5288        let mut b = __s_b.launch_builder(&f);
5289        match ff {
5290            Some(t) => { b.arg(q0).arg(k0).arg(v0).arg(wq).arg(wk).arg(wv)
5291                          .arg(&mut *q).arg(&mut *k).arg(&mut *v)
5292                          .arg(&nc).arg(&rqi).arg(&rki).arg(pos).arg(&nhq).arg(&nhk)
5293                          .arg(&theta_scale).arg(&freq_scale).arg(t).arg(&eps);
5294                         unsafe { b.launch(cfg)?; } }
5295            None => { let null: u64 = 0;
5296                      b.arg(q0).arg(k0).arg(v0).arg(wq).arg(wk).arg(wv)
5297                       .arg(&mut *q).arg(&mut *k).arg(&mut *v)
5298                       .arg(&nc).arg(&rqi).arg(&rki).arg(pos).arg(&nhq).arg(&nhk)
5299                       .arg(&theta_scale).arg(&freq_scale).arg(&null).arg(&eps);
5300                      unsafe { b.launch(cfg)?; } }
5301        }
5302        Ok(())
5303    }
5304
5305    /// FUSED norm+rope+APPEND (m=1 decode, 2026-07-23): one launch replaces the
5306    /// rms_norm_qkv_rope + append_kv_quantized_dc pair. Kernel lives in the flash fatbins
5307    /// (format-flavored quant tail) — `g` must mirror the append path's flavor exactly.
5308    #[allow(clippy::too_many_arguments)]
5309    pub fn rms_norm_qkv_rope_append_dc(&self, q0: &CudaSlice<f32>, k0: &CudaSlice<f32>,
5310                             v0: &CudaSlice<f32>,
5311                             wq: &CudaSlice<f32>, wk: &CudaSlice<f32>, wv: &CudaSlice<f32>,
5312                             q: &mut CudaSlice<f32>, k: &mut CudaSlice<f32>, v: &mut CudaSlice<f32>,
5313                             head_dim: usize, rq: usize, rk: usize,
5314                             pos: &CudaSlice<i32>, nh_q: usize, nh_k: usize,
5315                             base: f32, freq_scale: f32, ff: Option<&CudaSlice<f32>>, eps: f32,
5316                             kc: &mut CudaSlice<u8>, vc: &mut CudaSlice<u8>,
5317                             t_dev: &CudaSlice<i32>, k_tok_bytes: usize, v_tok_bytes: usize,
5318                             g: bool)
5319                             -> Result<(), Box<dyn std::error::Error>> {
5320        let rows = rq + rk + rk;
5321        let theta_scale = base.powf(-2.0 / head_dim as f32);
5322        let (nc, rqi, rki, nhq, nhk) = (head_dim as i32, rq as i32, rk as i32, nh_q as i32, nh_k as i32);
5323        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
5324        if Self::pdl_on() && Self::pdl_wb_on() {
5325            use cudarc::driver::{DevicePtr, DevicePtrMut};
5326            let s = &self.gpu.stream();
5327            let (p0, _a0) = q0.device_ptr(s); let (p1, _a1) = k0.device_ptr(s);
5328            let (p2, _a2) = v0.device_ptr(s);
5329            let (pwq, _a3) = wq.device_ptr(s); let (pwk, _a4) = wk.device_ptr(s);
5330            let (pwv, _a5) = wv.device_ptr(s);
5331            let (pq, _a6) = q.device_ptr_mut(s); let (pk, _a7) = k.device_ptr_mut(s);
5332            let (pv, _a8) = v.device_ptr_mut(s);
5333            let (pp, _a9) = pos.device_ptr(s);
5334            let pff: u64 = match ff { Some(t) => { let (p, _gg) = t.device_ptr(s); p as u64 }
5335                                      None => 0 };
5336            let (pkc, _a10) = kc.device_ptr_mut(s); let (pvc, _a11) = vc.device_ptr_mut(s);
5337            let (pt, _a12) = t_dev.device_ptr(s);
5338            let mut ps = [
5339                &p0 as *const _ as *mut std::ffi::c_void, &p1 as *const _ as *mut _,
5340                &p2 as *const _ as *mut _, &pwq as *const _ as *mut _,
5341                &pwk as *const _ as *mut _, &pwv as *const _ as *mut _,
5342                &pq as *const _ as *mut _, &pk as *const _ as *mut _,
5343                &pv as *const _ as *mut _, &nc as *const _ as *mut _,
5344                &rqi as *const _ as *mut _, &rki as *const _ as *mut _,
5345                &pp as *const _ as *mut _, &nhq as *const _ as *mut _,
5346                &nhk as *const _ as *mut _, &theta_scale as *const _ as *mut _,
5347                &freq_scale as *const _ as *mut _, &pff as *const _ as *mut _,
5348                &eps as *const _ as *mut _, &pkc as *const _ as *mut _,
5349                &pvc as *const _ as *mut _, &pt as *const _ as *mut _,
5350                &ktb as *const _ as *mut _, &vtb as *const _ as *mut _,
5351            ];
5352            unsafe { self.launch_pdl_flash(g, "rms_norm_qkv_rope_append_dc_f32",
5353                                           (rows as u32, 1, 1), (rms_block(), 1, 1), 0, &mut ps)?; }
5354            return Ok(());
5355        }
5356        let f = if g { self.func_g("rms_norm_qkv_rope_append_dc_f32") }
5357                else { self.func("rms_norm_qkv_rope_append_dc_f32") };
5358        let cfg = LaunchConfig { grid_dim: (rows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
5359        let __s_b = self.gpu.stream();
5360        let mut b = __s_b.launch_builder(&f);
5361        match ff {
5362            Some(t) => { b.arg(q0).arg(k0).arg(v0).arg(wq).arg(wk).arg(wv)
5363                          .arg(&mut *q).arg(&mut *k).arg(&mut *v)
5364                          .arg(&nc).arg(&rqi).arg(&rki).arg(pos).arg(&nhq).arg(&nhk)
5365                          .arg(&theta_scale).arg(&freq_scale).arg(t).arg(&eps)
5366                          .arg(&mut *kc).arg(&mut *vc).arg(t_dev).arg(&ktb).arg(&vtb);
5367                         unsafe { b.launch(cfg)?; } }
5368            None => { let null: u64 = 0;
5369                      b.arg(q0).arg(k0).arg(v0).arg(wq).arg(wk).arg(wv)
5370                       .arg(&mut *q).arg(&mut *k).arg(&mut *v)
5371                       .arg(&nc).arg(&rqi).arg(&rki).arg(pos).arg(&nhq).arg(&nhk)
5372                       .arg(&theta_scale).arg(&freq_scale).arg(&null).arg(&eps)
5373                       .arg(&mut *kc).arg(&mut *vc).arg(t_dev).arg(&ktb).arg(&vtb);
5374                      unsafe { b.launch(cfg)?; } }
5375        }
5376        Ok(())
5377    }
5378
5379    /// wave-2 fold: a + b with the sum emitted q8_1 alongside f32.
5380    pub fn add_q8_1(&self, a: &CudaSlice<f32>, b: &CudaSlice<f32>, res: &mut CudaSlice<f32>,
5381                    ncols: usize, nrows: usize)
5382                    -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
5383        debug_assert!(ncols % 128 == 0);
5384        let mut out_q = self.alloc_uninit::<i8>(nrows * ncols)?;
5385        let mut out_d = self.alloc_uninit::<f32>(nrows * (ncols / 32))?;
5386        let f = self.func("add_q8_1_f32");
5387        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
5388        let nc = ncols as i32;
5389        let __s_b2 = self.gpu.stream();
5390        let mut b2 = __s_b2.launch_builder(&f);
5391        b2.arg(a).arg(b).arg(&mut *res).arg(&mut out_q).arg(&mut out_d).arg(&nc);
5392        unsafe { b2.launch(cfg)?; }
5393        Ok((out_q, out_d))
5394    }
5395
5396    /// E4B FFN-tail exit fusion (glue wave 5): resid = b + rms(a, wa) emitted f32 + q8_1 pair
5397    /// in ONE launch — replaces rms_norm(a,wa->sn) + add_q8_1(sn,b). Same rms_block() config
5398    /// as both parents (bit-identity: identical reduction + quad-walk quantize).
5399    pub fn rms_pre_add_q8_1(&self, a: &CudaSlice<f32>, wa: &CudaSlice<f32>, b: &CudaSlice<f32>,
5400                            res: &mut CudaSlice<f32>, ncols: usize, nrows: usize, eps: f32)
5401                            -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
5402        debug_assert!(ncols % 128 == 0);
5403        let mut out_q = self.alloc_uninit::<i8>(nrows * ncols)?;
5404        let mut out_d = self.alloc_uninit::<f32>(nrows * (ncols / 32))?;
5405        let f = self.func("rms_pre_add_q8_1_f32");
5406        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1),
5407                                 shared_mem_bytes: 0 };
5408        let (nc, ep) = (ncols as i32, eps);
5409        let __s_b2 = self.gpu.stream();
5410        let mut b2 = __s_b2.launch_builder(&f);
5411        b2.arg(a).arg(wa).arg(b).arg(&mut *res).arg(&mut out_q).arg(&mut out_d).arg(&nc).arg(&ep);
5412        unsafe { b2.launch(cfg)?; }
5413        Ok((out_q, out_d))
5414    }
5415
5416    /// L2 norm per row (head_dim), no weight.
5417    /// PREFILL l2 dispatch (round 27): the warp-per-row float4 v2 when the numeric-config
5418    /// seam allows (MEMRA_L2_V2, default ON, d_state==128 only); else the strided kernel.
5419    pub fn l2_v2_on(ncols: usize) -> bool {
5420        ncols == 128 && std::env::var("MEMRA_L2_V2").as_deref() != Ok("0")
5421    }
5422
5423    pub fn l2_norm_pp(&self, x: &CudaSlice<f32>, dst: &mut CudaSlice<f32>,
5424                      dst16: Option<&mut CudaSlice<u8>>, ncols: usize, nrows: usize,
5425                      eps: f32) -> Result<(), Box<dyn std::error::Error>> {
5426        if Self::l2_v2_on(ncols) {
5427            let f = self.func("l2_norm_pp_v2_f32");
5428            let rows_per_block = 8u32;   // 256 threads = 8 warps = 8 rows
5429            let cfg = LaunchConfig { grid_dim: ((nrows as u32).div_ceil(rows_per_block), 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
5430            let (nc, nr, e) = (ncols as i32, nrows as i32, eps);
5431            // mirror-fold: bf16 twin address by value (0 = skip; matches the nullable param)
5432            let d16: u64 = match dst16 { Some(d) => self.addr_u8(d), None => 0 };
5433            let __s_b = self.gpu.stream();
5434            let mut b = __s_b.launch_builder(&f);
5435            b.arg(x).arg(dst).arg(&d16).arg(&nc).arg(&nr).arg(&e);
5436            unsafe { b.launch(cfg)?; }
5437            return Ok(());
5438        }
5439        self.l2_norm(x, dst, ncols, nrows, eps)
5440    }
5441
5442    pub fn l2_norm(&self, x: &CudaSlice<f32>, dst: &mut CudaSlice<f32>, ncols: usize, nrows: usize,
5443                   eps: f32) -> Result<(), Box<dyn std::error::Error>> {
5444        let f = self.func("l2_norm_f32");
5445        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
5446        let (nc, e) = (ncols as i32, eps);
5447        let __s_b = self.gpu.stream();
5448        let mut b = __s_b.launch_builder(&f);
5449        b.arg(x).arg(dst).arg(&nc).arg(&e);
5450        unsafe { b.launch(cfg)?; }
5451        Ok(())
5452    }
5453
5454    /// L2-norm with blockDim=32 (warp-tree reduction) — BIT-IDENTICAL to gdn_prep_decode_f32's
5455    /// per-warp L2 norm. The verify path MUST use this to match decode's FP accumulation order:
5456    /// l2_norm at blockDim=256 produces a different shfl-tree reduction of the 128-element
5457    /// squared-sum (pairwise tree vs serial-4-then-warp-tree), causing ULP differences that
5458    /// propagate through gdn_scan and flip argmax on marginal logits.
5459    pub fn l2_norm_decode(&self, x: &CudaSlice<f32>, dst: &mut CudaSlice<f32>, ncols: usize,
5460                          nrows: usize, eps: f32) -> Result<(), Box<dyn std::error::Error>> {
5461        let f = self.func("l2_norm_f32");
5462        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
5463        let (nc, e) = (ncols as i32, eps);
5464        let __s_b = self.gpu.stream();
5465        let mut b = __s_b.launch_builder(&f);
5466        b.arg(x).arg(dst).arg(&nc).arg(&e);
5467        unsafe { b.launch(cfg)?; }
5468        Ok(())
5469    }
5470
5471    /// RoPE NEOX in-place. x:[head_dim, n_heads, n_tokens], pos:[n_tokens].
5472    pub fn rope_neox(&self, x: &mut CudaSlice<f32>, pos: &CudaSlice<i32>, head_dim: usize,
5473                     n_dims: usize, n_heads: usize, n_tokens: usize, freq_base: f32, freq_scale: f32)
5474                     -> Result<(), Box<dyn std::error::Error>> {
5475        let f = self.func("rope_neox_f32");
5476        let theta_scale = (freq_base).powf(-2.0 / n_dims as f32);
5477        let grid = (n_heads * n_tokens) as u32;
5478        let cfg = LaunchConfig { grid_dim: (grid, 1, 1), block_dim: ((head_dim / 2) as u32, 1, 1), shared_mem_bytes: 0 };
5479        let (hd, nd, nh) = (head_dim as i32, n_dims as i32, n_heads as i32);
5480        let __s_b = self.gpu.stream();
5481        let mut b = __s_b.launch_builder(&f);
5482        b.arg(x).arg(pos).arg(&hd).arg(&nd).arg(&nh).arg(&theta_scale).arg(&freq_scale);
5483        unsafe { b.launch(cfg)?; }
5484        Ok(())
5485    }
5486
5487    /// RoPE NEOX with per-dim freq factors (gemma4 global layers, rope_freqs.weight [n_dims/2]).
5488    pub fn rope_neox_ff(&self, x: &mut CudaSlice<f32>, pos: &CudaSlice<i32>, head_dim: usize,
5489                        n_dims: usize, n_heads: usize, n_tokens: usize, freq_base: f32,
5490                        freq_scale: f32, ff: &CudaSlice<f32>)
5491                        -> Result<(), Box<dyn std::error::Error>> {
5492        let f = self.func("rope_neox_ff_f32");
5493        let theta_scale = (freq_base).powf(-2.0 / n_dims as f32);
5494        let grid = (n_heads * n_tokens) as u32;
5495        let cfg = LaunchConfig { grid_dim: (grid, 1, 1), block_dim: ((head_dim / 2) as u32, 1, 1), shared_mem_bytes: 0 };
5496        let (hd, nd, nh) = (head_dim as i32, n_dims as i32, n_heads as i32);
5497        let __s_b = self.gpu.stream();
5498        let mut b = __s_b.launch_builder(&f);
5499        b.arg(x).arg(pos).arg(&hd).arg(&nd).arg(&nh).arg(&theta_scale).arg(&freq_scale).arg(ff);
5500        unsafe { b.launch(cfg)?; }
5501        Ok(())
5502    }
5503
5504    /// gemma4: rope q and k in one launch (per-row chain = rope_neox / rope_neox_ff verbatim).
5505    #[allow(clippy::too_many_arguments)]
5506    pub fn rope_neox2(&self, q: &mut CudaSlice<f32>, k: &mut CudaSlice<f32>,
5507                      pos: &CudaSlice<i32>, head_dim: usize, n_dims: usize,
5508                      nh_q: usize, nh_k: usize, n_tokens: usize, freq_base: f32,
5509                      freq_scale: f32, ff: Option<&CudaSlice<f32>>)
5510                      -> Result<(), Box<dyn std::error::Error>> {
5511        let f = self.func("rope_neox2_f32");
5512        let theta_scale = (freq_base).powf(-2.0 / n_dims as f32);
5513        let grid = ((nh_q + nh_k) * n_tokens) as u32;
5514        let cfg = LaunchConfig { grid_dim: (grid, 1, 1), block_dim: ((head_dim / 2) as u32, 1, 1), shared_mem_bytes: 0 };
5515        let (hd, nd, nq, nk, nt) = (head_dim as i32, n_dims as i32, nh_q as i32, nh_k as i32, n_tokens as i32);
5516        let __s_b = self.gpu.stream();
5517        let mut b = __s_b.launch_builder(&f);
5518        b.arg(q).arg(k).arg(pos).arg(&hd).arg(&nd).arg(&nq).arg(&nk).arg(&nt)
5519         .arg(&theta_scale).arg(&freq_scale);
5520        match ff {
5521            Some(ffv) => { b.arg(ffv); unsafe { b.launch(cfg)?; } }
5522            None => {
5523                let null: u64 = 0;
5524                b.arg(&null);
5525                unsafe { b.launch(cfg)?; }
5526            }
5527        }
5528        Ok(())
5529    }
5530
5531    /// gemma4 R1: dst = GELU_tanh(gate) * up.
5532    pub fn gelu_tanh_mul(&self, gate: &CudaSlice<f32>, up: &CudaSlice<f32>, dst: &mut CudaSlice<f32>, n: usize)
5533                         -> Result<(), Box<dyn std::error::Error>> {
5534        let f = self.func("gelu_tanh_mul_f32");
5535        let cfg = LaunchConfig::for_num_elems(n as u32);
5536        let ni = n as i32;
5537        let __s_b = self.gpu.stream();
5538        let mut b = __s_b.launch_builder(&f);
5539        b.arg(gate).arg(up).arg(dst).arg(&ni);
5540        unsafe { b.launch(cfg)?; }
5541        Ok(())
5542    }
5543
5544    pub fn silu_mul(&self, gate: &CudaSlice<f32>, up: &CudaSlice<f32>, dst: &mut CudaSlice<f32>, n: usize)
5545                    -> Result<(), Box<dyn std::error::Error>> {
5546        let f = self.func("silu_mul_f32");
5547        // float4 kernel: one thread per 4 elements (tail handled in-kernel)
5548        let cfg = LaunchConfig::for_num_elems((n as u32).div_ceil(4));
5549        let ni = n as i32;
5550        let __s_b = self.gpu.stream();
5551        let mut b = __s_b.launch_builder(&f);
5552        b.arg(gate).arg(up).arg(dst).arg(&ni);
5553        unsafe { b.launch(cfg)?; }
5554        Ok(())
5555    }
5556
5557    /// f16out twin of `silu_mul` (task #17): the epilogue also emits the fp16 GEMM operand
5558    /// for the down projection — kills the standalone convert pass. Bit-identical class.
5559    pub fn silu_mul_f16out(&self, gate: &CudaSlice<f32>, up: &CudaSlice<f32>,
5560                           dst: &mut CudaSlice<f32>, dst16: &mut CudaSlice<u8>, n: usize)
5561                           -> Result<(), Box<dyn std::error::Error>> {
5562        let f = self.func("silu_mul_f16out_f32");
5563        let cfg = LaunchConfig::for_num_elems((n as u32).div_ceil(4));
5564        let ni = n as i32;
5565        let __s_b = self.gpu.stream();
5566        let mut b = __s_b.launch_builder(&f);
5567        b.arg(gate).arg(up).arg(dst).arg(dst16).arg(&ni);
5568        unsafe { b.launch(cfg)?; }
5569        Ok(())
5570    }
5571
5572    /// FFN SwiGLU epilogue fusion (RANK3 LEVER 2): `dst = silu(gate*gs) * (up*us)` in ONE launch,
5573    /// folding the per-tensor NVFP4 macro-scale (`gs`,`us`) that would otherwise be two separate
5574    /// `scale_inplace` launches on the gate/up matmul outputs. BIT-IDENTICAL to
5575    /// scale_inplace(gate,gs); scale_inplace(up,us); silu_mul(gate,up,dst) — identical float ops in
5576    /// identical order. For non-NVFP4 weights gs==us==1.0 -> identical to `silu_mul`. Net: -2
5577    /// launches per dense FFN layer (the gate+up post-matmul scales).
5578    pub fn silu_mul_scaled(&self, gate: &CudaSlice<f32>, up: &CudaSlice<f32>, gs: f32, us: f32,
5579                           dst: &mut CudaSlice<f32>, n: usize) -> Result<(), Box<dyn std::error::Error>> {
5580        let f = self.func("silu_mul_scaled_f32");
5581        let cfg = LaunchConfig::for_num_elems(n as u32);
5582        let ni = n as i32;
5583        let (gsf, usf) = (gs, us);
5584        let __s_b = self.gpu.stream();
5585        let mut b = __s_b.launch_builder(&f);
5586        b.arg(gate).arg(up).arg(&gsf).arg(&usf).arg(dst).arg(&ni);
5587        unsafe { b.launch(cfg)?; }
5588        Ok(())
5589    }
5590
5591    /// swigluoai (MiniMax-M3 / GPT-OSS): clamped SwiGLU epilogue, math 1:1 vs llama.cpp
5592    /// ggml_cuda_op_swiglu_oai_single. `dst = swish_alpha(clamp(gate*gs)) * (1 + clamp(up*us))`.
5593    /// gs/us fold the NVFP4 macro-scales exactly like `silu_mul_scaled`.
5594    #[allow(clippy::too_many_arguments)]
5595    pub fn swigluoai_mul_scaled(&self, gate: &CudaSlice<f32>, up: &CudaSlice<f32>, gs: f32, us: f32,
5596                                alpha: f32, limit: f32, dst: &mut CudaSlice<f32>, n: usize)
5597                                -> Result<(), Box<dyn std::error::Error>> {
5598        let f = self.func("swigluoai_mul_scaled_f32");
5599        let cfg = LaunchConfig::for_num_elems(n as u32);
5600        let ni = n as i32;
5601        let __s_b = self.gpu.stream();
5602        let mut b = __s_b.launch_builder(&f);
5603        b.arg(gate).arg(up).arg(&gs).arg(&us).arg(&alpha).arg(&limit).arg(dst).arg(&ni);
5604        unsafe { b.launch(cfg)?; }
5605        Ok(())
5606    }
5607
5608    /// RANK2 LEVER (q8_1 quant-fold): SwiGLU epilogue that EMITS the q8_1 quantization of `act`
5609    /// directly (aq int8 [n] + ad f32 [n/32]), so ffn_down's standalone `quantize_q8_1` launch is
5610    /// removed — the down-proj activation has one consumer, so the quant folds into the producer for
5611    /// free (no extra HBM read; no f32 `act` write). gs/us fold the gate/up NVFP4 macro-scales like
5612    /// `silu_mul_scaled`. BIT-IDENTICAL q8_1 to silu_mul_scaled(...) then quantize_q8_1(...). Only
5613    /// valid when ffn_down uses the q8_1 dp4a/mmvq path; the caller checks `uses_q8_1_fast(ffn_down)`.
5614    /// n must be a multiple of 32 (n_ff always is).
5615    pub fn silu_mul_scaled_q8_1(&self, gate: &CudaSlice<f32>, up: &CudaSlice<f32>, gs: f32, us: f32,
5616                                n: usize)
5617                                -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
5618        let f = self.func("silu_mul_scaled_q8_1");
5619        let nblk = n / 32;
5620        let mut aq = self.alloc_uninit::<i8>(n)?;       // full-overwrite output
5621        let mut ad = self.alloc_uninit::<f32>(nblk)?;   // full-overwrite output
5622        // WARP-PER-BLOCK kernel: one warp (32 lanes) per 32-block -> n threads total.
5623        let cfg = LaunchConfig::for_num_elems(n as u32);
5624        let (gsf, usf, ni) = (gs, us, n as i32);
5625        let __s_b = self.gpu.stream();
5626        let mut b = __s_b.launch_builder(&f);
5627        b.arg(gate).arg(up).arg(&gsf).arg(&usf).arg(&mut aq).arg(&mut ad).arg(&ni);
5628        unsafe { b.launch(cfg)?; }
5629        Ok((aq, ad))
5630    }
5631
5632    pub fn add(&self, a: &CudaSlice<f32>, b_in: &CudaSlice<f32>, dst: &mut CudaSlice<f32>, n: usize)
5633               -> Result<(), Box<dyn std::error::Error>> {
5634        let f = self.func("add_f32");
5635        // float4 kernel: one thread per 4 elements (tail handled in-kernel)
5636        let cfg = LaunchConfig::for_num_elems((n as u32).div_ceil(4));
5637        let ni = n as i32;
5638        let __s_bld = self.gpu.stream();
5639        let mut bld = __s_bld.launch_builder(&f);
5640        bld.arg(a).arg(b_in).arg(dst).arg(&ni);
5641        unsafe { bld.launch(cfg)?; }
5642        Ok(())
5643    }
5644
5645    pub fn mul(&self, a: &CudaSlice<f32>, b_in: &CudaSlice<f32>, dst: &mut CudaSlice<f32>, n: usize)
5646               -> Result<(), Box<dyn std::error::Error>> {
5647        let f = self.func("mul_f32");
5648        let cfg = LaunchConfig::for_num_elems(n as u32);
5649        let ni = n as i32;
5650        let __s_bld = self.gpu.stream();
5651        let mut bld = __s_bld.launch_builder(&f);
5652        bld.arg(a).arg(b_in).arg(dst).arg(&ni);
5653        unsafe { bld.launch(cfg)?; }
5654        Ok(())
5655    }
5656
5657    /// Unified weight-tensor matmul: dispatches quant tensors to qmatvec (weights packed) and
5658    /// float tensors to cuBLASLt. y[m,out] = x[m,in] @ W[out,in]^T.
5659    pub fn matmul(&self, w: &crate::model::GpuTensor, x: &CudaSlice<f32>, m: usize)
5660                  -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
5661        use crate::model::GpuTensor;
5662        let in_f = w.in_features();
5663        let out_f = w.out_features();
5664        // PREFILL (T>1) ROOT FIX: batched tensor-core int8 GEMM. Decodes each weight tile to int8
5665        // in smem ONCE and reuses across all tokens via mma — vs the dp4a matvec's per-token weight
5666        // re-read. Only the 4 daily-hot dtypes; m=1 decode keeps dp4a (it's bandwidth-bound, mma
5667        // gives nothing). Quantize the activation once here then call the GEMM.
5668        // m cutoff FIXED at 16: the m=4 MMA-verify A/B (2026-07-06, was MEMRA_GEMM_M) measured
5669        // NEGATIVE — the MMA tile grid starves at m=4 (BN=256 -> grid.y=1) and its FP order
5670        // shifted verify argmax at tight margins. Do not lower without re-running that battery.
5671        #[allow(non_snake_case)]
5672        // VERIFY-EXACT scope pushes the GEMM crossover out of reach (usize::MAX) — the
5673        // t>=16 dflash verify must ride the decode-exact batched class (parity law).
5674        let GEMM_M_THRESHOLD = if self.verify_exact_on() { usize::MAX } else { 16usize };
5675
5676        // PREFILL GEMM (m>=16). ACCURACY-FIRST dispatch (2026-06-28, prefill-gemm-beat-research wf
5677        // wllbyo6vc step 1): the int8 W4A8 GEMM (qmatvec_gemm, q8_1 activation, s32 accumulate) is
5678        // ACCURATE (prefill logit maxdiff 0.159, < dp4a 0.55) and the default. The FP4 W4A4 mxf4 path
5679        // (try_fp4_gemm) quantizes the ACTIVATION to e2m1 4-bit (8 magnitude levels) -> maxdiff 1.0
5680        // when combined — a real accuracy loss, NOT a math bug. So FP4-W4A4 is taken ONLY under the
5681        // explicit MEMRA_FP4 opt-in AND it must come SECOND (int8 W4A8 is the correct default for NVFP4).
5682        // The workflow plan rebuilds the FP4 path (kill per-K repack, widen K, deepen pipeline, TMA) to
5683        // be both fast AND accurate; until then NVFP4 prefill defaults to the accurate int8 GEMM.
5684        // TINY-OUT_F GUARD (2026-06-28, ncu trace): the tiling GEMM's grid is (ceil(out_f/BM=64),
5685        // ceil(m/BN=256)). For tiny out_f (ssm_beta/ssm_alpha out_f=num_v_heads~32), grid.x=1 -> only
5686        // ceil(m/256) CTAs (e.g. 2 for m=512) on 82 SMs = 0.39% SM throughput, 852us EACH (measured
5687        // worst offender). The dp4a path grids (out_f, m) = far more CTAs, filling the GPU. So route
5688        // out_f < 2*BM to dp4a (skip the tiling GEMM which structurally can't fill the SMs here).
5689        const GEMM_MIN_OUT_F: usize = 128;   // 2*BM; below this the GEMM grid.x starves the 82 SMs
5690        // VENDORED llama MMQ prefill GEMMs. NVFP4 W4A8 is DEFAULT-ON (2026-07-05 flip: same int8
5691        // accuracy class as the int8 GEMM below at ~1.9x pp512, rp-loader coexists with the A6
5692        // repack; MEMRA_MMQ_W4A8=0 = escape hatch). W4A4 mxf4nvf4 + Q4_K/Q5_K stay behind MEMRA_MMQ=1.
5693        // The env policy lives in mmq_supports/qmatvec_mmq. Feeds raw f32 activation `x` (the
5694        // launcher quantizes internally). out_f>=MMQ_Y/2 keeps the tile grid from starving the SMs.
5695        // FP8-ACT PREFILL (MEMRA_PP_FP8=1, probe verdict 2026-07-08): F8-E4M3-origin projections
5696        // carry their raw e4m3 device bytes (the `fp8` operand stashed at load next to the Q8_0
5697        // re-encode) — cuBLASLt FP8 TN at 620-795 TF vs 47-72 TF for this class's int8 GEMM.
5698        // Weight side EXACT (checkpoint bytes); activation rides ONE per-batch e4m3 scale
5699        // (amax/448) folded with weight_scale in-GEMM. Prefill only; decode keeps Q8_0 untouched.
5700        if m >= GEMM_M_THRESHOLD {
5701            if let Some(y) = self.try_fp8_gemm(w, x, m)? { return Ok(y); }
5702            // PER-BLOCK FP8 MMQ (lane/fp8-mmq): the block-128 class try_fp8_gemm skips (cuBLASLt
5703            // takes no block grid on sm_120). Exact per block — the checkpoint's e4m3 bytes and its
5704            // f32 grid go into the tile unchanged. TWO SOURCES, TWO DEFAULTS: the load-time stash is
5705            // opt-in (MEMRA_FP8_MMQ=1), the native-resident QT_F8_E4M3_BLK grid is DEFAULT ON
5706            // (MEMRA_FP8_MMQ=0 reverts it to dequant-per-call) — see fp8_ffi.rs for why the same
5707            // tile defaults differently by operand source.
5708            if let Some(y) = self.try_fp8_blk_mmq(w, x, m)? { return Ok(y); }
5709            // FP16-mirror prefill (MEMRA_PP_F16=1, probe 2026-07-26: 3.2-3.7x the MMQ class).
5710            // Mirror presence IS the gate (only built under the env). Decode never reaches here.
5711            if let Some(y) = self.try_f16_gemm(w, x, m)? { return Ok(y); }
5712        }
5713        // F8-E4M3 BLOCK-128 (QT_F8_E4M3_BLK, lane/fp8-blk128-decode). TWO arms, split at the SAME
5714        // m threshold the rest of this method uses:
5715        //   * m >= threshold (prefill): dequant-per-call to the ARM B' Q8_0 slab and recurse, so
5716        //     prefill keeps the floor's kernels AND the floor's bits (try_e4m3_blk_prefill).
5717        //   * m <  threshold: the native per-block GEMV — m=1 decode and the m=2..15 verify tiers.
5718        //     grid.y=m runs the exact m=1 program per (token,row), so the decode-parity law holds
5719        //     across every tier by construction with no batched twin needed.
5720        //
5721        // NOT gated on `fast`: this dtype has no dp4a twin and no Stage-A f32-dequant oracle (the
5722        // generic `deq()` switch has no block-scale input), exactly as QT_F8_E4M3 has none, so
5723        // MEMRA_FAST=0 cannot route it anywhere else. Placed before every GEMM/MMQ arm below
5724        // because gemm_supports/mmq_supports/mmvq_supports all deliberately REFUSE this qtype —
5725        // reaching the generic tail would panic rather than produce wrong numbers, and this pair of
5726        // arms is what makes sure it never gets there.
5727        if let GpuTensor::Quant { qtype, .. } = w {
5728            if *qtype == QT_F8_E4M3_BLK {
5729                if m >= GEMM_M_THRESHOLD {
5730                    if let Some(y) = self.try_e4m3_blk_prefill(w, x, m)? { return Ok(y); }
5731                }
5732                let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
5733                if let Some(y) = self.try_e4m3_blk_pre(w, &aq, &ad, m)? { return Ok(y); }
5734            }
5735        }
5736        if m >= GEMM_M_THRESHOLD && out_f >= GEMM_MIN_OUT_F && self.mmq_supports(w) {
5737            return self.qmatvec_mmq(w, x, m);
5738        }
5739        if m >= GEMM_M_THRESHOLD && out_f >= GEMM_MIN_OUT_F && self.gemm_supports(w) {
5740            let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
5741            return self.qmatvec_gemm(w, &aq, &ad, m);
5742        }
5743        // FP4 W4A4 only as an explicit speed/accuracy tradeoff opt-in, and only if the int8 GEMM
5744        // above didn't already handle this weight (e.g. NVFP4 with in_f%64!=0, or MEMRA_NO_GEMM set).
5745        if m >= GEMM_M_THRESHOLD {
5746            if let Some(y) = self.try_fp4_gemm(w, x, m, in_f, out_f)? { return Ok(y); }
5747        }
5748        // Stage-B fast int8 dp4a is the DEFAULT since 2026-07-08 (it has been the daily path
5749        // for weeks; the old opt-in flag was a silent-slow-path landmine). MEMRA_FAST=0 reverts
5750        // to Stage-A f32-dequant (the correctness oracle path).
5751        let fast = std::env::var("MEMRA_FAST").as_deref() != Ok("0");
5752        // PERF-3 decode-GEMV: m=1 warp-per-row MMVQ (MEMRA_MMVQ). The big decode matvecs reach
5753        // `matmul` directly (ffn_down, lm_head output, wo), so route them here too — not only the
5754        // matmul_pre siblings. qmatvec_mmvq_raw quantizes the activation internally (q8_1) like the
5755        // _fast paths; the NVFP4 macro-scale is applied by the `scale != 1.0` block below.
5756        if m == 1 && fast {
5757            if let GpuTensor::Quant { bytes, qtype, row_bytes, rp, rp4, scale, .. } = w {
5758                if self.mmvq_supports(*qtype) {
5759                    // NVFP4 macro-scale rides the kernel's fused epilogue arg (one launch total);
5760                    // non-NVFP4 has scale==1.0 so qmatvec_mmvq skips scale_inplace either way.
5761                    // Q4_0 split-plane mirror (rp4): the decode arm reads it via the _rp twins.
5762                    let (bytes, rp) = match rp4 { Some(m4) => (m4, true), None => (bytes, *rp) };
5763                    let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
5764                    return self.qmatvec_mmvq(bytes, &aq, &ad, m, in_f, out_f, *qtype, *row_bytes, *scale, rp);
5765                }
5766            }
5767        }
5768        // BATCHED weight-resident matvec for the m=2-4 band (the MTP/verify forward's ffn_down, wo, and
5769        // lm_head `output` reach `matmul` directly at m=T=2..4). Walks the weight ONCE, dp4a vs all m
5770        // activation columns -> 1 weight read for m tokens (vs grid.y=m re-reading m times below). Quant
5771        // the activation once here (q8_1) like the _fast paths; macro-scale applied via the scale!=1.0
5772        // block below. MEMRA_NO_BATCHED -> per-m path.
5773        //
5774        // DECODE-PARITY GATE (2026-07-07, the 9B synth K=3/4/6 spec FAIL root cause): the batched
5775        // kernels are bit-identical per (token,row) to MMVQ's 32-thread warp reduce, NOT to the
5776        // dp4a kernels' 128-thread two-level reduce. Without MEMRA_MMVQ the m=1 decode chain rides
5777        // dp4a, so a verify riding batched here has a DIFFERENT FP order than the decode it must
5778        // match bit-for-bit — greedy spec flips at tight-margin tokens (the old HANDOVER "ENV LAW:
5779        // FAST+MMVQ both required" footgun, closed here). Parity law: the m>1 kernel CLASS must be
5780        // a pure function of (dtype, env) equal to the m=1 class — batched iff MMVQ. Without MMVQ
5781        // the verify falls to the per-m grid.y=m dp4a path below (each column = the exact m=1
5782        // dp4a program). MEMRA_MMVQ=1 (the daily config) is dispatch-unchanged.
5783        if (2..=16).contains(&m) && fast && std::env::var("MEMRA_NO_BATCHED").is_err()
5784            && (m <= 4 || Self::b8_enabled()) {
5785            // b16 tier (2026-07-11, spec K>7): Q4_0/Q6_K have base+_rp b16 kernels; Q8_0's
5786            // b16 exists only as the split-plane _rp twin, so it joins iff the q8rp mirror
5787            // is present (rp4) — the mirror pick below then routes to the _rp family.
5788            // QT_F8_E4M3 joins unconditionally (lane/rp-on-st): its b16 IS the base kernel,
5789            // because the native e4m3 row layout is already aligned and needs no mirror.
5790            // NVFP4/Q4_K/Q8_0 all join unconditionally now (lane/rp-on-st): each has base + _rp
5791            // b16 twins, so either residency layout has its aligned form at this width. Q8_0's
5792            // old `rp4.is_some()` precondition is GONE — the mirror is a bandwidth lever, not the
5793            // exact tier's admission ticket (it was refusing FP8-ST over 23.9 MiB of ssm_beta).
5794            let m_ok = m <= 8 || matches!(w, GpuTensor::Quant { qtype, .. }
5795                if *qtype == QT_Q4_0 || *qtype == QT_Q6_K || *qtype == QT_F8_E4M3
5796                    || *qtype == QT_NVFP4 || *qtype == QT_Q4_K || *qtype == QT_Q5_K || *qtype == QT_Q8_0);
5797            if m_ok {
5798            if let GpuTensor::Quant { bytes, qtype, row_bytes, rp, rp4, .. } = w {
5799                if self.batched_supports(*qtype) && self.mmvq_supports(*qtype) {
5800                    let (bytes, rp) = match rp4 { Some(m4) => (m4, true), None => (bytes, *rp) };
5801                    let mcols = Self::batched_mcols(m);
5802                    let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
5803                    let mut y = self.qmatvec_mmvq_batched(bytes, &aq, &ad, m, in_f, out_f, *qtype, *row_bytes, mcols, 1.0, rp)?;
5804                    if let GpuTensor::Quant { scale, .. } = w {
5805                        if *scale != 1.0 { self.scale_inplace(&mut y, *scale, m * out_f)?; }
5806                    }
5807                    return Ok(y);
5808                }
5809            }
5810        }
5811        }
5812        // F8-E4M3 (MEMRA_ST_E4M3) catch-all for the m<16 band the arms above didn't take (m=9..15,
5813        // the K=8 verify tier; or m=2..8 under MEMRA_NO_BATCHED/MEMRA_B8=0): grid.y=m e4m3 mmvq —
5814        // the SAME per-(token,row) program as the m=1 decode launch (bit-identical by construction),
5815        // weight re-read m times (rare tier; exactness over bandwidth here). There is no _dp4a twin
5816        // for this dtype, so the generic match below must never see it under `fast`.
5817        if fast {
5818            if let GpuTensor::Quant { bytes, qtype, row_bytes, scale, .. } = w {
5819                if *qtype == QT_F8_E4M3 {
5820                    let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
5821                    return self.qmatvec_mmvq(bytes, &aq, &ad, m, in_f, out_f, *qtype, *row_bytes,
5822                                             *scale, false);
5823                }
5824            }
5825        }
5826        let mut y = match w {
5827            GpuTensor::Quant { bytes, qtype, row_bytes, .. } if fast && *qtype == QT_Q8_0 =>
5828                self.qmatvec_q8_0_fast(bytes, x, m, in_f, out_f, *row_bytes)?,
5829            GpuTensor::Quant { bytes, qtype, row_bytes, .. } if fast && *qtype == QT_Q4_K =>
5830                self.qmatvec_q4_K_fast(bytes, x, m, in_f, out_f, *row_bytes)?,
5831            GpuTensor::Quant { bytes, qtype, row_bytes, .. } if fast && *qtype == QT_Q6_K =>
5832                self.qmatvec_q6_K_fast(bytes, x, m, in_f, out_f, *row_bytes)?,
5833            GpuTensor::Quant { bytes, qtype, row_bytes, .. } if fast && *qtype == QT_Q5_K =>
5834                self.qmatvec_q5_K_fast(bytes, x, m, in_f, out_f, *row_bytes)?,
5835            GpuTensor::Quant { bytes, qtype, row_bytes, .. } if fast && *qtype == QT_Q3_K =>
5836                self.qmatvec_q3_K_fast(bytes, x, m, in_f, out_f, *row_bytes)?,
5837            GpuTensor::Quant { bytes, qtype, row_bytes, rp, .. } if fast && *qtype == QT_NVFP4 =>
5838                self.qmatvec_dp4a_named(
5839                    if *rp { "qmatvec_nvfp4_dp4a_rp" } else { "qmatvec_nvfp4_dp4a" },
5840                    bytes, x, m, in_f, out_f, *row_bytes)?,
5841            // IQ4_XS trunk fast path — DEFAULT ON since 2026-08-02 (MEMRA_IQ_FAST=0 reverts to
5842            // Stage-A; see iq_fast_enabled). The old opt-in default was the KAT-Coder decode
5843            // anomaly (research/kat-anomaly-20260802/).
5844            GpuTensor::Quant { bytes, qtype, row_bytes, .. }
5845                if fast && *qtype == QT_IQ4_XS && Self::iq_fast_enabled() =>
5846                self.qmatvec_iq4_XS_fast(bytes, x, m, in_f, out_f, *row_bytes)?,
5847            // B3: IQ3_S uses the Stage-A f32 dequant-in-kernel path. There is NO
5848            // qmatvec_iq3_s_dp4a kernel — do NOT add a `*qtype == QT_IQ3_S` fast guard here
5849            // without first writing the matching kernel, or func() will panic
5850            // "kernel ... not in any fatbin".
5851            GpuTensor::Quant { bytes, qtype, row_bytes, rp, .. } =>
5852                // Stage-A generic: repacked NVFP4 uses the device-side split-plane tag (the
5853                // deq(row,j) form cannot address the planes; same value/product order).
5854                self.qmatvec(bytes, x, m, in_f, out_f,
5855                             if *rp && *qtype == QT_NVFP4 { QT_NVFP4_RP } else { *qtype },
5856                             *row_bytes)?,
5857            GpuTensor::Float { data, .. } => self.linear(x, data, m, in_f, out_f)?,
5858            // MEMRA_FULL_PREC bf16-resident weight: dequant-on-use to f32 scratch, then the same
5859            // cuBLASLt f32 GEMV as the Float arm.
5860            GpuTensor::FloatBf16 { data, .. } =>
5861                self.linear_bf16_chunked(x, data, m, in_f, out_f, false)?,
5862        };
5863        // NVFP4 per-tensor macro-scale (post-matmul). scale==1.0 for all other quants/float -> no-op.
5864        if let GpuTensor::Quant { scale, .. } = w {
5865            if *scale != 1.0 { self.scale_inplace(&mut y, *scale, m * out_f)?; }
5866        }
5867        Ok(y)
5868    }
5869
5870    /// True if `w` would take the int8-dp4a fast path under MEMRA_FAST (so its activation can be
5871    /// pre-quantized once and shared across sibling matmuls via `matmul_pre`).
5872    pub fn uses_q8_1_fast(&self, w: &crate::model::GpuTensor) -> bool {
5873        use crate::model::GpuTensor;
5874        if std::env::var("MEMRA_FAST").as_deref() == Ok("0") { return false; }
5875        match w {
5876            // QT_F8_E4M3_BLK is admitted for the same reason QT_F8_E4M3 is: its ONLY kernel class
5877            // takes the shared q8_1 activation, so callers may pre-quantize once and share it
5878            // across siblings. It is NOT admitted to any of the fused/dual epilogue doors those
5879            // siblings can then open (`q8_fused_params`, `e4m3_fused_params` and
5880            // `matmul_pre_dual_noscale` all match on their own qtype and refuse this one) — the
5881            // block class has no fused twin yet, so each of its projections takes its own launch.
5882            GpuTensor::Quant { qtype, .. } => matches!(*qtype,
5883                QT_Q8_0 | QT_Q4_K | QT_Q6_K | QT_Q5_K | QT_Q3_K | QT_NVFP4 | QT_F8_E4M3
5884                | QT_F8_E4M3_BLK | QT_Q4_0)
5885                || (*qtype == QT_IQ4_XS && Self::iq_fast_enabled()),
5886            GpuTensor::Float { .. } | GpuTensor::FloatBf16 { .. } => false,
5887        }
5888    }
5889
5890    /// matmul with a PRE-QUANTIZED q8_1 activation (aq,ad from `quantize_q8_1`). Skips the
5891    /// per-matmul re-quantize so sibling matmuls that share an input (gate+up share `z`;
5892    /// q/k/v + wqkv/gate/beta/alpha share `h`) quantize ONCE. Caller MUST have checked
5893    /// `uses_q8_1_fast(w)`; falls back to plain `matmul` otherwise (Stage-A / Float / non-fast).
5894    pub fn matmul_pre(&self, w: &crate::model::GpuTensor, aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
5895                      x_fallback: &CudaSlice<f32>, m: usize)
5896                      -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
5897        use crate::model::GpuTensor;
5898        // Every raw-f32 arm below (fp8/f16/MMQ/fp4) reads m*in_f from x_fallback. Callers that
5899        // pre-quantized and dropped the f32 input pass an EMPTY x_fallback (E4B's fusion port:
5900        // h = zeros(0)) — the length guard keeps those on the aq/ad GEMM instead of feeding a
5901        // 0-byte buffer to a convert kernel (illegal address -> cublasLt status 13; the E4B
5902        // rc=30013 dig, 2026-07-31).
5903        let x_raw_ok = x_fallback.len() >= m * w.in_features();
5904        // FP8-ACT PREFILL (MEMRA_PP_FP8=1): same arm as `matmul` — the fp8 operand needs the RAW
5905        // f32 activation (per-batch e4m3 quant differs from q8_1), so x_fallback not aq/ad.
5906        if m >= 16 && x_raw_ok && !self.verify_exact_on() {
5907            if let Some(y) = self.try_fp8_gemm(w, x_fallback, m)? { return Ok(y); }
5908            // PER-BLOCK FP8 MMQ — same arm as `matmul` (stash opt-in, native-resident default ON);
5909            // its own quantizer wants the RAW f32 activation, so x_fallback not aq/ad.
5910            if let Some(y) = self.try_fp8_blk_mmq(w, x_fallback, m)? { return Ok(y); }
5911            // FP16-mirror prefill (same arm as `matmul` — fp16 wants the RAW f32 activation).
5912            if let Some(y) = self.try_f16_gemm(w, x_fallback, m)? { return Ok(y); }
5913        }
5914        // BLOCK-128 e4m3 (QT_F8_E4M3_BLK) — the same two arms as `matmul`, split at the same m, and
5915        // placed at the same point in the order (after the prefill GEMM hooks, before every arm
5916        // that refuses this qtype). The prefill arm needs the RAW f32 activation for the Q8_0
5917        // dispatch it recurses into, so it takes x_fallback and is skipped when that is empty
5918        // (a pre-quantized caller that dropped its f32 input never runs at prefill m anyway).
5919        if m >= 16 && x_raw_ok && !self.verify_exact_on() {
5920            if let Some(y) = self.try_e4m3_blk_prefill(w, x_fallback, m)? { return Ok(y); }
5921        }
5922        if let Some(y) = self.try_e4m3_blk_pre(w, aq, ad, m)? { return Ok(y); }
5923        // VENDORED llama MMQ prefill GEMMs (NVFP4 W4A8 default-on; W4A4/k-quant behind MEMRA_MMQ=1
5924        // — policy in mmq_supports) — use the RAW f32 activation (their own internal quant:
5925        // q8_1 D4 for NVFP4 W4A8, FP8/UE4M3 for W4A4, q8_1 DS4 for Q4_K/Q5_K), so x_fallback not
5926        // aq/ad.
5927        if m >= 16 && w.out_features() >= 128 && self.mmq_supports(w) && !self.verify_exact_on()
5928            && x_raw_ok {
5929            return self.qmatvec_mmq(w, x_fallback, m);
5930        }
5931        // Stage-C FP4 prefill (MEMRA_FP4): native mxf4 GEMM needs the f32 activation (FP4-quant differs
5932        // from q8_1), so re-quantize from x_fallback rather than reuse aq/ad. NVFP4 only, m>=16.
5933        if m >= 16 && x_raw_ok && !self.verify_exact_on() {
5934            if let Some(y) = self.try_fp4_gemm(w, x_fallback, m, w.in_features(), w.out_features())? {
5935                return Ok(y);
5936            }
5937        }
5938        // Prefill GEMM root fix: if T>1 and the dtype has a GEMM kernel, batch via tensor cores
5939        // (reuses the already-quantized aq/ad — no extra quantize). m=1 falls through to dp4a.
5940        if m >= 16 && self.gemm_supports(w) && !self.verify_exact_on() {
5941            return self.qmatvec_gemm(w, aq, ad, m);
5942        }
5943        if !self.uses_q8_1_fast(w) { return self.matmul(w, x_fallback, m); }
5944        let in_f = w.in_features();
5945        let out_f = w.out_features();
5946        let (bytes, qtype, row_bytes, scale, rp) = match w {
5947            GpuTensor::Quant { bytes, qtype, row_bytes, scale, rp, .. } => (bytes, *qtype, *row_bytes, *scale, *rp),
5948            _ => unreachable!("uses_q8_1_fast guaranteed Quant"),
5949        };
5950        // Q4_0 split-plane mirror: only the mmvq/batched decode arms read it (the _rp twins);
5951        // the dp4a/oracle tails below keep the raw GGUF bytes.
5952        let (mbytes, mrp) = match w {
5953            GpuTensor::Quant { rp4: Some(m4), .. } => (m4, true),
5954            _ => (bytes, rp),
5955        };
5956        // PERF-3 decode-GEMV: warp-per-row MMVQ for the m=1 decode arm, gated behind MEMRA_MMVQ.
5957        // Only the 4 daily-hot dtypes have an _mmvq kernel (Q8_0/Q4_K/Q6_K/NVFP4); Q5_K/Q3_K/IQ4_XS
5958        // keep _dp4a (the oracle/fallback). Bit-equivalent to _dp4a up to f32 reduction order.
5959        if m == 1 && self.mmvq_supports(qtype) {
5960            return self.qmatvec_mmvq(mbytes, aq, ad, m, in_f, out_f, qtype, row_bytes, scale, mrp);
5961        }
5962        // BATCHED weight-resident matvec for the m=2-4 band (the MTP/verify forward: full_attn_verify
5963        // and decode_step_t run their projections at m=T=k=2..4). The plain _dp4a path below launches
5964        // grid.y=m INDEPENDENT blocks per output row -> the weight row is re-read m times from HBM/L2.
5965        // The _b2/_b4 kernels walk the weight ONCE and dp4a vs all m activation columns, so m tokens
5966        // cost ~1 weight read instead of m (decode is weight-BW-bound). BIT-IDENTICAL per (token,row)
5967        // to the _mmvq path (32-thread warp reduce — NOT the dp4a 128-thread reduce below).
5968        // m=2 -> mcols=2; m∈{3,4} -> mcols=4; m∈{5..8} -> mcols=8 (kernel guards c>=m).
5969        // MEMRA_NO_BATCHED forces the per-m grid.y=m path (the A/B reference); MEMRA_B8=0 keeps
5970        // m=5..8 on the old per-m path (b8-tier-only seam).
5971        // DECODE-PARITY GATE (2026-07-07): batched iff mmvq_supports — see matmul's parity note.
5972        // Without MEMRA_MMVQ, m=1 decode rides dp4a (the arm below at m=1); the verify must ride
5973        // the SAME class per column (grid.y=m dp4a = the exact m=1 dp4a program per column).
5974        if (2..=16).contains(&m) && self.batched_supports(qtype) && self.mmvq_supports(qtype)
5975            && std::env::var("MEMRA_NO_BATCHED").is_err()
5976            && (m <= 4 || Self::b8_enabled())
5977            // b16 tier: every class routed here now has base + _rp b16 kernels (Q4_0/Q6_K
5978            // pre-existing; NVFP4/Q4_K/Q8_0-base/F8_E4M3 added lane/rp-on-st 2026-08-06), so
5979            // there is no mirror precondition left — `mrp` still selects the LAYOUT below.
5980            && (m <= 8 || qtype == QT_Q4_0 || qtype == QT_Q6_K || qtype == QT_NVFP4
5981                || qtype == QT_Q4_K || qtype == QT_Q5_K || qtype == QT_F8_E4M3 || qtype == QT_Q8_0) {
5982            let mcols = Self::batched_mcols(m);
5983            return self.qmatvec_mmvq_batched(mbytes, aq, ad, m, in_f, out_f, qtype, row_bytes, mcols, scale, mrp);
5984        }
5985        // F8-E4M3 catch-all (m=9..15 / batched-disabled seams): grid.y=m e4m3 mmvq — this dtype
5986        // has NO _dp4a twin, and per (token,row) the mmvq body is the exact m=1 decode program.
5987        // Q4_0 joins the catch-all (2026-07-11): adaptive-K cap 8 makes verify t=9 reachable
5988        // for the first time (past the b8 tier) and Q4_0 has no dp4a twin either. The mirror
5989        // (mbytes/mrp) keeps the rp layout consistent with the m=1 decode program.
5990        if qtype == QT_F8_E4M3 || qtype == QT_Q4_0 {
5991            let (b2, r2) = if qtype == QT_Q4_0 { (mbytes, mrp) } else { (bytes, rp) };
5992            return self.qmatvec_mmvq(b2, aq, ad, m, in_f, out_f, qtype, row_bytes, scale, r2);
5993        }
5994        let name = match qtype {
5995            QT_Q8_0 => "qmatvec_q8_0_dp4a", QT_Q4_K => "qmatvec_q4_K_dp4a",
5996            QT_Q6_K => "qmatvec_q6_K_dp4a", QT_Q5_K => "qmatvec_q5_K_dp4a",
5997            QT_Q3_K => "qmatvec_q3_K_dp4a",
5998            QT_NVFP4 => if rp { "qmatvec_nvfp4_dp4a_rp" } else { "qmatvec_nvfp4_dp4a" },
5999            QT_IQ4_XS => "qmatvec_iq4_XS_dp4a",
6000            _ => unreachable!(),
6001        };
6002        let f = self.func(name);
6003        let mut y = self.alloc_uninit::<f32>(m * out_f)?;  // full-overwrite GEMM output: skip memset
6004        let cfg = LaunchConfig { grid_dim: (out_f as u32, m as u32, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
6005        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
6006        let __s_b = self.gpu.stream();
6007        let mut b = __s_b.launch_builder(&f);
6008        b.arg(bytes).arg(aq).arg(ad).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
6009        unsafe { b.launch(cfg)?; }
6010        if scale != 1.0 { self.scale_inplace(&mut y, scale, m * out_f)?; }
6011        Ok(y)
6012    }
6013
6014    /// DECODE-EXACT matmul at any m: guarantees the SAME warp-per-row (MMVQ, 32-thread) FP
6015    /// accumulation order as the T=1 decode path for EVERY token row. The spec-decode verify MUST
6016    /// use this for linear-attn projections to be bit-identical to greedy decode. The dp4a kernel
6017    /// (128 threads, two-level reduction) used by `matmul`/`matmul_pre` at m>=5 has a different
6018    /// shfl-tree shape that produces ULP differences propagating through gdn_scan into argmax flips.
6019    /// The MMVQ kernel with grid.y=m already processes each row independently (same 32-thread warp
6020    /// reduce as m=1); this method just forces that path unconditionally.
6021    pub fn matmul_decode_exact(&self, w: &crate::model::GpuTensor, x: &CudaSlice<f32>, m: usize)
6022                               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6023        use crate::model::GpuTensor;
6024        // FLOAT tensors (35B ssm_beta/ssm_alpha on every linear layer, F32 ne=[2048,32]): the
6025        // generic path is cuBLASLt, whose reduction splits are n-DEPENDENT — m=1 vs m=2 col-0
6026        // outputs differ in every bit (probe 2026-07-06: 32/32 bit-diff, maxdiff 3.5e-3), which
6027        // shifted 35B verify logits 0.26-0.56 vs eager and flipped greedy at tight margins (the
6028        // p3 spec FAIL). Decode-exact contract: per-COLUMN m=1 cuBLASLt calls — each column's
6029        // reduction is the exact kernel the T=1 decode path runs, so verify==decode bit-for-bit.
6030        // m<=10 here (K+2 verify tier), so the extra launches are a handful of 4us gemvs.
6031        if let GpuTensor::Float { data, .. } = w {
6032            return self.linear_decode_exact(x, data, m, w.in_features(), w.out_features());
6033        }
6034        // MEMRA_FULL_PREC bf16-resident weight: dequant-on-use, then the per-column decode-exact
6035        // float linear (same n-independent reduction contract as the Float arm above).
6036        if let GpuTensor::FloatBf16 { data, .. } = w {
6037            let (in_f, out_f) = (w.in_features(), w.out_features());
6038            return self.linear_bf16_chunked(x, data, m, in_f, out_f, true);
6039        }
6040        if !self.uses_q8_1_fast(w) { return self.matmul(w, x, m); }
6041        let in_f = w.in_features();
6042        let out_f = w.out_features();
6043        let (bytes, qtype, row_bytes, scale, rp) = match w {
6044            GpuTensor::Quant { bytes, qtype, row_bytes, scale, rp, .. } => (bytes, *qtype, *row_bytes, *scale, *rp),
6045            _ => return self.matmul(w, x, m),
6046        };
6047        // Q4_0 split-plane mirror for the mmvq/batched arms below (dp4a tail = matmul_pre,
6048        // which does its own mirror pick).
6049        let (bytes, rp) = match w {
6050            GpuTensor::Quant { rp4: Some(m4), .. } => (m4, true),
6051            _ => (bytes, rp),
6052        };
6053        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
6054        // BLOCK-128 e4m3 (QT_F8_E4M3_BLK): the same single kernel every other entry dispatches, so
6055        // the decode-exact contract needs nothing special — grid.y=m runs the m=1 program per
6056        // (token,row) by construction, which is exactly what this method exists to guarantee.
6057        if let Some(y) = self.try_e4m3_blk_pre(w, &aq, &ad, m)? { return Ok(y); }
6058        // Batched weight-resident matvec for m=2-8: BIT-IDENTICAL per (token,row) to MMVQ (exact
6059        // integer dp4a, same warp reduce — kernel-check gate rel=0.00e0), one weight read for m
6060        // tokens. The dispatch the divergence fix must avoid is dp4a's 128-thread two-level
6061        // reduce, NOT this. m=5..8 is the K=4..7 spec-verify tier (b8): pre-b8 T=5 fell to the
6062        // grid.y=m per-row MMVQ below = 5 full weight reads/launch — the measured 27B K=4 cliff.
6063        // DECODE-PARITY GATE (2026-07-07): batched (MMVQ-class order) only when the m=1 decode
6064        // chain rides MMVQ too — without MEMRA_MMVQ decode is dp4a, so the exact-contract here
6065        // must be per-column dp4a (matmul_pre fallthrough), not the MMVQ order.
6066        if (2..=16).contains(&m) && self.batched_supports(qtype) && self.mmvq_supports(qtype)
6067            && std::env::var("MEMRA_NO_BATCHED").is_err()
6068            && (m <= 4 || Self::b8_enabled())
6069            // Every b16 class has base + _rp twins after lane/rp-on-st (see matmul_pre's note):
6070            // no mirror precondition, `rp` selects the layout only.
6071            && (m <= 8 || qtype == QT_Q4_0 || qtype == QT_Q6_K || qtype == QT_F8_E4M3
6072                || qtype == QT_NVFP4 || qtype == QT_Q4_K || qtype == QT_Q5_K || qtype == QT_Q8_0) {
6073            let mcols = Self::batched_mcols(m);
6074            return self.qmatvec_mmvq_batched(bytes, &aq, &ad, m, in_f, out_f, qtype, row_bytes, mcols, scale, rp);
6075        }
6076        if self.mmvq_supports(qtype) {
6077            // MMVQ at grid.y=m: each row is processed by its own warp independently — same 32-thread
6078            // accumulation + warp_reduce_sum as m=1 decode. Bit-identical per row.
6079            return self.qmatvec_mmvq(bytes, &aq, &ad, m, in_f, out_f, qtype, row_bytes, scale, rp);
6080        }
6081        // Fallback for non-MMVQ quant types (Q5_K, Q3_K): use dp4a (the only available kernel).
6082        // These types are not used in the 27B's linear-attn NVFP4+Q4_K layers.
6083        self.matmul_pre(w, &aq, &ad, x, m)
6084    }
6085
6086    /// DECODE-EXACT matmul from a PRE-QUANTIZED q8_1 activation (batched-verify epilogue
6087    /// re-fuse, lane/vt-fixes fix 2, 2026-08-03): the EXACT `matmul_decode_exact` dispatch for
6088    /// q8_1-fast Quant tensors, with the caller's (aq, ad) replacing the internal
6089    /// `quantize_q8_1`. quantize_q8_1 is deterministic (same input bytes -> same q8 bytes), so
6090    /// sharing one quantize across sibling matmuls of the same activation — or consuming the
6091    /// q8 emitted by a fused epilogue (rms_norm_q8_1 / add_rms_norm_q8_1 /
6092    /// silu_mul_scaled_q8_1 / gated_rmsnorm_q8_1, all kernel-check-pinned bit-identical to
6093    /// their unfused chains) — cannot change any dispatched kernel's input bytes.
6094    /// Caller MUST guarantee `uses_q8_1_fast(w)` (the fused epilogues only exist on that path).
6095    pub fn matmul_decode_exact_pre(&self, w: &crate::model::GpuTensor, aq: &CudaSlice<i8>,
6096                                   ad: &CudaSlice<f32>, m: usize)
6097                                   -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
6098        use crate::model::GpuTensor;
6099        debug_assert!(self.uses_q8_1_fast(w),
6100                      "matmul_decode_exact_pre: caller must guarantee q8_1-fast");
6101        // BLOCK-128 e4m3: same single kernel, all m — see matmul_decode_exact's note.
6102        if let Some(y) = self.try_e4m3_blk_pre(w, aq, ad, m)? { return Ok(y); }
6103        let in_f = w.in_features();
6104        let out_f = w.out_features();
6105        let (bytes, qtype, row_bytes, scale, rp) = match w {
6106            GpuTensor::Quant { bytes, qtype, row_bytes, scale, rp, .. } =>
6107                (bytes, *qtype, *row_bytes, *scale, *rp),
6108            _ => return Err("matmul_decode_exact_pre: Quant tensor required (q8_1-fast contract)".into()),
6109        };
6110        // Q4_0 split-plane mirror — same pick as matmul_decode_exact.
6111        let (bytes, rp) = match w {
6112            GpuTensor::Quant { rp4: Some(m4), .. } => (m4, true),
6113            _ => (bytes, rp),
6114        };
6115        // Dispatch mirror of matmul_decode_exact's q8_1-fast tail, condition for condition.
6116        if (2..=16).contains(&m) && self.batched_supports(qtype) && self.mmvq_supports(qtype)
6117            && std::env::var("MEMRA_NO_BATCHED").is_err()
6118            && (m <= 4 || Self::b8_enabled())
6119            && (m <= 8 || qtype == QT_Q4_0 || qtype == QT_Q6_K || qtype == QT_F8_E4M3
6120                || qtype == QT_NVFP4 || qtype == QT_Q4_K || qtype == QT_Q5_K || qtype == QT_Q8_0) {
6121            let mcols = Self::batched_mcols(m);
6122            return self.qmatvec_mmvq_batched(bytes, aq, ad, m, in_f, out_f, qtype, row_bytes, mcols, scale, rp);
6123        }
6124        if self.mmvq_supports(qtype) {
6125            return self.qmatvec_mmvq(bytes, aq, ad, m, in_f, out_f, qtype, row_bytes, scale, rp);
6126        }
6127        // Non-MMVQ quant types (Q5_K/Q3_K under MEMRA_MMVQ=0): dp4a via matmul_pre — the same
6128        // fallback matmul_decode_exact takes. m <= 16 on the verify tier never reads x_fallback.
6129        let x0 = self.zeros(0)?;
6130        self.matmul_pre(w, aq, ad, &x0, m)
6131    }
6132
6133    /// DUAL gate+up batched matvec from a PRE-QUANTIZED activation, macro-scales DEFERRED
6134    /// (lane/vt-fixes fix 2): same eligibility as `matmul_decode_exact_dual`, but the caller's
6135    /// (aq, ad) replaces the internal quantize and the NVFP4 per-tensor scales are RETURNED
6136    /// instead of applied via two `scale_inplace` launches — the fused SwiGLU epilogue
6137    /// (`silu_mul_scaled_q8_1`) folds them, exactly like the m=1 decode chain does. Deferring
6138    /// is value-exact: `y[i]*s` inline in the epilogue is the same IEEE multiply scale_inplace
6139    /// would store (f32 store/load round-trips are exact). None -> caller falls back to the
6140    /// per-tensor path.
6141    pub fn matmul_decode_exact_dual_pre(&self, w0: &crate::model::GpuTensor,
6142                                        w1: &crate::model::GpuTensor,
6143                                        aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, m: usize)
6144        -> Result<Option<((CudaSlice<f32>, f32), (CudaSlice<f32>, f32))>, Box<dyn std::error::Error>> {
6145        use crate::model::GpuTensor;
6146        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
6147        let on = *ON.get_or_init(|| {
6148            std::env::var("MEMRA_SPEC_DUAL_T").map(|v| v != "0").unwrap_or(true)
6149        });
6150        if !on || !(2..=7).contains(&m) || std::env::var("MEMRA_NO_BATCHED").is_ok()
6151            || !self.uses_q8_1_fast(w0) || !self.uses_q8_1_fast(w1) {
6152            return Ok(None);
6153        }
6154        // DECODE-PARITY GATE (lane/nvfp4-strict, 2026-08-05): batched iff MMVQ — the dual
6155        // kernels are the MMVQ warp-reduce family, and without MEMRA_MMVQ the m=1 decode
6156        // chain this verify must match bit-for-bit rides dp4a (see matmul_decode_exact's
6157        // note). The singles enforce this via `mmvq_supports`; the dual door skipped it.
6158        if !self.mmvq_supports(QT_NVFP4) { return Ok(None); }
6159        let (in_f, out_f) = (w0.in_features(), w0.out_features());
6160        if w1.in_features() != in_f || w1.out_features() != out_f {
6161            return Ok(None);
6162        }
6163        let (b0, b1, row_bytes, s0, s1, rp) = match (w0, w1) {
6164            (GpuTensor::Quant { bytes: b0, qtype: q0, row_bytes: rb0, scale: s0, rp: rp0, rp4: None, .. },
6165             GpuTensor::Quant { bytes: b1, qtype: q1, row_bytes: rb1, scale: s1, rp: rp1, rp4: None, .. })
6166                if *q0 == QT_NVFP4 && *q1 == QT_NVFP4 && rb0 == rb1 && rp0 == rp1 =>
6167                (b0, b1, *rb0, *s0, *s1, *rp0),
6168            _ => return Ok(None),
6169        };
6170        // m=5..7: only the exact-width rp duals exist (vt-fixes fix 1b); GGUF layout keeps
6171        // the singles. The b8 dual (MCOLS=8 at m=5..8) measured FLAT and stays dead.
6172        if m > 4 && !(rp && Self::b8_enabled()
6173            && std::env::var("MEMRA_B567").as_deref() != Ok("0")) {
6174            return Ok(None);
6175        }
6176        let (y0, y1) = self.qmatvec_batched_dual_raw(b0, b1, aq, ad, m, in_f, out_f, row_bytes, rp)?;
6177        Ok(Some(((y0, s0), (y1, s1))))
6178    }
6179
6180    /// DUAL gate+up BATCHED matvec at verify t=2..8 (lane/verify-economics, 2026-08-02): ONE
6181    /// launch computes both FFN projections of a verify batch — same activation, same shape,
6182    /// blockIdx.y selects the tensor. Per (tensor, token, row) the kernel body is the single
6183    /// batched program on the SAME layout (split-plane rp: b2 rp / b4 rpr2 / b8 rpr2; GGUF:
6184    /// b2 base / b4 r2 / b8 r2) -> BIT-IDENTICAL to the two single `matmul_decode_exact`
6185    /// launches (kernel-check gates bitwise on both layouts; run-spec K=1..8 arbitrates e2e).
6186    /// The one activation quantize replaces two IDENTICAL quantizes of the same `x` (same
6187    /// kernel, same input -> same q8_1 bytes), and the two independent weight streams in one
6188    /// grid restore the memory-level parallelism the two-launch form loses to tail drain +
6189    /// launch gap (m=1 dual_mr2 precedent: DRAM 40% -> 47-50% on the 27B pair).
6190    /// `Some((y0, y1))` only when both tensors are NVFP4, the SAME layout (both rp or both
6191    /// GGUF, no rp4 mirror), identical (in_f, out_f, row_bytes), q8_1-fast, and m in 2..=4
6192    /// (the b2/b4 tiers = verify T for K=1..3, the profitable-K window — the b8 dual measured
6193    /// FLAT vs the rpsc singles x3 interleaved, research/verify-economics-20260802, and was
6194    /// killed per doctrine). None -> caller runs the two singles. MEMRA_SPEC_DUAL_T=0 rollback.
6195    pub fn matmul_decode_exact_dual(&self, w0: &crate::model::GpuTensor, w1: &crate::model::GpuTensor,
6196                                    x: &CudaSlice<f32>, m: usize)
6197        -> Result<Option<(CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
6198        use crate::model::GpuTensor;
6199        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
6200        let on = *ON.get_or_init(|| {
6201            std::env::var("MEMRA_SPEC_DUAL_T").map(|v| v != "0").unwrap_or(true)
6202        });
6203        if !on || !(2..=4).contains(&m) || std::env::var("MEMRA_NO_BATCHED").is_ok()
6204            || !self.uses_q8_1_fast(w0) || !self.uses_q8_1_fast(w1) {
6205            return Ok(None);
6206        }
6207        // DECODE-PARITY GATE (lane/nvfp4-strict, 2026-08-05): batched iff MMVQ — same law as
6208        // the singles' `batched_supports && mmvq_supports` check in matmul_decode_exact,
6209        // which this dual door bypassed. Without MEMRA_MMVQ the m=1 decode is dp4a; the
6210        // verify must ride the per-column dp4a class, not the MMVQ-family dual.
6211        if !self.mmvq_supports(QT_NVFP4) { return Ok(None); }
6212        let (in_f, out_f) = (w0.in_features(), w0.out_features());
6213        if w1.in_features() != in_f || w1.out_features() != out_f {
6214            return Ok(None);
6215        }
6216        let (b0, b1, row_bytes, s0, s1, rp) = match (w0, w1) {
6217            (GpuTensor::Quant { bytes: b0, qtype: q0, row_bytes: rb0, scale: s0, rp: rp0, rp4: None, .. },
6218             GpuTensor::Quant { bytes: b1, qtype: q1, row_bytes: rb1, scale: s1, rp: rp1, rp4: None, .. })
6219                if *q0 == QT_NVFP4 && *q1 == QT_NVFP4 && rb0 == rb1 && rp0 == rp1 =>
6220                (b0, b1, *rb0, *s0, *s1, *rp0),
6221            _ => return Ok(None),
6222        };
6223        // Engagement receipt (MEMRA_DEBUG=1): the first dead-arm A/B lesson — a `rp: false`
6224        // gate silently no-op'd the whole experiment; prove the arm is live in the log.
6225        if std::env::var("MEMRA_DEBUG").is_ok() {
6226            static ONCE: std::sync::Once = std::sync::Once::new();
6227            ONCE.call_once(|| eprintln!("[memra] dual gate+up batched ENGAGED (m={m} rp={rp})"));
6228        }
6229        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
6230        let (y0, y1) = self.qmatvec_batched_dual_raw(b0, b1, &aq, &ad, m, in_f, out_f, row_bytes, rp)?;
6231        let mut y0 = y0;
6232        let mut y1 = y1;
6233        if s0 != 1.0 { self.scale_inplace(&mut y0, s0, m * out_f)?; }
6234        if s1 != 1.0 { self.scale_inplace(&mut y1, s1, m * out_f)?; }
6235        Ok(Some((y0, y1)))
6236    }
6237
6238    /// Launch body of the dual batched twins from raw NVFP4 weight bytes + a pre-quantized q8_1
6239    /// activation (kernel-check's bit-equivalence entry; matmul_decode_exact_dual's core).
6240    /// mcols tier = batched_mcols(m); macro-scale NOT applied. `rp` selects the split-plane
6241    /// twins (both buffers must be the repacked layout).
6242    #[allow(clippy::too_many_arguments)]
6243    pub fn qmatvec_batched_dual_raw(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>,
6244                                    aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
6245                                    m: usize, in_f: usize, out_f: usize, row_bytes: usize, rp: bool)
6246        -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6247        const ROWS_PER_BLOCK: u32 = 4;
6248        let mcols = Self::batched_mcols(m);
6249        // EXACT-WIDTH duals at m=5..7 (vt-fixes fix 1b): rp-only; bit-identical to the two
6250        // b5/b6/b7 singles (blockIdx.y selects the tensor, same template body).
6251        let tiny_rp1 = rp && mcols == 4 && out_f <= 128
6252            && std::env::var("MEMRA_NVFP4_AUX_DUAL").as_deref() != Ok("0");
6253        let (name, rows_per_block) = if tiny_rp1 {
6254            ("qmatvec_nvfp4_mmvq_dual_b4_rp", ROWS_PER_BLOCK)
6255        } else { match (mcols, rp, m) {
6256            (2, false, _) => ("qmatvec_nvfp4_mmvq_dual_b2", ROWS_PER_BLOCK),
6257            (4, false, _) => ("qmatvec_nvfp4_mmvq_dual_b4_r2", ROWS_PER_BLOCK * 2),
6258            (2, true, _) => ("qmatvec_nvfp4_mmvq_dual_b2_rp", ROWS_PER_BLOCK),
6259            (4, true, _) => ("qmatvec_nvfp4_mmvq_dual_b4_rpr2", ROWS_PER_BLOCK * 2),
6260            (8, true, 5) => ("qmatvec_nvfp4_mmvq_dual_b5_rpr2", ROWS_PER_BLOCK * 2),
6261            (8, true, 6) => ("qmatvec_nvfp4_mmvq_dual_b6_rpr2", ROWS_PER_BLOCK * 2),
6262            (8, true, 7) => ("qmatvec_nvfp4_mmvq_dual_b7_rpr2", ROWS_PER_BLOCK * 2),
6263            _ => return Err(format!("qmatvec_batched_dual_raw: no dual kernel for m {m}").into()),
6264        }};
6265        let f = self.func(name);
6266        let mut y0 = self.alloc_uninit::<f32>(m * out_f)?;
6267        let mut y1 = self.alloc_uninit::<f32>(m * out_f)?;
6268        let cfg = LaunchConfig {
6269            grid_dim: ((out_f as u32 + rows_per_block - 1) / rows_per_block, 2, 1),
6270            block_dim: (32, ROWS_PER_BLOCK, 1),
6271            shared_mem_bytes: 0,
6272        };
6273        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
6274        let __s_b = self.gpu.stream();
6275        let mut b = __s_b.launch_builder(&f);
6276        b.arg(b0).arg(b1).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1)
6277            .arg(&inf).arg(&outf).arg(&mi).arg(&rb);
6278        unsafe { b.launch(cfg)?; }
6279        Ok((y0, y1))
6280    }
6281
6282    /// Like `matmul_pre` but RETURNS THE RAW (un-macro-scaled) matmul output together with the
6283    /// per-tensor NVFP4 scale, instead of applying `scale_inplace` internally. Used by the fused
6284    /// SwiGLU epilogue (RANK3 LEVER 2) so the gate/up scales fold into one `silu_mul_scaled` launch.
6285    /// `Some((y_raw, scale))` only on the m==1 decode fast path (mmvq / dp4a) where the scale is a
6286    /// separate post-launch op we can defer; returns `None` for every other path (prefill GEMM, FP4
6287    /// GEMM, Stage-A, Float) so the caller falls back to the scaled `matmul_pre` + `silu_mul`.
6288    /// DUAL gate+up NVFP4 matvec (mm-fusion): ONE launch computes both projections (same
6289    /// activation, same shape) — grid.y selects the tensor. Bit-identical per element to two
6290    /// mr2 launches at m=1. Returns (gate_raw, up_raw) un-scaled (caller folds the two macro
6291    /// scales into the SwiGLU epilogue, same as the matmul_pre_noscale contract). None unless
6292    /// both tensors are NVFP4 q8_1-fast with identical (in_f, out_f, row_bytes) and m==1.
6293    pub fn matmul_pre_dual_noscale(&self, w0: &crate::model::GpuTensor, w1: &crate::model::GpuTensor,
6294                                   aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, m: usize)
6295        -> Result<Option<((CudaSlice<f32>, f32), (CudaSlice<f32>, f32))>, Box<dyn std::error::Error>> {
6296        use crate::model::GpuTensor;
6297        if m != 1 || !self.uses_q8_1_fast(w0) || !self.uses_q8_1_fast(w1) { return Ok(None); }
6298        // FP-ORDER LAW (lane/nvfp4-strict, 2026-08-05): every kernel this door can dispatch
6299        // (q8_0 fused2, nvfp4 dual_mr2) is the MMVQ family — 32-thread warp reduce. Without
6300        // MEMRA_MMVQ the m=1 singles ride dp4a (128-thread two-level reduce), so fusing here
6301        // would mix dispatch families across the pair — the exact class `q8_fused_params`
6302        // already refuses for Q8_0. The NVFP4 arm lacked this check, which is why
6303        // decode-batch-gate `--mode strict`'s equalizing env (MEMRA_MMVQ=0) never pinned
6304        // NVFP4 models: decode_step_h kept riding dual_mr2 while the batched body fell to
6305        // dp4a (gate1 maxdiff 1.639e-1 / gate2 step-8 divergence at the 2026-08-05 train
6306        // HEAD, research/nvfp4-strict-20260805/). Default env (MMVQ on) is dispatch-unchanged.
6307        if !self.mmvq_supports(QT_NVFP4) { return Ok(None); }
6308        let (in_f, out_f) = (w0.in_features(), w0.out_features());
6309        if w1.in_features() != in_f || w1.out_features() != out_f { return Ok(None); }
6310        // Q8_0 ARM (lane/q27-deepdive, 2026-08-05): the dense-FFN gate+up pair on a Q8_0 trunk fell
6311        // through this NVFP4-only gate to two `matmul_pre_noscale` launches — measured 128 of the
6312        // 1015 launches/token on q27-Q8_0 decode, the single largest un-fused class in the tick
6313        // (nsys `research/q27-deepdive-20260805/nsys/`). `q8_fused2_core` already serves the same
6314        // pair shape for the shared-expert gate/up, and its kernel body is `qmatvec_q8_0_mmvq`
6315        // VERBATIM per (tensor,row) -> BIT-IDENTICAL to the two separate launches. Q8_0 carries no
6316        // macro-scale (q8_fused_params requires scale==1.0), so the noscale contract is satisfied
6317        // by returning 1.0 for both: the SwiGLU epilogue's fold becomes the identity it already is
6318        // on this dtype today. Seam: MEMRA_Q8_FFN_FUSE2=0 rolls back to the two-launch pair.
6319        // rp4 guard: with MEMRA_Q8RP the singles route to the `_rp` split-plane twin over the
6320        // mirror buffer; the fused2 kernel has no `_rp` form, so fusing there would swap
6321        // dispatch families mid-model. Bail and let the two singles run (mirror lane unchanged).
6322        let no_mirror = |w: &crate::model::GpuTensor| {
6323            !matches!(w, GpuTensor::Quant { rp4: Some(_), .. })
6324        };
6325        if self.q8_ffn_fuse2_on()
6326            && no_mirror(w0) && no_mirror(w1)
6327            && let Some([p0, p1]) = self.q8_fused_params(&[w0, w1])
6328        {
6329            let (y0, y1) = self.q8_fused2_core(p0.0, p1.0, aq, ad, in_f, p0.1, p1.1, p0.2)?;
6330            return Ok(Some(((y0, 1.0), (y1, 1.0))));
6331        }
6332        // F8-E4M3 ARM (lane/fp8-decode-v1, 2026-08-05): with native e4m3 residency the FFN gate+up
6333        // pair (and the ssm beta+alpha dual, which routes through this same entry) fell through
6334        // both the NVFP4 gate below and the Q8_0 arm above to two `matmul_pre_noscale` launches —
6335        // native residency was UN-FUSING the trunk relative to the Q8_0 slab it replaces. The
6336        // fused2 kernel body is `qmatvec_e4m3_mmvq` VERBATIM per (tensor,row). Contract match:
6337        // `matmul_pre_noscale` on e4m3 launches with scale 1.0 and RETURNS the per-tensor
6338        // weight_scale for the caller to fold, so we pass ws=1.0 here and return (s0,s1) — same
6339        // bits, and the two macro-scale multiplies still fold into the SwiGLU epilogue.
6340        // MEMRA_E4M3_DUAL=0 rolls back to the two-launch pair.
6341        if let Some([p0, p1]) = self.e4m3_fused_params(&[w0, w1]) {
6342            let (y0, y1) = self.e4m3_fused2_core(p0.0, p1.0, aq, ad, in_f, p0.1, p1.1, p0.2,
6343                                                 1.0, 1.0)?;
6344            return Ok(Some(((y0, p0.3), (y1, p1.3))));
6345        }
6346        let (b0, q0, rb0, s0, rp0) = match w0 {
6347            GpuTensor::Quant { bytes, qtype, row_bytes, scale, rp, .. } => (bytes, *qtype, *row_bytes, *scale, *rp),
6348            _ => return Ok(None),
6349        };
6350        let (b1, q1, rb1, s1, rp1) = match w1 {
6351            GpuTensor::Quant { bytes, qtype, row_bytes, scale, rp, .. } => (bytes, *qtype, *row_bytes, *scale, *rp),
6352            _ => return Ok(None),
6353        };
6354        if q0 != QT_NVFP4 || q1 != QT_NVFP4 || rb0 != rb1 || rp0 != rp1 { return Ok(None); }
6355        const ROWS_PER_BLOCK: u32 = 4;   // matches MEMRA_MMVQ_ROWS in qmatvec.cu
6356        const RPW: u32 = 2;
6357        let rows_per_block = ROWS_PER_BLOCK * RPW;
6358        let f = self.func(if rp0 { "qmatvec_nvfp4_mmvq_dual_mr2_rp" } else { "qmatvec_nvfp4_mmvq_dual_mr2" });
6359        let mut y0 = self.alloc_uninit::<f32>(out_f)?;
6360        let mut y1 = self.alloc_uninit::<f32>(out_f)?;
6361        let cfg = LaunchConfig {
6362            grid_dim: ((out_f as u32 + rows_per_block - 1) / rows_per_block, 2, 1),
6363            block_dim: (32, ROWS_PER_BLOCK, 1), shared_mem_bytes: 0,
6364        };
6365        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, 1i32, rb0 as i64);
6366        // noscale contract: the caller folds s0/s1 into the SwiGLU epilogue — the kernel's fused
6367        // yscale args stay 1.0 here (they exist for the single-tensor callers).
6368        let one = 1.0f32;
6369        let __s_b = self.gpu.stream();
6370        let mut b = __s_b.launch_builder(&f);
6371        b.arg(b0).arg(b1).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1)
6372         .arg(&inf).arg(&outf).arg(&mi).arg(&rb).arg(&one).arg(&one);
6373        unsafe { b.launch(cfg)?; }
6374        Ok(Some(((y0, s0), (y1, s1))))
6375    }
6376
6377    /// FUSED Q8_0 m=1 matvec PAIR with UNEQUAL out_f (trunk launch-fusion, 2026-07-05). Folds two
6378    /// same-input q8_0 projections (35B trunk: wqkv+wqkv_gate 8192/4096, gate_shexp+up_shexp
6379    /// 512/512) into ONE launch via a block-offset split (blocks [0,nb0) -> w0, rest -> w1) — the
6380    /// dual-mr2 recipe with the same-out_f restriction lifted. Per (tensor,row) the kernel body is
6381    /// qmatvec_q8_0_mmvq VERBATIM -> BIT-IDENTICAL to two separate m=1 launches. Returns None when
6382    /// ineligible (not both Q8_0 / in_f mismatch / MEMRA_MMVQ off / MEMRA_Q8_DUAL=0) — caller falls
6383    /// back to the per-tensor path.
6384    pub fn matmul_q8_fused2(&self, w0: &crate::model::GpuTensor, w1: &crate::model::GpuTensor,
6385                            aq: &CudaSlice<i8>, ad: &CudaSlice<f32>)
6386        -> Result<Option<(CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
6387        // e4m3 twin (lane/fp8-decode-v1): this entry is the trunk's generic m=1 pair door
6388        // (wqkv+wqkv_gate, ssm_beta+alpha, gate_shexp+up_shexp), so admitting QT_F8_E4M3 here
6389        // fuses the NATIVE-RESIDENCY FP8 trunk at every existing call site with no call-site
6390        // change. Scale is folded in-kernel per range -> the returned buffers are already scaled,
6391        // exactly like the per-tensor `matmul_pre` e4m3 dispatch this replaces.
6392        if let Some([p0, p1]) = self.e4m3_fused_params(&[w0, w1]) {
6393            return Ok(Some(self.e4m3_fused2_core(p0.0, p1.0, aq, ad, w0.in_features(),
6394                                                 p0.1, p1.1, p0.2, p0.3, p1.3)?));
6395        }
6396        let Some([p0, p1]) = self.q8_fused_params(&[w0, w1]) else { return Ok(None) };
6397        Ok(Some(self.q8_fused2_core(p0.0, p1.0, aq, ad, w0.in_features(), p0.1, p1.1, p0.2)?))
6398    }
6399
6400    #[allow(clippy::too_many_arguments)]
6401    fn q8_fused2_core(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>,
6402                      aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
6403                      in_f: usize, out0: usize, out1: usize, row_bytes: usize)
6404        -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6405        const ROWS_PER_BLOCK: u32 = 4;   // matches MEMRA_MMVQ_ROWS in qmatvec.cu
6406        let nb0 = (out0 as u32).div_ceil(ROWS_PER_BLOCK);
6407        let nb1 = (out1 as u32).div_ceil(ROWS_PER_BLOCK);
6408        let f = self.func("qmatvec_q8_0_mmvq_fused2");
6409        let mut y0 = self.alloc_uninit::<f32>(out0)?;
6410        let mut y1 = self.alloc_uninit::<f32>(out1)?;
6411        let cfg = LaunchConfig { grid_dim: (nb0 + nb1, 1, 1), block_dim: (32, ROWS_PER_BLOCK, 1),
6412                                 shared_mem_bytes: 0 };
6413        let (inf, o0, o1, rbl) = (in_f as i32, out0 as i32, out1 as i32, row_bytes as i64);
6414        let __s_b = self.gpu.stream();
6415        let mut b = __s_b.launch_builder(&f);
6416        b.arg(b0).arg(b1).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1)
6417         .arg(&inf).arg(&o0).arg(&o1).arg(&rbl);
6418        unsafe { b.launch(cfg)?; }
6419        Ok((y0, y1))
6420    }
6421
6422    /// f32-activation entry for the fused2 pair: quantizes x to q8_1 ONCE then runs the fused
6423    /// launch — replaces two `matmul(w, x, 1)` calls that would each re-quantize the same x
6424    /// (35B shared-expert gate+up per MoE layer per token). Same bits: quantize_q8_1 is
6425    /// deterministic, the fused body is the MMVQ kernel verbatim. None when ineligible (the
6426    /// callers' m==1-under-MEMRA_FAST dispatch would take MMVQ; anything else falls back).
6427    pub fn matmul_q8_fused2_x(&self, w0: &crate::model::GpuTensor, w1: &crate::model::GpuTensor,
6428                              x: &CudaSlice<f32>)
6429        -> Result<Option<(CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
6430        if !self.uses_q8_1_fast(w0) || !self.uses_q8_1_fast(w1) { return Ok(None); }
6431        if let Some([p0, p1]) = self.e4m3_fused_params(&[w0, w1]) {
6432            let (aq, ad) = self.quantize_q8_1(x, 1, w0.in_features())?;
6433            return Ok(Some(self.e4m3_fused2_core(p0.0, p1.0, &aq, &ad, w0.in_features(),
6434                                                 p0.1, p1.1, p0.2, p0.3, p1.3)?));
6435        }
6436        let Some([p0, p1]) = self.q8_fused_params(&[w0, w1]) else { return Ok(None) };
6437        let (aq, ad) = self.quantize_q8_1(x, 1, w0.in_features())?;
6438        Ok(Some(self.q8_fused2_core(p0.0, p1.0, &aq, &ad, w0.in_features(), p0.1, p1.1, p0.2)?))
6439    }
6440
6441    /// Test entry for the kernel_check gate: launch the fused2 kernel from raw weight bytes,
6442    /// quantizing the f32 activation internally (mirrors qmatvec_mmvq_raw; no env gating).
6443    #[allow(clippy::too_many_arguments)]
6444    pub fn qmatvec_q8_fused2_raw(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>, x: &CudaSlice<f32>,
6445                                 in_f: usize, out0: usize, out1: usize, row_bytes: usize)
6446        -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6447        let (aq, ad) = self.quantize_q8_1(x, 1, in_f)?;
6448        self.q8_fused2_core(b0, b1, &aq, &ad, in_f, out0, out1, row_bytes)
6449    }
6450
6451    /// FUSED Q8_0 m=1 matvec TRIPLE (wq+wk+wv on the 35B full-attn layers: out_f 8192/512/512).
6452    /// Same block-offset recipe as `matmul_q8_fused2` with three ranges. BIT-IDENTICAL per
6453    /// (tensor,row) to three separate m=1 MMVQ launches.
6454    /// FUSED Q4_0 m=1 TRIPLE (gemma q/k/v — same quantized input; per (tensor,row) chain
6455    /// identical to the mr2 kernel). Returns None unless all three are Q4_0 with equal in_f.
6456    pub fn matmul_q4_fused3(&self, w0: &crate::model::GpuTensor, w1: &crate::model::GpuTensor,
6457                            w2: &crate::model::GpuTensor,
6458                            aq: &CudaSlice<i8>, ad: &CudaSlice<f32>)
6459        -> Result<Option<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
6460        use crate::model::GpuTensor;
6461        let q4 = |w: &GpuTensor| -> Option<(usize, usize)> {
6462            match w {
6463                GpuTensor::Quant { qtype, row_bytes, .. } if *qtype == QT_Q4_0 =>
6464                    Some((*row_bytes, w.out_features())),
6465                _ => None,
6466            }
6467        };
6468        let (Some((rb0, o0)), Some((rb1, o1)), Some((rb2, o2))) = (q4(w0), q4(w1), q4(w2))
6469        else { return Ok(None) };
6470        if w0.in_features() != w1.in_features() || w0.in_features() != w2.in_features() {
6471            return Ok(None);
6472        }
6473        // Effective (bytes, rp) per tensor: mirror (rp4) OR the in-place swap (rp flag,
6474        // bytes already split). Mixed layouts cannot share one fused launch -> fall back to
6475        // the separate matvecs (each routes its own rp).
6476        fn eff(w: &GpuTensor) -> (&CudaSlice<u8>, bool) {
6477            match w {
6478                GpuTensor::Quant { bytes, rp4, rp, .. } => match rp4 {
6479                    Some(m) => (m, true),
6480                    None => (bytes, *rp),
6481                },
6482                _ => unreachable!(),
6483            }
6484        }
6485        let ((b0, rp0), (b1, rp1), (b2, rp2)) = (eff(w0), eff(w1), eff(w2));
6486        if rp0 != rp1 || rp1 != rp2 { return Ok(None); }
6487        let rp = rp0;
6488        let rpb: u32 = 4;
6489        // mr1 (one row/warp, 2026-07-14): follows the singles' MEMRA_Q40_MR default — the
6490        // fused t=1 kernels were left on mr2 when the singles flipped (DRAM-duty map:
6491        // fused3 57% / fused2 86%; small qkv segments starve under mr2's half grid).
6492        let mr1 = rp && Self::q40_mr1_on();
6493        let nb = |o: usize| if mr1 { (o as u32).div_ceil(rpb) }
6494                            else { (o as u32).div_ceil(2).div_ceil(rpb) };
6495        let grid = nb(o0) + nb(o1) + nb(o2);
6496        let mut y0 = self.alloc_uninit::<f32>(o0)?;
6497        let mut y1 = self.alloc_uninit::<f32>(o1)?;
6498        let mut y2 = self.alloc_uninit::<f32>(o2)?;
6499        let f = self.func(if mr1 { "qmatvec_q4_0_mmvq_fused3_mr1_rp" }
6500                          else if rp { "qmatvec_q4_0_mmvq_fused3_rp" }
6501                          else { "qmatvec_q4_0_mmvq_fused3" });
6502        let cfg = LaunchConfig { grid_dim: (grid, 1, 1), block_dim: (32, rpb, 1), shared_mem_bytes: 0 };
6503        let inf = w0.in_features() as i32;
6504        let (oo0, oo1, oo2) = (o0 as i32, o1 as i32, o2 as i32);
6505        let (r0, r1, r2) = (rb0 as i64, rb1 as i64, rb2 as i64);
6506        // PDL wave-A (2026-07-23): the mr1 kernel carries MEMRA_PDL_ENTRY; only that
6507        // variant may take the programmatic-serialization launch.
6508        if mr1 && Self::pdl_on() && Self::pdl_mmvq_on() {
6509            {
6510            use cudarc::driver::{DevicePtr, DevicePtrMut};
6511            let s = &self.gpu.stream();
6512            let (p0, _g0) = b0.device_ptr(s); let (p1, _g1) = b1.device_ptr(s);
6513            let (p2, _g2) = b2.device_ptr(s); let (paq, _g3) = aq.device_ptr(s);
6514            let (pad, _g4) = ad.device_ptr(s);
6515            let (py0, _g5) = y0.device_ptr_mut(s); let (py1, _g6) = y1.device_ptr_mut(s);
6516            let (py2, _g7) = y2.device_ptr_mut(s);
6517            let mut ps = [
6518                &p0 as *const _ as *mut std::ffi::c_void, &p1 as *const _ as *mut _,
6519                &p2 as *const _ as *mut _, &paq as *const _ as *mut _,
6520                &pad as *const _ as *mut _, &py0 as *const _ as *mut _,
6521                &py1 as *const _ as *mut _, &py2 as *const _ as *mut _,
6522                &inf as *const _ as *mut _, &oo0 as *const _ as *mut _,
6523                &oo1 as *const _ as *mut _, &oo2 as *const _ as *mut _,
6524                &r0 as *const _ as *mut _, &r1 as *const _ as *mut _,
6525                &r2 as *const _ as *mut _,
6526            ];
6527            unsafe { self.launch_pdl("qmatvec_q4_0_mmvq_fused3_mr1_rp",
6528                                     (grid, 1, 1), (32, rpb, 1), &mut ps)?; }
6529            }
6530            return Ok(Some((y0, y1, y2)));
6531        }
6532        let __s_b = self.gpu.stream();
6533        let mut b = __s_b.launch_builder(&f);
6534        b.arg(b0).arg(b1).arg(b2).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1).arg(&mut y2)
6535         .arg(&inf).arg(&oo0).arg(&oo1).arg(&oo2).arg(&r0).arg(&r1).arg(&r2);
6536        unsafe { b.launch(cfg)?; }
6537        Ok(Some((y0, y1, y2)))
6538    }
6539
6540    /// Slot-fed fused3 twin (alloc-free capture lane): identical launch, caller-owned outputs.
6541    /// Returns Ok(false) when the fused path is unavailable (caller falls back).
6542    #[allow(clippy::too_many_arguments)]
6543    pub fn matmul_q4_fused3_into(&self, w0: &crate::model::GpuTensor, w1: &crate::model::GpuTensor,
6544                                 w2: &crate::model::GpuTensor,
6545                                 aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
6546                                 y0: &mut CudaSlice<f32>, y1: &mut CudaSlice<f32>,
6547                                 y2: &mut CudaSlice<f32>)
6548        -> Result<bool, Box<dyn std::error::Error>> {
6549        use crate::model::GpuTensor;
6550        let q4 = |w: &GpuTensor| -> Option<(usize, usize)> {
6551            match w {
6552                GpuTensor::Quant { qtype, row_bytes, .. } if *qtype == QT_Q4_0 =>
6553                    Some((*row_bytes, w.out_features())),
6554                _ => None,
6555            }
6556        };
6557        let (Some((rb0, o0)), Some((rb1, o1)), Some((rb2, o2))) = (q4(w0), q4(w1), q4(w2))
6558        else { return Ok(false) };
6559        if w0.in_features() != w1.in_features() || w0.in_features() != w2.in_features() {
6560            return Ok(false);
6561        }
6562        fn eff(w: &GpuTensor) -> (&CudaSlice<u8>, bool) {
6563            match w {
6564                GpuTensor::Quant { bytes, rp4, rp, .. } => match rp4 {
6565                    Some(m) => (m, true),
6566                    None => (bytes, *rp),
6567                },
6568                _ => unreachable!(),
6569            }
6570        }
6571        let ((b0, rp0), (b1, rp1), (b2, rp2)) = (eff(w0), eff(w1), eff(w2));
6572        if rp0 != rp1 || rp1 != rp2 { return Ok(false); }
6573        let rp = rp0;
6574        let rpb: u32 = 4;
6575        let mr1 = rp && Self::q40_mr1_on();
6576        let nb = |o: usize| if mr1 { (o as u32).div_ceil(rpb) }
6577                            else { (o as u32).div_ceil(2).div_ceil(rpb) };
6578        let grid = nb(o0) + nb(o1) + nb(o2);
6579        debug_assert!(y0.len() >= o0 && y1.len() >= o1 && y2.len() >= o2);
6580        let f = self.func(if mr1 { "qmatvec_q4_0_mmvq_fused3_mr1_rp" }
6581                          else if rp { "qmatvec_q4_0_mmvq_fused3_rp" }
6582                          else { "qmatvec_q4_0_mmvq_fused3" });
6583        let cfg = LaunchConfig { grid_dim: (grid, 1, 1), block_dim: (32, rpb, 1), shared_mem_bytes: 0 };
6584        let inf = w0.in_features() as i32;
6585        let (oo0, oo1, oo2) = (o0 as i32, o1 as i32, o2 as i32);
6586        let (r0, r1, r2) = (rb0 as i64, rb1 as i64, rb2 as i64);
6587        // PDL wave-A: identical to the owned twin (capture-lane parity).
6588        if mr1 && Self::pdl_on() && Self::pdl_mmvq_on() {
6589            use cudarc::driver::{DevicePtr, DevicePtrMut};
6590            let s = &self.gpu.stream();
6591            let (p0, _g0) = b0.device_ptr(s); let (p1, _g1) = b1.device_ptr(s);
6592            let (p2, _g2) = b2.device_ptr(s); let (paq, _g3) = aq.device_ptr(s);
6593            let (pad, _g4) = ad.device_ptr(s);
6594            let (py0, _g5) = y0.device_ptr_mut(s); let (py1, _g6) = y1.device_ptr_mut(s);
6595            let (py2, _g7) = y2.device_ptr_mut(s);
6596            let mut ps = [
6597                &p0 as *const _ as *mut std::ffi::c_void, &p1 as *const _ as *mut _,
6598                &p2 as *const _ as *mut _, &paq as *const _ as *mut _,
6599                &pad as *const _ as *mut _, &py0 as *const _ as *mut _,
6600                &py1 as *const _ as *mut _, &py2 as *const _ as *mut _,
6601                &inf as *const _ as *mut _, &oo0 as *const _ as *mut _,
6602                &oo1 as *const _ as *mut _, &oo2 as *const _ as *mut _,
6603                &r0 as *const _ as *mut _, &r1 as *const _ as *mut _,
6604                &r2 as *const _ as *mut _,
6605            ];
6606            unsafe { self.launch_pdl("qmatvec_q4_0_mmvq_fused3_mr1_rp",
6607                                     (grid, 1, 1), (32, rpb, 1), &mut ps)?; }
6608            return Ok(true);
6609        }
6610        let __s_b = self.gpu.stream();
6611        let mut b = __s_b.launch_builder(&f);
6612        b.arg(b0).arg(b1).arg(b2).arg(aq).arg(ad).arg(&mut *y0).arg(&mut *y1).arg(&mut *y2)
6613         .arg(&inf).arg(&oo0).arg(&oo1).arg(&oo2).arg(&r0).arg(&r1).arg(&r2);
6614        unsafe { b.launch(cfg)?; }
6615        Ok(true)
6616    }
6617
6618    /// FUSED Q4_0 m=1 PAIR (gemma shared gate+up).
6619    pub fn matmul_q4_fused2(&self, w0: &crate::model::GpuTensor, w1: &crate::model::GpuTensor,
6620                            aq: &CudaSlice<i8>, ad: &CudaSlice<f32>)
6621        -> Result<Option<(CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
6622        use crate::model::GpuTensor;
6623        let q4 = |w: &GpuTensor| -> Option<(usize, usize)> {
6624            match w {
6625                GpuTensor::Quant { qtype, row_bytes, .. } if *qtype == QT_Q4_0 =>
6626                    Some((*row_bytes, w.out_features())),
6627                _ => None,
6628            }
6629        };
6630        let (Some((rb0, o0)), Some((rb1, o1))) = (q4(w0), q4(w1)) else { return Ok(None) };
6631        if w0.in_features() != w1.in_features() { return Ok(None); }
6632        // Effective (bytes, rp) per tensor (mirror or in-place swap); mixed -> separate matvecs.
6633        fn eff(w: &GpuTensor) -> (&CudaSlice<u8>, bool) {
6634            match w {
6635                GpuTensor::Quant { bytes, rp4, rp, .. } => match rp4 {
6636                    Some(m) => (m, true),
6637                    None => (bytes, *rp),
6638                },
6639                _ => unreachable!(),
6640            }
6641        }
6642        let ((b0, rp0), (b1, rp1)) = (eff(w0), eff(w1));
6643        if rp0 != rp1 { return Ok(None); }
6644        let rp = rp0;
6645        let rpb: u32 = 4;
6646        // mr1 twin — see matmul_q4_fused3.
6647        let mr1 = rp && Self::q40_mr1_on();
6648        let nb = |o: usize| if mr1 { (o as u32).div_ceil(rpb) }
6649                            else { (o as u32).div_ceil(2).div_ceil(rpb) };
6650        let grid = nb(o0) + nb(o1);
6651        let mut y0 = self.alloc_uninit::<f32>(o0)?;
6652        let mut y1 = self.alloc_uninit::<f32>(o1)?;
6653        let f = self.func(if mr1 { "qmatvec_q4_0_mmvq_fused2_mr1_rp" }
6654                          else if rp { "qmatvec_q4_0_mmvq_fused2_rp" }
6655                          else { "qmatvec_q4_0_mmvq_fused2" });
6656        let cfg = LaunchConfig { grid_dim: (grid, 1, 1), block_dim: (32, rpb, 1), shared_mem_bytes: 0 };
6657        let inf = w0.in_features() as i32;
6658        let (oo0, oo1) = (o0 as i32, o1 as i32);
6659        let (r0, r1) = (rb0 as i64, rb1 as i64);
6660        // PDL wave-A: mr1 kernel carries MEMRA_PDL_ENTRY.
6661        if mr1 && Self::pdl_on() && Self::pdl_mmvq_on() {
6662            {
6663            use cudarc::driver::{DevicePtr, DevicePtrMut};
6664            let s = &self.gpu.stream();
6665            let (p0, _g0) = b0.device_ptr(s); let (p1, _g1) = b1.device_ptr(s);
6666            let (paq, _g2) = aq.device_ptr(s); let (pad, _g3) = ad.device_ptr(s);
6667            let (py0, _g4) = y0.device_ptr_mut(s); let (py1, _g5) = y1.device_ptr_mut(s);
6668            let mut ps = [
6669                &p0 as *const _ as *mut std::ffi::c_void, &p1 as *const _ as *mut _,
6670                &paq as *const _ as *mut _, &pad as *const _ as *mut _,
6671                &py0 as *const _ as *mut _, &py1 as *const _ as *mut _,
6672                &inf as *const _ as *mut _, &oo0 as *const _ as *mut _,
6673                &oo1 as *const _ as *mut _, &r0 as *const _ as *mut _,
6674                &r1 as *const _ as *mut _,
6675            ];
6676            unsafe { self.launch_pdl("qmatvec_q4_0_mmvq_fused2_mr1_rp",
6677                                     (grid, 1, 1), (32, rpb, 1), &mut ps)?; }
6678            }
6679            return Ok(Some((y0, y1)));
6680        }
6681        let __s_b = self.gpu.stream();
6682        let mut b = __s_b.launch_builder(&f);
6683        b.arg(b0).arg(b1).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1)
6684         .arg(&inf).arg(&oo0).arg(&oo1).arg(&r0).arg(&r1);
6685        unsafe { b.launch(cfg)?; }
6686        Ok(Some((y0, y1)))
6687    }
6688
6689    /// Slot-fed fused2 twin (alloc-free capture lane): identical launch, caller-owned outputs.
6690    pub fn matmul_q4_fused2_into(&self, w0: &crate::model::GpuTensor, w1: &crate::model::GpuTensor,
6691                                 aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
6692                                 y0: &mut CudaSlice<f32>, y1: &mut CudaSlice<f32>)
6693        -> Result<bool, Box<dyn std::error::Error>> {
6694        use crate::model::GpuTensor;
6695        let q4 = |w: &GpuTensor| -> Option<(usize, usize)> {
6696            match w {
6697                GpuTensor::Quant { qtype, row_bytes, .. } if *qtype == QT_Q4_0 =>
6698                    Some((*row_bytes, w.out_features())),
6699                _ => None,
6700            }
6701        };
6702        let (Some((rb0, o0)), Some((rb1, o1))) = (q4(w0), q4(w1)) else { return Ok(false) };
6703        if w0.in_features() != w1.in_features() { return Ok(false); }
6704        fn eff(w: &GpuTensor) -> (&CudaSlice<u8>, bool) {
6705            match w {
6706                GpuTensor::Quant { bytes, rp4, rp, .. } => match rp4 {
6707                    Some(m) => (m, true),
6708                    None => (bytes, *rp),
6709                },
6710                _ => unreachable!(),
6711            }
6712        }
6713        let ((b0, rp0), (b1, rp1)) = (eff(w0), eff(w1));
6714        if rp0 != rp1 { return Ok(false); }
6715        let rp = rp0;
6716        let rpb: u32 = 4;
6717        let mr1 = rp && Self::q40_mr1_on();
6718        let nb = |o: usize| if mr1 { (o as u32).div_ceil(rpb) }
6719                            else { (o as u32).div_ceil(2).div_ceil(rpb) };
6720        let grid = nb(o0) + nb(o1);
6721        debug_assert!(y0.len() >= o0 && y1.len() >= o1);
6722        let f = self.func(if mr1 { "qmatvec_q4_0_mmvq_fused2_mr1_rp" }
6723                          else if rp { "qmatvec_q4_0_mmvq_fused2_rp" }
6724                          else { "qmatvec_q4_0_mmvq_fused2" });
6725        let cfg = LaunchConfig { grid_dim: (grid, 1, 1), block_dim: (32, rpb, 1), shared_mem_bytes: 0 };
6726        let inf = w0.in_features() as i32;
6727        let (oo0, oo1) = (o0 as i32, o1 as i32);
6728        let (r0, r1) = (rb0 as i64, rb1 as i64);
6729        // PDL wave-A: identical to the owned twin (capture-lane parity).
6730        if mr1 && Self::pdl_on() && Self::pdl_mmvq_on() {
6731            use cudarc::driver::{DevicePtr, DevicePtrMut};
6732            let s = &self.gpu.stream();
6733            let (p0, _g0) = b0.device_ptr(s); let (p1, _g1) = b1.device_ptr(s);
6734            let (paq, _g2) = aq.device_ptr(s); let (pad, _g3) = ad.device_ptr(s);
6735            let (py0, _g4) = y0.device_ptr_mut(s); let (py1, _g5) = y1.device_ptr_mut(s);
6736            let mut ps = [
6737                &p0 as *const _ as *mut std::ffi::c_void, &p1 as *const _ as *mut _,
6738                &paq as *const _ as *mut _, &pad as *const _ as *mut _,
6739                &py0 as *const _ as *mut _, &py1 as *const _ as *mut _,
6740                &inf as *const _ as *mut _, &oo0 as *const _ as *mut _,
6741                &oo1 as *const _ as *mut _, &r0 as *const _ as *mut _,
6742                &r1 as *const _ as *mut _,
6743            ];
6744            unsafe { self.launch_pdl("qmatvec_q4_0_mmvq_fused2_mr1_rp",
6745                                     (grid, 1, 1), (32, rpb, 1), &mut ps)?; }
6746            return Ok(true);
6747        }
6748        let __s_b = self.gpu.stream();
6749        let mut b = __s_b.launch_builder(&f);
6750        b.arg(b0).arg(b1).arg(aq).arg(ad).arg(&mut *y0).arg(&mut *y1)
6751         .arg(&inf).arg(&oo0).arg(&oo1).arg(&r0).arg(&r1);
6752        unsafe { b.launch(cfg)?; }
6753        Ok(true)
6754    }
6755
6756    /// BATCHED fused2 (2026-07-13, megakernel-microcosm probe): gate+up b-tier matvecs in
6757    /// ONE segmented-grid launch — the up segment fills SMs as the gate segment drains
6758    /// (the per-launch tail waves behind the 6x-falsified b-tier plateau). Bit-identical
6759    /// per row to two mr2_rp launches. rp layout required; m in 2..=8 (b16 has no twin).
6760    pub fn matmul_q4_fused2_batched(&self, w0: &crate::model::GpuTensor,
6761                                    w1: &crate::model::GpuTensor,
6762                                    aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, m: usize)
6763        -> Result<Option<(CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
6764        use crate::model::GpuTensor;
6765        if m < 2 || m > 8 { return Ok(None); }
6766        let q4 = |w: &GpuTensor| -> Option<(usize, usize)> {
6767            match w {
6768                GpuTensor::Quant { qtype, row_bytes, .. } if *qtype == QT_Q4_0 =>
6769                    Some((*row_bytes, w.out_features())),
6770                _ => None,
6771            }
6772        };
6773        let (Some((rb0, o0)), Some((_rb1, o1))) = (q4(w0), q4(w1)) else { return Ok(None) };
6774        if w0.in_features() != w1.in_features() { return Ok(None); }
6775        fn eff(w: &GpuTensor) -> (&CudaSlice<u8>, bool) {
6776            match w {
6777                GpuTensor::Quant { bytes, rp4, rp, .. } => match rp4 {
6778                    Some(mr) => (mr, true),
6779                    None => (bytes, *rp),
6780                },
6781                _ => unreachable!(),
6782            }
6783        }
6784        let ((b0, rp0), (b1, rp1)) = (eff(w0), eff(w1));
6785        if !rp0 || !rp1 { return Ok(None); }
6786        let mcols = Self::batched_mcols(m);
6787        let rpb: u32 = 4;
6788        let nb = |o: usize| (o as u32).div_ceil(2 * rpb);
6789        let grid = nb(o0) + nb(o1);
6790        let mut y0 = self.alloc_uninit::<f32>(m * o0)?;
6791        let mut y1 = self.alloc_uninit::<f32>(m * o1)?;
6792        let f = self.func(match mcols { 2 => "qmatvec_q4_0_mmvq_b2_f2_rp",
6793                                        4 => "qmatvec_q4_0_mmvq_b4_f2_rp",
6794                                        _ => "qmatvec_q4_0_mmvq_b8_f2_rp" });
6795        let cfg = LaunchConfig { grid_dim: (grid, 1, 1), block_dim: (32, rpb, 1),
6796                                 shared_mem_bytes: 0 };
6797        let inf = w0.in_features() as i32;
6798        let (oo0, oo1, mi) = (o0 as i32, o1 as i32, m as i32);
6799        let rb = rb0 as i64;
6800        let __s_b = self.gpu.stream();
6801        let mut b = __s_b.launch_builder(&f);
6802        b.arg(b0).arg(b1).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1)
6803         .arg(&inf).arg(&oo0).arg(&oo1).arg(&mi).arg(&rb);
6804        unsafe { b.launch(cfg)?; }
6805        Ok(Some((y0, y1)))
6806    }
6807
6808    /// BATCHED fused3 (see matmul_q4_fused2_batched): three-segment single launch for the
6809    /// verify qkv triple. Same-in_f q4_0 rp tensors, m in 2..=8. Bit-identical per row.
6810    #[allow(clippy::too_many_arguments)]
6811    pub fn matmul_q4_fused3_batched(&self, w0: &crate::model::GpuTensor,
6812                                    w1: &crate::model::GpuTensor, w2: &crate::model::GpuTensor,
6813                                    aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, m: usize)
6814        -> Result<Option<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
6815        use crate::model::GpuTensor;
6816        if m < 2 || m > 8 { return Ok(None); }
6817        let q4 = |w: &GpuTensor| -> Option<usize> {
6818            match w {
6819                GpuTensor::Quant { qtype, .. } if *qtype == QT_Q4_0 => Some(w.out_features()),
6820                _ => None,
6821            }
6822        };
6823        let (Some(o0), Some(o1), Some(o2)) = (q4(w0), q4(w1), q4(w2)) else { return Ok(None) };
6824        if w0.in_features() != w1.in_features() || w0.in_features() != w2.in_features() {
6825            return Ok(None);
6826        }
6827        fn eff(w: &GpuTensor) -> (&CudaSlice<u8>, bool) {
6828            match w {
6829                GpuTensor::Quant { bytes, rp4, rp, .. } => match rp4 {
6830                    Some(mr) => (mr, true),
6831                    None => (bytes, *rp),
6832                },
6833                _ => unreachable!(),
6834            }
6835        }
6836        let ((b0, rp0), (b1, rp1), (b2, rp2)) = (eff(w0), eff(w1), eff(w2));
6837        if !rp0 || !rp1 || !rp2 { return Ok(None); }
6838        let mcols = Self::batched_mcols(m);
6839        let rpb: u32 = 4;
6840        let nb = |o: usize| (o as u32).div_ceil(2 * rpb);
6841        let grid = nb(o0) + nb(o1) + nb(o2);
6842        let mut y0 = self.alloc_uninit::<f32>(m * o0)?;
6843        let mut y1 = self.alloc_uninit::<f32>(m * o1)?;
6844        let mut y2 = self.alloc_uninit::<f32>(m * o2)?;
6845        let f = self.func(match mcols { 2 => "qmatvec_q4_0_mmvq_b2_f3_rp",
6846                                        4 => "qmatvec_q4_0_mmvq_b4_f3_rp",
6847                                        _ => "qmatvec_q4_0_mmvq_b8_f3_rp" });
6848        let cfg = LaunchConfig { grid_dim: (grid, 1, 1), block_dim: (32, rpb, 1),
6849                                 shared_mem_bytes: 0 };
6850        let inf = w0.in_features() as i32;
6851        let (oo0, oo1, oo2, mi) = (o0 as i32, o1 as i32, o2 as i32, m as i32);
6852        let rb = 0i64;
6853        let __s_b = self.gpu.stream();
6854        let mut b = __s_b.launch_builder(&f);
6855        b.arg(b0).arg(b1).arg(b2).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1).arg(&mut y2)
6856         .arg(&inf).arg(&oo0).arg(&oo1).arg(&oo2).arg(&mi).arg(&rb);
6857        unsafe { b.launch(cfg)?; }
6858        Ok(Some((y0, y1, y2)))
6859    }
6860
6861    pub fn matmul_q8_fused3(&self, w0: &crate::model::GpuTensor, w1: &crate::model::GpuTensor,
6862                            w2: &crate::model::GpuTensor,
6863                            aq: &CudaSlice<i8>, ad: &CudaSlice<f32>)
6864        -> Result<Option<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
6865        // e4m3 twin (lane/fp8-decode-v1): the full-attn wq/wk/wv triple — on the NV-27B those three
6866        // are per-tensor FP8, so native residency without this arm meant three separate launches.
6867        if let Some([p0, p1, p2]) = self.e4m3_fused_params(&[w0, w1, w2]) {
6868            return Ok(Some(self.e4m3_fused3_core(p0.0, p1.0, p2.0, aq, ad, w0.in_features(),
6869                                                 p0.1, p1.1, p2.1, p0.2,
6870                                                 p0.3, p1.3, p2.3)?));
6871        }
6872        let Some([p0, p1, p2]) = self.q8_fused_params(&[w0, w1, w2]) else { return Ok(None) };
6873        Ok(Some(self.q8_fused3_core(p0.0, p1.0, p2.0, aq, ad, w0.in_features(),
6874                                    p0.1, p1.1, p2.1, p0.2)?))
6875    }
6876
6877    #[allow(clippy::too_many_arguments)]
6878    fn q8_fused3_core(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>, b2: &CudaSlice<u8>,
6879                      aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
6880                      in_f: usize, out0: usize, out1: usize, out2: usize, row_bytes: usize)
6881        -> Result<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6882        const ROWS_PER_BLOCK: u32 = 4;
6883        let nb0 = (out0 as u32).div_ceil(ROWS_PER_BLOCK);
6884        let nb1 = (out1 as u32).div_ceil(ROWS_PER_BLOCK);
6885        let nb2 = (out2 as u32).div_ceil(ROWS_PER_BLOCK);
6886        let f = self.func("qmatvec_q8_0_mmvq_fused3");
6887        let mut y0 = self.alloc_uninit::<f32>(out0)?;
6888        let mut y1 = self.alloc_uninit::<f32>(out1)?;
6889        let mut y2 = self.alloc_uninit::<f32>(out2)?;
6890        let cfg = LaunchConfig { grid_dim: (nb0 + nb1 + nb2, 1, 1), block_dim: (32, ROWS_PER_BLOCK, 1),
6891                                 shared_mem_bytes: 0 };
6892        let (inf, o0, o1, o2, rbl) = (in_f as i32, out0 as i32, out1 as i32, out2 as i32, row_bytes as i64);
6893        let __s_b = self.gpu.stream();
6894        let mut b = __s_b.launch_builder(&f);
6895        b.arg(b0).arg(b1).arg(b2).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1).arg(&mut y2)
6896         .arg(&inf).arg(&o0).arg(&o1).arg(&o2).arg(&rbl);
6897        unsafe { b.launch(cfg)?; }
6898        Ok((y0, y1, y2))
6899    }
6900
6901    /// Test entry for the kernel_check gate: fused3 from raw weight bytes (internal q8_1 quant).
6902    #[allow(clippy::too_many_arguments)]
6903    pub fn qmatvec_q8_fused3_raw(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>, b2: &CudaSlice<u8>,
6904                                 x: &CudaSlice<f32>, in_f: usize, out0: usize, out1: usize,
6905                                 out2: usize, row_bytes: usize)
6906        -> Result<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6907        let (aq, ad) = self.quantize_q8_1(x, 1, in_f)?;
6908        self.q8_fused3_core(b0, b1, b2, &aq, &ad, in_f, out0, out1, out2, row_bytes)
6909    }
6910
6911    /// BATCHED twin of `matmul_q8_fused2` for the verify t=2-4 tier (MEMRA_SPEC_FUSED_T call
6912    /// sites, lane/close35b): ONE launch computes both same-input Q8_0 projections for m tokens.
6913    /// Per (tensor,token,row) the kernel body is q8_0_mmvq_batched VERBATIM with the identical
6914    /// row mapping (Q8_0's batched_variant is always "base") -> BIT-IDENTICAL to the two
6915    /// per-tensor _b2/_b4 launches `matmul_decode_exact` dispatches at m=2-4, with the caller's
6916    /// single shared q8_1 activation replacing two per-call re-quantizes (quantize_q8_1 is
6917    /// deterministic -> same bytes). None when ineligible (m outside 2..=4 / not both Q8_0 /
6918    /// in_f mismatch / MEMRA_MMVQ=0 / MEMRA_Q8_DUAL=0 / MEMRA_NO_BATCHED set — the last keeps
6919    /// dispatch parity: without batched kernels decode-exact runs grid.y=m MMVQ, and the fused
6920    /// twin must not introduce a batched program the reference path would not run).
6921    pub fn matmul_q8_fused2_t(&self, w0: &crate::model::GpuTensor, w1: &crate::model::GpuTensor,
6922                              aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, m: usize)
6923        -> Result<Option<(CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
6924        // m<=8 (lane/q27-deepdive, 2026-08-05): was 2..=4 (the verify tier's mcols 2/4). The
6925        // serving tick's mcols-8 tier now has its fused2_b8 wrapper, so c=5..8 batched decode
6926        // fuses too — same template body, still bit-identical to the two _b8 launches.
6927        if !(2..=8).contains(&m) || std::env::var("MEMRA_NO_BATCHED").is_ok() { return Ok(None); }
6928        // e4m3 twin: MEMRA_B8 parity — without it m=5..8 e4m3 decode runs the per-m grid.y=m path,
6929        // so the fused b8 launch would introduce a batched program the reference path would not run.
6930        if let Some([p0, p1]) = self.e4m3_fused_params(&[w0, w1]) {
6931            if m > 4 && !Self::b8_enabled() { return Ok(None); }
6932            return Ok(Some(self.e4m3_fused2_t_core(p0.0, p1.0, aq, ad, m, w0.in_features(),
6933                                                   p0.1, p1.1, p0.2, p0.3, p1.3)?));
6934        }
6935        let Some([p0, p1]) = self.q8_fused_params(&[w0, w1]) else { return Ok(None) };
6936        Ok(Some(self.q8_fused2_t_core(p0.0, p1.0, aq, ad, m, w0.in_features(), p0.1, p1.1, p0.2)?))
6937    }
6938
6939    #[allow(clippy::too_many_arguments)]
6940    fn q8_fused2_t_core(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>,
6941                        aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, m: usize,
6942                        in_f: usize, out0: usize, out1: usize, row_bytes: usize)
6943        -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6944        const ROWS_PER_BLOCK: u32 = 4;   // matches MEMRA_MMVQ_ROWS in qmatvec.cu
6945        let nb0 = (out0 as u32).div_ceil(ROWS_PER_BLOCK);
6946        let nb1 = (out1 as u32).div_ceil(ROWS_PER_BLOCK);
6947        let f = self.func(match Self::batched_mcols(m) {
6948            2 => "qmatvec_q8_0_mmvq_fused2_b2",
6949            4 => "qmatvec_q8_0_mmvq_fused2_b4",
6950            // b8 = the SERVING tier (lane/q27-deepdive): c=5..8 batched decode.
6951            _ => "qmatvec_q8_0_mmvq_fused2_b8",
6952        });
6953        let mut y0 = self.alloc_uninit::<f32>(m * out0)?;
6954        let mut y1 = self.alloc_uninit::<f32>(m * out1)?;
6955        let cfg = LaunchConfig { grid_dim: (nb0 + nb1, 1, 1), block_dim: (32, ROWS_PER_BLOCK, 1),
6956                                 shared_mem_bytes: 0 };
6957        let (inf, o0, o1, mi, rbl) = (in_f as i32, out0 as i32, out1 as i32, m as i32, row_bytes as i64);
6958        let __s_b = self.gpu.stream();
6959        let mut b = __s_b.launch_builder(&f);
6960        b.arg(b0).arg(b1).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1)
6961         .arg(&inf).arg(&o0).arg(&o1).arg(&mi).arg(&rbl);
6962        unsafe { b.launch(cfg)?; }
6963        Ok((y0, y1))
6964    }
6965
6966    /// Test entry for the kernel_check gate: fused2 batched from raw weight bytes (internal
6967    /// q8_1 quant of the [m, in_f] activation), no env gating.
6968    #[allow(clippy::too_many_arguments)]
6969    pub fn qmatvec_q8_fused2_t_raw(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>,
6970                                   x: &CudaSlice<f32>, m: usize,
6971                                   in_f: usize, out0: usize, out1: usize, row_bytes: usize)
6972        -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
6973        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
6974        self.q8_fused2_t_core(b0, b1, &aq, &ad, m, in_f, out0, out1, row_bytes)
6975    }
6976
6977    /// BATCHED twin of `matmul_q8_fused3` (wq+wk+wv at verify t=2-4). Same contract as
6978    /// `matmul_q8_fused2_t` with three ranges.
6979    #[allow(clippy::too_many_arguments)]
6980    pub fn matmul_q8_fused3_t(&self, w0: &crate::model::GpuTensor, w1: &crate::model::GpuTensor,
6981                              w2: &crate::model::GpuTensor,
6982                              aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, m: usize)
6983        -> Result<Option<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>)>, Box<dyn std::error::Error>> {
6984        if !(2..=4).contains(&m) || std::env::var("MEMRA_NO_BATCHED").is_ok() { return Ok(None); }
6985        if let Some([p0, p1, p2]) = self.e4m3_fused_params(&[w0, w1, w2]) {
6986            return Ok(Some(self.e4m3_fused3_t_core(p0.0, p1.0, p2.0, aq, ad, m, w0.in_features(),
6987                                                   p0.1, p1.1, p2.1, p0.2,
6988                                                   p0.3, p1.3, p2.3)?));
6989        }
6990        let Some([p0, p1, p2]) = self.q8_fused_params(&[w0, w1, w2]) else { return Ok(None) };
6991        Ok(Some(self.q8_fused3_t_core(p0.0, p1.0, p2.0, aq, ad, m, w0.in_features(),
6992                                      p0.1, p1.1, p2.1, p0.2)?))
6993    }
6994
6995    #[allow(clippy::too_many_arguments)]
6996    fn q8_fused3_t_core(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>, b2: &CudaSlice<u8>,
6997                        aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, m: usize,
6998                        in_f: usize, out0: usize, out1: usize, out2: usize, row_bytes: usize)
6999        -> Result<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7000        const ROWS_PER_BLOCK: u32 = 4;
7001        let nb0 = (out0 as u32).div_ceil(ROWS_PER_BLOCK);
7002        let nb1 = (out1 as u32).div_ceil(ROWS_PER_BLOCK);
7003        let nb2 = (out2 as u32).div_ceil(ROWS_PER_BLOCK);
7004        let f = self.func(if Self::batched_mcols(m) == 2 { "qmatvec_q8_0_mmvq_fused3_b2" }
7005                          else { "qmatvec_q8_0_mmvq_fused3_b4" });
7006        let mut y0 = self.alloc_uninit::<f32>(m * out0)?;
7007        let mut y1 = self.alloc_uninit::<f32>(m * out1)?;
7008        let mut y2 = self.alloc_uninit::<f32>(m * out2)?;
7009        let cfg = LaunchConfig { grid_dim: (nb0 + nb1 + nb2, 1, 1), block_dim: (32, ROWS_PER_BLOCK, 1),
7010                                 shared_mem_bytes: 0 };
7011        let (inf, o0, o1, o2, mi, rbl) = (in_f as i32, out0 as i32, out1 as i32, out2 as i32,
7012                                          m as i32, row_bytes as i64);
7013        let __s_b = self.gpu.stream();
7014        let mut b = __s_b.launch_builder(&f);
7015        b.arg(b0).arg(b1).arg(b2).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1).arg(&mut y2)
7016         .arg(&inf).arg(&o0).arg(&o1).arg(&o2).arg(&mi).arg(&rbl);
7017        unsafe { b.launch(cfg)?; }
7018        Ok((y0, y1, y2))
7019    }
7020
7021    /// Test entry for the kernel_check gate: fused3 batched from raw weight bytes.
7022    #[allow(clippy::too_many_arguments)]
7023    pub fn qmatvec_q8_fused3_t_raw(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>, b2: &CudaSlice<u8>,
7024                                   x: &CudaSlice<f32>, m: usize, in_f: usize, out0: usize,
7025                                   out1: usize, out2: usize, row_bytes: usize)
7026        -> Result<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7027        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
7028        self.q8_fused3_t_core(b0, b1, b2, &aq, &ad, m, in_f, out0, out1, out2, row_bytes)
7029    }
7030
7031    /// Rollback seam for the Q8_0 dense-FFN gate+up fusion arm in `matmul_pre_dual_noscale`
7032    /// (lane/q27-deepdive, 2026-08-05). Default ON; `MEMRA_Q8_FFN_FUSE2=0` restores the
7033    /// two-`matmul_pre_noscale` pair. Read once — the dispatch must not vary within a run.
7034    pub fn q8_ffn_fuse2_on(&self) -> bool {
7035        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
7036        *ON.get_or_init(|| std::env::var("MEMRA_Q8_FFN_FUSE2").as_deref() != Ok("0"))
7037    }
7038
7039    /// Eligibility + param extraction for the fused q8_0 launches: every tensor must be Quant Q8_0
7040    /// with macro-scale 1.0 (always true for GGUF q8_0; only NVFP4 carries scale) and share w[0]'s
7041    /// in_f (q8_0 row_bytes is a pure function of in_f, so equal in_f => equal row_bytes). MEMRA_MMVQ
7042    /// must be on: the fused body is the MMVQ kernel; without it decode m=1 runs dp4a and fusing
7043    /// would mix dispatch families (FP-order law). MEMRA_Q8_DUAL=0 = rollback seam.
7044    #[allow(clippy::type_complexity)]
7045    fn q8_fused_params<'w, const N: usize>(&self, ws: &[&'w crate::model::GpuTensor; N])
7046        -> Option<[(&'w CudaSlice<u8>, usize, usize); N]> {
7047        use crate::model::GpuTensor;
7048        if std::env::var("MEMRA_MMVQ").as_deref() == Ok("0") { return None; }
7049        if std::env::var("MEMRA_Q8_DUAL").is_ok_and(|v| v == "0") { return None; }
7050        let in_f = ws[0].in_features();
7051        let mut out: [Option<(&CudaSlice<u8>, usize, usize)>; N] = [None; N];
7052        for (i, w) in ws.iter().enumerate() {
7053            match w {
7054                GpuTensor::Quant { bytes, qtype, row_bytes, scale, .. }
7055                    if *qtype == QT_Q8_0 && *scale == 1.0 && w.in_features() == in_f =>
7056                        out[i] = Some((bytes, w.out_features(), *row_bytes)),
7057                _ => return None,
7058            }
7059        }
7060        Some(out.map(|o| o.unwrap()))
7061    }
7062
7063    /// Rollback seam for the F8-E4M3 launch-fusion arm (lane/fp8-decode-v1, 2026-08-05).
7064    /// Default ON; `MEMRA_E4M3_DUAL=0` restores the per-tensor m=1/batched launches.
7065    pub fn e4m3_dual_on(&self) -> bool {
7066        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
7067        *ON.get_or_init(|| std::env::var("MEMRA_E4M3_DUAL").as_deref() != Ok("0"))
7068    }
7069
7070    /// Eligibility + param extraction for the FUSED e4m3 launches — the QT_F8_E4M3 twin of
7071    /// `q8_fused_params`. Differences that are inherent to the dtype, not policy:
7072    ///   * each tensor carries its OWN per-tensor `weight_scale` (returned as the 4th field);
7073    ///     Q8_0 hard-requires scale==1.0 because it has no macro-scale at all.
7074    ///   * no MEMRA_MMVQ gate: `mmvq_supports` exempts QT_F8_E4M3 (the e4m3 mmvq family is that
7075    ///     dtype's ONLY int8-act kernel class), so the per-tensor fallback these fused kernels
7076    ///     replace is ALWAYS the same mmvq body under every env — the FP-order law holds.
7077    ///   * `row_bytes == in_f` is asserted rather than derived: the native-residency load arm keeps
7078    ///     the checkpoint's raw [out_f, in_f] rows, and a re-encoded slab must never reach here.
7079    /// Rejects any split-plane mirror (`rp`/`rp4`): there is no `_rp` e4m3 fused form, so fusing
7080    /// there would swap dispatch families mid-model. MEMRA_E4M3_DUAL=0 = rollback seam.
7081    #[allow(clippy::type_complexity)]
7082    fn e4m3_fused_params<'w, const N: usize>(&self, ws: &[&'w crate::model::GpuTensor; N])
7083        -> Option<[(&'w CudaSlice<u8>, usize, usize, f32); N]> {
7084        use crate::model::GpuTensor;
7085        if !self.e4m3_dual_on() { return None; }
7086        let in_f = ws[0].in_features();
7087        let mut out: [Option<(&CudaSlice<u8>, usize, usize, f32)>; N] = [None; N];
7088        for (i, w) in ws.iter().enumerate() {
7089            match w {
7090                GpuTensor::Quant { bytes, qtype, row_bytes, scale, rp, rp4, .. }
7091                    if *qtype == QT_F8_E4M3 && w.in_features() == in_f
7092                        && *row_bytes == in_f && !*rp && rp4.is_none() =>
7093                        out[i] = Some((bytes, w.out_features(), *row_bytes, *scale)),
7094                _ => return None,
7095            }
7096        }
7097        Some(out.map(|o| o.unwrap()))
7098    }
7099
7100    /// FUSED e4m3 m=1 PAIR. Block-offset split (`qmatvec_e4m3_mmvq_fused2`), per-tensor
7101    /// weight_scale folded at the write like the single-tensor `qmatvec_e4m3_mmvq` — so per
7102    /// (tensor,row) this is BIT-IDENTICAL to two separate m=1 launches, scale included.
7103    #[allow(clippy::too_many_arguments)]
7104    fn e4m3_fused2_core(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>,
7105                        aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
7106                        in_f: usize, out0: usize, out1: usize, row_bytes: usize,
7107                        ws0: f32, ws1: f32)
7108        -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7109        const ROWS_PER_BLOCK: u32 = 4;   // matches MEMRA_MMVQ_ROWS in qmatvec.cu
7110        let nb0 = (out0 as u32).div_ceil(ROWS_PER_BLOCK);
7111        let nb1 = (out1 as u32).div_ceil(ROWS_PER_BLOCK);
7112        let f = self.func("qmatvec_e4m3_mmvq_fused2");
7113        let mut y0 = self.alloc_uninit::<f32>(out0)?;
7114        let mut y1 = self.alloc_uninit::<f32>(out1)?;
7115        let cfg = LaunchConfig { grid_dim: (nb0 + nb1, 1, 1), block_dim: (32, ROWS_PER_BLOCK, 1),
7116                                 shared_mem_bytes: 0 };
7117        let (inf, o0, o1, rbl) = (in_f as i32, out0 as i32, out1 as i32, row_bytes as i64);
7118        let __s_b = self.gpu.stream();
7119        let mut b = __s_b.launch_builder(&f);
7120        b.arg(b0).arg(b1).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1)
7121         .arg(&inf).arg(&o0).arg(&o1).arg(&rbl).arg(&ws0).arg(&ws1);
7122        unsafe { b.launch(cfg)?; }
7123        Ok((y0, y1))
7124    }
7125
7126    /// FUSED e4m3 m=1 TRIPLE (`qmatvec_e4m3_mmvq_fused3`). Same contract as the pair.
7127    #[allow(clippy::too_many_arguments)]
7128    fn e4m3_fused3_core(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>, b2: &CudaSlice<u8>,
7129                        aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
7130                        in_f: usize, out0: usize, out1: usize, out2: usize, row_bytes: usize,
7131                        ws0: f32, ws1: f32, ws2: f32)
7132        -> Result<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7133        const ROWS_PER_BLOCK: u32 = 4;
7134        let nb0 = (out0 as u32).div_ceil(ROWS_PER_BLOCK);
7135        let nb1 = (out1 as u32).div_ceil(ROWS_PER_BLOCK);
7136        let nb2 = (out2 as u32).div_ceil(ROWS_PER_BLOCK);
7137        let f = self.func("qmatvec_e4m3_mmvq_fused3");
7138        let mut y0 = self.alloc_uninit::<f32>(out0)?;
7139        let mut y1 = self.alloc_uninit::<f32>(out1)?;
7140        let mut y2 = self.alloc_uninit::<f32>(out2)?;
7141        let cfg = LaunchConfig { grid_dim: (nb0 + nb1 + nb2, 1, 1), block_dim: (32, ROWS_PER_BLOCK, 1),
7142                                 shared_mem_bytes: 0 };
7143        let (inf, o0, o1, o2, rbl) = (in_f as i32, out0 as i32, out1 as i32, out2 as i32,
7144                                      row_bytes as i64);
7145        let __s_b = self.gpu.stream();
7146        let mut b = __s_b.launch_builder(&f);
7147        b.arg(b0).arg(b1).arg(b2).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1).arg(&mut y2)
7148         .arg(&inf).arg(&o0).arg(&o1).arg(&o2).arg(&rbl).arg(&ws0).arg(&ws1).arg(&ws2);
7149        unsafe { b.launch(cfg)?; }
7150        Ok((y0, y1, y2))
7151    }
7152
7153    /// BATCHED FUSED e4m3 pair (m=2..8). The batched kernels carry no `ws` arg (every batched
7154    /// kernel in the tree is scale-free), so each output takes its own `scale_inplace` — the
7155    /// SAME post-op the per-tensor batched dispatch applies, hence still bit-identical.
7156    #[allow(clippy::too_many_arguments)]
7157    fn e4m3_fused2_t_core(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>,
7158                          aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, m: usize,
7159                          in_f: usize, out0: usize, out1: usize, row_bytes: usize,
7160                          ws0: f32, ws1: f32)
7161        -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7162        const ROWS_PER_BLOCK: u32 = 4;
7163        let nb0 = (out0 as u32).div_ceil(ROWS_PER_BLOCK);
7164        let nb1 = (out1 as u32).div_ceil(ROWS_PER_BLOCK);
7165        let f = self.func(match Self::batched_mcols(m) {
7166            2 => "qmatvec_e4m3_mmvq_fused2_b2",
7167            4 => "qmatvec_e4m3_mmvq_fused2_b4",
7168            _ => "qmatvec_e4m3_mmvq_fused2_b8",
7169        });
7170        let mut y0 = self.alloc_uninit::<f32>(m * out0)?;
7171        let mut y1 = self.alloc_uninit::<f32>(m * out1)?;
7172        let cfg = LaunchConfig { grid_dim: (nb0 + nb1, 1, 1), block_dim: (32, ROWS_PER_BLOCK, 1),
7173                                 shared_mem_bytes: 0 };
7174        let (inf, o0, o1, mi, rbl) = (in_f as i32, out0 as i32, out1 as i32, m as i32,
7175                                      row_bytes as i64);
7176        let __s_b = self.gpu.stream();
7177        let mut b = __s_b.launch_builder(&f);
7178        b.arg(b0).arg(b1).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1)
7179         .arg(&inf).arg(&o0).arg(&o1).arg(&mi).arg(&rbl);
7180        unsafe { b.launch(cfg)?; }
7181        if ws0 != 1.0 { self.scale_inplace(&mut y0, ws0, m * out0)?; }
7182        if ws1 != 1.0 { self.scale_inplace(&mut y1, ws1, m * out1)?; }
7183        Ok((y0, y1))
7184    }
7185
7186    /// BATCHED FUSED e4m3 triple (m=2..4). Same contract as the batched pair.
7187    #[allow(clippy::too_many_arguments)]
7188    fn e4m3_fused3_t_core(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>, b2: &CudaSlice<u8>,
7189                          aq: &CudaSlice<i8>, ad: &CudaSlice<f32>, m: usize,
7190                          in_f: usize, out0: usize, out1: usize, out2: usize, row_bytes: usize,
7191                          ws0: f32, ws1: f32, ws2: f32)
7192        -> Result<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7193        const ROWS_PER_BLOCK: u32 = 4;
7194        let nb0 = (out0 as u32).div_ceil(ROWS_PER_BLOCK);
7195        let nb1 = (out1 as u32).div_ceil(ROWS_PER_BLOCK);
7196        let nb2 = (out2 as u32).div_ceil(ROWS_PER_BLOCK);
7197        let f = self.func(if Self::batched_mcols(m) == 2 { "qmatvec_e4m3_mmvq_fused3_b2" }
7198                          else { "qmatvec_e4m3_mmvq_fused3_b4" });
7199        let mut y0 = self.alloc_uninit::<f32>(m * out0)?;
7200        let mut y1 = self.alloc_uninit::<f32>(m * out1)?;
7201        let mut y2 = self.alloc_uninit::<f32>(m * out2)?;
7202        let cfg = LaunchConfig { grid_dim: (nb0 + nb1 + nb2, 1, 1), block_dim: (32, ROWS_PER_BLOCK, 1),
7203                                 shared_mem_bytes: 0 };
7204        let (inf, o0, o1, o2, mi, rbl) = (in_f as i32, out0 as i32, out1 as i32, out2 as i32,
7205                                          m as i32, row_bytes as i64);
7206        let __s_b = self.gpu.stream();
7207        let mut b = __s_b.launch_builder(&f);
7208        b.arg(b0).arg(b1).arg(b2).arg(aq).arg(ad).arg(&mut y0).arg(&mut y1).arg(&mut y2)
7209         .arg(&inf).arg(&o0).arg(&o1).arg(&o2).arg(&mi).arg(&rbl);
7210        unsafe { b.launch(cfg)?; }
7211        if ws0 != 1.0 { self.scale_inplace(&mut y0, ws0, m * out0)?; }
7212        if ws1 != 1.0 { self.scale_inplace(&mut y1, ws1, m * out1)?; }
7213        if ws2 != 1.0 { self.scale_inplace(&mut y2, ws2, m * out2)?; }
7214        Ok((y0, y1, y2))
7215    }
7216
7217    /// BLOCK-128 e4m3 MMVQ launcher (`qmatvec_e4m3_blk_mmvq`, lane/fp8-blk128-decode 2026-08-05).
7218    /// The per-block-dequant twin of `qmatvec_mmvq`'s QT_F8_E4M3 arm: same grid/block decomposition
7219    /// (warp per output row, ROWS_PER_BLOCK warps per block, grid.y = m), same q8_1 activation, but
7220    /// the weight scale is a resident [rows, cols] f32 grid read per k128 block inside the kernel
7221    /// instead of one scalar folded at the write. It cannot share `qmatvec_mmvq`'s body because
7222    /// that launcher's arg list is fixed at (bytes, aq, ad, y, in_f, out_f, m, row_bytes [, scale]).
7223    ///
7224    /// `mr` and `rp` have no analogue here (no split-plane e4m3 layout exists), so there is exactly
7225    /// one kernel and no name table — a shape this cannot serve must be refused at LOAD, not here.
7226    pub fn qmatvec_e4m3_blk_mmvq(&self, bytes: &CudaSlice<u8>, aq: &CudaSlice<i8>,
7227                                 ad: &CudaSlice<f32>, scales: &CudaSlice<f32>,
7228                                 m: usize, in_f: usize, out_f: usize, row_bytes: usize,
7229                                 scale_cols: usize)
7230        -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7231        let mut y = self.alloc_uninit::<f32>(m * out_f)?;   // full-overwrite output: skip memset
7232        self.qmatvec_e4m3_blk_mmvq_into(bytes, aq, ad, scales, m, in_f, out_f, row_bytes,
7233                                        scale_cols, &mut y)?;
7234        Ok(y)
7235    }
7236
7237    /// Slot-fed twin of `qmatvec_e4m3_blk_mmvq` (caller-owned output; the alloc-free capture lane).
7238    #[allow(clippy::too_many_arguments)]
7239    pub fn qmatvec_e4m3_blk_mmvq_into(&self, bytes: &CudaSlice<u8>, aq: &CudaSlice<i8>,
7240                                      ad: &CudaSlice<f32>, scales: &CudaSlice<f32>,
7241                                      m: usize, in_f: usize, out_f: usize, row_bytes: usize,
7242                                      scale_cols: usize, y: &mut CudaSlice<f32>)
7243        -> Result<(), Box<dyn std::error::Error>> {
7244        const ROWS_PER_BLOCK: u32 = 4;   // matches MEMRA_MMVQ_ROWS in qmatvec.cu
7245        let f = self.func("qmatvec_e4m3_blk_mmvq");
7246        let cfg = LaunchConfig {
7247            grid_dim: ((out_f as u32).div_ceil(ROWS_PER_BLOCK), m as u32, 1),
7248            block_dim: (32, ROWS_PER_BLOCK, 1),   // warp-per-row
7249            shared_mem_bytes: 0,                  // warp-only reduce
7250        };
7251        let (inf, outf, mi, rb, sc) =
7252            (in_f as i32, out_f as i32, m as i32, row_bytes as i64, scale_cols as i32);
7253        let __s_b = self.gpu.stream();
7254        let mut b = __s_b.launch_builder(&f);
7255        b.arg(bytes).arg(aq).arg(ad).arg(scales).arg(&mut *y)
7256         .arg(&inf).arg(&outf).arg(&mi).arg(&rb).arg(&sc);
7257        unsafe { b.launch(cfg)?; }
7258        Ok(())
7259    }
7260
7261    /// BLOCK-128 e4m3 BATCHED matvec (lane/rp-on-st, 2026-08-06): the weight-read-once twin of
7262    /// `qmatvec_e4m3_blk_mmvq` for m=2..16. Per (token,row) BIT-IDENTICAL to the grid.y=m launch
7263    /// (same fmaf chain, same per-k32 `s * ad` fold, same warp reduce), so it inherits the
7264    /// decode-exactness contract while reading the weight ONCE for up to `mcols` columns instead
7265    /// of `m` times. `mcols` must be one of {2,4,8,16} and satisfy `mcols >= m`.
7266    #[allow(clippy::too_many_arguments)]
7267    pub fn qmatvec_e4m3_blk_mmvq_batched(&self, bytes: &CudaSlice<u8>, aq: &CudaSlice<i8>,
7268                                         ad: &CudaSlice<f32>, scales: &CudaSlice<f32>,
7269                                         m: usize, in_f: usize, out_f: usize, row_bytes: usize,
7270                                         scale_cols: usize, mcols: usize)
7271        -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7272        const ROWS_PER_BLOCK: u32 = 4;   // matches MEMRA_MMVQ_ROWS in qmatvec.cu
7273        debug_assert!(mcols >= m, "blk batched: mcols {mcols} < m {m}");
7274        let name = match mcols {
7275            2 => "qmatvec_e4m3_blk_mmvq_b2",
7276            4 => "qmatvec_e4m3_blk_mmvq_b4",
7277            8 => "qmatvec_e4m3_blk_mmvq_b8",
7278            16 => "qmatvec_e4m3_blk_mmvq_b16",
7279            _ => return Err(format!("qmatvec_e4m3_blk_mmvq_batched: no kernel for mcols {mcols}").into()),
7280        };
7281        let mut y = self.alloc_uninit::<f32>(m * out_f)?;
7282        let f = self.func(name);
7283        let cfg = LaunchConfig {
7284            grid_dim: ((out_f as u32).div_ceil(ROWS_PER_BLOCK), 1, 1),
7285            block_dim: (32, ROWS_PER_BLOCK, 1),
7286            shared_mem_bytes: 0,
7287        };
7288        let (inf, outf, mi, rb, sc) =
7289            (in_f as i32, out_f as i32, m as i32, row_bytes as i64, scale_cols as i32);
7290        let __s_b = self.gpu.stream();
7291        let mut b = __s_b.launch_builder(&f);
7292        b.arg(bytes).arg(aq).arg(ad).arg(scales).arg(&mut y)
7293         .arg(&inf).arg(&outf).arg(&mi).arg(&rb).arg(&sc);
7294        unsafe { b.launch(cfg)?; }
7295        Ok(y)
7296    }
7297
7298    /// Test entry for the kernel_check exactness gate: the block-128 e4m3 batched MMVQ from raw
7299    /// bytes with an internal q8_1 quantize (mirrors `qmatvec_batched_raw`).
7300    #[allow(clippy::too_many_arguments)]
7301    pub fn qmatvec_e4m3_blk_batched_raw(&self, bytes: &CudaSlice<u8>, x: &CudaSlice<f32>,
7302                                        scales: &CudaSlice<f32>, m: usize, in_f: usize,
7303                                        out_f: usize, row_bytes: usize, scale_cols: usize,
7304                                        mcols: usize)
7305        -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7306        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
7307        self.qmatvec_e4m3_blk_mmvq_batched(bytes, &aq, &ad, scales, m, in_f, out_f, row_bytes,
7308                                           scale_cols, mcols)
7309    }
7310
7311    /// Test entry for the kernel_check exactness gate: the block-128 e4m3 MMVQ from raw bytes with
7312    /// an internal q8_1 quantize (mirrors `qmatvec_mmvq_raw`).
7313    #[allow(clippy::too_many_arguments)]
7314    pub fn qmatvec_e4m3_blk_mmvq_raw(&self, bytes: &CudaSlice<u8>, x: &CudaSlice<f32>,
7315                                     scales: &CudaSlice<f32>, m: usize, in_f: usize, out_f: usize,
7316                                     row_bytes: usize, scale_cols: usize)
7317        -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7318        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
7319        self.qmatvec_e4m3_blk_mmvq(bytes, &aq, &ad, scales, m, in_f, out_f, row_bytes, scale_cols)
7320    }
7321
7322    /// Test entries for the kernel_check bit-parity gate: fused e4m3 launches from raw weight
7323    /// bytes with internal q8_1 quantize, no env gating (mirrors `qmatvec_q8_fused*_raw`).
7324    #[allow(clippy::too_many_arguments)]
7325    pub fn qmatvec_e4m3_fused2_raw(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>, x: &CudaSlice<f32>,
7326                                   in_f: usize, out0: usize, out1: usize, row_bytes: usize,
7327                                   ws0: f32, ws1: f32)
7328        -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7329        let (aq, ad) = self.quantize_q8_1(x, 1, in_f)?;
7330        self.e4m3_fused2_core(b0, b1, &aq, &ad, in_f, out0, out1, row_bytes, ws0, ws1)
7331    }
7332
7333    #[allow(clippy::too_many_arguments)]
7334    pub fn qmatvec_e4m3_fused3_raw(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>, b2: &CudaSlice<u8>,
7335                                   x: &CudaSlice<f32>, in_f: usize, out0: usize, out1: usize,
7336                                   out2: usize, row_bytes: usize, ws0: f32, ws1: f32, ws2: f32)
7337        -> Result<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7338        let (aq, ad) = self.quantize_q8_1(x, 1, in_f)?;
7339        self.e4m3_fused3_core(b0, b1, b2, &aq, &ad, in_f, out0, out1, out2, row_bytes,
7340                              ws0, ws1, ws2)
7341    }
7342
7343    #[allow(clippy::too_many_arguments)]
7344    pub fn qmatvec_e4m3_fused2_t_raw(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>,
7345                                     x: &CudaSlice<f32>, m: usize, in_f: usize, out0: usize,
7346                                     out1: usize, row_bytes: usize, ws0: f32, ws1: f32)
7347        -> Result<(CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7348        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
7349        self.e4m3_fused2_t_core(b0, b1, &aq, &ad, m, in_f, out0, out1, row_bytes, ws0, ws1)
7350    }
7351
7352    #[allow(clippy::too_many_arguments)]
7353    pub fn qmatvec_e4m3_fused3_t_raw(&self, b0: &CudaSlice<u8>, b1: &CudaSlice<u8>,
7354                                     b2: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize,
7355                                     in_f: usize, out0: usize, out1: usize, out2: usize,
7356                                     row_bytes: usize, ws0: f32, ws1: f32, ws2: f32)
7357        -> Result<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
7358        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
7359        self.e4m3_fused3_t_core(b0, b1, b2, &aq, &ad, m, in_f, out0, out1, out2, row_bytes,
7360                                ws0, ws1, ws2)
7361    }
7362
7363    /// THE single dispatch point for `QT_F8_E4M3_BLK` from a PRE-QUANTIZED q8_1 activation
7364    /// (lane/fp8-blk128-decode). Every `matmul_pre`-family entry calls this first, so the block-128
7365    /// class has exactly ONE code path across `matmul`, `matmul_pre`, `matmul_pre_noscale`,
7366    /// `matmul_decode_exact` and `matmul_decode_exact_pre` — the same kernel at the same grid for
7367    /// every m, which is what makes verify == decode bit-for-bit at every tier for free.
7368    ///
7369    /// Returns None for any other qtype (the caller continues its normal dispatch). The `blk: Some`
7370    /// pattern is part of the match, not an unwrap: qtype and grid presence are set together in the
7371    /// one residency arm that builds this tensor, and a qtype-without-grid would be a construction
7372    /// bug — better to fall through and hit a loud refusal than to unwrap a None here.
7373    fn try_e4m3_blk_pre(&self, w: &crate::model::GpuTensor, aq: &CudaSlice<i8>,
7374                        ad: &CudaSlice<f32>, m: usize)
7375        -> Result<Option<CudaSlice<f32>>, Box<dyn std::error::Error>> {
7376        use crate::model::GpuTensor;
7377        if let GpuTensor::Quant { bytes, qtype, row_bytes, blk: Some(g), .. } = w {
7378            if *qtype == QT_F8_E4M3_BLK {
7379                // BATCHED tier m=2..16 (lane/rp-on-st): weight read ONCE for up to mcols columns
7380                // instead of m grid.y re-reads. Bit-identical per (token,row) to the grid.y=m form
7381                // below, so the decode-exactness contract is preserved at every width. Gated by
7382                // the same seams the other batched families honor (MEMRA_NO_BATCHED, MEMRA_B8) so
7383                // one rollback door covers every dtype's batched tier.
7384                if (2..=16).contains(&m) && std::env::var("MEMRA_NO_BATCHED").is_err()
7385                    && (m <= 4 || Self::b8_enabled()) {
7386                    let mcols = Self::batched_mcols(m);
7387                    return Ok(Some(self.qmatvec_e4m3_blk_mmvq_batched(
7388                        bytes, aq, ad, &g.scales, m, w.in_features(), w.out_features(),
7389                        *row_bytes, g.cols, mcols)?));
7390                }
7391                return Ok(Some(self.qmatvec_e4m3_blk_mmvq(
7392                    bytes, aq, ad, &g.scales, m, w.in_features(), w.out_features(),
7393                    *row_bytes, g.cols)?));
7394            }
7395        }
7396        Ok(None)
7397    }
7398
7399    /// PREFILL (m >= GEMM_M_THRESHOLD) for `QT_F8_E4M3_BLK` — DEQUANT-PER-CALL to the Q8_0 slab
7400    /// this class's residency replaced, then the ordinary Q8_0 prefill dispatch on the transient.
7401    ///
7402    /// WHY THIS EXISTS AT ALL, i.e. the regression it prevents: the decode kernel is a warp-per-row
7403    /// GEMV. At grid.y=m it re-reads the whole weight once PER TOKEN, so letting a 512-token prefill
7404    /// chunk reach it would be a ~500x weight-traffic blowup on the single most bandwidth-bound part
7405    /// of the forward. Native residency is a DECODE win and must not be paid for in prefill, so
7406    /// prefill keeps the floor's arithmetic and the floor's kernels.
7407    ///
7408    /// WHY DEQUANT-PER-CALL rather than a second resident slab: a resident slab is dual residency —
7409    /// it gives back the entire 1.0-vs-1.0625 B/weight win this lane exists to capture (and then
7410    /// some, since the e4m3 copy stays too). The transient costs one linear device pass per
7411    /// (projection, prefill call) and frees immediately.
7412    ///
7413    /// NUMERICALLY IT IS THE FLOOR, EXACTLY: `fp8_blk_dequant_q8_0` is the merged ARM B' kernel,
7414    /// gate-proven BYTE-IDENTICAL to the host dequant+re-encode (kernel-check `fp8-blk-gpu`). So the
7415    /// slab these bytes form is bit-for-bit the slab the `MEMRA_ST_E4M3_BLK=0` arm makes resident,
7416    /// and every prefill kernel downstream sees identical input — prefill logits under this lane are
7417    /// bit-identical to prefill logits under the floor, which is what makes the decode A/B a clean
7418    /// single-variable comparison instead of a two-variable one.
7419    ///
7420    /// WHAT IT COSTS, MEASURED, AND WHY THAT COST IS MOSTLY STRUCTURAL (27B block-128 ckpt, pp512,
7421    /// this rig = RTX 5090 Laptop, ~896 GB/s GDDR7). This arm makes prefill move the weight THREE
7422    /// times instead of once: read 6.88 GB of e4m3, write 7.31 GB of Q8_0, then the MMQ reads that
7423    /// 7.31 GB back. The two extra passes are 14.19 GB = 15.8 ms at this card's roofline against a
7424    /// ~332 ms pp512, i.e. **~-4.5% pp is a floor no kernel tuning can remove** — only deleting the
7425    /// dequant can. Measured: the dequant kernel costs 27.9 ms/pass (nsys, 208 projections) after
7426    /// the 2026-08-05 vector rewrite (was 66.5 ms at one byte per thread), and e2e pp512 is
7427    /// 1451.4 vs the slab arm's 1541.6 tok/s = -5.8% (N=3 interleaved pairs). So ~1.3pp of the
7428    /// -5.8% is residual kernel inefficiency and ~4.5pp is the extra traffic itself.
7429    ///
7430    /// SO THE DEQUANT IS NO LONGER THE DEFAULT ROUTE — it is the FALLBACK. The per-block FP8 MMQ
7431    /// tile (`try_fp8_blk_mmq`) consumes the resident e4m3 bytes + grid DIRECTLY, deleting both extra
7432    /// passes, and since 2026-08-05 it runs FIRST and by default for the native-resident source
7433    /// (`fp8_blk_mmq_native_enabled`; `MEMRA_FP8_MMQ=0` is the seam back to this dequant). On paper
7434    /// the trade was unassumable — lane/fp8-mmq-v2 measured that tile at 0.85-1.09x the Q8_0 MMQ
7435    /// floor GEMM-only, so it swapped a -4.5% traffic cost for a 0-to-15% GEMM cost of unknown sign.
7436    /// Measured on the 27B (3 arms interleaved, N=3, research/fp8blk-20260805/VERDICT.md): slab
7437    /// 1540.5 / this dequant 1449.1 / the tile 1553.3 tok/s, min(tile) > max(slab). The tile wins
7438    /// because v2's denominator had its slab already resident while this class's floor must build it
7439    /// every call; same tile, opposite sign, because the question changed.
7440    ///
7441    /// THIS ARM STILL RUNS, and is not dead code: every `try_fp8_blk_mmq` precondition (in_f % 16,
7442    /// grid dims vs shape, per-tensor scale == 1.0, the e4m3-NaN scan) refuses by falling through to
7443    /// here, so a checkpoint the tile cannot take keeps exact prefill on the floor's own bits rather
7444    /// than losing the class. It is also what `MEMRA_FP8_MMQ=0` reverts to.
7445    fn try_e4m3_blk_prefill(&self, w: &crate::model::GpuTensor, x: &CudaSlice<f32>, m: usize)
7446        -> Result<Option<CudaSlice<f32>>, Box<dyn std::error::Error>> {
7447        use crate::model::GpuTensor;
7448        let GpuTensor::Quant { bytes, qtype, blk: Some(g), .. } = w else { return Ok(None) };
7449        if *qtype != QT_F8_E4M3_BLK { return Ok(None) }
7450        // NO-DEQUANT ROUTE, THE DEFAULT (MEMRA_FP8_MMQ=0 reverts): the per-block MMQ tile eats the
7451        // resident e4m3 bytes and grid as-is, so neither extra weight pass happens. Its own
7452        // preconditions (in_f % 16, grid dims, scale == 1.0, no e4m3 NaN code) can refuse — fall
7453        // through to the dequant below when they do, never silently produce nothing.
7454        if let Some(y) = self.try_fp8_blk_mmq(w, x, m)? { return Ok(Some(y)); }
7455        let (in_f, out_f) = (w.in_features(), w.out_features());
7456        let slab = self.fp8_blk_dequant_q8_0_dev(bytes, &g.scales, out_f, in_f)?;
7457        let tmp = GpuTensor::Quant {
7458            bytes: slab,
7459            qtype: QT_Q8_0,
7460            row_bytes: in_f / 32 * 34,
7461            ne: vec![in_f as u64, out_f as u64],
7462            scale: 1.0,
7463            rp: false,
7464            #[cfg(memra_cutlass)]
7465            cutlass: None,
7466            fp8: None, blk: None, f16: None, rp4: None,
7467        };
7468        // Recursion terminates: `tmp` is QT_Q8_0 with `blk: None`, so it cannot re-enter this arm.
7469        Ok(Some(self.matmul(&tmp, x, m)?))
7470    }
7471
7472    pub fn matmul_pre_noscale(&self, w: &crate::model::GpuTensor, aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
7473                              m: usize) -> Result<Option<(CudaSlice<f32>, f32)>, Box<dyn std::error::Error>> {
7474        use crate::model::GpuTensor;
7475        // BLOCK-128 e4m3: every scale factor is folded inside the kernel per k128, so the
7476        // "separable post-op scale" this entry exists to defer is 1.0 — return it explicitly
7477        // rather than let the tail below refuse and cost the caller a re-dispatch.
7478        if m == 1 {
7479            if let Some(y) = self.try_e4m3_blk_pre(w, aq, ad, m)? { return Ok(Some((y, 1.0))); }
7480        }
7481        // Only the m==1 fast path applies the scale as a separable post-op; bail everywhere else.
7482        if m != 1 || !self.uses_q8_1_fast(w) { return Ok(None); }
7483        let in_f = w.in_features();
7484        let out_f = w.out_features();
7485        let (bytes, qtype, row_bytes, scale, rp) = match w {
7486            GpuTensor::Quant { bytes, qtype, row_bytes, scale, rp, .. } => (bytes, *qtype, *row_bytes, *scale, *rp),
7487            _ => return Ok(None),
7488        };
7489        // MMVQ warp-per-row (scale==1.0 passed -> kernel skips its internal scale; we return scale).
7490        if self.mmvq_supports(qtype) {
7491            // Q4_0 split-plane mirror (dp4a fallback below keeps the raw GGUF bytes).
7492            let (mbytes, mrp) = match w {
7493                GpuTensor::Quant { rp4: Some(m4), .. } => (m4, true),
7494                _ => (bytes, rp),
7495            };
7496            let y = self.qmatvec_mmvq(mbytes, aq, ad, m, in_f, out_f, qtype, row_bytes, /*scale*/ 1.0, mrp)?;
7497            return Ok(Some((y, scale)));
7498        }
7499        // dp4a fallback: same launch as matmul_pre but WITHOUT the post scale_inplace.
7500        let name = match qtype {
7501            QT_Q8_0 => "qmatvec_q8_0_dp4a", QT_Q4_K => "qmatvec_q4_K_dp4a",
7502            QT_Q6_K => "qmatvec_q6_K_dp4a", QT_Q5_K => "qmatvec_q5_K_dp4a",
7503            QT_Q3_K => "qmatvec_q3_K_dp4a",
7504            QT_NVFP4 => if rp { "qmatvec_nvfp4_dp4a_rp" } else { "qmatvec_nvfp4_dp4a" },
7505            QT_IQ4_XS => "qmatvec_iq4_XS_dp4a",
7506            _ => return Ok(None),
7507        };
7508        let f = self.func(name);
7509        let mut y = self.alloc_uninit::<f32>(m * out_f)?;
7510        let cfg = LaunchConfig { grid_dim: (out_f as u32, m as u32, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
7511        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
7512        let __s_b = self.gpu.stream();
7513        let mut b = __s_b.launch_builder(&f);
7514        b.arg(bytes).arg(aq).arg(ad).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
7515        unsafe { b.launch(cfg)?; }
7516        Ok(Some((y, scale)))
7517    }
7518
7519    /// True if `qtype` has a warp-per-row MMVQ decode kernel AND MEMRA_MMVQ is set. Only the 4
7520    /// daily-hot dtypes (Q8_0, Q4_K, Q6_K, NVFP4) — others keep the _dp4a matvec (oracle/fallback).
7521    pub fn mmvq_supports(&self, qtype: i32) -> bool {
7522        // DEFAULT ON since 2026-07-08 (MEMRA_MMVQ=0 reverts to the _dp4a matvec class).
7523        // QT_F8_E4M3 is exempt from the MEMRA_MMVQ=0 escape: the e4m3 mmvq family is that dtype's
7524        // ONLY int8-act kernel class (there is no _dp4a twin), so its m=1/verify/batched dispatch
7525        // is a pure function of the dtype — the decode-parity law holds under every env.
7526        if qtype == QT_F8_E4M3 { return true; }
7527        if std::env::var("MEMRA_MMVQ").as_deref() == Ok("0") { return false; }
7528        matches!(qtype, QT_Q8_0 | QT_Q4_K | QT_Q5_K | QT_Q6_K | QT_NVFP4 | QT_Q4_0)
7529    }
7530
7531    /// PERF-3 warp-per-row MMVQ launcher (decode m=1 hot path). block=(32,ROWS_PER_BLOCK,1):
7532    /// one warp owns one output row, warp-only __shfl reduction (no smem barrier). Bit-equivalent
7533    /// to qmatvec_*_dp4a up to f32 reduction order. Pre-quantized q8_1 activation (aq,ad). NVFP4
7534    /// per-tensor macro-scale applied post (scale==1.0 for other dtypes -> no-op).
7535    pub fn qmatvec_mmvq(&self, bytes: &CudaSlice<u8>, aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
7536                        m: usize, in_f: usize, out_f: usize, qtype: i32, row_bytes: usize, scale: f32,
7537                        rp: bool)
7538                        -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7539        let mut y = self.alloc_uninit::<f32>(m * out_f)?;  // full-overwrite GEMM output: skip memset
7540        self.qmatvec_mmvq_into(bytes, aq, ad, m, in_f, out_f, qtype, row_bytes, scale, rp, &mut y)?;
7541        Ok(y)
7542    }
7543
7544    /// Slot-fed MMVQ twin (alloc-free capture lane): full policy body, caller-owned output.
7545    #[allow(clippy::too_many_arguments)]
7546    pub fn qmatvec_mmvq_into(&self, bytes: &CudaSlice<u8>, aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
7547                        m: usize, in_f: usize, out_f: usize, qtype: i32, row_bytes: usize, scale: f32,
7548                        rp: bool, y: &mut CudaSlice<f32>)
7549                        -> Result<(), Box<dyn std::error::Error>> {
7550        debug_assert!(y.len() >= m * out_f);
7551        const ROWS_PER_BLOCK: u32 = 4;   // matches MEMRA_MMVQ_ROWS in qmatvec.cu
7552        // SMALL-SHAPE GRID FILL (H100 lane, 2026-07-26 microbench: attn qkv out_f=2048 =
7553        // 0.97 waves at the 4-warp block -> 66% of peak). The g2 twin (2 warps/block)
7554        // doubles the grid when the 4-warp launch would be sub-wave; per-row program
7555        // identical -> bit-identical. MEMRA_Q80_G2=0 reverts.
7556        if qtype == QT_Q8_0 && rp && m == 1 && out_f >= 64
7557            && (out_f as u32).div_ceil(ROWS_PER_BLOCK) < 4 * self.sm_count() as u32
7558            && {
7559                static G2: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
7560                *G2.get_or_init(|| std::env::var("MEMRA_Q80_G2").as_deref() != Ok("0"))
7561            }
7562        {
7563            let f = self.func("qmatvec_q8_0_mmvq_rp_g2");
7564            let cfg = LaunchConfig {
7565                grid_dim: ((out_f as u32).div_ceil(2), 1, 1),
7566                block_dim: (32, 2, 1),
7567                shared_mem_bytes: 0,
7568            };
7569            let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, 1i32, row_bytes as i64);
7570            let __s_b = self.gpu.stream();
7571            let mut b = __s_b.launch_builder(&f);
7572            b.arg(bytes).arg(aq).arg(ad).arg(&mut *y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
7573            unsafe { b.launch(cfg)?; }
7574            if scale != 1.0 { self.scale_inplace(y, scale, out_f)?; }
7575            return Ok(());
7576        }
7577        // Multi-row-per-warp (mr2) policy, fixed since the 2026-07 sweeps (the MEMRA_MMVQ_MR
7578        // override + mr4 kernel were retired 2026-07-08 — mr4 regressed on register pressure and
7579        // crashed under rp; q4_K/q6_K mr2 measured flat, "no gain = no change"):
7580        //   NVFP4 m=1 -> mr2 (clean +1-2% on 9B: RPW acc chains hide the weight-load latency
7581        //     that pins the single-row kernel at 30-46% DRAM). Bit-identical per row.
7582        //   Q5_K m=1 -> mr2 (2026-07-05: the FR-Spec trimmed draft head is Q5_K 32768 rows = 8%
7583        //     of the 27B p3 spec wall; latency-bound like the other k-quants pre-fix).
7584        //   Q4_K/Q6_K m=1 -> single-row (mr2 measured +0.7% / flat — weight-bandwidth-bound).
7585        let mut mr: u32 = if m == 1 && (qtype == QT_NVFP4 || qtype == QT_Q5_K) { 2 } else { 1 };
7586        // Q4_0 mr (gemma trunk): DEFAULT 1 since 2026-07-13 (MEMRA_Q40_MR=2 reverts) — the
7587        // mr1 rp twin doubles the block count and wins the tail-quantization/latency battle
7588        // on every gemma model (E4B +3.75%: 198.9 vs 191.7; 26B +0.7%; 31B +0.9%; N=2-3
7589        // valid-window interleaved, bit-identical per row — same dot program).
7590        if m == 1 && qtype == QT_Q4_0 {
7591            static Q40MR: std::sync::OnceLock<u32> = std::sync::OnceLock::new();
7592            // shape policy PROBED NEGATIVE (2026-07-13): tall-only mr1 197.2 vs
7593            // mr1-everywhere 198.7 — mr1 wins wide-output shapes too; arm removed.
7594            mr = *Q40MR.get_or_init(|| std::env::var("MEMRA_Q40_MR").ok()
7595                .and_then(|v| v.parse().ok()).unwrap_or(1));
7596        }
7597        // q5issue lane (2026-07-08): MEMRA_Q5K_ISSUE swaps the q5_K m=1 mmvq kernels for the
7598        // issue-reduced `_il` bodies (uint4 header/qh/qs loads + branchless scale decode —
7599        // cuts ~34 LDG.U16 + ~5 LDG.U8 + a warp-divergent scale branch per 32-elem group-row
7600        // to 5 LDG.128). Bit-identical per (token,row) to the reference kernels.
7601        // `1` = shape-aware policy (N=3 clock-locked micro-bench, mem P0, synthetic real shapes):
7602        //   out_f <= 65536 (trunk/frspec regime): il at the default mr — mr2_il -9.5%/-10.5%
7603        //     on 4096x4096/4096x8192, -3.1% on the 32768 frspec head vs the mr2-ref default;
7604        //   out_f > 65536 (the 248320-row 27B lm_head, already ~97% of the mem wall): mr2_il
7605        //     REGRESSES +22% there but mr1_il wins -2.1% vs the mr2-ref default -> force mr=1.
7606        // `2` = force il at the current mr for EVERY shape (A/B probe seam). Default OFF.
7607        let q5_mode = std::env::var("MEMRA_Q5K_ISSUE").ok();
7608        let q5_force = q5_mode.as_deref() == Some("2");
7609        // DEFAULT ON since 2026-07-08 (MEMRA_Q5K_ISSUE=0 reverts): +1.8% 9B plain e2e N=3
7610        // (128.2 -> 130.4), 27B flat (its big head is already at the mem wall), all gates green.
7611        let q5_il = qtype == QT_Q5_K && m == 1
7612            && (q5_force || q5_mode.as_deref().map(|v| v != "0").unwrap_or(true));
7613        if q5_il && !q5_force && out_f > 65536 { mr = 1; }
7614        // Q4_0 split-plane rp: mr2 default; MEMRA_Q40_MR=1 reaches the mr1 rp twin
7615        // (2026-07-13 — the tall-input/short-output tail-quantization probe).
7616        if qtype == QT_Q4_0 && rp && mr != 1 { mr = 2; }
7617        // Q8_0 rp (H100 lane): mr1 default — the q4_0 mr2 recipe MEASURED NEGATIVE on H100
7618        // (2026-07-26 N=3: mr1 186.2 vs mr2 171.5 tok/s; halving the grid on 132 SMs costs
7619        // more than 2-row ILP buys). mr2 kernel stays behind MEMRA_Q80_MR=2 for the corpus.
7620        if qtype == QT_Q8_0 && rp {
7621            static Q80MR: std::sync::OnceLock<u32> = std::sync::OnceLock::new();
7622            mr = *Q80MR.get_or_init(|| std::env::var("MEMRA_Q80_MR").ok()
7623                .and_then(|v| v.parse().ok()).unwrap_or(1));
7624        }
7625        let name = match (qtype, mr, rp) {
7626            (QT_NVFP4, 2, false) => "qmatvec_nvfp4_mmvq_mr2",
7627            (QT_NVFP4, 2, true)  => "qmatvec_nvfp4_mmvq_mr2_rp",
7628            (QT_NVFP4, _, true)  => "qmatvec_nvfp4_mmvq_rp",
7629            (QT_Q4_0, 1, true)   => "qmatvec_q4_0_mmvq_rp",
7630            (QT_Q4_0, _, true)   => "qmatvec_q4_0_mmvq_mr2_rp",
7631            (QT_Q5_K, 2, _) => if q5_il { "qmatvec_q5_K_mmvq_mr2_il" } else { "qmatvec_q5_K_mmvq_mr2" },
7632            (QT_Q8_0, 2, true) => "qmatvec_q8_0_mmvq_mr2_rp",
7633            // rpca (cp.async-staged weight ring): MEASURED NEGATIVE on H100 for Q8_0
7634            // (2026-07-26 N=3: 181.8 vs plain rp 185.5 — the smem round-trip exceeds the
7635            // latency it hides for 8-bit direct-dp4a; the NVFP4 win case overlaps table
7636            // decode with half the bytes). OPT-IN via MEMRA_Q80_CA=1 for the corpus.
7637            (QT_Q8_0, _, true) if in_f % 1024 == 0 && {
7638                static CA: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
7639                *CA.get_or_init(|| std::env::var("MEMRA_Q80_CA").as_deref() == Ok("1"))
7640            } => "qmatvec_q8_0_mmvq_rpca",
7641            (QT_Q8_0, _, true) => "qmatvec_q8_0_mmvq_rp",
7642            (QT_Q8_0, _, _) => "qmatvec_q8_0_mmvq",
7643            // K-quant split-plane twins (H100 K-quant coalescing fix, 2026-08-01): the rp4
7644            // mirror routes here; GGUF layout keeps the plain kernels. rp bytes MUST never
7645            // reach a GGUF-layout kernel or vice versa.
7646            (QT_Q4_K, _, true) => "qmatvec_q4_K_mmvq_rp",
7647            (QT_Q6_K, _, true) => "qmatvec_q6_K_mmvq_rp",
7648            (QT_Q4_K, _, _) => "qmatvec_q4_K_mmvq",
7649            (QT_Q4_0, 2, false) => "qmatvec_q4_0_mmvq_mr2",
7650            (QT_Q4_0, _, false) => "qmatvec_q4_0_mmvq",
7651            (QT_Q5_K, _, _) => if q5_il { "qmatvec_q5_K_mmvq_il" } else { "qmatvec_q5_K_mmvq" },
7652            (QT_Q6_K, _, _) => "qmatvec_q6_K_mmvq",
7653            (QT_NVFP4, _, false) => "qmatvec_nvfp4_mmvq",
7654            (QT_F8_E4M3, _, _) => "qmatvec_e4m3_mmvq",
7655            _ => panic!("qmatvec_mmvq: qtype {qtype} has no MMVQ kernel"),
7656        };
7657        let f = self.func(name);
7658        // each block still has ROWS_PER_BLOCK warps; with mr rows/warp it covers ROWS_PER_BLOCK*mr rows.
7659        let rows_per_block = ROWS_PER_BLOCK * mr;
7660        let cfg = LaunchConfig {
7661            grid_dim: ((out_f as u32 + rows_per_block - 1) / rows_per_block, m as u32, 1),
7662            block_dim: (32, ROWS_PER_BLOCK, 1),   // warp-per-row (x mr rows each)
7663            shared_mem_bytes: 0,                  // warp-only reduce at m=1
7664        };
7665        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
7666        let __s_b = self.gpu.stream();
7667        let mut b = __s_b.launch_builder(&f);
7668        // NVFP4 + e4m3 mmvq kernels take the macro-scale as a fused epilogue arg (applied at the
7669        // write — bit-identical to the old separate scale_inplace pass, minus one launch per matvec:
7670        // 53 scale launches/token on the 9B; for e4m3 the scale is the checkpoint's per-tensor f32
7671        // weight_scale). Other mmvq kernels keep the 8-arg signature.
7672        if qtype == QT_NVFP4 || qtype == QT_F8_E4M3 {
7673            b.arg(bytes).arg(aq).arg(ad).arg(&mut *y).arg(&inf).arg(&outf).arg(&mi).arg(&rb).arg(&scale);
7674            unsafe { b.launch(cfg)?; }
7675        } else if Self::pdl_on() && Self::pdl_mmvq_on()
7676            && matches!(name, "qmatvec_q4_0_mmvq_rp" | "qmatvec_q6_K_mmvq"
7677                              | "qmatvec_q6_K_mmvq_rp") {
7678            // PDL wave-A (2026-07-23): the two decode-hot single-matvec kernels carry
7679            // MEMRA_PDL_ENTRY — grid launches while the producer drains. ONLY the marked
7680            // names may take this launch (unmarked kernels would read unordered).
7681            {
7682            use cudarc::driver::{DevicePtr, DevicePtrMut};
7683            let s = &self.gpu.stream();
7684            let (pw, _g0) = bytes.device_ptr(s); let (paq, _g1) = aq.device_ptr(s);
7685            let (pad, _g2) = ad.device_ptr(s); let (py, _g3) = y.device_ptr_mut(s);
7686            let mut ps = [
7687                &pw as *const _ as *mut std::ffi::c_void, &paq as *const _ as *mut _,
7688                &pad as *const _ as *mut _, &py as *const _ as *mut _,
7689                &inf as *const _ as *mut _, &outf as *const _ as *mut _,
7690                &mi as *const _ as *mut _, &rb as *const _ as *mut _,
7691            ];
7692            unsafe { self.launch_pdl(name, cfg.grid_dim, cfg.block_dim, &mut ps)?; }
7693            }
7694            if scale != 1.0 { self.scale_inplace(y, scale, m * out_f)?; }
7695        } else {
7696            b.arg(bytes).arg(aq).arg(ad).arg(&mut *y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
7697            unsafe { b.launch(cfg)?; }
7698            if scale != 1.0 { self.scale_inplace(y, scale, m * out_f)?; }
7699        }
7700        Ok(())
7701    }
7702
7703    /// Test entry for the kernel_check bit-equivalence gate: run the warp-per-row MMVQ directly
7704    /// from raw weight bytes (quantize the f32 activation `x` to q8_1 internally). NVFP4 per-tensor
7705    /// macro-scale is NOT applied (caller compares bare, like qmatvec_*_fast). Mirrors qmatvec_gemm_raw.
7706    pub fn qmatvec_mmvq_raw(&self, bytes: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize, in_f: usize,
7707                            out_f: usize, qtype: i32, row_bytes: usize, rp: bool)
7708                            -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
7709        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
7710        self.qmatvec_mmvq(bytes, &aq, &ad, m, in_f, out_f, qtype, row_bytes, 1.0, rp)
7711    }
7712
7713    /// True if `qtype` has a batched weight-resident (`_b2`/`_b4`) matvec kernel. These mirror the
7714    /// `_mmvq` kernels but iterate the m token columns INSIDE one warp/row, so the weight bytes leave
7715    /// HBM/L2 once for m tokens (vs grid.y=m re-reading m times). The 5 daily-hot dtypes have them.
7716    pub fn batched_supports(&self, qtype: i32) -> bool {
7717        matches!(qtype, QT_Q8_0 | QT_Q4_K | QT_Q5_K | QT_Q6_K | QT_NVFP4 | QT_F8_E4M3 | QT_Q4_0)
7718    }
7719
7720    /// IQ4_XS trunk fast seam: MEMRA_IQ_FAST=0 reverts non-expert IQ4_XS matmuls to the Stage-A
7721    /// f32 oracle path. Default ON since 2026-08-02 (research/kat-anomaly-20260802/): the old
7722    /// opt-in default left every IQ4_XS-trunk artifact (KAT-Coder IQ4_XS: attn_qkv/attn_gate/
7723    /// ssm_out/shexp, ~0.52GB re-read per decode tick) on the oracle kernel — decode 106.7 ->
7724    /// 193.4 tok/s (x5 interleaved), pp512 228 -> 697, same bytes, via qmatvec_iq4_XS_dp4a. The
7725    /// supported artifacts carry IQ4_XS only in EXPERT banks (their own dispatch, not this seam),
7726    /// so this admission is dispatch-unchanged for every non-IQ4_XS-trunk model.
7727    pub fn iq_fast_enabled() -> bool {
7728        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
7729        *ON.get_or_init(|| std::env::var("MEMRA_IQ_FAST").map(|v| v != "0").unwrap_or(true))
7730    }
7731
7732    /// b8 tier seam: MEMRA_B8=0 keeps m=5..8 on the per-m grid.y=m path (m=2..4 batched dispatch
7733    /// unaffected). Default ON — the K=4..7 spec-verify weight-read-once fix.
7734    pub fn b8_enabled() -> bool {
7735        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
7736        *ON.get_or_init(|| std::env::var("MEMRA_B8").map(|v| v != "0").unwrap_or(true))
7737    }
7738
7739    /// Compile-time column batch for a runtime m: 2 -> b2, 3..4 -> b4, 5..8 -> b8.
7740    pub fn batched_mcols(m: usize) -> usize {
7741        if m == 2 { 2 } else if m <= 4 { 4 } else if m <= 8 { 8 } else { 16 }
7742    }
7743
7744    /// Kernel name for the batched matvec of `(qtype, mcols)`. mcols ∈ {2,4,8}. The b8 tier is the
7745    /// K=4..7 spec-verify fix (T=5..8): pre-b8 those T fell to grid.y=m per-row MMVQ = m full
7746    /// weight reads/launch — the measured 27B K=4 cliff (101 -> 73 tok/s at p3 despite acceptance
7747    /// holding 54%). One b8 launch reads the weight ONCE for up to 8 columns (c >= m masked).
7748    fn batched_kernel_name(qtype: i32, mcols: usize) -> Option<&'static str> {
7749        Some(match (qtype, mcols) {
7750            (QT_Q8_0, 2) => "qmatvec_q8_0_mmvq_b2", (QT_Q8_0, 4) => "qmatvec_q8_0_mmvq_b4",
7751            (QT_Q8_0, 8) => "qmatvec_q8_0_mmvq_b8",
7752            // b16 now has BOTH forms (lane/rp-on-st, 2026-08-06). It used to be rp-ONLY, which
7753            // made the q8rp mirror the exact-16 tier's admission ticket for any model carrying a
7754            // single Q8_0 matmul — measured as the FP8-ST refusal (`L0.ssm_beta qtype=0
7755            // rp4=false`, 96 t / 23.9 MiB = 0.143% of resident weight). The mirror stays a
7756            // BANDWIDTH lever on Q8_0-dominant GGUFs; it is no longer a correctness prerequisite.
7757            (QT_Q8_0, 16) => "qmatvec_q8_0_mmvq_b16",
7758            (QT_Q4_K, 2) => "qmatvec_q4_K_mmvq_b2", (QT_Q4_K, 4) => "qmatvec_q4_K_mmvq_b4",
7759            (QT_Q4_K, 8) => "qmatvec_q4_K_mmvq_b8",
7760            // b16 base + _rp (lane/rp-on-st): the 9B NVFP4 GGUF's blocker — real NVFP4 GGUFs keep
7761            // Q4_K attention next to NVFP4 MLP, and the tier's predicate is an ALL.
7762            (QT_Q4_K, 16) => "qmatvec_q4_K_mmvq_b16",
7763            (QT_Q5_K, 2) => "qmatvec_q5_K_mmvq_b2", (QT_Q5_K, 4) => "qmatvec_q5_K_mmvq_b4",
7764            (QT_Q5_K, 8) => "qmatvec_q5_K_mmvq_b8",
7765            // b16 base only (lane/rp-on-st): Q5_K has no rp twins at any width, so there is
7766            // nothing to mirror. Named by the diagnostic as `L0.wqkv_gate qtype=3` on the 9B.
7767            (QT_Q5_K, 16) => "qmatvec_q5_K_mmvq_b16",
7768            (QT_Q6_K, 2) => "qmatvec_q6_K_mmvq_b2", (QT_Q6_K, 4) => "qmatvec_q6_K_mmvq_b4",
7769            (QT_Q6_K, 8) => "qmatvec_q6_K_mmvq_b8", (QT_Q6_K, 16) => "qmatvec_q6_K_mmvq_b16",
7770            (QT_NVFP4, 2) => "qmatvec_nvfp4_mmvq_b2", (QT_NVFP4, 4) => "qmatvec_nvfp4_mmvq_b4",
7771            (QT_NVFP4, 8) => "qmatvec_nvfp4_mmvq_b8",
7772            // b16 (lane/rp-on-st): no mirror needed — NVFP4's 36 B/k32 block is already the
7773            // aligned form its own kernel walks. Unlocks the exact-16 tier for every NVFP4 model
7774            // AND for the mixed FP8-ST artifact, whose 193 NVFP4 tensors were refusing it.
7775            (QT_NVFP4, 16) => "qmatvec_nvfp4_mmvq_b16",
7776            (QT_F8_E4M3, 2) => "qmatvec_e4m3_mmvq_b2", (QT_F8_E4M3, 4) => "qmatvec_e4m3_mmvq_b4",
7777            (QT_F8_E4M3, 8) => "qmatvec_e4m3_mmvq_b8",
7778            // b16 tier (lane/rp-on-st): e4m3 needs NO split-plane mirror to reach it — its native
7779            // row-major layout is already 32B-aligned per k32 block, so the base kernel IS the
7780            // aligned form. Contrast Q8_0, whose b16 exists only as the `_rp` twin (hence q8rp).
7781            (QT_F8_E4M3, 16) => "qmatvec_e4m3_mmvq_b16",
7782            (QT_Q4_0, 2) => "qmatvec_q4_0_mmvq_b2", (QT_Q4_0, 4) => "qmatvec_q4_0_mmvq_b4",
7783            (QT_Q4_0, 8) => "qmatvec_q4_0_mmvq_b8", (QT_Q4_0, 16) => "qmatvec_q4_0_mmvq_b16",
7784            _ => return None,
7785        })
7786    }
7787
7788    /// BATCHED weight-tile-resident matvec from a PRE-QUANTIZED q8_1 activation (the m=2-8 verify/MTP
7789    /// win). One warp walks the weight row ONCE, dp4a vs all m activation columns -> weight HBM/L2
7790    /// traffic 1x for m tokens (vs grid.y=m re-reading it m times). `mcols` ∈ {2,4,8} is the
7791    /// compile-time batch; m must be <= mcols (the c >= m columns are masked in-kernel). y is
7792    /// [m, out_f] token-major. NVFP4 per-tensor macro-scale applied post
7793    /// (scale==1.0 for other dtypes -> no-op). BIT-IDENTICAL per (token,row) to qmatvec_*_mmvq.
7794    ///
7795    /// NVFP4 VARIANT DISPATCH: the batched NVFP4 kernel measured memory-LATENCY bound on the real
7796    /// 27B verify (ncu --set full, 12 steady launches: long_scoreboard 18-30 stalls/issue vs <=1.7
7797    /// for every other reason, DRAM only 41-51% active, lg_throttle 0.7, L1 hit 94% — ONE 6-LDG
7798    /// weight wavefront in flight per warp is the binding constraint, NOT bandwidth and NOT the
7799    /// column-unroll break). Two exactness-free fixes, chosen PER SHAPE from the DRAM-cold 8-copy
7800    /// msweep on all six 27B shapes (2026-07-03):
7801    ///   `pf` = next-g weight-prefetch double-buffer (48 regs, occupancy intact) — wins everywhere
7802    ///          it applies for b4 (-3..-14%), never loses;
7803    ///   `r2` = two rows/warp (67 regs -> 7 resident blocks/SM) — the bigger win (-8.5..-30%) but
7804    ///          wave-quantization-sensitive: with the grid halved to ceil(out_f/8) blocks, a
7805    ///          fractional straggler wave (waves in ~1.05-1.5) costs a full extra latency round on
7806    ///          a latency-bound kernel (27B ffn_down 640 blocks / 574 resident = 1.11 waves: +17%),
7807    ///          while <=1 wave (9B ffn_down 0.89: -30%) or >=2 waves (tail amortized; qkv 2.2:
7808    ///          -8.5%, ffn_gate 3.8: -12.5%) win. For b2, r2 wins on DEEP k-loops (in_f>=6144:
7809    ///          -8..-19%) where the 2-col body starves weight MLP hardest; pf measured negative.
7810    /// b4: r2 when waves(out_f) <= 1 (and grid fills >=half the SMs) or >= 2, else pf.
7811    /// b2: in_f>=6144 -> r2, else base.
7812    /// MEMRA_MMVQ_BV=base|pf|r2|pfr2 forces one variant everywhere (A/B + rollback seam).
7813    /// All variants BIT-IDENTICAL per (token,row): same dp4a order, scales, adg factor, reduce —
7814    /// only load issue time and the row->warp mapping change (kernel-check gates all of them).
7815    /// `rp` = the weight buffer is the A6 SPLIT-PLANE repacked layout (NVFP4 only): the same
7816    /// wave-aware auto rule applies, mapped onto the `_rp` twins (rp/rpr2/rpr2w8 mirror
7817    /// pf/r2/r2w8 — regs 44/67/64 land in the same residency classes).
7818    /// The variant the batched dispatch will pick for this (shape, m, mcols, layout) — exposed so
7819    /// gates can distinguish bit-identical variants (bit-bad==0 required) from the k-split family
7820    /// (deterministic but k-reduce-order-shifted: rel<1e-3 + run-to-run bit-identity required).
7821    /// Device SM count (cached) — grid-fill policy input.
7822    pub fn sm_count(&self) -> i32 {
7823        static SMS: std::sync::OnceLock<i32> = std::sync::OnceLock::new();
7824        *SMS.get_or_init(|| {
7825            use cudarc::driver::sys::CUdevice_attribute_enum as A;
7826            self.gpu.ctx.attribute(A::CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT).unwrap_or(82)
7827        })
7828    }
7829
7830    pub fn batched_variant(&self, _m: usize, in_f: usize, out_f: usize, qtype: i32,
7831                           row_bytes: usize, mcols: usize, rp: bool) -> &'static str {
7832        // Q8_0 never joined the auto variant machinery (on sm_120 its only batched shapes
7833        // were tiny aux tensors). On Q8_0-trunk models the layout is the whole game: the
7834        // split-plane mirror (rp) routes to the _rp twins (H100 coalescing fix, 2026-07-26);
7835        // GGUF layout stays "base". rp bytes MUST never reach the base kernel or vice versa.
7836        if qtype == QT_Q8_0 {
7837            return if rp { "rp" } else { "base" };
7838        }
7839        static BV: std::sync::OnceLock<&'static str> = std::sync::OnceLock::new();
7840        let bv = *BV.get_or_init(|| match std::env::var("MEMRA_MMVQ_BV").as_deref() {
7841            Ok("base") => "base", Ok("pf") => "pf", Ok("r2") => "r2", Ok("r2w8") => "r2w8",
7842            Ok("pfr2") => "pfr2", Ok("ca") => "ca", Ok("car2") => "car2",
7843            // rp* = SPLIT-PLANE REPACKED layout kernels (A6 prototype): W must already be the
7844            // repacked buffer (msweep MSWEEP_RP harness) — never valid on GGUF-layout weights.
7845            Ok("rp") => "rp", Ok("rpr2") => "rpr2", Ok("rpr2w8") => "rpr2w8",
7846            // rpca* = cp.async software-pipelined split-plane (2026-07-05): hides the _rp
7847            // long_scoreboard load stall. rp-layout only; b4/b2 (no b8 twin).
7848            Ok("rpca") => "rpca", Ok("rpcar2") => "rpcar2",
7849            // 2026-07-06 m-small latency arc: rpsc = rpr2 + per-warp smem scale prestage (kills
7850            // the scale-plane global dependency, zero reg growth); rpms/rpmsc = m-split x2
7851            // across warp pairs (2x blocks of rpr2, column halves per warp, BIT-identical to
7852            // _rp); rpks/rpksc = k-split x2 (fastest microbench cells but k-reduce-order-shifted:
7853            // run-spec self-consistency FAILED on the 27B daily driver — verify logits must be
7854            // bit-identical to the decode path — measurement corpus ONLY, never auto).
7855            Ok("rpsc") => "rpsc", Ok("rpms") => "rpms", Ok("rpmsc") => "rpmsc",
7856            Ok("rpks") => "rpks", Ok("rpksc") => "rpksc",
7857            _ => "auto",
7858        });
7859        // cp.async ring variants need 16B-aligned rows (in_f%256==0 -> (in_f/64)*36 % 16 == 0)
7860        // and whole 32-group warp iterations (nsb%32==0 <=> in_f%1024==0). All 27B/9B trunk
7861        // shapes qualify; anything else falls back to the register variants.
7862        let ca_ok = qtype == QT_NVFP4 && (row_bytes % 16 == 0) && (in_f % 1024 == 0);
7863        // rpsc: smem scale plane fits (nsb64 <= 272) + int4-aligned staging (nsb64 % 4 == 0).
7864        // rpks/rpksc: half-plane staging alignment needs nsb64 % 8 == 0 (in_f % 512 == 0).
7865        // MEMRA_KS=0 removes the 2026-07-06 rpsc/rpks/rpksc entries from AUTO (rollback seam;
7866        // forced MEMRA_MMVQ_BV values still work).
7867        static KS_ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
7868        let ks_on = *KS_ON.get_or_init(|| std::env::var("MEMRA_KS").as_deref() != Ok("0"));
7869        let sc_ok = ks_on && qtype == QT_NVFP4 && (in_f % 256 == 0) && (in_f / 64 <= 272);
7870        let ks_ok = ks_on && qtype == QT_NVFP4 && (in_f % 512 == 0) && (in_f / 64 <= 272);
7871        static SMS: std::sync::OnceLock<i32> = std::sync::OnceLock::new();
7872        let sms = *SMS.get_or_init(|| {
7873            use cudarc::driver::sys::CUdevice_attribute_enum as A;
7874            self.gpu.ctx.attribute(A::CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT).unwrap_or(82)
7875        });
7876        // k-quant r2 port (2026-07-04): q4_K/q5_K/q6_K have _r2/_r2w8 twins. ncu on the DRAM-cold
7877        // 9B msweep showed q4_K/q5_K b4 memory-latency bound like NVFP4 pre-fix (long_scoreboard
7878        // 19.6/16.4 per issue, DRAM 47.7/38.2%, L2 weight hit ~13%); q6_K lm_head is the exception
7879        // at DRAM 90-91% = wall-bound (yet r2 still wins -8%: deeper MLP raises achieved DRAM).
7880        // No _pf port (a k-quant group stages 10+ words vs NVFP4's 5 — register cost outweighs;
7881        // r2 covers the same MLP) and no rp (GGUF layout only). Q8_0 stays base: its only real
7882        // batched shapes are the tiny out_f=32 ssm_alpha/beta (8-block grids never fill one SM).
7883        // AUTO RULE = the measured winners table (differs from NVFP4's!):
7884        //   r2w8 NEVER in auto — the reg squeeze (72 -> 64 regs = stack spill) loses to unbounded
7885        //     r2 on every measured k-quant cell, incl. the wave-crossing lm_heads (q6_K 1316 vs
7886        //     r2 1258us) — kernels kept behind the force seam for the corpus;
7887        //   q4_K: r2 whenever the halved grid fills the SMs (blocks >= 4*SMs), INCLUDING the
7888        //     1.05-2.0 straggler window where NVFP4's r2 lost (qkv 1.78 waves: r2 -15% here; the
7889        //     k-quant base kernel leaves more latency on the table than a straggler wave costs);
7890        //   q5_K/q6_K: r2 only at waves >= 2 (the 248320-row lm_heads, 48+ waves: q6_K -8%, q5_K
7891        //     -2%); mid shapes measured base-or-flat (q5_K qkv 49.1 base vs 49.7 r2, attn_gate
7892        //     flat, attn_k base) — the 5/6-bit two-stream unpack makes r2's staging pricier.
7893        //   b2 same table with 8-row blocks: q4_K r2 when filled (-3..-22% all measured shapes),
7894        //     q5_K/q6_K r2 at waves >= 2 (27B lm_head -2.9%; 9B q6_K flat, harmless).
7895        let kq_r2 = matches!(qtype, QT_Q4_K | QT_Q5_K | QT_Q6_K);
7896        // MEMRA_KQ_BV=base|r2|r2w8 forces the k-quant variant WITHOUT touching the NVFP4 dispatch
7897        // (MEMRA_MMVQ_BV is global — an interleaved k-quant-only e2e A/B needs this narrower seam).
7898        static KQBV: std::sync::OnceLock<&'static str> = std::sync::OnceLock::new();
7899        let kq_bv = *KQBV.get_or_init(|| match std::env::var("MEMRA_KQ_BV").as_deref() {
7900            Ok("base") => "base", Ok("r2") => "r2", Ok("r2w8") => "r2w8",
7901            _ => "auto",
7902        });
7903        let variant: &'static str = if qtype == QT_Q4_0 {
7904            // Q4_0 r2 (gemma verify trunk, 2026-07-10): shared activation loads + the
7905            // row-independent ones-sum computed once per (col,group) for 2 rows. Same
7906            // fill rule as q4_K: r2 when the halved grid still fills the SMs.
7907            static Q40BV: std::sync::OnceLock<&'static str> = std::sync::OnceLock::new();
7908            let q40 = *Q40BV.get_or_init(|| match std::env::var("MEMRA_Q40_BV").as_deref() {
7909                // ms/sm/la = force-only measurement seams (ALL FLAT/NEGATIVE 2026-07-13,
7910                // never auto): m-split flat (nvcc keeps 72 regs); smem-slab −11% (staging
7911                // + syncs cost more than the stalls, bank-pad made no difference);
7912                // register load-ahead flat (nvcc already reorders). The b-tier limiter
7913                // is still unidentified — see the jsonl row.
7914                Ok("base") => "base", Ok("r2") => "r2", Ok("ms") => "ms", Ok("sm") => "sm",
7915                Ok("la") => "la", _ => "auto",
7916            });
7917            let v = if q40 != "auto" { q40 }
7918            else if (out_f as u32).div_ceil(8) >= 4 * sms as u32 { "r2" } else { "base" };
7919            // split-plane mirror twins (2026-07-10): same fill rule, _rp names.
7920            // (m-split r2 pair twin PROBED FLAT 2026-07-13 — nvcc kept 72 regs either way
7921            // and the limiter is the per-column activation load chain (long_scoreboard
7922            // 42.5%), not occupancy; arm killed per doctrine, jsonl row is the record.)
7923            if rp { match v { "ms" => "r2ms_rp", "sm" => "r2sm_rp", "la" => "r2la_rp",
7924                              "r2" => "r2_rp", _ => "rp" } }
7925            else if matches!(v, "ms" | "sm" | "la") { "r2" } else { v }
7926        } else if qtype != QT_NVFP4 && !kq_r2 {
7927            "base"
7928        } else if kq_r2 && rp {
7929            // K-quant split-plane mirror (2026-08-01): only the plain _rp batched twins are
7930            // compiled for q4_K/q6_K — rp is a LAYOUT, it must survive every heuristic
7931            // (split-plane bytes through a GGUF-layout kernel = NaN). q5_K never mirrors.
7932            "rp"
7933        } else if kq_r2 {
7934            // k-quant r2w8 only exists at b4 (b2_r2 already 8-resident; b8 has no w8 twin) ->
7935            // mcols != 4 forced r2w8 falls to unbounded r2.
7936            if kq_bv != "auto" {
7937                if kq_bv == "r2w8" && mcols != 4 { "r2" } else { kq_bv }
7938            } else if bv != "auto" {
7939                match bv {
7940                    "r2" | "pfr2" | "rpr2" | "car2" => "r2",
7941                    "r2w8" | "rpr2w8" => if mcols != 4 { "r2" } else { "r2w8" },
7942                    _ => "base",   // base/pf/ca/rp forced -> base (no such k-quant kernels)
7943                }
7944            } else {
7945                let blocks = (out_f + 7) / 8;
7946                let waves = blocks as f64 / (7 * sms as usize) as f64;
7947                let filled = blocks >= 4 * sms as usize;
7948                let use_r2 = if qtype == QT_Q4_K { filled } else { waves >= 2.0 };
7949                if use_r2 { "r2" } else { "base" }
7950            }
7951        } else if bv != "auto" {
7952            // r2w8 only exists for b4/b8 (the b2_r2 kernel is already 8-blocks-resident at 60 regs).
7953            // ca/car2 need the alignment gate AND have no b8 twins; pfr2 has no b8 twin either —
7954            // unsupported (shape, mcols) combos fall back to pf/r2.
7955            // On rp buffers, forced legacy names map to their rp twins (layout law).
7956            let v = if bv == "r2w8" && mcols == 2 { "r2" }
7957                else if bv == "ca" && (!ca_ok || mcols == 8) { "pf" }
7958                else if bv == "car2" && (!ca_ok || mcols == 8) { "r2" }
7959                else if bv == "pfr2" && mcols == 8 { "r2" }
7960                else if (bv == "rpr2w8" || bv == "rpr2") && mcols == 2 { "rpr2" }
7961                // rpca* has no b8 twin (falls to rpr2w8/rpr2); needs the ca alignment gate.
7962                else if (bv == "rpca" || bv == "rpcar2") && (!ca_ok || mcols == 8) {
7963                    if mcols == 8 { "rpr2w8" } else { "rpr2" }
7964                }
7965                else if bv == "rpcar2" && mcols == 2 { "rpca" }
7966                // rpsc/rpmsc/rpks* gate on smem-fit + alignment; fall to rpr2 outside it
7967                // (rpms has no smem and no alignment need — always valid on rp buffers).
7968                else if (bv == "rpsc" || bv == "rpmsc") && !sc_ok { "rpr2" }
7969                else if (bv == "rpks" || bv == "rpksc") && !ks_ok { "rpr2" }
7970                else { bv };
7971            if rp {
7972                match v {
7973                    "base" | "pf" | "ca" | "rp" => "rp",
7974                    "r2" | "pfr2" | "car2" | "rpr2" => "rpr2",
7975                    "r2w8" | "rpr2w8" => if mcols == 2 { "rpr2" } else { "rpr2w8" },
7976                    other => other,   // rpca/rpcar2/rpsc/rpks/rpksc pass through (already rp-layout)
7977                }
7978            } else { v }
7979        } else if mcols == 8 {
7980            // b8 AUTO (2026-07-06 m-small latency arc, g7e DRAM-cold rp msweep m=5/6/8 all five
7981            // 27B shapes): rpsc — the rpr2w8 schedule with the warp's scale rows prestaged to
7982            // smem, leaving ONE global dependency (the quant stream) in the k-loop at zero reg
7983            // growth. BIT-identical to rpr2w8 and wins or ties EVERY b8 cell: ffn_gate m5
7984            // 50.7->46.9 m8 64.1->57.1 (-11%), qkv m8 34.6->33.0, ssm_out m8 29.7->28.8,
7985            // attn_gate m8 26.9->26.1, ffn_down m5 58.2->56.9. The faster split-grid twins are
7986            // OUT: rpksc (k-split, ffn_down m5 -21%) broke run-spec self-consistency (k-reduce
7987            // order shifts verify argmax at tie margins — verify must stay bit-identical to the
7988            // m=1 decode chain); rpmsc (m-split, bit-identical) measured NEGATIVE everywhere
7989            // (twin warp's duplicated weight stream: ffn_down m5 85.7 vs 56.9).
7990            if rp { if sc_ok { "rpsc" } else { "rpr2w8" } } else { "r2w8" }
7991        } else if mcols >= 4 {
7992            // r2 runs 7 resident blocks/SM (67 regs); its __launch_bounds__(128,8) twin `r2w8`
7993            // (64 regs) runs 8. grid = ceil(out_f/8) for both. rp twins land in the same
7994            // residency classes (rp 44 regs ~ pf-class occupancy, rpr2 67, rpr2w8 64).
7995            let blocks = (out_f + 7) / 8;
7996            let r7 = 7 * sms as usize;
7997            let r8 = 8 * sms as usize;
7998            let waves = blocks as f64 / r7 as f64;
7999            let filled = blocks >= 4 * sms as usize;
8000            // 2026-07-06 m-small latency arc: b4 keeps the wave rule (rpms/rpmsc measured
8001            // flat-to-negative at m=3/4 on every shape — the m-split twin duplicates the weight
8002            // stream; rpsc b4 also negative on r2-class picks, ffn_down m4 51.1 vs 46.5).
8003            if filled && blocks.div_ceil(r8) < blocks.div_ceil(r7) {
8004                // the extra residency drops the INTEGER wave count -> the straggler wave a
8005                // latency-bound kernel pays in full disappears (ffn_down 1.11 -> 0.98 waves:
8006                // 112.5 -> 81.6us, beats pf 90.1; qkv 2.23 -> 1.95: 58.1 -> 51.1).
8007                if rp { "rpr2w8" } else { "r2w8" }
8008            } else if waves >= 2.0 || (waves <= 1.0 && filled) {
8009                // tail amortized (>=2 waves) or single wave: unbounded r2 (no reg-squeeze tax —
8010                // gate/up 81.1 vs 83.9 bounded, attn_q 61.0 vs 63.4).
8011                if rp { "rpr2" } else { "r2" }
8012            } else {
8013                // fractional straggler-wave window with no crossing, or grid too small to fill
8014                // the SMs (tiny out_f<=1024 shapes want max row-parallelism): prefetch variant
8015                // (rp = the r1 split-plane twin — measured the attn_gate winner, 35.4 vs pf 36.4).
8016                if rp { "rp" } else { "pf" }
8017            }
8018        } else if in_f >= 6144 {
8019            // b2 deep-k (2026-07-06): every new twin measured flat-to-negative here (rpms 44.1
8020            // vs rpr2 40.8 ffn_down; rpsc 43.6; the winning rpks is banned on k-order) — rpr2
8021            // stays.
8022            if rp { "rpr2" } else { "r2" }
8023        }
8024        else if rp {
8025            // b2 shallow-k: qkv (out_f=10240, 0.97 waves at 7-resident) is the one measured cell
8026            // where the r2-schedule scale-prestage twin beats the r1 rp pick (24.7 vs 28.9us
8027            // -15%); the wider (ffn_gate 1.65 waves) and smaller (attn_gate 0.58) shapes LOSE
8028            // (41.8 vs 38.2 / 16.6 vs 14.6) — gate on the single-wave window.
8029            let waves = ((out_f + 7) / 8) as f64 / (7 * sms as usize) as f64;
8030            if sc_ok && waves >= 0.9 && waves <= 1.1 { "rpsc" } else { "rp" }
8031        } else { "base" };
8032        variant
8033    }
8034
8035    pub fn qmatvec_mmvq_batched(&self, bytes: &CudaSlice<u8>, aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
8036                                m: usize, in_f: usize, out_f: usize, qtype: i32, row_bytes: usize,
8037                                mcols: usize, scale: f32, rp: bool)
8038                                -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8039        const ROWS_PER_BLOCK: u32 = 4;
8040        // TUNE SEAM (H100 lane): MEMRA_BVAR forces the batched-variant pick for the whole
8041        // process — the auto heuristics were tuned on sm_120 (82 SMs / 858 GB/s) and the
8042        // sm_90a re-tune sweeps this seam empirically. Layout variants stay safe: an rp
8043        // weight keeps its rp-layout kernel family regardless of the override.
8044        let forced: Option<&'static str> = {
8045            static V: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
8046            V.get_or_init(|| std::env::var("MEMRA_BVAR").ok())
8047                .as_deref()
8048                .map(|s| Box::leak(s.to_string().into_boxed_str()) as &'static str)
8049        };
8050        let variant = match forced {
8051            Some(v) if !rp || v.contains("rp") => v,
8052            _ => self.batched_variant(m, in_f, out_f, qtype, row_bytes, mcols, rp),
8053        };
8054        let base_name = Self::batched_kernel_name(qtype, mcols)
8055            .ok_or_else(|| format!("qmatvec_mmvq_batched: no kernel for qtype {qtype} mcols {mcols}"))?;
8056        // b16 tier (t=9..16 verify): only base/_rp b16 kernels are compiled — the b2..b8
8057        // per-shape perf variants (r2/pf/...) do not apply at this width. rp is a LAYOUT,
8058        // not a perf variant: it must survive (base kernel on split-plane bytes = NaN).
8059        let variant = if mcols == 16 { if rp { "rp" } else { "base" } } else { variant };
8060        // EXACT-WIDTH b5/b6/b7 twins (lane/vt-fixes fix 1, 2026-08-03): the b8 kernels
8061        // allocate acc[WROWS][8] at ANY m, so T=5..7 verify paid the full 8-wide register
8062        // tax — the measured T=4->5 cliff. The same template at MCOLS=m runs the identical
8063        // per-(token,row) chain (columns c >= m never execute in either form) ->
8064        // BIT-IDENTICAL to the b8 launch. NVFP4 split-plane only (the sm_120 default trunk);
8065        // covers both b8 auto schedules (rpsc, rpr2w8). MEMRA_B567=0 rollback.
8066        static B567: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
8067        let b567 = *B567.get_or_init(|| std::env::var("MEMRA_B567").as_deref() != Ok("0"));
8068        if b567 && qtype == QT_NVFP4 && rp && mcols == 8 && (5..=7).contains(&m)
8069            && matches!(variant, "rpsc" | "rpr2w8") {
8070            let f = self.func(&format!("qmatvec_nvfp4_mmvq_b{m}_{variant}"));
8071            let rows_per_block = ROWS_PER_BLOCK * 2; // r2-class schedules: 2 rows/warp
8072            let mut y = self.alloc_uninit::<f32>(m * out_f)?;
8073            let cfg = LaunchConfig {
8074                grid_dim: ((out_f as u32 + rows_per_block - 1) / rows_per_block, 1, 1),
8075                block_dim: (32, ROWS_PER_BLOCK, 1), shared_mem_bytes: 0 };
8076            let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
8077            let __s_b = self.gpu.stream();
8078            let mut b = __s_b.launch_builder(&f);
8079            b.arg(bytes).arg(aq).arg(ad).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
8080            unsafe { b.launch(cfg)?; }
8081            if scale != 1.0 { self.scale_inplace(&mut y, scale, m * out_f)?; }
8082            return Ok(y);
8083        }
8084        let (name, rows_per_block): (std::borrow::Cow<'static, str>, u32) = match variant {
8085            "base" => (base_name.into(), ROWS_PER_BLOCK),
8086            "pf" => (format!("{base_name}_pf").into(), ROWS_PER_BLOCK),
8087            "ca" => (format!("{base_name}_ca").into(), ROWS_PER_BLOCK),
8088            "rp" => (format!("{base_name}_rp").into(), ROWS_PER_BLOCK),
8089            "rpca" => (format!("{base_name}_rpca").into(), ROWS_PER_BLOCK), // 1 row/warp cp.async
8090            // split families: 2 warp-pairs x 2 rows = 4 rows/block (the k-range or column set
8091            // splits across the pair's two warps; grid.x doubles vs rpr2 at the same regs).
8092            "rpks" => (format!("{base_name}_rpks").into(), ROWS_PER_BLOCK),
8093            "rpksc" => (format!("{base_name}_rpksc").into(), ROWS_PER_BLOCK),
8094            "rpms" => (format!("{base_name}_rpms").into(), ROWS_PER_BLOCK),
8095            "rpmsc" => (format!("{base_name}_rpmsc").into(), ROWS_PER_BLOCK),
8096            "r2ms_rp" => (format!("{base_name}_r2ms_rp").into(), ROWS_PER_BLOCK),
8097            "r2sm_rp" => (format!("{base_name}_r2sm_rp").into(), ROWS_PER_BLOCK * 2),
8098            "r2la_rp" => (format!("{base_name}_r2la_rp").into(), ROWS_PER_BLOCK * 2),
8099            v => (format!("{base_name}_{v}").into(), ROWS_PER_BLOCK * 2), // r2-class: 2 rows/warp
8100        };
8101        debug_assert!(!rp || name.contains("_rp"), "rp weight dispatched to a GGUF-layout kernel");
8102        let f = self.func(&name);
8103        let mut y = self.alloc_uninit::<f32>(m * out_f)?;
8104        // r2sm_rp: [MCOLS][32 blk][8 int] activation slab + [MCOLS][32] f32 scales.
8105        let smem = if name.contains("_r2sm_rp") { (mcols * 32 * 9 * 4 + mcols * 32 * 4) as u32 }
8106                   else { 0 };
8107        let cfg = LaunchConfig {
8108            grid_dim: ((out_f as u32 + rows_per_block - 1) / rows_per_block, 1, 1),
8109            block_dim: (32, ROWS_PER_BLOCK, 1), shared_mem_bytes: smem };
8110        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
8111        let __s_b = self.gpu.stream();
8112        let mut b = __s_b.launch_builder(&f);
8113        b.arg(bytes).arg(aq).arg(ad).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
8114        unsafe { b.launch(cfg)?; }
8115        if scale != 1.0 { self.scale_inplace(&mut y, scale, m * out_f)?; }
8116        Ok(y)
8117    }
8118
8119    /// BATCHED weight-tile-resident matvec from raw weight bytes (quantizes the f32 activation `x` to
8120    /// q8_1 internally; macro-scale NOT applied — caller compares bare, like qmatvec_*_fast). For the
8121    /// kernel_check bit-equivalence gate. `mcols` ∈ {2,4,8}. Works for Q8_0/Q4_K/Q5_K/Q6_K/NVFP4.
8122    pub fn qmatvec_batched_raw(&self, bytes: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize,
8123                               in_f: usize, out_f: usize, qtype: i32, row_bytes: usize, mcols: usize,
8124                               rp: bool)
8125                               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8126        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
8127        self.qmatvec_mmvq_batched(bytes, &aq, &ad, m, in_f, out_f, qtype, row_bytes, mcols, 1.0, rp)
8128    }
8129
8130    /// Back-compat NVFP4-only batched raw launcher (used by older gates). Delegates to the generic one.
8131    pub fn qmatvec_nvfp4_batched_raw(&self, bytes: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize,
8132                                     in_f: usize, out_f: usize, row_bytes: usize, mcols: usize,
8133                                     rp: bool)
8134                                     -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8135        self.qmatvec_batched_raw(bytes, x, m, in_f, out_f, QT_NVFP4, row_bytes, mcols, rp)
8136    }
8137
8138    /// Stage-C FP4 gate (MEMRA_FP4): if `w` is an NVFP4 weight with in_f%64==0, run the native mxf4
8139    /// block-scale GEMM and apply the per-tensor macro-scale, returning Some(y). Else None (caller
8140    /// falls through to the int8 GEMM / dp4a). Strict opt-in over the proven int8 path; m>=16 only.
8141    fn try_fp4_gemm(&self, w: &crate::model::GpuTensor, x: &CudaSlice<f32>, m: usize,
8142                    in_f: usize, out_f: usize)
8143                    -> Result<Option<CudaSlice<f32>>, Box<dyn std::error::Error>> {
8144        use crate::model::GpuTensor;
8145        if cfg!(memra_portable_cuda) { return Ok(None); }
8146        if std::env::var("MEMRA_FP4").is_err() { return Ok(None); }
8147        // CUTLASS prefill branch (m>=128 + MEMRA_FP4_CUTLASS + a repacked CutlassWeight present): route
8148        // to the CUTLASS sm120 NVFP4 GEMM, folding the per-tensor macro-scale into the epilogue alpha
8149        // (1/scale) — no post-matmul scale_inplace. Decode (m<128) and the m∈[16,128) middle band keep
8150        // the hand-roll below: CUTLASS's 128-row M-tile wastes work under 128.
8151        // The hand-roll applies the per-tensor macro-scale as a POST-matmul MULTIPLY (scale_inplace(y,
8152        // scale)); CUTLASS's epilogue does D = alpha * (A@B^T), so alpha == scale reproduces it exactly
8153        // (NOT 1/scale — the plan sketch had this inverted; the kernel_check arm gates it). scale==1.0
8154        // for the common no-macro-scale case.
8155        #[cfg(memra_cutlass)]
8156        if m >= 128 && std::env::var("MEMRA_FP4_CUTLASS").is_ok() {
8157            if let GpuTensor::Quant { bytes, qtype, scale, row_bytes, cutlass, .. } = w {
8158                if *qtype == QT_NVFP4 && in_f % 64 == 0 {
8159                    if let Some(cw) = cutlass {
8160                        // Resident fast path: load-time-repacked B + swizzled SFB (no per-call repack).
8161                        let y = self.cutlass_fp4_gemm(&cw.b_packed, &cw.sfb_swizzled, x, *scale,
8162                                                      m, out_f, in_f)?;
8163                        return Ok(Some(y));
8164                    } else if std::env::var("MEMRA_FP4_CUTLASS_OTF").is_ok() {
8165                        // On-the-fly repack (MEMRA_FP4_CUTLASS_OTF): de-interleave + swizzle the B operand
8166                        // from raw bytes per prefill call. No resident doubling of the NVFP4 weight VRAM
8167                        // (the load-time repack ~doubles it) — needed for models that don't fit the
8168                        // resident path (e.g. the 27B on 24GB). Slower (per-call repack) but argmax-exact.
8169                        let (b_packed, sfb_sw) = self.build_cutlass_weight(bytes, out_f, in_f, *row_bytes)?;
8170                        let y = self.cutlass_fp4_gemm(&b_packed, &sfb_sw, x, *scale, m, out_f, in_f)?;
8171                        return Ok(Some(y));
8172                    }
8173                }
8174            }
8175        }
8176        if let GpuTensor::Quant { bytes, qtype, row_bytes, scale, rp, .. } = w {
8177            // A6: the hand-rolled W4A4 mxf4 GEMM reads 36B GGUF blocks — no rp port (MEMRA_FP4 is
8178            // an opt-in accuracy tradeoff); repacked tensors fall through to the int8 GEMM.
8179            if *qtype == QT_NVFP4 && in_f % 64 == 0 && !*rp {
8180                let y = self.qmatvec_gemm_nvfp4_fp4(bytes, x, m, in_f, out_f, *row_bytes, *scale)?;
8181                return Ok(Some(y));
8182            }
8183        }
8184        Ok(None)
8185    }
8186
8187    /// rms_norm + fused fp16 twin (task #14): f32 output verbatim `rms_norm` + the fp16
8188    /// copy the f16-mirror GEMM group would otherwise produce with a standalone convert
8189    /// launch. BIT-IDENTICAL end-to-end (same reduction, same __float2half values).
8190    pub fn rms_norm_f16out(&self, x: &CudaSlice<f32>, w: &CudaSlice<f32>,
8191                           dst: &mut CudaSlice<f32>, dst16: &mut CudaSlice<u8>,
8192                           ncols: usize, nrows: usize, eps: f32)
8193                           -> Result<(), Box<dyn std::error::Error>> {
8194        let f = self.func("rms_norm_f16out_f32");
8195        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
8196        let (nc, e) = (ncols as i32, eps);
8197        let __s_b = self.gpu.stream();
8198        let mut b = __s_b.launch_builder(&f);
8199        b.arg(x).arg(w).arg(dst).arg(dst16).arg(&nc).arg(&e);
8200        unsafe { b.launch(cfg)?; }
8201        Ok(())
8202    }
8203
8204    /// add+norm(+f16out) fusion for the prefill trunk (round 28; add_rms_norm precedent —
8205    /// bit-identical to add_f32 -> rms_norm_f16out). block_dim matches rms_norm_f16out's.
8206    #[allow(clippy::too_many_arguments)]
8207    pub fn add_rms_norm_f16out(&self, a: &CudaSlice<f32>, b: &CudaSlice<f32>, w: &CudaSlice<f32>,
8208                               res: &mut CudaSlice<f32>, dst: &mut CudaSlice<f32>,
8209                               dst16: &mut CudaSlice<u8>, ncols: usize, nrows: usize, eps: f32)
8210                               -> Result<(), Box<dyn std::error::Error>> {
8211        let f = self.func("add_rms_norm_f16out_f32");
8212        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
8213        let (nc, e) = (ncols as i32, eps);
8214        let __s_lb = self.gpu.stream();
8215        let mut lb = __s_lb.launch_builder(&f);
8216        lb.arg(a).arg(b).arg(w).arg(res).arg(dst).arg(dst16).arg(&nc).arg(&e);
8217        unsafe { lb.launch(cfg)?; }
8218        Ok(())
8219    }
8220
8221    /// matmul_group with a PRE-EMITTED fp16 activation (task #14: the producer norm fused
8222    /// the convert). Mirror-less members fall back to `matmul` on the f32 activation.
8223    pub fn matmul_group_xh(&self, ws: &[&crate::model::GpuTensor], x: &CudaSlice<f32>,
8224                           xh: &CudaSlice<u8>, m: usize)
8225                           -> Result<Vec<CudaSlice<f32>>, Box<dyn std::error::Error>> {
8226        let mut out = Vec::with_capacity(ws.len());
8227        let in_f = ws[0].in_features();
8228        for w in ws {
8229            if w.in_features() == in_f && m >= 16 && !self.verify_exact_on() {
8230                if let Some(y) = self.try_f16_gemm_pre(w, xh, m)? {
8231                    out.push(y);
8232                    continue;
8233                }
8234            }
8235            out.push(self.matmul(w, x, m)?);
8236        }
8237        Ok(out)
8238    }
8239
8240    /// task #14 pad-proofing: zero beta/g_log at rows >= len_d[0] (pads become identity
8241    /// GDN steps). Layouts [T, H].
8242    pub fn gdn_pad_mask(&self, beta: &mut CudaSlice<f32>, g_log: &mut CudaSlice<f32>,
8243                        len_d: &CudaSlice<i32>, h: usize, t: usize)
8244                        -> Result<(), Box<dyn std::error::Error>> {
8245        let f = self.func("gdn_pad_mask_f32");
8246        let cfg = LaunchConfig::for_num_elems((t * h) as u32);
8247        let (hi, ti) = (h as i32, t as i32);
8248        let __s_b = self.gpu.stream();
8249        let mut b = __s_b.launch_builder(&f);
8250        b.arg(beta).arg(g_log).arg(len_d).arg(&hi).arg(&ti);
8251        unsafe { b.launch(cfg)?; }
8252        Ok(())
8253    }
8254
8255    /// task #14 pad-proofing: dst[ncols] = src row (len_d[0]-1) — device-indexed last-row
8256    /// gather for the padded prime graph's h_seed/hlast.
8257    pub fn row_gather_dev(&self, src: &CudaSlice<f32>, dst: &mut CudaSlice<f32>,
8258                          len_d: &CudaSlice<i32>, ncols: usize)
8259                          -> Result<(), Box<dyn std::error::Error>> {
8260        let f = self.func("row_gather_dev_f32");
8261        let cfg = LaunchConfig::for_num_elems(ncols as u32);
8262        let nc = ncols as i32;
8263        let __s_b = self.gpu.stream();
8264        let mut b = __s_b.launch_builder(&f);
8265        b.arg(src).arg(dst).arg(len_d).arg(&nc);
8266        unsafe { b.launch(cfg)?; }
8267        Ok(())
8268    }
8269
8270    /// Grouped matmul: several weights consuming ONE activation (hybrid layers: the GDN
8271    /// 4-tuple wqkv/gate/beta/alpha, attention q/k/v, ffn gate/up). Semantics identical to
8272    /// calling `matmul` per weight; the f16-mirror arm converts the activation ONCE for the
8273    /// whole group instead of once per GEMM (the standalone converts were ~250 launches/prime
8274    /// of small-kernel gap fuel — nsys 2026-07-26). Any member without a mirror (or with a
8275    /// different in_f) falls back to its own `matmul` — behavior unchanged.
8276    pub fn matmul_group(&self, ws: &[&crate::model::GpuTensor], x: &CudaSlice<f32>, m: usize)
8277                        -> Result<Vec<CudaSlice<f32>>, Box<dyn std::error::Error>> {
8278        use crate::model::GpuTensor;
8279        let mut out = Vec::with_capacity(ws.len());
8280        let any_mirror = ws.iter().any(|w| matches!(w, GpuTensor::Quant { f16: Some(_), .. }));
8281        if m >= 16 && any_mirror && !self.verify_exact_on() {
8282            let in_f = ws[0].in_features();
8283            let xh = self.f16_act(x, m * in_f, in_f)?;
8284            for w in ws {
8285                if w.in_features() == in_f {
8286                    if let Some(y) = self.try_f16_gemm_pre(w, &xh, m)? {
8287                        out.push(y);
8288                        continue;
8289                    }
8290                }
8291                out.push(self.matmul(w, x, m)?);
8292            }
8293            return Ok(out);
8294        }
8295        for w in ws {
8296            out.push(self.matmul(w, x, m)?);
8297        }
8298        Ok(out)
8299    }
8300
8301    /// Cross-request grouped matmul (task #13): run ONE projection group over the
8302    /// CONCATENATION of several sequences' activations (m = sum of per-seq rows — the
8303    /// GEMM-batch win vLLM gets from continuous batching), then split each output back
8304    /// into per-seq buffers. Zero view plumbing: gather/scatter are stream-ordered D2D
8305    /// copies (~us at prime sizes). NUMERIC CONFIG NOTE: a GEMM at m=sum tiles K
8306    /// differently than per-seq GEMMs — argmax-gated like every prefill GEMM change.
8307    pub fn matmul_group_multi(&self, ws: &[&crate::model::GpuTensor],
8308                              xs: &[&CudaSlice<f32>], ms: &[usize])
8309                              -> Result<Vec<Vec<CudaSlice<f32>>>, Box<dyn std::error::Error>> {
8310        assert_eq!(xs.len(), ms.len());
8311        let in_f = ws[0].in_features();
8312        let total: usize = ms.iter().sum();
8313        let mut xcat = self.uninit(total * in_f)?;
8314        let mut off = 0usize;
8315        for (x, &m) in xs.iter().zip(ms) {
8316            self.copy_into(&mut xcat, off * in_f, x, m * in_f)?;
8317            off += m;
8318        }
8319        let ys = self.matmul_group(ws, &xcat, total)?;
8320        let mut out: Vec<Vec<CudaSlice<f32>>> = (0..xs.len()).map(|_| Vec::new()).collect();
8321        for (w, y) in ws.iter().zip(ys) {
8322            let out_f = w.out_features();
8323            let mut off = 0usize;
8324            for (s, &m) in ms.iter().enumerate() {
8325                let mut ys_s = self.uninit(m * out_f)?;
8326                let src = y.slice(off * out_f..(off + m) * out_f);
8327                self.gpu.stream().memcpy_dtod(&src, &mut ys_s)?;
8328                out[s].push(ys_s);
8329                off += m;
8330            }
8331        }
8332        Ok(out)
8333    }
8334
8335    /// True if `w`'s qtype has a batched tensor-core GEMM kernel (the prefill T>1 root fix).
8336    /// Only the 4 daily-hot dtypes: Q8_0, Q4_K, Q6_K, NVFP4. NVFP4 needs in_f % 64 == 0.
8337    /// DEFAULT-ON (2026-06-28): measured pp512 9B-NVFP4 = 1413 tok/s WITH this GEMM vs 298 with the
8338    /// dp4a fallback (4.7x) AND MORE accurate (prefill logit maxdiff 0.159 vs dp4a 0.55, both argmax
8339    /// MATCH). The int8 tensor-core GEMM is unconditional (its historical MEMRA_GEMM opt-in gate
8340    /// shipped with Phase 0 — mma + smem swizzle + cp.async — and was removed). Prefill-only
8341    /// (m>=GEMM_M_THRESHOLD); m=1 decode keeps dp4a/MMVQ (this returns true but matmul only calls it
8342    /// at m>=threshold). Portable CUDA targets always use the correctness fallback; on sm_120a,
8343    /// MEMRA_NO_GEMM forces that same dp4a fallback (the bit-reference).
8344    pub fn gemm_supports(&self, w: &crate::model::GpuTensor) -> bool {
8345        use crate::model::GpuTensor;
8346        if !legacy_quant_gemm_allowed(
8347            cfg!(memra_portable_cuda),
8348            cfg!(memra_hopper_mma),
8349            std::env::var_os("MEMRA_NO_GEMM").is_some(),
8350        ) {
8351            return false;
8352        }
8353        match w {
8354            GpuTensor::Quant { qtype, .. } =>
8355                matches!(*qtype, QT_Q8_0 | QT_Q4_K | QT_Q6_K | QT_Q5_K | QT_Q4_0)
8356                || (*qtype == QT_NVFP4 && w.in_features() % 64 == 0),
8357            GpuTensor::Float { .. } | GpuTensor::FloatBf16 { .. } => false,
8358        }
8359    }
8360
8361    /// Batched tensor-core int8 GEMM with a PRE-QUANTIZED q8_1 activation (aq,ad). The prefill
8362    /// (T>1) root fix: decode each weight 32-block to int8 in shared memory ONCE per (row-tile,
8363    /// K-step) and reuse it across all BN tokens via mma.sync.m16n8k32.s8 — amortizing the weight
8364    /// read/decode N-fold (vs the dp4a matvec's per-token re-read). s32 accumulate is exact vs
8365    /// dp4a; only the final f32 block-scale rounding differs. Caller MUST have checked
8366    /// `gemm_supports(w)`. y[m,out] token-major. NVFP4 per-tensor macro-scale applied post.
8367    pub fn qmatvec_gemm(&self, w: &crate::model::GpuTensor, aq: &CudaSlice<i8>, ad: &CudaSlice<f32>,
8368                        m: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8369        use crate::model::GpuTensor;
8370        let in_f = w.in_features();
8371        let out_f = w.out_features();
8372        let (bytes, qtype, row_bytes, scale, rp) = match w {
8373            GpuTensor::Quant { bytes, qtype, row_bytes, scale, rp, .. } => (bytes, *qtype, *row_bytes, *scale, *rp),
8374            _ => unreachable!("gemm_supports guaranteed Quant"),
8375        };
8376        // wgmma arm (sm_90a, task 8): the m64n64k32 warpgroup kernel reads the rp4 split-plane
8377        // mirror AS-IS (qplane rows = its A operand, the half dplane its scales) and the same
8378        // (aq, ad) activation planes. Same numeric class as the mma kernel below (exact s32 per
8379        // 32-block, one f32 scale fold per block, ascending K) — argmax/tolerance gated like
8380        // every prefill GEMM, not bit-gated. MEMRA_WGMMA=0 restores the portable kernel.
8381        if cfg!(memra_hopper_mma) && qtype == QT_Q8_0 && out_f % 64 == 0 && wgmma_gemm_enabled() {
8382            if let GpuTensor::Quant { rp4: Some(m4), .. } = w {
8383                let mut y = self.qmatvec_gemm_q8_0_wgmma_raw(m4, aq, ad, m, in_f, out_f)?;
8384                if scale != 1.0 { self.scale_inplace(&mut y, scale, m * out_f)?; }
8385                return Ok(y);
8386            }
8387        }
8388        let name = match qtype {
8389            QT_Q8_0 => "qmatvec_gemm_q8_0", QT_Q4_K => "qmatvec_gemm_q4_K",
8390            QT_Q4_0 => if rp { "qmatvec_gemm_q4_0_rp" } else { "qmatvec_gemm_q4_0" },
8391            QT_Q5_K => "qmatvec_gemm_q5_K",
8392            QT_Q6_K => "qmatvec_gemm_q6_K",
8393            QT_NVFP4 => if rp { "qmatvec_gemm_nvfp4_rp" } else { "qmatvec_gemm_nvfp4" },
8394            _ => unreachable!(),
8395        };
8396        let f = self.func(name);
8397        let mut y = self.alloc_uninit::<f32>(m * out_f)?;  // full-overwrite GEMM output: skip memset
8398        // CTA tile MUST match the .cu per-kernel tile. MMQ-PORT: kernel1 (Q8_0/Q4_K/Q5_K) runs llama's
8399        // 128x128 SQUARE tile (K1_BM=128 x K1_BN=128, 8 warps); kernel2 (Q6_K/NVFP4) keeps 64x256, 4 warps
8400        // (the macro BM/BN in the .cu). Grid dims are selected by qtype so each launches its own tile.
8401        let is_k1 = matches!(qtype, QT_Q8_0 | QT_Q4_K | QT_Q5_K | QT_Q4_0);
8402        // TUNE SEAM: MEMRA_GEMM_K1_LAUNCH overrides kernel1's launch tile to match a -D-swept fatbin.
8403        let k1_tile = if is_k1 { k1_launch_override().unwrap_or((128, 128, 8)) } else { (128, 128, 8) };
8404        let (bm, bn): (u32, u32) = if is_k1 { (k1_tile.0, k1_tile.1) } else { (64, 256) };
8405        let warps: u32 = if is_k1 { k1_tile.2 } else {
8406            match qtype { QT_NVFP4 => 8, _ => 4 }
8407        };
8408        let cfg = LaunchConfig {
8409            grid_dim: ((out_f as u32 + bm - 1) / bm, (m as u32 + bn - 1) / bn, 1),
8410            block_dim: (32, warps, 1),
8411            shared_mem_bytes: 0,
8412        };
8413        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
8414        let __s_b = self.gpu.stream();
8415        let mut b = __s_b.launch_builder(&f);
8416        b.arg(bytes).arg(aq).arg(ad).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
8417        unsafe { b.launch(cfg)?; }
8418        if scale != 1.0 { self.scale_inplace(&mut y, scale, m * out_f)?; }
8419        Ok(y)
8420    }
8421
8422    /// Test entry: run the GEMM directly from raw weight bytes + qtype (no GpuTensor). Quantizes
8423    /// the f32 activation `x` to q8_1 internally then launches the tensor-core GEMM. NVFP4 per-tensor
8424    /// macro-scale is NOT applied here (caller passes it separately, like the dp4a path). Used by
8425    /// kernel_check for the bit-equivalence gate vs qmatvec_*_dp4a.
8426    pub fn qmatvec_gemm_raw(&self, bytes: &CudaSlice<u8>, x: &CudaSlice<f32>, m: usize, in_f: usize,
8427                            out_f: usize, qtype: i32, row_bytes: usize)
8428                            -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8429        let (aq, ad) = self.quantize_q8_1(x, m, in_f)?;
8430        let name = match qtype {
8431            QT_Q8_0 => "qmatvec_gemm_q8_0", QT_Q4_K => "qmatvec_gemm_q4_K",
8432            QT_Q4_0 => "qmatvec_gemm_q4_0",
8433            QT_Q5_K => "qmatvec_gemm_q5_K",
8434            QT_Q6_K => "qmatvec_gemm_q6_K", QT_NVFP4 => "qmatvec_gemm_nvfp4",
8435            QT_NVFP4_RP => "qmatvec_gemm_nvfp4_rp",
8436            _ => panic!("qmatvec_gemm_raw: qtype {qtype} has no GEMM kernel"),
8437        };
8438        let f = self.func(name);
8439        let mut y = self.alloc_uninit::<f32>(m * out_f)?;  // full-overwrite GEMM output: skip memset
8440        // MMQ-PORT: kernel1 (Q8_0/Q4_K/Q5_K) = llama 128x128 tile, 8 warps; kernel2 (Q6_K/NVFP4) = 64x256,
8441        // 4/8 warps. Grid tile per qtype (must match the .cu K1_BM/K1_BN vs BM/BN). KEEP IN SYNC w/ qmatvec_gemm.
8442        let is_k1 = matches!(qtype, QT_Q8_0 | QT_Q4_K | QT_Q5_K | QT_Q4_0);
8443        // TUNE SEAM: MEMRA_GEMM_K1_LAUNCH overrides kernel1's launch tile to match a -D-swept fatbin.
8444        let k1_tile = if is_k1 { k1_launch_override().unwrap_or((128, 128, 8)) } else { (128, 128, 8) };
8445        let (bm, bn): (u32, u32) = if is_k1 { (k1_tile.0, k1_tile.1) } else { (64, 256) };
8446        let warps: u32 = if is_k1 { k1_tile.2 } else {
8447            match qtype { QT_NVFP4 | QT_NVFP4_RP => 8, _ => 4 }
8448        };
8449        let cfg = LaunchConfig {
8450            grid_dim: ((out_f as u32 + bm - 1) / bm, (m as u32 + bn - 1) / bn, 1),
8451            block_dim: (32, warps, 1), shared_mem_bytes: 0,
8452        };
8453        let (inf, outf, mi, rb) = (in_f as i32, out_f as i32, m as i32, row_bytes as i64);
8454        let __s_b = self.gpu.stream();
8455        let mut b = __s_b.launch_builder(&f);
8456        b.arg(bytes).arg(&aq).arg(&ad).arg(&mut y).arg(&inf).arg(&outf).arg(&mi).arg(&rb);
8457        unsafe { b.launch(cfg)?; }
8458        Ok(y)
8459    }
8460
8461    /// H100 warpgroup GEMM raw entry (task 8): launch `qmatvec_gemm_q8_0_wgmma` on an rp4
8462    /// split-plane mirror + pre-quantized (aq, ad) activation planes. One warpgroup (128 thr)
8463    /// owns a 64x64 C tile; grid (out_f/64, ceil(m/64)). out_f % 64 == 0 REQUIRED (row loads
8464    /// and dplane scale reads are unguarded); the token edge is guarded in-kernel.
8465    /// Standalone harness verdict (tools/bench_q8_gemm_wgmma.cu, 4096x4096x512): rel 1.6e-05
8466    /// vs CPU ref, 179us vs the portable mma kernel's 688us (3.84x, unpipelined).
8467    pub fn qmatvec_gemm_q8_0_wgmma_raw(&self, rp4: &CudaSlice<u8>, aq: &CudaSlice<i8>,
8468                                       ad: &CudaSlice<f32>, m: usize, in_f: usize, out_f: usize)
8469                                       -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8470        assert!(out_f % 64 == 0 && in_f % 32 == 0, "wgmma GEMM needs out_f%64==0, in_f%32==0");
8471        let f = self.func("qmatvec_gemm_q8_0_wgmma");
8472        let mut y = self.alloc_uninit::<f32>(m * out_f)?;  // full-overwrite GEMM output
8473        let cfg = LaunchConfig {
8474            grid_dim: ((out_f / 64) as u32, (m as u32).div_ceil(64), 1),
8475            block_dim: (128, 1, 1), shared_mem_bytes: 0,
8476        };
8477        let (inf, outf, mi) = (in_f as i32, out_f as i32, m as i32);
8478        let __s_b = self.gpu.stream();
8479        let mut b = __s_b.launch_builder(&f);
8480        b.arg(rp4).arg(aq).arg(ad).arg(&mut y).arg(&inf).arg(&outf).arg(&mi);
8481        unsafe { b.launch(cfg)?; }
8482        Ok(y)
8483    }
8484
8485    /// y[i] *= s. NVFP4 per-tensor macro-scale broadcast over the whole output.
8486    pub fn scale_inplace(&self, y: &mut CudaSlice<f32>, s: f32, n: usize)
8487                         -> Result<(), Box<dyn std::error::Error>> {
8488        let f = self.func("scale_f32");
8489        let cfg = LaunchConfig::for_num_elems(n as u32);
8490        let (sf, ni) = (s, n as i32);
8491        let __s_b = self.gpu.stream();
8492        let mut b = __s_b.launch_builder(&f);
8493        b.arg(y).arg(&sf).arg(&ni);
8494        unsafe { b.launch(cfg)?; }
8495        Ok(())
8496    }
8497
8498    /// MEMRA_FULL_PREC dequant-on-use: expand a bf16-resident weight (`GpuTensor::FloatBf16`, raw
8499    /// bf16 bytes) to a transient f32 scratch of `n` elements, which then feeds the existing f32
8500    /// cuBLASLt GEMV. The scratch is freed when the caller drops it, so peak VRAM = resident bf16
8501    /// weights + ONE (largest) weight's f32 expansion + activations. SLOW IS FINE (research mode).
8502    pub fn bf16_to_f32(&self, data: &cudarc::driver::CudaView<'_, u8>, n: usize)
8503                       -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8504        let mut out = self.alloc_uninit::<f32>(n)?;
8505        let f = self.func("bf16_to_f32");
8506        let cfg = LaunchConfig::for_num_elems(n as u32);
8507        let ni = n as i32;
8508        let __s_b = self.gpu.stream();
8509        let mut b = __s_b.launch_builder(&f);
8510        b.arg(data).arg(&mut out).arg(&ni);
8511        unsafe { b.launch(cfg)?; }
8512        Ok(out)
8513    }
8514
8515    /// Chunked bf16 linear (MEMRA_FULL_PREC): y[m,out] = x @ W_bf16^T with the f32 dequant scratch
8516    /// bounded to CHUNK_ROWS rows (256MB at in_f=4096) instead of the whole weight — the 4GB
8517    /// lm_head expansion OOM'd the 24GB budget. Row-chunking partitions OUTPUT rows; each row's
8518    /// dot is computed by the identical kernel on identical bytes, so per-(token,row) results are
8519    /// bit-identical to the unchunked form. `exact` selects linear_decode_exact (per-column m=1
8520    /// calls, the spec-verify contract) vs plain linear.
8521    fn linear_bf16_chunked(&self, x: &CudaSlice<f32>, data: &CudaSlice<u8>, m: usize,
8522                           in_f: usize, out_f: usize, exact: bool)
8523                           -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8524        const CHUNK_BYTES: usize = 256 << 20;
8525        let chunk_rows = (CHUNK_BYTES / (in_f * 4)).max(1).min(out_f);
8526        if chunk_rows >= out_f {
8527            let wf32 = self.bf16_to_f32(&data.slice(0..in_f * out_f * 2), in_f * out_f)?;
8528            return if exact { self.linear_decode_exact(x, &wf32, m, in_f, out_f) }
8529                   else { self.linear(x, &wf32, m, in_f, out_f) };
8530        }
8531        let mut y = self.alloc_uninit::<f32>(m * out_f)?;
8532        let mut r0 = 0usize;
8533        while r0 < out_f {
8534            let rows = chunk_rows.min(out_f - r0);
8535            let wslice = data.slice(r0 * in_f * 2..(r0 + rows) * in_f * 2);
8536            let wf32 = self.bf16_to_f32(&wslice, in_f * rows)?;
8537            let yc = if exact { self.linear_decode_exact(x, &wf32, m, in_f, rows)? }
8538                     else { self.linear(x, &wf32, m, in_f, rows)? };
8539            // scatter [m, rows] into y[m, out_f] at column offset r0 (m is tiny in decode/verify)
8540            for mi in 0..m {
8541                let src = yc.slice(mi * rows..(mi + 1) * rows);
8542                let mut dst = y.slice_mut(mi * out_f + r0..mi * out_f + r0 + rows);
8543                self.gpu.stream().memcpy_dtod(&src, &mut dst)?;
8544            }
8545            r0 += rows;
8546        }
8547        Ok(y)
8548    }
8549
8550    /// On-device linear: y[m,out] = x[m,in] @ W[out,in]^T, weights row-major [out,in] (ggml).
8551    /// cuBLASLt col-major mapping (see memra_runtime::Gpu::linear_f32 for the derivation).
8552    /// DECODE-EXACT float linear: per-column m=1 cuBLASLt calls. cuBLASLt's reduction split is
8553    /// n-dependent (lt_ndep probe: m=1 vs m=2 col0 differs every bit), so spec-verify batches
8554    /// must not batch float matmuls the T=1 decode chain runs at m=1. Used by the small-t MoE
8555    /// router/shexp sites and matmul_decode_exact's Float arm.
8556    pub fn linear_decode_exact(&self, x: &CudaSlice<f32>, w: &CudaSlice<f32>, m_tokens: usize,
8557                               in_f: usize, out_f: usize)
8558                               -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8559        if m_tokens == 1 { return self.linear(x, w, 1, in_f, out_f); }
8560        let xv = self.view(x, m_tokens * in_f);
8561        let mut y = self.alloc_uninit::<f32>(m_tokens * out_f)?;
8562        for t in 0..m_tokens {
8563            let row = xv.slice(t * in_f..(t + 1) * in_f);
8564            let mut xr = self.alloc_uninit::<f32>(in_f)?;
8565            self.copy_view_into(&mut xr, 0, &row, in_f)?;
8566            let yr = self.linear(&xr, w, 1, in_f, out_f)?;
8567            self.copy_into(&mut y, t * out_f, &yr, out_f)?;
8568        }
8569        Ok(y)
8570    }
8571
8572    pub fn linear(&self, x: &CudaSlice<f32>, w: &CudaSlice<f32>, m_tokens: usize, in_f: usize, out_f: usize)
8573                  -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
8574        use cudarc::cublaslt::{Matmul, MatmulConfig};
8575        let mut c = self.alloc_uninit::<f32>(m_tokens * out_f)?;  // cuBLASLt beta=0: C fully written
8576        let cfg = MatmulConfig {
8577            transa: true, transb: false, transc: false,
8578            m: out_f as u64, n: m_tokens as u64, k: in_f as u64,
8579            alpha: 1.0, lda: in_f as i64, ldb: in_f as i64, beta: 0.0, ldc: out_f as i64,
8580            stride_a: None, stride_b: None, stride_c: None, stride_bias: None, batch_size: None,
8581        };
8582        unsafe { self.gpu.blas.matmul(cfg, w, x, &mut c, None, None)?; }
8583        Ok(c)
8584    }
8585
8586    /// Naive SDPA. Q:[head_dim,n_head,T], K/V:[head_dim,n_head_kv,T_kv] -> O:[head_dim,n_head,T].
8587    pub fn sdpa_naive(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
8588                      o: &mut CudaSlice<f32>, head_dim: usize, n_head: usize, n_head_kv: usize,
8589                      t: usize, t_kv: usize, scale: f32, causal: bool)
8590                      -> Result<(), Box<dyn std::error::Error>> {
8591        let f = self.func("sdpa_naive_f32");
8592        let cfg = LaunchConfig {
8593            grid_dim: (n_head as u32, t as u32, 1),
8594            block_dim: (128, 1, 1),
8595            shared_mem_bytes: (t_kv * 4) as u32,
8596        };
8597        let (hd, nh, nhkv, ti, tkvi, cz) = (head_dim as i32, n_head as i32, n_head_kv as i32, t as i32, t_kv as i32, causal as i32);
8598        let __s_b = self.gpu.stream();
8599        let mut b = __s_b.launch_builder(&f);
8600        b.arg(q).arg(k).arg(v).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi).arg(&scale).arg(&cz);
8601        unsafe { b.launch(cfg)?; }
8602        Ok(())
8603    }
8604
8605    /// Windowed sdpa_naive twin (gemma4 R6): masks keys older than q_pos-(window-1).
8606    #[allow(clippy::too_many_arguments)]
8607    pub fn sdpa_naive_w(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
8608                        o: &mut CudaSlice<f32>, head_dim: usize, n_head: usize, n_head_kv: usize,
8609                        t: usize, t_kv: usize, scale: f32, causal: bool, window: usize)
8610                        -> Result<(), Box<dyn std::error::Error>> {
8611        let f = self.func("sdpa_naive_w_f32");
8612        let cfg = LaunchConfig {
8613            grid_dim: (n_head as u32, t as u32, 1),
8614            block_dim: (128, 1, 1),
8615            shared_mem_bytes: (t_kv * 4) as u32,
8616        };
8617        let (hd, nh, nhkv, ti, tkvi, cz, wi) = (head_dim as i32, n_head as i32, n_head_kv as i32,
8618                                                t as i32, t_kv as i32, causal as i32, window as i32);
8619        let __s_b = self.gpu.stream();
8620        let mut b = __s_b.launch_builder(&f);
8621        b.arg(q).arg(k).arg(v).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi)
8622         .arg(&scale).arg(&cz).arg(&wi);
8623        unsafe { b.launch(cfg)?; }
8624        Ok(())
8625    }
8626
8627    /// SDPA where K/V are CudaViews into a resident KV cache (decode hot path, no host round-trip).
8628    pub fn sdpa_naive_view(&self, q: &CudaSlice<f32>, k: &cudarc::driver::CudaView<f32>,
8629                           v: &cudarc::driver::CudaView<f32>, o: &mut CudaSlice<f32>,
8630                           head_dim: usize, n_head: usize, n_head_kv: usize, t: usize, t_kv: usize,
8631                           scale: f32, causal: bool) -> Result<(), Box<dyn std::error::Error>> {
8632        let f = self.func("sdpa_naive_f32");
8633        let cfg = LaunchConfig {
8634            grid_dim: (n_head as u32, t as u32, 1), block_dim: (128, 1, 1),
8635            shared_mem_bytes: (t_kv * 4) as u32,
8636        };
8637        let (hd, nh, nhkv, ti, tkvi, cz) = (head_dim as i32, n_head as i32, n_head_kv as i32, t as i32, t_kv as i32, causal as i32);
8638        let __s_b = self.gpu.stream();
8639        let mut b = __s_b.launch_builder(&f);
8640        b.arg(q).arg(k).arg(v).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi).arg(&scale).arg(&cz);
8641        unsafe { b.launch(cfg)?; }
8642        Ok(())
8643    }
8644
8645    /// Correctness fallback for quantized resident K/V views. Dequantizes K and V once into f32
8646    /// workspaces, then calls `sdpa_naive`. This is an explicit API: the optimized prefill view
8647    /// dispatch remains unchanged, so callers can use it as a reference or compatibility path.
8648    /// Dequant a quantized KV view into caller-owned f32 buffers (one grid-stride launch).
8649    /// `g` picks the kf8vf8-module stamp for e4m3 caches (same flag contract as fa_decode/
8650    /// fa_prefill_view). Used by the E4B shared-KV prefill arms (2026-07-31) to feed the
8651    /// f32 fa_prefill_w / fa_prefill_hd512 twins from the target layer's quantized rows.
8652    #[allow(clippy::too_many_arguments)]
8653    pub fn fa_dequant_kv_view_f32(&self, k: &cudarc::driver::CudaView<u8>,
8654                                  v: &cudarc::driver::CudaView<u8>,
8655                                  kf: &mut CudaSlice<f32>, vf: &mut CudaSlice<f32>,
8656                                  kv_dim_k: usize, kv_dim_v: usize, t_kv: usize,
8657                                  k_tok_bytes: usize, v_tok_bytes: usize, g: bool)
8658                                  -> Result<(), Box<dyn std::error::Error>> {
8659        let f = if g { self.func_g("fa_dequant_kv_ws_f32") } else { self.func("fa_dequant_kv_ws_f32") };
8660        let total = (t_kv * (kv_dim_k + kv_dim_v)) as u64;
8661        let nblk = ((total + 255) / 256).min(65535 * 16) as u32;
8662        let cfg = LaunchConfig { grid_dim: (nblk.max(1), 1, 1), block_dim: (256, 1, 1),
8663                                 shared_mem_bytes: 0 };
8664        let (kdk, kdv, tkvi) = (kv_dim_k as i32, kv_dim_v as i32, t_kv as i32);
8665        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
8666        let __s_b = self.gpu.stream();
8667        let mut b = __s_b.launch_builder(&f);
8668        b.arg(k).arg(v).arg(&mut *kf).arg(&mut *vf).arg(&kdk).arg(&kdv).arg(&tkvi).arg(&ktb).arg(&vtb);
8669        unsafe { b.launch(cfg)?; }
8670        Ok(())
8671    }
8672
8673    #[allow(clippy::too_many_arguments)]
8674    pub fn sdpa_naive_quantized_view(
8675        &self,
8676        q: &CudaSlice<f32>,
8677        k: &cudarc::driver::CudaView<u8>,
8678        v: &cudarc::driver::CudaView<u8>,
8679        o: &mut CudaSlice<f32>,
8680        head_dim: usize,
8681        n_head: usize,
8682        n_head_kv: usize,
8683        t: usize,
8684        t_kv: usize,
8685        scale: f32,
8686        causal: bool,
8687        k_tok_bytes: usize,
8688        v_tok_bytes: usize,
8689    ) -> Result<(), Box<dyn std::error::Error>> {
8690        let kv_dim = n_head_kv * head_dim;
8691        let mut kf = self.uninit(t_kv * kv_dim)?;
8692        let mut vf = self.uninit(t_kv * kv_dim)?;
8693        let f = self.func("fa_dequant_kv_ws_f32");
8694        let total = (2 * t_kv * kv_dim) as u64;
8695        let nblk = ((total + 255) / 256).min(65535 * 16) as u32;
8696        let cfg = LaunchConfig {
8697            grid_dim: (nblk.max(1), 1, 1),
8698            block_dim: (256, 1, 1),
8699            shared_mem_bytes: 0,
8700        };
8701        let (kv_dim_i, t_kv_i) = (kv_dim as i32, t_kv as i32);
8702        let (k_tok_bytes_i, v_tok_bytes_i) = (k_tok_bytes as i64, v_tok_bytes as i64);
8703        let __s_b = self.gpu.stream();
8704        let mut b = __s_b.launch_builder(&f);
8705        b.arg(k)
8706            .arg(v)
8707            .arg(&mut kf)
8708            .arg(&mut vf)
8709            .arg(&kv_dim_i)
8710            .arg(&kv_dim_i)
8711            .arg(&t_kv_i)
8712            .arg(&k_tok_bytes_i)
8713            .arg(&v_tok_bytes_i);
8714        unsafe { b.launch(cfg)? };
8715        self.sdpa_naive(
8716            q, &kf, &vf, o, head_dim, n_head, n_head_kv, t, t_kv, scale, causal,
8717        )
8718    }
8719
8720    /// WINDOWED twin of `sdpa_naive_quantized_view` (step35 SWA prefill): dequant the KV byte
8721    /// view into f32 workspaces with the SAME `fa_dequant_kv_ws_f32` launch, then run
8722    /// `sdpa_naive_w` instead of `sdpa_naive`. `window == 0` is the unwindowed form (the kernel
8723    /// treats a non-positive window as "no window mask"), so this is a strict superset of the
8724    /// unwindowed function above and produces bit-identical output at window == 0.
8725    ///
8726    /// Why this exists: EVERY windowed FlashAttention stamp in flash_attn.cu is head_dim-256
8727    /// only (`fa_prefill_w_f32` == `fa_prefill_f32_body<256>`, and the quantized-view windowed
8728    /// twins likewise), while step35 is head_dim 128. Its SWA layers therefore have no windowed
8729    /// FA path and take this f32 floor in v0 — same cache bytes, same numeric class as the
8730    /// unwindowed quantized-view fallback, so the chunk-invariance contract holds on both.
8731    #[allow(clippy::too_many_arguments)]
8732    pub fn sdpa_naive_w_quantized_view(
8733        &self,
8734        q: &CudaSlice<f32>,
8735        k: &cudarc::driver::CudaView<u8>,
8736        v: &cudarc::driver::CudaView<u8>,
8737        o: &mut CudaSlice<f32>,
8738        head_dim: usize,
8739        n_head: usize,
8740        n_head_kv: usize,
8741        t: usize,
8742        t_kv: usize,
8743        scale: f32,
8744        causal: bool,
8745        window: usize,
8746        k_tok_bytes: usize,
8747        v_tok_bytes: usize,
8748    ) -> Result<(), Box<dyn std::error::Error>> {
8749        let kv_dim = n_head_kv * head_dim;
8750        let mut kf = self.uninit(t_kv * kv_dim)?;
8751        let mut vf = self.uninit(t_kv * kv_dim)?;
8752        let f = self.func("fa_dequant_kv_ws_f32");
8753        let total = (2 * t_kv * kv_dim) as u64;
8754        let nblk = ((total + 255) / 256).min(65535 * 16) as u32;
8755        let cfg = LaunchConfig {
8756            grid_dim: (nblk.max(1), 1, 1),
8757            block_dim: (256, 1, 1),
8758            shared_mem_bytes: 0,
8759        };
8760        let (kv_dim_i, t_kv_i) = (kv_dim as i32, t_kv as i32);
8761        let (k_tok_bytes_i, v_tok_bytes_i) = (k_tok_bytes as i64, v_tok_bytes as i64);
8762        let __s_b = self.gpu.stream();
8763        let mut b = __s_b.launch_builder(&f);
8764        b.arg(k)
8765            .arg(v)
8766            .arg(&mut kf)
8767            .arg(&mut vf)
8768            .arg(&kv_dim_i)
8769            .arg(&kv_dim_i)
8770            .arg(&t_kv_i)
8771            .arg(&k_tok_bytes_i)
8772            .arg(&v_tok_bytes_i);
8773        unsafe { b.launch(cfg)? };
8774        self.sdpa_naive_w(
8775            q, &kf, &vf, o, head_dim, n_head, n_head_kv, t, t_kv, scale, causal, window,
8776        )
8777    }
8778
8779    /// Hand-written FlashAttention prefill (sm_120, FA-2 online softmax on validated mma.sync,
8780    /// head_dim 256 or 128 (template-stamped twins), GQA, causal). Replaces sdpa_naive for T>1.
8781    /// Q/K/V/O [head_dim, n_head(_kv), T].
8782    pub fn fa_prefill(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
8783                      o: &mut CudaSlice<f32>, head_dim: usize, n_head: usize, n_head_kv: usize,
8784                      t: usize, t_kv: usize, scale: f32, causal: bool)
8785                      -> Result<(), Box<dyn std::error::Error>> {
8786        if portable_mma_gated() {
8787            return self.sdpa_naive(q, k, v, o, head_dim, n_head, n_head_kv,
8788                                   t, t_kv, scale, causal);
8789        }
8790        // FA3 v10 arm (task #20, OPT-IN MEMRA_FA3=1 — harness-proven 883us vs the shipped
8791        // kernel's 993us at T=2048): TMA-swizzled wgmma FA, fresh causal hd256 only.
8792        // NEW NUMERIC CONFIG (GDN-mma precedent): online softmax / bf16-P class — the
8793        // run-gen argmax + greedy-stream batteries arbitrate; not bit-paired.
8794        // PROMOTED default-ON hopper (2026-07-27): 3-seed 2048-prime -> 128-decode
8795        // streams MATCH vs mma, full battery green, lane interleaved 5/5 (+2.4%).
8796        // MEMRA_FA3=0 reverts; kernel-check pins the mma config regardless.
8797        let fa3_on = head_dim == 256 && causal && t == t_kv
8798            && match std::env::var("MEMRA_FA3").as_deref() {
8799                Ok("0") => false,
8800                Ok("1") => true,
8801                _ => cfg!(memra_hopper_mma),
8802            };
8803        if fa3_on {
8804            let n = t * n_head * head_dim;
8805            let nkv = t * n_head_kv * head_dim;
8806            let mut q16 = self.alloc_u8_uninit(n * 2)?;
8807            let mut k16 = self.alloc_u8_uninit(nkv * 2)?;
8808            let mut v16 = self.alloc_u8_uninit(nkv * 2)?;
8809            self.f32_to_bf16_into(q, &mut q16, n)?;
8810            self.f32_to_bf16_into(k, &mut k16, nkv)?;
8811            self.f32_to_bf16_into(v, &mut v16, nkv)?;
8812            let rc = {
8813                use cudarc::driver::{DevicePtr, DevicePtrMut};
8814                let stream = self.gpu.stream();
8815                let (qp, _g1) = q16.device_ptr(&stream);
8816                let (kp, _g2) = k16.device_ptr(&stream);
8817                let (vp, _g3) = v16.device_ptr(&stream);
8818                let (op, _g4) = o.device_ptr_mut(&stream);
8819                unsafe {
8820                    memra_fa3_prefill(qp as *const core::ffi::c_void,
8821                                     kp as *const core::ffi::c_void,
8822                                     vp as *const core::ffi::c_void,
8823                                     op as *mut f32,
8824                                     t as i32, n_head as i32, n_head_kv as i32,
8825                                     head_dim as i32, scale,
8826                                     stream.cu_stream() as *mut core::ffi::c_void)
8827                }
8828            };
8829            if rc != 0 {
8830                return Err(format!("memra_fa3_prefill rc={rc}").into());
8831            }
8832            return Ok(());
8833        }
8834        // FLOOR PORT (P2+P0a+P0b+P1): 4 warps/CTA, BLOCK_Q=64 query rows, BK=32 KV tile,
8835        // Q-in-reg + register-O, grid.y=n_head_kv (4 Q-heads share staged K/V).
8836        // P1 plain arm (MEMRA_FA_P1=1 opt-in until the qwen battery): the engine-study body
8837        // (FA2 schedule + boundary split + swizzle) on the non-windowed lane. bf16 pre-convert.
8838        static FA_P1: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
8839        let fa_p1 = *FA_P1.get_or_init(|| std::env::var("MEMRA_FA_P1").as_deref() == Ok("1"));
8840        if fa_p1 && head_dim == 256 && !std::env::var("MEMRA_FA_FLOOR").is_ok() {
8841            const BLOCK_Q: usize = 64; const BKX: usize = 32;
8842            let f = self.func("fa_prefill_bf16_p1");
8843            let shmem = (2 * (2 * BKX * head_dim + BLOCK_Q * BKX)
8844                       + 4 * (BLOCK_Q * BKX + 2 * BLOCK_Q)) as u32;
8845            use cudarc::driver::sys::CUfunction_attribute_enum as A;
8846            f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
8847            let cfg = LaunchConfig {
8848                grid_dim: ((t as u32 + BLOCK_Q as u32 - 1) / BLOCK_Q as u32, n_head as u32, 1),
8849                block_dim: (32, 4, 1), shared_mem_bytes: shmem,
8850            };
8851            let (hd, nh, nhkv, ti, tkvi, cz) = (head_dim as i32, n_head as i32,
8852                n_head_kv as i32, t as i32, t_kv as i32, causal as i32);
8853            let qb = self.f32_to_bf16(q, t * n_head * head_dim)?;
8854            let kb = self.f32_to_bf16(k, t_kv * n_head_kv * head_dim)?;
8855            let vb = self.f32_to_bf16(v, t_kv * n_head_kv * head_dim)?;
8856            let __s_b = self.gpu.stream();
8857            let mut b = __s_b.launch_builder(&f);
8858            b.arg(&qb).arg(&kb).arg(&vb).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti)
8859             .arg(&tkvi).arg(&scale).arg(&cz);
8860            unsafe { b.launch(cfg)?; }
8861            return Ok(());
8862        }
8863        // Edge 5a (DEFAULT): fa_prefill_f32_pp — register-resident softmax (no sSw smem
8864        // round-trip), the FA3 softmax-GEMM overlap variant. ncu (pp512): short_scoreboard
8865        // 4.32->3.47, wait 1.99->1.45, per-call ~577us->~440us (1.31x) at flat 12.1% warps /
8866        // 255 regs / 2 CTAs (occupancy preserved). Bit-safe: 9B+27B argmax MATCH, rel 2.55e-3
8867        // vs floor 3.03e-3. MEMRA_FA_FLOOR reverts to the serialized-softmax floor kernel.
8868        const BK: usize = 32;
8869        // W2 lane (MEMRA_FA_PP_W2=1, ncu 2026-07-26): 2-warp/32-row CTA tile doubles grid.x —
8870        // bit-identical per-row math, pure coverage trade for the 6.25%-occupancy starvation.
8871        let w2 = std::env::var("MEMRA_FA_PP_W2").as_deref() == Ok("1");
8872        let (block_q, warps, w2_sfx): (usize, u32, &str) =
8873            if w2 { (32, 2, "_w2") } else { (64, 4, "") };
8874        // hd128 twins (2026-07-07): the prefill kernels are template-stamped at 256 (original
8875        // names, dispatch unchanged) and 128 (`_hd128`, the MiniMax-M3 class). Callers gate
8876        // other head_dims to sdpa_naive before reaching here.
8877        let hd_sfx = fa_hd_suffix(head_dim)?;
8878        let floor = std::env::var("MEMRA_FA_FLOOR").is_ok();
8879        // BF16-KV staging lane (2026-07-26, default ON): the kernel converts K/V to bf16
8880        // during staging anyway — pre-converting to bf16 mirrors is BIT-IDENTICAL (same
8881        // __float2bfloat16 values into the same mma) and turns the 67%-of-stalls scalar
8882        // staging into int4 vector copies. MEMRA_FA_BF16KV=0 reverts.
8883        let bf16kv = !floor && !w2
8884            && std::env::var("MEMRA_FA_BF16KV").as_deref() != Ok("0");
8885        let (kb16, vb16) = if bf16kv {
8886            let n = t_kv * n_head_kv * head_dim;
8887            let mut kb = self.alloc_u8_uninit(n * 2)?;
8888            let mut vb = self.alloc_u8_uninit(n * 2)?;
8889            let fcv = self.func("f32_to_bf16_bulk");
8890            let ni = n as i64;
8891            let cfgc = LaunchConfig::for_num_elems((n as u32).div_ceil(4));
8892            let __s_b = self.gpu.stream();
8893            let mut b = __s_b.launch_builder(&fcv);
8894            b.arg(k).arg(&mut kb).arg(&ni);
8895            unsafe { b.launch(cfgc)?; }
8896            let __s_b = self.gpu.stream();
8897            let mut b = __s_b.launch_builder(&fcv);
8898            b.arg(v).arg(&mut vb).arg(&ni);
8899            unsafe { b.launch(cfgc)?; }
8900            (Some(kb), Some(vb))
8901        } else {
8902            (None, None)
8903        };
8904        let f = self.func(&if bf16kv {
8905            format!("fa_prefill_bf16kv_pp{hd_sfx}")
8906        } else {
8907            format!("fa_prefill_f32{}{}{hd_sfx}",
8908                    if floor { "" } else { "_pp" },
8909                    if floor { "" } else { w2_sfx })
8910        });
8911        // persistent smem: bf16*(KV_STAGES*(sK + sV) + sP) + f32*(sS + sM + sL);
8912        // the bf16kv ring doubles the K/V stages (KV_STAGES=2).
8913        let kv_stages = if bf16kv { 2 } else { 1 };
8914        let shmem = (2 * (kv_stages * 2 * BK * head_dim + block_q * BK)
8915                   + 4 * (block_q * BK + 2 * block_q)) as u32;
8916        use cudarc::driver::sys::CUfunction_attribute_enum as A;
8917        f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
8918        let cfg = LaunchConfig {
8919            grid_dim: ((t as u32 + block_q as u32 - 1) / block_q as u32, n_head as u32, 1),
8920            block_dim: (32, warps, 1), shared_mem_bytes: shmem,
8921        };
8922        let (hd, nh, nhkv, ti, tkvi, cz) = (head_dim as i32, n_head as i32, n_head_kv as i32, t as i32, t_kv as i32, causal as i32);
8923        let __s_b = self.gpu.stream();
8924        let mut b = __s_b.launch_builder(&f);
8925        b.arg(q);
8926        match (&kb16, &vb16) {
8927            (Some(kb), Some(vb)) => { b.arg(kb).arg(vb); }
8928            _ => { b.arg(k).arg(v); }
8929        }
8930        b.arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi).arg(&scale).arg(&cz);
8931        unsafe { b.launch(cfg)?; }
8932        Ok(())
8933    }
8934
8935    /// Windowed FA prefill (gemma4 SWA layers past the sliding window, hd256): fa_prefill's
8936    /// exact dispatch (pp default, MEMRA_FA_FLOOR seam) with the sliding-window mask + tile
8937    /// skip in-kernel. Replaces the O(T*T_kv) scalar sdpa_naive_w on the prime path.
8938    #[allow(clippy::too_many_arguments)]
8939    pub fn fa_prefill_w(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
8940                        o: &mut CudaSlice<f32>, head_dim: usize, n_head: usize, n_head_kv: usize,
8941                        t: usize, t_kv: usize, scale: f32, causal: bool, window: usize)
8942                        -> Result<(), Box<dyn std::error::Error>> {
8943        // sm_90a rides the mma twins (portable_mma_gated, 2026-07-31 — the raw
8944        // portable_cuda gate was stale-conservative on Hopper; fa_prefill already flipped).
8945        if portable_mma_gated() {
8946            return self.sdpa_naive_w(q, k, v, o, head_dim, n_head, n_head_kv,
8947                                     t, t_kv, scale, causal, window);
8948        }
8949        // Default: bf16-prestaged twin (same treatment as hd512 — Q/K/V pre-converted once,
8950        // int4 stage copies; bit-identical, kernel_check-gated). MEMRA_FAW_STAGE=f32 reverts;
8951        // MEMRA_FA_FLOOR keeps the f32 floor stamp untouched.
8952        static FAW_F32: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
8953        let faw_f32 = *FAW_F32.get_or_init(|| {
8954            std::env::var("MEMRA_FAW_STAGE").as_deref() == Ok("f32")
8955        });
8956        let floor = std::env::var("MEMRA_FA_FLOOR").is_ok();
8957        self.fa_prefill_w_arm(q, k, v, o, head_dim, n_head, n_head_kv, t, t_kv, scale, causal,
8958                              window, floor || faw_f32, floor)
8959    }
8960
8961    /// Windowed FA prefill with PRE-CONVERTED bf16 operands (producer-emitted; 31B glue lane).
8962    /// Launches the P1 stamp directly — callers guarantee qb/kb/vb hold the exact bf16 of q/k/v.
8963    #[allow(clippy::too_many_arguments)]
8964    pub fn fa_prefill_w_pre(&self, qb: &CudaSlice<u8>, kb: &CudaSlice<u8>, vb: &CudaSlice<u8>,
8965                            o: &mut CudaSlice<f32>, head_dim: usize, n_head: usize,
8966                            n_head_kv: usize, t: usize, t_kv: usize, scale: f32, causal: bool,
8967                            window: usize, v_f16: bool)
8968                            -> Result<(), Box<dyn std::error::Error>> {
8969        const BLOCK_Q: usize = 64; const BK: usize = 32;
8970        debug_assert_eq!(head_dim, 256);
8971        let hp = fa_f16pv_on() && faw_hp_on() && n_head % 2 == 0
8972            && (n_head / n_head_kv) % 2 == 0;
8973        debug_assert!(!v_f16 || hp, "f16 V emitted but the SWA hp arm is off");
8974        if hp {
8975            const BLOCK_QH: usize = 32;
8976            // V bytes must be f16 for the h2 stamp; producer normally emits f16 (v_f16),
8977            // else re-encode through the pooled scratch (stream-ordered reuse).
8978            let mut vguard = self.fa_vf16_scratch.lock().unwrap();
8979            let vh: &CudaSlice<u8> = if v_f16 { vb } else {
8980                let n = t_kv * n_head_kv * head_dim;
8981                if vguard.as_ref().map(|b| b.len() < n * 2).unwrap_or(true) {
8982                    *vguard = Some(self.alloc_uninit::<u8>(n * 2)?);
8983                }
8984                self.bf16_to_f16_into(vb, n, vguard.as_mut().unwrap())?;
8985                vguard.as_ref().unwrap()
8986            };
8987            let f = self.func("fa_prefill_w_bf16_p1h2");
8988            let shmem = (2 * (2 * BK * head_dim + 2 * BLOCK_QH * BK)
8989                       + 4 * (2 * BLOCK_QH)) as u32;
8990            use cudarc::driver::sys::CUfunction_attribute_enum as A;
8991            f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
8992            let cfg = LaunchConfig {
8993                grid_dim: ((t as u32).div_ceil(BLOCK_QH as u32), (n_head / 2) as u32, 1),
8994                block_dim: (32, 4, 1), shared_mem_bytes: shmem,
8995            };
8996            let (hd, nh, nhkv, ti, tkvi, cz, wi) = (head_dim as i32, n_head as i32,
8997                n_head_kv as i32, t as i32, t_kv as i32, causal as i32, window as i32);
8998            let __s_b = self.gpu.stream();
8999            let mut b = __s_b.launch_builder(&f);
9000            b.arg(qb).arg(kb).arg(vh).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi)
9001             .arg(&scale).arg(&cz).arg(&wi);
9002            unsafe { b.launch(cfg)?; }
9003            return Ok(());
9004        }
9005        let f = self.func("fa_prefill_w_bf16_p1");
9006        let shmem = (2 * (2 * BK * head_dim + BLOCK_Q * BK)
9007                   + 4 * (BLOCK_Q * BK + 2 * BLOCK_Q)) as u32;
9008        use cudarc::driver::sys::CUfunction_attribute_enum as A;
9009        f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9010        let cfg = LaunchConfig {
9011            grid_dim: ((t as u32 + BLOCK_Q as u32 - 1) / BLOCK_Q as u32, n_head as u32, 1),
9012            block_dim: (32, 4, 1), shared_mem_bytes: shmem,
9013        };
9014        let (hd, nh, nhkv, ti, tkvi, cz, wi) = (head_dim as i32, n_head as i32,
9015            n_head_kv as i32, t as i32, t_kv as i32, causal as i32, window as i32);
9016        let __s_b = self.gpu.stream();
9017        let mut b = __s_b.launch_builder(&f);
9018        b.arg(qb).arg(kb).arg(vb).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi)
9019         .arg(&scale).arg(&cz).arg(&wi);
9020        unsafe { b.launch(cfg)?; }
9021        Ok(())
9022    }
9023
9024    /// Windowed FA prefill with the stage arm FORCED — the kernel_check bit-identity entry.
9025    #[allow(clippy::too_many_arguments)]
9026    pub fn fa_prefill_w_arm(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
9027                            o: &mut CudaSlice<f32>, head_dim: usize, n_head: usize,
9028                            n_head_kv: usize, t: usize, t_kv: usize, scale: f32, causal: bool,
9029                            window: usize, f32_stage: bool, floor: bool)
9030                            -> Result<(), Box<dyn std::error::Error>> {
9031        const BLOCK_Q: usize = 64; const BK: usize = 32;
9032        debug_assert_eq!(head_dim, 256, "fa_prefill_w is stamped hd256 only");
9033        // P1 (2026-07-22 engine study): per-head Br=64 stamp with the FA2 schedule (V-copy
9034        // over GEMM0, next-K over softmax+GEMM1) + boundary/interior mask split. FP order
9035        // preserved -> bit-identical (gated). MEMRA_FAW_P1=0 reverts to the g4/o2 arms.
9036        static P1_ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
9037        let p1 = !floor && !f32_stage
9038            && *P1_ON.get_or_init(|| {
9039                std::env::var("MEMRA_FAW_P1").map(|v| v != "0").unwrap_or(true)
9040            });
9041        let hp = p1 && fa_f16pv_on() && faw_hp_on() && n_head % 2 == 0
9042            && (n_head / n_head_kv) % 2 == 0;
9043        if hp {
9044            const BLOCK_QH: usize = 32;
9045            let f = self.func("fa_prefill_w_bf16_p1h2");
9046            let shmem = (2 * (2 * BK * head_dim + 2 * BLOCK_QH * BK)
9047                       + 4 * (2 * BLOCK_QH)) as u32;
9048            use cudarc::driver::sys::CUfunction_attribute_enum as A;
9049            f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9050            let cfg = LaunchConfig {
9051                grid_dim: ((t as u32).div_ceil(BLOCK_QH as u32), (n_head / 2) as u32, 1),
9052                block_dim: (32, 4, 1), shared_mem_bytes: shmem,
9053            };
9054            let (hd, nh, nhkv, ti, tkvi, cz, wi) = (head_dim as i32, n_head as i32,
9055                n_head_kv as i32, t as i32, t_kv as i32, causal as i32, window as i32);
9056            let qb = self.f32_to_bf16(q, t * n_head * head_dim)?;
9057            let kb = self.f32_to_bf16(k, t_kv * n_head_kv * head_dim)?;
9058            let vh = self.f32_to_f16(v, t_kv * n_head_kv * head_dim)?;
9059            let __s_b = self.gpu.stream();
9060            let mut b = __s_b.launch_builder(&f);
9061            b.arg(&qb).arg(&kb).arg(&vh).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi)
9062             .arg(&scale).arg(&cz).arg(&wi);
9063            unsafe { b.launch(cfg)?; }
9064            return Ok(());
9065        }
9066        if p1 {
9067            let f = self.func("fa_prefill_w_bf16_p1");
9068            let shmem = (2 * (2 * BK * head_dim + BLOCK_Q * BK)
9069                       + 4 * (BLOCK_Q * BK + 2 * BLOCK_Q)) as u32;
9070            use cudarc::driver::sys::CUfunction_attribute_enum as A;
9071            f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9072            let cfg = LaunchConfig {
9073                grid_dim: ((t as u32 + BLOCK_Q as u32 - 1) / BLOCK_Q as u32, n_head as u32, 1),
9074                block_dim: (32, 4, 1), shared_mem_bytes: shmem,
9075            };
9076            let (hd, nh, nhkv, ti, tkvi, cz, wi) = (head_dim as i32, n_head as i32,
9077                n_head_kv as i32, t as i32, t_kv as i32, causal as i32, window as i32);
9078            let qb = self.f32_to_bf16(q, t * n_head * head_dim)?;
9079            let kb = self.f32_to_bf16(k, t_kv * n_head_kv * head_dim)?;
9080            let vb = self.f32_to_bf16(v, t_kv * n_head_kv * head_dim)?;
9081            let __s_b = self.gpu.stream();
9082            let mut b = __s_b.launch_builder(&f);
9083            b.arg(&qb).arg(&kb).arg(&vb).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi)
9084             .arg(&scale).arg(&cz).arg(&wi);
9085            unsafe { b.launch(cfg)?; }
9086            return Ok(());
9087        }
9088        // MQA head-grouping (MEMRA_FAW_G4=0 reverts): 4 heads/CTA share the staged K/V —
9089        // per-(head,row) FP chain identical to the per-head stamp -> bit-identical (gated).
9090        static G4_ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
9091        let g4 = !floor && !f32_stage && n_head_kv == 1 && n_head % 4 == 0
9092            && *G4_ON.get_or_init(|| {
9093                std::env::var("MEMRA_FAW_G4").map(|v| v != "0").unwrap_or(true)
9094            });
9095        if g4 {
9096            const SP_M: usize = 16;
9097            // Occupancy-2 twin (MEMRA_FAW_O2=0 reverts): one shared K/V buffer inside the dead
9098            // Q-stage region -> ~36.5KB smem, 2 CTA/SM (the llama hd256 mechanism). Bit-identical.
9099            static O2_ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
9100            let o2 = *O2_ON.get_or_init(|| {
9101                std::env::var("MEMRA_FAW_O2").map(|v| v != "0").unwrap_or(true)
9102            });
9103            let f = self.func(if o2 { "fa_prefill_w_bf16_g4o2" } else { "fa_prefill_w_bf16_g4" });
9104            let shmem = if o2 {
9105                (2 * (4 * SP_M * head_dim + 4 * SP_M * BK) + 4 * (4 * SP_M)) as u32
9106            } else {
9107                (2 * (2 * BK * head_dim + 4 * SP_M * head_dim + 4 * SP_M * BK)
9108                    + 4 * (4 * SP_M)) as u32
9109            };
9110            use cudarc::driver::sys::CUfunction_attribute_enum as A;
9111            f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9112            let cfg = LaunchConfig {
9113                grid_dim: ((t as u32).div_ceil(SP_M as u32), (n_head / 4) as u32, 1),
9114                block_dim: (32, 4, 1), shared_mem_bytes: shmem,
9115            };
9116            let (hd, nh, nhkv, ti, tkvi, cz, wi) = (head_dim as i32, n_head as i32,
9117                n_head_kv as i32, t as i32, t_kv as i32, causal as i32, window as i32);
9118            let qb = self.f32_to_bf16(q, t * n_head * head_dim)?;
9119            let kb = self.f32_to_bf16(k, t_kv * n_head_kv * head_dim)?;
9120            let vb = self.f32_to_bf16(v, t_kv * n_head_kv * head_dim)?;
9121            let __s_b = self.gpu.stream();
9122            let mut b = __s_b.launch_builder(&f);
9123            b.arg(&qb).arg(&kb).arg(&vb).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi)
9124             .arg(&scale).arg(&cz).arg(&wi);
9125            unsafe { b.launch(cfg)?; }
9126            return Ok(());
9127        }
9128        let f = self.func(if floor { "fa_prefill_w_f32" }
9129                          else if f32_stage { "fa_prefill_w_f32_pp" }
9130                          else { "fa_prefill_w_bf16_pp" });
9131        let shmem = (2 * (2 * BK * head_dim + BLOCK_Q * BK)
9132                   + 4 * (BLOCK_Q * BK + 2 * BLOCK_Q)) as u32;
9133        use cudarc::driver::sys::CUfunction_attribute_enum as A;
9134        f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9135        let cfg = LaunchConfig {
9136            grid_dim: ((t as u32 + BLOCK_Q as u32 - 1) / BLOCK_Q as u32, n_head as u32, 1),
9137            block_dim: (32, 4, 1), shared_mem_bytes: shmem,
9138        };
9139        let (hd, nh, nhkv, ti, tkvi, cz, wi) = (head_dim as i32, n_head as i32, n_head_kv as i32,
9140                                                t as i32, t_kv as i32, causal as i32, window as i32);
9141        if f32_stage {
9142            let __s_b = self.gpu.stream();
9143            let mut b = __s_b.launch_builder(&f);
9144            b.arg(q).arg(k).arg(v).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi)
9145             .arg(&scale).arg(&cz).arg(&wi);
9146            unsafe { b.launch(cfg)?; }
9147        } else {
9148            let qb = self.f32_to_bf16(q, t * n_head * head_dim)?;
9149            let kb = self.f32_to_bf16(k, t_kv * n_head_kv * head_dim)?;
9150            let vb = self.f32_to_bf16(v, t_kv * n_head_kv * head_dim)?;
9151            let __s_b = self.gpu.stream();
9152            let mut b = __s_b.launch_builder(&f);
9153            b.arg(&qb).arg(&kb).arg(&vb).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi)
9154             .arg(&scale).arg(&cz).arg(&wi);
9155            unsafe { b.launch(cfg)?; }
9156        }
9157        Ok(())
9158    }
9159
9160    /// hd512 FA prefill (gemma4 GLOBAL layers): BLOCK_Q=32 x 2 warps, Q staged in smem,
9161    /// grid.z = 2 O-halves (each CTA computes the full 512-dim scores, accumulates half the
9162    /// V dims). Replaces the scalar sdpa_naive on the prime path's globals.
9163    #[allow(clippy::too_many_arguments)]
9164    pub fn fa_prefill_hd512(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
9165                            o: &mut CudaSlice<f32>, head_dim: usize, n_head: usize,
9166                            n_head_kv: usize, t: usize, t_kv: usize, scale: f32, causal: bool)
9167                            -> Result<(), Box<dyn std::error::Error>> {
9168        // sm_90a rides the mma twins (portable_mma_gated, 2026-07-31 — same flip as _w).
9169        if portable_mma_gated() {
9170            return self.sdpa_naive(q, k, v, o, head_dim, n_head, n_head_kv,
9171                                   t, t_kv, scale, causal);
9172        }
9173        // Default: pre-convert Q/K/V to bf16 once and stage int4 (8 bf16/copy) — at 1 CTA/SM the
9174        // synchronous stage serializes with compute and MQA re-stages the same K/V per head CTA;
9175        // pre-converting halves staged bytes and cuts stage instructions 8x. BIT-IDENTICAL to the
9176        // f32-staged kernel (the converter applies the same __float2bfloat16 the stage applied;
9177        // kernel_check gates the identity). MEMRA_FA512_STAGE=f32 = rollback to the f32 kernel.
9178        static F32_STAGE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
9179        let f32_stage = *F32_STAGE.get_or_init(|| {
9180            std::env::var("MEMRA_FA512_STAGE").as_deref() == Ok("f32")
9181        });
9182        // Single-pass arm (MEMRA_FA512_SP=0 reverts to the z=2 bf16 kernel): GEMM0 split-K across
9183        // the 2 warps instead of recomputed per O-half CTA — the 2026-07-22 kernel-diff excess.
9184        // Own numeric config (partial-sum order) — battery-gated.
9185        static SP_ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
9186        let sp = !f32_stage
9187            && *SP_ON.get_or_init(|| {
9188                std::env::var("MEMRA_FA512_SP").map(|v| v != "0").unwrap_or(true)
9189            });
9190        self.fa_prefill_hd512_arm(q, k, v, o, head_dim, n_head, n_head_kv, t, t_kv, scale,
9191                                  causal, f32_stage, sp, sp && fa_f16pv_on())
9192    }
9193
9194    /// hd512 single-pass FA with PRE-CONVERTED bf16 operands (producer-emitted).
9195    #[allow(clippy::too_many_arguments)]
9196    pub fn fa_prefill_hd512_pre(&self, qb: &CudaSlice<u8>, kb: &CudaSlice<u8>, vb: &CudaSlice<u8>,
9197                                o: &mut CudaSlice<f32>, head_dim: usize, n_head: usize,
9198                                n_head_kv: usize, t: usize, t_kv: usize, scale: f32, causal: bool,
9199                                v_f16: bool)
9200                                -> Result<(), Box<dyn std::error::Error>> {
9201        debug_assert_eq!(head_dim, 512);
9202        const SP_M: usize = 16; const BKS: usize = 32;
9203        // f16-P/V door (MEMRA_FA_F16PV=1): P and the P@V accumulation in f16 (llama's fa=1 VKQ
9204        // class); KQ/softmax/rescale-band/final-normalize stay f32. Own numeric config —
9205        // battery-gated. V bytes must be f16 for the sp16 kernel (stage/ldmatrix are typeless).
9206        let f16pv = fa_f16pv_on();
9207        let nw = if f16pv { fa512_wide_warps() } else { 2 };
9208        let hp = f16pv && fa512_hp_on() && n_head % 2 == 0 && (n_head / n_head_kv) % 2 == 0;
9209        debug_assert!(!v_f16 || f16pv, "f16 V emitted without the door on");
9210        let mut vguard = self.fa_vf16_scratch.lock().unwrap();
9211        let vref: &CudaSlice<u8> = if f16pv && !v_f16 {
9212            // Fallback re-encode (producer emitted bf16); the emit lane normally hands f16.
9213            let n = t_kv * n_head_kv * head_dim;
9214            let need = n * 2;
9215            if vguard.as_ref().map(|b| b.len() < need).unwrap_or(true) {
9216                *vguard = Some(self.alloc_uninit::<u8>(need)?);
9217            }
9218            let dst = vguard.as_mut().unwrap();
9219            self.bf16_to_f16_into(vb, n, dst)?;
9220            vguard.as_ref().unwrap()
9221        } else { vb };
9222        let f = self.func(if hp { "fa_prefill_bf16_hd512_sp16h2" }
9223                          else { match (f16pv, nw) {
9224                              (true, 4) => "fa_prefill_bf16_hd512_sp16w4",
9225                              (true, _) => "fa_prefill_bf16_hd512_sp16",
9226                              _ => "fa_prefill_bf16_hd512_sp",
9227                          } });
9228        let (nwarp, npart) = if hp { (4usize, 4usize) } else if nw > 2 { (nw, nw) } else { (2, 1) };
9229        // h2 drops sQ (Q register-resident) and doubles sP/sS/sL for the head pair.
9230        let shmem = if hp {
9231            (2 * (2 * BKS * head_dim + 2 * SP_M * BKS)
9232               + 4 * (2 * npart * SP_M * BKS + 2 * SP_M)) as u32
9233        } else {
9234            (2 * (SP_M * head_dim + 2 * BKS * head_dim + SP_M * BKS)
9235               + 4 * (npart * SP_M * BKS + SP_M)) as u32
9236        };
9237        use cudarc::driver::sys::CUfunction_attribute_enum as A;
9238        f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9239        let grid_y = if hp { (n_head / 2) as u32 } else { n_head as u32 };
9240        let cfg = LaunchConfig {
9241            grid_dim: ((t as u32).div_ceil(SP_M as u32), grid_y, 1),
9242            block_dim: (32, nwarp as u32, 1), shared_mem_bytes: shmem,
9243        };
9244        let (hd, nh, nhkv, ti, tkvi, cz) = (head_dim as i32, n_head as i32, n_head_kv as i32,
9245                                            t as i32, t_kv as i32, causal as i32);
9246        let __s_b = self.gpu.stream();
9247        let mut b = __s_b.launch_builder(&f);
9248        b.arg(qb).arg(kb).arg(vref).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi)
9249         .arg(&scale).arg(&cz);
9250        unsafe { b.launch(cfg)?; }
9251        Ok(())
9252    }
9253
9254    /// hd512 FA prefill with the stage/sp arms FORCED — the kernel_check gate entry
9255    /// (`fa_prefill_hd512` picks the arms from MEMRA_FA512_STAGE / MEMRA_FA512_SP).
9256    #[allow(clippy::too_many_arguments)]
9257    pub fn fa_prefill_hd512_arm(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
9258                                o: &mut CudaSlice<f32>, head_dim: usize, n_head: usize,
9259                                n_head_kv: usize, t: usize, t_kv: usize, scale: f32, causal: bool,
9260                                f32_stage: bool, sp: bool, f16pv: bool)
9261                                -> Result<(), Box<dyn std::error::Error>> {
9262        debug_assert_eq!(head_dim, 512, "fa_prefill_hd512 is hd512 only");
9263        if sp && !f32_stage {
9264            // Single-pass: 16 q-rows/CTA, 2 warps, grid (ceil(T/16), n_head, 1).
9265            // smem: sQ[16][512] + sK[32][512] + sV[32][512] + sP[16][32] (bf16) + sS[16][32]+sL f32.
9266            // f16pv: sp16 kernel — f16 P + f16 P@V accum, V operand encoded f16.
9267            const SP_M: usize = 16; const BKS: usize = 32;
9268            let nw = if f16pv { fa512_wide_warps() } else { 2 };
9269            let hp = f16pv && fa512_hp_on() && n_head % 2 == 0 && (n_head / n_head_kv) % 2 == 0;
9270            let f = self.func(if hp { "fa_prefill_bf16_hd512_sp16h2" }
9271                              else { match (f16pv, nw) {
9272                                  (true, 4) => "fa_prefill_bf16_hd512_sp16w4",
9273                                  (true, _) => "fa_prefill_bf16_hd512_sp16",
9274                                  _ => "fa_prefill_bf16_hd512_sp",
9275                              } });
9276            let (nwarp, npart) = if hp { (4usize, 4usize) } else if nw > 2 { (nw, nw) } else { (2, 1) };
9277            let shmem = if hp {
9278                (2 * (2 * BKS * head_dim + 2 * SP_M * BKS)
9279                   + 4 * (2 * npart * SP_M * BKS + 2 * SP_M)) as u32
9280            } else {
9281                (2 * (SP_M * head_dim + 2 * BKS * head_dim + SP_M * BKS)
9282                   + 4 * (npart * SP_M * BKS + SP_M)) as u32
9283            };
9284            use cudarc::driver::sys::CUfunction_attribute_enum as A;
9285            f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9286            let grid_y = if hp { (n_head / 2) as u32 } else { n_head as u32 };
9287            let cfg = LaunchConfig {
9288                grid_dim: ((t as u32).div_ceil(SP_M as u32), grid_y, 1),
9289                block_dim: (32, nwarp as u32, 1), shared_mem_bytes: shmem,
9290            };
9291            let (hd, nh, nhkv, ti, tkvi, cz) = (head_dim as i32, n_head as i32, n_head_kv as i32,
9292                                                t as i32, t_kv as i32, causal as i32);
9293            let qb = self.f32_to_bf16(q, t * n_head * head_dim)?;
9294            let kb = self.f32_to_bf16(k, t_kv * n_head_kv * head_dim)?;
9295            let vb = if f16pv { self.f32_to_f16(v, t_kv * n_head_kv * head_dim)? }
9296                     else { self.f32_to_bf16(v, t_kv * n_head_kv * head_dim)? };
9297            let __s_b = self.gpu.stream();
9298            let mut b = __s_b.launch_builder(&f);
9299            b.arg(&qb).arg(&kb).arg(&vb).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi)
9300             .arg(&scale).arg(&cz);
9301            unsafe { b.launch(cfg)?; }
9302            return Ok(());
9303        }
9304        const BLOCK_Q: usize = 32; const BK: usize = 32; const HALF: usize = 256;
9305        let f = self.func(if f32_stage { "fa_prefill_f32_hd512" } else { "fa_prefill_bf16_hd512" });
9306        // sQ[32][512] + sK[BK][512] + sV[BK][256] + sP[32][BK] (bf16) + sL[32] f32
9307        let shmem = (2 * (BLOCK_Q * head_dim + BK * head_dim + BK * HALF + BLOCK_Q * BK)
9308                   + 4 * BLOCK_Q) as u32;
9309        use cudarc::driver::sys::CUfunction_attribute_enum as A;
9310        f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9311        let cfg = LaunchConfig {
9312            grid_dim: ((t as u32 + BLOCK_Q as u32 - 1) / BLOCK_Q as u32, n_head as u32, 2),
9313            block_dim: (32, 2, 1), shared_mem_bytes: shmem,
9314        };
9315        let (hd, nh, nhkv, ti, tkvi, cz) = (head_dim as i32, n_head as i32, n_head_kv as i32,
9316                                            t as i32, t_kv as i32, causal as i32);
9317        if f32_stage {
9318            let __s_b = self.gpu.stream();
9319            let mut b = __s_b.launch_builder(&f);
9320            b.arg(q).arg(k).arg(v).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi)
9321             .arg(&scale).arg(&cz);
9322            unsafe { b.launch(cfg)?; }
9323        } else {
9324            let qb = self.f32_to_bf16(q, t * n_head * head_dim)?;
9325            let kb = self.f32_to_bf16(k, t_kv * n_head_kv * head_dim)?;
9326            let vb = self.f32_to_bf16(v, t_kv * n_head_kv * head_dim)?;
9327            let __s_b = self.gpu.stream();
9328            let mut b = __s_b.launch_builder(&f);
9329            b.arg(&qb).arg(&kb).arg(&vb).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi)
9330             .arg(&scale).arg(&cz);
9331            unsafe { b.launch(cfg)?; }
9332        }
9333        Ok(())
9334    }
9335
9336    /// rope_neox2 with bf16 EMIT (31B glue lane): identical rope math/stores plus the post-rope
9337    /// values written as bf16 — the FA q/k operands come from this launch (bit-identical to the
9338    /// separate f32_to_bf16 the FA entries would run).
9339    #[allow(clippy::too_many_arguments)]
9340    pub fn rope_neox2_bf16e(&self, q: &mut CudaSlice<f32>, k: &mut CudaSlice<f32>,
9341                            qb: &mut CudaSlice<u8>, kb: &mut CudaSlice<u8>,
9342                            pos: &CudaSlice<i32>, head_dim: usize, n_dims: usize,
9343                            nh_q: usize, nh_k: usize, n_tokens: usize, base: f32,
9344                            freq_scale: f32, ff: Option<&CudaSlice<f32>>)
9345                            -> Result<(), Box<dyn std::error::Error>> {
9346        let f = self.func("rope_neox2_bf16e_f32");
9347        let rows = ((nh_q + nh_k) * n_tokens) as u32;
9348        let cfg = LaunchConfig { grid_dim: (rows, 1, 1),
9349                                 block_dim: ((head_dim / 2) as u32, 1, 1), shared_mem_bytes: 0 };
9350        let theta_scale = base.powf(-2.0 / n_dims as f32);
9351        let (hd, nd, nhq, nhk, nt) = (head_dim as i32, n_dims as i32, nh_q as i32,
9352                                      nh_k as i32, n_tokens as i32);
9353        let __s_b = self.gpu.stream();
9354        let mut b = __s_b.launch_builder(&f);
9355        match ff {
9356            Some(t) => { b.arg(&mut *q).arg(&mut *k).arg(&mut *qb).arg(&mut *kb).arg(pos)
9357                          .arg(&hd).arg(&nd).arg(&nhq).arg(&nhk).arg(&nt)
9358                          .arg(&theta_scale).arg(&freq_scale).arg(t);
9359                         unsafe { b.launch(cfg)?; } }
9360            None => { let null: u64 = 0;
9361                      b.arg(&mut *q).arg(&mut *k).arg(&mut *qb).arg(&mut *kb).arg(pos)
9362                       .arg(&hd).arg(&nd).arg(&nhq).arg(&nhk).arg(&nt)
9363                       .arg(&theta_scale).arg(&freq_scale).arg(&null);
9364                      unsafe { b.launch(cfg)?; } }
9365        }
9366        Ok(())
9367    }
9368
9369    /// Flat f32 -> bf16 conversion into a fresh scratch buffer (2 bytes/elem). `n % 4 == 0`
9370    /// (float4 in, 4x bf16 out). Feeds the bf16-staged hd512 FA prefill.
9371    pub fn f32_to_bf16(&self, x: &CudaSlice<f32>, n: usize)
9372                       -> Result<CudaSlice<u8>, Box<dyn std::error::Error>> {
9373        assert!(n % 4 == 0, "f32_to_bf16 requires n % 4 == 0, got {n}");
9374        let mut y = self.alloc_uninit::<u8>(n * 2)?;
9375        let f = self.func("f32_to_bf16_flat");
9376        let n_i = n as i64;
9377        let cfg = LaunchConfig {
9378            grid_dim: (((n / 4) as u32).div_ceil(256), 1, 1),
9379            block_dim: (256, 1, 1), shared_mem_bytes: 0,
9380        };
9381        let __s_b = self.gpu.stream();
9382        let mut b = __s_b.launch_builder(&f);
9383        b.arg(x).arg(&mut y).arg(&n_i);
9384        unsafe { b.launch(cfg)?; }
9385        Ok(y)
9386    }
9387
9388    pub fn f32_to_f16(&self, x: &CudaSlice<f32>, n: usize)
9389                      -> Result<CudaSlice<u8>, Box<dyn std::error::Error>> {
9390        assert!(n % 4 == 0, "f32_to_f16 requires n % 4 == 0, got {n}");
9391        let mut y = self.alloc_uninit::<u8>(n * 2)?;
9392        let f = self.func("f32_to_f16_flat");
9393        let n_i = n as i64;
9394        let cfg = LaunchConfig {
9395            grid_dim: (((n / 4) as u32).div_ceil(256), 1, 1),
9396            block_dim: (256, 1, 1), shared_mem_bytes: 0,
9397        };
9398        let __s_b = self.gpu.stream();
9399        let mut b = __s_b.launch_builder(&f);
9400        b.arg(x).arg(&mut y).arg(&n_i);
9401        unsafe { b.launch(cfg)?; }
9402        Ok(y)
9403    }
9404
9405    /// bf16 bytes -> f16 bytes, n elements (the f16-P/V door's V re-encode on the emit lane).
9406    pub fn bf16_to_f16(&self, xb: &CudaSlice<u8>, n: usize)
9407                       -> Result<CudaSlice<u8>, Box<dyn std::error::Error>> {
9408        let mut y = self.alloc_uninit::<u8>(n * 2)?;
9409        self.bf16_to_f16_into(xb, n, &mut y)?;
9410        Ok(y)
9411    }
9412
9413    /// Same conversion into a caller-owned (pooled) buffer; `y.len() >= n*2`.
9414    pub fn bf16_to_f16_into(&self, xb: &CudaSlice<u8>, n: usize, y: &mut CudaSlice<u8>)
9415                            -> Result<(), Box<dyn std::error::Error>> {
9416        assert!(n % 2 == 0, "bf16_to_f16 requires n % 2 == 0, got {n}");
9417        assert!(y.len() >= n * 2);
9418        let f = self.func("bf16_to_f16_flat");
9419        let n2 = (n / 2) as i64;
9420        let cfg = LaunchConfig {
9421            grid_dim: (((n / 2) as u32).div_ceil(256), 1, 1),
9422            block_dim: (256, 1, 1), shared_mem_bytes: 0,
9423        };
9424        let __s_b = self.gpu.stream();
9425        let mut b = __s_b.launch_builder(&f);
9426        b.arg(xb).arg(y).arg(&n2);
9427        unsafe { b.launch(cfg)?; }
9428        Ok(())
9429    }
9430
9431    /// task #18 (attn side): varlen FA — bf16 K/V mirrors (2 launches) + ONE
9432    /// fa_prefill_bf16kv launch for every fresh sequence. Same per-block math as the
9433    /// per-seq path (bit-gateable). Caller guarantees: fresh causal (T_kv == T),
9434    /// head_dim in {256, 128}, bf16kv lane on.
9435    #[allow(clippy::too_many_arguments)]
9436    pub fn fa_prefill_vl8(&self, seqs: &[FaSeqVl], head_dim: usize, n_head: usize,
9437                          n_head_kv: usize, scale: f32)
9438                          -> Result<(), Box<dyn std::error::Error>> {
9439        const BK: usize = 32;
9440        let b = seqs.len();
9441        assert!(b >= 1 && b <= 8);
9442        let mut packed = [FaSeqVl::default(); 8];
9443        packed[..b].copy_from_slice(seqs);
9444        let v = FaVl8(packed);
9445        let max_t = seqs.iter().map(|s| s.t).max().unwrap() as u32;
9446        let ept = (n_head_kv * head_dim) as i32;
9447        {
9448            let f = self.func("fa_mirror_vl");
9449            let max_n = (max_t as i64) * ept as i64;
9450            let blocks = ((max_n as u32).div_ceil(4)).div_ceil(256);
9451            for which in 0..2i32 {
9452                let cfg = LaunchConfig { grid_dim: (blocks, 1, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
9453                let __s_lb = self.gpu.stream();
9454                let mut lb = __s_lb.launch_builder(&f);
9455                lb.arg(&v).arg(&ept).arg(&which);
9456                unsafe { lb.launch(cfg)?; }
9457            }
9458        }
9459        let hd_sfx = fa_hd_suffix(head_dim)?;
9460        let f = self.func(&format!("fa_prefill_bf16kv_vl{hd_sfx}"));
9461        let block_q = 64usize;
9462        let kv_stages = 2usize;
9463        let shmem = (2 * (kv_stages * 2 * BK * head_dim + block_q * BK)
9464                   + 4 * (block_q * BK + 2 * block_q)) as u32;
9465        use cudarc::driver::sys::CUfunction_attribute_enum as A;
9466        f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9467        let cfg = LaunchConfig {
9468            grid_dim: (max_t.div_ceil(block_q as u32), n_head as u32, b as u32),
9469            block_dim: (32, 4, 1), shared_mem_bytes: shmem,
9470        };
9471        let (hd, nh, nhkv) = (head_dim as i32, n_head as i32, n_head_kv as i32);
9472        let __s_lb = self.gpu.stream();
9473        let mut lb = __s_lb.launch_builder(&f);
9474        lb.arg(&v).arg(&hd).arg(&nh).arg(&nhkv).arg(&scale);
9475        unsafe { lb.launch(cfg)?; }
9476        Ok(())
9477    }
9478
9479    /// task #18 (attn pre-FA): varlen split + QK-norm + RoPE + KV-append — FOUR launches
9480    /// for every fresh sequence (was 6 x B, plus the q/k/v split copies which the view
9481    /// inputs remove entirely). Fresh-only (append at t0=0, RoPE pos = token index).
9482    #[allow(clippy::too_many_arguments)]
9483    pub fn attn_pre_vl8(&self, seqs: &[AttnPreVl], wq: &CudaSlice<f32>, wk: &CudaSlice<f32>,
9484                        head_dim: usize, rope_dims: usize, n_head: usize, n_head_kv: usize,
9485                        eps: f32, freq_base: f32, freq_scale: f32,
9486                        kv_dim_k: usize, kv_dim_v: usize,
9487                        k_tok_bytes: usize, v_tok_bytes: usize)
9488                        -> Result<(), Box<dyn std::error::Error>> {
9489        let b = seqs.len();
9490        assert!(b >= 1 && b <= 8);
9491        let mut packed = [AttnPreVl::default(); 8];
9492        packed[..b].copy_from_slice(seqs);
9493        let v = AttnPreVl8(packed);
9494        let max_t = seqs.iter().map(|s| s.t).max().unwrap() as u32;
9495        let (hd, nh, nhkv) = (head_dim as i32, n_head as i32, n_head_kv as i32);
9496        {
9497            let f = self.func("q_gate_split_vl");
9498            let n = max_t * (n_head * head_dim) as u32;
9499            let cfg = LaunchConfig { grid_dim: (n.div_ceil(256), 1, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
9500            let __s_lb = self.gpu.stream();
9501            let mut lb = __s_lb.launch_builder(&f);
9502            lb.arg(&v).arg(&hd).arg(&nh);
9503            unsafe { lb.launch(cfg)?; }
9504        }
9505        {
9506            let f = self.func("attn_rms_vl");
9507            let cfg = LaunchConfig { grid_dim: (max_t * n_head as u32, 2, b as u32), block_dim: (rms_block(), 1, 1), shared_mem_bytes: 0 };
9508            let __s_lb = self.gpu.stream();
9509            let mut lb = __s_lb.launch_builder(&f);
9510            lb.arg(&v).arg(wq).arg(wk).arg(&hd).arg(&nh).arg(&nhkv).arg(&eps);
9511            unsafe { lb.launch(cfg)?; }
9512        }
9513        {
9514            let f = self.func("attn_rope_vl");
9515            let theta_scale = freq_base.powf(-2.0 / rope_dims as f32);
9516            let nd = rope_dims as i32;
9517            let cfg = LaunchConfig { grid_dim: (max_t * n_head as u32, 2, b as u32), block_dim: ((head_dim / 2) as u32, 1, 1), shared_mem_bytes: 0 };
9518            let __s_lb = self.gpu.stream();
9519            let mut lb = __s_lb.launch_builder(&f);
9520            lb.arg(&v).arg(&hd).arg(&nd).arg(&nh).arg(&nhkv).arg(&theta_scale).arg(&freq_scale);
9521            unsafe { lb.launch(cfg)?; }
9522        }
9523        {
9524            let f = self.func("append_kv_vl");
9525            let nblk = (kv_dim_k.max(kv_dim_v) / 32) as u32;
9526            let cfg = LaunchConfig { grid_dim: (nblk, max_t, b as u32), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
9527            let (kdk, kdv) = (kv_dim_k as i32, kv_dim_v as i32);
9528            let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
9529            let __s_lb = self.gpu.stream();
9530            let mut lb = __s_lb.launch_builder(&f);
9531            lb.arg(&v).arg(&kdk).arg(&kdv).arg(&ktb).arg(&vtb);
9532            unsafe { lb.launch(cfg)?; }
9533        }
9534        Ok(())
9535    }
9536
9537    /// FA prefill where K/V are QUANTIZED CudaViews into the resident byte KV cache (the T=K verify
9538    /// path, MTP-PLAN §D.3). Uses `fa_prefill_q` (inline-dequant during stage-to-smem). The view's
9539    /// base+offset pointer is honored; the kernel reads [0..t_kv*tok_bytes). Q is the T fresh query
9540    /// rows; t = T, t_kv = cache len. k_tok_bytes/v_tok_bytes are the per-token byte strides.
9541    pub fn fa_prefill_view(&self, q: &CudaSlice<f32>, k: &cudarc::driver::CudaView<u8>,
9542                           v: &cudarc::driver::CudaView<u8>, o: &mut CudaSlice<f32>,
9543                           head_dim: usize, n_head: usize, n_head_kv: usize,
9544                           t: usize, t_kv: usize, scale: f32, causal: bool,
9545                           k_tok_bytes: usize, v_tok_bytes: usize, g: bool)
9546                           -> Result<(), Box<dyn std::error::Error>> {
9547        if portable_mma_gated() {
9548            return self.sdpa_naive_quantized_view(q, k, v, o, head_dim, n_head, n_head_kv,
9549                                                  t, t_kv, scale, causal,
9550                                                  k_tok_bytes, v_tok_bytes);
9551        }
9552        const BLOCK_Q: usize = 64; const BK: usize = 32;
9553        // g = e4m3 cache: the kernel parses via DQ_K_ELEM/DQ_V_ELEM (format macros) — the
9554        // kf8vf8-module stamp reads fp8 with the identical MMA/softmax/PV body.
9555        let name = format!("fa_prefill_q{}", fa_hd_suffix(head_dim)?);
9556        let f = if g { self.func_g(&name) } else { self.func(&name) };
9557        let shmem = (2 * (2 * BK * head_dim + BLOCK_Q * BK)
9558                   + 4 * (BLOCK_Q * BK + 2 * BLOCK_Q)) as u32;
9559        use cudarc::driver::sys::CUfunction_attribute_enum as A;
9560        f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9561        let cfg = LaunchConfig {
9562            grid_dim: ((t as u32 + BLOCK_Q as u32 - 1) / BLOCK_Q as u32, n_head as u32, 1),
9563            block_dim: (32, 4, 1), shared_mem_bytes: shmem,
9564        };
9565        let (hd, nh, nhkv, ti, tkvi, cz) = (head_dim as i32, n_head as i32, n_head_kv as i32, t as i32, t_kv as i32, causal as i32);
9566        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
9567        let __s_b = self.gpu.stream();
9568        let mut b = __s_b.launch_builder(&f);
9569        b.arg(q).arg(k).arg(v).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi).arg(&scale).arg(&cz)
9570         .arg(&ktb).arg(&vtb);
9571        unsafe { b.launch(cfg)?; }
9572        Ok(())
9573    }
9574
9575    /// ARC B (2026-07-05): dequant-once chunk-prime FA. Same contract as `fa_prefill_view`, but
9576    /// instead of every (q-block, head) CTA re-dequanting the whole quantized KV stream inline
9577    /// (T/64 x n_head redundant at chunk prime — 30.5% of the 32k prime wall), dequant the full
9578    /// [t_kv, kv_dim] K and V ONCE into a resident bf16 workspace (fa_dequant_kv_ws_bf16), then
9579    /// run `fa_prefill_qw` (the bf16-workspace twin) over it. EXACT: the workspace holds the same
9580    /// __float2bfloat16(dq_*_elem(...)) values fa_prefill_q stages to smem, and the twin's MMA/
9581    /// softmax/PV code is byte-identical -> bit-identical O (kernel_check pins bitdiff=0).
9582    /// The workspace allocation is REUSED across layers/chunks (grown to the largest shape);
9583    /// contents are rewritten per call. MEMRA_PRIME_DEQW=0 falls back to fa_prefill_view (callers gate).
9584    #[allow(clippy::too_many_arguments)]
9585    pub fn fa_prefill_view_ws(&self, q: &CudaSlice<f32>, k: &cudarc::driver::CudaView<u8>,
9586                              v: &cudarc::driver::CudaView<u8>, o: &mut CudaSlice<f32>,
9587                              head_dim: usize, n_head: usize, n_head_kv: usize,
9588                              t: usize, t_kv: usize, scale: f32, causal: bool,
9589                              k_tok_bytes: usize, v_tok_bytes: usize, g: bool)
9590                              -> Result<(), Box<dyn std::error::Error>> {
9591        if portable_mma_gated() {
9592            return self.sdpa_naive_quantized_view(q, k, v, o, head_dim, n_head, n_head_kv,
9593                                                  t, t_kv, scale, causal,
9594                                                  k_tok_bytes, v_tok_bytes);
9595        }
9596        const BLOCK_Q: usize = 64; const BK: usize = 32;
9597        let kv_dim_k = n_head_kv * head_dim;
9598        let kv_dim_v = n_head_kv * head_dim;
9599        let k_ws_bytes = t_kv * kv_dim_k * 2;   // bf16
9600        let v_ws_bytes = t_kv * kv_dim_v * 2;
9601        // Lock held across BOTH launches: enqueue-only (µs), all compute serializes on gpu.stream.
9602        let mut guard = self.prime_deqw_ws.lock().unwrap();
9603        let need_grow = match guard.as_ref() {
9604            Some((kw, vw)) => kw.len() < k_ws_bytes || vw.len() < v_ws_bytes,
9605            None => true,
9606        };
9607        if need_grow {
9608            let grow = |cur: usize, need: usize| if cur >= need { cur } else { need };
9609            let (ck, cv) = guard.as_ref().map(|(a, b)| (a.len(), b.len())).unwrap_or((0, 0));
9610            *guard = Some((self.alloc_u8(grow(ck, k_ws_bytes))?, self.alloc_u8(grow(cv, v_ws_bytes))?));
9611        }
9612        let (kw, vw) = guard.as_mut().unwrap();
9613        // pass 1: dequant K+V once into the bf16 workspace (grid-stride, 1 thread/elem)
9614        {
9615            // only THIS pass parses KV bytes — pass 2 reads the bf16 workspace (format-free).
9616            let f = if g { self.func_g("fa_dequant_kv_ws_bf16") } else { self.func("fa_dequant_kv_ws_bf16") };
9617            let total = (t_kv * (kv_dim_k + kv_dim_v)) as u64;
9618            let nblk = ((total + 255) / 256).min(65535 * 16) as u32;
9619            let cfg = LaunchConfig { grid_dim: (nblk.max(1), 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
9620            let (kdk, kdv, tkvi) = (kv_dim_k as i32, kv_dim_v as i32, t_kv as i32);
9621            let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
9622            let __s_b = self.gpu.stream();
9623            let mut b = __s_b.launch_builder(&f);
9624            b.arg(k).arg(v).arg(&mut *kw).arg(&mut *vw).arg(&kdk).arg(&kdv).arg(&tkvi).arg(&ktb).arg(&vtb);
9625            unsafe { b.launch(cfg)?; }
9626        }
9627        // pass 2: the bf16-workspace prefill twin (same tile sizes/loop structure as fa_prefill_q).
9628        // DEFAULT: cp.async double-buffered staging twin (fa_prefill_qw_db, +32KB smem for the
9629        // second K/V tile pair, 1 CTA/SM): overlaps tile n+1's L2->smem copy with tile n's MMA.
9630        // Bit-identical output (staging is a pure byte copy; kernel_check pins bitdiff=0 under
9631        // both twins). A/B (27B g7e, N=3): 32k prime 17.10->16.51s, 16k 9.09->8.65s — the copy
9632        // latency hides behind the MMA pipe and beats the 2-CTA/SM occupancy of the sync twin.
9633        // MEMRA_PRIME_DEQW_DB=0 falls back to the single-buffer twin.
9634        let db = std::env::var("MEMRA_PRIME_DEQW_DB").map(|v| v != "0").unwrap_or(true);
9635        {
9636            let hd_sfx = fa_hd_suffix(head_dim)?;
9637            let f = self.func(&format!("fa_prefill_qw{}{hd_sfx}", if db { "_db" } else { "" }));
9638            let shmem = if db {
9639                // 4x KV tile buffers (bf16) + sP (bf16) + sL (f32)
9640                (2 * (4 * BK * head_dim + BLOCK_Q * BK) + 4 * BLOCK_Q) as u32
9641            } else {
9642                (2 * (2 * BK * head_dim + BLOCK_Q * BK)
9643                       + 4 * (BLOCK_Q * BK + 2 * BLOCK_Q)) as u32
9644            };
9645            use cudarc::driver::sys::CUfunction_attribute_enum as A;
9646            f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9647            let cfg = LaunchConfig {
9648                grid_dim: ((t as u32 + BLOCK_Q as u32 - 1) / BLOCK_Q as u32, n_head as u32, 1),
9649                block_dim: (32, 4, 1), shared_mem_bytes: shmem,
9650            };
9651            let (hd, nh, nhkv, ti, tkvi, cz) = (head_dim as i32, n_head as i32, n_head_kv as i32, t as i32, t_kv as i32, causal as i32);
9652            let (kdk, kdv) = (kv_dim_k as i32, kv_dim_v as i32);
9653            let __s_b = self.gpu.stream();
9654            let mut b = __s_b.launch_builder(&f);
9655            b.arg(q).arg(&*kw).arg(&*vw).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi).arg(&scale).arg(&cz)
9656             .arg(&kdk).arg(&kdv);
9657            unsafe { b.launch(cfg)?; }
9658        }
9659        Ok(())
9660    }
9661
9662    /// WINDOWED `fa_prefill_view_ws` twin at head_dim 128 (lane/pp-prefill 2026-08-07):
9663    /// step35's SWA prefill (win=512, 33 of 45 layers) previously had NO windowed FA prefill
9664    /// stamp — every windowed twin was hd256-only — and took `sdpa_naive_w_quantized_view`,
9665    /// the f32 floor, at 565 ms/layer on a pp4096 where the hd128 FA family does the harder
9666    /// causal-4096 in 3.3 ms (41% of the whole prime; research/pp-prefill-20260807 anatomy).
9667    /// Same two-pass shape as the unwindowed function: dequant K/V ONCE into the resident
9668    /// bf16 workspace, then the windowed qw kernel (`fa_prefill_qw_db_w_hd128`, cp.async
9669    /// double-buffered; MEMRA_PRIME_DEQW_DB=0 selects the single-buffer twin). The window
9670    /// mask is `fa_prefill_f32_body`'s exact predicate; `window == 0` is bit-identical to
9671    /// `fa_prefill_view_ws` by construction (default-arg body). NEW NUMERIC CLASS vs the
9672    /// f32 floor on SWA rows (bf16 MMA online-softmax vs f32 serial softmax) — adoption is
9673    /// gated by the full battery, and the class must change UNIFORMLY for a whole request
9674    /// (kernel selection keys on seq_end, never per chunk — the chunkfix law).
9675    /// hd128-only deliberately: the only windowed-prefill consumer at another head_dim is
9676    /// gemma4 (hd256), which already has `fa_prefill_w_f32`.
9677    #[allow(clippy::too_many_arguments)]
9678    pub fn fa_prefill_view_ws_w_hd128(&self, q: &CudaSlice<f32>, k: &cudarc::driver::CudaView<u8>,
9679                                      v: &cudarc::driver::CudaView<u8>, o: &mut CudaSlice<f32>,
9680                                      head_dim: usize, n_head: usize, n_head_kv: usize,
9681                                      t: usize, t_kv: usize, scale: f32, causal: bool,
9682                                      window: usize, k_tok_bytes: usize, v_tok_bytes: usize)
9683                                      -> Result<(), Box<dyn std::error::Error>> {
9684        assert_eq!(head_dim, 128, "fa_prefill_view_ws_w_hd128: only the hd128 twin is stamped");
9685        if portable_mma_gated() {
9686            return self.sdpa_naive_w_quantized_view(q, k, v, o, head_dim, n_head, n_head_kv,
9687                                                    t, t_kv, scale, causal, window,
9688                                                    k_tok_bytes, v_tok_bytes);
9689        }
9690        const BLOCK_Q: usize = 64; const BK: usize = 32;
9691        let kv_dim_k = n_head_kv * head_dim;
9692        let kv_dim_v = n_head_kv * head_dim;
9693        let k_ws_bytes = t_kv * kv_dim_k * 2;   // bf16
9694        let v_ws_bytes = t_kv * kv_dim_v * 2;
9695        let mut guard = self.prime_deqw_ws.lock().unwrap();
9696        let need_grow = match guard.as_ref() {
9697            Some((kw, vw)) => kw.len() < k_ws_bytes || vw.len() < v_ws_bytes,
9698            None => true,
9699        };
9700        if need_grow {
9701            let grow = |cur: usize, need: usize| if cur >= need { cur } else { need };
9702            let (ck, cv) = guard.as_ref().map(|(a, b)| (a.len(), b.len())).unwrap_or((0, 0));
9703            *guard = Some((self.alloc_u8(grow(ck, k_ws_bytes))?, self.alloc_u8(grow(cv, v_ws_bytes))?));
9704        }
9705        let (kw, vw) = guard.as_mut().unwrap();
9706        // pass 1: dequant K+V once into the bf16 workspace (identical to fa_prefill_view_ws —
9707        // the workspace bytes are the SAME __float2bfloat16(dq(...)) values either way).
9708        {
9709            let f = self.func("fa_dequant_kv_ws_bf16");
9710            let total = (t_kv * (kv_dim_k + kv_dim_v)) as u64;
9711            let nblk = ((total + 255) / 256).min(65535 * 16) as u32;
9712            let cfg = LaunchConfig { grid_dim: (nblk.max(1), 1, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
9713            let (kdk, kdv, tkvi) = (kv_dim_k as i32, kv_dim_v as i32, t_kv as i32);
9714            let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
9715            let __s_b = self.gpu.stream();
9716            let mut b = __s_b.launch_builder(&f);
9717            b.arg(k).arg(v).arg(&mut *kw).arg(&mut *vw).arg(&kdk).arg(&kdv).arg(&tkvi).arg(&ktb).arg(&vtb);
9718            unsafe { b.launch(cfg)?; }
9719        }
9720        // pass 2: the WINDOWED qw twin (db default, same as the unwindowed wrapper).
9721        let db = std::env::var("MEMRA_PRIME_DEQW_DB").map(|v| v != "0").unwrap_or(true);
9722        {
9723            let f = self.func(if db { "fa_prefill_qw_db_w_hd128" } else { "fa_prefill_qw_w_hd128" });
9724            let shmem = if db {
9725                (2 * (4 * BK * head_dim + BLOCK_Q * BK) + 4 * BLOCK_Q) as u32
9726            } else {
9727                (2 * (2 * BK * head_dim + BLOCK_Q * BK)
9728                       + 4 * (BLOCK_Q * BK + 2 * BLOCK_Q)) as u32
9729            };
9730            use cudarc::driver::sys::CUfunction_attribute_enum as A;
9731            f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9732            let cfg = LaunchConfig {
9733                grid_dim: ((t as u32 + BLOCK_Q as u32 - 1) / BLOCK_Q as u32, n_head as u32, 1),
9734                block_dim: (32, 4, 1), shared_mem_bytes: shmem,
9735            };
9736            let (hd, nh, nhkv, ti, tkvi, cz) = (head_dim as i32, n_head as i32, n_head_kv as i32, t as i32, t_kv as i32, causal as i32);
9737            let (kdk, kdv, wnd) = (kv_dim_k as i32, kv_dim_v as i32, window as i32);
9738            let __s_b = self.gpu.stream();
9739            let mut b = __s_b.launch_builder(&f);
9740            b.arg(q).arg(&*kw).arg(&*vw).arg(o).arg(&hd).arg(&nh).arg(&nhkv).arg(&ti).arg(&tkvi).arg(&scale).arg(&cz)
9741             .arg(&kdk).arg(&kdv).arg(&wnd);
9742            unsafe { b.launch(cfg)?; }
9743        }
9744        Ok(())
9745    }
9746
9747    /// FA decode (T=1 split-K) over the resident QUANTIZED KV cache (q8_0 K / q5_1 V) as u8 views.
9748    /// Replaces sdpa_naive_view for decode; inline-dequants per element. k_tok_bytes/v_tok_bytes are
9749    /// the per-token byte strides (differ: q8_0=34*nblk, q5_1=24*nblk per token).
9750    pub fn fa_decode(&self, q: &CudaSlice<f32>, k: &cudarc::driver::CudaView<u8>,
9751                     v: &cudarc::driver::CudaView<u8>, o: &mut CudaSlice<f32>,
9752                     head_dim: usize, n_head: usize, n_head_kv: usize, t_kv: usize, scale: f32,
9753                     k_tok_bytes: usize, v_tok_bytes: usize)
9754                     -> Result<(), Box<dyn std::error::Error>> {
9755        self.fa_decode_kvmod(q, k, v, o, head_dim, n_head, n_head_kv, t_kv, scale,
9756                             k_tok_bytes, v_tok_bytes, false)
9757    }
9758
9759    /// `fa_decode` with an explicit fp8-module flag (`g`): gemma windowed layers under
9760    /// MEMRA_GEMMA_WKV read an e4m3 cache — every kernel must come from the kf8vf8 module
9761    /// and the v4 lane (q8_0-hardcoded staging) is excluded.
9762    #[allow(clippy::too_many_arguments)]
9763    /// UNIFIED scalar decode launch (fa_decode_f32, nullable-ctr): ONE symbol for host-len
9764    /// (kvmod eager) and device-len (graph/stream) callers — the textually-identical f32_dc
9765    /// twin compiled apart and its ULP drift flipped 31B verify argmaxes (2026-07-12).
9766    #[allow(clippy::too_many_arguments)]
9767    #[allow(clippy::too_many_arguments)]
9768    fn fa_decode_scalar_unified(&self, q: &cudarc::driver::CudaView<f32>,
9769                                k: &cudarc::driver::CudaView<u8>,
9770                                v: &cudarc::driver::CudaView<u8>,
9771                                o: &mut cudarc::driver::CudaViewMut<f32>,
9772                                head_dim: usize, n_head: usize, n_head_kv: usize,
9773                                t_kv_host: usize, t_kv_dev: Option<&CudaSlice<i32>>,
9774                                scale: f32, n_splits: usize, split_keys: usize,
9775                                k_tok_bytes: usize, v_tok_bytes: usize, g: bool,
9776                                part_o: &mut CudaSlice<f32>, part_m: &mut CudaSlice<f32>,
9777                                part_l: &mut CudaSlice<f32>,
9778                                q8_out: Option<(&mut CudaSlice<i8>, &mut CudaSlice<f32>)>)
9779                                -> Result<(), Box<dyn std::error::Error>> {
9780        let f = if g { self.func_g("fa_decode_f32") } else { self.fa_func("fa_decode_f32", head_dim) };
9781        let cfg = LaunchConfig { grid_dim: (n_head as u32, n_splits as u32, 1),
9782            block_dim: (head_dim as u32, 1, 1), shared_mem_bytes: (4 * (head_dim + 32)) as u32 };
9783        let (hd, nh, nhkv, nsp) = (head_dim as i32, n_head as i32, n_head_kv as i32, n_splits as i32);
9784        let (ktb, vtb, tkvi, ski) = (k_tok_bytes as i64, v_tok_bytes as i64, t_kv_host as i32,
9785                                     split_keys as i32);
9786        let __s_b = self.gpu.stream();
9787        let mut b = __s_b.launch_builder(&f);
9788        match t_kv_dev {
9789            Some(d) => { b.arg(q).arg(k).arg(v).arg(&mut *part_o).arg(&mut *part_m).arg(&mut *part_l)
9790                          .arg(&hd).arg(&nh).arg(&nhkv).arg(&tkvi).arg(d).arg(&scale).arg(&nsp)
9791                          .arg(&ski).arg(&ktb).arg(&vtb);
9792                         unsafe { b.launch(cfg)?; } }
9793            None => { let null: u64 = 0;
9794                      b.arg(q).arg(k).arg(v).arg(&mut *part_o).arg(&mut *part_m).arg(&mut *part_l)
9795                       .arg(&hd).arg(&nh).arg(&nhkv).arg(&tkvi).arg(&null).arg(&scale).arg(&nsp)
9796                       .arg(&ski).arg(&ktb).arg(&vtb);
9797                      unsafe { b.launch(cfg)?; } }
9798        }
9799        let cfg2 = LaunchConfig { grid_dim: (n_head as u32, 1, 1),
9800            block_dim: (head_dim as u32, 1, 1), shared_mem_bytes: 0 };
9801        if let Some((oq, od)) = q8_out {
9802            // wave-5b: q8-emitting combine — the wo matmul_pre consumes the pair directly.
9803            let fc = if g { self.func_g("fa_decode_combine_q8_1") }
9804                     else { self.fa_func("fa_decode_combine_q8_1", head_dim) };
9805            let __s_b2 = self.gpu.stream();
9806            let mut b2 = __s_b2.launch_builder(&fc);
9807            b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(oq).arg(od).arg(&hd).arg(&nh).arg(&nsp);
9808            unsafe { b2.launch(cfg2)?; }
9809            return Ok(());
9810        }
9811        let fc = if g { self.func_g("fa_decode_combine_f32") } else { self.fa_func("fa_decode_combine_f32", head_dim) };
9812        let __s_b2 = self.gpu.stream();
9813        let mut b2 = __s_b2.launch_builder(&fc);
9814        b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(o).arg(&hd).arg(&nh).arg(&nsp);
9815        unsafe { b2.launch(cfg2)?; }
9816        Ok(())
9817    }
9818
9819    pub fn fa_decode_kvmod(&self, q: &CudaSlice<f32>, k: &cudarc::driver::CudaView<u8>,
9820                     v: &cudarc::driver::CudaView<u8>, o: &mut CudaSlice<f32>,
9821                     head_dim: usize, n_head: usize, n_head_kv: usize, t_kv: usize, scale: f32,
9822                     k_tok_bytes: usize, v_tok_bytes: usize, g: bool)
9823                     -> Result<(), Box<dyn std::error::Error>> {
9824        let q_view = q.as_view();
9825        let mut o_view = o.as_view_mut();
9826        self.fa_decode_kvmod_view(&q_view, k, v, &mut o_view, head_dim, n_head, n_head_kv,
9827                                  t_kv, scale, k_tok_bytes, v_tok_bytes, g)
9828    }
9829
9830    /// Row-view entry into `fa_decode_kvmod`. The kernel sees the selected Q/output rows as its
9831    /// base pointers, so the launch geometry and arithmetic are identical to the owned-slice entry.
9832    /// Batched fallback callers use this to avoid materializing rows around an otherwise unchanged
9833    /// per-session KV view and FA launch.
9834    #[allow(clippy::too_many_arguments)]
9835    pub fn fa_decode_kvmod_view(&self, q: &cudarc::driver::CudaView<f32>,
9836                     k: &cudarc::driver::CudaView<u8>, v: &cudarc::driver::CudaView<u8>,
9837                     o: &mut cudarc::driver::CudaViewMut<f32>,
9838                     head_dim: usize, n_head: usize, n_head_kv: usize, t_kv: usize, scale: f32,
9839                     k_tok_bytes: usize, v_tok_bytes: usize, g: bool)
9840                     -> Result<(), Box<dyn std::error::Error>> {
9841        // PERF-4: the warp-per-token vec path replaces the scalar element-per-thread fa_decode_f32 —
9842        // warp-per-token fa_decode_vec_q (grid=(n_head_kv,n_splits), block=(32,gqa_ratio)).
9843        // The block dequants each KV tile ONCE into smem (bf16) and broadcasts to all gqa Q-head
9844        // warps -> each KV byte leaves HBM/L2 ~1x/group (vs 4x). ARGS identical; func/grid/block/
9845        // smem/n_splits differ. fa_decode_f32 stays the bit-reference fallback. Combine is shared.
9846        //
9847        // SPLIT-K: the scalar path has grid.x=n_head (32) blocks; the vec path only has
9848        // grid.x=n_head_kv (8). To avoid starving the GPU at mid ctx, the vec path splits MORE
9849        // aggressively (64 keys/split vs 256) so grid.y rises and 8*n_splits fills the SMs.
9850        // At VERY short ctx (t_kv<96) even 1 split can't fill the GPU from 8 KV heads, so the
9851        // broadcast can't beat the scalar path's 4x-more-blocks latency hiding — fall back to
9852        // scalar there (measured crossover: vec 0.68x at t_kv=64, 1.23x at t_kv=96, 2.2x at 256).
9853        // DEFAULT-ON (2026-06-28): clean clock-locked sweep proved vec beats scalar at every
9854        // t_kv>=96 and the gain WIDENS with ctx (graph decode: +9.5% @128, +11.6% @512, +11.8%
9855        // @2048) — the KV-byte-broadcast (4x fewer HBM reads/group) compounds as attention grows.
9856        // MEMRA_NO_FA_VEC forces the scalar bit-reference. Below FA_VEC_MIN_TKV the scalar path's
9857        // 4x-more-blocks (grid.x=n_head=32 vs n_head_kv=8) hides latency better, so keep scalar there.
9858        // g + no-v4: the g-module REGISTER twin mis-decodes the gemma windowed shape
9859        // (root-cause open, jsonl) — only reachable by forcing v4 off (MEMRA_FA_V4_MAX);
9860        // fall to the exact scalar there instead of the broken register arm.
9861        let mut fa_vec = std::env::var("MEMRA_NO_FA_VEC").is_err() && t_kv >= fa_vec_min_tkv();
9862        // hd256 under g: v4-or-scalar. hd128/other under g ride the REGISTER g-lane (the
9863        // dq_K_lane/dq_V_lane macros are format-aware; v3 is format-gated off, v2/smem
9864        // arms are excluded under g). Mirrored in kvmod / fa_decode_dc / fa_geom_eager.
9865        if g && head_dim == 256 && !fa_v4_at(t_kv) { fa_vec = false; }
9866        let sp = fa_split_keys(t_kv, n_head_kv);
9867        let n_splits = if fa_vec { ((t_kv + sp - 1) / sp).max(1) } else { ((t_kv + 255) / 256).max(1) };
9868        let o_len = n_head * n_splits * head_dim;
9869        let ml_len = n_head * n_splits;
9870        let mut part_guard = self.fa_part_pool.lock().unwrap();
9871        if part_guard.as_ref().map(|pp| pp.0.len() < o_len || pp.1.len() < ml_len).unwrap_or(true) {
9872            // RETIRE-ON-GROW, never free (#68 root cause, 2026-08-04): captured graphs (the
9873            // per-session persistent draft graph, the decode/prime graph doors) BAKE these pool
9874            // buffer addresses. Dropping the old buffers on grow returns them to the async pool,
9875            // later live allocations land at those addresses, and the next graph REPLAY writes
9876            // its fa partials over them — the ST serve-spec corruption (acceptance collapse +
9877            // output corruption began the burst after the trunk's t_kv growth first realloc'd
9878            // this pool past the draft-capture size; research/fp8ship-20260804). Retiring keeps
9879            // the baked addresses alive (single-stream: eager writes the new buffers, replays
9880            // touch only the old — never concurrently). Doubling growth bounds retired VRAM
9881            // (total retired < final size).
9882            let old = part_guard.take();
9883            let (co, cm) = old.as_ref().map(|pp| (pp.0.len(), pp.1.len())).unwrap_or((0, 0));
9884            if let Some(old) = old {
9885                self.fa_part_retired.lock().unwrap().push(old);
9886            }
9887            if std::env::var("MEMRA_DEBUG_FAPOOL").is_ok() {
9888                eprintln!("[fa-pool] REALLOC o {} -> {} ml {} -> {} (old retired)", co, o_len, cm, ml_len);
9889            }
9890            *part_guard = Some((self.alloc_uninit::<f32>(o_len.max(2 * co))?,
9891                                self.alloc_uninit::<f32>(ml_len.max(2 * cm))?,
9892                                self.alloc_uninit::<f32>(ml_len.max(2 * cm))?));
9893        }
9894        let pg = part_guard.as_mut().unwrap();
9895        self.gpu.stream().memset_zeros(&mut pg.0.slice_mut(0..o_len))?;
9896        self.gpu.stream().memset_zeros(&mut pg.1.slice_mut(0..ml_len))?;
9897        self.gpu.stream().memset_zeros(&mut pg.2.slice_mut(0..ml_len))?;
9898        let (part_o, part_m, part_l) = (&mut pg.0, &mut pg.1, &mut pg.2);
9899        let (part_o, part_m, part_l) = (&mut *part_o, &mut *part_m, &mut *part_l);
9900        let (hd, nh, nhkv, tkvi, nsp) = (head_dim as i32, n_head as i32, n_head_kv as i32, t_kv as i32, n_splits as i32);
9901        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
9902        // The vec kernel holds head_dim/32 register accumulators (FA_DEC_MAX_DPL=8 -> head_dim<=256).
9903        // All shipped models use head_dim=256; fall back to scalar for anything wider rather than
9904        // silently truncating the accumulator.
9905        let fa_vec = fa_vec && head_dim <= 512 && head_dim % 32 == 0;
9906        // hd-512 vec crossover (MEMRA_FA512_MIN, default 512): the DPL16 twin wins at depth
9907        // (82.5 -> vec at 1736) but the scalar's more-blocks latency hiding wins at tiny t_kv
9908        // (the same scalar-floor physics as hd256's old 96 floor; short-ctx plain regressed
9909        // 178.4 -> 173.7 when 512 rode vec unconditionally).
9910        let fa512_min = fa512_min_tkv();
9911        // FA-DEEP pick (bit-identical twins, see fa_deep_at): default module only — the
9912        // g-module keeps the v4 pick (its class is not the depth-decay class).
9913        let deep = fa_vec && head_dim == 256 && fa_v4_at(t_kv) && !g
9914            && fa_deep_at(t_kv) && !matches!(fa_v4_mode(), "noB3" | "stage");
9915        let (f, cfg) = if fa_vec && head_dim == 512 && t_kv >= fa512_min {
9916            // gemma4 globals (hd 512): the DPL16 register twin (fa_decode_vec_q body with a
9917            // 16-slot accumulator ceiling). Scalar fallback measured 82.5us/layer at 1736 ctx.
9918            let gqa = (n_head / n_head_kv).max(1) as u32;
9919            let fv = self.fa_func("fa_decode_vec_q_dpl16", head_dim);
9920            (fv, LaunchConfig { grid_dim: (n_head_kv as u32, n_splits as u32, 1),
9921                 block_dim: (32, gqa, 1), shared_mem_bytes: 0 })
9922        } else if fa_vec && head_dim <= 256 {
9923            let gqa = (n_head / n_head_kv).max(1) as u32;
9924            // DEEP-CTX smem twin (2026-07-05): the register-dequant path's GQA reuse rides L2,
9925            // which holds to ~8k ctx but dies at 40k (layer KV ~37MB) — the 4 GQA warps then
9926            // re-read every KV byte from DRAM (4x traffic). Above MEMRA_FA_SMEM_TKV (default
9927            // 1024 — the 2026-07-05 crossover re-sweep on real prompts: p3 spec 73.8->79.2 at
9928            // 2048, flat down to 512, p2 +5%, p1/9B unchanged; the ARC-A probe's synthetic
9929            // 2.1x smem-at-all-depths pointed here; 0=never) dispatch the smem-broadcast twin:
9930            // dequant each tile ONCE per block.
9931            // Bit-identical per (token,split): same bf16 round-trip, same accumulation order,
9932            // same partial layout -> same combine. Short/mid ctx keeps the register path (it won
9933            // there by 12x — latency, not bandwidth, rules small KV).
9934            static SMEM_TKV: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
9935            let smem_tkv = *SMEM_TKV.get_or_init(|| {
9936                std::env::var("MEMRA_FA_SMEM_TKV").ok().and_then(|v| v.parse().ok())
9937                    .unwrap_or_else(|| FA_SMEM_TKV_DEFAULT.load(std::sync::atomic::Ordering::Relaxed))
9938            });
9939            if fa_v4_at(t_kv) && head_dim == 256 {
9940                // FA v4 lane (2026-07-10): key-per-lane score phase, zero shuffles per key.
9941                // NEW NUMERIC CONFIG (chunk-serial per-key dot) — battery-arbitrated.
9942                // g (fp8-windowed): the v4 staging is format-aware (2026-07-12) — kf8vf8 module.
9943                let v4name = match fa_v4_mode() {
9944                    "noB3" => "fa_decode_vec_q_v4_noB3",     // phase probe (WRONG OUTPUT)
9945                    "stage" => "fa_decode_vec_q_v4_stage",   // phase probe (WRONG OUTPUT)
9946                    _ if deep => "fa_decode_vec_q_v4_deep",
9947                    _ => "fa_decode_vec_q_v4",
9948                };
9949                let fv = if g { self.func_g(v4name) } else { self.func(v4name) };
9950                // fa_v4_smem (deep: fa_v4_deep_smem, +640B row pads) + sV (g: raw e4m3 sV
9951                // tile = 1B/elem — half the smem, 3->5 blocks/SM)
9952                let shmem = (if deep { 12160 } else { 11520 }
9953                             + 32 * head_dim * if g { 1 } else { 2 }) as u32;
9954                use cudarc::driver::sys::CUfunction_attribute_enum as A;
9955                fv.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9956                (fv,
9957                 LaunchConfig { grid_dim: (n_head_kv as u32, n_splits as u32, 1),
9958                     block_dim: (32, gqa, 1), shared_mem_bytes: shmem })
9959            } else if fa_v3_active(head_dim) {
9960                // FA v3 lane: dp4a-K hybrid (register-quantized Q, raw q8_0 K, staged-V kept).
9961                // smem = sV only (half of v2's).
9962                let fv = if g { self.func_g("fa_decode_vec_q_v3") } else { self.func("fa_decode_vec_q_v3") };
9963                let shmem = (32 * head_dim * 2) as u32;      // sV bf16 [FA_DEC_TILE=32][hd]
9964                (fv,
9965                 LaunchConfig { grid_dim: (n_head_kv as u32, n_splits as u32, 1),
9966                     block_dim: (32, gqa, 1), shared_mem_bytes: shmem })
9967            } else if fa_v2_on() {
9968                // FAVENDOR lane: llama fattn-vec tile-batched softmax + wide-load staging on
9969                // OUR smem KV broadcast. Replaces BOTH per-key twins when on; same grid/block/
9970                // partials; same 32KB sK+sV tile as the smem twin.
9971                let fv = if g { self.func_g("fa_decode_vec_q_v2") } else { self.func("fa_decode_vec_q_v2") };
9972                let shmem = (2 * 32 * head_dim * 2) as u32;   // sK+sV bf16 [FA_DEC_TILE=32][hd]
9973                (fv,
9974                 LaunchConfig { grid_dim: (n_head_kv as u32, n_splits as u32, 1),
9975                     block_dim: (32, gqa, 1), shared_mem_bytes: shmem })
9976            } else if smem_tkv > 0 && t_kv >= smem_tkv && !g
9977                && !(head_dim == 512 && Self::gkv_on()) {
9978                // (fp8 exclusions: the smem twin's V-stage is q5_1-hardcoded — neither the wkv
9979                // windowed layers (g) nor the gkv globals (hd512) may be forced onto it via
9980                // MEMRA_FA_SMEM_TKV; they fall through to the format-clean register/scalar arms.)
9981                let fv = if g { self.func_g("fa_decode_vec_q_smem") } else { self.func("fa_decode_vec_q_smem") };
9982                let shmem = (2 * 32 * head_dim * 2) as u32;   // sK+sV bf16 [FA_DEC_TILE=32][hd]
9983                use cudarc::driver::sys::CUfunction_attribute_enum as A;
9984                fv.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
9985                (fv,
9986                 LaunchConfig { grid_dim: (n_head_kv as u32, n_splits as u32, 1),
9987                     block_dim: (32, gqa, 1), shared_mem_bytes: shmem })
9988            } else {
9989                // REGISTER-DEQUANT kernel (2026-07-03): per-warp direct q8_0/q5_1 register
9990                // dequant, zero dynamic shared memory.
9991                let fv = if g { self.func_g("fa_decode_vec_q") } else { self.func("fa_decode_vec_q") };
9992                (fv,
9993                 LaunchConfig { grid_dim: (n_head_kv as u32, n_splits as u32, 1),
9994                     block_dim: (32, gqa, 1), shared_mem_bytes: 0 })
9995            }
9996        } else {
9997            // UNIFIED scalar (nullable-ctr symbol shared with graph/stream callers). The
9998            // split ladder value rides along so ns_eff reproduces THIS n_splits in-kernel.
9999            return self.fa_decode_scalar_unified(q, k, v, o, head_dim, n_head, n_head_kv,
10000                                                 t_kv, None, scale, n_splits,
10001                                                 if fa_vec { sp } else { 256 },
10002                                                 k_tok_bytes, v_tok_bytes, g,
10003                                                 part_o, part_m, part_l, None);
10004        };
10005        let __s_b = self.gpu.stream();
10006        let mut b = __s_b.launch_builder(&f);
10007        b.arg(q).arg(k).arg(v).arg(&mut *part_o).arg(&mut *part_m).arg(&mut *part_l)
10008         .arg(&hd).arg(&nh).arg(&nhkv).arg(&tkvi).arg(&scale).arg(&nsp).arg(&ktb).arg(&vtb);
10009        unsafe { b.launch(cfg)?; }
10010        // (combine re-tile refuted in the fa-deep lane — flat/worse both shapes; the v4
10011        // combine stays for all arms. Receipts research/fa-decode-deep-20260802/.)
10012        let (fc, cfg2) = (if g { self.func_g("fa_decode_combine_f32") } else { self.fa_func("fa_decode_combine_f32", head_dim) },
10013            LaunchConfig { grid_dim: (n_head as u32, 1, 1), block_dim: (head_dim as u32, 1, 1), shared_mem_bytes: 0 });
10014        let __s_b2 = self.gpu.stream();
10015        let mut b2 = __s_b2.launch_builder(&fc);
10016        b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(o).arg(&hd).arg(&nh).arg(&nsp);
10017        unsafe { b2.launch(cfg2)?; }
10018        Ok(())
10019    }
10020
10021    /// BATCHED-TICK increment 2: ONE fa_decode launch covering ALL B sequences of the
10022    /// batched decode step (blockIdx.z = sequence). Per-seq K/V cache bases ride a device
10023    /// pointer table (`kv_ptrs`, [2B] interleaved k0,v0,...); per-seq key bounds ride the
10024    /// tick's position table (`pos_seq`, T_kv = pos+1). v4-lane only: the CALLER
10025    /// (decode_batch) gates every row through `fa_seqs_eligible` AND one `fa_split_keys`
10026    /// rung (`split_keys`), so each sequence's split partition, key walk and combine order
10027    /// reproduce its per-seq eager v4 program exactly (kernel-check pins seqs-vs-loop bit
10028    /// identity; decode-batch-gate strict pins the whole tick vs decode_step_h).
10029    /// q is the stacked [B, n_head, head_dim] tick buffer read in place (no per-seq q
10030    /// copies); o is written [B, n_head, head_dim] in place (no per-seq a copies).
10031    #[allow(clippy::too_many_arguments)]
10032    pub fn fa_decode_batch_seqs_v4(&self, q: &CudaSlice<f32>,
10033                                   kv_ptrs: &cudarc::driver::CudaView<u64>,
10034                                   pos_seq: &CudaSlice<i32>, o: &mut CudaSlice<f32>,
10035                                   head_dim: usize, n_head: usize, n_head_kv: usize,
10036                                   b_n: usize, t_kv_max: usize, scale: f32,
10037                                   split_keys: usize, k_tok_bytes: usize, v_tok_bytes: usize)
10038                                   -> Result<(), Box<dyn std::error::Error>> {
10039        debug_assert!(head_dim == 256, "seqs twin is v4-stamped (hd256 only)");
10040        let n_splits_max = (t_kv_max + split_keys - 1) / split_keys;
10041        let o_len = b_n * n_head * n_splits_max * head_dim;
10042        let ml_len = b_n * n_head * n_splits_max;
10043        let mut part_guard = self.fa_part_pool.lock().unwrap();
10044        if part_guard.as_ref().map(|pp| pp.0.len() < o_len || pp.1.len() < ml_len).unwrap_or(true) {
10045            // RETIRE-ON-GROW, never free (#68 root cause, 2026-08-04): captured graphs (the
10046            // per-session persistent draft graph, the decode/prime graph doors) BAKE these pool
10047            // buffer addresses. Dropping the old buffers on grow returns them to the async pool,
10048            // later live allocations land at those addresses, and the next graph REPLAY writes
10049            // its fa partials over them — the ST serve-spec corruption (acceptance collapse +
10050            // output corruption began the burst after the trunk's t_kv growth first realloc'd
10051            // this pool past the draft-capture size; research/fp8ship-20260804). Retiring keeps
10052            // the baked addresses alive (single-stream: eager writes the new buffers, replays
10053            // touch only the old — never concurrently). Doubling growth bounds retired VRAM
10054            // (total retired < final size).
10055            let old = part_guard.take();
10056            let (co, cm) = old.as_ref().map(|pp| (pp.0.len(), pp.1.len())).unwrap_or((0, 0));
10057            if let Some(old) = old {
10058                self.fa_part_retired.lock().unwrap().push(old);
10059            }
10060            if std::env::var("MEMRA_DEBUG_FAPOOL").is_ok() {
10061                eprintln!("[fa-pool] REALLOC o {} -> {} ml {} -> {} (old retired)", co, o_len, cm, ml_len);
10062            }
10063            *part_guard = Some((self.alloc_uninit::<f32>(o_len.max(2 * co))?,
10064                                self.alloc_uninit::<f32>(ml_len.max(2 * cm))?,
10065                                self.alloc_uninit::<f32>(ml_len.max(2 * cm))?));
10066        }
10067        let pg = part_guard.as_mut().unwrap();
10068        self.gpu.stream().memset_zeros(&mut pg.0.slice_mut(0..o_len))?;
10069        self.gpu.stream().memset_zeros(&mut pg.1.slice_mut(0..ml_len))?;
10070        self.gpu.stream().memset_zeros(&mut pg.2.slice_mut(0..ml_len))?;
10071        let (part_o, part_m, part_l) = (&mut pg.0, &mut pg.1, &mut pg.2);
10072        let (hd, nh, nhkv) = (head_dim as i32, n_head as i32, n_head_kv as i32);
10073        let (nspm, spk) = (n_splits_max as i32, split_keys as i32);
10074        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
10075        let gqa = (n_head / n_head_kv).max(1) as u32;
10076        let f = self.func("fa_decode_vec_q_seqs_v4");
10077        // fa_v4_smem (11520B) + sV bf16 tile — the v4 eager arm's sizing on the default module.
10078        let shmem = (11520 + 32 * head_dim * 2) as u32;
10079        use cudarc::driver::sys::CUfunction_attribute_enum as A;
10080        f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
10081        let cfg = LaunchConfig { grid_dim: (n_head_kv as u32, n_splits_max as u32, b_n as u32),
10082            block_dim: (32, gqa, 1), shared_mem_bytes: shmem };
10083        {
10084            let __s_b = self.gpu.stream();
10085            let mut b = __s_b.launch_builder(&f);
10086            b.arg(q).arg(kv_ptrs).arg(pos_seq).arg(&mut *part_o).arg(&mut *part_m).arg(&mut *part_l)
10087             .arg(&hd).arg(&nh).arg(&nhkv).arg(&scale).arg(&nspm).arg(&spk).arg(&ktb).arg(&vtb);
10088            unsafe { b.launch(cfg)?; }
10089        }
10090        let fc = self.func("fa_decode_combine_seqs");
10091        let cfg2 = LaunchConfig { grid_dim: (n_head as u32, b_n as u32, 1),
10092            block_dim: (head_dim as u32, 1, 1), shared_mem_bytes: 0 };
10093        let __s_b2 = self.gpu.stream();
10094        let mut b2 = __s_b2.launch_builder(&fc);
10095        b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(o).arg(&hd).arg(&nh)
10096          .arg(pos_seq).arg(&nspm).arg(&spk);
10097        unsafe { b2.launch(cfg2)?; }
10098        Ok(())
10099    }
10100
10101    /// BATCHED-TICK increment 2: z-batched decode KV append — one launch appends this
10102    /// step's B rows, each into ITS OWN sequence cache at slot pos_seq[z], through the same
10103    /// [2B] interleaved pointer table the seqs FA reads. Each (block, z) warp executes the
10104    /// per-token appender's exact warp program on row z of the stacked [B, kv_dim] k/v —
10105    /// written cache bytes are BIT-IDENTICAL to the B per-seq calls it replaces
10106    /// (kernel-check pins the bytes). Default flash module only (callers exclude fp8-KV).
10107    #[allow(clippy::too_many_arguments)]
10108    pub fn append_kv_quantized_seqs(&self, k_rows: &CudaSlice<f32>, v_rows: &CudaSlice<f32>,
10109                                    kv_ptrs: &cudarc::driver::CudaView<u64>,
10110                                    pos_seq: &CudaSlice<i32>, b_n: usize,
10111                                    kv_dim_k: usize, kv_dim_v: usize,
10112                                    k_tok_bytes: usize, v_tok_bytes: usize)
10113                                    -> Result<(), Box<dyn std::error::Error>> {
10114        let f = self.func("append_quantize_kv_q8_0_q5_1_seqs");
10115        let nblk = (kv_dim_k.max(kv_dim_v) / 32) as u32;
10116        let cfg = LaunchConfig { grid_dim: (nblk, b_n as u32, 1),
10117            block_dim: (32, 1, 1), shared_mem_bytes: 0 };
10118        let (kdk, kdv) = (kv_dim_k as i32, kv_dim_v as i32);
10119        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
10120        let __s_b = self.gpu.stream();
10121        let mut b = __s_b.launch_builder(&f);
10122        b.arg(k_rows).arg(v_rows).arg(kv_ptrs).arg(pos_seq)
10123         .arg(&kdk).arg(&kdv).arg(&ktb).arg(&vtb);
10124        unsafe { b.launch(cfg)?; }
10125        Ok(())
10126    }
10127
10128    /// True iff the MULTI-ROW verify FA (`fa_decode_rows`) is usable for a verify batch whose
10129    /// FIRST row attends `base_len + 1` keys: every row must take the SAME kernel eager decode
10130    /// would (the vec path) — mirrors fa_decode's gate exactly (MEMRA_NO_FA_VEC + FA_VEC_MIN_TKV +
10131    /// head_dim), evaluated at the MINIMUM row bound so no row could have picked scalar.
10132    /// MEMRA_FA_ROWS_OFF=1 is the A/B + fallback seam (per-row loop).
10133    pub fn fa_rows_eligible(&self, base_len: usize, head_dim: usize) -> bool {
10134        std::env::var("MEMRA_NO_FA_VEC").is_err()
10135            && std::env::var("MEMRA_FA_ROWS_OFF").is_err()
10136            && base_len + 1 >= fa_vec_min_tkv()
10137            && head_dim <= 256 && head_dim % 32 == 0
10138    }
10139
10140    /// MULTI-ROW verify FA: run fa_decode_vec_q's EXACT per-row program for T causal query rows
10141    /// (row r attends keys [0..base_len+r+1)) in ONE kernel launch with grid.z = row, plus ONE
10142    /// row-batched combine. Replaces the T separate (fa_decode + combine) launches of the spec
10143    /// verify — same per-row split partition (n_splits_r = ceil(t_kv_r/split_keys), the
10144    /// fa_split_keys formula), same key-walk order, same reduce shapes => bit-identical outputs
10145    /// per row (kernel-check pins rows-vs-loop byte identity; run-spec is the end gate).
10146    /// Caller must have checked `fa_rows_eligible(base_len, head_dim)`.
10147    /// q is the verify's token-major [T, n_head, head_dim] stack; o is written [T, n_head, head_dim].
10148    #[allow(clippy::too_many_arguments)]
10149    pub fn fa_decode_rows(&self, q: &CudaSlice<f32>, k: &cudarc::driver::CudaView<u8>,
10150                          v: &cudarc::driver::CudaView<u8>, o: &mut CudaSlice<f32>,
10151                          head_dim: usize, n_head: usize, n_head_kv: usize,
10152                          base_len: usize, t: usize, scale: f32,
10153                          k_tok_bytes: usize, v_tok_bytes: usize,
10154                          // hd512 dpl16 twin is DEVICE-LEN (graph arc): base_dev/plus feed the
10155                          // kernel; host base_len keeps sizing the splits/partials. hd256 twins
10156                          // keep the host arg. None is a bug for hd512 (asserted below).
10157                          base_dev: Option<(&CudaSlice<i32>, i32)>,
10158                          // K and V planes hold the same values (gemma globals, wv:=wk): pick
10159                          // the _kv twin — V plane never read, value rides the q8_0 key dq.
10160                          kv_shared: bool,
10161                          // this layer's cache is e4m3 (gemma windowed under wkv): resolve the
10162                          // hd256 rows kernel from the kf8vf8 module. PER-CALL — a global env
10163                          // check here hijacked qwen/kernel-check hd256 rows (8 FAILs, 230ebbe).
10164                          g: bool,
10165                          // t=1 decode arm only: emit (int8, per-32 scales) from the dc combine
10166                          // (hd512 path) — the standalone quantize launch folds away.
10167                          mut q8_out: Option<(&mut CudaSlice<i8>, &mut CudaSlice<f32>)>)
10168                          -> Result<(), Box<dyn std::error::Error>> {
10169        debug_assert!(base_len + 1 >= fa_vec_min_tkv() && head_dim <= 512 && head_dim % 32 == 0);
10170        let t_kv_max = base_len + t;                       // LAST row's key bound
10171        let mut sp = fa_split_keys(t_kv_max, n_head_kv);   // env/default — same value every row
10172        // hd512 split override (MEMRA_FA_SP512, 2026-07-11): gemma globals have n_head_kv=2 so
10173        // the grid is (2 x n_splits) — at depth ~29 splits = 58 blocks on 82 SMs (half idle,
10174        // rows_dpl16 8x off its byte floor). EVERY gemma hd512 caller shares THIS wrapper
10175        // (parity law), so the partition is freely tunable — verify and decode move together.
10176        if head_dim == 512 {
10177            static SP512: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
10178            // default 16 (2026-07-11 depth sweep, N=2: plain 155.4->156.5, depth spec
10179            // 236.9->250.4; 12/24/32 all worse). hd512 exists only on gemma globals.
10180            let v = *SP512.get_or_init(|| std::env::var("MEMRA_FA_SP512").ok()
10181                .and_then(|x| x.parse().ok()).unwrap_or(0));
10182            sp = if v >= 8 { v } else { FA_SP512_DEFAULT.load(std::sync::atomic::Ordering::Relaxed) };
10183        }
10184        let (hd, nh, nhkv) = (head_dim as i32, n_head as i32, n_head_kv as i32);
10185        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
10186        let gqa = (n_head / n_head_kv).max(1) as u32;
10187        // LADDER-RUNG STRADDLE FIX (issue #10, 2026-07-13, g7e-proven): one sp for every row
10188        // diverges from eager decode when a split-ladder rung falls INSIDE the batch — row r's
10189        // eager twin used fa_split_keys(t_kv_r), the batch used fa_split_keys(t_kv_max), and
10190        // the different partition changes the combine's FP order (greedy tie flips at depth;
10191        // MEMRA_FA_SPLIT=64 pin -> PASS on the exact g7e failing config). Fix: group
10192        // consecutive rows by their OWN ladder value and launch once per group — each row then
10193        // executes the exact per-row program eager ran. Rungs land once per doubling, so this
10194        // is 1 launch in the common case and 2 on a crossing round. hd512 keeps one group (its
10195        // sp override is t_kv-independent by construction).
10196        let mut groups: Vec<(usize, usize, usize)> = Vec::new();   // (row0, t_g, sp_g)
10197        if head_dim == 512 || fa_split_keys(base_len + 1, n_head_kv) == sp {
10198            groups.push((0, t, sp));
10199        } else {
10200            let mut r0 = 0usize;
10201            while r0 < t {
10202                let sp_g = fa_split_keys(base_len + r0 + 1, n_head_kv);
10203                let mut r1 = r0 + 1;
10204                while r1 < t && fa_split_keys(base_len + r1 + 1, n_head_kv) == sp_g { r1 += 1; }
10205                groups.push((r0, r1 - r0, sp_g));
10206                r0 = r1;
10207            }
10208        }
10209        // Deep-ctx smem twin for the VERIFY rows (2026-07-05): same threshold + rationale as
10210        // fa_decode's dispatch — at 40k the register path's GQA L2-reuse premise is dead and the
10211        // verify multiplies the 4x DRAM re-read by T rows. Bit-identical per (row,token,split).
10212        static SMEM_TKV_R: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
10213        let smem_tkv = *SMEM_TKV_R.get_or_init(|| {
10214            std::env::var("MEMRA_FA_SMEM_TKV").ok().and_then(|v| v.parse().ok())
10215                .unwrap_or_else(|| FA_SMEM_TKV_DEFAULT.load(std::sync::atomic::Ordering::Relaxed))
10216        });
10217        let v4 = fa_v4_at(base_len + t) && head_dim == 256;
10218        let v3 = fa_v3_active(head_dim);
10219        let smem_rows = head_dim <= 256 && !v3 && !fa_v2_on() && smem_tkv > 0 && t_kv_max >= smem_tkv;
10220        // kv_shared twin RETIRED (2026-07-11 depth run-gen gate): the wv:=wk premise fails
10221        // POST-cache — cached K is k-normed+roped, cached V is not; the twin fed roped keys
10222        // in as values. Verify/decode/stream gates were blind (both sides shared the wrong
10223        // symbol — the parity law's blind spot); only prefill-vs-decode at depth caught it.
10224        let _ = kv_shared;
10225        // i2 twin: 2-key interleaved walk (MEMRA_FA_I2=0 reverts). i4 probed NEGATIVE
10226        // (157.3 vs 161.2 depth plain — register pressure past i2's sweet spot; jsonl).
10227        let i2 = head_dim == 512 && std::env::var("MEMRA_FA_I2").as_deref() != Ok("0");
10228        // v4-hd512 (MEMRA_FA_V512=1 opt-in, 2026-07-14): the v4 key-per-lane recipe on the
10229        // globals lane (depth profile: i2 ~4.6x off its byte floor — the v3-class
10230        // reduce-per-key latency signature). NEW NUMERIC CONFIG shared by every hd512
10231        // caller (decode+verify flip together); run-gen argmax + acceptance arbitrate.
10232        // T-BATCHED hd512 (DEFAULT ON 2026-07-14, MEMRA_FA_TB512=0 seam): one block per
10233        // (kv_head, split) stages its tile once and loops the rows over it — kills the
10234        // x t DRAM re-read of the full-ctx globals (depth cell +1.4%, plain flat, N=3
10235        // interleaved). FIXED absolute partition = NEW NUMERIC for the combine order,
10236        // shared by every hd512 caller through this wrapper (decode+verify flip together;
10237        // depth stream identical, acceptance unshifted, spec 256/256 x3 models).
10238        // Requires sp <= 32 (single staged tile; acc reused per row). The z-form v4_512
10239        // sibling (in-kernel dp4a port alone) probed FLAT — hd512 was DRAM-re-read-bound,
10240        // not unpack-bound; jsonl 2026-07-14.
10241        static TB512: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
10242        // gqa <= 16 = fa_v4_smem_512's q-array capacity; past it fall to the register twins.
10243        let tb512 = head_dim == 512 && sp <= 32 && n_head / n_head_kv.max(1) <= 16
10244            && *TB512.get_or_init(|| std::env::var("MEMRA_FA_TB512").as_deref() != Ok("0"));
10245        let fname = if tb512 { "fa_decode_vec_q_rows_v4_512_tb" }
10246                    else if i2 { "fa_decode_vec_q_rows_dpl16_i2" }
10247                    else if head_dim == 512 { "fa_decode_vec_q_rows_dpl16" }   // gemma globals (parity law)
10248                    else if v4 { "fa_decode_vec_q_rows_v4" }
10249                    else if v3 { "fa_decode_vec_q_rows_v3" }
10250                    else if fa_v2_on() { "fa_decode_vec_q_rows_v2" }
10251                    else if smem_rows { "fa_decode_vec_q_rows_smem" }
10252                    else { "fa_decode_vec_q_rows" };
10253        let f = if head_dim == 512 { self.fa_func(fname, head_dim) }
10254                else if g {
10255                    // FP8-WINDOWED: hd256 rows over an e4m3 cache — kf8vf8 module, SAME symbol
10256                    // choice as decode's kvmod dispatch (parity law: excluding v4 here paired
10257                    // g-module rows against decode's g-module v4 — different programs, short-VG
10258                    // maxdiff 2.0 / spec stream 0/128, 2026-07-12). rows_v4 is format-aware
10259                    // since fda9790; only the smem twin stays excluded (V-stage q5_1-only).
10260                    // hd128 (qwen fp8-KV) lands on the base/register rows via fname — the
10261                    // dq macros are format-aware.
10262                    self.func_g(if smem_rows { "fa_decode_vec_q_rows" } else { fname })
10263                }
10264                else { self.func(fname) };
10265        let shmem = if tb512 {
10266            // fa_v4_smem_512 (q 9KB gqa<=16 + k tile 18KB) + sV 32*512 (e4m3 module halves it)
10267            let gk = Self::gkv_on();
10268            let sh = (8192 + 1024 + 32 * 512 + 32 * 64
10269                      + 32 * head_dim * if gk { 1 } else { 2 }) as u32;
10270            use cudarc::driver::sys::CUfunction_attribute_enum as A;
10271            f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, sh as i32)?;
10272            sh
10273        } else if v4 || v3 || smem_rows || fa_v2_on() {
10274            // v4: fa_v4_smem (11.5KB) + sV; v3 stages sV only; v2/smem twins stage sK+sV.
10275            let sh = (if v4 { 11520 + 32 * head_dim * if g { 1 } else { 2 } }
10276                      else if v3 { 32 * head_dim * 2 } else { 2 * 32 * head_dim * 2 }) as u32;
10277            use cudarc::driver::sys::CUfunction_attribute_enum as A;
10278            f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, sh as i32)?;
10279            sh
10280        } else { 0 };
10281        // Per-GROUP launches (single group in the common case — identical to the pre-fix
10282        // single launch there): each group gets its own partials (the rows kernel indexes
10283        // partials by its LOCAL grid.z row) and q/o row-offset views.
10284        for &(r0, t_g, sp_g) in &groups {
10285            let n_splits_g = (base_len + r0 + t_g).div_ceil(sp_g);
10286            let (nspm, spk) = (n_splits_g as i32, sp_g as i32);
10287            let base_i = (base_len + r0) as i32;
10288            let o_len = t_g * n_head * n_splits_g * head_dim;
10289            let ml_len = t_g * n_head * n_splits_g;
10290            let mut part_guard = self.fa_part_pool.lock().unwrap();
10291        if part_guard.as_ref().map(|pp| pp.0.len() < o_len || pp.1.len() < ml_len).unwrap_or(true) {
10292            // RETIRE-ON-GROW, never free (#68 root cause, 2026-08-04): captured graphs (the
10293            // per-session persistent draft graph, the decode/prime graph doors) BAKE these pool
10294            // buffer addresses. Dropping the old buffers on grow returns them to the async pool,
10295            // later live allocations land at those addresses, and the next graph REPLAY writes
10296            // its fa partials over them — the ST serve-spec corruption (acceptance collapse +
10297            // output corruption began the burst after the trunk's t_kv growth first realloc'd
10298            // this pool past the draft-capture size; research/fp8ship-20260804). Retiring keeps
10299            // the baked addresses alive (single-stream: eager writes the new buffers, replays
10300            // touch only the old — never concurrently). Doubling growth bounds retired VRAM
10301            // (total retired < final size).
10302            let old = part_guard.take();
10303            let (co, cm) = old.as_ref().map(|pp| (pp.0.len(), pp.1.len())).unwrap_or((0, 0));
10304            if let Some(old) = old {
10305                self.fa_part_retired.lock().unwrap().push(old);
10306            }
10307            if std::env::var("MEMRA_DEBUG_FAPOOL").is_ok() {
10308                eprintln!("[fa-pool] REALLOC o {} -> {} ml {} -> {} (old retired)", co, o_len, cm, ml_len);
10309            }
10310            *part_guard = Some((self.alloc_uninit::<f32>(o_len.max(2 * co))?,
10311                                self.alloc_uninit::<f32>(ml_len.max(2 * cm))?,
10312                                self.alloc_uninit::<f32>(ml_len.max(2 * cm))?));
10313        }
10314        let pg = part_guard.as_mut().unwrap();
10315        self.gpu.stream().memset_zeros(&mut pg.0.slice_mut(0..o_len))?;
10316        self.gpu.stream().memset_zeros(&mut pg.1.slice_mut(0..ml_len))?;
10317        self.gpu.stream().memset_zeros(&mut pg.2.slice_mut(0..ml_len))?;
10318        let (part_o, part_m, part_l) = (&mut pg.0, &mut pg.1, &mut pg.2);
10319            let (part_o, part_m, part_l) = (&mut *part_o, &mut *part_m, &mut *part_l);
10320            let qv = self.view(q, t * n_head * head_dim);
10321            let q_g = qv.slice(r0 * n_head * head_dim..(r0 + t_g) * n_head * head_dim);
10322            let cfg = LaunchConfig { grid_dim: (n_head_kv as u32, n_splits_g as u32, t_g as u32),
10323                block_dim: (32, gqa, 1), shared_mem_bytes: shmem };
10324            {
10325                let __s_b = self.gpu.stream();
10326                let mut b = __s_b.launch_builder(&f);
10327                if tb512 {
10328                    // rows-inner launch: grid.z dropped, the kernel loops n_rows itself.
10329                    let (bd, plus) = base_dev.expect("hd512 rows twin requires a device base counter");
10330                    let plus_g = plus + r0 as i32;
10331                    let nr = t_g as i32;
10332                    if Self::pdl_on() && Self::pdl_wb_on() {
10333                        // wave-B2b: flavor mirrors fa_func(fname, 512) = gkv.
10334                        use cudarc::driver::{DevicePtr, DevicePtrMut};
10335                        let s = &self.gpu.stream();
10336                        let (pq, _b0) = q_g.device_ptr(s); let (pk, _b1) = k.device_ptr(s);
10337                        let (pv, _b2) = v.device_ptr(s);
10338                        let (po, _b3) = part_o.device_ptr_mut(s);
10339                        let (pm, _b4) = part_m.device_ptr_mut(s);
10340                        let (pl, _b5) = part_l.device_ptr_mut(s);
10341                        let (pb, _b6) = bd.device_ptr(s);
10342                        let mut ps = [
10343                            &pq as *const _ as *mut std::ffi::c_void, &pk as *const _ as *mut _,
10344                            &pv as *const _ as *mut _, &po as *const _ as *mut _,
10345                            &pm as *const _ as *mut _, &pl as *const _ as *mut _,
10346                            &hd as *const _ as *mut _, &nh as *const _ as *mut _,
10347                            &nhkv as *const _ as *mut _, &pb as *const _ as *mut _,
10348                            &plus_g as *const _ as *mut _, &scale as *const _ as *mut _,
10349                            &nspm as *const _ as *mut _, &spk as *const _ as *mut _,
10350                            &ktb as *const _ as *mut _, &vtb as *const _ as *mut _,
10351                            &nr as *const _ as *mut _,
10352                        ];
10353                        unsafe { self.launch_pdl_flash(Self::gkv_on(),
10354                            "fa_decode_vec_q_rows_v4_512_tb",
10355                            (n_head_kv as u32, n_splits_g as u32, 1), (32, gqa, 1),
10356                            shmem, &mut ps)?; }
10357                    } else {
10358                    let cfg_tb = LaunchConfig {
10359                        grid_dim: (n_head_kv as u32, n_splits_g as u32, 1),
10360                        block_dim: (32, gqa, 1), shared_mem_bytes: shmem };
10361                    b.arg(&q_g).arg(k).arg(v).arg(&mut *part_o).arg(&mut *part_m).arg(&mut *part_l)
10362                     .arg(&hd).arg(&nh).arg(&nhkv).arg(bd).arg(&plus_g).arg(&scale).arg(&nspm).arg(&spk)
10363                     .arg(&ktb).arg(&vtb).arg(&nr);
10364                    unsafe { b.launch(cfg_tb)?; }
10365                    }
10366                } else if head_dim == 512 {
10367                    let (bd, plus) = base_dev.expect("hd512 rows twin requires a device base counter");
10368                    let plus_g = plus + r0 as i32;
10369                    b.arg(&q_g).arg(k).arg(v).arg(&mut *part_o).arg(&mut *part_m).arg(&mut *part_l)
10370                     .arg(&hd).arg(&nh).arg(&nhkv).arg(bd).arg(&plus_g).arg(&scale).arg(&nspm).arg(&spk)
10371                     .arg(&ktb).arg(&vtb);
10372                    unsafe { b.launch(cfg)?; }
10373                } else {
10374                    b.arg(&q_g).arg(k).arg(v).arg(&mut *part_o).arg(&mut *part_m).arg(&mut *part_l)
10375                     .arg(&hd).arg(&nh).arg(&nhkv).arg(&base_i).arg(&scale).arg(&nspm).arg(&spk)
10376                     .arg(&ktb).arg(&vtb);
10377                    unsafe { b.launch(cfg)?; }
10378                }
10379            }
10380            let cfg2 = LaunchConfig { grid_dim: (n_head as u32, t_g as u32, 1),
10381                    block_dim: (head_dim as u32, 1, 1), shared_mem_bytes: 0 };
10382            let mut o_g = o.slice_mut(r0 * n_head * head_dim..(r0 + t_g) * n_head * head_dim);
10383            if head_dim == 512 {
10384                // device-len combine (shared by verify/eager/graph — parity by symbol): the
10385                // per-row n_splits derives from the SAME counter the rows kernel read.
10386                let (bd, plus) = base_dev.unwrap();
10387                let plus_g = plus + r0 as i32;
10388                if let Some((oq, od)) = q8_out.as_mut() {
10389                    // wave-5b port (2026-07-23, t=1 decode only): q8-emitting dc combine.
10390                    debug_assert!(t == 1, "rows q8 emit is a t=1 decode arm");
10391                    if Self::pdl_on() && Self::pdl_wb_on() {
10392                        // wave-B2: flavor mirrors fa_func (hd512 + gkv → kf8vf8).
10393                        use cudarc::driver::{DevicePtr, DevicePtrMut};
10394                        let s = &self.gpu.stream();
10395                        let (po, _g0) = part_o.device_ptr(s); let (pm, _g1) = part_m.device_ptr(s);
10396                        let (pl, _g2) = part_l.device_ptr(s);
10397                        let (pq, _g3) = oq.device_ptr_mut(s); let (pd, _g4) = od.device_ptr_mut(s);
10398                        let (pb, _g5) = bd.device_ptr(s);
10399                        let mut ps = [
10400                            &po as *const _ as *mut std::ffi::c_void, &pm as *const _ as *mut _,
10401                            &pl as *const _ as *mut _, &pq as *const _ as *mut _,
10402                            &pd as *const _ as *mut _, &hd as *const _ as *mut _,
10403                            &nh as *const _ as *mut _, &pb as *const _ as *mut _,
10404                            &plus_g as *const _ as *mut _, &nspm as *const _ as *mut _,
10405                            &spk as *const _ as *mut _,
10406                        ];
10407                        unsafe { self.launch_pdl_flash(Self::gkv_on(),
10408                            "fa_decode_combine_rows_dc_q8_1",
10409                            cfg2.grid_dim, cfg2.block_dim, 0, &mut ps)?; }
10410                        continue;
10411                    }
10412                    let fc = self.fa_func("fa_decode_combine_rows_dc_q8_1", head_dim);
10413                    let __s_b2 = self.gpu.stream();
10414                    let mut b2 = __s_b2.launch_builder(&fc);
10415                    b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(&mut **oq).arg(&mut **od)
10416                      .arg(&hd).arg(&nh).arg(bd).arg(&plus_g).arg(&nspm).arg(&spk);
10417                    unsafe { b2.launch(cfg2)?; }
10418                    continue;
10419                }
10420                let fc = self.fa_func("fa_decode_combine_rows_dc", head_dim);
10421                let __s_b2 = self.gpu.stream();
10422                let mut b2 = __s_b2.launch_builder(&fc);
10423                b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(&mut o_g).arg(&hd).arg(&nh)
10424                  .arg(bd).arg(&plus_g).arg(&nspm).arg(&spk);
10425                unsafe { b2.launch(cfg2)?; }
10426            } else {
10427                // q8 emit is wired for the hd512 dc-combine arm only — a Some here would
10428                // leave the caller's pair unwritten (consumer would read garbage).
10429                assert!(q8_out.is_none(), "rows q8 emit requires the hd512 dc combine");
10430                let fc = self.func("fa_decode_combine_rows");
10431                let __s_b2 = self.gpu.stream();
10432                let mut b2 = __s_b2.launch_builder(&fc);
10433                b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(&mut o_g).arg(&hd).arg(&nh)
10434                  .arg(&base_i).arg(&nspm).arg(&spk);
10435                unsafe { b2.launch(cfg2)?; }
10436            }
10437        }
10438        Ok(())
10439    }
10440
10441    /// WINDOWED verify rows (gemma R6 deep-ctx): every row attends exactly `window` keys —
10442    /// bit-identical per row to the T=1 decode's fa_decode over the window VIEW. Caller gates
10443    /// base_len + 1 >= window (no under-window rows) and head_dim == 256 (v4 stamp).
10444    #[allow(clippy::too_many_arguments)]
10445    pub fn fa_decode_rows_w(&self, q: &CudaSlice<f32>, k: &cudarc::driver::CudaView<u8>,
10446                            v: &cudarc::driver::CudaView<u8>, o: &mut CudaSlice<f32>,
10447                            head_dim: usize, n_head: usize, n_head_kv: usize,
10448                            base_dev: &CudaSlice<i32>, base_plus: i32, t: usize, scale: f32,
10449                            window: usize, k_tok_bytes: usize, v_tok_bytes: usize,
10450                            q8_out: Option<(&mut CudaSlice<i8>, &mut CudaSlice<f32>)>)
10451                            -> Result<(), Box<dyn std::error::Error>> {
10452        // DEVICE-LEN (graph arc step 1, 2026-07-11): the causal base rides an i32 counter
10453        // (kernel T_kv = dev[0] + base_plus + r + 1) so depth graphs can replay with len
10454        // advancing on-device. dc paths pass kvl.len_d with plus=-1; verify/eager sync the
10455        // counter with one async set_i32_one first. Partials/splits size from `window` (host).
10456        debug_assert!(head_dim == 256);
10457        // windowed split (MEMRA_FA_SPW, default 32 — re-swept 2026-07-12 under the raw-e4m3 sV
10458        // occupancy ceiling (4 blocks/SM): t=1 decode is GRID-limited (win/sp splits x nkv
10459        // blocks), so smaller splits fill the ceiling — 1.7k 174.4/174.0 vs 48's 170.7/170.3,
10460        // 4.9k 159.8 vs 157.4 (N=2 interleaved, stable window). Spec serving prefers 64
10461        // (verify t=K+1 fills the grid via grid.z=t; depth K=7 281.3 vs 249.3 at 32) — set
10462        // MEMRA_FA_SPW=64 there, same config law as MEMRA_GEMMA_GKV=0. MUST be one value for
10463        // ALL widths: a t-keyed probe broke decode-vs-verify combine order (stream 9/128).
10464        let sp = {
10465            static SPW: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
10466            let v = *SPW.get_or_init(|| std::env::var("MEMRA_FA_SPW").ok()
10467                .and_then(|x| x.parse().ok()).unwrap_or(0));
10468            if v >= 8 { v } else { FA_SPW_DEFAULT.load(std::sync::atomic::Ordering::Relaxed) }
10469        };
10470        let n_splits_max = (window + sp - 1) / sp;
10471        let (hd, nh, nhkv) = (head_dim as i32, n_head as i32, n_head_kv as i32);
10472        let (nspm, spk, wini) = (n_splits_max as i32, sp as i32, window as i32);
10473        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
10474        let gqa = (n_head / n_head_kv).max(1) as u32;
10475        let o_len = t * n_head * n_splits_max * head_dim;
10476        let ml_len = t * n_head * n_splits_max;
10477        let mut part_guard = self.fa_part_pool.lock().unwrap();
10478        if part_guard.as_ref().map(|pp| pp.0.len() < o_len || pp.1.len() < ml_len).unwrap_or(true) {
10479            // RETIRE-ON-GROW, never free (#68 root cause, 2026-08-04): captured graphs (the
10480            // per-session persistent draft graph, the decode/prime graph doors) BAKE these pool
10481            // buffer addresses. Dropping the old buffers on grow returns them to the async pool,
10482            // later live allocations land at those addresses, and the next graph REPLAY writes
10483            // its fa partials over them — the ST serve-spec corruption (acceptance collapse +
10484            // output corruption began the burst after the trunk's t_kv growth first realloc'd
10485            // this pool past the draft-capture size; research/fp8ship-20260804). Retiring keeps
10486            // the baked addresses alive (single-stream: eager writes the new buffers, replays
10487            // touch only the old — never concurrently). Doubling growth bounds retired VRAM
10488            // (total retired < final size).
10489            let old = part_guard.take();
10490            let (co, cm) = old.as_ref().map(|pp| (pp.0.len(), pp.1.len())).unwrap_or((0, 0));
10491            if let Some(old) = old {
10492                self.fa_part_retired.lock().unwrap().push(old);
10493            }
10494            if std::env::var("MEMRA_DEBUG_FAPOOL").is_ok() {
10495                eprintln!("[fa-pool] REALLOC o {} -> {} ml {} -> {} (old retired)", co, o_len, cm, ml_len);
10496            }
10497            *part_guard = Some((self.alloc_uninit::<f32>(o_len.max(2 * co))?,
10498                                self.alloc_uninit::<f32>(ml_len.max(2 * cm))?,
10499                                self.alloc_uninit::<f32>(ml_len.max(2 * cm))?));
10500        }
10501        let pg = part_guard.as_mut().unwrap();
10502        self.gpu.stream().memset_zeros(&mut pg.0.slice_mut(0..o_len))?;
10503        self.gpu.stream().memset_zeros(&mut pg.1.slice_mut(0..ml_len))?;
10504        self.gpu.stream().memset_zeros(&mut pg.2.slice_mut(0..ml_len))?;
10505        let (part_o, part_m, part_l) = (&mut pg.0, &mut pg.1, &mut pg.2);
10506        // Lane pick: decode AND verify both land here in the windowed regime (parity law —
10507        // hybrid_forward verify_attn), so the pick only needs internal consistency, not
10508        // clone-of-decode bit fidelity (SASS-proven impossible for textually identical
10509        // kernels, jsonl 2026-07-10). v4 under the threshold; smem twin at/above the smem
10510        // floor (deep-ctx broadcast win); register twin between.
10511        static SMEM_TKV_W: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
10512        let smem_tkv = *SMEM_TKV_W.get_or_init(|| {
10513            std::env::var("MEMRA_FA_SMEM_TKV").ok().and_then(|v| v.parse().ok())
10514                .unwrap_or_else(|| FA_SMEM_TKV_DEFAULT.load(std::sync::atomic::Ordering::Relaxed))
10515        });
10516        // MULTI-ROW v4: resurrected 2026-07-14 (the '33 tok/s collapse' was a paired-map
10517        // partial-write bug, not the mechanism) and falsified HONESTLY at gqa 2: bit-exact
10518        // but −1.7% on the 31B depth cell — the sp helper warp already hides staging
10519        // in-block, and mr trades L2-cheap redundant bytes for serialized per-warp gqa
10520        // score/B3 chains. Arm deleted; jsonl row 2026-07-14 is the record.
10521        use cudarc::driver::sys::CUfunction_attribute_enum as A;
10522        // FP8-WINDOWED (wkv): the v4 family is format-aware (2026-07-12 KFMT/VFMT staging
10523        // arms) — wkv rides the SAME lane logic, resolved from the kf8vf8 module. One symbol
10524        // per (lane, format-module) keeps parity structural; the old register-i2 detour
10525        // (-33%) is retired.
10526        let wg = Self::wkv_on();
10527        // STAGING-PARALLEL v4 (MEMRA_FA_SPW2, default ON at gqa==1): warp 1 = staging helper
10528        // (v4 is 61% staging); score phases identical to v4_w. Same symbol all t.
10529        let sp2 = gqa <= 4 && fa_v4_at(window)
10530            && std::env::var("MEMRA_FA_SPW2").as_deref() != Ok("0");
10531        if sp2 {
10532            let sh = (11520 + 32 * head_dim * if wg { 1 } else { 2 }) as u32;
10533            if Self::pdl_on() && Self::pdl_wb_on() {
10534                // wave-B2b: flavor mirrors wg.
10535                use cudarc::driver::{DevicePtr, DevicePtrMut};
10536                let s = &self.gpu.stream();
10537                let (pq, _b0) = q.device_ptr(s); let (pk, _b1) = k.device_ptr(s);
10538                let (pv, _b2) = v.device_ptr(s);
10539                let (po, _b3) = part_o.device_ptr_mut(s);
10540                let (pm, _b4) = part_m.device_ptr_mut(s);
10541                let (pl, _b5) = part_l.device_ptr_mut(s);
10542                let (pb, _b6) = base_dev.device_ptr(s);
10543                let mut ps = [
10544                    &pq as *const _ as *mut std::ffi::c_void, &pk as *const _ as *mut _,
10545                    &pv as *const _ as *mut _, &po as *const _ as *mut _,
10546                    &pm as *const _ as *mut _, &pl as *const _ as *mut _,
10547                    &hd as *const _ as *mut _, &nh as *const _ as *mut _,
10548                    &nhkv as *const _ as *mut _, &pb as *const _ as *mut _,
10549                    &base_plus as *const _ as *mut _, &scale as *const _ as *mut _,
10550                    &nspm as *const _ as *mut _, &spk as *const _ as *mut _,
10551                    &ktb as *const _ as *mut _, &vtb as *const _ as *mut _,
10552                    &wini as *const _ as *mut _,
10553                ];
10554                unsafe { self.launch_pdl_flash(wg, "fa_decode_vec_q_rows_v4_w_sp",
10555                    (n_head_kv as u32, n_splits_max as u32, t as u32), (32, gqa + 1, 1),
10556                    sh, &mut ps)?; }
10557            } else {
10558            let f = if wg { self.func_g("fa_decode_vec_q_rows_v4_w_sp") }
10559                    else { self.func("fa_decode_vec_q_rows_v4_w_sp") };
10560            f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, sh as i32)?;
10561            let cfg = LaunchConfig { grid_dim: (n_head_kv as u32, n_splits_max as u32, t as u32),
10562                block_dim: (32, gqa + 1, 1), shared_mem_bytes: sh };
10563            let __s_b = self.gpu.stream();
10564            let mut b = __s_b.launch_builder(&f);
10565            b.arg(q).arg(k).arg(v).arg(&mut *part_o).arg(&mut *part_m).arg(&mut *part_l)
10566             .arg(&hd).arg(&nh).arg(&nhkv).arg(base_dev).arg(&base_plus).arg(&scale).arg(&nspm).arg(&spk)
10567             .arg(&ktb).arg(&vtb).arg(&wini);
10568            unsafe { b.launch(cfg)?; }
10569            }
10570        } else {
10571        if fa_v4_at(window) && Self::pdl_on() && Self::pdl_wb_on() {
10572            // wave-B2b: the v4_w pick only (smem/reg twins stay builder-launched).
10573            let sh = (11520 + 32 * head_dim * if wg { 1 } else { 2 }) as u32;
10574            use cudarc::driver::{DevicePtr, DevicePtrMut};
10575            let s = &self.gpu.stream();
10576            let (pq, _b0) = q.device_ptr(s); let (pk, _b1) = k.device_ptr(s);
10577            let (pv, _b2) = v.device_ptr(s);
10578            let (po, _b3) = part_o.device_ptr_mut(s);
10579            let (pm, _b4) = part_m.device_ptr_mut(s);
10580            let (pl, _b5) = part_l.device_ptr_mut(s);
10581            let (pb, _b6) = base_dev.device_ptr(s);
10582            let mut ps = [
10583                &pq as *const _ as *mut std::ffi::c_void, &pk as *const _ as *mut _,
10584                &pv as *const _ as *mut _, &po as *const _ as *mut _,
10585                &pm as *const _ as *mut _, &pl as *const _ as *mut _,
10586                &hd as *const _ as *mut _, &nh as *const _ as *mut _,
10587                &nhkv as *const _ as *mut _, &pb as *const _ as *mut _,
10588                &base_plus as *const _ as *mut _, &scale as *const _ as *mut _,
10589                &nspm as *const _ as *mut _, &spk as *const _ as *mut _,
10590                &ktb as *const _ as *mut _, &vtb as *const _ as *mut _,
10591                &wini as *const _ as *mut _,
10592            ];
10593            unsafe { self.launch_pdl_flash(wg, "fa_decode_vec_q_rows_v4_w",
10594                (n_head_kv as u32, n_splits_max as u32, t as u32), (32, gqa, 1),
10595                sh, &mut ps)?; }
10596        } else {
10597        let pick = |name: &str| if wg { self.func_g(name) } else { self.func(name) };
10598        let (f, sh) = if fa_v4_at(window) {
10599            let f = pick("fa_decode_vec_q_rows_v4_w");
10600            (f, (11520 + 32 * head_dim * if wg { 1 } else { 2 }) as u32)
10601        } else if smem_tkv > 0 && window >= smem_tkv {
10602            // NOTE: the smem twin's V-stage is still q5_1-hardcoded — unreachable under wkv
10603            // at the gemma window (v4 covers it); revisit if the smem floor ever drops.
10604            (pick("fa_decode_vec_q_rows_smem_w"), (2 * 32 * head_dim * 2) as u32)
10605        } else {
10606            (pick("fa_decode_vec_q_rows_reg_w"), 0u32)
10607        };
10608        f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, sh as i32)?;
10609        let cfg = LaunchConfig { grid_dim: (n_head_kv as u32, n_splits_max as u32, t as u32),
10610            block_dim: (32, gqa, 1), shared_mem_bytes: sh };
10611        let __s_b = self.gpu.stream();
10612        let mut b = __s_b.launch_builder(&f);
10613        b.arg(q).arg(k).arg(v).arg(&mut *part_o).arg(&mut *part_m).arg(&mut *part_l)
10614         .arg(&hd).arg(&nh).arg(&nhkv).arg(base_dev).arg(&base_plus).arg(&scale).arg(&nspm).arg(&spk)
10615         .arg(&ktb).arg(&vtb).arg(&wini);
10616        unsafe { b.launch(cfg)?; }
10617        }
10618        }
10619        let cfg2 = LaunchConfig { grid_dim: (n_head as u32, t as u32, 1),
10620                block_dim: (head_dim as u32, 1, 1), shared_mem_bytes: 0 };
10621        if let Some((oq, od)) = q8_out {
10622            // wave-5b port (2026-07-23): q8-emitting combine — the t=1 decode's wo matvec
10623            // consumes the pair directly; the standalone quantize launch folds away.
10624            if Self::pdl_on() && Self::pdl_wb_on() {
10625                // wave-B2: flavor mirrors the builder's wg choice.
10626                use cudarc::driver::{DevicePtr, DevicePtrMut};
10627                let s = &self.gpu.stream();
10628                let (po, _g0) = part_o.device_ptr(s); let (pm, _g1) = part_m.device_ptr(s);
10629                let (pl, _g2) = part_l.device_ptr(s);
10630                let (pq, _g3) = oq.device_ptr_mut(s); let (pd, _g4) = od.device_ptr_mut(s);
10631                let mut ps = [
10632                    &po as *const _ as *mut std::ffi::c_void, &pm as *const _ as *mut _,
10633                    &pl as *const _ as *mut _, &pq as *const _ as *mut _,
10634                    &pd as *const _ as *mut _, &hd as *const _ as *mut _,
10635                    &nh as *const _ as *mut _, &nspm as *const _ as *mut _,
10636                    &spk as *const _ as *mut _, &wini as *const _ as *mut _,
10637                ];
10638                unsafe { self.launch_pdl_flash(wg, "fa_decode_combine_rows_w_q8_1",
10639                                               cfg2.grid_dim, cfg2.block_dim, 0, &mut ps)?; }
10640                return Ok(());
10641            }
10642            let fc = if wg { self.func_g("fa_decode_combine_rows_w_q8_1") }
10643                     else { self.func("fa_decode_combine_rows_w_q8_1") };
10644            let __s_b2 = self.gpu.stream();
10645            let mut b2 = __s_b2.launch_builder(&fc);
10646            b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(oq).arg(od).arg(&hd).arg(&nh)
10647              .arg(&nspm).arg(&spk).arg(&wini);
10648            unsafe { b2.launch(cfg2)?; }
10649            return Ok(());
10650        }
10651        let fc = if wg { self.func_g("fa_decode_combine_rows_w") }
10652                 else { self.func("fa_decode_combine_rows_w") };
10653        let __s_b2 = self.gpu.stream();
10654        let mut b2 = __s_b2.launch_builder(&fc);
10655        b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(o).arg(&hd).arg(&nh)
10656          .arg(&nspm).arg(&spk).arg(&wini);
10657        unsafe { b2.launch(cfg2)?; }
10658        Ok(())
10659    }
10660
10661    /// ROUND-STREAM stage (c): fa rows with the causal base from a device counter. Two lanes:
10662    /// v3 (qwen stream, fa_v3_active) and v4 (gemma hd256 burst — rows_v4_dc, g-module aware);
10663    /// `t_kv_upper` sizes splits/partials — the same one-sp-for-all-rows approximation class
10664    /// the host rows path already uses (battery-arbitrated); actual per-row bounds derive
10665    /// in-kernel from the counter (+ base_plus, v4 lane only — v3's kernel has no plus arg).
10666    #[allow(clippy::too_many_arguments)]
10667    pub fn fa_decode_rows_dc(&self, q: &CudaSlice<f32>, k: &cudarc::driver::CudaView<u8>,
10668                             v: &cudarc::driver::CudaView<u8>, o: &mut CudaSlice<f32>,
10669                             head_dim: usize, n_head: usize, n_head_kv: usize,
10670                             base_dev: &CudaSlice<i32>, t_kv_upper: usize, t: usize, scale: f32,
10671                             k_tok_bytes: usize, v_tok_bytes: usize, base_plus: i32, g: bool)
10672                             -> Result<(), Box<dyn std::error::Error>> {
10673        let v4 = head_dim == 256 && fa_v4_at(t_kv_upper);
10674        assert!(v4 || fa_v3_active(head_dim), "stream fa rows requires the v3 or v4 lane");
10675        assert!(v4 || base_plus == 0, "v3_dc kernel takes no plus arg");
10676        if v4 {
10677            let sp = fa_split_keys(t_kv_upper, n_head_kv);
10678            let n_splits_max = (t_kv_upper + sp - 1) / sp;
10679            let (hd, nh, nhkv) = (head_dim as i32, n_head as i32, n_head_kv as i32);
10680            let (nspm, spk) = (n_splits_max as i32, sp as i32);
10681            let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
10682            let gqa = (n_head / n_head_kv).max(1) as u32;
10683            let o_len = t * n_head * n_splits_max * head_dim;
10684            let ml_len = t * n_head * n_splits_max;
10685            let mut part_guard = self.fa_part_pool.lock().unwrap();
10686            if part_guard.as_ref().map(|pp| pp.0.len() < o_len || pp.1.len() < ml_len).unwrap_or(true) {
10687                // RETIRE-ON-GROW, never free (#68 root cause, 2026-08-04): captured graphs (the
10688            // per-session persistent draft graph, the decode/prime graph doors) BAKE these pool
10689            // buffer addresses. Dropping the old buffers on grow returns them to the async pool,
10690            // later live allocations land at those addresses, and the next graph REPLAY writes
10691            // its fa partials over them — the ST serve-spec corruption (acceptance collapse +
10692            // output corruption began the burst after the trunk's t_kv growth first realloc'd
10693            // this pool past the draft-capture size; research/fp8ship-20260804). Retiring keeps
10694            // the baked addresses alive (single-stream: eager writes the new buffers, replays
10695            // touch only the old — never concurrently). Doubling growth bounds retired VRAM
10696            // (total retired < final size).
10697                let old = part_guard.take();
10698                let (co, cm) = old.as_ref().map(|pp| (pp.0.len(), pp.1.len())).unwrap_or((0, 0));
10699                if let Some(old) = old {
10700                    self.fa_part_retired.lock().unwrap().push(old);
10701                }
10702                if std::env::var("MEMRA_DEBUG_FAPOOL").is_ok() {
10703                    eprintln!("[fa-pool] REALLOC o {} -> {} ml {} -> {} (old retired)", co, o_len, cm, ml_len);
10704                }
10705                *part_guard = Some((self.alloc_uninit::<f32>(o_len.max(2 * co))?,
10706                                    self.alloc_uninit::<f32>(ml_len.max(2 * cm))?,
10707                                    self.alloc_uninit::<f32>(ml_len.max(2 * cm))?));
10708            }
10709            let pg = part_guard.as_mut().unwrap();
10710            self.gpu.stream().memset_zeros(&mut pg.0.slice_mut(0..o_len))?;
10711            self.gpu.stream().memset_zeros(&mut pg.1.slice_mut(0..ml_len))?;
10712            self.gpu.stream().memset_zeros(&mut pg.2.slice_mut(0..ml_len))?;
10713            let (part_o, part_m, part_l) = (&mut pg.0, &mut pg.1, &mut pg.2);
10714            let f = if g { self.func_g("fa_decode_vec_q_rows_v4_dc") }
10715                    else { self.func("fa_decode_vec_q_rows_v4_dc") };
10716            let sh = (11520 + 32 * head_dim * if g { 1 } else { 2 }) as u32;
10717            use cudarc::driver::sys::CUfunction_attribute_enum as A;
10718            f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, sh as i32)?;
10719            let cfg = LaunchConfig { grid_dim: (n_head_kv as u32, n_splits_max as u32, t as u32),
10720                block_dim: (32, gqa, 1), shared_mem_bytes: sh };
10721            let __s_b = self.gpu.stream();
10722            let mut b = __s_b.launch_builder(&f);
10723            b.arg(q).arg(k).arg(v).arg(&mut *part_o).arg(&mut *part_m).arg(&mut *part_l)
10724             .arg(&hd).arg(&nh).arg(&nhkv).arg(base_dev).arg(&base_plus).arg(&scale)
10725             .arg(&nspm).arg(&spk).arg(&ktb).arg(&vtb);
10726            unsafe { b.launch(cfg)?; }
10727            let fc = self.func("fa_decode_combine_rows_dc");
10728            let cfg2 = LaunchConfig { grid_dim: (n_head as u32, t as u32, 1),
10729                block_dim: (head_dim as u32, 1, 1), shared_mem_bytes: 0 };
10730            let __s_b2 = self.gpu.stream();
10731            let mut b2 = __s_b2.launch_builder(&fc);
10732            b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(o).arg(&hd).arg(&nh)
10733              .arg(base_dev).arg(&base_plus).arg(&nspm).arg(&spk);
10734            unsafe { b2.launch(cfg2)?; }
10735            return Ok(());
10736        }
10737        let sp = fa_split_keys(t_kv_upper, n_head_kv);
10738        let n_splits_max = (t_kv_upper + sp - 1) / sp;
10739        let (hd, nh, nhkv) = (head_dim as i32, n_head as i32, n_head_kv as i32);
10740        let (nspm, spk) = (n_splits_max as i32, sp as i32);
10741        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
10742        let gqa = (n_head / n_head_kv).max(1) as u32;
10743        let o_len = t * n_head * n_splits_max * head_dim;
10744        let ml_len = t * n_head * n_splits_max;
10745        let mut part_guard = self.fa_part_pool.lock().unwrap();
10746        if part_guard.as_ref().map(|pp| pp.0.len() < o_len || pp.1.len() < ml_len).unwrap_or(true) {
10747            // RETIRE-ON-GROW, never free (#68 root cause, 2026-08-04): captured graphs (the
10748            // per-session persistent draft graph, the decode/prime graph doors) BAKE these pool
10749            // buffer addresses. Dropping the old buffers on grow returns them to the async pool,
10750            // later live allocations land at those addresses, and the next graph REPLAY writes
10751            // its fa partials over them — the ST serve-spec corruption (acceptance collapse +
10752            // output corruption began the burst after the trunk's t_kv growth first realloc'd
10753            // this pool past the draft-capture size; research/fp8ship-20260804). Retiring keeps
10754            // the baked addresses alive (single-stream: eager writes the new buffers, replays
10755            // touch only the old — never concurrently). Doubling growth bounds retired VRAM
10756            // (total retired < final size).
10757            let old = part_guard.take();
10758            let (co, cm) = old.as_ref().map(|pp| (pp.0.len(), pp.1.len())).unwrap_or((0, 0));
10759            if let Some(old) = old {
10760                self.fa_part_retired.lock().unwrap().push(old);
10761            }
10762            if std::env::var("MEMRA_DEBUG_FAPOOL").is_ok() {
10763                eprintln!("[fa-pool] REALLOC o {} -> {} ml {} -> {} (old retired)", co, o_len, cm, ml_len);
10764            }
10765            *part_guard = Some((self.alloc_uninit::<f32>(o_len.max(2 * co))?,
10766                                self.alloc_uninit::<f32>(ml_len.max(2 * cm))?,
10767                                self.alloc_uninit::<f32>(ml_len.max(2 * cm))?));
10768        }
10769        let pg = part_guard.as_mut().unwrap();
10770        self.gpu.stream().memset_zeros(&mut pg.0.slice_mut(0..o_len))?;
10771        self.gpu.stream().memset_zeros(&mut pg.1.slice_mut(0..ml_len))?;
10772        self.gpu.stream().memset_zeros(&mut pg.2.slice_mut(0..ml_len))?;
10773        let (part_o, part_m, part_l) = (&mut pg.0, &mut pg.1, &mut pg.2);
10774        let f = self.func("fa_decode_vec_q_rows_v3_dc");
10775        let sh = (32 * head_dim * 2) as u32;
10776        use cudarc::driver::sys::CUfunction_attribute_enum as A;
10777        f.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, sh as i32)?;
10778        let cfg = LaunchConfig { grid_dim: (n_head_kv as u32, n_splits_max as u32, t as u32),
10779            block_dim: (32, gqa, 1), shared_mem_bytes: sh };
10780        let __s_b = self.gpu.stream();
10781        let mut b = __s_b.launch_builder(&f);
10782        b.arg(q).arg(k).arg(v).arg(&mut *part_o).arg(&mut *part_m).arg(&mut *part_l)
10783         .arg(&hd).arg(&nh).arg(&nhkv).arg(base_dev).arg(&scale).arg(&nspm).arg(&spk)
10784         .arg(&ktb).arg(&vtb);
10785        unsafe { b.launch(cfg)?; }
10786        let fc = self.func("fa_decode_combine_rows_dc");
10787        let cfg2 = LaunchConfig { grid_dim: (n_head as u32, t as u32, 1),
10788            block_dim: (head_dim as u32, 1, 1), shared_mem_bytes: 0 };
10789        let plus0 = 0i32;
10790        let __s_b2 = self.gpu.stream();
10791        let mut b2 = __s_b2.launch_builder(&fc);
10792        b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(o).arg(&hd).arg(&nh)
10793          .arg(base_dev).arg(&plus0).arg(&nspm).arg(&spk);
10794        unsafe { b2.launch(cfg2)?; }
10795        Ok(())
10796    }
10797
10798    /// Device-counter variant of `fa_decode` (CUDA-GRAPH-PLAN Phase 2). The sequence length is read
10799    /// from `t_kv_dev[0]` (resident device i32[1]) for the attention loop bound + per-split key range;
10800    /// the GRID `n_splits` is sized for `bucket_max` (the bucket's max t_kv — baked at capture time).
10801    /// Empty splits (key range beyond the actual t_kv) write an empty partial (m=NEG_INF) so the
10802    /// shared combine skips them -> bit-correct for ANY actual t_kv <= bucket_max.
10803    ///
10804    /// BIT-IDENTITY (the gate): pass `bucket_max == actual_t_kv` and this reproduces `fa_decode`
10805    /// EXACTLY (same n_splits, same per, same split boundaries, same combine) while reading t_kv from
10806    /// device. Bucketing (bucket_max > t_kv) is for the future captured path and changes split
10807    /// grouping (different but mathematically-equal log-sum-exp merge).
10808    pub fn fa_decode_dc(&self, q: &CudaSlice<f32>, k: &cudarc::driver::CudaView<u8>,
10809                        v: &cudarc::driver::CudaView<u8>, o: &mut CudaSlice<f32>,
10810                        head_dim: usize, n_head: usize, n_head_kv: usize,
10811                        t_kv_dev: &CudaSlice<i32>, bucket_max: usize, scale: f32,
10812                        k_tok_bytes: usize, v_tok_bytes: usize, g: bool)
10813                        -> Result<(), Box<dyn std::error::Error>> {
10814        self.fa_decode_dc_q8(q, k, v, o, head_dim, n_head, n_head_kv, t_kv_dev, bucket_max,
10815                             scale, k_tok_bytes, v_tok_bytes, g, None)
10816    }
10817
10818    /// `fa_decode_dc` with an optional q8_1 sink (wave 5b): when `q8_out` is given the
10819    /// combine emits (int8, per-32 scales) for the wo matmul_pre and skips the f32 O write.
10820    #[allow(clippy::too_many_arguments)]
10821    pub fn fa_decode_dc_q8(&self, q: &CudaSlice<f32>, k: &cudarc::driver::CudaView<u8>,
10822                        v: &cudarc::driver::CudaView<u8>, o: &mut CudaSlice<f32>,
10823                        head_dim: usize, n_head: usize, n_head_kv: usize,
10824                        t_kv_dev: &CudaSlice<i32>, bucket_max: usize, scale: f32,
10825                        k_tok_bytes: usize, v_tok_bytes: usize, g: bool,
10826                        q8_out: Option<(&mut CudaSlice<i8>, &mut CudaSlice<f32>)>)
10827                        -> Result<(), Box<dyn std::error::Error>> {
10828        // The fa_vec gate + n_splits are sized from bucket_max (host, fixed at capture). The kernel
10829        // reads the ACTUAL t_kv from t_kv_dev for the per-split bound. DEFAULT-ON to MATCH the eager
10830        // `fa_decode` gate above — graph capture must mirror eager's kernel choice or the graph-vs-eager
10831        // bit-identity gate breaks. MEMRA_NO_FA_VEC forces scalar on BOTH paths in lockstep.
10832        // `g` = this layer's cache is e4m3 (gemma windowed under wkv) — every pick below must
10833        // mirror fa_decode_kvmod's g-routing or the graph diverges from eager (short/mid 1/96,
10834        // 2026-07-12).
10835        let mut fa_vec = std::env::var("MEMRA_NO_FA_VEC").is_err() && bucket_max >= fa_vec_min_tkv();
10836        if g && head_dim == 256 && !fa_v4_at(bucket_max) { fa_vec = false; }   // mirror kvmod/geom
10837        let sp = fa_split_keys(bucket_max, n_head_kv);
10838        let n_splits = if fa_vec { ((bucket_max + sp - 1) / sp).max(1) } else { ((bucket_max + 255) / 256).max(1) };
10839        let o_len = n_head * n_splits * head_dim;
10840        let ml_len = n_head * n_splits;
10841        let mut part_guard = self.fa_part_pool.lock().unwrap();
10842        if part_guard.as_ref().map(|pp| pp.0.len() < o_len || pp.1.len() < ml_len).unwrap_or(true) {
10843            // RETIRE-ON-GROW, never free (#68 root cause, 2026-08-04): captured graphs (the
10844            // per-session persistent draft graph, the decode/prime graph doors) BAKE these pool
10845            // buffer addresses. Dropping the old buffers on grow returns them to the async pool,
10846            // later live allocations land at those addresses, and the next graph REPLAY writes
10847            // its fa partials over them — the ST serve-spec corruption (acceptance collapse +
10848            // output corruption began the burst after the trunk's t_kv growth first realloc'd
10849            // this pool past the draft-capture size; research/fp8ship-20260804). Retiring keeps
10850            // the baked addresses alive (single-stream: eager writes the new buffers, replays
10851            // touch only the old — never concurrently). Doubling growth bounds retired VRAM
10852            // (total retired < final size).
10853            let old = part_guard.take();
10854            let (co, cm) = old.as_ref().map(|pp| (pp.0.len(), pp.1.len())).unwrap_or((0, 0));
10855            if let Some(old) = old {
10856                self.fa_part_retired.lock().unwrap().push(old);
10857            }
10858            if std::env::var("MEMRA_DEBUG_FAPOOL").is_ok() {
10859                eprintln!("[fa-pool] REALLOC o {} -> {} ml {} -> {} (old retired)", co, o_len, cm, ml_len);
10860            }
10861            *part_guard = Some((self.alloc_uninit::<f32>(o_len.max(2 * co))?,
10862                                self.alloc_uninit::<f32>(ml_len.max(2 * cm))?,
10863                                self.alloc_uninit::<f32>(ml_len.max(2 * cm))?));
10864        }
10865        let pg = part_guard.as_mut().unwrap();
10866        self.gpu.stream().memset_zeros(&mut pg.0.slice_mut(0..o_len))?;
10867        self.gpu.stream().memset_zeros(&mut pg.1.slice_mut(0..ml_len))?;
10868        self.gpu.stream().memset_zeros(&mut pg.2.slice_mut(0..ml_len))?;
10869        let (part_o, part_m, part_l) = (&mut pg.0, &mut pg.1, &mut pg.2);
10870        let (hd, nh, nhkv, nsp) = (head_dim as i32, n_head as i32, n_head_kv as i32, n_splits as i32);
10871        let (ktb, vtb) = (k_tok_bytes as i64, v_tok_bytes as i64);
10872        let fa_vec = fa_vec && head_dim <= 512 && head_dim % 32 == 0;
10873        // FA-DEEP pick keyed on bucket_max (the fa_v4_at precedent) — bit-identical twins,
10874        // so a threshold falling between t_kv and bucket_max cannot diverge eager-vs-graph.
10875        let deep = fa_vec && head_dim == 256 && fa_v4_at(bucket_max) && !g
10876            && fa_deep_at(bucket_max) && !matches!(fa_v4_mode(), "noB3" | "stage");
10877        let (f, cfg) = if fa_vec && head_dim == 512 && bucket_max >= {
10878            static FA512_MIN_DC: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
10879            *FA512_MIN_DC.get_or_init(|| std::env::var("MEMRA_FA512_MIN").ok()
10880                .and_then(|v| v.parse().ok()).unwrap_or(512))
10881        } {
10882            // gemma globals dc twin (mirror the eager dpl16 pick incl the crossover floor).
10883            let gqa = (n_head / n_head_kv).max(1) as u32;
10884            (self.fa_func("fa_decode_vec_q_dpl16_dc", head_dim),
10885             LaunchConfig { grid_dim: (n_head_kv as u32, n_splits as u32, 1),
10886                 block_dim: (32, gqa, 1), shared_mem_bytes: 0 })
10887        } else if fa_vec && head_dim == 512 {
10888            // under the 512 floor eager runs scalar — the SAME unified symbol, ctr non-null;
10889            // ns_eff in-kernel reproduces eager's ceil(t_kv/sp) partition for the LIVE len.
10890            let q_view = q.as_view();
10891            let mut o_view = o.as_view_mut();
10892            return self.fa_decode_scalar_unified(&q_view, k, v, &mut o_view,
10893                                                 head_dim, n_head, n_head_kv,
10894                                                 0, Some(t_kv_dev), scale, n_splits, sp,
10895                                                 k_tok_bytes, v_tok_bytes, g,
10896                                                 &mut *part_o, &mut *part_m, &mut *part_l, q8_out);
10897        } else if fa_vec && head_dim == 256 && fa_v4_at(bucket_max) {
10898            // gemma/qwen v4 dc twin (eager default lane) — capture must mirror eager's pick,
10899            // incl the g-module route + raw-e4m3 sV sizing.
10900            let gqa = (n_head / n_head_kv).max(1) as u32;
10901            let fv = if g { self.func_g("fa_decode_vec_q_v4_dc") }
10902                     else if deep { self.func("fa_decode_vec_q_v4_deep_dc") }
10903                     else { self.func("fa_decode_vec_q_v4_dc") };
10904            let shmem = (if deep { 12160 } else { 11520 }
10905                         + 32 * head_dim * if g { 1 } else { 2 }) as u32;
10906            use cudarc::driver::sys::CUfunction_attribute_enum as A;
10907            fv.set_attribute(A::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shmem as i32)?;
10908            (fv, LaunchConfig { grid_dim: (n_head_kv as u32, n_splits as u32, 1),
10909                 block_dim: (32, gqa, 1), shared_mem_bytes: shmem })
10910        } else if fa_vec && fa_v3_active(head_dim) {
10911            // FA v3 lane _dc twin: the captured graph must run the SAME walk body as eager
10912            // under MEMRA_FA_V3=1 (eager, rows-verify and graph switch together).
10913            let gqa = (n_head / n_head_kv).max(1) as u32;
10914            let fv = if g { self.func_g("fa_decode_vec_q_v3_dc") } else { self.func("fa_decode_vec_q_v3_dc") };
10915            let shmem = (32 * head_dim * 2) as u32;       // sV bf16 [FA_DEC_TILE=32][hd]
10916            (fv,
10917             LaunchConfig { grid_dim: (n_head_kv as u32, n_splits as u32, 1),
10918                 block_dim: (32, gqa, 1), shared_mem_bytes: shmem })
10919        } else if fa_vec && fa_v2_on() {
10920            // FAVENDOR lane: v2 _dc twin — the captured graph must run the SAME walk body as
10921            // eager under MEMRA_FA_V2=1 or graph_decode_gate's bit-identity breaks (the flag is
10922            // a numeric config; eager, rows-verify and graph all switch together).
10923            let gqa = (n_head / n_head_kv).max(1) as u32;
10924            let fv = if g { self.func_g("fa_decode_vec_q_v2_dc") } else { self.func("fa_decode_vec_q_v2_dc") };
10925            let shmem = (2 * 32 * head_dim * 2) as u32;   // sK+sV bf16 [FA_DEC_TILE=32][hd]
10926            (fv,
10927             LaunchConfig { grid_dim: (n_head_kv as u32, n_splits as u32, 1),
10928                 block_dim: (32, gqa, 1), shared_mem_bytes: shmem })
10929        } else if fa_vec {
10930            let gqa = (n_head / n_head_kv).max(1) as u32;
10931            // REGISTER-DEQUANT twin: zero dynamic smem (see fa_decode above).
10932            let fv = if g { self.func_g("fa_decode_vec_q_dc") } else { self.func("fa_decode_vec_q_dc") };
10933            (fv,
10934             LaunchConfig { grid_dim: (n_head_kv as u32, n_splits as u32, 1),
10935                 block_dim: (32, gqa, 1), shared_mem_bytes: 0 })
10936        } else {
10937            let q_view = q.as_view();
10938            let mut o_view = o.as_view_mut();
10939            return self.fa_decode_scalar_unified(&q_view, k, v, &mut o_view,
10940                                                 head_dim, n_head, n_head_kv,
10941                                                 0, Some(t_kv_dev), scale, n_splits,
10942                                                 if fa_vec { sp } else { 256 },
10943                                                 k_tok_bytes, v_tok_bytes, g,
10944                                                 &mut *part_o, &mut *part_m, &mut *part_l, q8_out);
10945        };
10946        let ski = sp as i32;   // one-partition law: the twins derive ns_eff from (T_kv, ski)
10947        let __s_b = self.gpu.stream();
10948        let mut b = __s_b.launch_builder(&f);
10949        b.arg(q).arg(k).arg(v).arg(&mut *part_o).arg(&mut *part_m).arg(&mut *part_l)
10950         .arg(&hd).arg(&nh).arg(&nhkv).arg(t_kv_dev).arg(&scale).arg(&nsp).arg(&ski)
10951         .arg(&ktb).arg(&vtb);
10952        unsafe { b.launch(cfg)?; }
10953        let cfg2 = LaunchConfig { grid_dim: (n_head as u32, 1, 1), block_dim: (head_dim as u32, 1, 1), shared_mem_bytes: 0 };
10954        if let Some((oq, od)) = q8_out {
10955            let fc = if g { self.func_g("fa_decode_combine_q8_1") }
10956                     else { self.fa_func("fa_decode_combine_q8_1", head_dim) };
10957            let __s_b2 = self.gpu.stream();
10958            let mut b2 = __s_b2.launch_builder(&fc);
10959            b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(oq).arg(od).arg(&hd).arg(&nh).arg(&nsp);
10960            unsafe { b2.launch(cfg2)?; }
10961            return Ok(());
10962        }
10963        let fc = if g { self.func_g("fa_decode_combine_f32") } else { self.fa_func("fa_decode_combine_f32", head_dim) };
10964        let __s_b2 = self.gpu.stream();
10965        let mut b2 = __s_b2.launch_builder(&fc);
10966        b2.arg(&*part_o).arg(&*part_m).arg(&*part_l).arg(o).arg(&hd).arg(&nh).arg(&nsp);
10967        unsafe { b2.launch(cfg2)?; }
10968        Ok(())
10969    }
10970
10971    /// EAGER fa_decode geometry for a given actual `t_kv` (CUDA-GRAPH-PLAN §3.3 bucketing). Returns
10972    /// `(fa_vec, n_splits)` EXACTLY as `fa_decode` computes them so the graph-capture path can key its
10973    /// bucket on the same `(kernel, n_splits)` pair and pass a `bucket_max` that reproduces eager's
10974    /// n_splits bit-for-bit. (Per = ceil(t_kv/n_splits) is then recomputed from the DEVICE t_kv inside
10975    /// the kernel and matches eager when n_splits matches — the bit-identity contract.)
10976    pub fn fa_geom_eager(&self, t_kv: usize, head_dim: usize, n_head_kv: usize, g: bool) -> (bool, usize) {
10977        // MUST mirror `fa_decode` / `fa_decode_dc` (default-ON 2026-06-28). This is the bucket-key
10978        // source: if it disagrees with the actual kernel pick, the graph captures the wrong path and
10979        // replay diverges from eager. All three sites read MEMRA_NO_FA_VEC in lockstep.
10980        let fa_ok = std::env::var("MEMRA_NO_FA_VEC").is_err() && t_kv >= fa_vec_min_tkv();
10981        // hd512 dpl16 vec lane (gemma globals, 2026-07-11 graph-arc fix): the original key
10982        // hardcoded vec = hd<=256, so for hd512 it bucketed by the SCALAR 256-key splits while
10983        // the dpl16/rows_dpl16 kernels split by the ladder — n_splits changed WITHIN a bucket
10984        // (mid-ctx graph mismatch at pos 19 + partials OOB at longer runs). Mirror the real
10985        // fa_decode dispatch: vec512 above the fa512 floor, vec256 as before.
10986        let vec512 = fa_ok && head_dim == 512 && t_kv >= fa512_min_tkv();
10987        let mut fa_vec = vec512 || (fa_ok && head_dim <= 256 && head_dim % 32 == 0);
10988        // g (fp8-windowed): mirror kvmod's clamp — only the v4 lane parses e4m3 in the vec
10989        // family; everything else falls to the g-module scalar.
10990        // hd256 under g: v4-or-scalar. hd128/other under g ride the REGISTER g-lane (the
10991        // dq_K_lane/dq_V_lane macros are format-aware; v3 is format-gated off, v2/smem
10992        // arms are excluded under g). Mirrored in kvmod / fa_decode_dc / fa_geom_eager.
10993        if g && head_dim == 256 && !fa_v4_at(t_kv) { fa_vec = false; }
10994        let sp = fa_split_keys(t_kv, n_head_kv);
10995        let n_splits = if fa_vec { ((t_kv + sp - 1) / sp).max(1) } else { ((t_kv + 255) / 256).max(1) };
10996        (fa_vec, n_splits)
10997    }
10998
10999    /// `bucket_max` (host t_kv to feed `fa_decode_dc` / `full_attn_decode_dc`) that makes the _dc
11000    /// kernel pick the SAME (fa_vec, n_splits) as eager would for actual `t_kv`. Because the dc
11001    /// launcher derives both from `bucket_max` via the same formulas, we just hand it `t_kv` itself:
11002    /// the n_splits is then identical, and the per-split boundaries (computed from the DEVICE t_kv in
11003    /// the kernel) match eager exactly. The bucket KEY (for the graph HashMap) is `(fa_vec, n_splits)`.
11004    pub fn fa_bucket_key(&self, t_kv: usize, head_dim: usize, n_head_kv: usize, g: bool) -> (bool, usize) {
11005        self.fa_geom_eager(t_kv, head_dim, n_head_kv, g)
11006    }
11007
11008    /// CUDA-graph capture wrapper (CUDA-GRAPH-PLAN §3.2, llama.cpp warmup pattern). Runs `step`
11009    /// inline TWICE (warmup — lets the caching allocator settle to stable pointers and any one-time
11010    /// kernel attribute/JIT happen outside capture), then captures a THIRD invocation on the Engine's
11011    /// decode stream (RELAXED mode) and instantiates it into a replayable `CudaGraph`. The closure
11012    /// must enqueue ONLY device work on `e.stream()` (no dtoh / no synchronize / no host branch on
11013    /// device data) — every per-step varying scalar must come from a device counter. Returns the
11014    /// instantiated graph; `CudaGraph::launch()` replays the whole step in one dispatch.
11015    /// `capture_graph` with CAPTURE-RETAIN: every Engine allocation made during the warmups
11016    /// and the capture is kept alive in the returned keeper — hold it as long as the graph
11017    /// replays (transients returning to the pool get reused by unrelated work and corrupt
11018    /// replays; the draft-graph root cause). Model-generic, next capture reuses it.
11019    pub fn capture_graph_retained<F>(&self, step: F)
11020        -> Result<(cudarc::driver::CudaGraph, Vec<Box<dyn std::any::Any + Send>>), Box<dyn std::error::Error>>
11021        where F: FnMut(&Engine) -> Result<(), Box<dyn std::error::Error>>
11022    {
11023        use cudarc::driver::sys::CUgraphInstantiate_flags;
11024        self.capture_graph_retained_flags(
11025            CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH, step)
11026    }
11027
11028    /// Retained capture with an explicit instantiate flag. ALLOC-FREE captured graphs
11029    /// (zero mem nodes — the gemma slotted door) should pass UPLOAD instead of
11030    /// AUTO_FREE_ON_LAUNCH: the auto-free flag's launch-time mem-pool scan was measured at
11031    /// ~0.25us/node (205us on the 826-node step) even with nothing to free.
11032    pub fn capture_graph_retained_flags<F>(&self,
11033        flags: cudarc::driver::sys::CUgraphInstantiate_flags, mut step: F)
11034        -> Result<(cudarc::driver::CudaGraph, Vec<Box<dyn std::any::Any + Send>>), Box<dyn std::error::Error>>
11035        where F: FnMut(&Engine) -> Result<(), Box<dyn std::error::Error>>
11036    {
11037        use cudarc::driver::sys::CUstreamCaptureMode;
11038        // KEEP scope = WARMUPS ONLY (2026-07-13): keep_if_capturing retains via
11039        // CudaSlice::clone, which is a device ALLOC + D2D COPY on the stream — clones made
11040        // while the capture region is open become dead copy NODES replayed every launch
11041        // (E4B: 1440 copies = 0.74ms/token, the whole graph-vs-eager regression). The
11042        // warmup runs allocate the same transient sequence at the same pool addresses, so
11043        // retaining the warmup clones preserves the draft-graph fix without polluting the
11044        // captured graph.
11045        self.capture_keep.lock().unwrap().clear();
11046        let was_tracking = self.gpu.ctx.is_event_tracking();
11047        if was_tracking { unsafe { self.gpu.ctx.disable_event_tracking(); } }
11048        let mut run = || -> Result<cudarc::driver::CudaGraph, Box<dyn std::error::Error>> {
11049            self.capture_keep_on.store(true, std::sync::atomic::Ordering::Relaxed);
11050            let w = (|| { step(self)?; step(self) })();
11051            self.capture_keep_on.store(false, std::sync::atomic::Ordering::Relaxed);
11052            w?;
11053            self.gpu.stream().synchronize()?;
11054            self.gpu.stream().begin_capture(CUstreamCaptureMode::CU_STREAM_CAPTURE_MODE_RELAXED)?;
11055            let r = step(self);
11056            let g = self.gpu.stream().end_capture(flags);
11057            r?;
11058            let graph = g?.ok_or("capture produced no graph (stream was not capturing)")?;
11059            graph.upload()?;
11060            Ok(graph)
11061        };
11062        let result = run();
11063        self.capture_keep_on.store(false, std::sync::atomic::Ordering::Relaxed);
11064        if was_tracking { unsafe { self.gpu.ctx.enable_event_tracking(); } }
11065        let keeper = std::mem::take(&mut *self.capture_keep.lock().unwrap());
11066        Ok((result?, keeper))
11067    }
11068
11069    pub fn capture_graph<F>(&self, mut step: F) -> Result<cudarc::driver::CudaGraph, Box<dyn std::error::Error>>
11070        where F: FnMut(&Engine) -> Result<(), Box<dyn std::error::Error>>
11071    {
11072        use cudarc::driver::sys::{CUstreamCaptureMode, CUgraphInstantiate_flags};
11073        // EVENT TRACKING OFF for capture. The Engine creates a 2nd stream (copy_stream) so cudarc is in
11074        // multi-stream mode and, by default, records a CudaEvent per CudaSlice alloc/use to serialize
11075        // cross-stream access. Those per-buffer event waits issue stream ops that are NOT permitted
11076        // inside a capture region (CUDA_ERROR_STREAM_CAPTURE_UNSUPPORTED). The captured decode step is
11077        // strictly SINGLE-STREAM (every kernel on gpu.stream), so this synchronization is unnecessary
11078        // here — disable it for the whole warmup+capture, re-enable after. SAFETY: the decode-dc path
11079        // touches only gpu.stream; no buffer crosses to copy_stream during capture.
11080        let was_tracking = self.gpu.ctx.is_event_tracking();
11081        if was_tracking { unsafe { self.gpu.ctx.disable_event_tracking(); } }
11082        // Q1 PROBE (MEMRA_GRAPH_IFLAG): the generic capture body's cuMemAllocAsync nodes are
11083        // EXACTLY BALANCED by in-graph free nodes (measured census q27: 1589 ALLOC / 1589
11084        // FREE), so AUTO_FREE_ON_LAUNCH has nothing to reclaim at launch — it only pays its
11085        // per-node launch-time mem-pool scan. `upload` / `none` select the alternatives to
11086        // measure that scan's real cost on the generic path. Diagnostic door only; the
11087        // default stays AUTO_FREE until a measured A/B justifies moving it.
11088        let iflag = {
11089            static F: std::sync::OnceLock<CUgraphInstantiate_flags> = std::sync::OnceLock::new();
11090            *F.get_or_init(|| match std::env::var("MEMRA_GRAPH_IFLAG").as_deref() {
11091                // UPLOAD = the gemma slotted door's zero-mem-node choice; PRIORITY = the flag
11092                // hybrid_forward.rs:5935 actually ships (both drop the auto-free launch scan).
11093                Ok("upload") => CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_UPLOAD,
11094                Ok("priority") =>
11095                    CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_USE_NODE_PRIORITY,
11096                _ => CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH,
11097            })
11098        };
11099        // MEMRA_GRAPH_CAPTIME=1 (Q1 lane): phase-resolved capture cost. Recapture is paid at
11100        // every kernel-class crossing, so it — not steady-state decode — is the quantity a
11101        // mem-node reduction could plausibly shrink. Only `instantiate` (cuStreamEndCapture +
11102        // cuGraphInstantiateWithFlags) and `upload` scale with node count; the warmups are
11103        // eager step executions and are node-count-invariant. Printing the split bounds the
11104        // refactor's ceiling instead of assuming it.
11105        let ct = {
11106            static T: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
11107            *T.get_or_init(|| std::env::var("MEMRA_GRAPH_CAPTIME").as_deref() == Ok("1"))
11108        };
11109        // MEMRA_GRAPH_WARMUPS (Q1 lane; DEFAULT 1 since lane/graph-warmups 2026-08-05): the
11110        // phase split showed the eager warmups are 80% of recapture cost (q27 27.4 of 34.4 ms
11111        // pod / 42% of 52.6 ms 5090) — 3x larger than the ENTIRE mem-node ceiling the audit
11112        // chased, and node-count-invariant, so no capture-body refactor could touch it.
11113        // Warmup 2's theorized job was async-pool ADDRESS STABILITY: warmup 1's allocs may
11114        // grow/map the pool, warmup 2 re-walks the same sequence over the freed blocks so the
11115        // captured third run bakes settled addresses. That hazard is the #68 stale-baked-
11116        // address class — which the engine now guards STRUCTURALLY rather than by re-walking:
11117        // in-body transients are captured as BALANCED in-graph alloc/free node pairs (census
11118        // 1589/1589 — replays allocate for themselves; no baked transient pointers), every
11119        // externally-referenced buffer is stable-pointer by design (fa_part_pool retires-on-
11120        // grow and never frees, resident counters/scratch, cache set in place), and the
11121        // draft-graph path additionally rides capture_graph_retained (capture_keep holds all
11122        // warmup+capture allocs alive). One warmup therefore suffices for kernel-attr
11123        // settling and pool mapping. Arbitrated adversarially, not by taste:
11124        // graph-warmup-stress (pool-growth cycles large<->small x10, overlap arm, forced
11125        // recaptures over freed blocks — bit-identity vs eager + canary teeth) is GREEN at
11126        // warmups=1 on the deployment rig, plus graph-decode-gate 256-step bit-identity,
11127        // graph-session-gate, run-spec K=1..8 (receipts research/graph-warmups-5090-20260805/
11128        // + the pod's research/graph-allocfree-20260805/). Measured: recapture -38..-42% q27 /
11129        // -41% q9, decode +~1%, capture+prime -13ms. MEMRA_GRAPH_WARMUPS=2 = the rollback
11130        // seam; tools/graph-warmup-stress-gate.sh = the gate any regression re-runs.
11131        let warmups = {
11132            static W: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
11133            *W.get_or_init(|| std::env::var("MEMRA_GRAPH_WARMUPS").ok()
11134                .and_then(|v| v.parse().ok()).filter(|n| *n >= 1).unwrap_or(1))
11135        };
11136        let mut run = || -> Result<cudarc::driver::CudaGraph, Box<dyn std::error::Error>> {
11137            let t_w = std::time::Instant::now();
11138            // warmup: inline runs (no capture) so allocator pointers + kernel attrs are stable.
11139            for _ in 0..warmups { step(self)?; }
11140            self.gpu.stream().synchronize()?;
11141            let ms_warm = t_w.elapsed().as_secs_f64() * 1e3;
11142            // capture the third run.
11143            let t_c = std::time::Instant::now();
11144            self.gpu.stream().begin_capture(CUstreamCaptureMode::CU_STREAM_CAPTURE_MODE_RELAXED)?;
11145            // If the body errors mid-capture, end the capture before propagating so the stream isn't
11146            // left in a capturing state.
11147            let r = step(self);
11148            let ms_body = t_c.elapsed().as_secs_f64() * 1e3;
11149            let t_i = std::time::Instant::now();
11150            let g = self.gpu.stream().end_capture(iflag);
11151            let ms_inst = t_i.elapsed().as_secs_f64() * 1e3;
11152            r?;
11153            let graph = g?.ok_or("capture produced no graph (stream was not capturing)")?;
11154            let t_u = std::time::Instant::now();
11155            graph.upload()?;
11156            if ct {
11157                println!("[graph-captime] warmup2x {ms_warm:.2} ms  capture-body {ms_body:.2} ms  \
11158                          instantiate {ms_inst:.2} ms  upload {:.2} ms",
11159                         t_u.elapsed().as_secs_f64() * 1e3);
11160            }
11161            Ok(graph)
11162        };
11163        let result = run();
11164        if was_tracking { unsafe { self.gpu.ctx.enable_event_tracking(); } }
11165        result
11166    }
11167
11168    /// gdn_scan variant where state_in/out are CudaViews (resident SSM state, in-place per step).
11169    pub fn gdn_scan_s128_view(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
11170                              g: &CudaSlice<f32>, beta: &CudaSlice<f32>,
11171                              state_in: &cudarc::driver::CudaView<f32>,
11172                              state_out: &mut cudarc::driver::CudaViewMut<f32>,
11173                              o: &mut CudaSlice<f32>, n_head: usize, t: usize, scale: f32)
11174                              -> Result<(), Box<dyn std::error::Error>> {
11175        let f = self.func("gdn_scan_s128");
11176        const S_V: u32 = 128; const WARP: u32 = 32; const COLS: u32 = 4;
11177        let cfg = LaunchConfig { grid_dim: (n_head as u32, 1, S_V / COLS), block_dim: (WARP, COLS, 1), shared_mem_bytes: 0 };
11178        let (h, ti) = (n_head as i32, t as i32);
11179        let __s_b = self.gpu.stream();
11180        let mut b = __s_b.launch_builder(&f);
11181        b.arg(q).arg(k).arg(v).arg(g).arg(beta).arg(state_in).arg(state_out).arg(o).arg(&h).arg(&ti).arg(&scale);
11182        unsafe { b.launch(cfg)?; }
11183        Ok(())
11184    }
11185
11186    /// conv1d where the input is a CudaView (resident conv state assembled in place).
11187    pub fn ssm_conv1d_view(&self, x: &cudarc::driver::CudaView<f32>, w: &CudaSlice<f32>, y: &mut CudaSlice<f32>,
11188                           conv_dim: usize, t: usize, d_conv: usize, silu: bool)
11189                           -> Result<(), Box<dyn std::error::Error>> {
11190        let f = self.func("ssm_conv1d_silu_f32");
11191        // grid.x = channel, grid.y = T-tiles (block 256 strides over T) — parallel over both axes.
11192        let cfg = LaunchConfig { grid_dim: (conv_dim as u32, ((t as u32 + 255) / 256).max(1), 1),
11193                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11194        let (cd, ti, dc, s) = (conv_dim as i32, t as i32, d_conv as i32, silu as i32);
11195        let __s_b = self.gpu.stream();
11196        let mut b = __s_b.launch_builder(&f);
11197        b.arg(x).arg(w).arg(y).arg(&cd).arg(&ti).arg(&dc).arg(&s);
11198        unsafe { b.launch(cfg)?; }
11199        Ok(())
11200    }
11201
11202    /// Depthwise causal conv1d + optional SiLU.
11203    /// x:[conv_dim, T+d_conv-1] channel-major (first d_conv-1 cols = carried state),
11204    /// w:[d_conv, conv_dim] kernel-major, y:[conv_dim, T] channel-major.
11205    /// FUSED prefill conv (token-major input, zero left-state): replaces
11206    /// transpose + zeros + conv_left_pad + ssm_conv1d with ONE launch reading the matmul output
11207    /// directly. Output channel-major [conv_dim, T], SiLU applied. BIT-IDENTICAL accumulation.
11208    pub fn ssm_conv1d_tm(&self, qkv_tm: &CudaSlice<f32>, w: &CudaSlice<f32>, y: &mut CudaSlice<f32>,
11209                         conv_dim: usize, t: usize, d_conv: usize)
11210                         -> Result<(), Box<dyn std::error::Error>> {
11211        let f = self.func("ssm_conv1d_tm_f32");
11212        let cfg = LaunchConfig {
11213            grid_dim: (((conv_dim + 255) / 256) as u32, t as u32, 1),
11214            block_dim: (256, 1, 1), shared_mem_bytes: 0,
11215        };
11216        let (cd, ti, dc) = (conv_dim as i32, t as i32, d_conv as i32);
11217        let __s_b = self.gpu.stream();
11218        let mut b = __s_b.launch_builder(&f);
11219        b.arg(qkv_tm).arg(w).arg(y).arg(&cd).arg(&ti).arg(&dc);
11220        unsafe { b.launch(cfg)?; }
11221        Ok(())
11222    }
11223
11224    /// BATCHED verify conv (T>1, carried state): window reads the resident conv ring for
11225    /// negative rows; separate ring-update launch afterwards. BIT-IDENTICAL per value to the
11226    /// T=1 chain. T >= pad rides the pure input-column ring update (unchanged legacy path);
11227    /// T < pad (the MEMRA_SPEC_M2 t=2 verify arm) needs old-ring sources for the roll — the
11228    /// update kernel would race reading the ring it rewrites, so that arm clones the ring
11229    /// (dtod) and rolls via ssm_conv_ring_rebuild (PURE COPIES: the ring stores raw input
11230    /// columns; the final ring == what T sequential decode ring rolls leave).
11231    pub fn ssm_conv1d_tm_state(&self, qkv_tm: &CudaSlice<f32>, conv_state: &mut CudaSlice<f32>,
11232                               w: &CudaSlice<f32>, y: &mut CudaSlice<f32>,
11233                               conv_dim: usize, t: usize, d_conv: usize)
11234                               -> Result<(), Box<dyn std::error::Error>> {
11235        self.ssm_conv1d_tm_state_pad(qkv_tm, conv_state, w, y, conv_dim, t, d_conv, None)
11236    }
11237
11238    /// task #14: `pad_len` = device true length for PADDED prime graphs — the ring update
11239    /// reads rows [len-pad, len) instead of the pad tail. None = the classic host-T path.
11240    #[allow(clippy::too_many_arguments)]
11241    pub fn ssm_conv1d_tm_state_pad(&self, qkv_tm: &CudaSlice<f32>, conv_state: &mut CudaSlice<f32>,
11242                               w: &CudaSlice<f32>, y: &mut CudaSlice<f32>,
11243                               conv_dim: usize, t: usize, d_conv: usize,
11244                               pad_len: Option<&CudaSlice<i32>>)
11245                               -> Result<(), Box<dyn std::error::Error>> {
11246        assert!(t >= 1, "ssm_conv1d_tm_state requires T >= 1");
11247        // clone BEFORE the window kernel is issued is not required (stream-ordered: the dtod and
11248        // the window kernel both read the pre-roll ring; the roll launches after both) — but
11249        // cloning first keeps the ordering trivially correct under any future stream split.
11250        let ring_old = if t < d_conv - 1 { Some(self.clone_dtod(conv_state)?) } else { None };
11251        {
11252            let f = self.func("ssm_conv1d_tm_state_f32");
11253            let cfg = LaunchConfig {
11254                grid_dim: (((conv_dim + 255) / 256) as u32, t as u32, 1),
11255                block_dim: (256, 1, 1), shared_mem_bytes: 0,
11256            };
11257            let (cd, ti, dc) = (conv_dim as i32, t as i32, d_conv as i32);
11258            let __s_b = self.gpu.stream();
11259            let mut b = __s_b.launch_builder(&f);
11260            b.arg(qkv_tm).arg(&*conv_state).arg(w).arg(y).arg(&cd).arg(&ti).arg(&dc);
11261            unsafe { b.launch(cfg)?; }
11262        }
11263        match (ring_old, pad_len) {
11264            (None, Some(len_d)) => {
11265                let f = self.func("ssm_conv_ring_update_dev_f32");
11266                let n = conv_dim * (d_conv - 1);
11267                let cfg = LaunchConfig::for_num_elems(n as u32);
11268                let (cd, dc) = (conv_dim as i32, d_conv as i32);
11269                let __s_b = self.gpu.stream();
11270                let mut b = __s_b.launch_builder(&f);
11271                b.arg(qkv_tm).arg(conv_state).arg(len_d).arg(&cd).arg(&dc);
11272                unsafe { b.launch(cfg)?; }
11273            }
11274            (None, None) => {
11275                let f = self.func("ssm_conv_ring_update_f32");
11276                let n = conv_dim * (d_conv - 1);
11277                let cfg = LaunchConfig::for_num_elems(n as u32);
11278                let (cd, ti, dc) = (conv_dim as i32, t as i32, d_conv as i32);
11279                let __s_b = self.gpu.stream();
11280                let mut b = __s_b.launch_builder(&f);
11281                b.arg(qkv_tm).arg(conv_state).arg(&cd).arg(&ti).arg(&dc);
11282                unsafe { b.launch(cfg)?; }
11283            }
11284            (Some(old), _) => self.ssm_conv_ring_rebuild(qkv_tm, &old, conv_state, conv_dim, t, d_conv)?,
11285        }
11286        Ok(())
11287    }
11288
11289    /// qkv-view twin (task #16): batched prime reads the concat GEMM output directly.
11290    pub fn ssm_conv1d_tm_state_pad_v(&self, qkv_tm: &cudarc::driver::CudaView<f32>, conv_state: &mut CudaSlice<f32>,
11291                               w: &CudaSlice<f32>, y: &mut CudaSlice<f32>,
11292                               conv_dim: usize, t: usize, d_conv: usize,
11293                               pad_len: Option<&CudaSlice<i32>>)
11294                               -> Result<(), Box<dyn std::error::Error>> {
11295        assert!(t >= 1, "ssm_conv1d_tm_state requires T >= 1");
11296        // clone BEFORE the window kernel is issued is not required (stream-ordered: the dtod and
11297        // the window kernel both read the pre-roll ring; the roll launches after both) — but
11298        // cloning first keeps the ordering trivially correct under any future stream split.
11299        let ring_old = if t < d_conv - 1 { Some(self.clone_dtod(conv_state)?) } else { None };
11300        {
11301            let f = self.func("ssm_conv1d_tm_state_f32");
11302            let cfg = LaunchConfig {
11303                grid_dim: (((conv_dim + 255) / 256) as u32, t as u32, 1),
11304                block_dim: (256, 1, 1), shared_mem_bytes: 0,
11305            };
11306            let (cd, ti, dc) = (conv_dim as i32, t as i32, d_conv as i32);
11307            let __s_b = self.gpu.stream();
11308            let mut b = __s_b.launch_builder(&f);
11309            b.arg(qkv_tm).arg(&*conv_state).arg(w).arg(y).arg(&cd).arg(&ti).arg(&dc);
11310            unsafe { b.launch(cfg)?; }
11311        }
11312        match (ring_old, pad_len) {
11313            (None, Some(len_d)) => {
11314                let f = self.func("ssm_conv_ring_update_dev_f32");
11315                let n = conv_dim * (d_conv - 1);
11316                let cfg = LaunchConfig::for_num_elems(n as u32);
11317                let (cd, dc) = (conv_dim as i32, d_conv as i32);
11318                let __s_b = self.gpu.stream();
11319                let mut b = __s_b.launch_builder(&f);
11320                b.arg(qkv_tm).arg(conv_state).arg(len_d).arg(&cd).arg(&dc);
11321                unsafe { b.launch(cfg)?; }
11322            }
11323            (None, None) => {
11324                let f = self.func("ssm_conv_ring_update_f32");
11325                let n = conv_dim * (d_conv - 1);
11326                let cfg = LaunchConfig::for_num_elems(n as u32);
11327                let (cd, ti, dc) = (conv_dim as i32, t as i32, d_conv as i32);
11328                let __s_b = self.gpu.stream();
11329                let mut b = __s_b.launch_builder(&f);
11330                b.arg(qkv_tm).arg(conv_state).arg(&cd).arg(&ti).arg(&dc);
11331                unsafe { b.launch(cfg)?; }
11332            }
11333            (Some(_), _) => unreachable!(
11334                "ssm_conv1d_tm_state_pad_v: T < d_conv-1 has no view path (PRIME_MIN_T gates it)"),
11335        }
11336        Ok(())
11337    }
11338
11339    /// PREFIX conv-ring rebuild (spec REPLAY-FREE partial accept): overwrite the resident ring
11340    /// with the state a T=1 chain holds after only the FIRST `tc` columns of `qkv_tm` — the last
11341    /// `pad` entries of [ring_old | cols 0..tc-1]. PURE COPIES (the ring stores raw inputs; no
11342    /// arithmetic, cannot perturb FP order). `ring_old` = the pre-round snapshot ring.
11343    pub fn ssm_conv_ring_rebuild(&self, qkv_tm: &CudaSlice<f32>, ring_old: &CudaSlice<f32>,
11344                                 conv_state: &mut CudaSlice<f32>,
11345                                 conv_dim: usize, tc: usize, d_conv: usize)
11346                                 -> Result<(), Box<dyn std::error::Error>> {
11347        let f = self.func("ssm_conv_ring_rebuild_f32");
11348        let n = conv_dim * (d_conv - 1);
11349        let cfg = LaunchConfig::for_num_elems(n as u32);
11350        let (cd, ti, dc) = (conv_dim as i32, tc as i32, d_conv as i32);
11351        let __s_b = self.gpu.stream();
11352        let mut b = __s_b.launch_builder(&f);
11353        b.arg(qkv_tm).arg(ring_old).arg(conv_state).arg(&cd).arg(&ti).arg(&dc);
11354        unsafe { b.launch(cfg)?; }
11355        Ok(())
11356    }
11357
11358    /// FUSED decode GDN prep (T=1): repack + q/k L2-norm + beta sigmoid + g_log in one launch.
11359    /// Replaces 5 tiny serialized kernels on the decode critical path. L2 reduce runs as a 32-lane
11360    /// warp tree (vs l2_norm_f32's 256-thread two-level tree) — same math, different FP sum order;
11361    /// the argmax + run-spec gates are the authority.
11362    #[allow(clippy::too_many_arguments)]
11363    pub fn gdn_prep_decode(&self, conv_out: &CudaSlice<f32>, beta_raw: &CudaSlice<f32>,
11364                           alpha: &CudaSlice<f32>, dt_bias: &CudaSlice<f32>, a: &CudaSlice<f32>,
11365                           q_l2: &mut CudaSlice<f32>, k_l2: &mut CudaSlice<f32>, v_g: &mut CudaSlice<f32>,
11366                           beta: &mut CudaSlice<f32>, g_log: &mut CudaSlice<f32>,
11367                           d_state: usize, num_v: usize, num_k: usize, key_dim: usize, eps: f32)
11368                           -> Result<(), Box<dyn std::error::Error>> {
11369        let f = self.func("gdn_prep_decode_f32");
11370        let cfg = LaunchConfig { grid_dim: (num_v as u32, 1, 1), block_dim: (32, 4, 1), shared_mem_bytes: 0 };
11371        let (ds, nv, nk, kd) = (d_state as i32, num_v as i32, num_k as i32, key_dim as i32);
11372        let __s_b = self.gpu.stream();
11373        let mut b = __s_b.launch_builder(&f);
11374        b.arg(conv_out).arg(beta_raw).arg(alpha).arg(dt_bias).arg(a)
11375         .arg(q_l2).arg(k_l2).arg(v_g).arg(beta).arg(g_log)
11376         .arg(&ds).arg(&nv).arg(&nk).arg(&kd).arg(&eps);
11377        unsafe { b.launch(cfg)?; }
11378        Ok(())
11379    }
11380
11381    /// FUSED prefill conv + GDN repack: token-major qkv -> q_g/k_g/v_g in ONE launch (no conv_out
11382    /// materialization, no qkv_to_gdn_repack pass). BIT-IDENTICAL values; scatter matches
11383    /// qkv_to_gdn_repack's modulo head-repeat mapping exactly.
11384    #[allow(clippy::too_many_arguments)]
11385    pub fn ssm_conv1d_gdn(&self, qkv_tm: &CudaSlice<f32>, w: &CudaSlice<f32>,
11386                          q_g: &mut CudaSlice<f32>, k_g: &mut CudaSlice<f32>, v_g: &mut CudaSlice<f32>,
11387                          conv_dim: usize, t: usize, d_conv: usize,
11388                          d_state: usize, num_v: usize, num_k: usize, key_dim: usize)
11389                          -> Result<(), Box<dyn std::error::Error>> {
11390        let f = self.func("ssm_conv1d_gdn_f32");
11391        let cfg = LaunchConfig {
11392            grid_dim: (((conv_dim + 255) / 256) as u32, t as u32, 1),
11393            block_dim: (256, 1, 1), shared_mem_bytes: 0,
11394        };
11395        let (cd, ti, dc) = (conv_dim as i32, t as i32, d_conv as i32);
11396        let (ds, nv, nk, kd) = (d_state as i32, num_v as i32, num_k as i32, key_dim as i32);
11397        let __s_b = self.gpu.stream();
11398        let mut b = __s_b.launch_builder(&f);
11399        b.arg(qkv_tm).arg(w).arg(q_g).arg(k_g).arg(v_g)
11400         .arg(&cd).arg(&ti).arg(&dc).arg(&ds).arg(&nv).arg(&nk).arg(&kd);
11401        unsafe { b.launch(cfg)?; }
11402        Ok(())
11403    }
11404
11405    pub fn ssm_conv1d(&self, x: &CudaSlice<f32>, w: &CudaSlice<f32>, y: &mut CudaSlice<f32>,
11406                      conv_dim: usize, t: usize, d_conv: usize, silu: bool)
11407                      -> Result<(), Box<dyn std::error::Error>> {
11408        let f = self.func("ssm_conv1d_silu_f32");
11409        let cfg = LaunchConfig { grid_dim: (conv_dim as u32, ((t as u32 + 255) / 256).max(1), 1),
11410                                 block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11411        let (cd, ti, dc, s) = (conv_dim as i32, t as i32, d_conv as i32, silu as i32);
11412        let __s_b = self.gpu.stream();
11413        let mut b = __s_b.launch_builder(&f);
11414        b.arg(x).arg(w).arg(y).arg(&cd).arg(&ti).arg(&dc).arg(&s);
11415        unsafe { b.launch(cfg)?; }
11416        Ok(())
11417    }
11418
11419    /// Gated DeltaNet scan, S_v=128. q,k,v:[128,H,T]; g,beta:[H,T]; state:[128,128,H] transposed;
11420    /// o:[128,H,T]. Single sequence.
11421    pub fn gdn_scan_s128(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
11422                         g: &CudaSlice<f32>, beta: &CudaSlice<f32>, state_in: &CudaSlice<f32>,
11423                         state_out: &mut CudaSlice<f32>, o: &mut CudaSlice<f32>,
11424                         n_head: usize, t: usize, scale: f32)
11425                         -> Result<(), Box<dyn std::error::Error>> {
11426        let f = self.func("gdn_scan_s128");
11427        const S_V: u32 = 128; const WARP: u32 = 32; const COLS_PER_BLOCK: u32 = 4;
11428        let cfg = LaunchConfig {
11429            grid_dim: (n_head as u32, 1, S_V / COLS_PER_BLOCK),
11430            block_dim: (WARP, COLS_PER_BLOCK, 1),
11431            shared_mem_bytes: 0,
11432        };
11433        let (h, ti) = (n_head as i32, t as i32);
11434        let __s_b = self.gpu.stream();
11435        let mut b = __s_b.launch_builder(&f);
11436        b.arg(q).arg(k).arg(v).arg(g).arg(beta).arg(state_in).arg(state_out).arg(o).arg(&h).arg(&ti).arg(&scale);
11437        unsafe { b.launch(cfg)?; }
11438        Ok(())
11439    }
11440
11441    // ==== B2' batched decode state ops (decode_batch.rs) ====
11442    // Per-seq state pointers ride device u64 arrays (views into the per-step pointer table).
11443    // Bodies are the single-seq kernels per sequence — bit-identical per row.
11444
11445    #[allow(clippy::too_many_arguments)]
11446    pub fn ssm_conv1d_fused_decode_b(
11447        &self, qkv_cols: &CudaSlice<f32>, conv_state_ptrs: &cudarc::driver::CudaView<u64>,
11448        w: &CudaSlice<f32>, conv_outs: &mut CudaSlice<f32>, conv_dim: usize, d_conv: usize,
11449        b_n: usize) -> Result<(), Box<dyn std::error::Error>> {
11450        let f = self.func("ssm_conv1d_fused_decode_b_f32");
11451        let cfg = LaunchConfig {
11452            grid_dim: (((conv_dim + 255) / 256) as u32, 1, b_n as u32),
11453            block_dim: (256, 1, 1), shared_mem_bytes: 0,
11454        };
11455        let (cd, dc) = (conv_dim as i32, d_conv as i32);
11456        let __s_b = self.gpu.stream();
11457        let mut b = __s_b.launch_builder(&f);
11458        b.arg(qkv_cols).arg(conv_state_ptrs).arg(w).arg(conv_outs).arg(&cd).arg(&dc);
11459        unsafe { b.launch(cfg)?; }
11460        Ok(())
11461    }
11462
11463    #[allow(clippy::too_many_arguments)]
11464    pub fn gdn_prep_decode_b(
11465        &self, conv_outs: &CudaSlice<f32>, beta_raws: &CudaSlice<f32>, alphas: &CudaSlice<f32>,
11466        dt_bias: &CudaSlice<f32>, a: &CudaSlice<f32>,
11467        q_l2: &mut CudaSlice<f32>, k_l2: &mut CudaSlice<f32>, v_g: &mut CudaSlice<f32>,
11468        beta: &mut CudaSlice<f32>, g_log: &mut CudaSlice<f32>,
11469        d_state: usize, num_v: usize, num_k: usize, key_dim: usize, eps: f32,
11470        conv_dim: usize, b_n: usize) -> Result<(), Box<dyn std::error::Error>> {
11471        let f = self.func("gdn_prep_decode_b_f32");
11472        let cfg = LaunchConfig {
11473            grid_dim: (num_v as u32, 1, b_n as u32),
11474            block_dim: (32, 4, 1), shared_mem_bytes: 0,
11475        };
11476        let (ds, nv, nk, kd, cd) =
11477            (d_state as i32, num_v as i32, num_k as i32, key_dim as i32, conv_dim as i32);
11478        let __s_b = self.gpu.stream();
11479        let mut b = __s_b.launch_builder(&f);
11480        b.arg(conv_outs).arg(beta_raws).arg(alphas).arg(dt_bias).arg(a)
11481         .arg(q_l2).arg(k_l2).arg(v_g).arg(beta).arg(g_log)
11482         .arg(&ds).arg(&nv).arg(&nk).arg(&kd).arg(&eps).arg(&cd);
11483        unsafe { b.launch(cfg)?; }
11484        Ok(())
11485    }
11486
11487    #[allow(clippy::too_many_arguments)]
11488    pub fn gdn_scan_s128_batched(
11489        &self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
11490        g: &CudaSlice<f32>, beta: &CudaSlice<f32>,
11491        state_in_ptrs: &cudarc::driver::CudaView<u64>,
11492        state_out_ptrs: &cudarc::driver::CudaView<u64>,
11493        o: &mut CudaSlice<f32>, n_head: usize, b_n: usize, scale: f32)
11494        -> Result<(), Box<dyn std::error::Error>> {
11495        let f = self.func("gdn_scan_s128_b");
11496        const S_V: u32 = 128; const WARP: u32 = 32; const COLS_PER_BLOCK: u32 = 4;
11497        let cfg = LaunchConfig {
11498            grid_dim: (n_head as u32, b_n as u32, S_V / COLS_PER_BLOCK),
11499            block_dim: (WARP, COLS_PER_BLOCK, 1), shared_mem_bytes: 0,
11500        };
11501        let h = n_head as i32;
11502        let __s_b = self.gpu.stream();
11503        let mut b = __s_b.launch_builder(&f);
11504        b.arg(q).arg(k).arg(v).arg(g).arg(beta).arg(state_in_ptrs).arg(state_out_ptrs)
11505         .arg(o).arg(&h).arg(&scale);
11506        unsafe { b.launch(cfg)?; }
11507        Ok(())
11508    }
11509
11510    /// A4 seam: chunked WY GDN prefill. DEFAULT ON (`MEMRA_GDN_CHUNKED=0` = rollback to the
11511    /// sequential scan). Flipped 2026-07-04 with the full battery green: kernel-check ALL
11512    /// GREEN x {9B, 27B} incl the f64-truth chunk gates; run-gen argmax 82==82 both models
11513    /// on AND off (24/24 sweep runs); run-spec K={1,2,3,4,6,8} PASS x {9B synth, 9B text,
11514    /// 27B p2, 27B p3}; e2e first-16-token agreement 6/6 (full-256 drifts at index 47-125
11515    /// on 5/6 prompts — accepted cache-state-FP class, batched-prime precedent).
11516    /// PREFILL-ONLY: decode + spec verify never route here (decode==verify dispatch
11517    /// identity law); prime_cache/forward/forward_last are the only callers.
11518    pub fn gdn_chunked_enabled() -> bool {
11519        static E: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
11520        *E.get_or_init(|| std::env::var("MEMRA_GDN_CHUNKED").map(|v| v != "0").unwrap_or(true))
11521    }
11522
11523    /// A4 chunk size (MEMRA_GDN_CHUNK, default 32 — the sweep winner: the O(T*C) chunk
11524    /// matrices grow with C while the sequential state pass is C-flat, so smaller chunks
11525    /// win; C=32/64 also get the register-history solve template). Clamped to multiples
11526    /// of 32 in [32, 128] (kernel row mappings require it).
11527    pub fn gdn_chunk_size() -> usize {
11528        static C: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
11529        *C.get_or_init(|| {
11530            let c: usize = std::env::var("MEMRA_GDN_CHUNK").ok()
11531                .and_then(|v| v.parse().ok()).unwrap_or(32);
11532            c.clamp(32, 128) / 32 * 32
11533        })
11534    }
11535
11536    /// A4: chunked WY / blockwise-inverse GDN prefill (see cu/hybrid.cu K1-K5 header for the
11537    /// math). Same contract as `gdn_scan_s128` (layouts, state ping-pong) but chunk-parallel:
11538    /// NOT bit-identical to the sequential scan (chunked FP accumulation order); run-gen
11539    /// argmax + run-spec batteries are the accuracy authority. PREFILL callers only.
11540    #[allow(clippy::too_many_arguments)]
11541    /// task #18: K1-K3 of the chunked WY scan (shared by the per-seq path and the
11542    /// batched-prime varlen path). Returns (gcum, P, U, W); `A` is K3-internal.
11543    #[allow(clippy::too_many_arguments, clippy::type_complexity)]
11544    #[allow(clippy::too_many_arguments)]
11545    pub fn gdn_chunk_k123(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
11546                          g: &CudaSlice<f32>, beta: &CudaSlice<f32>, wb16: Option<&mut CudaSlice<u8>>,
11547                          n_head: usize, t: usize, c: usize, hk: usize,
11548                          k2w: Option<(&CudaSlice<u8>, &CudaSlice<u8>, &mut CudaSlice<u8>)>)
11549                          -> Result<(CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>, CudaSlice<f32>), Box<dyn std::error::Error>> {
11550        const D: usize = 128;
11551        let h = n_head;
11552        let nc = (t + c - 1) / c;
11553        let (hi, ti, ci) = (h as i32, t as i32, c as i32);
11554        let mut gcum = self.uninit(t * h)?;
11555        let mut a = self.uninit(nc * h * c * c)?;
11556        let mut p = self.uninit(nc * h * c * c)?;
11557        let mut u = self.uninit(nc * h * c * D)?;
11558        let mut w = self.uninit(nc * h * c * D)?;
11559        {   // K1
11560            let f = self.func("gdn_chunk_cumgate_f32");
11561            let cfg = LaunchConfig { grid_dim: (nc as u32, h as u32, 1), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
11562            let __s_b = self.gpu.stream();
11563            let mut b = __s_b.launch_builder(&f);
11564            b.arg(g).arg(&mut gcum).arg(&hi).arg(&ti).arg(&ci);
11565            unsafe { b.launch(cfg)?; }
11566        }
11567        if let Some((qb, kb, pb)) = k2w {
11568            // K2-wgmma (MEMRA_GDN_WGMMA path, c==32): A + pre-masked Pb16 in one kernel;
11569            // the P f32 buffer stays UNWRITTEN (its only wgmma-path consumer is Pb16).
11570            assert!(c == 32, "gdn_k2_wgmma is a C==32 tile");
11571            let f = self.func("gdn_k2_wgmma");
11572            let cfg = LaunchConfig { grid_dim: (nc as u32, h as u32, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
11573            let hki = hk as i32;
11574            let __s_b = self.gpu.stream();
11575            let mut b = __s_b.launch_builder(&f);
11576            b.arg(qb).arg(kb).arg(&gcum).arg(beta).arg(&mut a).arg(&mut *pb).arg(&hi).arg(&ti).arg(&ci).arg(&hki);
11577            unsafe { b.launch(cfg)?; }
11578        } else if c <= 64 && !portable_mma_gated() {   // K2 register-tiled (2x2 outputs/thread, whole-chunk smem k tile)
11579            let f = self.func("gdn_chunk_attn_f32");
11580            let jt = ((c + 31) / 32) as u32;
11581            let cfg = LaunchConfig { grid_dim: (nc as u32, h as u32, jt), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11582            let hki = hk as i32;
11583            let __s_b = self.gpu.stream();
11584            let mut b = __s_b.launch_builder(&f);
11585            b.arg(q).arg(k).arg(&gcum).arg(beta).arg(&mut a).arg(&mut p).arg(&hi).arg(&ti).arg(&ci).arg(&hki);
11586            unsafe { b.launch(cfg)?; }
11587        } else {       // K2 generic (C = 128, or the portable target's low-smem fallback)
11588            assert!(hk == h, "generic K2 is broadcast-only (de-broadcast rides C==32)");
11589            let f = self.func("gdn_chunk_attn_g_f32");
11590            let cfg = LaunchConfig { grid_dim: (nc as u32, h as u32, 1), block_dim: (32, 8, 1), shared_mem_bytes: 0 };
11591            let __s_b = self.gpu.stream();
11592            let mut b = __s_b.launch_builder(&f);
11593            b.arg(q).arg(k).arg(&gcum).arg(beta).arg(&mut a).arg(&mut p).arg(&hi).arg(&ti).arg(&ci);
11594            unsafe { b.launch(cfg)?; }
11595        }
11596        {   // K3 (register-history templates for C=32/64; local-memory generic otherwise)
11597            let cfg = LaunchConfig { grid_dim: (nc as u32, h as u32, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11598            match c {
11599                32 | 64 => {
11600                    let f = self.func(if c == 32 { "gdn_chunk_solve32_f32" } else { "gdn_chunk_solve64_f32" });
11601                    // mirror-fold: W's bf16 twin emitted on store (0 = skip)
11602                    let wb: u64 = match wb16 { Some(d) => self.addr_u8(d), None => 0 };
11603                    let hki = hk as i32;
11604                    let __s_b = self.gpu.stream();
11605                    let mut b = __s_b.launch_builder(&f);
11606                    b.arg(v).arg(k).arg(&a).arg(&gcum).arg(&mut u).arg(&mut w).arg(&wb).arg(&hi).arg(&ti).arg(&hki);
11607                    unsafe { b.launch(cfg)?; }
11608                }
11609                _ => {
11610                    assert!(hk == h, "generic K3 is broadcast-only");
11611                    let f = self.func("gdn_chunk_solve_f32");
11612                    let __s_b = self.gpu.stream();
11613                    let mut b = __s_b.launch_builder(&f);
11614                    b.arg(v).arg(k).arg(&a).arg(&gcum).arg(&mut u).arg(&mut w).arg(&hi).arg(&ti).arg(&ci);
11615                    unsafe { b.launch(cfg)?; }
11616                }
11617            }
11618        }
11619        Ok((gcum, p, u, w))
11620    }
11621
11622    /// task #21 de-broadcast seam: q/k stored at num_k distinct GQA heads instead of
11623    /// the num_v broadcast. MEMRA_GDN_DB=0 reverts. Only the chunked prefill path
11624    /// consumes the compact layout (hk plumbed; hk == H reproduces broadcast exactly).
11625    pub fn gdn_db_on() -> bool {
11626        std::env::var("MEMRA_GDN_DB").as_deref() != Ok("0")
11627    }
11628
11629    /// Whether the K4/K5 mma pair serves at chunk size `c` (mirrors gdn_scan_chunked's
11630    /// seam read — env re-read per call ON PURPOSE, kernel-check pins both configs).
11631    pub fn gdn_mma_enabled(&self, c: usize) -> bool {
11632        !portable_mma_gated() && c == 32
11633            && match std::env::var("MEMRA_GDN_MMA").as_deref() {
11634                Ok("1") => true,
11635                Ok("0") => false,
11636                _ => cfg!(memra_hopper_mma),
11637            }
11638    }
11639
11640    /// task #22: whether the fused K4+K5 (+K2) wgmma path serves (nested inside the
11641    /// mma config; same per-call env read discipline).
11642    pub fn gdn_wgmma_on(&self, c: usize) -> bool {
11643        self.gdn_mma_enabled(c)
11644            && match std::env::var("MEMRA_GDN_WGMMA").as_deref() {
11645                Ok("0") => false,
11646                Ok("1") => true,
11647                _ => cfg!(memra_hopper_mma),
11648            }
11649    }
11650
11651    /// task #18 conv-fuse: carried-ring conv + SiLU + GDN repack in ONE pass (the
11652    /// conv_out intermediate and its transposed re-read disappear — 11.8ms of the
11653    /// T=2048 prime). Ring update stays the separate follow-up launch (pad-aware).
11654    /// BIT-IDENTICAL values to ssm_conv1d_tm_state_pad + qkv_to_gdn_repack.
11655    #[allow(clippy::too_many_arguments)]
11656    pub fn ssm_conv1d_gdn_state_pad(&self, qkv_tm: &cudarc::driver::CudaView<f32>,
11657                               conv_state: &mut CudaSlice<f32>, w: &CudaSlice<f32>,
11658                               q_g: &mut CudaSlice<f32>, k_g: &mut CudaSlice<f32>,
11659                               v_g: &mut CudaSlice<f32>,
11660                               conv_dim: usize, t: usize, d_conv: usize,
11661                               d_state: usize, num_v: usize, num_k: usize, key_dim: usize,
11662                               hk: usize,
11663                               pad_len: Option<&CudaSlice<i32>>)
11664                               -> Result<(), Box<dyn std::error::Error>> {
11665        assert!(t >= d_conv - 1, "fused state conv requires T >= pad (PRIME_MIN_T gates)");
11666        {
11667            let f = self.func("ssm_conv1d_gdn_state_f32");
11668            let cfg = LaunchConfig {
11669                grid_dim: (((conv_dim + 255) / 256) as u32, t as u32, 1),
11670                block_dim: (256, 1, 1), shared_mem_bytes: 0,
11671            };
11672            let (cd, ti, dc) = (conv_dim as i32, t as i32, d_conv as i32);
11673            let (ds, nv, nk, kd, hki) = (d_state as i32, num_v as i32, num_k as i32, key_dim as i32, hk as i32);
11674            let __s_b = self.gpu.stream();
11675            let mut b = __s_b.launch_builder(&f);
11676            b.arg(qkv_tm).arg(&*conv_state).arg(w).arg(q_g).arg(k_g).arg(v_g)
11677             .arg(&cd).arg(&ti).arg(&dc).arg(&ds).arg(&nv).arg(&nk).arg(&kd).arg(&hki);
11678            unsafe { b.launch(cfg)?; }
11679        }
11680        match pad_len {
11681            Some(len_d) => {
11682                let f = self.func("ssm_conv_ring_update_dev_f32");
11683                let n = conv_dim * (d_conv - 1);
11684                let cfg = LaunchConfig::for_num_elems(n as u32);
11685                let (cd, dc) = (conv_dim as i32, d_conv as i32);
11686                let __s_b = self.gpu.stream();
11687                let mut b = __s_b.launch_builder(&f);
11688                b.arg(qkv_tm).arg(conv_state).arg(len_d).arg(&cd).arg(&dc);
11689                unsafe { b.launch(cfg)?; }
11690            }
11691            None => {
11692                let f = self.func("ssm_conv_ring_update_f32");
11693                let n = conv_dim * (d_conv - 1);
11694                let cfg = LaunchConfig::for_num_elems(n as u32);
11695                let (cd, ti, dc) = (conv_dim as i32, t as i32, d_conv as i32);
11696                let __s_b = self.gpu.stream();
11697                let mut b = __s_b.launch_builder(&f);
11698                b.arg(qkv_tm).arg(conv_state).arg(&cd).arg(&ti).arg(&dc);
11699                unsafe { b.launch(cfg)?; }
11700            }
11701        }
11702        Ok(())
11703    }
11704
11705    /// task #18 increment 2: allocate ONE sequence's chunk buffers (no launches) —
11706    /// K1-K5 all run varlen afterwards. `a`/`w` become struct members so the varlen
11707    /// K2/K3 can write them.
11708    pub fn gdn_chunk_alloc(&self, n_head: usize, t: usize, c: usize, hk: usize)
11709                           -> Result<GdnChunkBufs, Box<dyn std::error::Error>> {
11710        const D: usize = 128;
11711        assert!(c == 32, "gdn_chunk_alloc: varlen chain is the C==32 mma pair");
11712        let h = n_head;
11713        let nc = (t + c - 1) / c;
11714        Ok(GdnChunkBufs {
11715            gcum: self.uninit(t * h)?,
11716            a: self.uninit(nc * h * c * c)?,
11717            p: self.uninit(nc * h * c * c)?,
11718            u: self.uninit(nc * h * c * D)?,
11719            w: self.uninit(nc * h * c * D)?,
11720            kb16: self.alloc_u8_uninit(t * hk * D * 2)?,
11721            wb16: self.alloc_u8_uninit(nc * h * c * D * 2)?,
11722            y16: self.alloc_u8_uninit(nc * h * c * D * 2)?,
11723            ssnap16: self.alloc_u8_uninit(nc * h * D * D * 2)?,
11724            qb16: self.alloc_u8_uninit(t * hk * D * 2)?,
11725            pb16: self.alloc_u8_uninit(nc * h * c * c * 2)?,
11726            o: self.uninit(D * h * t)?,
11727            t, nc,
11728        })
11729    }
11730
11731    /// view-source twin of f32_to_bf16 (the batched FA3 v mirror reads a concat view).
11732    pub fn f32_to_bf16_v(&self, x: &cudarc::driver::CudaView<f32>, dst: &mut CudaSlice<u8>, n: usize)
11733                         -> Result<(), Box<dyn std::error::Error>> {
11734        let f = self.func("f32_to_bf16_bulk");
11735        let ni = n as i64;
11736        let cfg = LaunchConfig::for_num_elems((n as u32).div_ceil(4));
11737        let __s_b = self.gpu.stream();
11738        let mut b = __s_b.launch_builder(&f);
11739        b.arg(x).arg(dst).arg(&ni);
11740        unsafe { b.launch(cfg)?; }
11741        Ok(())
11742    }
11743
11744    /// f32 -> bf16 bulk mirror into a caller buffer (the K4/K5 operand mirrors).
11745    pub fn f32_to_bf16_into(&self, x: &CudaSlice<f32>, dst: &mut CudaSlice<u8>, n: usize)
11746                       -> Result<(), Box<dyn std::error::Error>> {
11747        let f = self.func("f32_to_bf16_bulk");
11748        let ni = n as i64;
11749        let cfg = LaunchConfig::for_num_elems((n as u32).div_ceil(4));
11750        let __s_b = self.gpu.stream();
11751        let mut b = __s_b.launch_builder(&f);
11752        b.arg(x).arg(dst).arg(&ni);
11753        unsafe { b.launch(cfg)?; }
11754        Ok(())
11755    }
11756
11757    /// task #18 increment 2: varlen K1+K2+K3 — three launches run every sequence's
11758    /// cumgate/attn/solve (per-block math identical to the per-seq kernels).
11759    pub fn gdn_chunk_k123_vl8(&self, seqs: &[GdnSeqVl], n_head: usize, hk: usize,
11760                              wq: Option<&GdnWVl8>)
11761                              -> Result<(), Box<dyn std::error::Error>> {
11762        let b = seqs.len();
11763        assert!(b >= 1 && b <= 8, "gdn_chunk_k123_vl8: 1..=8 sequences");
11764        let mut packed = [GdnSeqVl::default(); 8];
11765        packed[..b].copy_from_slice(seqs);
11766        let v = GdnVl8(packed);
11767        let (hi, ci) = (n_head as i32, 32i32);
11768        let max_nc = seqs.iter().map(|a| a.nc).max().unwrap() as u32;
11769        {
11770            let f = self.func("gdn_chunk_cumgate_vl");
11771            let cfg = LaunchConfig { grid_dim: (max_nc, n_head as u32, b as u32), block_dim: (32, 1, 1), shared_mem_bytes: 0 };
11772            let __s_lb = self.gpu.stream();
11773            let mut lb = __s_lb.launch_builder(&f);
11774            lb.arg(&v).arg(&hi).arg(&ci);
11775            unsafe { lb.launch(cfg)?; }
11776        }
11777        let hki = hk as i32;
11778        if let Some(w) = wq {   // K2-wgmma vl twin (writes A + pre-masked Pb16)
11779            let f = self.func("gdn_k2_wgmma_vl");
11780            let cfg = LaunchConfig { grid_dim: (max_nc, n_head as u32, b as u32), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
11781            let __s_lb = self.gpu.stream();
11782            let mut lb = __s_lb.launch_builder(&f);
11783            lb.arg(&v).arg(w).arg(&hi).arg(&ci).arg(&hki);
11784            unsafe { lb.launch(cfg)?; }
11785        } else {
11786            let f = self.func("gdn_chunk_attn_vl");
11787            let cfg = LaunchConfig { grid_dim: (max_nc, n_head as u32, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11788            let __s_lb = self.gpu.stream();
11789            let mut lb = __s_lb.launch_builder(&f);
11790            lb.arg(&v).arg(&hi).arg(&ci).arg(&hki);
11791            unsafe { lb.launch(cfg)?; }
11792        }
11793        {
11794            let f = self.func("gdn_chunk_solve32_vl");
11795            let cfg = LaunchConfig { grid_dim: (max_nc, n_head as u32, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11796            let __s_lb = self.gpu.stream();
11797            let mut lb = __s_lb.launch_builder(&f);
11798            lb.arg(&v).arg(&hi).arg(&ci).arg(&hki);
11799            unsafe { lb.launch(cfg)?; }
11800        }
11801        Ok(())
11802    }
11803
11804    /// task #18 increment 3: varlen PREP chain — conv(+ring) / repack / fused-l2 /
11805    /// fused gate-prep, 5 launches for every sequence (per-element math identical
11806    /// to the per-seq kernels; l2/gate fusions write disjoint outputs).
11807    #[allow(clippy::too_many_arguments)]
11808    pub fn gdn_prep_vl8(&self, seqs: &[GdnPrepVl], conv_w: &CudaSlice<f32>,
11809                        dt_bias: &CudaSlice<f32>, a: &CudaSlice<f32>,
11810                        conv_dim: usize, d_conv: usize, d_state: usize,
11811                        num_v: usize, num_k: usize, key_dim: usize, hk: usize, eps: f32)
11812                        -> Result<(), Box<dyn std::error::Error>> {
11813        let b = seqs.len();
11814        assert!(b >= 1 && b <= 8);
11815        let mut packed = [GdnPrepVl::default(); 8];
11816        packed[..b].copy_from_slice(seqs);
11817        let v = GdnPrepVl8(packed);
11818        let max_t = seqs.iter().map(|s| s.t).max().unwrap() as u32;
11819        let (cdi, dci) = (conv_dim as i32, d_conv as i32);
11820        let conv_fuse = std::env::var("MEMRA_CONV_FUSE").as_deref() != Ok("0");
11821        assert!(conv_fuse || hk == num_v, "de-broadcast requires the fused conv");
11822        if conv_fuse {
11823            let f = self.func("ssm_conv1d_gdn_state_vl");
11824            let cfg = LaunchConfig { grid_dim: ((conv_dim as u32).div_ceil(256), max_t, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11825            let (dsi, nvi, nki, kdi, hki) = (d_state as i32, num_v as i32, num_k as i32, key_dim as i32, hk as i32);
11826            let __s_lb = self.gpu.stream();
11827            let mut lb = __s_lb.launch_builder(&f);
11828            lb.arg(&v).arg(conv_w).arg(&cdi).arg(&dci).arg(&dsi).arg(&nvi).arg(&nki).arg(&kdi).arg(&hki);
11829            unsafe { lb.launch(cfg)?; }
11830        } else {
11831            let f = self.func("ssm_conv1d_tm_state_vl");
11832            let cfg = LaunchConfig { grid_dim: ((conv_dim as u32).div_ceil(256), max_t, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11833            let __s_lb = self.gpu.stream();
11834            let mut lb = __s_lb.launch_builder(&f);
11835            lb.arg(&v).arg(conv_w).arg(&cdi).arg(&dci);
11836            unsafe { lb.launch(cfg)?; }
11837        }
11838        {
11839            let f = self.func("ssm_conv_ring_update_vl");
11840            let n = (conv_dim * (d_conv - 1)) as u32;
11841            let cfg = LaunchConfig { grid_dim: (n.div_ceil(256), 1, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11842            let __s_lb = self.gpu.stream();
11843            let mut lb = __s_lb.launch_builder(&f);
11844            lb.arg(&v).arg(&cdi).arg(&dci);
11845            unsafe { lb.launch(cfg)?; }
11846        }
11847        if !conv_fuse {
11848            let f = self.func("qkv_to_gdn_repack_vl");
11849            let n = max_t * (num_v * d_state) as u32;
11850            let cfg = LaunchConfig { grid_dim: (n.div_ceil(256), 1, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11851            let (dsi, nvi, nki, kdi) = (d_state as i32, num_v as i32, num_k as i32, key_dim as i32);
11852            let __s_lb = self.gpu.stream();
11853            let mut lb = __s_lb.launch_builder(&f);
11854            lb.arg(&v).arg(&dsi).arg(&nvi).arg(&nki).arg(&kdi);
11855            unsafe { lb.launch(cfg)?; }
11856        }
11857        if Self::l2_v2_on(d_state) {
11858            let f = self.func("gdn_l2_v2_vl");
11859            let cfg = LaunchConfig { grid_dim: ((max_t * hk as u32).div_ceil(8), 2, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11860            let (dsi, nvi) = (d_state as i32, hk as i32);
11861            let __s_lb = self.gpu.stream();
11862            let mut lb = __s_lb.launch_builder(&f);
11863            lb.arg(&v).arg(&dsi).arg(&nvi).arg(&eps);
11864            unsafe { lb.launch(cfg)?; }
11865        } else {
11866            let f = self.func("gdn_l2_vl");
11867            let cfg = LaunchConfig { grid_dim: (max_t * hk as u32, 2, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11868            let (dsi, nvi) = (d_state as i32, hk as i32);
11869            let __s_lb = self.gpu.stream();
11870            let mut lb = __s_lb.launch_builder(&f);
11871            lb.arg(&v).arg(&dsi).arg(&nvi).arg(&eps);
11872            unsafe { lb.launch(cfg)?; }
11873        }
11874        {
11875            let f = self.func("gdn_gate_prep_vl");
11876            let n = max_t * num_v as u32;
11877            let cfg = LaunchConfig { grid_dim: (n.div_ceil(256), 1, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11878            let nvi = num_v as i32;
11879            let __s_lb = self.gpu.stream();
11880            let mut lb = __s_lb.launch_builder(&f);
11881            lb.arg(&v).arg(dt_bias).arg(a).arg(&nvi);
11882            unsafe { lb.launch(cfg)?; }
11883        }
11884        Ok(())
11885    }
11886
11887    /// varlen bf16 mirrors over the gdnseq_t table (which: 0 = k_l2 -> kb16, 1 = w -> wb16).
11888    pub fn gdn_mirror_vl8(&self, seqs: &[GdnSeqVl], n_head: usize, which: i32, hk: usize)
11889                          -> Result<(), Box<dyn std::error::Error>> {
11890        let b = seqs.len();
11891        assert!(b >= 1 && b <= 8);
11892        let mut packed = [GdnSeqVl::default(); 8];
11893        packed[..b].copy_from_slice(seqs);
11894        let v = GdnVl8(packed);
11895        let ept = (if which == 0 { hk } else { n_head } * 128) as i32;
11896        let max_n = seqs.iter().map(|s| if which == 0 { s.t as i64 * ept as i64 }
11897                                        else { s.nc as i64 * ept as i64 * 32 }).max().unwrap();
11898        let f = self.func("gdn_mirror_vl");
11899        let blocks = ((max_n as u32).div_ceil(4)).div_ceil(256);
11900        let cfg = LaunchConfig { grid_dim: (blocks, 1, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11901        let __s_lb = self.gpu.stream();
11902        let mut lb = __s_lb.launch_builder(&f);
11903        lb.arg(&v).arg(&ept).arg(&which);
11904        unsafe { lb.launch(cfg)?; }
11905        Ok(())
11906    }
11907
11908    /// varlen gated-norm tail (+f16out) — one launch replaces B gated_rmsnorm calls.
11909    pub fn gdn_tail_vl8(&self, seqs: &[GdnPrepVl], norm_w: &CudaSlice<f32>,
11910                        d_state: usize, num_v: usize, eps: f32)
11911                        -> Result<(), Box<dyn std::error::Error>> {
11912        let b = seqs.len();
11913        assert!(b >= 1 && b <= 8);
11914        let mut packed = [GdnPrepVl::default(); 8];
11915        packed[..b].copy_from_slice(seqs);
11916        let v = GdnPrepVl8(packed);
11917        let max_t = seqs.iter().map(|s| s.t).max().unwrap() as u32;
11918        let f = self.func("gated_rmsnorm_f16out_vl");
11919        // block_dim MUST match gated_rmsnorm's (128): the reduction tree order pins the scale
11920        let cfg = LaunchConfig { grid_dim: (max_t * num_v as u32, 1, b as u32), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
11921        let (dsi, nvi) = (d_state as i32, num_v as i32);
11922        let __s_lb = self.gpu.stream();
11923        let mut lb = __s_lb.launch_builder(&f);
11924        lb.arg(&v).arg(norm_w).arg(&dsi).arg(&nvi).arg(&eps);
11925        unsafe { lb.launch(cfg)?; }
11926        Ok(())
11927    }
11928
11929    /// Raw device address helpers for the varlen by-value arg struct (single-stream
11930    /// launches; every buffer outlives the call — the f16 FFI discipline).
11931    pub fn addr_f32(&self, x: &CudaSlice<f32>) -> u64 {
11932        use cudarc::driver::DevicePtr;
11933        let s = self.gpu.stream();
11934        let (p, _g) = x.device_ptr(&s);
11935        p as u64
11936    }
11937    pub fn addr_f32_mut(&self, x: &mut CudaSlice<f32>) -> u64 {
11938        use cudarc::driver::DevicePtrMut;
11939        let s = self.gpu.stream();
11940        let (p, _g) = x.device_ptr_mut(&s);
11941        p as u64
11942    }
11943    pub fn addr_f32v(&self, x: &cudarc::driver::CudaView<f32>) -> u64 {
11944        use cudarc::driver::DevicePtr;
11945        let s = self.gpu.stream();
11946        let (p, _g) = x.device_ptr(&s);
11947        p as u64
11948    }
11949    pub fn addr_u8(&self, x: &CudaSlice<u8>) -> u64 {
11950        use cudarc::driver::DevicePtr;
11951        let s = self.gpu.stream();
11952        let (p, _g) = x.device_ptr(&s);
11953        p as u64
11954    }
11955
11956    /// task #18: the varlen K4+K5 pair — TWO launches run every sequence's state pass
11957    /// and output pass (grid gains a seq dim; per-block math identical to the per-seq
11958    /// launches, so this is strictly bit-gateable against them).
11959    pub fn gdn_chunk_vl8(&self, seqs: &[GdnSeqVl], n_head: usize, scale: f32, hk: usize,
11960                         wq: Option<&GdnWVl8>)
11961                         -> Result<(), Box<dyn std::error::Error>> {
11962        const NSPLIT: u32 = 4;
11963        let b = seqs.len();
11964        assert!(b >= 1 && b <= 8, "gdn_chunk_vl8: 1..=8 sequences");
11965        let mut packed = [GdnSeqVl::default(); 8];
11966        packed[..b].copy_from_slice(seqs);
11967        let v = GdnVl8(packed);
11968        let (hi, ci) = (n_head as i32, 32i32);
11969        let max_nc = seqs.iter().map(|a| a.nc).max().unwrap() as u32;
11970        let hki = hk as i32;
11971        if let Some(w) = wq {
11972            // K4+K5 fused wgmma vl twin: one launch, Y/Ssnap never materialized.
11973            let f = self.func("gdn_k45_wgmma_vl");
11974            let cfg = LaunchConfig { grid_dim: (n_head as u32, NSPLIT, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11975            let __s_lb = self.gpu.stream();
11976            let mut lb = __s_lb.launch_builder(&f);
11977            lb.arg(&v).arg(w).arg(&scale).arg(&hi).arg(&ci).arg(&hki);
11978            unsafe { lb.launch(cfg)?; }
11979            let _ = max_nc;
11980            return Ok(());
11981        }
11982        {
11983            let f = self.func("gdn_chunk_state_mma_vl");
11984            let cfg = LaunchConfig { grid_dim: (n_head as u32, NSPLIT, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11985            let __s_lb = self.gpu.stream();
11986            let mut lb = __s_lb.launch_builder(&f);
11987            lb.arg(&v).arg(&hi).arg(&ci).arg(&hki);
11988            unsafe { lb.launch(cfg)?; }
11989        }
11990        {
11991            let f = self.func("gdn_chunk_output_mma_vl");
11992            let cfg = LaunchConfig { grid_dim: (max_nc, n_head as u32, b as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
11993            let __s_lb = self.gpu.stream();
11994            let mut lb = __s_lb.launch_builder(&f);
11995            lb.arg(&v).arg(&hi).arg(&ci).arg(&scale).arg(&hki);
11996            unsafe { lb.launch(cfg)?; }
11997        }
11998        Ok(())
11999    }
12000    pub fn gdn_scan_chunked(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
12001                            g: &CudaSlice<f32>, beta: &CudaSlice<f32>, kb16_pre: Option<&CudaSlice<u8>>,
12002                            qb16_pre: Option<&CudaSlice<u8>>,
12003                            state_in: &CudaSlice<f32>,
12004                            state_out: &mut CudaSlice<f32>, o: &mut CudaSlice<f32>,
12005                            n_head: usize, t: usize, scale: f32, c: usize, hk: usize)
12006                            -> Result<(), Box<dyn std::error::Error>> {
12007        const D: usize = 128;
12008        const NSPLIT: u32 = 4;
12009        assert!(c >= 1 && c <= 128, "gdn_scan_chunked: C must be in 1..=128");
12010        let h = n_head;
12011        let nc = (t + c - 1) / c;
12012        let (hi, ti, ci) = (h as i32, t as i32, c as i32);
12013        // mirror-fold (round 27): on the mma path W's bf16 twin is emitted by K3's store
12014        // (wb16 pre-allocated and threaded through k123) and k's by the producer l2 when
12015        // the caller hands `kb16_pre` — both standalone mirror passes disappear.
12016        let gdn_mma_pre = !portable_mma_gated() && c == 32
12017            && match std::env::var("MEMRA_GDN_MMA").as_deref() {
12018                Ok("1") => true,
12019                Ok("0") => false,
12020                _ => cfg!(memra_hopper_mma),
12021            };
12022        let mut wb16_pre: Option<CudaSlice<u8>> = if gdn_mma_pre {
12023            Some(self.alloc_u8_uninit(nc * h * c * D * 2)?)
12024        } else { None };
12025        // K2-wgmma pre-work (MEMRA_GDN_WGMMA): the kb16/qb16 mirrors hoist ABOVE K123 so
12026        // K2 rides them via cp.async; K2 writes the pre-masked Pb16 directly (the
12027        // gdn_p_bf16_masked pass and the in-branch mirror builds disappear).
12028        let gdn_wgmma_pre = gdn_mma_pre
12029            && match std::env::var("MEMRA_GDN_WGMMA").as_deref() {
12030                Ok("0") => false,
12031                Ok("1") => true,
12032                _ => cfg!(memra_hopper_mma),
12033            };
12034        let nk = t * hk * D;
12035        let mut kb16_local: Option<CudaSlice<u8>> = None;
12036        if gdn_mma_pre && kb16_pre.is_none() {
12037            let mut kb = self.alloc_u8_uninit(nk * 2)?;
12038            let f = self.func("f32_to_bf16_bulk");
12039            let n2 = nk as i64;
12040            let cfg2 = LaunchConfig::for_num_elems((nk as u32).div_ceil(4));
12041            let __s_b = self.gpu.stream();
12042            let mut b = __s_b.launch_builder(&f);
12043            b.arg(k).arg(&mut kb).arg(&n2);
12044            unsafe { b.launch(cfg2)?; }
12045            kb16_local = Some(kb);
12046        }
12047        let kb16_ref0: Option<&CudaSlice<u8>> = kb16_local.as_ref().or(kb16_pre);
12048        if let Some(kb) = kb16_pre { assert!(kb.len() >= nk * 2, "kb16_pre too small"); }
12049        let mut qb16: Option<CudaSlice<u8>> = None;
12050        let mut pb16: Option<CudaSlice<u8>> = None;
12051        if gdn_wgmma_pre {
12052            // mirror-fold (round 35): prep's l2 v2 emits qb16 in-epilogue (kb16 pattern);
12053            // the standalone bulk cvt only serves callers without the prep mirror.
12054            if qb16_pre.is_none() {
12055                let mut qb = self.alloc_u8_uninit(nk * 2)?;
12056                let f = self.func("f32_to_bf16_bulk");
12057                let n2 = nk as i64;
12058                let cfg2 = LaunchConfig::for_num_elems((nk as u32).div_ceil(4));
12059                let __s_b = self.gpu.stream();
12060                let mut b = __s_b.launch_builder(&f);
12061                b.arg(q).arg(&mut qb).arg(&n2);
12062                unsafe { b.launch(cfg2)?; }
12063                qb16 = Some(qb);
12064            } else if let Some(qb) = qb16_pre {
12065                assert!(qb.len() >= nk * 2, "qb16_pre too small");
12066            }
12067            pb16 = Some(self.alloc_u8_uninit(nc * h * c * c * 2)?);
12068        }
12069        let qb16_ref0: Option<&CudaSlice<u8>> = qb16.as_ref().or(qb16_pre);
12070        let k2w = if gdn_wgmma_pre {
12071            Some((*qb16_ref0.as_ref().unwrap(),
12072                  *kb16_ref0.as_ref().unwrap(),
12073                  pb16.as_mut().unwrap()))
12074        } else { None };
12075        let (gcum, p, u, w) = self.gdn_chunk_k123(q, k, v, g, beta, wb16_pre.as_mut(), n_head, t, c, hk, k2w)?;
12076        let _ = &w;
12077        let mut y = self.uninit(nc * h * c * D)?;
12078        let mut ssnap = self.uninit(nc * h * D * D)?;   // chunk-start state snapshots (K5 phase 1)
12079        // K4-MMA seam (MEMRA_GDN_MMA; harness verdict 1.75x — tools/bench_gdn_k4.cu, ledger
12080        // 2026-07-26): M in mma accumulator fragments, bf16 W/k mirrors through a cp.async
12081        // ring. C==32 only (the kernel's tile). PROMOTED default-ON on the Hopper lane
12082        // after the STATE-CARRY battery (2026-07-26): 2048-token prime (64 in-kernel state
12083        // carries) -> 256 greedy decode tokens IDENTICAL to f32 on 3 seeds, AND chunked-
12084        // continuation prime (MEMRA_PRIME_CHUNK=512, 4 cross-call carries via cache.recur)
12085        // IDENTICAL on 2 seeds; plus argmax MATCH, pp512 +3.5% (17286), oracle out
12086        // mean_rel ~1e-4. kernel-check pins BOTH configs (f32 tight band forced =0; mma
12087        // band 8e-2/8e-1 vs f64 truth). =0 reverts; portable stays f32. NOT read via
12088        // OnceLock ON PURPOSE: kernel-check toggles the env per call to pin both forms.
12089        let gdn_mma = !portable_mma_gated() && c == 32
12090            && match std::env::var("MEMRA_GDN_MMA").as_deref() {
12091                Ok("1") => true,
12092                Ok("0") => false,
12093                _ => cfg!(memra_hopper_mma),
12094            };
12095        if gdn_mma {
12096            let wb16 = wb16_pre.take().expect("mma path pre-allocates wb16 (K3 store fold)");
12097            let kb16_ref: &CudaSlice<u8> = kb16_ref0.expect("mma path pre-builds kb16 above K123");
12098            // K4+K5 FUSED wgmma seam (MEMRA_GDN_WGMMA, task #22; harness verdict
12099            // tools/bench_gdn_wgmma.cu v5, ledger 1f08b997: in-band Y 1.07e-2 / state
12100            // 1.03e-2 / O 1.08e-2, 91.3us vs 70.4 K4-only at H=32 T=512). K5's output
12101            // pass runs inside the persistent-M kernel; Y and Ssnap are never
12102            // materialized. New numeric class (gk folds into k^T instead of ys) —
12103            // explicit opt-in until the state-carry battery promotes it. Env read per
12104            // call (kernel-check pins configs by toggling env, GDN_MMA precedent).
12105            // PROMOTED default-ON hopper (2026-07-27): full battery green — harness
12106            // in-band, argmax gate PASS, 3-seed greedy IDENTICAL after ~2k prime,
12107            // chunked-continuation IDENTICAL, kernel-check + decode-batch gates green,
12108            // official prefill lane +0.74% interleaved x5 (5/5 rounds). =0 reverts.
12109            if gdn_wgmma_pre {
12110                // qb16/pb16 pre-built above K123 (K2-wgmma wrote the masked Pb16).
12111                let qb16 = qb16_ref0.unwrap();
12112                let pb16 = pb16.as_ref().unwrap();
12113                {
12114                    let f = self.func("gdn_k45_wgmma");
12115                    let cfg = LaunchConfig { grid_dim: (h as u32, 4, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
12116                    let hki = hk as i32;
12117                    let __s_b = self.gpu.stream();
12118                    let mut b = __s_b.launch_builder(&f);
12119                    b.arg(kb16_ref).arg(&gcum).arg(beta).arg(&u).arg(&wb16).arg(qb16).arg(pb16)
12120                     .arg(o).arg(&scale).arg(state_in).arg(&mut *state_out).arg(&hi).arg(&ti).arg(&ci).arg(&hki);
12121                    unsafe { b.launch(cfg)?; }
12122                }
12123                return Ok(());
12124            }
12125            // COUPLED PAIR: K4-mma writes Y and Ssnap as bf16 (their only consumer is
12126            // K5-mma, which rounds to bf16 regardless — identical numerics, half the
12127            // traffic; harness K5 63.0 -> 35.3us). Fresh bf16 buffers replace the f32 ones.
12128            let mut y16 = self.alloc_u8_uninit(nc * h * c * D * 2)?;
12129            let mut ssnap16 = self.alloc_u8_uninit(nc * h * D * D * 2)?;
12130            {
12131                let f = self.func("gdn_chunk_state_mma");
12132                let cfg = LaunchConfig { grid_dim: (h as u32, NSPLIT, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
12133                let hki = hk as i32;
12134                let __s_b = self.gpu.stream();
12135                let mut b = __s_b.launch_builder(&f);
12136                b.arg(kb16_ref).arg(&gcum).arg(beta).arg(&u).arg(&wb16).arg(&mut y16).arg(&mut ssnap16)
12137                 .arg(state_in).arg(&mut *state_out).arg(&hi).arg(&ti).arg(&ci).arg(&hki);
12138                unsafe { b.launch(cfg)?; }
12139            }
12140            {   // K5-mma (bf16 St/Y consumers)
12141                let f = self.func("gdn_chunk_output_mma");
12142                let jt = ((c + 31) / 32) as u32;
12143                let cfg = LaunchConfig { grid_dim: (nc as u32, h as u32, jt), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
12144                let hki = hk as i32;
12145                let __s_b = self.gpu.stream();
12146                let mut b = __s_b.launch_builder(&f);
12147                b.arg(q).arg(&gcum).arg(&p).arg(&y16).arg(&ssnap16).arg(o).arg(&hi).arg(&ti).arg(&ci).arg(&scale).arg(&hki);
12148                unsafe { b.launch(cfg)?; }
12149            }
12150            return Ok(());
12151        }
12152        {   // K4 (sequential over chunks inside; blocks col-partition the state)
12153            let f = self.func("gdn_chunk_state_f32");
12154            let cfg = LaunchConfig { grid_dim: (h as u32, NSPLIT, 1), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
12155            let __s_b = self.gpu.stream();
12156            let mut b = __s_b.launch_builder(&f);
12157            b.arg(k).arg(&gcum).arg(beta).arg(&u).arg(&w).arg(&mut y).arg(&mut ssnap)
12158             .arg(state_in).arg(&mut *state_out).arg(&hi).arg(&ti).arg(&ci);
12159            unsafe { b.launch(cfg)?; }
12160        }
12161        {   // K5 (j-blocked: grid.z = 32-row output blocks per chunk; writes o fully)
12162            let f = self.func("gdn_chunk_output_f32");
12163            let jt = ((c + 31) / 32) as u32;
12164            let cfg = LaunchConfig { grid_dim: (nc as u32, h as u32, jt), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
12165            let __s_b = self.gpu.stream();
12166            let mut b = __s_b.launch_builder(&f);
12167            b.arg(q).arg(&gcum).arg(&p).arg(&y).arg(&ssnap).arg(o).arg(&hi).arg(&ti).arg(&ci).arg(&scale);
12168            unsafe { b.launch(cfg)?; }
12169        }
12170        Ok(())
12171    }
12172
12173    /// PREFILL GDN scan dispatch (the A4 seam): chunked WY form when enabled and T is in the
12174    /// batched-prefill regime, else the sequential scan. Callers: hybrid_forward::linear_attn
12175    /// (forward/forward_last) + linear_attn_prime (prime_cache). Decode (T=1) and the spec
12176    /// verify call `gdn_scan_s128` DIRECTLY — the decode==verify dispatch identity is untouched.
12177    ///
12178    /// MEMRA_GDN_DIFF=1: numerical-oracle mode — runs BOTH forms on the same inputs, prints the
12179    /// per-call (== per-layer, in call order) output/state error distribution, and keeps the
12180    /// SEQUENTIAL results so the run stays on the shipped path (stage-1 prototype evidence).
12181    #[allow(clippy::too_many_arguments)]
12182    #[allow(clippy::too_many_arguments)]
12183    pub fn gdn_scan_prefill(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
12184                            g: &CudaSlice<f32>, beta: &CudaSlice<f32>, kb16_pre: Option<&CudaSlice<u8>>,
12185                            qb16_pre: Option<&CudaSlice<u8>>,
12186                            state_in: &CudaSlice<f32>,
12187                            state_out: &mut CudaSlice<f32>, o: &mut CudaSlice<f32>,
12188                            n_head: usize, t: usize, scale: f32, hk: usize)
12189                            -> Result<(), Box<dyn std::error::Error>> {
12190        if std::env::var("MEMRA_GDN_DIFF").is_ok() && t >= 16 {
12191            assert!(hk == n_head, "GDN_DIFF oracle is broadcast-only");
12192            return self.gdn_scan_diff(q, k, v, g, beta, state_in, state_out, o, n_head, t, scale);
12193        }
12194        if Self::gdn_chunked_enabled() && t >= 16 {
12195            self.gdn_scan_chunked(q, k, v, g, beta, kb16_pre, qb16_pre, state_in, state_out, o, n_head, t, scale,
12196                                  Self::gdn_chunk_size(), hk)
12197        } else {
12198            assert!(hk == n_head, "s128 scan is broadcast-only (prep guarantees by predicate)");
12199            self.gdn_scan_s128(q, k, v, g, beta, state_in, state_out, o, n_head, t, scale)
12200        }
12201    }
12202
12203    /// Stage-1 oracle: run sequential AND chunked, report per-call error stats, keep sequential.
12204    #[allow(clippy::too_many_arguments)]
12205    fn gdn_scan_diff(&self, q: &CudaSlice<f32>, k: &CudaSlice<f32>, v: &CudaSlice<f32>,
12206                     g: &CudaSlice<f32>, beta: &CudaSlice<f32>, state_in: &CudaSlice<f32>,
12207                     state_out: &mut CudaSlice<f32>, o: &mut CudaSlice<f32>,
12208                     n_head: usize, t: usize, scale: f32)
12209                     -> Result<(), Box<dyn std::error::Error>> {
12210        static CALL: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
12211        let call = CALL.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
12212        let mut o_c = self.uninit(o.len())?;
12213        let mut st_c = self.uninit(state_out.len())?;
12214        self.gdn_scan_chunked(q, k, v, g, beta, None, None, state_in, &mut st_c, &mut o_c,
12215                              n_head, t, scale, Self::gdn_chunk_size(), n_head)?;
12216        self.gdn_scan_s128(q, k, v, g, beta, state_in, state_out, o, n_head, t, scale)?;
12217        let (oh_s, oh_c) = (self.dtoh(o)?, self.dtoh(&o_c)?);
12218        let (sh_s, sh_c) = (self.dtoh(state_out)?, self.dtoh(&st_c)?);
12219        let stats = |a: &[f32], b: &[f32]| -> (f32, f32, f64) {
12220            let mut max_abs = 0f32; let mut max_rel = 0f32; let mut sum_rel = 0f64;
12221            for (x, y) in a.iter().zip(b) {
12222                let ad = (x - y).abs();
12223                let rel = ad / x.abs().max(y.abs()).max(1e-3);
12224                if ad > max_abs { max_abs = ad; }
12225                if rel > max_rel { max_rel = rel; }
12226                sum_rel += rel as f64;
12227            }
12228            (max_abs, max_rel, sum_rel / a.len() as f64)
12229        };
12230        let (o_ma, o_mr, o_mean) = stats(&oh_s, &oh_c);
12231        let (s_ma, s_mr, s_mean) = stats(&sh_s, &sh_c);
12232        println!("[gdn-diff call {call:3} T={t} C={}] out: max_abs={o_ma:.3e} max_rel={o_mr:.3e} mean_rel={o_mean:.3e} | \
12233                  state: max_abs={s_ma:.3e} max_rel={s_mr:.3e} mean_rel={s_mean:.3e}",
12234                 Self::gdn_chunk_size());
12235        Ok(())
12236    }
12237
12238    /// softplus-based g_log: g_log[h,t] = a[h] * softplus(alpha[h,t] + dt_bias[h]). a pre-negated.
12239    pub fn gdn_glog(&self, alpha: &CudaSlice<f32>, dt_bias: &CudaSlice<f32>, a: &CudaSlice<f32>,
12240                    g_log: &mut CudaSlice<f32>, n_head: usize, t: usize)
12241                    -> Result<(), Box<dyn std::error::Error>> {
12242        let f = self.func("gdn_glog_f32");
12243        let cfg = LaunchConfig::for_num_elems((n_head * t) as u32);
12244        let (h, ti) = (n_head as i32, t as i32);
12245        let __s_b = self.gpu.stream();
12246        let mut b = __s_b.launch_builder(&f);
12247        b.arg(alpha).arg(dt_bias).arg(a).arg(g_log).arg(&h).arg(&ti);
12248        unsafe { b.launch(cfg)?; }
12249        Ok(())
12250    }
12251
12252    /// view twins (task #16): the batched prime's GDN core reads the CONCAT projection
12253    /// buffers at row offsets (CudaView) — same kernels, same values, no split copies.
12254    pub fn sigmoid_v(&self, x: &cudarc::driver::CudaView<f32>, y: &mut CudaSlice<f32>, n: usize)
12255                     -> Result<(), Box<dyn std::error::Error>> {
12256        let f = self.func("sigmoid_f32");
12257        let cfg = LaunchConfig::for_num_elems(n as u32);
12258        let ni = n as i32;
12259        let __s_b = self.gpu.stream();
12260        let mut b = __s_b.launch_builder(&f);
12261        b.arg(x).arg(y).arg(&ni);
12262        unsafe { b.launch(cfg)?; }
12263        Ok(())
12264    }
12265
12266    pub fn gdn_glog_v(&self, alpha: &cudarc::driver::CudaView<f32>, dt_bias: &CudaSlice<f32>,
12267                      a: &CudaSlice<f32>, g_log: &mut CudaSlice<f32>, n_head: usize, t: usize)
12268                      -> Result<(), Box<dyn std::error::Error>> {
12269        let f = self.func("gdn_glog_f32");
12270        let cfg = LaunchConfig::for_num_elems((n_head * t) as u32);
12271        let (h, ti) = (n_head as i32, t as i32);
12272        let __s_b = self.gpu.stream();
12273        let mut b = __s_b.launch_builder(&f);
12274        b.arg(alpha).arg(dt_bias).arg(a).arg(g_log).arg(&h).arg(&ti);
12275        unsafe { b.launch(cfg)?; }
12276        Ok(())
12277    }
12278
12279    pub fn sigmoid(&self, x: &CudaSlice<f32>, y: &mut CudaSlice<f32>, n: usize)
12280                   -> Result<(), Box<dyn std::error::Error>> {
12281        let f = self.func("sigmoid_f32");
12282        let cfg = LaunchConfig::for_num_elems(n as u32);
12283        let ni = n as i32;
12284        let __s_b = self.gpu.stream();
12285        let mut b = __s_b.launch_builder(&f);
12286        b.arg(x).arg(y).arg(&ni);
12287        unsafe { b.launch(cfg)?; }
12288        Ok(())
12289    }
12290
12291    /// attn out-gate fused epilogue (task #17): dst = a * sigmoid(g) + fp16 twin, one launch
12292    /// (replaces sigmoid + mul + convert). Bit-identical class.
12293    pub fn sig_mul_f16out(&self, a: &CudaSlice<f32>, g: &CudaSlice<f32>,
12294                          dst: &mut CudaSlice<f32>, dst16: &mut CudaSlice<u8>, n: usize)
12295                          -> Result<(), Box<dyn std::error::Error>> {
12296        let f = self.func("sig_mul_f16out_f32");
12297        let cfg = LaunchConfig::for_num_elems(n as u32);
12298        let ni = n as i32;
12299        let __s_b = self.gpu.stream();
12300        let mut b = __s_b.launch_builder(&f);
12301        b.arg(a).arg(g).arg(dst).arg(dst16).arg(&ni);
12302        unsafe { b.launch(cfg)?; }
12303        Ok(())
12304    }
12305
12306    /// step35 (Step-3.7-Flash) SEPARATE head-wise attention gate: one scalar per query head,
12307    /// broadcast over head_dim. `dst = a * sigmoid(g)` where `a`/`dst` are `[head_dim, n_head, T]`
12308    /// (the `q_gate_split` layout) and `g` is the PRE-sigmoid `attn_gate` projection output in
12309    /// token-major `[T, n_head]`. `dst16` is the optional fp16 operand for wo (None -> skipped).
12310    ///
12311    /// NOT interchangeable with `sig_mul_f16out`, which gates FULL WIDTH (qwen35 packs one gate
12312    /// value per (head, dim) element inside wq). Using this for that, or that for this, silently
12313    /// applies the wrong number of distinct gate values.
12314    #[allow(clippy::too_many_arguments)]
12315    pub fn attn_head_gate(&self, a: &CudaSlice<f32>, g: &CudaSlice<f32>,
12316                          dst: &mut CudaSlice<f32>, dst16: Option<&mut CudaSlice<u8>>,
12317                          head_dim: usize, n_head: usize, t: usize)
12318                          -> Result<(), Box<dyn std::error::Error>> {
12319        let f = self.func("attn_head_gate_f32");
12320        let cfg = LaunchConfig::for_num_elems((head_dim * n_head * t) as u32);
12321        let (hd, nh, ti) = (head_dim as i32, n_head as i32, t as i32);
12322        // nullable device pointer by value (0 = skip), same convention as `l2_norm_pp`.
12323        let d16: u64 = match dst16 { Some(d) => self.addr_u8(d), None => 0 };
12324        let __s_b = self.gpu.stream();
12325        let mut b = __s_b.launch_builder(&f);
12326        b.arg(a).arg(g).arg(dst).arg(&d16).arg(&hd).arg(&nh).arg(&ti);
12327        unsafe { b.launch(cfg)?; }
12328        Ok(())
12329    }
12330
12331    /// step35 CLAMPED SwiGLU: `dst = min(silu(gate*gs), limit) * clamp(up*us, +-limit)`.
12332    /// Verbatim from llama.cpp `llama-graph.cpp:2146-2165` (routed, `swiglu_clamp_exp`) and
12333    /// `:1751-1770` (shared, `swiglu_clamp_shexp`), non-DEEPSEEK4 branch.
12334    ///
12335    /// This is NOT `swigluoai_mul_scaled`: that one clamps the gate BEFORE swish and multiplies by
12336    /// `(1 + clamp(up))`. Caller MUST check `limit > 1e-6` (upstream's eps gate) and use the plain
12337    /// `silu_mul_scaled` path otherwise — at limit=0 this kernel would clamp every positive
12338    /// activation to zero. On Step-3.7-Flash only layers 43 (7.0) and 44 (16.0) have a live limit.
12339    #[allow(clippy::too_many_arguments)]
12340    pub fn swiglu_clamped_mul_scaled(&self, gate: &CudaSlice<f32>, up: &CudaSlice<f32>,
12341                                     gs: f32, us: f32, limit: f32,
12342                                     dst: &mut CudaSlice<f32>, n: usize)
12343                                     -> Result<(), Box<dyn std::error::Error>> {
12344        debug_assert!(limit > 1e-6, "swiglu_clamped needs a live limit; use silu_mul_scaled");
12345        let f = self.func("swiglu_clamped_mul_scaled_f32");
12346        let cfg = LaunchConfig::for_num_elems(n as u32);
12347        let ni = n as i32;
12348        let __s_b = self.gpu.stream();
12349        let mut b = __s_b.launch_builder(&f);
12350        b.arg(gate).arg(up).arg(&gs).arg(&us).arg(&limit).arg(dst).arg(&ni);
12351        unsafe { b.launch(cfg)?; }
12352        Ok(())
12353    }
12354
12355    /// gated RMSNorm: dst = RMSNorm(o, w[ncols]) * silu(z), per row of ncols. nrows blocks.
12356    pub fn gated_rmsnorm(&self, o: &CudaSlice<f32>, w: &CudaSlice<f32>, z: &CudaSlice<f32>,
12357                         dst: &mut CudaSlice<f32>, ncols: usize, nrows: usize, eps: f32)
12358                         -> Result<(), Box<dyn std::error::Error>> {
12359        let f = self.func("gated_rmsnorm_f32");
12360        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
12361        let (nc, e) = (ncols as i32, eps);
12362        let __s_b = self.gpu.stream();
12363        let mut b = __s_b.launch_builder(&f);
12364        b.arg(o).arg(w).arg(z).arg(dst).arg(&nc).arg(&e);
12365        unsafe { b.launch(cfg)?; }
12366        Ok(())
12367    }
12368
12369    /// f16out twin of `gated_rmsnorm` (task #17): epilogue also emits the fp16 operand for
12370    /// the ssm_out GEMM. Bit-identical class (same floats + the cvt kernel's __float2half).
12371    pub fn gated_rmsnorm_f16out(&self, o: &CudaSlice<f32>, w: &CudaSlice<f32>, z: &CudaSlice<f32>,
12372                                dst: &mut CudaSlice<f32>, dst16: &mut CudaSlice<u8>,
12373                                ncols: usize, nrows: usize, eps: f32)
12374                                -> Result<(), Box<dyn std::error::Error>> {
12375        let f = self.func("gated_rmsnorm_f16out_f32");
12376        // block_dim MUST match gated_rmsnorm's (128): the reduction tree order pins the scale
12377        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
12378        let (nc, e) = (ncols as i32, eps);
12379        let __s_b = self.gpu.stream();
12380        let mut b = __s_b.launch_builder(&f);
12381        b.arg(o).arg(w).arg(z).arg(dst).arg(dst16).arg(&nc).arg(&e);
12382        unsafe { b.launch(cfg)?; }
12383        Ok(())
12384    }
12385
12386    /// add+RMSNorm emitting the f32 normed row AND its q8_1 quantization in one launch (the MoE
12387    /// layer input: z feeds the router matmul as f32, the expert dp4a as q8_1). BIT-IDENTICAL to
12388    /// add_rms_norm + quantize_q8_1. Returns (q, d) alongside the caller-provided res/z buffers.
12389    #[allow(clippy::too_many_arguments)]
12390    pub fn add_rms_norm_zq8(&self, a: &CudaSlice<f32>, b_in: &CudaSlice<f32>, w: &CudaSlice<f32>,
12391                            res: &mut CudaSlice<f32>, z: &mut CudaSlice<f32>,
12392                            ncols: usize, nrows: usize, eps: f32)
12393                            -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
12394        assert!(ncols % 32 == 0);
12395        let mut q = self.alloc_uninit::<i8>(nrows * ncols)?;
12396        let mut d = self.alloc_uninit::<f32>(nrows * (ncols / 32))?;
12397        let f = self.func("add_rms_norm_zq8");
12398        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (1024, 1, 1), shared_mem_bytes: 0 };
12399        let (nc, ep) = (ncols as i32, eps);
12400        let __s_b = self.gpu.stream();
12401        let mut b = __s_b.launch_builder(&f);
12402        b.arg(a).arg(b_in).arg(w).arg(res).arg(z).arg(&mut q).arg(&mut d).arg(&nc).arg(&ep);
12403        unsafe { b.launch(cfg)?; }
12404        Ok((q, d))
12405    }
12406
12407    /// gated RMSNorm emitting q8_1 directly (fused quantize epilogue) — the ssm_out matvec input.
12408    /// BIT-IDENTICAL bytes to gated_rmsnorm + quantize_q8_1 (ncols % 32 == 0; blocks never straddle
12409    /// rows). Saves one launch per linear-attn layer (36/token on the 9B).
12410    /// z-view twins of gated_rmsnorm(+f16out) — task #16 batched-prime split removal.
12411    pub fn gated_rmsnorm_zv(&self, o: &CudaSlice<f32>, w: &CudaSlice<f32>,
12412                            z: &cudarc::driver::CudaView<f32>,
12413                            dst: &mut CudaSlice<f32>, ncols: usize, nrows: usize, eps: f32)
12414                            -> Result<(), Box<dyn std::error::Error>> {
12415        let f = self.func("gated_rmsnorm_f32");
12416        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
12417        let (nc, e) = (ncols as i32, eps);
12418        let __s_b = self.gpu.stream();
12419        let mut b = __s_b.launch_builder(&f);
12420        b.arg(o).arg(w).arg(z).arg(dst).arg(&nc).arg(&e);
12421        unsafe { b.launch(cfg)?; }
12422        Ok(())
12423    }
12424
12425    pub fn gated_rmsnorm_f16out_zv(&self, o: &CudaSlice<f32>, w: &CudaSlice<f32>,
12426                                   z: &cudarc::driver::CudaView<f32>,
12427                                   dst: &mut CudaSlice<f32>, dst16: &mut CudaSlice<u8>,
12428                                   ncols: usize, nrows: usize, eps: f32)
12429                                   -> Result<(), Box<dyn std::error::Error>> {
12430        let f = self.func("gated_rmsnorm_f16out_f32");
12431        // block_dim MUST match gated_rmsnorm's (128): the reduction tree order pins the scale
12432        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
12433        let (nc, e) = (ncols as i32, eps);
12434        let __s_b = self.gpu.stream();
12435        let mut b = __s_b.launch_builder(&f);
12436        b.arg(o).arg(w).arg(z).arg(dst).arg(dst16).arg(&nc).arg(&e);
12437        unsafe { b.launch(cfg)?; }
12438        Ok(())
12439    }
12440
12441    pub fn gated_rmsnorm_q8_1(&self, o: &CudaSlice<f32>, w: &CudaSlice<f32>, z: &CudaSlice<f32>,
12442                              ncols: usize, nrows: usize, eps: f32)
12443                              -> Result<(CudaSlice<i8>, CudaSlice<f32>), Box<dyn std::error::Error>> {
12444        assert!(ncols % 32 == 0);
12445        let f = self.func("gated_rmsnorm_q8_1");
12446        let mut out_q = self.alloc_uninit::<i8>(nrows * ncols)?;
12447        let mut out_d = self.alloc_uninit::<f32>(nrows * (ncols / 32))?;
12448        let cfg = LaunchConfig { grid_dim: (nrows as u32, 1, 1), block_dim: (128, 1, 1), shared_mem_bytes: 0 };
12449        let (nc, ep) = (ncols as i32, eps);
12450        let __s_b = self.gpu.stream();
12451        let mut b = __s_b.launch_builder(&f);
12452        b.arg(o).arg(w).arg(z).arg(&mut out_q).arg(&mut out_d).arg(&nc).arg(&ep);
12453        unsafe { b.launch(cfg)?; }
12454        Ok((out_q, out_d))
12455    }
12456
12457    /// transpose [rows,cols] row-major -> [cols,rows] row-major.
12458    pub fn transpose(&self, inp: &CudaSlice<f32>, rows: usize, cols: usize)
12459                     -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
12460        let f = self.func("transpose_f32");
12461        let mut out = self.zeros(rows * cols)?;
12462        let cfg = LaunchConfig::for_num_elems((rows * cols) as u32);
12463        let (r, c) = (rows as i32, cols as i32);
12464        let __s_b = self.gpu.stream();
12465        let mut b = __s_b.launch_builder(&f);
12466        b.arg(inp).arg(&mut out).arg(&r).arg(&c);
12467        unsafe { b.launch(cfg)?; }
12468        Ok(out)
12469    }
12470
12471    /// repeat-interleave heads: in[head_dim,n_in,T] -> out[head_dim,n_out,T].
12472    pub fn repeat_heads(&self, inp: &CudaSlice<f32>, out: &mut CudaSlice<f32>,
12473                        head_dim: usize, n_in: usize, n_out: usize, t: usize)
12474                        -> Result<(), Box<dyn std::error::Error>> {
12475        let f = self.func("repeat_heads_f32");
12476        let cfg = LaunchConfig::for_num_elems((head_dim * n_out * t) as u32);
12477        let (hd, ni, no, ti) = (head_dim as i32, n_in as i32, n_out as i32, t as i32);
12478        let __s_b = self.gpu.stream();
12479        let mut b = __s_b.launch_builder(&f);
12480        b.arg(inp).arg(out).arg(&hd).arg(&ni).arg(&no).arg(&ti);
12481        unsafe { b.launch(cfg)?; }
12482        Ok(())
12483    }
12484
12485    /// q|gate split (on-device). qf:[T, n_head*2*head_dim] -> q_out,gate_out:[head_dim,n_head,T].
12486    /// Replaces the dtoh->host-double-loop->htod in full_attn / full_attn_decode.
12487    pub fn q_gate_split(&self, qf: &CudaSlice<f32>, q_out: &mut CudaSlice<f32>,
12488                        gate_out: &mut CudaSlice<f32>, head_dim: usize, n_head: usize, t: usize)
12489                        -> Result<(), Box<dyn std::error::Error>> {
12490        let f = self.func("q_gate_split_f32");
12491        let cfg = LaunchConfig::for_num_elems((head_dim * n_head * t) as u32);
12492        let (hd, nh, ti) = (head_dim as i32, n_head as i32, t as i32);
12493        let __s_b = self.gpu.stream();
12494        let mut b = __s_b.launch_builder(&f);
12495        b.arg(qf).arg(q_out).arg(gate_out).arg(&hd).arg(&nh).arg(&ti);
12496        unsafe { b.launch(cfg)?; }
12497        Ok(())
12498    }
12499
12500    /// qkv->GDN repack (on-device). conv_out:[conv_dim,T] channel-major ->
12501    /// q_g/k_g/v_g:[d_state,num_v,T] with q/k head-repeat kh = vh % num_k (validated modulo mapping).
12502    /// Replaces the dtoh->host-q/k/v-repack->3x-htod in linear_attn / linear_attn_decode.
12503    pub fn qkv_to_gdn_repack(&self, conv_out: &CudaSlice<f32>, q_g: &mut CudaSlice<f32>,
12504                             k_g: &mut CudaSlice<f32>, v_g: &mut CudaSlice<f32>,
12505                             d_state: usize, num_v: usize, num_k: usize, key_dim: usize, t: usize)
12506                             -> Result<(), Box<dyn std::error::Error>> {
12507        let f = self.func("qkv_to_gdn_repack_f32");
12508        let cfg = LaunchConfig::for_num_elems((d_state * num_v * t) as u32);
12509        let (ds, nv, nk, kd, ti) = (d_state as i32, num_v as i32, num_k as i32, key_dim as i32, t as i32);
12510        let __s_b = self.gpu.stream();
12511        let mut b = __s_b.launch_builder(&f);
12512        b.arg(conv_out).arg(q_g).arg(k_g).arg(v_g).arg(&ds).arg(&nv).arg(&nk).arg(&kd).arg(&ti);
12513        unsafe { b.launch(cfg)?; }
12514        Ok(())
12515    }
12516
12517    /// conv left zero-pad (prefill from zero state). src:[conv_dim,T] -> dst:[conv_dim,T+pad],
12518    /// cols 0..pad = 0, cols pad..pad+T = src. `dst` MUST be pre-zeroed. No dtoh/host-loop/htod.
12519    pub fn conv_left_pad(&self, src: &CudaSlice<f32>, dst: &mut CudaSlice<f32>,
12520                         conv_dim: usize, t: usize, pad: usize)
12521                         -> Result<(), Box<dyn std::error::Error>> {
12522        let f = self.func("conv_left_pad_f32");
12523        let cfg = LaunchConfig::for_num_elems((conv_dim * t) as u32);
12524        let (cd, ti, p) = (conv_dim as i32, t as i32, pad as i32);
12525        let __s_b = self.gpu.stream();
12526        let mut b = __s_b.launch_builder(&f);
12527        b.arg(src).arg(dst).arg(&cd).arg(&ti).arg(&p);
12528        unsafe { b.launch(cfg)?; }
12529        Ok(())
12530    }
12531
12532    /// conv-state assemble + ring roll (decode T=1). conv_state:[conv_dim,pad] (resident),
12533    /// qkv_col:[conv_dim] -> conv_in:[conv_dim,pad+1]; AND rolls conv_state (keep last pad cols).
12534    /// Replaces the dtoh->host-conv-ring-assemble->ring-update->htod in linear_attn_decode.
12535    pub fn conv_assemble_and_roll(&self, qkv_col: &CudaSlice<f32>, conv_state: &mut CudaSlice<f32>,
12536                                  conv_in: &mut CudaSlice<f32>, conv_dim: usize, pad: usize)
12537                                  -> Result<(), Box<dyn std::error::Error>> {
12538        let f = self.func("conv_assemble_and_roll_f32");
12539        let cfg = LaunchConfig::for_num_elems(conv_dim as u32);
12540        let (cd, p) = (conv_dim as i32, pad as i32);
12541        let __s_b = self.gpu.stream();
12542        let mut b = __s_b.launch_builder(&f);
12543        b.arg(qkv_col).arg(conv_state).arg(conv_in).arg(&cd).arg(&p);
12544        unsafe { b.launch(cfg)?; }
12545        Ok(())
12546    }
12547
12548    /// RANK3 LEVER (conv fuse, T=1 DECODE): fused conv_assemble_and_roll + ssm_conv1d_silu in ONE
12549    /// launch. Assembles the conv window [conv_state | qkv_col] in registers, computes the depthwise
12550    /// causal conv + SiLU into `conv_out`, and rolls the ring — never materializing conv_in to HBM.
12551    /// Replaces e.conv_assemble_and_roll(...) + e.ssm_conv1d(...). BIT-IDENTICAL to that two-kernel
12552    /// sequence (same 8-wide accumulation order, same SiLU). `conv_out` is [conv_dim] (T=1).
12553    pub fn ssm_conv1d_fused_decode(&self, qkv_col: &CudaSlice<f32>, conv_state: &mut CudaSlice<f32>,
12554                                   w: &CudaSlice<f32>, conv_out: &mut CudaSlice<f32>,
12555                                   conv_dim: usize, d_conv: usize)
12556                                   -> Result<(), Box<dyn std::error::Error>> {
12557        let f = self.func("ssm_conv1d_fused_decode_f32");
12558        let cfg = LaunchConfig::for_num_elems(conv_dim as u32);
12559        let (cd, dc) = (conv_dim as i32, d_conv as i32);
12560        let __s_b = self.gpu.stream();
12561        let mut b = __s_b.launch_builder(&f);
12562        b.arg(qkv_col).arg(conv_state).arg(w).arg(conv_out).arg(&cd).arg(&dc);
12563        unsafe { b.launch(cfg)?; }
12564        Ok(())
12565    }
12566
12567    /// Copy a contiguous range [start, start+len) out of src into a fresh slice (device→device via host).
12568    /// Used for qkv split views. Small/rare; not perf-critical in Stage 1.
12569    pub fn slice_range(&self, src: &CudaSlice<f32>, start: usize, len: usize)
12570                       -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
12571        let host = self.gpu.stream().clone_dtoh(src)?;
12572        self.gpu.stream().synchronize()?;
12573        Ok(self.htod(&host[start..start + len])?)
12574    }
12575}
12576
12577#[cfg(test)]
12578mod target_dispatch_tests {
12579    use super::legacy_quant_gemm_allowed;
12580
12581    #[test]
12582    fn legacy_quant_gemm_arch_policy_honors_the_escape_hatch() {
12583        // sm_120a native lane
12584        assert!(legacy_quant_gemm_allowed(false, false, false));
12585        assert!(!legacy_quant_gemm_allowed(false, false, true));
12586        // pure portable lane (sm_89): gated
12587        assert!(!legacy_quant_gemm_allowed(true, false, false));
12588        assert!(!legacy_quant_gemm_allowed(true, false, true));
12589        // Hopper-MMA lane (sm_90a): portable build, int8-MMA GEMM re-admitted
12590        assert!(legacy_quant_gemm_allowed(true, true, false));
12591        assert!(!legacy_quant_gemm_allowed(true, true, true));
12592    }
12593
12594    #[cfg(all(memra_portable_cuda, not(memra_hopper_mma)))]
12595    #[test]
12596    fn portable_build_disables_legacy_quant_gemm_without_an_env_override() {
12597        assert!(!legacy_quant_gemm_allowed(cfg!(memra_portable_cuda), cfg!(memra_hopper_mma), false));
12598    }
12599
12600    #[cfg(memra_hopper_mma)]
12601    #[test]
12602    fn hopper_mma_build_re_admits_legacy_quant_gemm() {
12603        assert!(legacy_quant_gemm_allowed(cfg!(memra_portable_cuda), cfg!(memra_hopper_mma), false));
12604        assert!(super::portable_mma_gated() == false);
12605    }
12606}
12607
12608/// The memra-kv device seam (Phase D): the cache's 7 ops delegate to the engine's
12609/// inherent methods (inherent methods win name resolution, so no recursion).
12610impl memra_kv::KvDev for Engine {
12611    fn zeros(&self, n: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
12612        Engine::zeros(self, n)
12613    }
12614    fn uninit(&self, n: usize) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
12615        Engine::uninit(self, n)
12616    }
12617    fn alloc_u8(&self, n: usize) -> Result<CudaSlice<u8>, Box<dyn std::error::Error>> {
12618        Engine::alloc_u8(self, n)
12619    }
12620    fn htod_i32(&self, v: &[i32]) -> Result<CudaSlice<i32>, Box<dyn std::error::Error>> {
12621        Engine::htod_i32(self, v)
12622    }
12623    fn clone_dtod(&self, src: &CudaSlice<f32>) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
12624        Engine::clone_dtod(self, src)
12625    }
12626    fn copy_into(&self, dst: &mut CudaSlice<f32>, off: usize, src: &CudaSlice<f32>, len: usize)
12627                 -> Result<(), Box<dyn std::error::Error>> {
12628        Engine::copy_into(self, dst, off, src, len)
12629    }
12630    fn set_i32_one(&self, d: &mut CudaSlice<i32>, v: i32) -> Result<(), Box<dyn std::error::Error>> {
12631        Engine::set_i32_one(self, d, v)
12632    }
12633}