Skip to main content

memra_engine/
model.rs

1//! Dense transformer model: loads GGUF weights to GPU (Stage-1: dequant→f32), runs the
2//! shared full-attention + SwiGLU forward graph. Arch-agnostic via ModelConfig; this path is
3//! exactly the dense-transformer graph (qwen3) and the full-attention layers of hybrids.
4
5use crate::{
6    Engine, QT_BF16, QT_F8_E4M3, QT_F32, QT_IQ3_S, QT_IQ4_XS, QT_NVFP4, QT_NVFP4_RP, QT_Q2_K,
7    QT_Q3_K, QT_Q4_0, QT_Q4_K, QT_Q5_K, QT_Q6_K, QT_Q8_0,
8};
9use cudarc::driver::CudaSlice;
10use memra_gguf::config::ModelConfig;
11use memra_gguf::source::{DiskExtent, GgufSource, TensorSource};
12use memra_gguf::{GgmlType, GgufFile, dequant};
13use std::collections::HashMap;
14use std::path::Path;
15
16/// RESIDENCY CENSUS (lane/fp8-decode-v1, 2026-08-05) — per-qtype tally of the 2D matmul weights
17/// that actually went resident, keyed by `QT_*`. The FP8-ST decode arm's whole claim is about
18/// WHICH container the checkpoint's projections end up in, and the two candidate containers
19/// differ in bytes (e4m3 1.0 B/w vs the Q8_0 re-encode 1.0625 B/w). Before this instrument the
20/// only evidence available was end-to-end tok/s, which cannot distinguish "the arm ran and was
21/// flat" from "the arm never engaged" — the exact ambiguity in this lane's first loadprobe pair.
22/// Slot = qtype index; `.0` = tensor count, `.1` = resident bytes.
23static RESIDENCY_CENSUS: [(std::sync::atomic::AtomicUsize, std::sync::atomic::AtomicU64); 16] = {
24    #[allow(clippy::declare_interior_mutable_const)]
25    const Z: (std::sync::atomic::AtomicUsize, std::sync::atomic::AtomicU64) = (
26        std::sync::atomic::AtomicUsize::new(0),
27        std::sync::atomic::AtomicU64::new(0),
28    );
29    [Z; 16]
30};
31
32fn residency_census_note(qtype: i32, bytes: usize) {
33    use std::sync::atomic::Ordering::Relaxed;
34    if let Some(slot) = RESIDENCY_CENSUS.get(qtype as usize) {
35        slot.0.fetch_add(1, Relaxed);
36        slot.1.fetch_add(bytes as u64, Relaxed);
37    }
38}
39
40/// Human-readable residency census: one line per qtype that took at least one 2D weight, plus a
41/// total. Callers print it right after load — see `run-gen`'s `MEMRA_RESIDENCY_CENSUS=1`.
42pub fn residency_census_report() -> String {
43    use std::sync::atomic::Ordering::Relaxed;
44    let name = |q: usize| -> &'static str {
45        match q as i32 {
46            QT_Q8_0 => "Q8_0",
47            QT_Q4_K => "Q4_K",
48            QT_Q6_K => "Q6_K",
49            QT_Q5_K => "Q5_K",
50            QT_Q3_K => "Q3_K",
51            QT_IQ4_XS => "IQ4_XS",
52            QT_IQ3_S => "IQ3_S",
53            QT_NVFP4 => "NVFP4",
54            QT_F32 => "F32",
55            QT_NVFP4_RP => "NVFP4_RP",
56            QT_F8_E4M3 => "F8_E4M3",
57            QT_BF16 => "BF16",
58            QT_Q4_0 => "Q4_0",
59            QT_Q2_K => "Q2_K",
60            crate::QT_F8_E4M3_BLK => "F8_E4M3_BLK",
61            _ => "?",
62        }
63    };
64    let mut out = String::from("residency census (2D matmul weights, resident container):\n");
65    let (mut tn, mut tb) = (0usize, 0u64);
66    for (q, slot) in RESIDENCY_CENSUS.iter().enumerate() {
67        let (n, b) = (slot.0.load(Relaxed), slot.1.load(Relaxed));
68        if n == 0 {
69            continue;
70        }
71        tn += n;
72        tb += b;
73        out += &format!(
74            "  {:>9}: {:>4} tensors  {:>9.3} MiB\n",
75            name(q),
76            n,
77            b as f64 / (1024.0 * 1024.0)
78        );
79    }
80    out += &format!(
81        "  {:>9}: {:>4} tensors  {:>9.3} MiB",
82        "TOTAL",
83        tn,
84        tb as f64 / (1024.0 * 1024.0)
85    );
86    out
87}
88
89/// Refuse attacker-controlled filesystem objects in the model-local repack cache.
90///
91/// Repack artifacts are derived data, but they are opened by the serving process and therefore
92/// must not be allowed to follow a model-provided symlink into an arbitrary path. `create_dir_all`
93/// and ordinary `File::create` both follow links; use `symlink_metadata` for the directory and
94/// `O_NOFOLLOW` for the final file component on Unix. The non-Unix fallback still rejects existing
95/// symlinks and keeps the same behavior on platforms without that flag.
96fn ensure_repack_cache_dir(path: &Path) -> std::io::Result<()> {
97    match std::fs::symlink_metadata(path) {
98        Ok(meta) => {
99            if meta.file_type().is_symlink() || !meta.is_dir() {
100                return Err(std::io::Error::new(
101                    std::io::ErrorKind::InvalidData,
102                    format!("repack cache directory is not a real directory: {path:?}"),
103                ));
104            }
105            Ok(())
106        }
107        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
108            match std::fs::create_dir(path) {
109                Ok(()) => Ok(()),
110                Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
111                    ensure_repack_cache_dir(path)
112                }
113                Err(error) => Err(error),
114            }
115        }
116        Err(error) => Err(error),
117    }
118}
119
120fn repack_cache_is_fresh(path: &Path, expected_len: usize) -> bool {
121    std::fs::symlink_metadata(path)
122        .is_ok_and(|meta| meta.file_type().is_file() && meta.len() == expected_len as u64)
123}
124
125#[cfg(unix)]
126fn open_repack_cache_dir(path: &Path) -> std::io::Result<std::fs::File> {
127    use std::os::unix::fs::OpenOptionsExt;
128    let mut options = std::fs::OpenOptions::new();
129    options
130        .read(true)
131        .custom_flags(libc::O_DIRECTORY | libc::O_NOFOLLOW);
132    options.open(path)
133}
134
135#[cfg(not(unix))]
136fn open_repack_cache_dir(path: &Path) -> std::io::Result<std::fs::File> {
137    std::fs::OpenOptions::new().read(true).open(path)
138}
139
140#[cfg(unix)]
141fn open_repack_cache(path: &Path, write: bool) -> std::io::Result<std::fs::File> {
142    use std::ffi::CString;
143    use std::os::unix::ffi::OsStrExt;
144    use std::os::unix::io::{AsRawFd, FromRawFd};
145
146    let parent = path.parent().ok_or_else(|| {
147        std::io::Error::new(
148            std::io::ErrorKind::InvalidInput,
149            "repack cache has no parent",
150        )
151    })?;
152    let name = path.file_name().ok_or_else(|| {
153        std::io::Error::new(
154            std::io::ErrorKind::InvalidInput,
155            "repack cache has no filename",
156        )
157    })?;
158    let name = CString::new(name.as_bytes()).map_err(|_| {
159        std::io::Error::new(
160            std::io::ErrorKind::InvalidInput,
161            "repack cache filename has NUL",
162        )
163    })?;
164    let dir = open_repack_cache_dir(parent)?;
165    let flags = if write {
166        libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL | libc::O_NOFOLLOW
167    } else {
168        libc::O_RDONLY | libc::O_NOFOLLOW
169    };
170    let fd = unsafe { libc::openat(dir.as_raw_fd(), name.as_ptr(), flags, 0o600) };
171    if fd < 0 {
172        return Err(std::io::Error::last_os_error());
173    }
174    // SAFETY: openat returned a fresh, owned descriptor.
175    let file = unsafe { std::fs::File::from_raw_fd(fd) };
176    let metadata = file.metadata()?;
177    if !metadata.is_file() {
178        return Err(std::io::Error::new(
179            std::io::ErrorKind::InvalidData,
180            format!("repack cache is not a regular file: {path:?}"),
181        ));
182    }
183    if std::os::unix::fs::MetadataExt::nlink(&metadata) > 1 {
184        return Err(std::io::Error::new(
185            std::io::ErrorKind::InvalidData,
186            format!("repack cache refuses a multiply-linked file: {path:?}"),
187        ));
188    }
189    Ok(file)
190}
191
192#[cfg(not(unix))]
193fn open_repack_cache(path: &Path, write: bool) -> std::io::Result<std::fs::File> {
194    let metadata = std::fs::symlink_metadata(path)?;
195    if metadata.file_type().is_symlink() || !metadata.is_file() {
196        return Err(std::io::Error::new(
197            std::io::ErrorKind::InvalidData,
198            format!("repack cache is not a regular file: {path:?}"),
199        ));
200    }
201    let mut options = std::fs::OpenOptions::new();
202    options.read(!write).write(write);
203    options.open(path)
204}
205
206/// Write one repack artifact through a descriptor for its real parent directory. The payload is
207/// first written to an O_EXCL temporary sibling, fsynced, and atomically renamed into place; a
208/// pre-existing symlink, non-regular file, or hard link is rejected before the rename. Thus a
209/// malformed model cannot truncate a service-owned inode, and a crash cannot leave a fresh-sized
210/// partial cache that a later load would mistake for valid data.
211fn write_repack_cache<F>(path: &Path, write: F) -> std::io::Result<()>
212where
213    F: FnOnce(&mut std::io::BufWriter<std::fs::File>) -> std::io::Result<()>,
214{
215    use std::io::Write;
216
217    let parent = path.parent().ok_or_else(|| {
218        std::io::Error::new(
219            std::io::ErrorKind::InvalidInput,
220            "repack cache has no parent",
221        )
222    })?;
223    let name = path.file_name().ok_or_else(|| {
224        std::io::Error::new(
225            std::io::ErrorKind::InvalidInput,
226            "repack cache has no filename",
227        )
228    })?;
229    let dir = open_repack_cache_dir(parent)?;
230
231    #[cfg(unix)]
232    {
233        use std::ffi::CString;
234        use std::os::unix::ffi::OsStrExt;
235        use std::os::unix::io::{AsRawFd, FromRawFd};
236        static TEMP_COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
237        let name = CString::new(name.as_bytes()).map_err(|_| {
238            std::io::Error::new(
239                std::io::ErrorKind::InvalidInput,
240                "repack cache filename has NUL",
241            )
242        })?;
243        let mut temp_name = None;
244        let mut temp_file = None;
245        for _ in 0..32 {
246            let suffix = TEMP_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
247            let candidate = CString::new(format!(
248                ".{}.tmp-{}-{suffix}",
249                name.to_string_lossy(),
250                std::process::id()
251            ))
252            .map_err(|_| {
253                std::io::Error::new(
254                    std::io::ErrorKind::InvalidInput,
255                    "temporary filename has NUL",
256                )
257            })?;
258            let fd = unsafe {
259                libc::openat(
260                    dir.as_raw_fd(),
261                    candidate.as_ptr(),
262                    libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL | libc::O_NOFOLLOW,
263                    0o600,
264                )
265            };
266            if fd >= 0 {
267                temp_name = Some(candidate);
268                // SAFETY: openat returned a fresh, owned descriptor.
269                temp_file = Some(unsafe { std::fs::File::from_raw_fd(fd) });
270                break;
271            }
272            let error = std::io::Error::last_os_error();
273            if error.kind() != std::io::ErrorKind::AlreadyExists {
274                return Err(error);
275            }
276        }
277        let temp_name = temp_name.ok_or_else(|| {
278            std::io::Error::new(
279                std::io::ErrorKind::AlreadyExists,
280                "could not allocate a unique repack cache temporary",
281            )
282        })?;
283        let mut out = std::io::BufWriter::new(temp_file.expect("temporary file accompanies name"));
284        let result = write(&mut out).and_then(|()| {
285            out.flush()?;
286            out.get_ref().sync_all()?;
287            Ok(())
288        });
289        drop(out);
290        if let Err(error) = result {
291            unsafe {
292                libc::unlinkat(dir.as_raw_fd(), temp_name.as_ptr(), 0);
293            }
294            return Err(error);
295        }
296
297        // Never replace a caller-provided link or a hard-linked service inode. If a race swaps the
298        // final entry after this check, renameat only replaces that directory entry; it cannot
299        // write through the swapped inode, and the temporary remains private to this directory.
300        if let Ok(metadata) = std::fs::symlink_metadata(path)
301            && (metadata.file_type().is_symlink()
302                || !metadata.is_file()
303                || std::os::unix::fs::MetadataExt::nlink(&metadata) > 1)
304        {
305            unsafe {
306                libc::unlinkat(dir.as_raw_fd(), temp_name.as_ptr(), 0);
307            }
308            return Err(std::io::Error::new(
309                std::io::ErrorKind::InvalidData,
310                format!("repack cache target is not a private regular file: {path:?}"),
311            ));
312        }
313        let status = unsafe {
314            libc::renameat(
315                dir.as_raw_fd(),
316                temp_name.as_ptr(),
317                dir.as_raw_fd(),
318                name.as_ptr(),
319            )
320        };
321        if status != 0 {
322            unsafe {
323                libc::unlinkat(dir.as_raw_fd(), temp_name.as_ptr(), 0);
324            }
325            return Err(std::io::Error::last_os_error());
326        }
327        dir.sync_all()
328    }
329
330    #[cfg(not(unix))]
331    {
332        let temp = parent.join(format!(
333            ".{}.tmp-{}",
334            name.to_string_lossy(),
335            std::process::id()
336        ));
337        let mut out = std::io::BufWriter::new(
338            std::fs::OpenOptions::new()
339                .write(true)
340                .create_new(true)
341                .open(&temp)?,
342        );
343        write(&mut out)?;
344        out.flush()?;
345        out.get_ref().sync_all()?;
346        drop(out);
347        if let Ok(metadata) = std::fs::symlink_metadata(path) {
348            if metadata.file_type().is_symlink() || !metadata.is_file() {
349                std::fs::remove_file(&temp).ok();
350                return Err(std::io::Error::new(
351                    std::io::ErrorKind::InvalidData,
352                    format!("repack cache target is not a private regular file: {path:?}"),
353                ));
354            }
355        }
356        std::fs::rename(temp, path)
357    }
358}
359
360/// A weight tensor resident on GPU. Quantized weights stay in GGUF block bytes (`Quant`);
361/// small non-quant tensors (norms, sometimes embed/lm_head) are kept dequantized as f32 (`Float`).
362/// This keeps VRAM ~= on-disk quant size (fixes the f32-on-load OOM).
363#[allow(clippy::large_enum_variant)] // allow: variant size asymmetry is deliberate; these enums live in per-layer tables, not hot moves
364pub enum GpuTensor {
365    Quant {
366        bytes: CudaSlice<u8>,
367        qtype: i32,
368        row_bytes: usize,
369        ne: Vec<u64>,
370        scale: f32,
371        /// SPLIT-PLANE walk-order repack (A6, 2026-07-04): NVFP4 matmul weights are repacked at
372        /// load into [quant plane out_f x in_f/64 x 32B][scale plane out_f x in_f/64 x 4B] — same
373        /// bytes, same total size, but a lane's per-group weight read becomes ONE 16B-aligned
374        /// LDG.128 + a dense 4B scale word instead of 5 scattered 4B LDGs at 36B stride (the "18B
375        /// straggle"). Every consumer kernel has an `_rp` twin (bit-identical: pure byte
376        /// permutation, same dot order). `rp=false` = original GGUF block layout (all other
377        /// dtypes, MoE-staged expert bytes, MEMRA_RP=0 escape).
378        rp: bool,
379        /// CUTLASS NVFP4 prefill operand (repacked B + swizzled SFB), built ALONGSIDE `bytes` at load
380        /// when MEMRA_FP4_CUTLASS is set. `bytes` stays raw GGUF so decode (MMVQ/dp4a) is untouched;
381        /// prefill (m>=128) reads this. Only ever Some for NVFP4 weights under cfg(memra_cutlass).
382        #[cfg(memra_cutlass)]
383        cutlass: Option<CutlassWeight>,
384        /// FP8-ACT PREFILL operand (MEMRA_PP_FP8=1, probe verdict 2026-07-08): the checkpoint's RAW
385        /// e4m3 bytes + per-tensor f32 weight_scale, stashed ALONGSIDE the Q8_0 re-encode for the
386        /// F8-E4M3-origin 2D projections (~1 B/w extra on those layers). `bytes` stays Q8_0 so
387        /// decode (dp4a/MMVQ) is untouched; only the m>=16 prefill dispatch (cuBLASLt FP8 TN,
388        /// fp8_ffi.rs) reads this. None unless the env is set at load (zero VRAM cost by default).
389        fp8: Option<Fp8Weight>,
390        /// Q4_0 SPLIT-PLANE MIRROR (2026-07-10, the 18B-straggle cure for decode): qs plane
391        /// [out_f x nblk x 16B] + d plane [out_f x nblk x 2B] built device-side at model load
392        /// (q4_0_split_rp_build) for decode-hot trunk weights. Raw `bytes` stay resident —
393        /// prefill (gemm/MMQ) and Stage-A read those; the m<=8 mmvq/batched/fused dispatch
394        /// reads this when present (`_rp` twins; microprobe m=1 1.34x, m=3 1.17x, bitwise).
395        /// None everywhere except where the arch-load hook opted in (VRAM cost = weight size).
396        rp4: Option<CudaSlice<u8>>,
397        /// BLOCK-128 WEIGHT-SCALE GRID for a NATIVE e4m3 resident weight (lane/fp8-blk128-decode,
398        /// 2026-08-05). `Some` iff `qtype == QT_F8_E4M3_BLK`, and then `bytes` are the checkpoint's
399        /// raw e4m3 codes ([out_f, in_f], row_bytes == in_f), `scale == 1.0`, and THIS is the only
400        /// dequant scale in the tensor — decode reads it in-kernel (`qmatvec_e4m3_blk_mmvq`),
401        /// prefill reads it in the per-block MMQ tile. Distinct from `fp8: Some(Fp8Weight { blk })`,
402        /// which is the MEMRA_PP_FP8 *stash*: a SECOND e4m3 copy carried alongside a Q8_0 slab.
403        /// Here there is one copy and `fp8` stays None.
404        blk: Option<Fp8BlockScales>,
405        /// FP16 DEQUANT MIRROR (MEMRA_PP_F16=1, probe 2026-07-26): row-major fp16 of a 2D Q8_0
406        /// projection, built device-side at load (f16_ffi::build_q8_f16). `bytes` stay Q8_0 so
407        /// decode is untouched; the m>=16 prefill dispatch (cuBLASLt FP16 TN, 611-687 TF vs
408        /// MMQ's ~200 TF class) reads this. None unless the env is set (VRAM = 2 B/w extra).
409        f16: Option<CudaSlice<u8>>,
410    },
411    Float {
412        data: CudaSlice<f32>,
413        ne: Vec<u64>,
414    },
415    /// BF16-RESIDENT full-precision matmul weight (MEMRA_FULL_PREC only). Holds the checkpoint's raw
416    /// bf16 bytes (`u8`, little-endian u16 pairs) — 2 B/w vs the 4 B/w a `Float` f32 materialization
417    /// would cost, so the 9B trunk stays ~18GB in VRAM instead of ~36GB. Consumed via dequant-on-use:
418    /// each matmul expands this to a transient f32 scratch and rides the SAME cuBLASLt f32 GEMV the
419    /// `Float` arm uses (bit-identical to a load-time bf16->f32 dequant, just deferred). Never a norm
420    /// (norms stay `Float` f32); never on a fast/GEMM/MMQ path (uses_q8_1_fast/gemm_supports = false).
421    FloatBf16 {
422        data: CudaSlice<u8>,
423        ne: Vec<u64>,
424    },
425}
426
427/// FP8-native prefill operand: raw checkpoint e4m3 codes `[out_f, in_f]` row-major (EXACT — the
428/// weight side of the FP8 GEMM does no re-quantization) + its weight scale(s). Per-tensor class:
429/// `scale` is the dequant scalar folded into the GEMM's scale pointer together with the per-batch
430/// activation scale, `blk == None`. Block-128 class (Qwen official FP8): `blk == Some` and
431/// `scale == 1.0` — see `Fp8BlockScales` for the resident layout contract.
432pub struct Fp8Weight {
433    pub bytes: CudaSlice<u8>,
434    pub scale: f32,
435    pub blk: Option<Fp8BlockScales>,
436}
437
438/// Device-resident block-128 weight-scale grid for an e4m3 operand (B1b, lane fp8st 2026-08-03).
439///
440/// STORAGE LAYOUT (the canonical device layout every future consumer builds from): a flat f32
441/// buffer in the CHECKPOINT'S on-disk order — row-major `[rows = ceil(out_f/128),
442/// cols = ceil(in_f/128)]`, so `scales[ob * cols + kb]` scales the 128x128 weight tile at
443/// output-block `ob`, input-block `kb` (uploaded verbatim from `memra_gguf::source::F8BlockGrid`,
444/// no permutation — one host decode, one htod). Rationale: (1) the per-block-dequant mmvq twin
445/// (qmatvec_e4m3_mmvq extension, DECISION.md B1) indexes `(o >> 7) * cols + (e >> 7)` — natural
446/// in this order; (2) for cuBLASLt BLK128x128 the weight `[out, in]` row-major is the TN GEMM's
447/// column-major `[k=in, n=out]` A operand, and this same linear order IS that view's column-major
448/// block grid with ld = cols(=kblk) — probe P1 (`probe/fp8_lt_blk_probe.cu`) verifies whether
449/// sm_120 accepts it directly; if Lt wants a different order, the reorder happens at the GEMM
450/// plan build, NOT here. NO KERNEL CONSUMES THIS YET: the loader keeps every block-128 tensor's
451/// decode/prefill on the Q8_0 re-encode until the consuming kernels land (try_fp8_gemm skips
452/// blk operands; the QT_F8_E4M3 one-copy arm rejects them). This struct's job is bytes+scales
453/// resident and correct.
454pub struct Fp8BlockScales {
455    pub scales: CudaSlice<f32>,
456    pub rows: usize, // ceil(out_f/128)
457    pub cols: usize, // ceil(in_f/128)
458}
459
460/// Host-side split-plane repack of NVFP4 GGUF block bytes (A6). Input: out_f rows of in_f/64
461/// 36-byte blocks ([4B UE4M3 scales][32B packed e2m1]). Output (same length): quant plane
462/// (out_f x nsb64 x 32B) followed by scale plane (out_f x nsb64 x 4B). Pure byte permutation.
463pub fn repack_nvfp4_split(bytes: &[u8], out_f: usize) -> Vec<u8> {
464    let row_bytes = bytes.len() / out_f;
465    let nsb64 = row_bytes / 36;
466    debug_assert_eq!(
467        row_bytes % 36,
468        0,
469        "NVFP4 row_bytes must be a multiple of 36"
470    );
471    let qplane = out_f * nsb64 * 32;
472    let mut rp = vec![0u8; bytes.len()];
473    for o in 0..out_f {
474        for s in 0..nsb64 {
475            let src = &bytes[o * row_bytes + s * 36..o * row_bytes + s * 36 + 36];
476            rp[qplane + (o * nsb64 + s) * 4..qplane + (o * nsb64 + s) * 4 + 4]
477                .copy_from_slice(&src[0..4]);
478            rp[(o * nsb64 + s) * 32..(o * nsb64 + s) * 32 + 32].copy_from_slice(&src[4..36]);
479        }
480    }
481    rp
482}
483
484/// Inverse of `repack_nvfp4_split` (the roundtrip gate).
485pub fn unpack_nvfp4_split(rp: &[u8], out_f: usize) -> Vec<u8> {
486    let row_bytes = rp.len() / out_f;
487    let nsb64 = row_bytes / 36;
488    let qplane = out_f * nsb64 * 32;
489    let mut back = vec![0u8; rp.len()];
490    for o in 0..out_f {
491        for s in 0..nsb64 {
492            back[o * row_bytes + s * 36..o * row_bytes + s * 36 + 4].copy_from_slice(
493                &rp[qplane + (o * nsb64 + s) * 4..qplane + (o * nsb64 + s) * 4 + 4],
494            );
495            back[o * row_bytes + s * 36 + 4..o * row_bytes + s * 36 + 36]
496                .copy_from_slice(&rp[(o * nsb64 + s) * 32..(o * nsb64 + s) * 32 + 32]);
497        }
498    }
499    back
500}
501
502/// A6 repack seam: default ON, `MEMRA_RP=0` restores the GGUF block layout everywhere (rollback/A-B).
503pub fn rp_enabled() -> bool {
504    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
505    *ON.get_or_init(|| std::env::var("MEMRA_RP").map(|v| v != "0").unwrap_or(true))
506}
507
508/// FULL-PRECISION LOADER MODE (MEMRA_FULL_PREC=1, default OFF — MTP-heal research platform).
509/// Bypasses the standing loader law (large BF16/F8 -> Q8_0/NVFP4 re-encode, the "Float-poison"
510/// tripwire). Under this flag every weight loads as Float and compute rides the Stage-A f32 oracle
511/// path end to end — SLOW IS FINE, this mode exists for exactness (the MTP acceptance CEILING at
512/// full precision), not speed. Large 2D matmul weights stay bf16-resident (`GpuTensor::FloatBf16`)
513/// with dequant-on-use so the 9B (~18GB bf16) + f32 activations fit 24GB instead of blowing to
514/// ~38GB as an all-f32 materialization. The Float-poison tripwire warnings are CORRECT behavior
515/// here and are suppressed. See docs/FLAGS.md and HANDOVER "MEMRA DUAL-SHAPE".
516pub fn full_prec_enabled() -> bool {
517    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
518    *ON.get_or_init(|| {
519        std::env::var("MEMRA_FULL_PREC")
520            .map(|v| v == "1")
521            .unwrap_or(false)
522    })
523}
524
525/// LOADER-LAW allowlist (loadersweep audit 2026-07-08): 2D Float tensors that are DELIBERATELY
526/// Float despite being matmul-class. Every entry needs an audit rationale — this list silences
527/// the tripwire below, so an unjustified entry re-opens the trap.
528///   * ffn_gate_inp (MoE router, 35B GGUF F32 [2048,256] / M3 ST F32 [6144,64]): the router's
529///     top-k SELECTION is discontinuous — quantizing shifts logits and flips expert choice (a
530///     class change, not an FP-order change). llama.cpp keeps every router F32 (its converter
531///     forces F32) so Float is bench-parity, it sits on NO all-or-nothing predicate, and the
532///     decode-exact contract is already built around its cuBLASLt path
533///     (hybrid_forward.rs moe_ffn_sequential_zq8 router comment).
534fn float_2d_audited(name: &str) -> bool {
535    name.ends_with("ffn_gate_inp.weight")
536        // hc_{attn,ffn}_fn (crate::hyper): DELIBERATELY Float. It is an f32-island operand —
537        // [(2+streams)*streams, streams*hidden], 24 rows on glm5_next — consumed by one
538        // Engine::linear per site whose output feeds the Sinkhorn gates directly. A Q8_0 encode
539        // would quantize the input to a normalization, and the tensor sits on no q8-fast
540        // predicate: the mixers read the COLLAPSED hidden, never this.
541        || name.ends_with("hc_attn_fn")
542        || name.ends_with("hc_ffn_fn")
543}
544
545/// Once-per-name-pattern loader-law warning (`blk.{il}.` collapses to `blk.*.` so a 48-layer
546/// offender prints ONE line, not 48). See the call site in `load_from_source` for the law.
547fn warn_float_2d_once(name: &str, ne: &[u64], src_type: GgmlType) {
548    use std::sync::{Mutex, OnceLock};
549    static SEEN: OnceLock<Mutex<std::collections::HashSet<String>>> = OnceLock::new();
550    let pat = match name.strip_prefix("blk.").and_then(|r| r.split_once('.')) {
551        Some((_, suffix)) => format!("blk.*.{suffix}"),
552        None => name.to_string(),
553    };
554    let mut seen = SEEN
555        .get_or_init(|| Mutex::new(std::collections::HashSet::new()))
556        .lock()
557        .unwrap();
558    if seen.insert(pat.clone()) {
559        eprintln!(
560            "[loader-law] WARNING: {pat} loads as 2D Float ne={ne:?} (src {src_type:?}) — \
561                   a Float matmul weight rides cuBLAS f32 GEMV and poisons all-or-nothing q8-fast \
562                   predicates (uses_q8_1_fast/mixer_in_q8_1_fast). If matmul-class: Q8_0-encode at \
563                   load (model.rs ssm arm / source.rs BF16+F8 gates). If deliberately Float: add \
564                   it to float_2d_audited with the audit rationale."
565        );
566    }
567}
568
569/// CUTLASS-layout NVFP4 weight (B operand) for the prefill FP4 GEMM. Built once at load from the raw
570/// GGUF bytes (de-interleave + SFB swizzle). Coexists with the raw `bytes` (decode reads bytes).
571#[cfg(memra_cutlass)]
572pub struct CutlassWeight {
573    /// Plain K-contiguous packed e2m1, [out_f, in_f/2] bytes.
574    pub b_packed: CudaSlice<u8>,
575    /// Swizzled SFB (CUTLASS SfAtom layout), sized via cutlass_sfb_size(out_f, in_f).
576    pub sfb_swizzled: CudaSlice<u8>,
577}
578
579impl GpuTensor {
580    /// GATE constructor (kernel-check nvfp4-fused4 cell, hermes sweep 2026-08-23): a
581    /// split-plane (`rp: true`) NVFP4 quant tensor from raw GGUF-layout bytes — the
582    /// exact residency shape the safetensors A1 import produces, which is what the
583    /// fused4/fused3 doors require. Production loads go through `load_from_source`;
584    /// this exists so the identity gates can build a deterministic synthetic quartet
585    /// on targets whose GGUF mints carry no all-NVFP4 mixer.
586    pub fn nvfp4_rp_from_raw(
587        e: &Engine,
588        raw: &[u8],
589        in_f: usize,
590        out_f: usize,
591        scale: f32,
592    ) -> Result<Self, Box<dyn std::error::Error>> {
593        assert_eq!(raw.len() % out_f, 0, "raw bytes must tile out_f rows");
594        let row_bytes = raw.len() / out_f;
595        assert_eq!(
596            row_bytes,
597            in_f / 64 * 36,
598            "NVFP4 row layout: 36B per 64 values"
599        );
600        let bytes = e.htod_bytes(&repack_nvfp4_split(raw, out_f))?;
601        Ok(GpuTensor::Quant {
602            bytes,
603            qtype: crate::QT_NVFP4,
604            row_bytes,
605            ne: vec![in_f as u64, out_f as u64],
606            scale,
607            rp: true,
608            #[cfg(memra_cutlass)]
609            cutlass: None,
610            fp8: None,
611            rp4: None,
612            blk: None,
613            f16: None,
614        })
615    }
616
617    pub fn ne(&self) -> &[u64] {
618        match self {
619            GpuTensor::Quant { ne, .. } => ne,
620            GpuTensor::Float { ne, .. } => ne,
621            GpuTensor::FloatBf16 { ne, .. } => ne,
622        }
623    }
624    pub fn in_features(&self) -> usize {
625        self.ne()[0] as usize
626    }
627    pub fn out_features(&self) -> usize {
628        self.ne()[1] as usize
629    }
630    /// Per-tensor post-matmul macro-scale (NVFP4 carries scale != 1.0; all others -> 1.0, a no-op).
631    /// Used by the fused SwiGLU epilogue to fold the gate/up scale into one kernel.
632    pub fn scale(&self) -> f32 {
633        match self {
634            GpuTensor::Quant { scale, .. } => *scale,
635            GpuTensor::Float { .. } | GpuTensor::FloatBf16 { .. } => 1.0,
636        }
637    }
638
639    /// CUDA device ordinal this tensor resides on.
640    pub fn ordinal(&self) -> usize {
641        match self {
642            GpuTensor::Quant { bytes, .. } => bytes.ordinal(),
643            GpuTensor::Float { data, .. } => data.ordinal(),
644            GpuTensor::FloatBf16 { data, .. } => data.ordinal(),
645        }
646    }
647
648    /// Load a tensor, keeping quant types packed and float types as f32. (GGUF entry point —
649    /// thin wrapper over the source-agnostic `load_from_source`; behavior is unchanged.)
650    pub fn load(e: &Engine, g: &GgufFile, name: &str) -> Result<Self, Box<dyn std::error::Error>> {
651        Self::load_from_source(e, &GgufSource(g), name)
652    }
653
654    /// Source-agnostic load: works from any `TensorSource` (GGUF or safetensors). The engine's
655    /// forward graph only ever asks for ggml-style names; the source maps them to its own layout.
656    ///
657    /// RESIDENCY CENSUS (lane/fp8-decode-v1, 2026-08-05): the wrapper tallies what each 2D
658    /// matmul weight ACTUALLY became — resident qtype + resident bytes — so the FP8-ST decode
659    /// arm's claim ("e4m3 stays native instead of paying the Q8_0-slab tax") is a measured
660    /// per-checkpoint fact rather than an assumption about the checkpoint's dtype mix. Read it
661    /// with `residency_census_report()`; zero cost when never read.
662    pub fn load_from_source(
663        e: &Engine,
664        src: &dyn TensorSource,
665        name: &str,
666    ) -> Result<Self, Box<dyn std::error::Error>> {
667        let t = Self::load_from_source_inner(e, src, name)?;
668        if let GpuTensor::Quant {
669            qtype, bytes, ne, ..
670        } = &t
671            && ne.len() == 2
672        {
673            residency_census_note(*qtype, bytes.len());
674        }
675        Ok(t)
676    }
677
678    #[allow(clippy::manual_is_multiple_of)] // allow: divisor is runtime-derived; the modulo form keeps a zero divisor loud (a panic), where is_multiple_of would return false silently
679    fn load_from_source_inner(
680        e: &Engine,
681        src: &dyn TensorSource,
682        name: &str,
683    ) -> Result<Self, Box<dyn std::error::Error>> {
684        // A1 DIRECT NVFP4 IMPORT (2026-07-04): a PLAIN modelopt/Reza NVFP4 weight from a
685        // safetensors source repacks straight into the A6 split-plane resident layout in ONE host
686        // pass (nvfp4_repack::repack_modelopt_to_split — the scale plane is the file's
687        // weight_scale bytes verbatim), never materializing the GGUF 36B-block intermediate.
688        // The GGUF hop remains only for MEMRA_ST_DIRECT=0 (rollback/A-B seam — byte-identical
689        // resident weights either way), MEMRA_RP=0, the hybrid V-reorder transforms, and the
690        // opt-in CUTLASS resident operand (which is built from raw GGUF-layout bytes).
691        let cutlass_wants_raw = cfg!(memra_cutlass) && std::env::var("MEMRA_FP4_CUTLASS").is_ok();
692        let st_direct = std::env::var("MEMRA_ST_DIRECT")
693            .map(|v| v != "0")
694            .unwrap_or(true);
695        if rp_enabled()
696            && st_direct
697            && !cutlass_wants_raw
698            && let Some(nv) = src.find_nvfp4_native(name)
699            && nv.in_f % 64 == 0
700            && nv.out_f > 0
701        {
702            // Same post-matmul macro-scale sibling lookup as the GGUF-layout arm below.
703            let stem = name.strip_suffix(".weight").unwrap_or(name);
704            let scale = match src.find(&format!("{stem}.scale")) {
705                Some(sv) => f32::from_le_bytes(sv.bytes[..4].try_into().unwrap()),
706                None => 1.0,
707            };
708            let bytes = e.htod_bytes(&memra_gguf::nvfp4_repack::repack_modelopt_to_split(
709                nv.wbytes, &nv.wscale, nv.out_f, nv.in_f,
710            ))?;
711            return Ok(GpuTensor::Quant {
712                bytes,
713                qtype: QT_NVFP4,
714                row_bytes: nv.in_f / 64 * 36,
715                ne: vec![nv.in_f as u64, nv.out_f as u64],
716                scale,
717                rp: true,
718                #[cfg(memra_cutlass)]
719                cutlass: None,
720                fp8: None,
721                blk: None,
722                f16: None,
723                rp4: None,
724            });
725        }
726        // E4M3-DIRECT (DEFAULT since lane/fp8-decode-v1 2026-08-05; MEMRA_ST_E4M3=0 rolls back to the
727        // Q8_0 slab. Introduced default-off by lane e4m3dec 2026-07-08): F8-E4M3-origin 2D projections keep
728        // the checkpoint's RAW e4m3 device bytes + per-tensor weight_scale as the ONE resident copy
729        // (QT_F8_E4M3) instead of the Q8_0 re-encode — decode dequants e4m3 in-kernel
730        // (qmatvec_e4m3_mmvq, the checkpoint's own precision, no lossy re-quant hop), prefill
731        // (m>=16) rides the cuBLASLt FP8 GEMM on the SAME bytes (try_fp8_gemm). Frees the Q8_0
732        // duplicate the MEMRA_PP_FP8 stash needed (~3.4GB on the NV-27B) — full FP8 prefill coverage
733        // with no VRAM budget. Placed BEFORE `find` so the host-side F8->Q8_0 re-encode is skipped
734        // entirely (faster load). in_f%32 is the q8_1 activation block gate (every F8 projection in
735        // the NV-27B satisfies it; a violator falls through to the Q8_0 arm unchanged).
736        // BLOCK-128 CLASS: served by its OWN qtype since lane/fp8-blk128-decode (2026-08-05) —
737        // see the second arm below. It must not enter the per-tensor arm: the QT_F8_E4M3 kernel
738        // family consumes ONE scalar weight scale, so a block-128 operand through it would
739        // silently dequant every tile at scale 1.0.
740        if crate::fp8_ffi::st_e4m3_enabled()
741            && let Some(f8) = src.find_fp8_native(name)
742            && f8.blk.is_none()
743            && f8.in_f % 32 == 0
744            && f8.out_f > 0
745        {
746            return Ok(GpuTensor::Quant {
747                bytes: e.htod_bytes(&f8.bytes)?,
748                qtype: crate::QT_F8_E4M3,
749                row_bytes: f8.in_f,
750                ne: vec![f8.in_f as u64, f8.out_f as u64],
751                scale: f8.scale,
752                rp: false,
753                #[cfg(memra_cutlass)]
754                cutlass: None,
755                fp8: None,
756                blk: None,
757                f16: None,
758                rp4: None,
759            });
760        }
761        // E4M3-BLK-DIRECT (lane/fp8-blk128-decode, 2026-08-05) — the block-128 twin of the arm
762        // above, and the Qwen-3.8 day-one path. A block-128 FP8 checkpoint (Qwen3.6-FP8's
763        // `weight_block_size [128,128]`, the DeepSeek-V3 lineage) keeps its RAW e4m3 codes plus its
764        // [ceil(out/128), ceil(in/128)] f32 scale grid as the ONE resident copy (QT_F8_E4M3_BLK)
765        // instead of the ARM B' Q8_0 slab: decode dequants per k128 block in-kernel
766        // (qmatvec_e4m3_blk_mmvq — the checkpoint's own precision, no lossy re-quant hop) at
767        // 1.0 B/weight instead of 1.0625, and prefill (m>=16) rides the per-block FP8 MMQ tile on
768        // the SAME bytes+grid (try_fp8_blk_mmq) with NO stash duplicate.
769        //
770        // ORDERING / DISJOINTNESS (the decode-v1 landmine, restated for this arm): the three FP8
771        // arms are mutually exclusive by their scale class, checked in this order —
772        //   1. `blk.is_none()`          -> QT_F8_E4M3      (per-tensor scalar; arm above)
773        //   2. `blk.is_some()` + native -> QT_F8_E4M3_BLK  (this arm)
774        //   3. `blk.is_some()`          -> ARM B' Q8_0 slab (MEMRA_FP8_BLK_GPU) / host re-encode
775        // so ARM B' KEEPS working wherever it is still the path: whenever this arm declines (env
776        // rollback, NaN codes present, ragged in_f, grid-shape mismatch) control falls through to
777        // it unchanged. It is not cross-gated on this arm's flag — a tensor this arm CLAIMS
778        // returns here and never reaches ARM B' at all, and one it declines must reach it.
779        //
780        // NaN PRECONDITION, enforced at LOAD (not asserted): the decode kernel decodes e4m3 with
781        // the HARDWARE intrinsic (magnitude 0x7F -> NaN) while the ARM B'/host reference decodes
782        // it to 0.0 (modelopt). A tensor carrying 0x7F/0xFF therefore cannot ride this kernel, so
783        // the bytes are scanned once on the device (fp8_blk_nan_count, the same precondition the
784        // prefill MMQ arm uses) and a non-zero count declines to the Q8_0 floor for THAT tensor.
785        // Real Qwen FP8 checkpoints carry none (the exporter saturates at +-448), so this is a
786        // guard, not a cost centre: one linear pass over bytes already on the device.
787        if crate::fp8_ffi::st_e4m3_blk_enabled()
788            && let Some(f8) = src.find_fp8_native(name)
789            && let Some(grid) = f8.blk.as_ref()
790        {
791            let (in_f, out_f) = (f8.in_f, f8.out_f);
792            // in_f % 32: the q8_1 activation block gate (and the kernel's 2x LDG.128 line).
793            // The grid dims must match the shape — a mismatch means operand and grid came
794            // from different tensors; refuse rather than index a wrong block. scale == 1.0
795            // is the block class's identity (source.rs sets it alongside a grid); anything
796            // else would be a second, unapplied factor.
797            if in_f % 32 == 0
798                && out_f > 0
799                && f8.bytes.len() == out_f * in_f
800                && grid.rows == out_f.div_ceil(128)
801                && grid.cols == in_f.div_ceil(128)
802                && grid.scales.len() == grid.rows * grid.cols
803                && f8.scale == 1.0
804            {
805                let bytes = e.htod_bytes(&f8.bytes)?;
806                if e.fp8_blk_nan_count(&bytes)? == 0 {
807                    let scales = e.htod(&grid.scales)?;
808                    return Ok(GpuTensor::Quant {
809                        bytes,
810                        qtype: crate::QT_F8_E4M3_BLK,
811                        row_bytes: in_f,
812                        ne: vec![in_f as u64, out_f as u64],
813                        scale: 1.0,
814                        rp: false,
815                        #[cfg(memra_cutlass)]
816                        cutlass: None,
817                        fp8: None,
818                        blk: Some(Fp8BlockScales {
819                            scales,
820                            rows: grid.rows,
821                            cols: grid.cols,
822                        }),
823                        f16: None,
824                        rp4: None,
825                    });
826                }
827                crate::fp8_ffi::note_blk_native_nan_refused();
828            }
829        }
830        // ARM B' — GPU BLOCK-128 DEQUANT (MEMRA_FP8_BLK_GPU=1, default OFF; lane fp8-gemm-arm
831        // 2026-08-03). A block-128 FP8 checkpoint (Qwen official FP8 / DeepSeek-V3 lineage)
832        // currently loads via the host path: full f32 dequant of the tensor (f8_deq_f32) then a
833        // host Q8_0 re-encode (f32_to_q8_0) — correct, but a serial CPU pass over every byte of
834        // every projection. This arm does the same math on the GPU in ONE pass
835        // (cu/fp8_blk_dequant.cu): upload the raw e4m3 codes + the scale grid, write Q8_0
836        // blocks directly. BYTE-IDENTICAL to the host path (kernel-check [fp8-blk-gpu] arm
837        // asserts it on ragged and aligned shapes), so the resident tensor, the MMQ/MMVQ
838        // dispatch, and decode are all bit-for-bit unchanged — this is a LOAD-TIME
839        // optimization only, not a numeric config change.
840        //
841        // Placed BEFORE `find` for exactly the reason the MEMRA_ST_E4M3 arm above is: `find`
842        // would otherwise do the host dequant+re-encode we are replacing. Per-tensor and
843        // per-row scale classes are NOT touched (find_fp8_native returns blk=None / None for
844        // them) and neither are V-reorder Transform targets (find_fp8_native rejects those with
845        // a grid — the permutation invalidates the on-disk grid, so they keep the host path).
846        //
847        // NO st_e4m3 EXCLUSION (lane/fp8-decode-v1 2026-08-05): this arm used to carry
848        // `&& !st_e4m3_enabled()`, written when MEMRA_ST_E4M3 was default OFF and meant only as
849        // "the native arm above already claimed this tensor". Once native residency became the
850        // DEFAULT that condition would have been true on every run and silently disabled ARM B'
851        // for the whole block-128 class — the exact silent-slow-path landmine the flags doctrine
852        // forbids. The two arms are already disjoint by construction and need no cross-gate: the
853        // arm above returns only when `f8.blk.is_none()`, this one runs only when `f8.blk` is
854        // Some, so a tensor that reaches here was never eligible for native residency.
855        if crate::fp8_ffi::fp8_blk_gpu_enabled()
856            && let Some(f8) = src.find_fp8_native(name)
857            && let Some(grid) = f8.blk.as_ref()
858        {
859            let (in_f, out_f) = (f8.in_f, f8.out_f);
860            if in_f % 32 == 0 && out_f > 0 && f8.bytes.len() == out_f * in_f {
861                let bytes = e.fp8_blk_dequant_q8_0(&f8.bytes, &grid.scales, out_f, in_f)?;
862                return Ok(GpuTensor::Quant {
863                    bytes,
864                    qtype: QT_Q8_0,
865                    row_bytes: in_f / 32 * 34,
866                    ne: vec![in_f as u64, out_f as u64],
867                    scale: 1.0,
868                    rp: false,
869                    #[cfg(memra_cutlass)]
870                    cutlass: None,
871                    fp8: None,
872                    blk: None,
873                    f16: None,
874                    rp4: None,
875                });
876            }
877        }
878        let v = src
879            .find(name)
880            .unwrap_or_else(|| panic!("missing tensor {name}"));
881        let qtype = match v.ggml_type {
882            GgmlType::Q8_0 => Some(QT_Q8_0),
883            GgmlType::Q4_K => Some(QT_Q4_K),
884            GgmlType::Q6_K => Some(QT_Q6_K),
885            GgmlType::Q5_K => Some(QT_Q5_K),
886            GgmlType::Q3_K => Some(QT_Q3_K),
887            GgmlType::IQ4_XS => Some(QT_IQ4_XS),
888            GgmlType::IQ3_S => Some(QT_IQ3_S),
889            GgmlType::NVFP4 => Some(QT_NVFP4),
890            GgmlType::Q4_0 => Some(QT_Q4_0),
891            // F32/F16/BF16 (the dtypes safetensors carries) -> Float path below.
892            _ => None,
893        };
894        match qtype {
895            Some(qt) => {
896                // RANK GUARD (glm53-flash lane, 2026-08-28). Every quantized resident layout in
897                // this engine is a MATRIX: `row_bytes` below is derived from `ne[1]` alone, which
898                // is the out-feature count only for a 2D `[in, out]` tensor. On a 3D operand —
899                // `attn_k_b` ne [nope, kv_rank, head], `attn_v_b` ne [kv_rank, v, head] — `ne[1]`
900                // is the MIDDLE axis, so the derived stride is off by the head count and every
901                // consumer would read a plausible, wrong weight. On ne.len() == 1 the index panics
902                // with no name attached. Refuse by name instead of computing something plausible.
903                //
904                // This is not a gap to fill with a 3D quant arm: the MLA absorb/decompress kernels
905                // take `&CudaSlice<f32>`, so the correct route for a checkpoint that ships these
906                // quantized is the source-side dequant-split (`TransformKind::MlaKeyUpSplit` /
907                // `MlaValueUpSplit` in memra-gguf), which emits F32 3D and never reaches here.
908                if v.ne.len() != 2 {
909                    return Err(format!(
910                        "{name}: quantized tensor (qtype {qt}) has {}-D ne {:?}, but every \
911                         quantized resident layout in this engine is 2-D — row_bytes is derived \
912                         from ne[1] as the out-feature count and would be wrong here. A 3-D \
913                         operand must be dequantized at the source (see TensorTransform::\
914                         SplitMlaKv) or split per head before it reaches the loader.",
915                        v.ne.len(),
916                        v.ne
917                    )
918                    .into());
919                }
920                let out_f = v.ne[1] as usize;
921                let row_bytes = v.bytes.len() / out_f;
922                // NVFP4 two-level scale: per-16 ue4m3 micro-scale is in the dequant; the per-tensor
923                // F32 macro-scale lives in a sibling "<stem>.scale" tensor, applied POST-matmul
924                // (llama build_lora_mm: ggml_mul(res, w_s)). ".input_scale" is the W4A4 activation
925                // scale — UNUSED on our W4A16/f32 path. Only NVFP4 carries it; others -> 1.0 (no-op).
926                let scale = if qt == QT_NVFP4 {
927                    let stem = name.strip_suffix(".weight").unwrap_or(name);
928                    match src.find(&format!("{stem}.scale")) {
929                        Some(sv) => f32::from_le_bytes(sv.bytes[..4].try_into().unwrap()),
930                        None => 1.0,
931                    }
932                } else {
933                    1.0
934                };
935                // A6 SPLIT-PLANE repack: NVFP4 2-D matmul weights upload in walk-order layout
936                // (host-side permutation before htod — zero VRAM spike, layer-streamed by
937                // construction). Every consumer kernel dispatches its `_rp` twin off the flag.
938                let rp = qt == QT_NVFP4
939                    && v.ne.len() == 2
940                    && (v.ne[0] as usize).is_multiple_of(64)
941                    && v.bytes.len() % out_f == 0
942                    && (v.bytes.len() / out_f).is_multiple_of(36)
943                    && rp_enabled();
944                let bytes = if rp {
945                    e.htod_bytes(&repack_nvfp4_split(&v.bytes, out_f))?
946                } else {
947                    e.htod_bytes(&v.bytes)?
948                };
949                // CUTLASS NVFP4 prefill operand, built from the RAW GGUF bytes (a temp raw upload
950                // when the resident `bytes` are repacked). Gated: only NVFP4 weights, only when
951                // MEMRA_FP4_CUTLASS is set, only under cfg(memra_cutlass). in_f%64==0 is the NVFP4
952                // K-block constraint (same as the dispatch).
953                #[cfg(memra_cutlass)]
954                let cutlass = {
955                    let in_f = v.ne[0] as usize;
956                    // Skip the resident repack when OTF is requested (per-call repack instead) — the
957                    // resident path ~doubles NVFP4 weight VRAM and OOMs larger models (e.g. 27B/24GB).
958                    if qt == QT_NVFP4
959                        && in_f % 64 == 0
960                        && v.ne.len() == 2
961                        && std::env::var("MEMRA_FP4_CUTLASS").is_ok()
962                        && std::env::var("MEMRA_FP4_CUTLASS_OTF").is_err()
963                    {
964                        let raw_dev;
965                        let src_dev = if rp {
966                            raw_dev = e.htod_bytes(&v.bytes)?;
967                            &raw_dev
968                        } else {
969                            &bytes
970                        };
971                        let (b_packed, sfb_swizzled) =
972                            e.build_cutlass_weight(src_dev, out_f, in_f, row_bytes)?;
973                        Some(CutlassWeight {
974                            b_packed,
975                            sfb_swizzled,
976                        })
977                    } else {
978                        None
979                    }
980                };
981                // FP8-ACT PREFILL operand (MEMRA_PP_FP8=1): for F8-E4M3-sourced projections (they
982                // surface as Q8_0 from the source's re-encode) ALSO stash the raw e4m3 device
983                // bytes + weight_scale. The source guarantees byte order matches `v` (the
984                // Transform arm's V-reorder is baked into both); the ne check guards a mixup.
985                // VRAM BUDGET (24GB rigs, 2026-07-08): the stash duplicates every F8-origin
986                // projection (~+3.4GB on the 27B) — fine on the 96GB box, OOM here. The stash
987                // spends from MEMRA_PP_FP8_BUDGET_MB (default 1536); once spent, remaining
988                // tensors ride the old path. Load order is layer order, so the budget covers a
989                // PREFIX of layers — coverage (and the prefill win) scales with the budget.
990                // MEMRA_FP8_MMQ=1 (lane/fp8-mmq) admits the SAME stash for the block-128 class:
991                // the per-block MMQ prefill kernel is that class's consumer, and it needs exactly
992                // what this arm makes resident (raw e4m3 bytes + the verbatim f32 grid). It shares
993                // the budget accounting below, so a 24GB rig still caps the duplicate.
994                // NOTE the gate here is `fp8_mmq_enabled` (the STASH gate, still opt-in) and NOT
995                // `fp8_blk_mmq_native_enabled` (default ON since 2026-08-05). That is deliberate:
996                // this arm's whole product is a DUPLICATE weight copy, and the native-resident route
997                // exists precisely to avoid one. A QT_F8_E4M3_BLK tensor already carries its own
998                // e4m3 bytes + grid, so it needs nothing from here; wiring the default-ON gate into
999                // this condition would spend the budget on copies no kernel reads.
1000                let fp8 = if qt == QT_Q8_0
1001                    && (crate::fp8_ffi::pp_fp8_enabled() || crate::fp8_ffi::fp8_mmq_enabled())
1002                {
1003                    match src.find_fp8_native(name) {
1004                        Some(f8)
1005                            if v.ne.len() == 2
1006                                && f8.in_f as u64 == v.ne[0]
1007                                && f8.out_f as u64 == v.ne[1] =>
1008                        {
1009                            use std::sync::atomic::{AtomicUsize, Ordering};
1010                            static FP8_SPENT: AtomicUsize = AtomicUsize::new(0);
1011                            static FP8_BUDGET: std::sync::OnceLock<usize> =
1012                                std::sync::OnceLock::new();
1013                            let budget = *FP8_BUDGET.get_or_init(|| {
1014                                std::env::var("MEMRA_PP_FP8_BUDGET_MB")
1015                                    .ok()
1016                                    .and_then(|v| v.parse::<usize>().ok())
1017                                    .unwrap_or(1536)
1018                                    << 20
1019                            });
1020                            let sz = f8.bytes.len();
1021                            if FP8_SPENT.fetch_add(sz, Ordering::Relaxed) + sz <= budget {
1022                                // Block-128 grid rides along resident (checkpoint order,
1023                                // Fp8BlockScales layout contract). try_fp8_gemm still skips blk
1024                                // operands (cuBLASLt takes no block grid on sm_120, P1-VERDICT);
1025                                // try_fp8_blk_mmq is their consumer under MEMRA_FP8_MMQ=1.
1026                                let blk = match f8.blk {
1027                                    Some(g) => Some(Fp8BlockScales {
1028                                        scales: e.htod(&g.scales)?,
1029                                        rows: g.rows,
1030                                        cols: g.cols,
1031                                    }),
1032                                    None => None,
1033                                };
1034                                Some(Fp8Weight {
1035                                    bytes: e.htod_bytes(&f8.bytes)?,
1036                                    scale: f8.scale,
1037                                    blk,
1038                                })
1039                            } else {
1040                                FP8_SPENT.fetch_sub(sz, Ordering::Relaxed);
1041                                None
1042                            }
1043                        }
1044                        _ => None,
1045                    }
1046                } else {
1047                    None
1048                };
1049                Ok(GpuTensor::Quant {
1050                    bytes,
1051                    qtype: qt,
1052                    row_bytes,
1053                    ne: v.ne.clone(),
1054                    scale,
1055                    rp,
1056                    #[cfg(memra_cutlass)]
1057                    cutlass,
1058                    fp8,
1059                    blk: None,
1060                    rp4: None,
1061                    f16: None,
1062                })
1063            }
1064            None => {
1065                let n: u64 = v.ne.iter().product();
1066                // FULL-PRECISION MODE (MEMRA_FULL_PREC): NO re-encodes. Large 2D bf16 matmul weights
1067                // stay bf16-resident (FloatBf16, dequant-on-use) so the trunk fits VRAM; everything
1068                // else (small 2D, 1D norms, F16/F32) rides the exact f32 Float path below. The ssm
1069                // Q8_0 re-encode and the Float-poison tripwire are BYPASSED here (both are the loader
1070                // law this mode exists to suspend — the warnings would be correct but noise).
1071                // MEMRA_BF16_MMV=1 shares the FULL_PREC bf16-resident arm for large 2D BF16
1072                // sources: raw checkpoint bytes on device (2 B/w, ~halving both VRAM and the
1073                // decode read traffic of every preserved non-expert weight — shexp, lm_head,
1074                // owning-stage attention, dense FFN, router) with the one-block-per-row bf16
1075                // matvec at decode m=1 and the chunked expansion path at m>1. Numeric-class
1076                // door, run-gen argmax gate + boot battery (see docs/FLAGS.md).
1077                if full_prec_enabled() || crate::Engine::bf16_mmv_on() {
1078                    // Only bf16 sources take the resident-bf16 arm; F16/F32 fall through to f32 Float
1079                    // (exact, and tiny/absent in the bf16 ST checkpoints this mode targets). The 1M
1080                    // threshold keeps small tensors (norms, gate_inp) on the proven f32 path — only
1081                    // the big trunk matrices need the 2 B/w VRAM saving. The MMV door uses 2M:
1082                    // the MoE router (288x4096 = 1.18M) is consumed via float_data() and its
1083                    // logits pick the routes — it stays exact-f32 so routing never moves.
1084                    let threshold = if full_prec_enabled() {
1085                        1_000_000
1086                    } else {
1087                        2_000_000
1088                    };
1089                    if v.ggml_type == GgmlType::BF16 && v.ne.len() == 2 && n >= threshold {
1090                        let data = e.htod_bytes(&v.bytes)?; // raw bf16 bytes, u16 LE pairs
1091                        // LOAD-TIME ENGAGEMENT RECEIPT. This is the only DOOR-GATED producer of
1092                        // FloatBf16 residency, so counting this line per arm is the door's own
1093                        // announce: it must be 0 with MEMRA_BF16_MMV=0 (and MEMRA_FULL_PREC off)
1094                        // and >0 with =1. It is NOT the only FloatBf16 producer in the engine --
1095                        // the masked-vocab trimmed head arms in hybrid.rs make FloatBf16
1096                        // unconditionally -- so this counts the door, not bf16 residency at large.
1097                        // Added because the 2026-08-28 sweep's `grep -c 'bf16.mmv'` returned 0 in
1098                        // BOTH arms: no such line existed anywhere in the tree, which is a RECEIPT
1099                        // DEFECT, not a no-engagement result.
1100                        eprintln!(
1101                            "[bf16-mmv] RESIDENT {name} ne={:?} n={n} admit={}",
1102                            v.ne,
1103                            if full_prec_enabled() {
1104                                "full_prec"
1105                            } else {
1106                                "bf16_mmv"
1107                            }
1108                        );
1109                        return Ok(GpuTensor::FloatBf16 {
1110                            data,
1111                            ne: v.ne.clone(),
1112                        });
1113                    }
1114                    if full_prec_enabled() {
1115                        let f32v = dequant::dequantize(v.ggml_type, &v.bytes, n as usize);
1116                        return Ok(GpuTensor::Float {
1117                            data: e.htod(&f32v)?,
1118                            ne: v.ne.clone(),
1119                        });
1120                    }
1121                }
1122                let f32v = dequant::dequantize(v.ggml_type, &v.bytes, n as usize);
1123                // ssm_beta/ssm_alpha stored F32 (the 35B GGUF): Q8_0-encode at load. F32 here
1124                // fails `mixer_in_q8_1_fast` for the whole linear-attn mixer -> every linear
1125                // layer falls off the fused norm+quantize chain onto cuBLAS f32 GEMV pairs
1126                // (the NV-27B in_proj_a/b lesson, same all-or-nothing capability check; nsys
1127                // 35B: 100 dot+reduce launches/token). Q8_0 of an F32 source is the same
1128                // class-lossless step every 9B GGUF already ships for these tensors.
1129                if v.ne.len() == 2
1130                    && v.ne[0].is_multiple_of(32)
1131                    && (name.ends_with("ssm_beta.weight") || name.ends_with("ssm_alpha.weight")
1132                        // E4B per_layer_model_proj (F16 [2560, 10752]): matmul-class — the
1133                        // loader-law recipe (2026-07-12). As Float it rode cuBLAS f32 whose
1134                        // m=1-vs-m=16 FP-order gap seeds inp_pl noise into EVERY layer's PLE
1135                        // tail; the 42-layer stack amplifies it to logit maxdiff ~27 and the
1136                        // chat-prompt prefill-vs-decode argmax gate fails.
1137                        || name.ends_with("per_layer_model_proj.weight"))
1138                {
1139                    let q8 = memra_gguf::nvfp4_repack::f32_to_q8_0(&f32v);
1140                    return GpuTensor::from_quant_bytes(
1141                        e,
1142                        &q8,
1143                        GgmlType::Q8_0,
1144                        v.ne[0],
1145                        v.ne[1],
1146                        1.0,
1147                    );
1148                }
1149                // LOADER-LAW TRIPWIRE (loadersweep 2026-07-08): a 2D Float tensor with both dims
1150                // >= 16 is almost certainly MATMUL-class, and a Float matmul weight (a) rides
1151                // cuBLAS f32 GEMV pairs (dot_kernel + reduce_1Block in nsys) and (b) fails
1152                // uses_q8_1_fast, poisoning every ALL-OR-NOTHING fast-path predicate it sits on
1153                // (mixer_in_q8_1_fast etc.) — the trap that cost measurable perf 4 times (NV-27B
1154                // in_proj_a/b BF16, 35B ssm_beta/alpha F32, M3 shexp cousin, M3 BF16 lm_head).
1155                // Fix recipe: name-gated f32_to_q8_0 encode at load (see the ssm arm above /
1156                // source.rs BF16+F8 gates). Norm-class tensors are 1D or have a dim < 16
1157                // (conv1d ne[0]=4) and never reach this warning.
1158                if v.ne.len() == 2 && v.ne[0] >= 16 && v.ne[1] >= 16 && !float_2d_audited(name) {
1159                    warn_float_2d_once(name, &v.ne, v.ggml_type);
1160                }
1161                // F32/F16/BF16 (or as-yet-unhandled quant): dequant to f32. Small tensors only.
1162                Ok(GpuTensor::Float {
1163                    data: e.htod(&f32v)?,
1164                    ne: v.ne.clone(),
1165                })
1166            }
1167        }
1168    }
1169
1170    /// Build a Quant tensor directly from raw ggml block bytes (FR-Spec self-trim: byte-level row
1171    /// gather from an already-loaded weight — rows in every ggml quant are independent, so a
1172    /// contiguous per-row byte copy is a lossless "trim"). `ne0` = in_features, `ne1` = rows.
1173    pub fn from_quant_bytes(
1174        e: &Engine,
1175        bytes: &[u8],
1176        ty: GgmlType,
1177        ne0: u64,
1178        ne1: u64,
1179        scale: f32,
1180    ) -> Result<Self, Box<dyn std::error::Error>> {
1181        let qt = match ty {
1182            GgmlType::Q8_0 => QT_Q8_0,
1183            GgmlType::Q4_K => QT_Q4_K,
1184            GgmlType::Q6_K => QT_Q6_K,
1185            GgmlType::Q5_K => QT_Q5_K,
1186            GgmlType::Q3_K => QT_Q3_K,
1187            GgmlType::IQ4_XS => QT_IQ4_XS,
1188            GgmlType::IQ3_S => QT_IQ3_S,
1189            GgmlType::NVFP4 => QT_NVFP4,
1190            GgmlType::Q4_0 => QT_Q4_0,
1191            other => panic!("from_quant_bytes: unsupported dtype {other:?}"),
1192        };
1193        let row_bytes = bytes.len() / ne1 as usize;
1194        // Same A6 repack as load_from_source: callers pass GGUF-layout host bytes (the FR-Spec
1195        // self-trim row-gathers from the source file bytes, which are always original layout).
1196        let rp = qt == QT_NVFP4
1197            && ne0.is_multiple_of(64)
1198            && row_bytes.is_multiple_of(36)
1199            && rp_enabled();
1200        let dev = if rp {
1201            e.htod_bytes(&repack_nvfp4_split(bytes, ne1 as usize))?
1202        } else {
1203            e.htod_bytes(bytes)?
1204        };
1205        Ok(GpuTensor::Quant {
1206            bytes: dev,
1207            qtype: qt,
1208            row_bytes,
1209            ne: vec![ne0, ne1],
1210            scale,
1211            rp,
1212            #[cfg(memra_cutlass)]
1213            cutlass: None,
1214            fp8: None,
1215            blk: None,
1216            f16: None,
1217            rp4: None,
1218        })
1219    }
1220
1221    pub fn load_opt(
1222        e: &Engine,
1223        g: &GgufFile,
1224        name: &str,
1225    ) -> Result<Option<Self>, Box<dyn std::error::Error>> {
1226        Self::load_opt_from_source(e, &GgufSource(g), name)
1227    }
1228
1229    pub fn load_opt_from_source(
1230        e: &Engine,
1231        src: &dyn TensorSource,
1232        name: &str,
1233    ) -> Result<Option<Self>, Box<dyn std::error::Error>> {
1234        if src.has(name) {
1235            Ok(Some(Self::load_from_source(e, src, name)?))
1236        } else {
1237            Ok(None)
1238        }
1239    }
1240
1241    /// Accessor for tensors that MUST be f32 (norm weights). Panics if quantized.
1242    pub fn float_data(&self) -> &CudaSlice<f32> {
1243        match self {
1244            GpuTensor::Float { data, .. } => data,
1245            GpuTensor::Quant { .. } => panic!("expected float tensor (norm), got quantized"),
1246            GpuTensor::FloatBf16 { .. } => {
1247                panic!("expected f32 float tensor (norm), got bf16-resident matmul weight")
1248            }
1249        }
1250    }
1251}
1252
1253pub struct Layer {
1254    pub attn_norm: GpuTensor,
1255    pub wq: GpuTensor,
1256    pub wk: GpuTensor,
1257    pub wv: GpuTensor,
1258    pub wo: GpuTensor,
1259    pub q_norm: Option<GpuTensor>,
1260    pub k_norm: Option<GpuTensor>,
1261    pub ffn_norm: GpuTensor,
1262    /// FFN: dense SwiGLU or routed MoE (OLMoE — dense attention + MoE FFN). Reuses the hybrid
1263    /// `Ffn` enum + `load_ffn` so the routed-expert forward is shared with `HybridModel::moe_ffn`.
1264    pub ffn: crate::hybrid::Ffn,
1265}
1266
1267/// Host-resident embedding table for row gather (dequant only the needed token rows).
1268pub struct EmbedHost {
1269    pub raw: Vec<u8>,
1270    pub ggml_type: GgmlType,
1271    pub n_embd: usize,
1272}
1273impl EmbedHost {
1274    pub fn from_gguf(g: &GgufFile, name: &str) -> Self {
1275        Self::from_source(&GgufSource(g), name)
1276    }
1277    pub fn from_source(src: &dyn TensorSource, name: &str) -> Self {
1278        let v = src
1279            .find(name)
1280            .unwrap_or_else(|| panic!("missing embed {name}"));
1281        EmbedHost {
1282            raw: v.bytes.to_vec(),
1283            ggml_type: v.ggml_type,
1284            n_embd: v.ne[0] as usize,
1285        }
1286    }
1287    /// QT int + row_bytes for this embed table's dtype (for the device embed-gather kernel).
1288    /// CUDA-GRAPH-PLAN Phase 1. Mirrors the GpuTensor qtype mapping.
1289    pub fn qt_and_row_bytes(&self, n_embd: usize) -> (i32, usize) {
1290        let (blk, tsize) = self.ggml_type.block_and_type_size();
1291        let row_bytes = (n_embd as u64 / blk * tsize) as usize;
1292        let qt = match self.ggml_type {
1293            GgmlType::Q8_0 => QT_Q8_0,
1294            GgmlType::Q4_K => QT_Q4_K,
1295            GgmlType::Q6_K => QT_Q6_K,
1296            GgmlType::Q5_K => QT_Q5_K,
1297            GgmlType::Q3_K => QT_Q3_K,
1298            GgmlType::IQ4_XS => QT_IQ4_XS,
1299            GgmlType::IQ3_S => QT_IQ3_S,
1300            GgmlType::NVFP4 => QT_NVFP4,
1301            GgmlType::F32 => QT_F32,
1302            // BF16 embed table (FULL_PREC research mode: qwen35-9b-hf) — device gather does the
1303            // exact bits<<16 expansion; 2 B/elem resident instead of an f32-doubled table.
1304            GgmlType::BF16 => QT_BF16,
1305            other => panic!("embed_gather: unsupported dtype {other:?}"),
1306        };
1307        (qt, row_bytes)
1308    }
1309
1310    /// Rows this embedding table actually holds — the ONLY bound that matters for a gather,
1311    /// and not the same number as `cfg.vocab_size` on a padded table.
1312    pub fn rows(&self, n_embd: usize) -> usize {
1313        let (blk, tsize) = self.ggml_type.block_and_type_size();
1314        let row_bytes = (n_embd as u64 / blk * tsize) as usize;
1315        self.raw.len().checked_div(row_bytes).unwrap_or(0)
1316    }
1317
1318    /// Gather rows for tokens -> [T, n_embd] f32, dequant per row from raw bytes, REFUSING an
1319    /// out-of-range id by name.
1320    ///
1321    /// WHY THIS IS A RESULT AND NOT A SLICE. 2026-09-02, 2x B200, boot D
1322    /// (`MEMRA_PRIME_CHUNK=4096 MEMRA_MOE_F16G=1`): the mode-1 expert GEMM corrupted the trunk,
1323    /// the logits went bad, the sampler handed back an id near `i32::MAX`, and this function's
1324    /// `self.raw[off..off + row_bytes]` panicked with `range start index 17592186036224 out of
1325    /// range`. That panic took the GPU WORKER down, the respawn hit it again, and every request
1326    /// after it was connection refused — one numeric bug in one door became a fleet outage
1327    /// (`research/glm5-b200-20260902/box/prefill/`, and LAW: engine panics are fleet-fatal).
1328    /// The numeric bug is fixed at its own site; this guard is the reason the NEXT one costs a
1329    /// request instead of a fleet. The message names the id, the bound and the likely upstream,
1330    /// because a bounds error with no attribution sends the next reader to the wrong file.
1331    pub fn try_gather(&self, n_embd: usize, tokens: &[u32]) -> Result<Vec<f32>, String> {
1332        let (blk, tsize) = self.ggml_type.block_and_type_size();
1333        let row_bytes = (n_embd as u64 / blk * tsize) as usize;
1334        let rows = self.rows(n_embd);
1335        let mut x = vec![0f32; tokens.len() * n_embd];
1336        for (ti, &tok) in tokens.iter().enumerate() {
1337            if tok as usize >= rows {
1338                return Err(format!(
1339                    "embed gather: token id {tok} at position {ti} is outside this table's                      {rows} rows ({} raw bytes / {row_bytes} B per row, dtype {:?}). An                      out-of-range id is produced UPSTREAM — corrupt logits, a sampler reading                      a stale or non-finite row, or a tokenizer/vocab mismatch — so fix it                      there; this refusal exists so the worker does not die on the slice",
1340                    self.raw.len(),
1341                    self.ggml_type,
1342                ));
1343            }
1344            let off = tok as usize * row_bytes;
1345            let row = dequant::dequantize(self.ggml_type, &self.raw[off..off + row_bytes], n_embd);
1346            x[ti * n_embd..ti * n_embd + n_embd].copy_from_slice(&row);
1347        }
1348        Ok(x)
1349    }
1350
1351    /// [`Self::try_gather`] for the call sites that cannot return an error (probes, gates).
1352    /// Serving paths take `try_gather` — see its header for why the difference is load-bearing.
1353    pub fn gather(&self, n_embd: usize, tokens: &[u32]) -> Vec<f32> {
1354        self.try_gather(n_embd, tokens)
1355            .unwrap_or_else(|err| panic!("{err}"))
1356    }
1357}
1358
1359pub struct Model {
1360    pub cfg: ModelConfig,
1361    pub embd: EmbedHost,
1362    pub output_norm: GpuTensor,
1363    pub output: GpuTensor,
1364    pub layers: Vec<Layer>,
1365}
1366
1367impl Model {
1368    /// Load a dense (vanilla-transformer) model from GGUF. Thin wrapper over
1369    /// `load_dense_from_source`. Panics if the arch has SSM/MoE layers.
1370    pub fn load_dense(e: &Engine, g: &GgufFile) -> Result<Self, Box<dyn std::error::Error>> {
1371        Self::load_dense_from_source(e, &GgufSource(g))
1372    }
1373
1374    /// Load a dense-attention model from any `TensorSource` — GGUF or a safetensors HF checkpoint.
1375    /// The whole loop speaks ggml names; the source maps them. The FFN is dense SwiGLU OR routed MoE
1376    /// (OLMoE: dense full-attention + MoE FFN). Panics on hybrid (SSM) arches — use the hybrid path.
1377    pub fn load_dense_from_source(
1378        e: &Engine,
1379        src: &dyn TensorSource,
1380    ) -> Result<Self, Box<dyn std::error::Error>> {
1381        let cfg = src.try_config().map_err(std::io::Error::other)?;
1382        let plan = match memra_gguf::model_packs::for_config(&cfg) {
1383            Some(pack) => pack.compile_plan(&cfg)?,
1384            None => memra_gguf::model_plan::ModelPlan::compile(&cfg)?,
1385        };
1386        if plan.layers.iter().any(|layer| {
1387            !matches!(
1388                layer.attention,
1389                memra_gguf::model_plan::AttentionPlan::Full(_)
1390            )
1391        }) {
1392            return Err("plain executor requires full-attention ModelPlan layers".into());
1393        }
1394        let embd = EmbedHost::from_source(src, "token_embd.weight");
1395        let output_norm = GpuTensor::load_from_source(e, src, "output_norm.weight")?;
1396        // tied embeddings: fall back to tok_embd if output.weight absent (OLMoE has untied output).
1397        let output = if src.has("output.weight") {
1398            GpuTensor::load_from_source(e, src, "output.weight")?
1399        } else {
1400            GpuTensor::load_from_source(e, src, "token_embd.weight")?
1401        };
1402        let mut resident = crate::hybrid::ResidentPlan::unsharded(e, src, &cfg);
1403        let mut step_runtimes = crate::hybrid::StepParallelRuntimeRegistry::default();
1404
1405        let mut layers = Vec::with_capacity(plan.layers.len());
1406        for (il, layer_plan) in plan.layers.iter().enumerate() {
1407            let il = il as u32;
1408            let p = |s: &str| format!("blk.{il}.{s}");
1409            let ffn = crate::hybrid::load_ffn(
1410                e,
1411                src,
1412                &cfg,
1413                &layer_plan.mlp,
1414                il,
1415                None,
1416                &mut resident,
1417                &mut step_runtimes,
1418            )?;
1419            layers.push(Layer {
1420                attn_norm: GpuTensor::load_from_source(e, src, &p("attn_norm.weight"))?,
1421                wq: GpuTensor::load_from_source(e, src, &p("attn_q.weight"))?,
1422                wk: GpuTensor::load_from_source(e, src, &p("attn_k.weight"))?,
1423                wv: GpuTensor::load_from_source(e, src, &p("attn_v.weight"))?,
1424                wo: GpuTensor::load_from_source(e, src, &p("attn_output.weight"))?,
1425                q_norm: GpuTensor::load_opt_from_source(e, src, &p("attn_q_norm.weight"))?,
1426                k_norm: GpuTensor::load_opt_from_source(e, src, &p("attn_k_norm.weight"))?,
1427                ffn_norm: GpuTensor::load_from_source(e, src, &p("ffn_norm.weight"))?,
1428                ffn,
1429            });
1430        }
1431        Ok(Model {
1432            cfg,
1433            embd,
1434            output_norm,
1435            output,
1436            layers,
1437        })
1438    }
1439
1440    /// Largest expert block (bytes) across all MoE layers — the fixed cache-slot size (mirrors
1441    /// `HybridModel::max_moe_block`). 0 for a dense (non-MoE) model.
1442    pub(crate) fn max_moe_block(&self) -> usize {
1443        use crate::hybrid::Ffn;
1444        let mut mx = 0usize;
1445        for l in &self.layers {
1446            if let Ffn::Moe(m) = &l.ffn {
1447                mx = mx
1448                    .max(m.gate_exps.max_expert_bytes())
1449                    .max(m.up_exps.max_expert_bytes())
1450                    .max(m.down_exps.max_expert_bytes());
1451            }
1452        }
1453        mx
1454    }
1455
1456    /// Gather embedding rows into f32 [T, n_embd] (token-major) by dequantizing only the needed
1457    /// rows from the host-side embedding bytes (token_embd is [n_embd, n_vocab], row per token).
1458    pub fn embed_tokens(
1459        &self,
1460        e: &Engine,
1461        tokens: &[u32],
1462    ) -> Result<CudaSlice<f32>, Box<dyn std::error::Error>> {
1463        let n_embd = self.cfg.n_embd as usize;
1464        let x = self.embd.try_gather(n_embd, tokens)?;
1465        e.htod(&x)
1466    }
1467}
1468
1469pub type TensorMap = HashMap<String, GpuTensor>;
1470
1471/// One layer's stacked 256-expert tensor, raw GGUF quant bytes held HOST-RESIDENT.
1472///
1473/// EDGE-1: these bytes are NEVER uploaded at load (uploading 29.75GB would OOM a 24GB GPU —
1474/// this is BUG-4). Per token, only the 8 routed experts are staged H2D into a small GPU scratch.
1475///
1476/// ne = [in_f, out_f, n_expert]; the expert axis (ne[2]) is the slowest/highest-stride axis, so
1477/// expert `e` occupies the CONTIGUOUS byte block `bytes[e*expert_stride .. (e+1)*expert_stride]`.
1478///
1479/// THE 3D FIX: GpuTensor::load computes `row_bytes = raw.len()/ne[1]`, which for a stacked 3D
1480/// tensor ignores the 256-expert axis and is 256x too large (gate_exps -> 430080 instead of 1680).
1481/// load() here uses `row_bytes = raw.len() / (out_f * n_expert)` (= 1680 gate/up, 544 down).
1482/// Host byte storage for the expert blocks. Default = a pageable `Vec<u8>` (current behavior). Under
1483/// MEMRA_MOE_PINNED (auto-on when MEMRA_MOE_CACHE is set), the bytes live in CUDA pinned host memory so
1484/// the miss-path `memcpy_htod` is a true DMA, not a pageable bounce copy (MOE-SLRU-PLAN §C.1).
1485///
1486/// CAVEAT (§C.1): `alloc_pinned` uses CU_MEMHOSTALLOC_WRITECOMBINED — great for H2D-only (the expert
1487/// bytes are never read by the CPU on the hot path), but write-combined memory is SLOW for CPU reads.
1488/// A future CPU-VNNI cold-expert fallback must NOT read from this buffer.
1489pub enum HostBuf {
1490    Paged(Vec<u8>),
1491    /// Pinned host memory. We keep the `PinnedHostSlice` alive (it owns the allocation; Drop frees it)
1492    /// AND cache its raw base pointer + len so the hot-path `as_bytes()` needs no per-call event sync.
1493    Pinned {
1494        slice: std::sync::Arc<cudarc::driver::PinnedHostSlice<u8>>,
1495        base: *const u8,
1496        len: usize,
1497    },
1498    /// Alias into a shared pinned slab (ST pinned tier): `owner` keeps the slab alive; `base`/`len`
1499    /// select this expert's window. Same DMA class as `Pinned`.
1500    PinnedAlias {
1501        owner: std::sync::Arc<HostBuf>,
1502        base: *const u8,
1503        len: usize,
1504    },
1505    /// SPILLING-PLAN §1, Tier 2 (disk): the bytes live in an mmap'd region of the GGUF file, NOT in
1506    /// RAM. `map` is `MAP_SHARED`, no `MAP_POPULATE` — zero upfront copy. The first `memcpy_htod` of
1507    /// this slice page-faults → NVMe read → DMA (the demand-fault disk path). `off`/`len` select this
1508    /// expert's contiguous block within the shared file mmap. Bit-identical to `Paged`/`Pinned` —
1509    /// those copied FROM exactly these on-disk bytes, so the GEMM result is unchanged.
1510    Mmap {
1511        map: std::sync::Arc<memmap2::Mmap>,
1512        /// The same opened inode backing `map`. It must outlive the loader source so future explicit
1513        /// positioned reads cannot accidentally reopen a replaced path.
1514        file: std::sync::Arc<std::fs::File>,
1515        /// Absolute byte offset within both the whole-file mmap and `file`.
1516        off: usize,
1517        len: usize,
1518    },
1519}
1520// SAFETY: `base` is a stable pinned-host pointer owned by `slice`; the buffer is written once at load
1521// then only READ for H2D. HostExps is shared `&` across the (single per-Engine) forward, so Send/Sync
1522// mirror the underlying PinnedHostSlice (which is already Send+Sync). The `Mmap` arm holds
1523// `Arc<Mmap>` + `Arc<File>` (both Send+Sync) plus plain usize fields, so it does not weaken bounds.
1524unsafe impl Send for HostBuf {}
1525unsafe impl Sync for HostBuf {}
1526impl HostBuf {
1527    #[inline]
1528    pub fn as_bytes(&self) -> &[u8] {
1529        match self {
1530            HostBuf::Paged(v) => v.as_slice(),
1531            // SAFETY: base+len are the pinned allocation's stable extent; written once at load, then
1532            // read-only. We avoid `as_slice()` here because it would synchronize the buffer's event
1533            // on every hot-path call.
1534            HostBuf::Pinned { base, len, .. } => unsafe { std::slice::from_raw_parts(*base, *len) },
1535            HostBuf::PinnedAlias { base, len, .. } => unsafe {
1536                std::slice::from_raw_parts(*base, *len)
1537            },
1538            // Slicing the mmap is the same `&[u8]` the kernel DMAs; the read page-faults the NVMe.
1539            HostBuf::Mmap { map, off, len, .. } => &map[*off..*off + *len],
1540        }
1541    }
1542    #[inline]
1543    #[allow(clippy::len_without_is_empty)] // allow: HostBuf is a sized byte slab; zero length is not a state callers name
1544    pub fn len(&self) -> usize {
1545        match self {
1546            HostBuf::Paged(v) => v.len(),
1547            HostBuf::Pinned { len, .. } => *len,
1548            HostBuf::PinnedAlias { len, .. } => *len,
1549            HostBuf::Mmap { len, .. } => *len,
1550        }
1551    }
1552
1553    /// Best-effort OS read-ahead for a future mmap-backed expert range. This does not touch or
1554    /// copy the bytes, so the zero-copy ownership contract is unchanged. Non-mmap buffers are
1555    /// already resident and need no advice. Kept fallible-at-the-OS but non-fatal at the call site:
1556    /// an unsupported/pressured kernel simply leaves the normal demand-fault path in place.
1557    #[inline]
1558    pub fn advise_willneed(&self, rel_off: usize, len: usize) -> bool {
1559        let HostBuf::Mmap {
1560            map,
1561            off,
1562            len: extent,
1563            ..
1564        } = self
1565        else {
1566            return false;
1567        };
1568        if len == 0 || rel_off > *extent || len > *extent - rel_off {
1569            return false;
1570        }
1571        #[cfg(unix)]
1572        {
1573            map.advise_range(memmap2::Advice::WillNeed, *off + rel_off, len)
1574                .is_ok()
1575        }
1576        #[cfg(not(unix))]
1577        {
1578            let _ = (map, off);
1579            false
1580        }
1581    }
1582
1583    #[inline]
1584    fn expert_source(&self, rel_off: usize, len: usize) -> ExpertSource<'_> {
1585        debug_assert!(rel_off <= self.len() && len <= self.len() - rel_off);
1586        match self {
1587            HostBuf::Mmap { map, file, off, .. } => {
1588                let offset = *off + rel_off;
1589                ExpertSource::Disk {
1590                    file,
1591                    offset: offset as u64,
1592                    len,
1593                    fallback: &map[offset..offset + len],
1594                    keepalive: ExpertKeepalive::Mmap(map.clone()),
1595                }
1596            }
1597            HostBuf::Pinned { slice, .. } => ExpertSource::Memory {
1598                bytes: &self.as_bytes()[rel_off..rel_off + len],
1599                keepalive: Some(ExpertKeepalive::Pinned(slice.clone())),
1600            },
1601            HostBuf::PinnedAlias { owner, .. } => ExpertSource::Memory {
1602                bytes: &self.as_bytes()[rel_off..rel_off + len],
1603                keepalive: Some(ExpertKeepalive::Buffer(owner.clone())),
1604            },
1605            HostBuf::Paged(_) => ExpertSource::Memory {
1606                bytes: &self.as_bytes()[rel_off..rel_off + len],
1607                // CUDA stages pageable input before returning from the async-copy API. Only true
1608                // pinned and mmap-backed sources need an explicit lifetime owner in the cache.
1609                keepalive: None,
1610            },
1611        }
1612    }
1613}
1614
1615/// Clonable ownership retained by asynchronous cache transfers. The payload is intentionally never
1616/// read: keeping it alive is the contract.
1617#[allow(dead_code)]
1618pub(crate) enum ExpertKeepalive {
1619    Pinned(std::sync::Arc<cudarc::driver::PinnedHostSlice<u8>>),
1620    Buffer(std::sync::Arc<HostBuf>),
1621    Mmap(std::sync::Arc<memmap2::Mmap>),
1622}
1623
1624/// Source-aware view of one expert block. The mmap fallback remains the byte oracle; retaining the
1625/// opened file enables a later explicit-read backend without changing tensor layout or numerics.
1626pub(crate) enum ExpertSource<'a> {
1627    Memory {
1628        bytes: &'a [u8],
1629        keepalive: Option<ExpertKeepalive>,
1630    },
1631    Disk {
1632        file: &'a std::sync::Arc<std::fs::File>,
1633        offset: u64,
1634        len: usize,
1635        fallback: &'a [u8],
1636        keepalive: ExpertKeepalive,
1637    },
1638}
1639
1640/// One layer's stacked 256-expert tensor, raw GGUF quant bytes held HOST-RESIDENT.
1641///
1642/// EDGE-1: these bytes are NEVER uploaded at load (uploading 29.75GB would OOM a 24GB GPU —
1643/// this is BUG-4). Per token, only the 8 routed experts are staged H2D into a small GPU scratch.
1644///
1645/// ne = [in_f, out_f, n_expert]; the expert axis (ne[2]) is the slowest/highest-stride axis, so
1646/// expert `e` occupies the CONTIGUOUS byte block `bytes[e*expert_stride .. (e+1)*expert_stride]`.
1647///
1648/// THE 3D FIX: GpuTensor::load computes `row_bytes = raw.len()/ne[1]`, which for a stacked 3D
1649/// tensor ignores the 256-expert axis and is 256x too large (gate_exps -> 430080 instead of 1680).
1650/// load() here uses `row_bytes = raw.len() / (out_f * n_expert)` (= 1680 gate/up, 544 down).
1651#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1652pub struct ExpertLayout {
1653    pub offset: usize,
1654    pub len: usize,
1655    pub qtype: i32,
1656    pub row_bytes: usize,
1657}
1658
1659fn staged_expert_qtype(ty: GgmlType) -> Option<i32> {
1660    Some(match ty {
1661        GgmlType::Q8_0 => QT_Q8_0,
1662        GgmlType::Q2_K => QT_Q2_K,
1663        GgmlType::Q4_K => QT_Q4_K,
1664        GgmlType::Q6_K => QT_Q6_K,
1665        GgmlType::Q5_K => QT_Q5_K,
1666        GgmlType::Q3_K => QT_Q3_K,
1667        GgmlType::IQ4_XS => QT_IQ4_XS,
1668        GgmlType::IQ3_S => QT_IQ3_S,
1669        GgmlType::NVFP4 => QT_NVFP4,
1670        GgmlType::F32 => QT_F32,
1671        GgmlType::BF16 => QT_BF16,
1672        _ => return None,
1673    })
1674}
1675
1676fn staged_expert_row_bytes(ty: GgmlType, in_f: usize) -> Option<usize> {
1677    staged_expert_qtype(ty)?;
1678    let (block, type_size) = ty.block_and_type_size();
1679    assert_eq!(
1680        in_f as u64 % block,
1681        0,
1682        "expert row width {in_f} is not divisible by {ty:?} block {block}"
1683    );
1684    Some((in_f as u64 / block * type_size) as usize)
1685}
1686
1687fn find_expert_disk_strict(
1688    src: &dyn TensorSource,
1689    name: &str,
1690) -> Result<Option<DiskExtent>, Box<dyn std::error::Error>> {
1691    if let Some(extent) = src.find_expert_disk(name) {
1692        return Ok(Some(extent));
1693    }
1694    if src.find_expert_mmap(name).is_some() {
1695        return Err(std::io::Error::new(
1696            std::io::ErrorKind::InvalidData,
1697            format!(
1698                "expert tensor {name} exposes legacy find_expert_mmap without find_expert_disk; \
1699                 disk-backed expert loading requires a retained Arc<File>"
1700            ),
1701        )
1702        .into());
1703    }
1704    Ok(None)
1705}
1706
1707pub struct HostExps {
1708    pub bytes: HostBuf, // raw GGUF block bytes (host); per-token DMA src for the 8 routed exps
1709    /// SPILLING-PLAN §1.1: per-expert backing tier. `None` => the layer fits in one `bytes` store and
1710    /// every expert slices it (the unchanged in-RAM path). `Some` => per-expert split: the hottest
1711    /// experts are `Pinned` (Tier 1, fast async DMA), the rest `Mmap` into the GGUF (Tier 2, disk
1712    /// demand-fault). `expert_bytes(e)` resolves `tiers[e]` if present, else slices `bytes`.
1713    pub tiers: Option<Vec<HostBuf>>,
1714    pub qtype: i32,           // QT_Q6_K (gate/up) | QT_Q8_0 (down)
1715    pub in_f: usize,          // ne[0]   (gate/up = 2048, down = 512)
1716    pub out_f: usize,         // ne[1]   (gate/up = 512,  down = 2048)
1717    pub n_expert: usize,      // ne[2] = 256
1718    pub row_bytes: usize,     // raw.len()/(out_f*n_expert)  -> 1680 (gate/up) / 544 (down)
1719    pub expert_stride: usize, // raw.len()/n_expert          -> 860160 (gate/up) / 1114112 (down)
1720    /// Per-expert encoding metadata when experts in this projection do not share one dtype/layout.
1721    /// `None` preserves the existing uniform slab contract and every resident/fused fast path.
1722    /// `Some` routes through the per-expert staged/cache path, using each entry's qtype/row size.
1723    pub layouts: Option<Vec<ExpertLayout>>,
1724    /// Per-expert post-matmul macro-scale (ModelOpt NVFP4 `weight_scale_2`, one scalar per expert
1725    /// tensor). `None` => all 1.0 (GGUF experts; block scales carry everything). The MoE forward
1726    /// folds gate/up macros into the activation epilogue (gs/us) and the down macro into the
1727    /// per-expert accumulate weight.
1728    pub macros: Option<Vec<f32>>,
1729    /// Native block-E4M3 scale plane for a uniform stacked expert bank. Scales are
1730    /// `[expert, output_block, input_block]` in checkpoint order.
1731    pub fp8_blk: Option<HostExpertFp8BlockScales>,
1732}
1733
1734pub struct HostExpertFp8BlockScales {
1735    pub scales: Vec<f32>,
1736    pub rows: usize,
1737    pub cols: usize,
1738    pub expert_stride: usize,
1739}
1740
1741impl HostExps {
1742    /// Load a stacked 3D expert tensor, keeping its quant bytes on the HOST. `e` supplies the CUDA
1743    /// context for the optional pinned allocation (§C.1). Default storage is pageable `Vec<u8>`
1744    /// (identical to the prior behavior); pinned is chosen when MEMRA_MOE_PINNED or MEMRA_MOE_CACHE is set.
1745    pub fn load(e: &Engine, g: &GgufFile, name: &str) -> Result<Self, Box<dyn std::error::Error>> {
1746        Self::load_stacked_from_source(e, &GgufSource(g), name)
1747    }
1748
1749    /// Load a STACKED 3D expert tensor (`ne=[in_f,out_f,n_expert]`) from any source. GGUF stores the
1750    /// experts this way; the source returns the same mmap bytes (`GgufSource::find` == `tensor_data`),
1751    /// so the GGUF path is byte-identical to the prior direct-`GgufFile` loader. (Safetensors stores N
1752    /// 2D tensors instead — those go through `load_from_source`, which gathers them.)
1753    /// Row-range variant for FUSED stacked tensors (gemma4 ffn_gate_up_exps: gate = rows
1754    /// [0,ff), up = [ff,2ff) per expert — llama-graph view convention). Copies only the range.
1755    pub fn load_stacked_split_from_source(
1756        e: &Engine,
1757        src: &dyn TensorSource,
1758        name: &str,
1759        row0: usize,
1760        row1: usize,
1761    ) -> Result<Self, Box<dyn std::error::Error>> {
1762        let t = src
1763            .find(name)
1764            .unwrap_or_else(|| panic!("missing exps tensor {name}"));
1765        assert_eq!(t.ne.len(), 3, "{name} is not 3D (ne={:?})", t.ne);
1766        let qtype = match t.ggml_type {
1767            GgmlType::Q8_0 => QT_Q8_0,
1768            GgmlType::Q4_K => QT_Q4_K,
1769            GgmlType::Q6_K => QT_Q6_K,
1770            GgmlType::Q5_K => QT_Q5_K,
1771            GgmlType::Q3_K => QT_Q3_K,
1772            GgmlType::IQ4_XS => QT_IQ4_XS,
1773            GgmlType::IQ3_S => QT_IQ3_S,
1774            GgmlType::NVFP4 => QT_NVFP4,
1775            GgmlType::Q4_0 => QT_Q4_0,
1776            other => panic!("exps {name} unsupported quant {other:?}"),
1777        };
1778        let raw: &[u8] = &t.bytes;
1779        let in_f = t.ne[0] as usize;
1780        let out_full = t.ne[1] as usize;
1781        let n_expert = t.ne[2] as usize;
1782        let full_stride = raw.len() / n_expert;
1783        let row_bytes = raw.len() / (out_full * n_expert);
1784        assert_eq!(full_stride, out_full * row_bytes, "{name} stride mismatch");
1785        let out_f = row1 - row0;
1786        let expert_stride = out_f * row_bytes;
1787        let mut buf = vec![0u8; n_expert * expert_stride];
1788        for ex in 0..n_expert {
1789            let s0 = ex * full_stride + row0 * row_bytes;
1790            buf[ex * expert_stride..(ex + 1) * expert_stride]
1791                .copy_from_slice(&raw[s0..s0 + expert_stride]);
1792        }
1793        let pinned = std::env::var("MEMRA_MOE_PINNED").is_ok()
1794            || std::env::var("MEMRA_MOE_CACHE").as_deref() != Ok("0");
1795        let bytes = if pinned {
1796            let mut pn = unsafe { e.ctx().alloc_pinned::<u8>(buf.len())? };
1797            {
1798                let dst = pn.as_mut_slice()?;
1799                dst.copy_from_slice(&buf);
1800            }
1801            let base = pn.as_ptr()?;
1802            let len = buf.len();
1803            HostBuf::Pinned {
1804                slice: std::sync::Arc::new(pn),
1805                base,
1806                len,
1807            }
1808        } else {
1809            HostBuf::Paged(buf)
1810        };
1811        Ok(HostExps {
1812            bytes,
1813            tiers: None,
1814            qtype,
1815            in_f,
1816            out_f,
1817            n_expert,
1818            row_bytes,
1819            expert_stride,
1820            layouts: None,
1821            macros: None,
1822            fp8_blk: None,
1823        })
1824    }
1825
1826    /// Stacked per-expert macro-scale sidecar: `blk.N.ffn_{proj}_exps.scale` f32 [n_expert]
1827    /// (the qwen3.6 NVFP4 converter emits one per stacked expert tensor — compressed-tensors
1828    /// global scales, inverted to multipliers). Absent (every k-quant GGUF) => None.
1829    /// NOTE gemma4 consumes ffn_down_exps.scale through its OWN router-fold (Gemma4MoeBits) —
1830    /// its MoE forward does not read HostExps::macros, so a Some here is inert there.
1831    fn stacked_macros(src: &dyn TensorSource, name: &str) -> Option<Vec<f32>> {
1832        let stem = name.strip_suffix(".weight")?;
1833        let sv = src.find(&format!("{stem}.scale"))?;
1834        if sv.ggml_type != GgmlType::F32 {
1835            return None;
1836        }
1837        let macros: Vec<f32> = sv
1838            .bytes
1839            .chunks_exact(4)
1840            .map(|c| f32::from_le_bytes(c.try_into().unwrap()))
1841            .collect();
1842        if macros.iter().all(|&m| m == 1.0) {
1843            None
1844        } else {
1845            Some(macros)
1846        }
1847    }
1848
1849    /// STACKED NVFP4-NATIVE ARM (Step-3.7-Flash-NVFP4 class, 2026-08-20): the checkpoint stores
1850    /// each routed projection as ONE stacked modelopt tensor `[E, out, in/2]` (not per-expert 2-D
1851    /// tensors — that class rides PATH B in `load_from_source`). Repack per expert into the GGUF
1852    /// 36B-block layout the staged qmatvec decodes, streaming into the same `.memra-repack`
1853    /// disk-cache tier PATH B uses (peak RAM = one expert), and mmap the cache. Per-expert
1854    /// `weight_scale_2` macros go to `macros` — the MoE forward folds them post-matmul; dropping
1855    /// them (~1e-5..1e-4 in the official artifact) produces garbage.
1856    fn load_nvfp4_stacked_native(
1857        src: &dyn TensorSource,
1858        name: &str,
1859    ) -> Result<Option<Self>, Box<dyn std::error::Error>> {
1860        let Some(bank) = src.find_nvfp4_stacked_native(name) else {
1861            return Ok(None);
1862        };
1863        let (n_expert, out_f, in_f) = (bank.n_expert, bank.out_f, bank.in_f);
1864        if in_f % 64 != 0 {
1865            return Err(
1866                format!("{name} stacked NVFP4 in_features {in_f} is not 64-aligned").into(),
1867            );
1868        }
1869        let row_bytes = in_f / 64 * 36;
1870        let expert_stride = out_f * row_bytes;
1871        let total = n_expert * expert_stride;
1872        let code_stride = out_f * in_f / 2;
1873        let scale_stride = out_f * in_f / 16;
1874        let macros = bank.macros.clone();
1875        let cache_path = if let Some(dir) = src.st_dir() {
1876            let cache_dir = dir.join(".memra-repack");
1877            ensure_repack_cache_dir(&cache_dir)?;
1878            Some(cache_dir.join(format!(
1879                "{}-stacked-{n_expert}x{out_f}x{in_f}{}.nvfp4",
1880                name.replace(['.', '/'], "-"),
1881                src.nvfp4_cache_tag()
1882            )))
1883        } else {
1884            None
1885        };
1886        let bytes = if let Some(cache) = cache_path.as_ref() {
1887            let fresh = repack_cache_is_fresh(cache, total);
1888            if !fresh {
1889                write_repack_cache(cache, |out| {
1890                    for expert in 0..n_expert {
1891                        use std::io::Write;
1892                        out.write_all(&memra_gguf::nvfp4_repack::repack_modelopt_to_gguf(
1893                            &bank.codes[expert * code_stride..(expert + 1) * code_stride],
1894                            &bank.scales[expert * scale_stride..(expert + 1) * scale_stride],
1895                            out_f,
1896                            in_f,
1897                        ))?;
1898                    }
1899                    Ok(())
1900                })?;
1901            }
1902            let file = std::sync::Arc::new(open_repack_cache(cache, false)?);
1903            let map = unsafe { memmap2::Mmap::map(file.as_ref())? };
1904            assert_eq!(map.len(), total, "repack cache {cache:?} size mismatch");
1905            let _ = memra_gguf::source::apply_expert_mmap_advice(&map);
1906            memra_gguf::source::populate_expert_slab(&file, total, name);
1907            HostBuf::Mmap {
1908                map: std::sync::Arc::new(map),
1909                file,
1910                off: 0,
1911                len: total,
1912            }
1913        } else {
1914            let mut buf: Vec<u8> = Vec::with_capacity(total);
1915            for expert in 0..n_expert {
1916                buf.extend_from_slice(&memra_gguf::nvfp4_repack::repack_modelopt_to_gguf(
1917                    &bank.codes[expert * code_stride..(expert + 1) * code_stride],
1918                    &bank.scales[expert * scale_stride..(expert + 1) * scale_stride],
1919                    out_f,
1920                    in_f,
1921                ));
1922            }
1923            assert_eq!(buf.len(), total);
1924            HostBuf::Paged(buf)
1925        };
1926        let all_one = macros.iter().all(|&value| value == 1.0);
1927        Ok(Some(HostExps {
1928            bytes,
1929            tiers: None,
1930            qtype: QT_NVFP4,
1931            in_f,
1932            out_f,
1933            n_expert,
1934            row_bytes,
1935            expert_stride,
1936            layouts: None,
1937            macros: if all_one { None } else { Some(macros) },
1938            fp8_blk: None,
1939        }))
1940    }
1941
1942    fn load_fp8_stacked_native_with_policy(
1943        src: &dyn TensorSource,
1944        name: &str,
1945        native_enabled: bool,
1946    ) -> Result<Option<Self>, Box<dyn std::error::Error>> {
1947        let Some(f8) = src.find_fp8_stacked_native(name) else {
1948            return Ok(None);
1949        };
1950        if f8.scale_rows != f8.out_f.div_ceil(128) || f8.scale_cols != f8.in_f.div_ceil(128) {
1951            return Err(format!(
1952                "{name} FP8 scale geometry mismatch: got {}x{}, expected {}x{}",
1953                f8.scale_rows,
1954                f8.scale_cols,
1955                f8.out_f.div_ceil(128),
1956                f8.in_f.div_ceil(128)
1957            )
1958            .into());
1959        }
1960        if f8.bytes.iter().any(|code| code & 0x7f == 0x7f) {
1961            return Err(format!("{name} FP8 code slab contains non-finite E4M3 values").into());
1962        }
1963        let scale_stride = f8.scale_rows * f8.scale_cols;
1964        if !native_enabled {
1965            if f8.in_f % 32 != 0 {
1966                return Err(format!(
1967                    "{name} FP8 rollback requires an input width divisible by 32, got {}",
1968                    f8.in_f
1969                )
1970                .into());
1971            }
1972            let mut q8 = Vec::new();
1973            for expert in 0..f8.n_expert {
1974                let mut data = Vec::with_capacity(f8.out_f * f8.in_f);
1975                for output in 0..f8.out_f {
1976                    let row = (expert * f8.out_f + output) * f8.in_f;
1977                    for input in 0..f8.in_f {
1978                        let scale = f8.scales
1979                            [expert * scale_stride + (output / 128) * f8.scale_cols + input / 128];
1980                        data.push(
1981                            memra_gguf::nvfp4_repack::fp8_e4m3_to_f32(f8.bytes[row + input])
1982                                * scale,
1983                        );
1984                    }
1985                }
1986                q8.extend_from_slice(&memra_gguf::nvfp4_repack::f32_to_q8_0(&data));
1987            }
1988            let row_bytes = f8.in_f / 32 * 34;
1989            let expert_stride = f8.out_f * row_bytes;
1990            assert_eq!(q8.len(), f8.n_expert * expert_stride);
1991            return Ok(Some(HostExps {
1992                bytes: HostBuf::Paged(q8),
1993                tiers: None,
1994                qtype: QT_Q8_0,
1995                in_f: f8.in_f,
1996                out_f: f8.out_f,
1997                n_expert: f8.n_expert,
1998                row_bytes,
1999                expert_stride,
2000                layouts: None,
2001                macros: None,
2002                fp8_blk: None,
2003            }));
2004        }
2005
2006        assert_eq!(
2007            f8.bytes.len(),
2008            f8.n_expert * f8.out_f * f8.in_f,
2009            "{name} FP8 code slab length mismatch"
2010        );
2011        assert_eq!(
2012            f8.scales.len(),
2013            f8.n_expert * scale_stride,
2014            "{name} FP8 scale slab length mismatch"
2015        );
2016        let expert_stride = f8.out_f * f8.in_f;
2017        let bytes = match find_expert_disk_strict(src, name)? {
2018            Some(extent) => {
2019                if extent.len != f8.bytes.len() {
2020                    return Err(format!(
2021                        "{name} FP8 mmap length mismatch: extent={} tensor={}",
2022                        extent.len,
2023                        f8.bytes.len()
2024                    )
2025                    .into());
2026                }
2027                let off = usize::try_from(extent.offset).map_err(|_| {
2028                    format!(
2029                        "{name} FP8 mmap offset {} does not fit usize",
2030                        extent.offset
2031                    )
2032                })?;
2033                HostBuf::Mmap {
2034                    map: extent.map,
2035                    file: extent.file,
2036                    off,
2037                    len: extent.len,
2038                }
2039            }
2040            None => HostBuf::Paged(f8.bytes.to_vec()),
2041        };
2042        Ok(Some(HostExps {
2043            bytes,
2044            tiers: None,
2045            qtype: crate::QT_F8_E4M3_BLK,
2046            in_f: f8.in_f,
2047            out_f: f8.out_f,
2048            n_expert: f8.n_expert,
2049            row_bytes: f8.in_f,
2050            expert_stride,
2051            layouts: None,
2052            macros: None,
2053            fp8_blk: Some(HostExpertFp8BlockScales {
2054                scales: f8.scales,
2055                rows: f8.scale_rows,
2056                cols: f8.scale_cols,
2057                expert_stride: scale_stride,
2058            }),
2059        }))
2060    }
2061
2062    pub fn load_stacked_from_source(
2063        e: &Engine,
2064        src: &dyn TensorSource,
2065        name: &str,
2066    ) -> Result<Self, Box<dyn std::error::Error>> {
2067        if let Some(exps) = Self::load_fp8_stacked_native_with_policy(
2068            src,
2069            name,
2070            crate::fp8_ffi::st_e4m3_blk_enabled(),
2071        )? {
2072            return Ok(exps);
2073        }
2074        if let Some(exps) = Self::load_nvfp4_stacked_native(src, name)? {
2075            return Ok(exps);
2076        }
2077
2078        let t = src
2079            .find(name)
2080            .unwrap_or_else(|| panic!("missing exps tensor {name}"));
2081        assert_eq!(
2082            t.ne.len(),
2083            3,
2084            "{name} is not a 3D stacked-expert tensor (ne={:?})",
2085            t.ne
2086        );
2087        // MMAP-BACKED SPILL TIER (Hy3 repack dir, 2026-07-09): when the source's on-disk layout IS
2088        // already the engine's expert layout (one expert-axis-slowest slab file per (layer, proj),
2089        // the transcoder's contract), back the HostExps with `HostBuf::Mmap` directly — ZERO host
2090        // copy. The default copy path below would pin/allocate the WHOLE stacked slab (80.5 GB for
2091        // Hy3-REAP50 on a 60 GB host = the M3 first-load OOM class); the mmap tier instead lets the
2092        // page cache carry the hot expert mass (RAM tier) and demand-faults the overflow from NVMe,
2093        // exactly like the proven M3 `.memra-repack` path (model.rs NVFP4 disk arm). Bit-identity:
2094        // `expert_bytes(e)` slices the same on-disk bytes the copy would have staged. The SLRU VRAM
2095        // cache stacks on top unchanged. The configured whole-map advice is applied at source open.
2096        if let Some(DiskExtent {
2097            map,
2098            file,
2099            offset,
2100            len,
2101        }) = find_expert_disk_strict(src, name)?
2102        {
2103            let off = usize::try_from(offset)
2104                .map_err(|_| format!("{name} disk offset {offset} does not fit usize"))?;
2105            let qtype = match t.ggml_type {
2106                GgmlType::Q8_0 => QT_Q8_0,
2107                GgmlType::Q4_K => QT_Q4_K,
2108                GgmlType::Q6_K => QT_Q6_K,
2109                GgmlType::Q5_K => QT_Q5_K,
2110                GgmlType::Q3_K => QT_Q3_K,
2111                GgmlType::IQ4_XS => QT_IQ4_XS,
2112                GgmlType::IQ3_S => QT_IQ3_S,
2113                GgmlType::NVFP4 => QT_NVFP4,
2114                GgmlType::Q4_0 => QT_Q4_0,
2115                other => panic!("exps {name} unsupported quant {other:?}"),
2116            };
2117            let in_f = t.ne[0] as usize;
2118            let out_f = t.ne[1] as usize;
2119            let n_expert = t.ne[2] as usize;
2120            let expert_stride = len / n_expert;
2121            let row_bytes = len / (out_f * n_expert);
2122            assert_eq!(
2123                expert_stride,
2124                out_f * row_bytes,
2125                "{name} stride mismatch: stride={expert_stride} out_f={out_f} row_bytes={row_bytes}"
2126            );
2127            assert_eq!(
2128                len,
2129                n_expert * expert_stride,
2130                "{name} mmap len != n_expert*stride"
2131            );
2132            return Ok(HostExps {
2133                bytes: HostBuf::Mmap {
2134                    map,
2135                    file,
2136                    off,
2137                    len,
2138                },
2139                tiers: None,
2140                qtype,
2141                in_f,
2142                out_f,
2143                n_expert,
2144                row_bytes,
2145                expert_stride,
2146                layouts: None,
2147                macros: Self::stacked_macros(src, name),
2148                fp8_blk: None,
2149            });
2150        }
2151        let raw: &[u8] = &t.bytes;
2152        // All quant types the staged-expert qmatvec can decode (dp4a-fast or Stage-A f32).
2153        let qtype = match t.ggml_type {
2154            GgmlType::Q8_0 => QT_Q8_0,
2155            GgmlType::Q4_K => QT_Q4_K,
2156            GgmlType::Q6_K => QT_Q6_K,
2157            GgmlType::Q5_K => QT_Q5_K,
2158            GgmlType::Q3_K => QT_Q3_K,
2159            GgmlType::IQ4_XS => QT_IQ4_XS,
2160            GgmlType::IQ3_S => QT_IQ3_S,
2161            GgmlType::NVFP4 => QT_NVFP4,
2162            GgmlType::Q4_0 => QT_Q4_0,
2163            other => panic!("exps {name} unsupported quant {other:?}"),
2164        };
2165        let in_f = t.ne[0] as usize;
2166        let out_f = t.ne[1] as usize;
2167        let n_expert = t.ne[2] as usize;
2168        // VERIFIED: gate/up Q6_K total/256 = 860160; row = total/(512*256) = 1680.
2169        //           down  Q8_0 total/256 = 1114112; row = total/(2048*256) = 544.
2170        let expert_stride = raw.len() / n_expert;
2171        let row_bytes = raw.len() / (out_f * n_expert);
2172        // sanity: expert_stride must equal out_f * row_bytes exactly (catches a dim mixup)
2173        assert_eq!(
2174            expert_stride,
2175            out_f * row_bytes,
2176            "{name} stride mismatch: stride={expert_stride} out_f={out_f} row_bytes={row_bytes}"
2177        );
2178
2179        let pinned = std::env::var("MEMRA_MOE_PINNED").is_ok()
2180            || std::env::var("MEMRA_MOE_CACHE").as_deref() != Ok("0");
2181        let bytes = if pinned {
2182            // alloc pinned host memory, copy the GGUF block bytes in once, cache the base pointer.
2183            let mut p = unsafe { e.ctx().alloc_pinned::<u8>(raw.len())? };
2184            {
2185                let dst = p.as_mut_slice()?;
2186                dst.copy_from_slice(raw);
2187            }
2188            let base = p.as_ptr()?; // syncs once here at load; stable afterward
2189            let len = raw.len();
2190            HostBuf::Pinned {
2191                slice: std::sync::Arc::new(p),
2192                base,
2193                len,
2194            }
2195        } else {
2196            HostBuf::Paged(raw.to_vec())
2197        };
2198        Ok(HostExps {
2199            bytes,
2200            tiers: None,
2201            qtype,
2202            in_f,
2203            out_f,
2204            n_expert,
2205            row_bytes,
2206            expert_stride,
2207            layouts: None,
2208            macros: Self::stacked_macros(src, name),
2209            fp8_blk: None,
2210        })
2211    }
2212
2213    /// SPILLING-PLAN §1.1, §2 step 4: load a stacked 3D expert tensor with a PER-EXPERT tier split.
2214    /// Under `MEMRA_SPILL_DISK`, the hottest experts (greedy in expert order, until the shared pinned
2215    /// budget in `ctx` is exhausted) get `HostBuf::Pinned` (Tier 1, fast async DMA); every remaining
2216    /// expert is `HostBuf::Mmap` into the GGUF (Tier 2, demand-faulted from disk on first H2D). The
2217    /// resulting bytes are bit-identical to the in-RAM path either way — `qmatvec_view` is untouched.
2218    ///
2219    /// `ctx.file_map` is ONE shared `MAP_SHARED` mmap of the whole GGUF (`Arc`-cloned per spilled
2220    /// expert), so the 120 expert tensors of a 40-layer MoE never open the file more than once.
2221    pub fn load_tiered(
2222        e: &Engine,
2223        g: &GgufFile,
2224        name: &str,
2225        ctx: &mut crate::spill::SpillCtx,
2226    ) -> Result<Self, Box<dyn std::error::Error>> {
2227        let t = g
2228            .find(name)
2229            .unwrap_or_else(|| panic!("missing exps tensor {name}"));
2230        assert_eq!(
2231            t.ne.len(),
2232            3,
2233            "{name} is not a 3D stacked-expert tensor (ne={:?})",
2234            t.ne
2235        );
2236        let raw = g.tensor_data(t);
2237        let qtype = match t.ggml_type {
2238            GgmlType::Q8_0 => QT_Q8_0,
2239            GgmlType::Q4_K => QT_Q4_K,
2240            GgmlType::Q6_K => QT_Q6_K,
2241            GgmlType::Q5_K => QT_Q5_K,
2242            GgmlType::Q3_K => QT_Q3_K,
2243            GgmlType::IQ4_XS => QT_IQ4_XS,
2244            GgmlType::IQ3_S => QT_IQ3_S,
2245            GgmlType::NVFP4 => QT_NVFP4,
2246            GgmlType::Q4_0 => QT_Q4_0,
2247            other => panic!("exps {name} unsupported quant {other:?}"),
2248        };
2249        let in_f = t.ne[0] as usize;
2250        let out_f = t.ne[1] as usize;
2251        let n_expert = t.ne[2] as usize;
2252        let expert_stride = raw.len() / n_expert;
2253        let row_bytes = raw.len() / (out_f * n_expert);
2254        assert_eq!(
2255            expert_stride,
2256            out_f * row_bytes,
2257            "{name} stride mismatch: stride={expert_stride} out_f={out_f} row_bytes={row_bytes}"
2258        );
2259
2260        // Byte offset of this tensor's data (start of expert 0) WITHIN ITS OWN SHARD's file; each
2261        // expert is the next `expert_stride` bytes. The `Mmap` arm slices `ctx.file_maps[t.shard]`
2262        // at these offsets — a split model's offsets are per-shard, not global.
2263        let (file_start, _file_end) = g.tensor_file_range(t);
2264
2265        // Per-expert tier decision under the shared running budget. `bytes` keeps a 0-byte sentinel
2266        // (`Paged(empty)`) since every read now goes through `tiers`.
2267        let mut tiers = Vec::with_capacity(n_expert);
2268        for ex in 0..n_expert {
2269            let blk = &raw[ex * expert_stride..(ex + 1) * expert_stride];
2270            let file_off = file_start + ex * expert_stride;
2271            tiers.push(crate::spill::place_expert(ctx, e, blk, file_off, t.shard)?);
2272        }
2273        Ok(HostExps {
2274            bytes: HostBuf::Paged(Vec::new()), // unused when `tiers` is Some
2275            tiers: Some(tiers),
2276            qtype,
2277            in_f,
2278            out_f,
2279            n_expert,
2280            row_bytes,
2281            expert_stride,
2282            layouts: None,
2283            macros: Self::stacked_macros(&GgufSource(g), name),
2284            fp8_blk: None,
2285        })
2286    }
2287
2288    /// MoE expert GATHER from a `TensorSource` (the safetensors path; ST-MOE-PLAN §1.3). GGUF stacks
2289    /// all experts into ONE 3D tensor; HF stores them as N separate 2D tensors
2290    /// `model.layers.{il}.mlp.experts.{e}.{gate,up,down}_proj.weight`. `find` returns `None` for the
2291    /// ggml `*_exps` name on purpose, so the experts are gathered out-of-band here.
2292    ///
2293    /// PATH A (load-time only, no quantize): each HF 2D expert tensor is dequantized to f32 and the
2294    /// per-expert blocks are concatenated expert-axis-slowest into ONE contiguous buffer — exactly the
2295    /// layout `expert_bytes(e)` slices and the staged `qmatvec_view` (qtype=QT_F32) reads. The same
2296    /// `expert_stride == out_f*row_bytes` invariant as the GGUF path is asserted at the end.
2297    ///
2298    /// `ggml_exps_name` is `blk.{il}.ffn_{gate,up,down}_exps.weight`; it is split to recover `il` and
2299    /// the proj. `n_expert` comes from `cfg.moe`. The HF per-expert literal `mlp.experts.{e}.{p}_proj`
2300    /// is the qwen3moe / olmoe layout (a future arch with `block_sparse_moe.experts.*` would need a
2301    /// branch in `hf_expert_name`).
2302    pub fn load_from_source(
2303        e: &Engine,
2304        src: &dyn TensorSource,
2305        ggml_exps_name: &str,
2306        n_expert: usize,
2307    ) -> Result<Self, Box<dyn std::error::Error>> {
2308        // Recover il + proj from `blk.{il}.ffn_{gate,up,down}_exps.weight`.
2309        let rest = ggml_exps_name
2310            .strip_prefix("blk.")
2311            .unwrap_or_else(|| panic!("not a blk.* name: {ggml_exps_name}"));
2312        let (il_s, suffix) = rest.split_once('.').unwrap();
2313        let il: u32 = il_s.parse().unwrap();
2314        let proj = match suffix {
2315            "ffn_gate_exps.weight" => "gate",
2316            "ffn_up_exps.weight" => "up",
2317            "ffn_down_exps.weight" => "down",
2318            other => panic!("not a *_exps suffix: {other}"),
2319        };
2320
2321        // A mixed-precision safetensors/repack source exposes experts as separate 2D tensors.
2322        // Detect a dtype/layout change before the uniform gather paths normalize the whole layer
2323        // to one encoding. Uniform checkpoints take the unchanged optimized path below.
2324        let mut signatures = Vec::with_capacity(n_expert);
2325        let active = src.active_experts(il);
2326        for ex in 0..n_expert {
2327            if active.is_some_and(|mask| !mask[ex]) {
2328                signatures.push((i32::MIN, 0));
2329                continue;
2330            }
2331            let name = format!("blk.{il}.ffn_{proj}_exps.{ex}.weight");
2332            if let Some(nv) = src.find_nvfp4_native(&name) {
2333                signatures.push((QT_NVFP4, nv.in_f / 64 * 36));
2334            } else {
2335                let v = src
2336                    .find(&name)
2337                    .unwrap_or_else(|| panic!("missing expert tensor {name}"));
2338                let in_f = v.ne[0] as usize;
2339                signatures.push(match staged_expert_row_bytes(v.ggml_type, in_f) {
2340                    Some(row_bytes) => (staged_expert_qtype(v.ggml_type).unwrap(), row_bytes),
2341                    None => (QT_F32, in_f * 4),
2342                });
2343            }
2344        }
2345        let mixed_layout = signatures.windows(2).any(|pair| pair[0] != pair[1]);
2346        if src.preserve_expert_encodings()
2347            && !mixed_layout
2348            && let Some(uniform) = Self::load_uniform_mmap_from_source(src, il, proj, n_expert)?
2349        {
2350            return Ok(uniform);
2351        }
2352        if src.preserve_expert_encodings() || mixed_layout {
2353            return Self::load_mixed_from_source(src, il, proj, n_expert);
2354        }
2355
2356        // PATH B (NVFP4-NATIVE GATHER, 2026-07-05): when the source exposes the experts as packed
2357        // ModelOpt/Reza NVFP4 (find_nvfp4_native), keep them QUANTIZED — repack each expert's
2358        // modelopt bytes to the GGUF 36B-block layout the staged qmatvec decodes, and concatenate.
2359        // No f32 blow-up: a 129GB checkpoint gathers to ~the same bytes instead of ~8x (which is
2360        // what makes MiniMax-M3 REAP50 loadable on a 60GB-RAM host at all, with spill on top).
2361        // Per-expert `weight_scale_2` macros go to `macros` (folded post-matmul by the MoE forward).
2362        {
2363            let name0 = format!("blk.{il}.ffn_{proj}_exps.0.weight");
2364            if let Some(nv0) = src.find_nvfp4_native(&name0) {
2365                let (in_f, out_f) = (nv0.in_f, nv0.out_f);
2366                let row_bytes = in_f / 64 * 36;
2367                let expert_stride = out_f * row_bytes;
2368                // ST DISK TIER (2026-07-06, the MiniMax OOM fix): when the total expert bytes
2369                // exceed host RAM (M3 REAP50 = 122GB repacked on a 60GB host, first-load host-OOM
2370                // at layer ~24), repack each layer ONCE into an on-disk cache file next to the
2371                // checkpoint and mmap it (HostBuf::Mmap, MAP_SHARED no-populate — the same tier-2
2372                // mechanism the GGUF spill path uses). Reloads hit the cache (size-checked), pay
2373                // zero repack. MEMRA_ST_REPACK_DISK=0 forces the old in-RAM gather.
2374                let disk = std::env::var("MEMRA_ST_REPACK_DISK")
2375                    .map(|v| v != "0")
2376                    .unwrap_or(true)
2377                    && src.st_dir().is_some();
2378                let cache_path = if let Some(dir) = src.st_dir() {
2379                    let cache_dir = dir.join(".memra-repack");
2380                    ensure_repack_cache_dir(&cache_dir)?;
2381                    Some(cache_dir.join(format!(
2382                        "blk{il}-{proj}-{n_expert}x{out_f}x{in_f}{}.nvfp4",
2383                        src.nvfp4_cache_tag()
2384                    )))
2385                } else {
2386                    None
2387                };
2388                let total = n_expert * expert_stride;
2389                let mut macros = vec![1.0f32; n_expert];
2390                let read_macros = |macros: &mut Vec<f32>| {
2391                    #[allow(clippy::needless_range_loop)]
2392                    // allow: the explicit index loop keeps the offset arithmetic visible and aligned with the device-side indexing
2393                    for ex in 0..n_expert {
2394                        let stem = format!("blk.{il}.ffn_{proj}_exps.{ex}");
2395                        if let Some(sv) = src.find(&format!("{stem}.scale")) {
2396                            macros[ex] = f32::from_le_bytes(sv.bytes[..4].try_into().unwrap());
2397                        }
2398                    }
2399                };
2400                let bytes = if disk {
2401                    let cp = cache_path.as_ref().unwrap();
2402                    let fresh = repack_cache_is_fresh(cp, total);
2403                    if !fresh {
2404                        // stream one expert at a time to disk — peak RAM = one expert (~8MB)
2405                        write_repack_cache(cp, |out| {
2406                            for ex in 0..n_expert {
2407                                use std::io::Write;
2408                                let name = format!("blk.{il}.ffn_{proj}_exps.{ex}.weight");
2409                                let nv = src.find_nvfp4_native(&name).unwrap_or_else(|| {
2410                                    panic!("expert {name} lost NVFP4-native mid-gather")
2411                                });
2412                                assert_eq!(
2413                                    (nv.in_f, nv.out_f),
2414                                    (in_f, out_f),
2415                                    "expert {ex} dims ({},{}) != expert 0 ({in_f},{out_f})",
2416                                    nv.in_f,
2417                                    nv.out_f
2418                                );
2419                                out.write_all(&memra_gguf::nvfp4_repack::repack_modelopt_to_gguf(
2420                                    nv.wbytes, &nv.wscale, out_f, in_f,
2421                                ))?;
2422                            }
2423                            Ok(())
2424                        })?;
2425                    }
2426                    read_macros(&mut macros);
2427                    let file = std::sync::Arc::new(open_repack_cache(cp, false)?);
2428                    let map = unsafe { memmap2::Mmap::map(file.as_ref())? };
2429                    assert_eq!(map.len(), total, "repack cache {cp:?} size mismatch");
2430                    // Default random preserves the original policy; normal lets Linux readahead
2431                    // within each multi-megabyte expert on the spill-bound path.
2432                    let _ = memra_gguf::source::apply_expert_mmap_advice(&map);
2433                    memra_gguf::source::populate_expert_slab(
2434                        &file,
2435                        total,
2436                        &format!("blk{il}-{proj}"),
2437                    );
2438                    let map = std::sync::Arc::new(map);
2439                    // ST PINNED TIER (2026-07-07, the M3 1.5-tok/s lever): mmap-only backing makes
2440                    // every SLRU miss a page-cache (or NVMe) synchronous read into the H2D copy.
2441                    // Pin as many experts as the live budget allows (same MemBudget probe + 0.6
2442                    // MemAvailable cap as the GGUF spill tier) — pinned pages upload via true
2443                    // async DMA at full PCIe. Budget is GLOBAL across layers (first-come: earlier
2444                    // layers pin first; routing is roughly uniform so early-layer bias is benign).
2445                    // MEMRA_ST_PINNED=0 disables (pure-mmap, the 2026-07-06 behavior).
2446                    // DEFAULT OFF (2026-07-07 measured): with a 122GB expert set on 60GB RAM,
2447                    // pinning 26GB EVICTED the page cache backing the mmap tier — every unpinned
2448                    // expert faulted cold from NVMe and gen fell 1.5 -> 0.05 tok/s (30x WORSE).
2449                    // Pinning only pays when (total - pinned) fits page cache; here it never can.
2450                    // MEMRA_ST_PINNED=1 opt-in for fits-in-RAM checkpoints (e.g. REAP-heavier cuts).
2451                    let tiers = if std::env::var("MEMRA_ST_PINNED")
2452                        .map(|v| v == "1")
2453                        .unwrap_or(false)
2454                    {
2455                        static PIN_BUDGET: std::sync::OnceLock<std::sync::Mutex<usize>> =
2456                            std::sync::OnceLock::new();
2457                        let budget = PIN_BUDGET.get_or_init(|| {
2458                            let b = crate::spill::MemBudget::probe(e)
2459                                .map(|b| b.free_pinnable_ram)
2460                                .unwrap_or(0);
2461                            eprintln!("[st-spill] free_pinnable_ram={} MiB", b >> 20);
2462                            std::sync::Mutex::new(b)
2463                        });
2464                        let mut rem = budget.lock().unwrap();
2465                        // ONE pinned slab per file prefix (n_pin experts contiguous): 1 alloc +
2466                        // 1 bulk copy instead of n_pin small allocs (per-expert cudaHostAllocs
2467                        // stalled the 122GB M3 load >10min).
2468                        let n_pin = (*rem / expert_stride).min(n_expert);
2469                        if n_pin == 0 {
2470                            None
2471                        } else {
2472                            let slab_len = n_pin * expert_stride;
2473                            let mut pn = unsafe { e.ctx().alloc_pinned::<u8>(slab_len)? };
2474                            {
2475                                let dst = pn.as_mut_slice()?;
2476                                dst.copy_from_slice(&map[..slab_len]);
2477                            }
2478                            let base = pn.as_ptr()?;
2479                            *rem -= slab_len;
2480                            let slab = std::sync::Arc::new(HostBuf::Pinned {
2481                                slice: std::sync::Arc::new(pn),
2482                                base,
2483                                len: slab_len,
2484                            });
2485                            let mut tiers: Vec<HostBuf> = Vec::with_capacity(n_expert);
2486                            for ex in 0..n_expert {
2487                                let off = ex * expert_stride;
2488                                if ex < n_pin {
2489                                    tiers.push(HostBuf::PinnedAlias {
2490                                        owner: slab.clone(),
2491                                        base: unsafe { base.add(off) },
2492                                        len: expert_stride,
2493                                    });
2494                                } else {
2495                                    tiers.push(HostBuf::Mmap {
2496                                        map: map.clone(),
2497                                        file: file.clone(),
2498                                        off,
2499                                        len: expert_stride,
2500                                    });
2501                                }
2502                            }
2503                            Some(tiers)
2504                        }
2505                    } else {
2506                        None
2507                    };
2508                    if let Some(tiers) = tiers {
2509                        let all_one = macros.iter().all(|&m| m == 1.0);
2510                        return Ok(HostExps {
2511                            bytes: HostBuf::Mmap {
2512                                map,
2513                                file,
2514                                off: 0,
2515                                len: total,
2516                            },
2517                            tiers: Some(tiers),
2518                            qtype: QT_NVFP4,
2519                            in_f,
2520                            out_f,
2521                            n_expert,
2522                            row_bytes,
2523                            expert_stride,
2524                            layouts: None,
2525                            macros: if all_one { None } else { Some(macros) },
2526                            fp8_blk: None,
2527                        });
2528                    }
2529                    HostBuf::Mmap {
2530                        map,
2531                        file,
2532                        off: 0,
2533                        len: total,
2534                    }
2535                } else {
2536                    let mut buf: Vec<u8> = Vec::with_capacity(total);
2537                    for ex in 0..n_expert {
2538                        let name = format!("blk.{il}.ffn_{proj}_exps.{ex}.weight");
2539                        let nv = src.find_nvfp4_native(&name).unwrap_or_else(|| {
2540                            panic!("expert {name} lost NVFP4-native mid-gather")
2541                        });
2542                        assert_eq!(
2543                            (nv.in_f, nv.out_f),
2544                            (in_f, out_f),
2545                            "expert {ex} dims ({},{}) != expert 0 ({in_f},{out_f})",
2546                            nv.in_f,
2547                            nv.out_f
2548                        );
2549                        buf.extend_from_slice(&memra_gguf::nvfp4_repack::repack_modelopt_to_gguf(
2550                            nv.wbytes, &nv.wscale, out_f, in_f,
2551                        ));
2552                    }
2553                    assert_eq!(buf.len(), total);
2554                    read_macros(&mut macros);
2555                    let pinned = std::env::var("MEMRA_MOE_PINNED").is_ok()
2556                        || std::env::var("MEMRA_MOE_CACHE").as_deref() != Ok("0");
2557                    if pinned {
2558                        let mut p = unsafe { e.ctx().alloc_pinned::<u8>(buf.len())? };
2559                        {
2560                            let dst = p.as_mut_slice()?;
2561                            dst.copy_from_slice(&buf);
2562                        }
2563                        let base = p.as_ptr()?;
2564                        let len = buf.len();
2565                        HostBuf::Pinned {
2566                            slice: std::sync::Arc::new(p),
2567                            base,
2568                            len,
2569                        }
2570                    } else {
2571                        HostBuf::Paged(buf)
2572                    }
2573                };
2574                let all_one = macros.iter().all(|&m| m == 1.0);
2575                return Ok(HostExps {
2576                    bytes,
2577                    tiers: None,
2578                    qtype: QT_NVFP4,
2579                    in_f,
2580                    out_f,
2581                    n_expert,
2582                    row_bytes,
2583                    expert_stride,
2584                    layouts: None,
2585                    macros: if all_one { None } else { Some(macros) },
2586                    fp8_blk: None,
2587                });
2588            }
2589        }
2590
2591        // expert 0 fixes (in_f, out_f); every later expert must match (catches a layer/arch mixup).
2592        let mut buf: Vec<u8> = Vec::new();
2593        let mut in_f = 0usize;
2594        let mut out_f = 0usize;
2595        for ex in 0..n_expert {
2596            // Per-expert ggml name; the source maps it to the HF expert tensor (ST-MOE-PLAN §1.3).
2597            let name = format!("blk.{il}.ffn_{proj}_exps.{ex}.weight");
2598            let v = src
2599                .find(&name)
2600                .unwrap_or_else(|| panic!("missing expert tensor {name}"));
2601            assert_eq!(v.ne.len(), 2, "expert {name} is not 2D (ne={:?})", v.ne);
2602            let (cur_in, cur_out) = (v.ne[0] as usize, v.ne[1] as usize);
2603            if ex == 0 {
2604                in_f = cur_in;
2605                out_f = cur_out;
2606            } else {
2607                assert_eq!(
2608                    (cur_in, cur_out),
2609                    (in_f, out_f),
2610                    "expert {ex} dims {:?} != expert 0 [{in_f},{out_f}]",
2611                    (cur_in, cur_out)
2612                );
2613            }
2614            // PATH A: dequant the 2D expert (F32/F16/BF16) to f32, append its bytes verbatim. The
2615            // dequantized [out_f, in_f] row-major f32 block is exactly one expert_stride slow→fast.
2616            let n = cur_in * cur_out;
2617            let f32v = dequant::dequantize(v.ggml_type, &v.bytes, n);
2618            buf.reserve(n * 4);
2619            for f in &f32v {
2620                buf.extend_from_slice(&f.to_le_bytes());
2621            }
2622        }
2623        let row_bytes = in_f * 4; // one out-row = in_f contiguous f32s
2624        let expert_stride = out_f * row_bytes;
2625        assert_eq!(
2626            buf.len(),
2627            n_expert * expert_stride,
2628            "{ggml_exps_name} gather size {} != n_expert*stride {}",
2629            buf.len(),
2630            n_expert * expert_stride
2631        );
2632        // Hold to the identical invariant as the GGUF path (ST-MOE-PLAN §1.3 step 4).
2633        assert_eq!(
2634            expert_stride,
2635            out_f * row_bytes,
2636            "{ggml_exps_name} stride mismatch: stride={expert_stride} out_f={out_f} row_bytes={row_bytes}"
2637        );
2638
2639        // Same pinned-vs-paged choice as the GGUF loader (the bytes are H2D-only on the hot path).
2640        let pinned = std::env::var("MEMRA_MOE_PINNED").is_ok()
2641            || std::env::var("MEMRA_MOE_CACHE").as_deref() != Ok("0");
2642        let bytes = if pinned {
2643            let mut p = unsafe { e.ctx().alloc_pinned::<u8>(buf.len())? };
2644            {
2645                let dst = p.as_mut_slice()?;
2646                dst.copy_from_slice(&buf);
2647            }
2648            let base = p.as_ptr()?;
2649            let len = buf.len();
2650            HostBuf::Pinned {
2651                slice: std::sync::Arc::new(p),
2652                base,
2653                len,
2654            }
2655        } else {
2656            HostBuf::Paged(buf)
2657        };
2658        Ok(HostExps {
2659            bytes,
2660            tiers: None,
2661            qtype: QT_F32,
2662            in_f,
2663            out_f,
2664            n_expert,
2665            row_bytes,
2666            expert_stride,
2667            layouts: None,
2668            macros: None,
2669            fp8_blk: None,
2670        })
2671    }
2672
2673    /// Coalesce a uniform v2 overlay back into the existing stacked-slab contract without copying.
2674    /// The artifact stores one record per original expert for coverage validation, but a full-bank
2675    /// uniform arm writes those records contiguously into one file. Keeping `layouts=None` preserves
2676    /// the uniform fused kernels while `HostBuf::Mmap` keeps the >RAM artifact zero-copy.
2677    fn load_uniform_mmap_from_source(
2678        src: &dyn TensorSource,
2679        il: u32,
2680        proj: &str,
2681        n_expert: usize,
2682    ) -> Result<Option<Self>, Box<dyn std::error::Error>> {
2683        if src
2684            .active_experts(il)
2685            .is_some_and(|mask| mask.iter().any(|&active| !active))
2686        {
2687            return Ok(None);
2688        }
2689        let mut first_map = None;
2690        let mut first_file = None;
2691        let mut base_offset = 0u64;
2692        let mut expert_stride = 0usize;
2693        let mut in_f = 0usize;
2694        let mut out_f = 0usize;
2695        let mut qtype = 0i32;
2696        let mut row_bytes = 0usize;
2697        let mut macros = vec![1.0f32; n_expert];
2698        #[allow(clippy::needless_range_loop)]
2699        // allow: the explicit index loop keeps the offset arithmetic visible and aligned with the device-side indexing
2700        for ex in 0..n_expert {
2701            let stem = format!("blk.{il}.ffn_{proj}_exps.{ex}");
2702            let name = format!("{stem}.weight");
2703            let Some(DiskExtent {
2704                map,
2705                file,
2706                offset,
2707                len,
2708            }) = find_expert_disk_strict(src, &name)?
2709            else {
2710                return Ok(None);
2711            };
2712            let Some(v) = src.find(&name) else {
2713                return Ok(None);
2714            };
2715            if v.ne.len() != 2 {
2716                return Ok(None);
2717            }
2718            let (cur_in, cur_out) = (v.ne[0] as usize, v.ne[1] as usize);
2719            let Some(cur_row_bytes) = staged_expert_row_bytes(v.ggml_type, cur_in) else {
2720                return Ok(None);
2721            };
2722            let cur_qtype = staged_expert_qtype(v.ggml_type).unwrap();
2723            if ex == 0 {
2724                base_offset = offset;
2725                expert_stride = len;
2726                in_f = cur_in;
2727                out_f = cur_out;
2728                qtype = cur_qtype;
2729                row_bytes = cur_row_bytes;
2730                first_map = Some(map);
2731                first_file = Some(file);
2732            } else if !std::sync::Arc::ptr_eq(first_map.as_ref().unwrap(), &map)
2733                || !std::sync::Arc::ptr_eq(first_file.as_ref().unwrap(), &file)
2734                || offset != base_offset + (ex * expert_stride) as u64
2735                || len != expert_stride
2736                || (cur_in, cur_out, cur_qtype, cur_row_bytes) != (in_f, out_f, qtype, row_bytes)
2737            {
2738                return Ok(None);
2739            }
2740            if let Some(scale) = src.find(&format!("{stem}.scale")) {
2741                macros[ex] = f32::from_le_bytes(scale.bytes[..4].try_into().unwrap());
2742            }
2743        }
2744        assert_eq!(expert_stride, out_f * row_bytes);
2745        let total = n_expert * expert_stride;
2746        let off = usize::try_from(base_offset)
2747            .map_err(|_| format!("uniform expert disk offset {base_offset} does not fit usize"))?;
2748        let all_one = macros.iter().all(|&scale| scale == 1.0);
2749        Ok(Some(HostExps {
2750            bytes: HostBuf::Mmap {
2751                map: first_map.unwrap(),
2752                file: first_file.unwrap(),
2753                off,
2754                len: total,
2755            },
2756            tiers: None,
2757            qtype,
2758            in_f,
2759            out_f,
2760            n_expert,
2761            row_bytes,
2762            expert_stride,
2763            layouts: None,
2764            macros: if all_one { None } else { Some(macros) },
2765            fp8_blk: None,
2766        }))
2767    }
2768
2769    fn load_mixed_from_source(
2770        src: &dyn TensorSource,
2771        il: u32,
2772        proj: &str,
2773        n_expert: usize,
2774    ) -> Result<Self, Box<dyn std::error::Error>> {
2775        let mut tiers = Vec::with_capacity(n_expert);
2776        let mut layouts = Vec::with_capacity(n_expert);
2777        let mut macros = vec![1.0f32; n_expert];
2778        let mut in_f = 0usize;
2779        let mut out_f = 0usize;
2780        let active = src.active_experts(il);
2781        let mut first_active = None;
2782
2783        for ex in 0..n_expert {
2784            if active.is_some_and(|mask| !mask[ex]) {
2785                layouts.push(ExpertLayout {
2786                    offset: 0,
2787                    len: 0,
2788                    qtype: QT_F32,
2789                    row_bytes: 0,
2790                });
2791                tiers.push(HostBuf::Paged(Vec::new()));
2792                continue;
2793            }
2794            let name = format!("blk.{il}.ffn_{proj}_exps.{ex}.weight");
2795            let stem = format!("blk.{il}.ffn_{proj}_exps.{ex}");
2796            if let Some(scale) = src.find(&format!("{stem}.scale")) {
2797                macros[ex] = f32::from_le_bytes(scale.bytes[..4].try_into().unwrap());
2798            }
2799            let (host, byte_len, qtype, row_bytes, cur_in, cur_out) = if let Some(DiskExtent {
2800                map,
2801                file,
2802                offset,
2803                len,
2804            }) =
2805                find_expert_disk_strict(src, &name)?
2806            {
2807                let v = src
2808                    .find(&name)
2809                    .unwrap_or_else(|| panic!("missing expert tensor {name}"));
2810                assert_eq!(v.ne.len(), 2, "expert {name} is not 2D (ne={:?})", v.ne);
2811                let (cur_in, cur_out) = (v.ne[0] as usize, v.ne[1] as usize);
2812                let row_bytes = staged_expert_row_bytes(v.ggml_type, cur_in).ok_or_else(|| {
2813                    format!("mmap expert {name} has unsupported qtype {:?}", v.ggml_type)
2814                })?;
2815                let off = usize::try_from(offset).map_err(|_| {
2816                    format!("expert {name} disk offset {offset} does not fit usize")
2817                })?;
2818                (
2819                    HostBuf::Mmap {
2820                        map,
2821                        file,
2822                        off,
2823                        len,
2824                    },
2825                    len,
2826                    staged_expert_qtype(v.ggml_type).unwrap(),
2827                    row_bytes,
2828                    cur_in,
2829                    cur_out,
2830                )
2831            } else if let Some(nv) = src.find_nvfp4_native(&name) {
2832                let bytes = memra_gguf::nvfp4_repack::repack_modelopt_to_gguf(
2833                    nv.wbytes, &nv.wscale, nv.out_f, nv.in_f,
2834                );
2835                let row_bytes = nv.in_f / 64 * 36;
2836                let byte_len = bytes.len();
2837                (
2838                    HostBuf::Paged(bytes),
2839                    byte_len,
2840                    QT_NVFP4,
2841                    row_bytes,
2842                    nv.in_f,
2843                    nv.out_f,
2844                )
2845            } else {
2846                let v = src
2847                    .find(&name)
2848                    .unwrap_or_else(|| panic!("missing expert tensor {name}"));
2849                assert_eq!(v.ne.len(), 2, "expert {name} is not 2D (ne={:?})", v.ne);
2850                let (cur_in, cur_out) = (v.ne[0] as usize, v.ne[1] as usize);
2851                if let Some(row_bytes) = staged_expert_row_bytes(v.ggml_type, cur_in) {
2852                    let bytes = v.bytes.into_owned();
2853                    let byte_len = bytes.len();
2854                    (
2855                        HostBuf::Paged(bytes),
2856                        byte_len,
2857                        staged_expert_qtype(v.ggml_type).unwrap(),
2858                        row_bytes,
2859                        cur_in,
2860                        cur_out,
2861                    )
2862                } else {
2863                    let f32v = dequant::dequantize(v.ggml_type, &v.bytes, cur_in * cur_out);
2864                    let mut bytes = Vec::with_capacity(f32v.len() * 4);
2865                    for f in f32v {
2866                        bytes.extend_from_slice(&f.to_le_bytes());
2867                    }
2868                    let byte_len = bytes.len();
2869                    (
2870                        HostBuf::Paged(bytes),
2871                        byte_len,
2872                        QT_F32,
2873                        cur_in * 4,
2874                        cur_in,
2875                        cur_out,
2876                    )
2877                }
2878            };
2879
2880            if first_active.is_none() {
2881                in_f = cur_in;
2882                out_f = cur_out;
2883                first_active = Some(ex);
2884            } else {
2885                assert_eq!(
2886                    (cur_in, cur_out),
2887                    (in_f, out_f),
2888                    "expert {ex} dims ({cur_in},{cur_out}) != first active expert ({in_f},{out_f})"
2889                );
2890            }
2891            assert_eq!(
2892                byte_len,
2893                cur_out * row_bytes,
2894                "expert {name} bytes {byte_len} != out_f*row_bytes {}",
2895                cur_out * row_bytes
2896            );
2897            layouts.push(ExpertLayout {
2898                offset: 0,
2899                len: byte_len,
2900                qtype,
2901                row_bytes,
2902            });
2903            tiers.push(host);
2904        }
2905
2906        let first = layouts[*first_active
2907            .as_ref()
2908            .expect("expert mask pruned every expert")];
2909        let expert_stride = layouts.iter().map(|layout| layout.len).max().unwrap_or(0);
2910        let all_one = macros.iter().all(|&scale| scale == 1.0);
2911        Ok(HostExps {
2912            bytes: HostBuf::Paged(Vec::new()),
2913            tiers: Some(tiers),
2914            qtype: first.qtype,
2915            in_f,
2916            out_f,
2917            n_expert,
2918            row_bytes: first.row_bytes,
2919            expert_stride,
2920            layouts: Some(layouts),
2921            macros: if all_one { None } else { Some(macros) },
2922            fp8_blk: None,
2923        })
2924    }
2925
2926    /// Host byte slice for expert `e` (the H2D DMA source). Contiguous block, offset honored.
2927    /// Resolves the per-expert tier when spilling is active (`tiers` Some), else slices the single
2928    /// Per-expert post-matmul macro-scale (1.0 when absent).
2929    #[inline]
2930    pub fn macro_scale(&self, e: usize) -> f32 {
2931        self.macros.as_ref().map(|m| m[e]).unwrap_or(1.0)
2932    }
2933
2934    #[inline]
2935    pub fn is_uniform_layout(&self) -> bool {
2936        self.layouts.is_none()
2937    }
2938
2939    #[inline]
2940    pub fn expert_layout(&self, e: usize) -> ExpertLayout {
2941        debug_assert!(
2942            e < self.n_expert,
2943            "expert index {e} >= n_expert {}",
2944            self.n_expert
2945        );
2946        self.layouts
2947            .as_ref()
2948            .map(|layouts| layouts[e])
2949            .unwrap_or(ExpertLayout {
2950                offset: e * self.expert_stride,
2951                len: self.expert_stride,
2952                qtype: self.qtype,
2953                row_bytes: self.row_bytes,
2954            })
2955    }
2956
2957    #[inline]
2958    pub fn max_expert_bytes(&self) -> usize {
2959        self.layouts
2960            .as_ref()
2961            .and_then(|layouts| layouts.iter().map(|layout| layout.len).max())
2962            .unwrap_or(self.expert_stride)
2963    }
2964
2965    /// backing store (unchanged in-RAM path). Each `tiers[e]` is exactly one expert's stride.
2966    #[inline]
2967    pub fn expert_bytes(&self, e: usize) -> &[u8] {
2968        let layout = self.expert_layout(e);
2969        match &self.tiers {
2970            Some(tiers) => {
2971                debug_assert_eq!(tiers[e].len(), layout.len);
2972                tiers[e].as_bytes()
2973            }
2974            None => &self.bytes.as_bytes()[layout.offset..layout.offset + layout.len],
2975        }
2976    }
2977
2978    /// Source-aware twin of `expert_bytes`. Per-expert tiers already point at one exact block, while
2979    /// a uniform slab needs the expert layout offset added to its base. Keeping those cases separate
2980    /// prevents expert `e` from being offset twice when a tier vector is present.
2981    #[inline]
2982    pub(crate) fn expert_source(&self, e: usize) -> ExpertSource<'_> {
2983        let layout = self.expert_layout(e);
2984        match &self.tiers {
2985            Some(tiers) => tiers[e].expert_source(0, layout.len),
2986            None => self.bytes.expert_source(layout.offset, layout.len),
2987        }
2988    }
2989
2990    /// Hint that expert `e` will be staged soon. Uniform slabs advise only this expert's window;
2991    /// mixed/pruned layouts advise the selected per-expert mmap. Returns false for resident or
2992    /// empty buffers and on unsupported kernels; callers always retain the demand-fault fallback.
2993    #[inline]
2994    pub fn prefetch_expert_pages(&self, e: usize) -> bool {
2995        let layout = self.expert_layout(e);
2996        match &self.tiers {
2997            Some(tiers) => tiers[e].advise_willneed(0, layout.len),
2998            None => self.bytes.advise_willneed(layout.offset, layout.len),
2999        }
3000    }
3001}
3002
3003#[cfg(test)]
3004mod tests {
3005    use super::{
3006        ExpertKeepalive, ExpertSource, HostBuf, HostExps, QT_BF16, QT_NVFP4, QT_Q2_K, QT_Q4_K,
3007        ensure_repack_cache_dir, open_repack_cache, repack_cache_is_fresh, repack_nvfp4_split,
3008        unpack_nvfp4_split, write_repack_cache,
3009    };
3010    use memra_gguf::nvfp4_repack::{repack_modelopt_to_gguf, repack_modelopt_to_split};
3011    use memra_gguf::source::{DiskExtent, Fp8StackedNative, TensorSource, TensorView};
3012    use memra_gguf::{GgmlType, config::ModelConfig};
3013    use std::borrow::Cow;
3014
3015    #[cfg(unix)]
3016    #[test]
3017    fn repack_cache_refuses_symlinked_directory_and_file() {
3018        use std::os::unix::fs::symlink;
3019
3020        let root = std::env::temp_dir().join(format!("memra-repack-links-{}", std::process::id()));
3021        std::fs::create_dir_all(&root).unwrap();
3022        let target_dir = root.join("target-dir");
3023        std::fs::create_dir(&target_dir).unwrap();
3024        let cache_dir = root.join(".memra-repack");
3025        symlink(&target_dir, &cache_dir).unwrap();
3026        let error = ensure_repack_cache_dir(&cache_dir).unwrap_err();
3027        assert_eq!(error.kind(), std::io::ErrorKind::InvalidData);
3028
3029        std::fs::remove_file(&cache_dir).unwrap();
3030        std::fs::create_dir(&cache_dir).unwrap();
3031        let target = root.join("outside.bin");
3032        std::fs::write(&target, b"keep").unwrap();
3033        let cache_file = cache_dir.join("artifact.nvfp4");
3034        symlink(&target, &cache_file).unwrap();
3035        assert!(!repack_cache_is_fresh(&cache_file, 4));
3036        let error = open_repack_cache(&cache_file, true).unwrap_err();
3037        assert_ne!(error.kind(), std::io::ErrorKind::NotFound);
3038        assert_eq!(std::fs::read(&target).unwrap(), b"keep");
3039
3040        let hardlink = cache_dir.join("hardlink.nvfp4");
3041        std::fs::hard_link(&target, &hardlink).unwrap();
3042        let error = write_repack_cache(&hardlink, |out| {
3043            use std::io::Write;
3044            out.write_all(b"replacement")
3045        })
3046        .unwrap_err();
3047        assert_eq!(error.kind(), std::io::ErrorKind::InvalidData);
3048        assert_eq!(std::fs::read(&target).unwrap(), b"keep");
3049        std::fs::remove_dir_all(root).ok();
3050    }
3051
3052    struct MixedExpertSource {
3053        bf16: Vec<u8>,
3054        q4k: Vec<u8>,
3055    }
3056
3057    impl TensorSource for MixedExpertSource {
3058        fn config(&self) -> ModelConfig {
3059            panic!("unused by HostExps mixed-loader test")
3060        }
3061
3062        fn find(&self, name: &str) -> Option<TensorView<'_>> {
3063            let (bytes, ggml_type) = if name == "blk.0.ffn_gate_exps.0.weight" {
3064                (&self.bf16, GgmlType::BF16)
3065            } else if name == "blk.0.ffn_gate_exps.1.weight" {
3066                (&self.q4k, GgmlType::Q4_K)
3067            } else {
3068                return None;
3069            };
3070            Some(TensorView {
3071                bytes: Cow::Borrowed(bytes),
3072                ggml_type,
3073                ne: vec![256, 2],
3074            })
3075        }
3076    }
3077
3078    struct PrunedExpertSource {
3079        q2k: Vec<u8>,
3080        nvfp4: Vec<u8>,
3081        active: Vec<bool>,
3082    }
3083
3084    struct MmapExpertSource {
3085        file: std::sync::Arc<std::fs::File>,
3086        map: std::sync::Arc<memmap2::Mmap>,
3087        base_offset: usize,
3088        expert_len: usize,
3089    }
3090
3091    struct LegacyMmapExpertSource {
3092        map: std::sync::Arc<memmap2::Mmap>,
3093        expert_len: usize,
3094    }
3095
3096    struct StackedFp8Source {
3097        file: std::sync::Arc<std::fs::File>,
3098        map: std::sync::Arc<memmap2::Mmap>,
3099        offset: usize,
3100        len: usize,
3101        scales: Vec<f32>,
3102    }
3103
3104    impl TensorSource for StackedFp8Source {
3105        fn config(&self) -> ModelConfig {
3106            panic!("unused by stacked FP8 ownership test")
3107        }
3108
3109        fn find(&self, _name: &str) -> Option<TensorView<'_>> {
3110            None
3111        }
3112
3113        fn find_fp8_stacked_native(&self, name: &str) -> Option<Fp8StackedNative<'_>> {
3114            (name == "blk.0.ffn_gate_exps.weight").then(|| Fp8StackedNative {
3115                bytes: &self.map[self.offset..self.offset + self.len],
3116                scales: self.scales.clone(),
3117                n_expert: 2,
3118                out_f: 2,
3119                in_f: 32,
3120                scale_rows: 1,
3121                scale_cols: 1,
3122            })
3123        }
3124
3125        fn find_expert_disk(&self, name: &str) -> Option<DiskExtent> {
3126            (name == "blk.0.ffn_gate_exps.weight").then(|| DiskExtent {
3127                map: self.map.clone(),
3128                file: self.file.clone(),
3129                offset: self.offset as u64,
3130                len: self.len,
3131            })
3132        }
3133    }
3134
3135    impl TensorSource for MmapExpertSource {
3136        fn config(&self) -> ModelConfig {
3137            panic!("unused by HostExps mmap-loader test")
3138        }
3139        fn preserve_expert_encodings(&self) -> bool {
3140            true
3141        }
3142        fn find(&self, name: &str) -> Option<TensorView<'_>> {
3143            let ex = match name {
3144                "blk.0.ffn_gate_exps.0.weight" => 0,
3145                "blk.0.ffn_gate_exps.1.weight" => 1,
3146                _ => return None,
3147            };
3148            let off = self.base_offset + ex * self.expert_len;
3149            Some(TensorView {
3150                bytes: Cow::Borrowed(&self.map[off..off + self.expert_len]),
3151                ggml_type: GgmlType::Q2_K,
3152                ne: vec![256, 2],
3153            })
3154        }
3155        fn find_expert_disk(&self, name: &str) -> Option<DiskExtent> {
3156            let ex = match name {
3157                "blk.0.ffn_gate_exps.0.weight" => 0,
3158                "blk.0.ffn_gate_exps.1.weight" => 1,
3159                _ => return None,
3160            };
3161            Some(DiskExtent {
3162                map: self.map.clone(),
3163                file: self.file.clone(),
3164                offset: (self.base_offset + ex * self.expert_len) as u64,
3165                len: self.expert_len,
3166            })
3167        }
3168    }
3169
3170    impl TensorSource for LegacyMmapExpertSource {
3171        fn config(&self) -> ModelConfig {
3172            panic!("unused by legacy mmap guard test")
3173        }
3174        fn preserve_expert_encodings(&self) -> bool {
3175            true
3176        }
3177        fn find(&self, name: &str) -> Option<TensorView<'_>> {
3178            let ex = match name {
3179                "blk.0.ffn_gate_exps.0.weight" => 0,
3180                "blk.0.ffn_gate_exps.1.weight" => 1,
3181                _ => return None,
3182            };
3183            let off = ex * self.expert_len;
3184            Some(TensorView {
3185                bytes: Cow::Borrowed(&self.map[off..off + self.expert_len]),
3186                ggml_type: GgmlType::Q2_K,
3187                ne: vec![256, 2],
3188            })
3189        }
3190        fn find_expert_mmap(
3191            &self,
3192            name: &str,
3193        ) -> Option<(std::sync::Arc<memmap2::Mmap>, usize, usize)> {
3194            let ex = match name {
3195                "blk.0.ffn_gate_exps.0.weight" => 0,
3196                "blk.0.ffn_gate_exps.1.weight" => 1,
3197                _ => return None,
3198            };
3199            Some((self.map.clone(), ex * self.expert_len, self.expert_len))
3200        }
3201    }
3202
3203    impl TensorSource for PrunedExpertSource {
3204        fn config(&self) -> ModelConfig {
3205            panic!("unused by HostExps pruned-loader test")
3206        }
3207        fn active_experts(&self, layer: u32) -> Option<&[bool]> {
3208            (layer == 0).then_some(self.active.as_slice())
3209        }
3210        fn find(&self, name: &str) -> Option<TensorView<'_>> {
3211            let (bytes, ggml_type) = match name {
3212                "blk.0.ffn_gate_exps.0.weight" => (&self.q2k, GgmlType::Q2_K),
3213                "blk.0.ffn_gate_exps.2.weight" => (&self.nvfp4, GgmlType::NVFP4),
3214                _ => return None,
3215            };
3216            Some(TensorView {
3217                bytes: Cow::Borrowed(bytes),
3218                ggml_type,
3219                ne: vec![256, 2],
3220            })
3221        }
3222    }
3223
3224    #[test]
3225    fn stacked_fp8_experts_retain_owned_mmap_and_scale_geometry() {
3226        let path = std::env::temp_dir().join(format!("memra-stacked-fp8-{}", std::process::id()));
3227        let offset = 11usize;
3228        let len = 2 * 2 * 32;
3229        let mut file_bytes = vec![0xA5; offset];
3230        file_bytes.extend((0..len).map(|i| (i % 127) as u8));
3231        std::fs::write(&path, &file_bytes).unwrap();
3232        let file = std::sync::Arc::new(std::fs::File::open(&path).unwrap());
3233        let map = std::sync::Arc::new(unsafe { memmap2::Mmap::map(file.as_ref()).unwrap() });
3234        let source = StackedFp8Source {
3235            file,
3236            map,
3237            offset,
3238            len,
3239            scales: vec![0.5, 0.25],
3240        };
3241
3242        let exps = HostExps::load_fp8_stacked_native_with_policy(
3243            &source,
3244            "blk.0.ffn_gate_exps.weight",
3245            true,
3246        )
3247        .unwrap()
3248        .unwrap();
3249        assert_eq!(exps.qtype, crate::QT_F8_E4M3_BLK);
3250        assert_eq!((exps.n_expert, exps.out_f, exps.in_f), (2, 2, 32));
3251        assert_eq!(exps.expert_stride, 64);
3252        assert!(matches!(exps.bytes, HostBuf::Mmap { .. }));
3253        assert_eq!(exps.expert_bytes(0), &file_bytes[offset..offset + 64]);
3254        assert_eq!(exps.expert_bytes(1), &file_bytes[offset + 64..offset + len]);
3255        let fp8 = exps.fp8_blk.as_ref().unwrap();
3256        assert_eq!((fp8.rows, fp8.cols, fp8.expert_stride), (1, 1, 1));
3257        assert_eq!(fp8.scales, vec![0.5, 0.25]);
3258
3259        drop(source);
3260        assert_eq!(exps.expert_bytes(1), &file_bytes[offset + 64..offset + len]);
3261        std::fs::remove_file(path).ok();
3262    }
3263
3264    #[test]
3265    fn stacked_fp8_experts_reject_non_finite_codes() {
3266        let path =
3267            std::env::temp_dir().join(format!("memra-stacked-fp8-nan-{}", std::process::id()));
3268        let len = 2 * 2 * 32;
3269        let mut file_bytes = vec![0x12; len];
3270        file_bytes[73] = 0x7f;
3271        std::fs::write(&path, &file_bytes).unwrap();
3272        let file = std::sync::Arc::new(std::fs::File::open(&path).unwrap());
3273        let map = std::sync::Arc::new(unsafe { memmap2::Mmap::map(file.as_ref()).unwrap() });
3274        let source = StackedFp8Source {
3275            file,
3276            map,
3277            offset: 0,
3278            len,
3279            scales: vec![0.5, 0.25],
3280        };
3281
3282        let err = match HostExps::load_fp8_stacked_native_with_policy(
3283            &source,
3284            "blk.0.ffn_gate_exps.weight",
3285            true,
3286        ) {
3287            Ok(_) => panic!("non-finite E4M3 code was accepted"),
3288            Err(err) => err,
3289        };
3290        assert!(err.to_string().contains("non-finite E4M3"));
3291        std::fs::remove_file(path).ok();
3292    }
3293
3294    /// A1 direct-import gate (engine side): the fused modelopt->split repack must be byte-for-byte
3295    /// the composition of the two passes it replaces (modelopt->GGUF blocks, then the A6
3296    /// split-plane repack). Also pins the split roundtrip on the same buffers.
3297    #[test]
3298    fn direct_split_equals_chained() {
3299        for (out_f, in_f) in [(1usize, 64usize), (3, 128), (5, 320), (8, 1024)] {
3300            let mut w = vec![0u8; out_f * in_f / 2];
3301            let mut s = vec![0u8; out_f * in_f / 16];
3302            for (i, b) in w.iter_mut().enumerate() {
3303                *b = ((i * 41 + 7) & 0xFF) as u8;
3304            }
3305            for (i, b) in s.iter_mut().enumerate() {
3306                *b = (0x20 + ((i * 11 + 5) % 0x50)) as u8;
3307            }
3308            let gguf = repack_modelopt_to_gguf(&w, &s, out_f, in_f);
3309            let chained = repack_nvfp4_split(&gguf, out_f);
3310            let direct = repack_modelopt_to_split(&w, &s, out_f, in_f);
3311            assert_eq!(
3312                direct, chained,
3313                "fused != chained at out_f={out_f} in_f={in_f}"
3314            );
3315            assert_eq!(
3316                unpack_nvfp4_split(&direct, out_f),
3317                gguf,
3318                "split roundtrip broken at out_f={out_f} in_f={in_f}"
3319            );
3320        }
3321    }
3322
3323    #[test]
3324    fn mixed_expert_loader_keeps_each_encoding_and_extent() {
3325        let source = MixedExpertSource {
3326            bf16: vec![0x5a; 256 * 2 * 2],
3327            q4k: vec![0xa5; 2 * 144],
3328        };
3329        let exps = HostExps::load_mixed_from_source(&source, 0, "gate", 2).unwrap();
3330        assert!(!exps.is_uniform_layout());
3331        assert_eq!(exps.max_expert_bytes(), 1024);
3332        assert_eq!(exps.expert_layout(0).qtype, QT_BF16);
3333        assert_eq!(exps.expert_layout(0).row_bytes, 512);
3334        assert_eq!(exps.expert_layout(0).len, 1024);
3335        assert_eq!(exps.expert_layout(1).qtype, QT_Q4_K);
3336        assert_eq!(exps.expert_layout(1).row_bytes, 144);
3337        assert_eq!(exps.expert_layout(1).len, 288);
3338        assert_eq!(exps.expert_bytes(0), source.bf16);
3339        assert_eq!(exps.expert_bytes(1), source.q4k);
3340        match exps.expert_source(1) {
3341            ExpertSource::Memory { bytes, .. } => assert_eq!(bytes, source.q4k),
3342            ExpertSource::Disk { .. } => panic!("paged expert unexpectedly became disk-backed"),
3343        }
3344    }
3345
3346    #[test]
3347    fn mixed_expert_loader_omits_masked_expert_bytes() {
3348        let source = PrunedExpertSource {
3349            q2k: vec![0x22; 2 * 84],
3350            nvfp4: vec![0x44; 2 * 4 * 36],
3351            active: vec![true, false, true],
3352        };
3353        let exps = HostExps::load_mixed_from_source(&source, 0, "gate", 3).unwrap();
3354        assert_eq!(exps.expert_layout(0).qtype, QT_Q2_K);
3355        assert_eq!(exps.expert_layout(0).row_bytes, 84);
3356        assert_eq!(exps.expert_layout(1).len, 0);
3357        assert_eq!(exps.expert_bytes(1), &[]);
3358        assert_eq!(exps.expert_layout(2).qtype, QT_NVFP4);
3359        assert_eq!(exps.expert_layout(2).row_bytes, 4 * 36);
3360    }
3361
3362    #[test]
3363    fn mixed_expert_loader_keeps_mmap_backing_zero_copy() {
3364        let path = std::env::temp_dir().join(format!("memra-mixed-mmap-{}", std::process::id()));
3365        let base_offset = 3usize;
3366        let expert_len = 2 * 84;
3367        let mut bytes = vec![0xE1; base_offset];
3368        bytes.extend(vec![0x31; expert_len]);
3369        bytes.extend(vec![0x72; expert_len]);
3370        std::fs::write(&path, &bytes).unwrap();
3371        let file = std::sync::Arc::new(std::fs::File::open(&path).unwrap());
3372        let map = std::sync::Arc::new(unsafe { memmap2::Mmap::map(file.as_ref()).unwrap() });
3373        let source = MmapExpertSource {
3374            file: file.clone(),
3375            map,
3376            base_offset,
3377            expert_len,
3378        };
3379        let exps = HostExps::load_mixed_from_source(&source, 0, "gate", 2).unwrap();
3380        assert!(matches!(
3381            exps.tiers.as_ref().unwrap()[0],
3382            HostBuf::Mmap { .. }
3383        ));
3384        assert!(matches!(
3385            exps.tiers.as_ref().unwrap()[1],
3386            HostBuf::Mmap { .. }
3387        ));
3388        assert_eq!(
3389            exps.expert_bytes(0),
3390            &bytes[base_offset..base_offset + expert_len]
3391        );
3392        assert_eq!(exps.expert_bytes(1), &bytes[base_offset + expert_len..]);
3393        match exps.expert_source(1) {
3394            ExpertSource::Disk {
3395                file: got_file,
3396                offset,
3397                len,
3398                fallback,
3399                keepalive,
3400            } => {
3401                assert!(std::sync::Arc::ptr_eq(got_file, &file));
3402                assert_eq!(offset, (base_offset + expert_len) as u64);
3403                assert_eq!(len, expert_len);
3404                assert_eq!(fallback, &bytes[base_offset + expert_len..]);
3405                match keepalive {
3406                    ExpertKeepalive::Mmap(owner) => {
3407                        assert!(std::sync::Arc::ptr_eq(&owner, &source.map));
3408                    }
3409                    _ => panic!("mmap expert did not retain its mmap owner"),
3410                }
3411            }
3412            ExpertSource::Memory { .. } => panic!("mixed mmap tier lost its disk extent"),
3413        }
3414        #[cfg(unix)]
3415        assert!(exps.prefetch_expert_pages(1));
3416        std::fs::remove_file(path).ok();
3417    }
3418
3419    #[test]
3420    fn tiered_expert_source_does_not_double_apply_layout_offset() {
3421        let path =
3422            std::env::temp_dir().join(format!("memra-tiered-source-offset-{}", std::process::id()));
3423        let base_offset = 7usize;
3424        let expert_len = 2 * 84;
3425        let mut bytes = vec![0xE3; base_offset];
3426        bytes.extend(vec![0x41; expert_len]);
3427        bytes.extend(vec![0x82; expert_len]);
3428        std::fs::write(&path, &bytes).unwrap();
3429        let file = std::sync::Arc::new(std::fs::File::open(&path).unwrap());
3430        let map = std::sync::Arc::new(unsafe { memmap2::Mmap::map(file.as_ref()).unwrap() });
3431        let exps = HostExps {
3432            bytes: HostBuf::Paged(Vec::new()),
3433            tiers: Some(vec![
3434                HostBuf::Mmap {
3435                    map: map.clone(),
3436                    file: file.clone(),
3437                    off: base_offset,
3438                    len: expert_len,
3439                },
3440                HostBuf::Mmap {
3441                    map,
3442                    file: file.clone(),
3443                    off: base_offset + expert_len,
3444                    len: expert_len,
3445                },
3446            ]),
3447            qtype: QT_Q2_K,
3448            in_f: 256,
3449            out_f: 2,
3450            n_expert: 2,
3451            row_bytes: 84,
3452            expert_stride: expert_len,
3453            layouts: None,
3454            macros: None,
3455            fp8_blk: None,
3456        };
3457
3458        // `expert_layout(1).offset == expert_len`, but tier 1 already starts at expert 1.
3459        assert_eq!(exps.expert_layout(1).offset, expert_len);
3460        match exps.expert_source(1) {
3461            ExpertSource::Disk {
3462                offset,
3463                len,
3464                fallback,
3465                ..
3466            } => {
3467                assert_eq!(offset, (base_offset + expert_len) as u64);
3468                assert_eq!(len, expert_len);
3469                assert_eq!(fallback, &bytes[base_offset + expert_len..]);
3470            }
3471            ExpertSource::Memory { .. } => panic!("tiered mmap expert lost its disk extent"),
3472        }
3473        std::fs::remove_file(path).ok();
3474    }
3475
3476    #[test]
3477    fn legacy_mmap_source_requires_retained_file_extent() {
3478        let path =
3479            std::env::temp_dir().join(format!("memra-legacy-mmap-source-{}", std::process::id()));
3480        let expert_len = 2 * 84;
3481        std::fs::write(&path, vec![0x64; 2 * expert_len]).unwrap();
3482        let file = std::fs::File::open(&path).unwrap();
3483        let map = std::sync::Arc::new(unsafe { memmap2::Mmap::map(&file).unwrap() });
3484        let source = LegacyMmapExpertSource { map, expert_len };
3485
3486        let err = match HostExps::load_uniform_mmap_from_source(&source, 0, "gate", 2) {
3487            Ok(_) => panic!("legacy mmap-only source silently fell back instead of failing"),
3488            Err(err) => err,
3489        };
3490        let message = err.to_string();
3491        assert!(
3492            message.contains("legacy find_expert_mmap without find_expert_disk"),
3493            "{message}"
3494        );
3495        assert!(message.contains("retained Arc<File>"), "{message}");
3496        std::fs::remove_file(path).ok();
3497    }
3498
3499    #[test]
3500    fn uniform_expert_loader_coalesces_contiguous_mmap() {
3501        let path = std::env::temp_dir().join(format!("memra-uniform-mmap-{}", std::process::id()));
3502        let base_offset = 5usize;
3503        let expert_len = 2 * 84;
3504        let mut bytes = vec![0xE2; base_offset];
3505        bytes.extend(vec![0x19; expert_len]);
3506        bytes.extend(vec![0x91; expert_len]);
3507        std::fs::write(&path, &bytes).unwrap();
3508        let file = std::sync::Arc::new(std::fs::File::open(&path).unwrap());
3509        let map = std::sync::Arc::new(unsafe { memmap2::Mmap::map(file.as_ref()).unwrap() });
3510        let source = MmapExpertSource {
3511            file: file.clone(),
3512            map,
3513            base_offset,
3514            expert_len,
3515        };
3516        let exps = HostExps::load_uniform_mmap_from_source(&source, 0, "gate", 2)
3517            .unwrap()
3518            .expect("contiguous mmap should coalesce");
3519        assert!(exps.is_uniform_layout());
3520        assert!(matches!(&exps.bytes, HostBuf::Mmap { .. }));
3521        assert_eq!(exps.expert_stride, expert_len);
3522        assert_eq!(
3523            exps.expert_bytes(0),
3524            &bytes[base_offset..base_offset + expert_len]
3525        );
3526        assert_eq!(exps.expert_bytes(1), &bytes[base_offset + expert_len..]);
3527        match exps.expert_source(1) {
3528            ExpertSource::Disk {
3529                file: got_file,
3530                offset,
3531                len,
3532                fallback,
3533                ..
3534            } => {
3535                assert!(std::sync::Arc::ptr_eq(got_file, &file));
3536                assert_eq!(offset, (base_offset + expert_len) as u64);
3537                assert_eq!(len, expert_len);
3538                assert_eq!(fallback, &bytes[base_offset + expert_len..]);
3539            }
3540            ExpertSource::Memory { .. } => panic!("uniform mmap slab lost its disk extent"),
3541        }
3542        #[cfg(unix)]
3543        assert!(exps.prefetch_expert_pages(1));
3544        std::fs::remove_file(path).ok();
3545    }
3546}