Skip to main content

cortiq_engine/
pipeline.rs

1//! Full inference pipeline: tokenize → embed → layers → lm_head → sample → decode.
2//!
3//! Prefill/decode contract: every token is forwarded exactly once and
4//! enters the KV cache exactly once. Logits for the next token are
5//! computed from the hidden state of the LAST forwarded token — the
6//! decode loop forwards the freshly sampled token, never re-embeds the
7//! prompt tail (v1 duplicated the last prompt token in the cache).
8
9use crate::attention::{self, QwenAttnCfg};
10use crate::inference;
11
12/// MiMo-V2 multi-token prediction (draft stack + speculative round). A
13/// child module so it runs on the pipeline's own helpers.
14#[path = "mimo_mtp.rs"]
15pub mod mimo_mtp;
16#[path = "clover.rs"]
17pub mod clover;
18use crate::kv_cache::KvCache;
19use crate::linear_core::{
20    GdnCfg, GdnWeights, ShortConvCfg, ShortConvWeights, VmfPhaseCfg, VmfPhaseWeights, gdn_forward,
21    gdn_pair, short_conv_forward, short_conv_forward_batch, short_conv_pair, vmf_phase_forward,
22    vmf_phase_pair,
23};
24use crate::pool::Pool;
25use crate::qtensor::QTensor;
26use crate::sampler::{self, SamplerConfig, SamplerScratch, SplitMix64};
27use crate::tokenizer::Tokenizer;
28use cortiq_core::mask::TaskMask;
29use cortiq_core::types::NormStyle;
30
31pub static GLOBAL_USE_GPU: std::sync::atomic::AtomicBool =
32    std::sync::atomic::AtomicBool::new(false);
33
34// MTP compaction statistics must describe the dense draft that will be
35// serialized, not an ambient serving experiment such as gate/oracle top-k.
36// A thread-local nesting counter keeps the guard local to calibration and
37// avoids mutating process-global environment variables mid-command.
38thread_local! {
39    static EXACT_MTP_CALIBRATION: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
40}
41
42struct ExactMtpCalibrationGuard;
43
44impl ExactMtpCalibrationGuard {
45    fn enter() -> Self {
46        EXACT_MTP_CALIBRATION.with(|depth| depth.set(depth.get().saturating_add(1)));
47        Self
48    }
49}
50
51impl Drop for ExactMtpCalibrationGuard {
52    fn drop(&mut self) {
53        EXACT_MTP_CALIBRATION.with(|depth| {
54            debug_assert!(depth.get() > 0, "unbalanced MTP calibration guard");
55            depth.set(depth.get().saturating_sub(1));
56        });
57    }
58}
59
60fn exact_mtp_calibration_active() -> bool {
61    EXACT_MTP_CALIBRATION.with(|depth| depth.get() != 0)
62}
63
64/// Extend an MTP draft-head shortlist to the model vocabulary.  Rows outside
65/// the shortlist were deliberately not evaluated, so they must be impossible
66/// draft candidates rather than neutral zero logits.
67fn pad_mtp_draft_logits(logits: &mut Vec<f32>, vocab_size: usize) {
68    logits.resize(vocab_size, f32::NEG_INFINITY);
69}
70
71/// Reusable per-pipeline forward scratch: the four norm outputs the
72/// decode paths recompute every layer (single: n1/p1; pair: all four).
73/// Plain buffers, resized once — steady-state decode reuses them.
74struct ForwardScratch {
75    n1: Vec<f32>,
76    n2: Vec<f32>,
77    p1: Vec<f32>,
78    p2: Vec<f32>,
79}
80
81impl ForwardScratch {
82    fn new(hidden: usize) -> Self {
83        Self {
84            n1: vec![0.0; hidden],
85            n2: vec![0.0; hidden],
86            p1: vec![0.0; hidden],
87            p2: vec![0.0; hidden],
88        }
89    }
90}
91
92/// Complete inference pipeline state.
93pub struct Pipeline {
94    /// In-process layer split across local GPUs: (device, first layer,
95    /// last layer) per segment, in execution order. `None` = one device.
96    /// Arc so cloning the plan out of `&mut self` does not fight the
97    /// borrow checker on the hot path.
98    gpu_plan: Option<std::sync::Arc<Vec<(usize, usize, usize)>>>,
99    /// Arc: the server shares one tokenizer handle across request
100    /// handlers without borrowing a pipeline slot.
101    pub tokenizer: std::sync::Arc<Tokenizer>,
102    pub kv_cache: KvCache,
103    pub sampler_config: SamplerConfig,
104    pub weights: PipelineWeights,
105    pub hidden_size: usize,
106    pub intermediate_size: usize,
107    pub num_heads: usize,
108    pub num_kv_heads: usize,
109    pub head_dim: usize,
110    /// Total virtual layers (num_layers × num_loops for looped models).
111    pub num_layers: usize,
112    /// Physical layers in weights.layers (≤ num_layers for looped models).
113    pub physical_layers: usize,
114    /// Looped Transformer: apply final norm after each loop iteration.
115    pub loop_final_norm: bool,
116    pub vocab_size: usize,
117    pub rms_eps: f64,
118    pub rope_base: f32,
119    pub norm_style: NormStyle,
120    /// RoPE dims actually rotated (≤ head_dim; Qwen3.5 uses head_dim/4).
121    pub rotary_dim: usize,
122    /// Optional Q-head count override for each attention layer (Laguna).
123    pub attention_heads_per_layer: Option<Vec<usize>>,
124    /// Optional KV-head count of each PHYSICAL attention layer (MiMo-V2:
125    /// 4 on full-attention layers, 8 on sliding ones). Set only through
126    /// [`Pipeline::set_attn_geometry`], which also reshapes the layer
127    /// caches. None = every layer has `num_kv_heads`.
128    pub kv_heads_per_layer: Option<Vec<usize>>,
129    /// Width of each V head when it is narrower than `head_dim` (MiMo-V2:
130    /// 128 against 192). V is zero-padded to `head_dim` inside the cache
131    /// and the attention output is compacted back to nh·v_head_dim before
132    /// o_proj (see `QwenAttnCfg::v_head_dim`). None = `head_dim`.
133    pub v_head_dim: Option<usize>,
134    /// `CMF_LAYER_DUMP=<dir>` (read once at construction; tests set it
135    /// directly): the hidden state after every layer, for every position,
136    /// as raw little-endian f32 files `p{pos:06}_l{li:02}.f32` of
137    /// `hidden_size` floats each. Written by the CPU layer walks — the
138    /// batched prefill (`prefill_batch_span`) and the single-token forward
139    /// (`forward_layers_span`) — so both prompt ingest and decode can be
140    /// diffed layer by layer against an external oracle (tools/mimo_ref).
141    /// Layers a device graph runs (wgpu token/batch graph, Metal chunk or
142    /// block graphs) and the own-stack families (DeepSeek-V4/V4.1,
143    /// Qwen3.8-Flash-Next, Gemma-3n) are not dumped: run with CMF_GPU=0
144    /// for a complete set. `li` is the virtual layer index. Final logits:
145    /// `CMF_LOGIT_DUMP=<file>` (hidden + logits of the first decode step).
146    pub layer_dump: Option<std::path::PathBuf>,
147    /// GPU-graph declines already logged for this pipeline, as (graph
148    /// site, reason) — one line each, see `graph_attn_decline_reason`.
149    graph_declines: std::cell::RefCell<Vec<(&'static str, &'static str)>>,
150    /// MiMo-V2 expert placement (prefix / dynamic bank / hybrid), decided
151    /// on the first forward — see `crate::mimo_moe`.
152    pub(crate) mimo_moe: crate::mimo_moe::Slot,
153    /// Opt-in MiniCPM q4tp expert cache for a graph-produced CPU tail.
154    /// It is deliberately separate from MiMo's placement policy because it
155    /// serves only the stock MiniCPM top-8 geometry and otherwise falls back
156    /// to the ordinary exact host MoE path.
157    pub(crate) minicpm_tail: crate::minicpm_tail::MiniCpmTail,
158    /// Linear-core geometry (present when the model has linear layers).
159    pub vmf_cfg: Option<VmfPhaseCfg>,
160    /// GatedDeltaNet geometry (faithful vendor operator).
161    pub gdn_cfg: Option<GdnCfg>,
162    /// MiniCPM-class logit scale (tied lm_head → cannot fold into weights).
163    pub logit_multiplier: Option<f32>,
164    /// Cooperative cancel: set from any thread (FFI `cortiq_cancel`,
165    /// a dropped server connection); the generate loop checks it at
166    /// every prefill chunk and decode step and finishes with
167    /// `finish_reason: "cancelled"`. Auto-cleared when honoured.
168    pub cancel: std::sync::Arc<std::sync::atomic::AtomicBool>,
169    /// A GPU graph failure is distinct from a user/request cancellation.
170    /// Graph code sets this before raising the cooperative cancel flag so the
171    /// generation API can return an error instead of reporting a successful
172    /// `finish_reason: cancelled` result.
173    graph_failed: std::sync::atomic::AtomicBool,
174    /// Token ids currently materialized in the KV cache (the forwarded
175    /// prompt + all generated tokens except the last, which is sampled
176    /// but not yet forwarded). Lets the next generate call prefill only
177    /// the suffix when a chat app resends the whole history.
178    pub kv_history: Vec<u32>,
179    /// Owner tag of `kv_history`: the resident device graph held that
180    /// sequence (true) or the host did. Reuse continues only on the owner.
181    pub kv_history_device: bool,
182    /// KDA geometry (Kimi Linear / Kimi-K3) — shared by every Kda layer.
183    pub kda_cfg: Option<crate::linear_core::KdaCfg>,
184    /// Gemma-3n stack (AltUp/LAuReL/PLE/KV-sharing): its own forward —
185    /// weights.layers stays empty, the KV caches are the shared ones.
186    pub g3n: Option<Box<(crate::g3n::G3nGlobals, Vec<crate::g3n::G3nLayer>)>>,
187    /// Gemma-4 E-series stack (dual-geometry gemma4 + PLE + shared KV):
188    /// its own per-token forward, same contract as `g3n`.
189    pub g4e: Option<Box<(crate::g4e::G4eGlobals, Vec<crate::g4e::G4eLayer>)>>,
190    /// DeepSeek-V4 runs its own stack too: its hidden state is `hc_mult`
191    /// copies of a vector, so no loop written for a single residual
192    /// stream can carry it.
193    pub dsv4: Option<
194        Box<(
195            crate::dsv4::Dsv4Globals,
196            Vec<crate::dsv4::Dsv4Layer>,
197            crate::dsv4::Dsv4Cfg,
198            crate::dsv4::Dsv4State,
199        )>,
200    >,
201    /// DeepSeek-V4.1 owns the shared CED/CSA2 attention state, raw Engram
202    /// lookup and four-stream mHC handoff. It cannot use the V4 cache
203    /// layout, so it has a dedicated executor and state tuple.
204    pub dsv41: Option<
205        Box<(
206            crate::dsv41::Dsv41Globals,
207            Vec<crate::dsv41::Dsv41Layer>,
208            crate::dsv41::Dsv41Cfg,
209            crate::dsv41::Dsv41State,
210        )>,
211    >,
212    /// Optional V4.1 vision tower. Text-only files leave this unset.
213    pub dsv41_vision: Option<crate::dsv41_vision::VisionModel>,
214    /// Prepared image rows consumed by the next V4.1 prefill.
215    dsv41_prefill: Option<(Vec<Option<Vec<f32>>>, Vec<bool>)>,
216    /// Qwen3.8-Flash-Next owns four residual streams plus QSA/PLE state;
217    /// the generic single-residual layer loop cannot represent it.
218    /// Block-diffusion generation (Clover): preview weights and the
219    /// decoding controls; `None` on every causal model.
220    pub(crate) clover: Option<std::sync::Arc<clover::Clover>>,
221    clover_stats: clover::CloverStats,
222    pub qwen4_exp: Option<
223        Box<(
224            crate::qwen4_exp::Globals,
225            Vec<crate::qwen4_exp::Layer>,
226            crate::qwen4_exp::Cfg,
227            crate::qwen4_exp::State,
228        )>,
229    >,
230    /// DeepSeek-V4's own speculation stack: three draft modules, each a full
231    /// layer, plus a confidence head on the last. Empty when the file has
232    /// none, which is the only signal the decode path needs.
233    pub dsv4_mtp: Vec<crate::dsv4::Dsv4Mtp>,
234    /// The draft's per-sequence state (KV rings, captured trunk hidden).
235    pub dspark: Option<crate::dsv4::DsparkState>,
236    /// Drafts awaiting their verdict: (position, proposals, still matching,
237    /// accepted so far).
238    pub dspark_pending: Vec<(usize, Vec<u32>, bool, usize)>,
239    /// Accepted prefix length of every graded draft.
240    pub dspark_hist: Vec<usize>,
241    /// The real tokens the drafts were graded against — a degenerate,
242    /// repeating output would make any acceptance number meaningless, and
243    /// the cheapest guard against believing one is to count them.
244    pub dspark_real: Vec<u32>,
245    /// The trunk's expert picks for the last few tokens, per layer. The
246    /// union over a window of them is what a batched verify would have to
247    /// read, and the ratio to the pick count is all it could save.
248    pub dspark_trunk_picks: Vec<Vec<(usize, Vec<usize>)>>,
249    /// (unique, total) expert picks per draft, trunk side and draft side.
250    pub dspark_exp: Vec<(usize, usize, usize, usize)>,
251    /// Wall time spent in the deliberately out-of-core draft. Kept separate
252    /// from trunk decode so block batching can be judged without conflating
253    /// it with GPU chain variance.
254    pub dspark_draft_ns: u128,
255    /// Per-layer winners of the last pooled route — the admission predictor.
256    m3_winners: Vec<Vec<u32>>,
257    /// Route frequency per (layer, expert) — the bank's prewarm profile.
258    m3_counts: Vec<u32>,
259    m3_n_exp: usize,
260    m3_tokens: u64,
261    m3_prewarmed: bool,
262    /// LFM2 short-convolution geometry (present when the model has
263    /// `ShortConv` mixer layers).
264    pub short_conv_cfg: Option<ShortConvCfg>,
265    /// Multi-token-prediction head (None = absent).
266    pub mtp: Option<MtpModule>,
267    /// MiMo-V2's draft stack (three chained MTP layers from the
268    /// `<stem>.mtp.cmf` sidecar); None = absent. Speculative greedy decode
269    /// uses it unless `CMF_MTP=0` / `CMF_MIMO_MTP=0`.
270    pub mimo_mtp: Option<mimo_mtp::MimoMtp>,
271    /// Set while the MiMo speculative verify runs `prefill_batch`: its MoE
272    /// layers take `moe_ffn_rows_exact` (each row bit-identical to decode).
273    verify_exact_moe: bool,
274    /// Speculative decode via MTP (greedy only; `CMF_MTP=0` disables).
275    pub speculative: bool,
276    /// Keep generating past end-of-sequence ids (the llama-bench contract
277    /// for a timed run). A loop flag, deliberately NOT a sampler
278    /// suppression: suppressed ids count as a penalty and switch the
279    /// speculative round and the greedy burst off, so a benchmark that
280    /// suppressed EOS never measured either.
281    pub ignore_eos: bool,
282    /// Draft-head shortlist guard: tokens left during which the draft
283    /// uses the FULL head because a recently committed id lay past the
284    /// `CMF_DRAFT_VOCAB` cut (Cyrillic and CJK ids sit above 131072 in
285    /// Qwen's table, so a prefix shortlist would draft nothing usable
286    /// there — measured on Russian prose: 2.9 → 1.6 accepted a round).
287    pub draft_full_streak: u32,
288    /// Adaptive draft depth for the speculative round (None until the
289    /// first round): grows while nearly every draft is accepted, shrinks
290    /// when fewer than half are. The verify's cost climbs with the rows on
291    /// a discrete card (RTX PRO 4000: 52 ms at 2 rows, 74 at 5, 80 at 6),
292    /// so prose wants k≈3 and code or the repetitive bench k≈5 — measured
293    /// 33.6 vs 27.6 tok/s on an essay at k=3 vs 5, 45.6 vs 38 on code.
294    /// `CMF_GRAPH_SPEC_K` pins it.
295    pub spec_k_adapt: Option<usize>,
296    /// EWMA of the accepted fraction that drives `spec_k_adapt`.
297    pub spec_acc_ewma: f32,
298    rng: SplitMix64,
299    sampler_scratch: SamplerScratch,
300    /// Speculative SAMPLING state (graph_spec_step, temperature > 0): the
301    /// correction token a rejected draft produced — committed by the loop
302    /// top in place of a fresh draw — and the per-round draft
303    /// distributions / target scratch, reused so a round allocates
304    /// nothing at the vocab size.
305    spec_forced: Option<u32>,
306    spec_q: Vec<Vec<f32>>,
307    spec_p: Vec<f32>,
308    spec_res: Vec<f32>,
309    /// The same three for the sparse chain (top-k configs).
310    spec_qs: Vec<sampler::Sparse>,
311    spec_ps: sampler::Sparse,
312    spec_ress: sampler::Sparse,
313    /// Which arm the MTP draft block runs on this generation: Some(true)
314    /// = the whole-token graph (device attention, one submit a step),
315    /// Some(false) = the per-op path; None = not decided yet. Decided
316    /// on the first draft and held, because the two arms keep the MTP
317    /// KV in different places (device mirror vs the CPU cache) and a
318    /// mid-run switch would read the wrong one.
319    mtp_graph_mode: Option<bool>,
320    /// The Metal verify graph of the round in flight, between its sync
321    /// (logits read) and the commit that replays the accepted prefix.
322    #[cfg(target_os = "macos")]
323    metal_verify: Option<MetalVerifyPending>,
324    /// Precomputed RoPE inverse frequencies [head_dim/2]. Arc: the
325    /// forward path clones a handle to escape the &mut self borrow —
326    /// cloning the table itself was a per-forward allocation.
327    pub(crate) inv_freq: std::sync::Arc<Vec<f32>>,
328    /// Reusable norm buffers for the decode hot path (roadmap §3 P0:
329    /// steady-state forward should not heap-allocate). Disjoint field
330    /// from `weights`/`kv_cache`, so split borrows keep working.
331    ws: ForwardScratch,
332    /// Persistent worker pool (None = serial; see CMF_THREADS).
333    pool: Option<std::sync::Arc<Pool>>,
334    // ── Dynamic per-token skill routing (spec §9, claim 14/16) ──
335    /// Source model, retained so a skill switch can re-resolve the
336    /// touched layers' FFN tensors (Mapped = mmap pointers, cheap).
337    pub(crate) model: Option<std::sync::Arc<cortiq_core::CmfModel>>,
338    /// Masks present → weights are dequantized f32 (rebuild path).
339    pub(crate) dyn_force_f32: bool,
340    /// Per-skill FFN layers actually replaced (derived from tensors, not
341    /// the meta `layers` field — ru2 replaces down_proj in 0..23 while
342    /// its meta says [20..23]). None = skill touches non-FFN tensors →
343    /// ineligible for cheap dynamic switching (honest refusal).
344    pub(crate) dyn_skill_layers: Vec<Option<Vec<usize>>>,
345    /// Currently overlaid skill (index into model.header.skills); None =
346    /// backbone. Set at load time to the statically-overlaid skill so
347    /// `set_active_skill(None)` correctly reverts it (else a static
348    /// skill would silently persist — the union-diff assumes dyn_active
349    /// always mirrors the live overlay). Switched by `set_active_skill`.
350    pub(crate) dyn_active: Option<usize>,
351    /// Pipeline was loaded with a soft blend (materialized working
352    /// tensors, not a single skill index) → dynamic routing refuses:
353    /// there is no single index to revert the blend from.
354    pub(crate) dyn_blend_loaded: bool,
355    /// Layer whose post-residual hidden feeds the router φ (shared by
356    /// swarm skills). None = φ capture off.
357    pub(crate) dyn_phi_layer: Option<usize>,
358    /// EMA of φ at `dyn_phi_layer` over the decode window (on-policy).
359    dyn_phi_ema: Vec<f32>,
360    dyn_phi_seen: usize,
361    /// Hysteresis router driving per-token skill switches during decode
362    /// (None = static/no dynamic routing). Taken out during generation.
363    pub dyn_router: Option<crate::swarm::DynRouter>,
364    /// O(1) Nyström attention setting (CLI/env/header-hint resolved by
365    /// the caller; None = plain cache attention everywhere).
366    o1_cfg: Option<crate::nystrom::O1Cfg>,
367    /// Bumped once per collecting→sealed transition — the GPU state mirror
368    /// re-uploads when it sees a new epoch (each fresh sealed state).
369    o1_epoch: u64,
370    /// Per-layer o1 flags derived from `o1_cfg` (Full layers only).
371    o1_flags: Vec<bool>,
372    /// Emit a structured per-token trace (B4 telemetry channel). Off by
373    /// default — the runtime is silent unless observation is requested.
374    trace: bool,
375    /// Confidence-calibration temperature (B1): reported probability is
376    /// softmax(logits / calib_temp). 1.0 = raw. Set from header.calibration.
377    calib_temp: f32,
378    /// Process-unique id keying this pipeline's device KV mirrors.
379    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
380    graph_kv_id: u64,
381    /// Decode asks the token graph to also run final-norm + lm_head on
382    /// the device (drops the separate per-op lm_head round trip).
383    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
384    graph_want_logits: bool,
385    /// NLL quality gates require the graph's fused head rather than silently
386    /// accepting a CPU head fallback. Generation keeps the historical
387    /// best-effort `graph_want_logits` behavior.
388    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
389    graph_head_required: bool,
390    /// Logits the graph produced for the token just forwarded (taken by
391    /// the decode loop; None = compute on the CPU path).
392    graph_logits: Option<Vec<f32>>,
393    /// Packed Embryo graph model, built lazily on the first eligible
394    /// resident call so other architectures pay no packing cost.
395    embryo_graph: Option<std::sync::Arc<crate::gpu::EmbryoGraphModel>>,
396    /// This pipeline's token graph refused for a STRUCTURAL reason (its
397    /// own weights / layer kinds): not retried. Per pipeline, never
398    /// process-wide: a skill lane whose pack does not build must not flip
399    /// the backbone slot mid-sequence onto the host path (and a new lane
400    /// must not clear the backbone's verdict) — R4/NF-2.
401    graph_refused: std::sync::atomic::AtomicBool,
402    /// Token embeddings are multiplied by this at input (Gemma: √hidden).
403    pub embed_multiplier: f32,
404    /// Attention score scale (1/√head_dim unless the arch overrides —
405    /// Gemma's query_pre_attn_scalar).
406    pub attn_scale: f32,
407    /// Sliding-window attention: (window, every-Nth-layer-is-global
408    /// pattern) — Gemma-3.
409    pub swa: Option<(usize, usize)>,
410    /// Sliding-window tail trimming, `(slack, align)` for
411    /// `LayerKvCache::trim_window`: every layer with a window keeps only
412    /// the rows its window can still read (Spark-X2.5: 576..639 rows per
413    /// 512-window layer instead of the whole context — 27 of the 4B's 36
414    /// layers). Set at load for Spark-X2.5 only (`CMF_SWA_TRIM=0` turns
415    /// it off); None keeps every row, as every other model always has.
416    pub swa_trim: Option<(usize, usize)>,
417    /// Explicit local/global schedule for architectures that cannot be
418    /// represented by Gemma's every-Nth-global convention.
419    pub sliding_layers: Option<Vec<bool>>,
420    /// Per-layer flag: this layer runs WITHOUT RoPE. Recursive Flint applies
421    /// position encoding only on its sliding-window layers; its 14
422    /// full-attention layers have none. The rotary dim of such a layer is 0,
423    /// which is what the attention kernel reads.
424    pub no_rope_layers: Option<Vec<bool>>,
425    /// Natively bounded anchor record (`arch.anchor_core`): the file's
426    /// operator, installed once at load. `Some` = the model is
427    /// bounded-native — `--o1`/`CMF_O1*` are refused, prefix reuse and
428    /// the penalty window are bounded, and no anchor layer stores
429    /// anything per position.
430    pub anchor_core: Option<cortiq_core::AnchorCoreConfig>,
431    /// The `[W][rd/2]` relative-rotation table every bounded layer shares
432    /// (built from `inv_freq` once the RoPE setup is final).
433    bounded_rope: Option<std::sync::Arc<crate::bounded::BoundedRope>>,
434    /// Bounded prefix-reuse key (length + rolling hash + tail) of a
435    /// bounded-native model; `kv_history` stays empty there so nothing
436    /// in the pipeline grows with the dialogue.
437    pub kv_prefix: KvPrefix,
438    /// Prompt positions the last `generate*` call actually forwarded
439    /// (prefix reuse subtracts what the cache already held).
440    pub last_prefill_tokens: usize,
441    /// RoPE table of the sliding (local) layers, when they use their
442    /// own base frequency (Gemma-3: 10k local vs 1M global).
443    pub inv_freq_local: Option<std::sync::Arc<Vec<f32>>>,
444    pub rotary_dim_local: Option<usize>,
445    pub rope_scale: f32,
446    pub rope_scale_local: f32,
447    /// Gemma-4: global layers run their own geometry — (head_dim,
448    /// num_kv_heads); sliding layers keep the base fields.
449    pub global_attn: Option<(usize, usize)>,
450    /// Gemma-4: the global layers' proportional RoPE table (len
451    /// global_head_dim/2, zero-padded tail = identity rotation).
452    pub inv_freq_global: Option<std::sync::Arc<Vec<f32>>>,
453    /// Scale-less RMS normalization of V heads before caching (Gemma-4).
454    pub attn_v_norm: bool,
455    /// HunYuan dense: per-head q/k norm runs after RoPE (see the arch flag).
456    pub qk_norm_after_rope: bool,
457    /// The per-head `self_attn.g_proj` output gate uses sigmoid
458    /// (Spark-X2.5) instead of softplus (Laguna).
459    pub proj_gate_sigmoid: bool,
460    /// Final-logit soft-capping C: logits = C·tanh(logits/C) (Gemma-4).
461    pub final_softcap: Option<f32>,
462    /// Cortiq Embryo hierarchical head: cluster matrix [C, hidden]. The
463    /// flat logits h·Eᵀ are turned into the two-level log-probabilities
464    /// log softmax_c(h·Cᵀ)[c(v)] + log softmax_{s∈c(v)}(h·E_c(v)ᵀ)[v].
465    pub head_clusters: Option<std::sync::Arc<Vec<f32>>>,
466    /// Gemma-2 attention-logit soft-capping (0.0 = off).
467    pub attn_softcap: f32,
468    /// Compute per-token confidence (a full-vocab softmax each
469    /// token). On by default; `bench --core` turns it off to match
470    /// llama-bench's core timing.
471    confidence_on: bool,
472    /// Test-only one-shot forward failure, scoped to this pipeline so
473    /// parallel scoring tests cannot consume one another's injection.
474    #[cfg(test)]
475    nll_test_fail_at: Option<usize>,
476    /// Test-only route override; avoids mutating the process-wide
477    /// `CMF_PREFILL` environment variable while forcing the serial path.
478    #[cfg(test)]
479    nll_test_force_serial: bool,
480}
481
482#[cfg(target_os = "macos")]
483impl Drop for Pipeline {
484    fn drop(&mut self) {
485        // the async replay writes into `kv_cache` Vecs about to be freed
486        let _ = crate::gpu_metal::wait_replay();
487        crate::gpu::kv_mirror_drop(self.graph_kv_id);
488    }
489}
490
491#[cfg(not(target_os = "macos"))]
492impl Drop for Pipeline {
493    fn drop(&mut self) {
494        // The wgpu resident Embryo graph owns recurrent/KV buffers keyed by
495        // this pipeline's sequence id.  Release that sequence image when a
496        // pooled pipeline is dropped; model weights stay cached for reuse.
497        crate::gpu::graph_kv_reset(self.graph_kv_id);
498    }
499}
500
501/// Model weights. Matrices are `QTensor` (owned f32 for small models
502/// and tests — bit-identical to the historical paths — or quantized
503/// bytes zero-copy from the CMF mmap for big models). 1-D norms are
504/// always small and stay f32.
505pub struct PipelineWeights {
506    /// Embedding table: [vocab_size, hidden_size]
507    pub embed_tokens: QTensor,
508    /// Per-layer weights
509    pub layers: Vec<LayerWeights>,
510    /// LM head: [vocab_size, hidden_size]
511    pub lm_head: QTensor,
512    /// Final norm: [hidden_size]
513    pub final_norm: Vec<f32>,
514}
515
516/// One transformer layer: shared norms + MLP, attention by kind.
517pub struct LayerWeights {
518    pub input_norm: Vec<f32>,
519    /// The pre-FFN norm (`post_attention_layernorm` classically;
520    /// `pre_feedforward_layernorm` on Gemma-2/3 sandwich layers).
521    pub post_norm: Vec<f32>,
522    /// Gemma-2/3 sandwich: norm applied to the ATTENTION OUTPUT before
523    /// its residual add (`post_attention_layernorm` there).
524    pub attn_out_norm: Option<Vec<f32>>,
525    /// Gemma-4: the whole layer output is multiplied by this scalar.
526    pub layer_scale: Option<f32>,
527    /// Gemma-2/3 sandwich: norm applied to the FFN OUTPUT before its
528    /// residual add (`post_feedforward_layernorm`).
529    pub ffn_out_norm: Option<Vec<f32>>,
530    pub ffn: FfnKind,
531    pub attn: AttnKind,
532}
533
534/// FFN gate activation: SiLU (SwiGLU family), tanh-GELU (Gemma's GeGLU),
535/// or a source-specific gated contract. A property of the model, carried on
536/// every FFN triple.
537#[derive(Clone, Copy, PartialEq, Debug, Default)]
538pub enum Act {
539    #[default]
540    Silu,
541    GeluTanh,
542    /// Exact erf GELU (HF `hidden_act = "gelu"`: Spark-X2.5).
543    Gelu,
544    /// Kimi-K3 SituAndMul: BOTH halves transform —
545    /// a = β·tanh(g/β)·σ(g), up' = linβ·tanh(u/linβ) (linβ>0), out = a·up'.
546    Situ {
547        beta: f32,
548        linear_beta: f32,
549    },
550    /// MiniMax M3's SwiGLU-OAI: upper-clamped gate, symmetrically clamped
551    /// up branch, `sigmoid(alpha * gate)`, and a `+1` up-branch offset.
552    /// It deliberately has no generic GPU graph arm until a matching kernel
553    /// exists; CPU uses the exact scalar reference in `inference`.
554    SwiGluOai {
555        alpha: f32,
556        limit: f32,
557    },
558}
559
560impl Act {
561    pub fn from_arch(name: &str) -> Self {
562        if name == "gelu_tanh" {
563            Self::GeluTanh
564        } else if name == "gelu" {
565            Self::Gelu
566        } else {
567            Self::Silu
568        }
569    }
570
571    /// Arch-driven constructor (activation name + situ betas).
572    pub fn from_arch_full(arch: &cortiq_core::ModelArch) -> Self {
573        match arch.hidden_act.as_str() {
574            "situ" => Self::Situ {
575                beta: arch.activation_situ_beta.unwrap_or(1.0) as f32,
576                linear_beta: arch.activation_situ_linear_beta.unwrap_or(0.0) as f32,
577            },
578            // `swigluoai` alone is deliberately not a generic activation
579            // alias.  Its alpha/limit are part of MiniMax M3's checked
580            // source contract, and accepting that string on an unrelated
581            // architecture would silently impose M3 math on its FFN.
582            "swigluoai" if arch.arch_name == "minimax_m3_vl" => Self::SwiGluOai {
583                alpha: 1.702,
584                limit: 7.0,
585            },
586            other => Self::from_arch(other),
587        }
588    }
589
590    /// Construct the MiniMax M3 SwiGLU-OAI activation from a checked model
591    /// contract. This is intentionally explicit rather than recognizing an
592    /// arbitrary `hidden_act` string: callers must validate `alpha` and
593    /// `limit` against their source architecture before enabling it.
594    #[inline]
595    pub fn swiglu_oai(alpha: f32, limit: f32) -> Self {
596        Self::SwiGluOai { alpha, limit }
597    }
598
599    #[inline]
600    pub fn apply(self, x: f32) -> f32 {
601        match self {
602            Self::Silu => inference::silu(x),
603            Self::GeluTanh => inference::gelu_tanh(x),
604            Self::Gelu => inference::gelu_erf(x),
605            Self::Situ { beta, .. } => beta * (x / beta).tanh() * (1.0 / (1.0 + (-x).exp())),
606            Self::SwiGluOai { alpha, limit } => {
607                let gate = x.min(limit);
608                gate * (1.0 / (1.0 + (-alpha * gate).exp()))
609            }
610        }
611    }
612
613    /// Gated combine — the FFN contract. Situ transforms the UP half
614    /// too, so callers must use this instead of apply(g)·u.
615    #[inline]
616    pub fn combine(self, g: f32, u: f32) -> f32 {
617        match self {
618            Self::Situ { linear_beta, .. } if linear_beta > 0.0 => {
619                self.apply(g) * (linear_beta * (u / linear_beta).tanh())
620            }
621            Self::SwiGluOai { alpha, limit } => {
622                inference::swiglu_oai(g, u, alpha, 1.0, Some(limit))
623            }
624            _ => self.apply(g) * u,
625        }
626    }
627
628    /// The wgpu graphs' arm for this activation; None = no device kernel
629    /// computes it, and the graph builder must refuse the layer (the dense
630    /// graph FFN once computed SiLU for whatever the model asked for).
631    pub fn graph_act(self) -> Option<crate::gpu::GraphAct> {
632        match self {
633            Self::Silu => Some(crate::gpu::GraphAct::Silu),
634            Self::Gelu => Some(crate::gpu::GraphAct::GeluErf),
635            Self::GeluTanh => Some(crate::gpu::GraphAct::GeluTanh),
636            Self::Situ { .. } | Self::SwiGluOai { .. } => None,
637        }
638    }
639
640    /// The WGPU graph arm.  Keep this deliberately separate from
641    /// `graph_act`: OAI needs its dedicated code rather than a generic SiLU
642    /// fallback.  Only the frozen MiniMax-M3 source constants get a device
643    /// code; a future OAI-like architecture with different constants must add
644    /// and test its own kernel contract.
645    pub(crate) fn wgpu_graph_act(self) -> Option<crate::gpu::GraphAct> {
646        self.moe_job_graph_contract().map(|(act, _)| act)
647    }
648
649    /// The exact device contract for a gate/up/down MoE job.  OAI is not a
650    /// generic "clamped SiLU": code 3 commits a backend to MiniMax M3's
651    /// alpha, asymmetric up transform, and frozen clamp.  Keep the clamp
652    /// alongside the marker so a backend cannot accidentally run code 3 with
653    /// a nearby model's limit.
654    pub(crate) fn moe_job_graph_contract(self) -> Option<(crate::gpu::GraphAct, f32)> {
655        match self {
656            Self::SwiGluOai { alpha, limit }
657                if alpha.to_bits() == 1.702f32.to_bits()
658                    && limit.to_bits() == 7.0f32.to_bits() =>
659            {
660                Some((crate::gpu::GraphAct::SwiGluOaiM3, 7.0))
661            }
662            _ => self.graph_act().map(|act| (act, 0.0)),
663        }
664    }
665}
666
667/// Dense gated triple — the FFN of a dense layer or of one expert.
668pub struct DenseFfn {
669    pub gate_proj: QTensor,
670    pub up_proj: QTensor,
671    pub down_proj: QTensor,
672    /// Gate activation (SiLU default; Gemma: tanh-GELU).
673    pub act: Act,
674    /// `down_proj` stored transposed (`[inter, hidden]`), when the file
675    /// carries it. Only the per-token sparse path reads it: a neuron's
676    /// down weights are a contiguous ROW there, so the token's chosen
677    /// neurons are the only bytes touched. `None` = the ordinary layout,
678    /// and the sparse path stays off.
679    pub down_t: Option<QTensor>,
680    /// Task tubes (spec: defragged task-conditional width). The three
681    /// matrices above are the CORE — the neurons every task computes;
682    /// each tube is an independently quantized slice of the SAME layer
683    /// holding the neurons only some tasks need. A tube is a normal
684    /// tensor triple, so every kernel runs it unchanged, and the bytes
685    /// of an inactive tube are never read. Empty = ordinary dense FFN.
686    pub segs: Vec<FfnSeg>,
687}
688
689/// One task tube: a contiguous slice of a layer's FFN neurons, stored
690/// as its own `[w, hidden]` / `[hidden, w]` triple. `start` is the
691/// neuron's index in the layer's FULL space (core first, then tubes in
692/// order) — the bit a task mask sets to switch this tube on.
693pub struct FfnSeg {
694    pub gate: QTensor,
695    pub up: QTensor,
696    pub down: QTensor,
697    pub start: usize,
698    pub width: usize,
699}
700
701/// FFN operator of a layer, decided by tensor presence at load time
702/// (router `mlp.gate.weight` in the directory = MoE layer).
703pub enum FfnKind {
704    Dense(DenseFfn),
705    /// Mixture-of-Experts (Qwen2-MoE / Qwen3-MoE): softmax over ALL
706    /// expert logits → top-k, optional renorm; experts stay quantized
707    /// in mmap — only the selected ones are touched per token.
708    Moe(MoeFfn),
709    /// Gemma-4 MoE: a dense MLP branch AND a routed-expert branch in
710    /// the SAME layer, each with its own norm sandwich. The dense
711    /// branch reads the pre-FFN-normed input; the expert branch (and
712    /// the router) read the RAW residual through `pre_norm_2`:
713    ///   d = post_norm_1(dense(x̂));  m = post_norm_2(Σwₑ·FFNₑ(pre_norm_2(h)))
714    ///   ffn_out = d + m   (the caller's ffn_out_norm + residual follow)
715    DenseMoe(Box<DenseMoeFfn>),
716}
717
718/// Gemma-4 dual-branch FFN (see `FfnKind::DenseMoe`).
719pub struct DenseMoeFfn {
720    pub dense: DenseFfn,
721    pub moe: MoeFfn,
722    /// post_feedforward_layernorm_1 — dense-branch output norm.
723    pub post_norm_1: Vec<f32>,
724    /// pre_feedforward_layernorm_2 — expert-branch input norm (applied
725    /// to the RAW residual, not the pre-FFN-normed activation).
726    pub pre_norm_2: Vec<f32>,
727    /// post_feedforward_layernorm_2 — expert-branch output norm.
728    pub post_norm_2: Vec<f32>,
729}
730
731pub struct MoeFfn {
732    /// Router `mlp.gate.weight` [num_experts, hidden].
733    pub router: QTensor,
734    pub experts: Vec<DenseFfn>,
735    pub top_k: usize,
736    pub norm_topk_prob: bool,
737    /// Router scores per-expert with a sigmoid (LFM2-MoE / DeepSeek-V3
738    /// `noaux_tc`) instead of a softmax over all experts (Qwen).
739    pub router_sigmoid: bool,
740    /// Addend in the selected-score denominator of a sigmoid router.
741    /// Most supported checkpoints use the historical `1e-6` floor; Flint's
742    /// reference routing uses `1e-20`; MiniMax M3.1 preserves the same FP32
743    /// `sum + 1e-20` law. This is per-MoE rather than a global family switch
744    /// because the same sigmoid routing law has several contracts.
745    pub sigmoid_denom_eps: f32,
746    /// Per-expert selection bias `mlp.expert_bias` [num_experts]
747    /// (LFM2-MoE): added to the sigmoid scores for the top-k CHOICE only;
748    /// the gathered weights use the unbiased scores. None = no bias.
749    pub expert_bias: Option<Vec<f32>>,
750    /// Top-k weights are multiplied by this after the optional renorm
751    /// (LFM2-MoE `routed_scaling_factor`; 1.0 = off).
752    pub routed_scaling: f32,
753    /// Adaptive routing (CMF_MOE_TAU, opt-in): keep the smallest
754    /// prefix of the top-k whose renormalized mass reaches τ —
755    /// confident tokens touch 1–2 experts, flat ones keep all k.
756    /// MoE decode is memory-bound, so skipped experts are skipped
757    /// weight traffic. None = classic fixed top-k (bit-identical).
758    pub route_tau: Option<f32>,
759    /// Always-on shared expert. Qwen2-MoE carries an additional sigmoid
760    /// gate; Laguna adds the shared expert unconditionally (`None`).
761    pub shared: Option<(DenseFfn, Option<QTensor>)>,
762    /// Expert-selection counters (truncated Fisher B-field of claim 12:
763    /// routing frequency during calibration). Filled by every forward,
764    /// read by the CLI via CMF_MOE_STATS. RefCell: decode is single-threaded.
765    pub stats: std::cell::RefCell<Vec<u64>>,
766    /// Per-CHANNEL sum of squares of this FFN's input, accumulated over a
767    /// calibration run (`CMF_RMS_TRACE`). These are the RMS activation
768    /// traces AWNP needs: raw weight magnitude says every channel matters
769    /// equally, and the question AWNP asks is whether the ACTIVATIONS
770    /// disagree. Off unless the env var is set — an f64 add per channel
771    /// per token is cheap, but not free.
772    pub act_sq: std::cell::RefCell<Vec<f64>>,
773    /// Raw FFN-input rows captured for the layers named by `CMF_ACT_DUMP`
774    /// (`"9,19"`). AWNP is nullspace PROJECTION: after dropping channels the
775    /// survivors are refitted to absorb what was removed, and how much they
776    /// can absorb depends on the activation COVARIANCE, not on per-channel
777    /// RMS. Per-channel numbers can only bound the cost from above.
778    pub act_rows: std::cell::RefCell<Vec<f32>>,
779    /// Task mask over routed experts (DTG-MA over MoE, claim-12 B-field
780    /// applied): `false` experts are excluded from selection, the
781    /// softmax renormalizes over the allowed set. Built by the loader
782    /// from CMF_MOE_MASK=<stats.json> + CMF_MOE_MASK_COVER. None = all.
783    pub mask: Option<Vec<bool>>,
784    /// Gemma-4: per-expert weight scale applied AFTER the top-k renorm
785    /// (`router.per_expert_scale`). None = 1.0 everywhere.
786    pub per_expert_scale: Option<Vec<f32>>,
787    /// Gemma-4: the router reads a SCALE-LESS rms-norm of its input
788    /// (the constant gain router.scale·√hidden is folded into the
789    /// router weights at convert time).
790    pub router_input_norm: bool,
791    /// Cortiq Embryo: resonance routing (P1) — the "logits" are
792    /// bias_e − ‖(x−μ_e) − U_eᵀU_e(x−μ_e)‖², argmax = the expert whose
793    /// descriptor reconstructs the input best. `router` is a placeholder.
794    pub resonance: Option<Resonance>,
795    /// Growth records (`kind = "expert_append"`, spec §9.5.1) mounted
796    /// behind the trunk experts, one entry per grown expert IN THE ORDER
797    /// they sit in `experts` (the tail `experts[experts.len() - grown.len()..]`).
798    /// The trunk keeps `experts.len() - grown.len()` experts. Empty on a
799    /// gated MoE, on a file without records and under `CMF_GROWTH=off`.
800    pub grown: Vec<GrownExpert>,
801}
802
803/// One grown expert of an `expert_append` record as the loader mounted it.
804#[derive(Debug, Clone, PartialEq, Eq)]
805pub struct GrownExpert {
806    /// The record's skill id.
807    pub record: String,
808    /// Position of the record in `header.skills`.
809    pub record_index: usize,
810    pub layer: usize,
811    /// The index the tensor name declares (`experts.{e}` — the chain rule
812    /// of the format); the executed position may be smaller when an
813    /// earlier record of the layer is not mounted.
814    pub expert: usize,
815}
816
817/// Per-expert resonance descriptors of one MoE layer (`mlp.desc.*`).
818pub struct Resonance {
819    /// [E, hidden]
820    pub mu: Vec<f32>,
821    /// [E, k, hidden] orthonormal directions (k may be 0)
822    pub u: Vec<f32>,
823    pub k: usize,
824    /// [E] selection bias (loss-free balancing, trained online)
825    pub bias: Vec<f32>,
826    /// [E] reconstruction-error shell: an expert whose error
827    /// `‖(x−μ)⊥U‖² = d² − proj` exceeds its shell scores `−∞` (it never
828    /// wins). `+inf` = no shell — every trunk expert; a grown expert
829    /// (`expert_append`) carries the finite `desc.shell` its record stores.
830    /// Empty = no shell anywhere (legacy constructors).
831    pub shell: Vec<f32>,
832}
833
834/// `CMF_GROWTH_SHELL` state: 0 = not yet read from the environment, 1 = on,
835/// 2 = off. Process-wide, like the environment it mirrors.
836static GROWTH_SHELL: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
837
838/// Is the growth shell applied (`Resonance::scores` −∞ rule, the resident
839/// graph's packed shell)? `CMF_GROWTH_SHELL=off` disables it for
840/// measurement; [`set_growth_shell`] overrides the environment in-process
841/// (`growth-eval --shell`). Default: on.
842pub fn growth_shell_enabled() -> bool {
843    use std::sync::atomic::Ordering;
844    match GROWTH_SHELL.load(Ordering::Relaxed) {
845        1 => true,
846        2 => false,
847        _ => {
848            let off = std::env::var("CMF_GROWTH_SHELL")
849                .map(|v| v.eq_ignore_ascii_case("off") || v == "0")
850                .unwrap_or(false);
851            GROWTH_SHELL.store(if off { 2 } else { 1 }, Ordering::Relaxed);
852            !off
853        }
854    }
855}
856
857/// Switch the growth shell on/off for this process (`None` = re-read
858/// `CMF_GROWTH_SHELL` on the next query). A pipeline packed into the
859/// resident graph BEFORE the switch keeps the shell it was packed with —
860/// build a new pipeline after switching.
861pub fn set_growth_shell(on: Option<bool>) {
862    GROWTH_SHELL.store(
863        match on {
864            Some(true) => 1,
865            Some(false) => 2,
866            None => 0,
867        },
868        std::sync::atomic::Ordering::Relaxed,
869    );
870}
871
872impl Resonance {
873    /// Does any expert carry a finite shell (a mounted growth record)?
874    pub fn has_shell(&self) -> bool {
875        self.shell.iter().any(|s| s.is_finite())
876    }
877
878    /// The shell the runtime applies right now: the stored one, or all
879    /// `+inf` when the shell is switched off (`CMF_GROWTH_SHELL=off`).
880    pub fn effective_shell(&self, ne: usize) -> Vec<f32> {
881        let mut out = vec![f32::INFINITY; ne];
882        if growth_shell_enabled() {
883            for (o, s) in out.iter_mut().zip(&self.shell) {
884                *o = *s;
885            }
886        }
887        out
888    }
889
890    /// Routing scores for one input row (higher = better). A grown
891    /// expert whose reconstruction error lies outside its shell gets
892    /// `−∞` (unless the shell is switched off); trunk rows are the exact
893    /// bit pattern they were before growth.
894    pub fn scores(&self, x: &[f32], out: &mut [f32]) {
895        let h = x.len();
896        let ne = out.len();
897        let shell_on = growth_shell_enabled() && !self.shell.is_empty();
898        for e in 0..ne {
899            let mu = &self.mu[e * h..(e + 1) * h];
900            let mut d2 = 0.0f32;
901            for j in 0..h {
902                let d = x[j] - mu[j];
903                d2 += d * d;
904            }
905            let mut proj = 0.0f32;
906            for i in 0..self.k {
907                let u = &self.u[(e * self.k + i) * h..(e * self.k + i + 1) * h];
908                let mut p = 0.0f32;
909                for j in 0..h {
910                    p += (x[j] - mu[j]) * u[j];
911                }
912                proj += p * p;
913            }
914            let err = d2 - proj;
915            out[e] = self.bias.get(e).copied().unwrap_or(0.0) - err;
916            if shell_on && err > self.shell.get(e).copied().unwrap_or(f32::INFINITY) {
917                out[e] = f32::NEG_INFINITY;
918            }
919        }
920    }
921}
922
923/// Attention operator of a layer. Extension point: new operators are
924/// new variants here + a forward in their own module.
925pub enum AttnKind {
926    /// GQA softmax attention (+ optional Qwen3.5 qk-norm / output gate).
927    Full {
928        wq: QTensor,
929        wk: QTensor,
930        wv: QTensor,
931        wo: QTensor,
932        q_norm: Option<Vec<f32>>,
933        k_norm: Option<Vec<f32>>,
934        output_gate: bool,
935        /// Laguna: a separate softplus projection applied to the attention
936        /// output before O. The bool means one scalar per head (broadcast
937        /// across head_dim); false means one scalar per element.
938        softplus_gate: Option<(QTensor, bool)>,
939        /// Qwen2-family projection biases (q, k, v).
940        bias: Option<(Vec<f32>, Vec<f32>, Vec<f32>)>,
941    },
942    /// Canonical linear core (VMF phase attention).
943    Linear(VmfPhaseWeights),
944    /// Faithful vendor linear operator (Qwen3.5 GatedDeltaNet).
945    LinearGdn(GdnWeights),
946    /// LFM2 gated short-convolution mixer (no KV cache; conv ring state
947    /// lives in the layer's `linear_state`).
948    ShortConv(ShortConvWeights),
949    /// DeepSeek-V2 Multi-head Latent Attention. v1 executes it as
950    /// expand-to-MHA: the latent is projected per token, K/V expand to
951    /// every head and live in the ordinary cache (K head layout
952    /// [rope | nope] so the standard partial rotary covers the shared
953    /// rope key; V rows are zero-padded to the K head_dim and the pad
954    /// is sliced off before O). Latent-resident cache is a later
955    /// optimization, not a semantic change.
956    Mla(Box<MlaWeights>),
957    /// Kimi Delta Attention (Kimi Linear / Kimi-K3): per-channel decayed
958    /// delta rule, separate q/k/v short convs, sigmoid-gated output norm.
959    /// State lives in the layer's `linear_state` (no KV cache).
960    Kda(Box<crate::linear_core::KdaWeights>),
961    /// Natively bounded softmax anchor `swa_sink_v1` (Embryo-O1): ring of
962    /// the last W raw keys with relative RoPE + trained NoPE sinks, one
963    /// softmax. State is the fixed-size ring in `LayerKvCache::bounded`;
964    /// nothing is stored per position (see `crate::bounded`).
965    Bounded(Box<crate::bounded::BoundedWeights>),
966}
967
968/// DeepSeek-V2 MLA projections (see `AttnKind::Mla`).
969pub struct MlaWeights {
970    /// `[nh·(rope+nope), hidden]` (or `[…, q_lora]` when compressed) —
971    /// the converter permutes each head rope-first so rotary_dim =
972    /// qk_rope works unchanged.
973    pub q_proj: QTensor,
974    /// Compressed q (K3/V3 class): x → q_a `[q_lora, hidden]` →
975    /// rms(q_a_norm) → q_proj (= q_b). None = direct q (V2-Lite).
976    pub q_a: Option<QTensor>,
977    pub q_a_norm: Option<Vec<f32>>,
978    /// `kv_a_proj_with_mqa` `[lora + rope, hidden]` (latent first).
979    pub kv_a: QTensor,
980    /// RMS-norm weights over the latent (`kv_a_layernorm`, [lora]).
981    pub kv_a_norm: Vec<f32>,
982    /// `[nh·(nope+v), lora]` — per head [k_nope | v].
983    pub kv_b: QTensor,
984    /// `[hidden, nh·v]`.
985    pub o_proj: QTensor,
986    pub nh: usize,
987    pub qk_rope: usize,
988    pub qk_nope: usize,
989    pub v_dim: usize,
990    pub lora: usize,
991    /// Softmax scale (1/√(rope+nope), YaRN-mscale-corrected at load).
992    pub scale: f32,
993    /// Kimi Linear NoPE: skip the rotary entirely (layout unchanged).
994    pub nope: bool,
995}
996
997/// Multi-token-prediction head (DeepSeek/Qwen style, spec §2.1):
998/// `x = eh_proj·[enorm(embed(next)); hnorm(hidden)]` → one transformer
999/// block over its own KV → shared lm_head. Drafts the token after next;
1000/// the main model verifies, so output is exact — MTP only buys speed.
1001pub struct MtpModule {
1002    pub enorm: Vec<f32>,
1003    pub hnorm: Vec<f32>,
1004    /// [hidden, 2·hidden]
1005    pub eh_proj: QTensor,
1006    pub layer: LayerWeights,
1007    pub final_norm: Vec<f32>,
1008    pub kv: crate::kv_cache::LayerKvCache,
1009}
1010
1011/// Teacher-forced activation statistics for one MTP draft FFN.
1012///
1013/// `mass[i]` is `Σ_rows |activation_i|`; `squared_mass[i]` is
1014/// `Σ_rows activation_i²`, where `activation = act(gate) · up` immediately
1015/// before the draft layer's down projection. `observed_positions` is the
1016/// number of teacher-forced rows that contributed to both arrays.
1017///
1018/// [`Pipeline::probe_mtp_ffn`] returns one of these for each actual draft
1019/// layer.  It intentionally has a separate type/API from
1020/// [`Pipeline::probe_ffn_mass`] so a compactor cannot accidentally substitute
1021/// trunk statistics for an MTP sidecar whose FFN has a different width.
1022#[derive(Clone, Debug, PartialEq)]
1023pub struct MtpFfnProbe {
1024    pub mass: Vec<f64>,
1025    pub squared_mass: Vec<f64>,
1026    pub observed_positions: usize,
1027}
1028
1029/// A Metal verify graph after its sync: what the commit needs — the
1030/// graph (per-layer replay scratch), the GDN layers in encode order (their
1031/// CPU states receive the replay), and the attention layers with the CPU
1032/// row count they were encoded against (the accepted rows are pulled from
1033/// the mirror from there).
1034/// One item of the Metal rows-graph plan.
1035#[cfg(target_os = "macos")]
1036enum MetalRowsItem<'a> {
1037    Gdn {
1038        run: Vec<crate::gpu_metal::GdnGpuLayer<'a>>,
1039        first: usize,
1040    },
1041    Attn {
1042        l: crate::gpu_metal::AttnGpuLayer<'a>,
1043        li: usize,
1044        q_norm: Option<&'a [f32]>,
1045        k_norm: Option<&'a [f32]>,
1046        output_gate: bool,
1047    },
1048}
1049
1050#[cfg(target_os = "macos")]
1051struct MetalVerifyPending {
1052    graph: crate::gpu_metal::VerifyGraph,
1053    gdn_layers: Vec<usize>,
1054    attn_layers: Vec<(usize, usize)>,
1055}
1056
1057/// A round's batched MTP warm-up, submitted but not yet waited
1058/// (`mtp_warm_batch_submit` → `mtp_warm_batch_finish`): the trunk commit's
1059/// GDN replay is queued between the two.
1060#[cfg(target_os = "macos")]
1061struct MetalWarmPending {
1062    graph: crate::gpu_metal::VerifyGraph,
1063    cpu_stored: usize,
1064    b: usize,
1065}
1066
1067#[cfg(target_os = "macos")]
1068enum MetalRowsRun {
1069    /// Capability/preflight refusal before a command buffer was committed.
1070    Declined,
1071    /// A graph was admitted and then failed; callers must clear the sequence
1072    /// rather than replaying it through CPU/serial state.
1073    Failed,
1074    Completed(MetalVerifyPending),
1075}
1076
1077#[cfg(target_os = "macos")]
1078enum MetalPrefillOutcome {
1079    Declined,
1080    Failed,
1081    Completed(Vec<f32>),
1082}
1083
1084#[cfg(target_os = "macos")]
1085enum MetalBatchNllOutcome {
1086    Declined,
1087    Failed(String),
1088    Completed(f64, usize),
1089}
1090
1091/// The speculation trial's phases (see the decode loop): up to five timed
1092/// speculative rounds, eight timed plain tokens, then the faster arm until a
1093/// re-check. Two consecutive rounds with no accepted draft skip directly to
1094/// the plain measurement: another speculative round cannot pay when it emits
1095/// exactly the one token the plain loop would have produced.
1096#[derive(Clone, Copy)]
1097enum SpecTrial {
1098    Spec {
1099        t0: std::time::Instant,
1100        gen0: usize,
1101        rounds: usize,
1102    },
1103    Plain {
1104        t0: std::time::Instant,
1105        gen0: usize,
1106    },
1107    Decided {
1108        spec: bool,
1109        recheck_at: usize,
1110    },
1111}
1112
1113/// `CMF_GRAPH_SPEC_TIME`: 0 = off, 1 = one line per speculative round
1114/// plus the host stamps of any OUTLIER round (wall > 1.4× the running
1115/// median), 2 = the host stamps of every round.
1116pub(crate) fn spec_time_level() -> u8 {
1117    static L: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
1118    *L.get_or_init(|| match std::env::var("CMF_GRAPH_SPEC_TIME") {
1119        Ok(v) => v.trim().parse::<u8>().map(|n| n.max(1)).unwrap_or(1),
1120        Err(_) => 0,
1121    })
1122}
1123
1124/// Decide whether the batched speculative verifier is admitted by the
1125/// user/backend policy, before capability checks.  Native Metal's row-batch
1126/// verifier is deliberately fail-closed by default: it has produced a
1127/// different *emitted* greedy token than the serial path on a real model.
1128/// It remains available only through the documented explicit experiment
1129/// spelling, `CMF_GRAPH_SPEC=1`; other backends retain their historical
1130/// nonzero override behaviour.
1131fn graph_spec_wanted_by_policy(
1132    spec_env: Option<&str>,
1133    spec_default_ok: bool,
1134    penalized: bool,
1135    metal_wgpu: bool,
1136    native_metal: bool,
1137) -> bool {
1138    match spec_env {
1139        Some("0") => false,
1140        // Keep the native path genuinely explicit.  In particular, a
1141        // typo/nonzero value must not silently re-enable an unqualified
1142        // verifier just because historical non-Metal parsing treated it as
1143        // force-on.
1144        Some("1") => true,
1145        Some(_) => !native_metal,
1146        // The discrete/wgpu default stays as it was; only native Metal has
1147        // a quality gate here.
1148        None => spec_default_ok && !penalized && !metal_wgpu && !native_metal,
1149    }
1150}
1151
1152/// The round's host stamps: `spec_stamp(name)` records the time since
1153/// the previous stamp (the section that just ended) — from anywhere on
1154/// the round's call chain (the Metal verify, the draft step, the commit),
1155/// no plumbing. Off (a single atomic load) unless `CMF_GRAPH_SPEC_TIME`
1156/// is set; one decode thread at a time is assumed (diagnostics).
1157struct SpecStampLog {
1158    t_last: std::time::Instant,
1159    items: Vec<(&'static str, f32)>,
1160}
1161
1162static SPEC_STAMPS: std::sync::Mutex<Option<SpecStampLog>> = std::sync::Mutex::new(None);
1163
1164pub(crate) fn spec_stamp(name: &'static str) {
1165    if spec_time_level() == 0 {
1166        return;
1167    }
1168    if let Ok(mut g) = SPEC_STAMPS.lock() {
1169        if let Some(log) = g.as_mut() {
1170            let now = std::time::Instant::now();
1171            log.items
1172                .push((name, (now - log.t_last).as_secs_f32() * 1e3));
1173            log.t_last = now;
1174        }
1175    }
1176}
1177
1178fn spec_stamps_begin() {
1179    if spec_time_level() == 0 {
1180        return;
1181    }
1182    if let Ok(mut g) = SPEC_STAMPS.lock() {
1183        *g = Some(SpecStampLog {
1184            t_last: std::time::Instant::now(),
1185            items: Vec::with_capacity(64),
1186        });
1187    }
1188}
1189
1190fn spec_stamps_take() -> Vec<(&'static str, f32)> {
1191    SPEC_STAMPS
1192        .lock()
1193        .ok()
1194        .and_then(|mut g| g.take())
1195        .map(|l| l.items)
1196        .unwrap_or_default()
1197}
1198
1199/// One line: every stamp name in first-seen order with its total over the
1200/// round and, when it fired more than once (the draft steps), the count.
1201fn spec_stamps_format(items: &[(&'static str, f32)]) -> String {
1202    let mut agg: Vec<(&'static str, f32, u32)> = Vec::with_capacity(items.len());
1203    for &(n, ms) in items {
1204        match agg.iter_mut().find(|e| e.0 == n) {
1205            Some(e) => {
1206                e.1 += ms;
1207                e.2 += 1;
1208            }
1209            None => agg.push((n, ms, 1)),
1210        }
1211    }
1212    let mut s = String::with_capacity(agg.len() * 16);
1213    for (n, ms, k) in agg {
1214        if k > 1 {
1215            s.push_str(&format!("{n} {ms:.1}/{k} "));
1216        } else {
1217            s.push_str(&format!("{n} {ms:.1} "));
1218        }
1219    }
1220    s
1221}
1222
1223/// The speculation monitor: exponential averages of a round's wall time
1224/// and of the tokens it produced, and the plain token's wall time — the
1225/// three numbers the keep/stop rule needs. A round pays when
1226/// `tokens_per_round · plain_ms > round_ms · 1.03`. The one-shot trial
1227/// (four rounds against eight tokens) mis-called prose: the first rounds
1228/// after a prompt are formulaic and accept well, the body does not (an
1229/// essay measured 39 against a plain 44.8 with the trial saying
1230/// "speculate"), so the rule now runs on EVERY round and stops after four
1231/// consecutive losing rounds; a stopped speculation is retried 128 tokens
1232/// later.
1233///
1234/// Native Metal (`metal: true`) does not pay the eight plain tokens up
1235/// front: on the 27B a plain token is ~150 ms, so the trial alone cost
1236/// ~1.2 s of every answer. There the plain phase is (a) skipped while the
1237/// rounds land at least `SPEC_PROXY_TOKENS` tokens each — a k=7 round on
1238/// Metal costs ~1.9 plain tokens (286 against 148 ms measured on the M4),
1239/// so 3.5 tokens/round cannot lose on any Metal round/plain ratio seen —
1240/// and (b) otherwise bounded to the fewest tokens that time it: two, or
1241/// as many as fit in `SPEC_PLAIN_MIN_MS` (a 150-ms token measures itself;
1242/// a 10-ms one needs the eight). The keep/stop rule itself is unchanged:
1243/// the moment a plain rate exists, it decides.
1244#[derive(Default, Clone, Copy)]
1245struct SpecMon {
1246    round_ms: f64,
1247    tokens: f64,
1248    plain_ms: f64,
1249    n: u32,
1250    fails: u32,
1251    /// Consecutive rounds that produced only their verifier-tip token (zero
1252    /// accepted MTP drafts). This is deliberately counted before the warm-up
1253    /// round is excluded from timing: two real zero-acceptance rounds are
1254    /// enough to stop charging an obviously losing speculative arm.
1255    zero_accept_rounds: u8,
1256    metal: bool,
1257}
1258
1259/// Tokens per round at or above which a Metal round pays without a plain
1260/// measurement (see `SpecMon`).
1261const SPEC_PROXY_TOKENS: f64 = 3.5;
1262/// The Metal plain phase: at least two tokens, and more until this much
1263/// wall time has been timed (up to the eight the other backends time).
1264const SPEC_PLAIN_MIN_MS: f64 = 200.0;
1265
1266impl SpecMon {
1267    fn round(&mut self, dt_ms: f64, produced: usize) {
1268        if produced <= 1 {
1269            self.zero_accept_rounds = self.zero_accept_rounds.saturating_add(1);
1270        } else {
1271            self.zero_accept_rounds = 0;
1272        }
1273        self.n += 1;
1274        if self.n == 1 {
1275            return; // round 1 pays the batch scratch and the draft mirror
1276        }
1277        let a = if self.n == 2 { 1.0 } else { 0.3 };
1278        self.round_ms += a * (dt_ms - self.round_ms);
1279        self.tokens += a * (produced as f64 - self.tokens);
1280    }
1281    fn pays(&self) -> bool {
1282        if self.plain_ms > 0.0 {
1283            self.tokens * self.plain_ms > self.round_ms * 1.03
1284        } else {
1285            self.metal && self.tokens >= SPEC_PROXY_TOKENS
1286        }
1287    }
1288    fn repeated_zero_acceptance(&self) -> bool {
1289        self.zero_accept_rounds >= 2
1290    }
1291    /// Has the plain phase timed enough tokens to decide?
1292    fn plain_done(&self, t0: std::time::Instant, gen0: usize, generated: usize) -> bool {
1293        let n = generated.saturating_sub(gen0);
1294        if n >= 8 {
1295            return true;
1296        }
1297        self.metal && n >= 2 && t0.elapsed().as_secs_f64() * 1e3 >= SPEC_PLAIN_MIN_MS
1298    }
1299}
1300
1301/// Ids of the consumed prefix the bounded reuse key remembers literally
1302/// (the rest is covered by the rolling hash).
1303pub const KV_PREFIX_TAIL: usize = 128;
1304
1305/// Bounded prefix-reuse key: how many ids the cache holds, a rolling
1306/// hash of ALL of them and the last [`KV_PREFIX_TAIL`] ids literally.
1307/// Answers "does this prompt strictly extend what is cached" exactly
1308/// (hash over the whole consumed prefix + literal tail) without keeping
1309/// the dialogue — the record is a fixed size whatever the session length.
1310#[derive(Debug, Clone, Default)]
1311pub struct KvPrefix {
1312    len: usize,
1313    hash: u64,
1314    tail: Vec<u32>,
1315    /// Owner of the state this key describes: the resident device graph
1316    /// (true) or the host. A turn continues the prefix only on its owner
1317    /// (R4: a host continuation of a device sequence reads an empty host
1318    /// state; a device continuation of a host sequence has no image).
1319    device: bool,
1320}
1321
1322impl KvPrefix {
1323    #[inline]
1324    fn fold(mut h: u64, ids: &[u32]) -> u64 {
1325        for &id in ids {
1326            h ^= id as u64;
1327            h = h.wrapping_mul(0x100000001b3);
1328            h ^= h >> 29;
1329        }
1330        h
1331    }
1332
1333    pub fn clear(&mut self) {
1334        self.len = 0;
1335        self.hash = 0xcbf29ce484222325;
1336        self.tail.clear();
1337        self.device = false;
1338    }
1339
1340    /// Was the prefix built on the resident device graph?
1341    pub fn on_device(&self) -> bool {
1342        self.device
1343    }
1344
1345    /// Tag the owner of the recorded prefix.
1346    pub fn set_on_device(&mut self, device: bool) {
1347        self.device = device;
1348    }
1349
1350    /// Ids the cache holds (the forwarded prefix).
1351    pub fn len(&self) -> usize {
1352        self.len
1353    }
1354
1355    pub fn is_empty(&self) -> bool {
1356        self.len == 0
1357    }
1358
1359    /// Literal tail currently kept (≤ `KV_PREFIX_TAIL`).
1360    pub fn tail_len(&self) -> usize {
1361        self.tail.len()
1362    }
1363
1364    /// Replace the key with `ids` (a fresh sequence).
1365    pub fn set(&mut self, ids: &[u32]) {
1366        self.clear();
1367        self.extend(ids);
1368    }
1369
1370    /// Append `more` to the consumed prefix (an extension-only turn).
1371    pub fn extend(&mut self, more: &[u32]) {
1372        if self.len == 0 && self.hash == 0 {
1373            self.hash = 0xcbf29ce484222325;
1374        }
1375        self.hash = Self::fold(self.hash, more);
1376        self.len += more.len();
1377        if more.len() >= KV_PREFIX_TAIL {
1378            self.tail.clear();
1379            self.tail
1380                .extend_from_slice(&more[more.len() - KV_PREFIX_TAIL..]);
1381        } else {
1382            let drop = (self.tail.len() + more.len()).saturating_sub(KV_PREFIX_TAIL);
1383            self.tail.drain(..drop);
1384            self.tail.extend_from_slice(more);
1385        }
1386    }
1387
1388    /// Cached positions when `ids` strictly extends the consumed prefix,
1389    /// 0 otherwise. The tail is compared literally first (cheap), then
1390    /// the hash over the whole prefix must agree.
1391    pub fn extension(&self, ids: &[u32]) -> usize {
1392        if self.len == 0 || ids.len() <= self.len {
1393            return 0;
1394        }
1395        let t = self.tail.len();
1396        if ids[self.len - t..self.len] != self.tail[..] {
1397            return 0;
1398        }
1399        if Self::fold(0xcbf29ce484222325, &ids[..self.len]) != self.hash {
1400            return 0;
1401        }
1402        self.len
1403    }
1404}
1405
1406/// Result of a generation call.
1407pub struct GenerateResult {
1408    pub text: String,
1409    pub token_ids: Vec<u32>,
1410    pub prompt_tokens: usize,
1411    pub tokens_generated: usize,
1412    pub finish_reason: String,
1413    /// Speculative-decode stats (0/0 when MTP is absent or inactive).
1414    pub mtp_drafted: usize,
1415    pub mtp_accepted: usize,
1416    /// Per-generated-token confidence = softmax probability of the token
1417    /// that was actually emitted (softmax probability on the chosen state). High =
1418    /// the model was sure; low = it was guessing. Same length as the
1419    /// generated slice of `token_ids`.
1420    pub token_confidence: Vec<f32>,
1421    /// Structured per-token telemetry (B4 channel). Empty unless
1422    /// `set_trace(true)`; otherwise same length as the generated slice.
1423    pub traces: Vec<TokenTrace>,
1424}
1425
1426/// One row of the structured telemetry trace (B4): the model's internal
1427/// routing state at the moment a token was emitted. Every field is a
1428/// quantity the runtime already computes — nothing is inferred or
1429/// estimated (anti-principle: only measured bytes).
1430#[derive(Clone, Debug)]
1431pub struct TokenTrace {
1432    /// 0-based index within the generated slice.
1433    pub t: usize,
1434    /// The emitted token id.
1435    pub token_id: u32,
1436    /// Softmax probability on the emitted token — how sure the model was.
1437    pub confidence: f32,
1438    /// Skill in force while this token was generated (None = backbone).
1439    pub active_skill: Option<String>,
1440    /// Recon error E = ‖r−BBᵀr‖²/‖φ‖² at the last routing eval — coherence
1441    /// with the active skill's subspace (low = coherent). None = no router
1442    /// or not yet evaluated.
1443    pub recon: Option<f32>,
1444    /// The router changed the active skill right after this token (a
1445    /// domain boundary crossed under the hysteresis barrier).
1446    pub switched: bool,
1447}
1448
1449/// Calibrated softmax probability of `id` under `logits` (the confidence on
1450/// the emitted token) — the confidence signal, cheap from logits already
1451/// computed for sampling. `temp` is the calibration temperature (B1):
1452/// softmax(logits / temp); 1.0 = raw.
1453#[cfg_attr(not(test), allow(dead_code))]
1454fn top1_prob_t(logits: &[f32], id: u32, temp: f32) -> f32 {
1455    let t = if temp > 1e-3 { temp } else { 1.0 };
1456    let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
1457    let sum: f32 = logits.iter().map(|&v| ((v - max) / t).exp()).sum();
1458    if sum > 0.0 {
1459        (((logits[id as usize] - max) / t).exp()) / sum
1460    } else {
1461        0.0
1462    }
1463}
1464
1465/// prefill-GEMM enabled? (CMF_PREFILL=seq — emergency fallback to the
1466/// sequential path.)
1467fn prefill_batched() -> bool {
1468    std::env::var("CMF_PREFILL")
1469        .map(|v| v != "seq")
1470        .unwrap_or(true)
1471}
1472
1473/// Decide the graph NLL route without conflating graph quality with the
1474/// optional native-Metal fused head. A hidden-state graph remains a valid
1475/// quality route on Vulkan/Wgpu; only native Metal requires graph logits.
1476#[inline]
1477fn nll_graph_policy(unmasked: bool, prefer_graph: bool, native_metal: bool) -> (bool, bool) {
1478    let graph_quality = unmasked && prefer_graph;
1479    let fused_head_quality = graph_quality && native_metal;
1480    (graph_quality, fused_head_quality)
1481}
1482
1483/// Input to the layer-major batched span walk: token ids (embeds itself,
1484/// full-stack and coordinator prefill) or ready boundary hiddens (the
1485/// network worker's side of a split).
1486#[derive(Clone, Copy)]
1487enum PrefillIn<'a> {
1488    Ids(&'a [u32]),
1489    Hidden(&'a [f32]),
1490}
1491
1492/// The batched prefill walks `weights.layers`. Architectures that load
1493/// their own stack (gemma-3n's AltUp replicas, DeepSeek-V4's hyper-
1494/// connections) leave that empty and must go position by position — asking
1495/// otherwise indexes an empty vector, which is a panic rather than a
1496/// fallback. Every call site goes through here so the next such
1497/// architecture is one line, not four.
1498impl Pipeline {
1499    pub(crate) fn can_prefill_batched(&self) -> bool {
1500        #[cfg(test)]
1501        let force_serial = self.nll_test_force_serial;
1502        #[cfg(not(test))]
1503        let force_serial = false;
1504        prefill_batched() && !force_serial && !self.weights.layers.is_empty()
1505    }
1506
1507    /// The backend's automatic capacity split for a mapped transformer.
1508    /// Kept as a method so prefill and decode use the exact same boundary.
1509    fn automatic_gpu_prefix(&self) -> Option<usize> {
1510        let (model, _, _, _) = self.weights.embed_tokens.graph_weight()?;
1511        crate::gpu::automatic_layer_prefix(&model, self.num_layers, self.physical_layers)
1512    }
1513
1514    /// Positions per batched pass of the layer-stack prefill for THIS
1515    /// model on THIS backend (see [`prefill_chunk_rule`]). Pub: the network
1516    /// split must chunk exactly like the local path to reproduce it.
1517    pub fn prefill_chunk(&self) -> usize {
1518        let env = env_prefill_chunk();
1519        if env.is_some() || ChunkHost::here() != ChunkHost::Other {
1520            return prefill_chunk_rule(env, ChunkHost::here(), false);
1521        }
1522        prefill_chunk_rule(
1523            None,
1524            ChunkHost::Other,
1525            self.chunk_stack_facts().dense_on_discrete(),
1526        )
1527    }
1528
1529    /// Architectures that run a dedicated per-token stack (gemma-3n's
1530    /// AltUp flow, the gemma-4 E-series PLE/shared-KV body): batched
1531    /// prefill and the graphs stay off for them.
1532    fn per_token_stack(&self) -> bool {
1533        self.g3n.is_some() || self.g4e.is_some()
1534    }
1535
1536    fn chunk_stack_facts(&self) -> ChunkStackFacts {
1537        let plain_dense = !self.weights.layers.is_empty()
1538            && !self.per_token_stack()
1539            && self.dsv4.is_none()
1540            && self.dsv41.is_none()
1541            && self.qwen4_exp.is_none()
1542            && self.weights.layers.iter().all(|lw| {
1543                matches!(lw.attn, AttnKind::Full { .. }) && matches!(lw.ffn, FfnKind::Dense(_))
1544            });
1545        let gpu_on = crate::gpu::enabled();
1546        ChunkStackFacts {
1547            plain_dense,
1548            discrete: gpu_on && crate::gpu::discrete(),
1549            gpu_on,
1550            // Only asked when the rest already qualifies: it opens the
1551            // backend's capacity plan.
1552            capacity_split: std::env::var_os("CMF_GPU_LAYERS").is_some()
1553                || (plain_dense && gpu_on && self.automatic_gpu_prefix().is_some()),
1554            multi_gpu: self.gpu_plan.is_some(),
1555            o1: self.o1_active(),
1556        }
1557    }
1558}
1559
1560/// Prefill chunk (positions per batched pass), model-agnostic form. On
1561/// macOS the AMX GEMM path wants tall panels — M=48 starves the matrix
1562/// units (ggml uses ubatch 512); elsewhere the historical 48 stays.
1563/// CMF_PREFILL_CHUNK overrides. The architectures with their own stacks
1564/// (DeepSeek-V4/V4.1) chunk with this; the layer-stack prefill asks
1565/// [`Pipeline::prefill_chunk`], which also knows the model and the card.
1566/// A different chunk is a different (equally valid) generation: panel
1567/// width reorders float accumulation.
1568pub fn prefill_chunk() -> usize {
1569    prefill_chunk_rule(env_prefill_chunk(), ChunkHost::here(), false)
1570}
1571
1572fn env_prefill_chunk() -> Option<usize> {
1573    std::env::var("CMF_PREFILL_CHUNK")
1574        .ok()
1575        .and_then(|v| v.parse::<usize>().ok())
1576}
1577
1578/// The host classes the chunk width distinguishes.
1579#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1580enum ChunkHost {
1581    Macos,
1582    /// Linux/Android aarch64 (phones, SBCs).
1583    Aarch64,
1584    /// Everything else: x86-64 Linux/Windows, CPU or Vulkan/DX12.
1585    Other,
1586}
1587
1588impl ChunkHost {
1589    fn here() -> Self {
1590        if cfg!(target_os = "macos") {
1591            ChunkHost::Macos
1592        } else if cfg!(target_arch = "aarch64") {
1593            ChunkHost::Aarch64
1594        } else {
1595            ChunkHost::Other
1596        }
1597    }
1598}
1599
1600/// Chunk for a plain dense stack whose every layer lives on a discrete
1601/// card. On x86 the layer-stack prefill is host-driven: each GEMM and the
1602/// chunk attention (which re-uploads the whole KV prefix per layer) is a
1603/// separate submit + readback, so 48 positions a pass left the card idle
1604/// between them. Measured in-process on an RTX 3090 (Vulkan), 2048-token
1605/// prompt — see CHANGELOG 0.7.6 for the table.
1606const DISCRETE_DENSE_PREFILL_CHUNK: usize = 512;
1607
1608/// The chunk-width rule. `dense_on_discrete` is true only for a plain
1609/// dense transformer (full attention, dense FFN, no special stack) that
1610/// is entirely resident on one discrete card — the one case measured
1611/// here. GDN hybrids, MoE, DeepSeek stacks, capacity-split and CPU-only
1612/// runs keep the width they were tuned with.
1613fn prefill_chunk_rule(env: Option<usize>, host: ChunkHost, dense_on_discrete: bool) -> usize {
1614    if let Some(n) = env {
1615        return n.max(1);
1616    }
1617    match host {
1618        ChunkHost::Macos => 512,
1619        // Mobile: big enough to feed the batched attend (gate b ≥ 32)
1620        // and the blocked SDOT GEMM without the memory of 512.
1621        ChunkHost::Aarch64 => 256,
1622        ChunkHost::Other if dense_on_discrete => DISCRETE_DENSE_PREFILL_CHUNK,
1623        ChunkHost::Other => 48,
1624    }
1625}
1626
1627/// What the chunk rule needs to know about a loaded stack.
1628#[derive(Clone, Copy, Debug, Default)]
1629struct ChunkStackFacts {
1630    /// Every layer is `AttnKind::Full` + `FfnKind::Dense`, and no
1631    /// architecture-owned stack (g3n, DeepSeek-V4/V4.1, qwen4-exp) is set.
1632    plain_dense: bool,
1633    /// The active GPU backend is a discrete card.
1634    discrete: bool,
1635    /// The backend is up and not paused.
1636    gpu_on: bool,
1637    /// A capacity-derived device prefix: some layers run on the host.
1638    capacity_split: bool,
1639    /// An in-process multi-GPU plan is set.
1640    multi_gpu: bool,
1641    /// O(1) layers (their Q trace is recorded by the prefill).
1642    o1: bool,
1643}
1644
1645impl ChunkStackFacts {
1646    fn dense_on_discrete(self) -> bool {
1647        self.plain_dense
1648            && self.discrete
1649            && self.gpu_on
1650            && !self.capacity_split
1651            && !self.multi_gpu
1652            && !self.o1
1653    }
1654}
1655
1656/// Number of prompt rows that have a real teacher-forced next-token pair in a
1657/// prefill span.  The final prompt row has no successor token, so it must not
1658/// be handed to the MTP warm-up.  Keeping this arithmetic in one helper makes
1659/// the full-chunk and tail-chunk boundaries explicit for both the graph and
1660/// CPU implementations.
1661#[inline]
1662fn mtp_prefill_pair_count(start: usize, end: usize, input_len: usize) -> usize {
1663    if end <= start || start >= input_len {
1664        return 0;
1665    }
1666    let rows = (end.min(input_len) - start).min(input_len - start);
1667    if end < input_len {
1668        rows
1669    } else {
1670        rows.saturating_sub(1)
1671    }
1672}
1673
1674/// Callback for streaming tokens. Return `false` to cancel.
1675pub type TokenCallback = Box<dyn FnMut(&str) -> bool + Send>;
1676
1677/// One layer's cache ownership at a cross-turn KV reuse boundary.
1678#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1679pub(crate) struct ReuseLayer {
1680    /// Exact-attention layer (rows in `LayerKvCache`); otherwise a
1681    /// recurrent / latent mixer whose state cannot be rewound.
1682    pub full: bool,
1683    /// Positions the host owner cache reaches (`pos_len`: a trimmed
1684    /// sliding tail stores only the newest of them).
1685    pub host_rows: usize,
1686    /// Rows the wgpu token graph's device mirror holds (None: no mirror).
1687    pub device_rows: Option<usize>,
1688    /// A recurrent state lives on the device (advanced past the host copy).
1689    pub device_state: bool,
1690}
1691
1692/// What a reused turn must do before its tail prefill runs on the HOST.
1693#[derive(Debug, Clone, PartialEq, Eq)]
1694pub(crate) enum ReusePlan {
1695    /// Host caches already hold exactly the reused prefix.
1696    Ready,
1697    /// Copy device mirror rows `[from..to)` into the host cache of each
1698    /// listed layer (the rows decode wrote on the device only).
1699    Pull(Vec<(usize, usize, usize)>),
1700    /// The prefix cannot be continued on the host exactly: start fresh.
1701    Fresh,
1702}
1703
1704/// The wgpu whole-token graph decodes into a DEVICE K/V mirror and never
1705/// writes those rows back to the host cache, while the chunked prefill of a
1706/// pure-attention model reads (and appends to) the host cache. A reused turn
1707/// therefore found its host cache ending at the previous PROMPT, not at the
1708/// previous answer: the tail prefill attended without the model's own
1709/// answer and appended its rows at the wrong index (MiniCPM5 on Vulkan
1710/// repeated its tool call instead of reading the tool result). Every layer
1711/// must hold exactly `reuse_from` host rows before the host continues; rows
1712/// that exist only on the device are pulled back, anything else is fresh.
1713pub(crate) fn kv_reuse_plan(reuse_from: usize, layers: &[ReuseLayer]) -> ReusePlan {
1714    let mut pulls = Vec::new();
1715    for (li, l) in layers.iter().enumerate() {
1716        if !l.full {
1717            if l.device_state {
1718                return ReusePlan::Fresh;
1719            }
1720            continue;
1721        }
1722        if l.host_rows == reuse_from {
1723            continue;
1724        }
1725        if l.host_rows < reuse_from && l.device_rows.is_some_and(|d| d >= reuse_from) {
1726            pulls.push((li, l.host_rows, reuse_from));
1727            continue;
1728        }
1729        return ReusePlan::Fresh;
1730    }
1731    if pulls.is_empty() {
1732        ReusePlan::Ready
1733    } else {
1734        ReusePlan::Pull(pulls)
1735    }
1736}
1737
1738impl Pipeline {
1739    /// Reset state whose meaning is local to a single MTP draft sequence.
1740    /// Unlike ordinary KV reuse, MTP is always rebuilt from the prompt.
1741    fn reset_fresh_mtp_draft_state(&mut self) {
1742        self.draft_full_streak = 0;
1743    }
1744
1745    /// Clear all per-sequence state, including backend device mirrors.
1746    ///
1747    /// The host KV/history buffers are only half of the request lifecycle on
1748    /// wgpu: GDN/O(1) state and cached graph bind groups are keyed by the
1749    /// pipeline id and otherwise survive a pooled request.  Keep every fresh
1750    /// sequence entry point on this one reset path so a new request cannot
1751    /// inherit the prior request's device state.
1752    fn clear_sequence_state(&mut self) {
1753        // a replay still writing the GDN owners must land before they are
1754        // cleared or reallocated (the device holds raw pointers to them)
1755        #[cfg(target_os = "macos")]
1756        let _ = crate::gpu_metal::wait_replay();
1757        self.kv_cache.clear();
1758        // Both reuse keys (the legacy `kv_history` and the bounded
1759        // `kv_prefix`) describe the state being dropped here.
1760        self.clear_history();
1761        self.graph_logits = None;
1762        if let Some(b) = &mut self.dsv41 {
1763            b.3.clear();
1764        }
1765        crate::gpu::graph_kv_reset(self.graph_kv_id);
1766        // MTP is detached from `self` for the duration of generation, so its
1767        // device mirror is not covered by the trunk reset above.  Reset the
1768        // derived id as well: a failed/aborted warm-up must never leave a
1769        // mirror that a later request can mistake for a current MTP cache.
1770        crate::gpu::graph_kv_reset(self.mtp_kv_id());
1771    }
1772
1773    /// Make the host caches own exactly the reused prefix `[0..reuse_from)`
1774    /// before a reused turn's tail prefill runs on the host (see
1775    /// [`kv_reuse_plan`]). Returns false when the prefix cannot be continued
1776    /// exactly — the caller then starts a fresh sequence. A model whose
1777    /// prefill runs through the token graph keeps its device state as the
1778    /// authority and is left untouched.
1779    fn prepare_kv_reuse(&mut self, reuse_from: usize) -> bool {
1780        if self.graph_prefill_preferred() {
1781            return true;
1782        }
1783        let kv_id = self.graph_kv_id;
1784        let layers: Vec<ReuseLayer> = (0..self.num_layers)
1785            .map(|li| {
1786                let full = matches!(
1787                    self.weights.layers[self.phys_layer(li)].attn,
1788                    AttnKind::Full { .. }
1789                );
1790                ReuseLayer {
1791                    full,
1792                    // Absolute depth: a trimmed sliding tail stores fewer
1793                    // rows than the positions it has seen.
1794                    host_rows: self.kv_cache.layers[li].pos_len(),
1795                    device_rows: crate::gpu::graph_kv_stored(kv_id, li),
1796                    device_state: crate::gpu::graph_state_resident(kv_id, li),
1797                }
1798            })
1799            .collect();
1800        // No wgpu device state at all (CPU, Metal — whose graph appends every
1801        // decoded row to the owner cache itself): the host is the owner and
1802        // the extension check already proved the prefix.
1803        if layers
1804            .iter()
1805            .all(|l| l.device_rows.is_none() && !l.device_state)
1806        {
1807            return true;
1808        }
1809        let plan = kv_reuse_plan(reuse_from, &layers);
1810        let (what, rows, n) = match &plan {
1811            ReusePlan::Ready => ("host ready", 0, 0),
1812            ReusePlan::Fresh => ("fresh", 0, 0),
1813            ReusePlan::Pull(p) => (
1814                "pull",
1815                p.iter().map(|&(_, a, b)| b - a).max().unwrap_or(0),
1816                p.len(),
1817            ),
1818        };
1819        let t0 = std::time::Instant::now();
1820        let ok = self.apply_kv_reuse_plan(reuse_from, plan, &layers);
1821        if std::env::var("CMF_PREFILL_PROF").is_ok() {
1822            eprintln!(
1823                "kv-reuse: {what}{}: {rows} device row(s) × {n} layer(s) to the host in {:.2} ms",
1824                if ok { "" } else { " (failed → fresh)" },
1825                t0.elapsed().as_secs_f64() * 1e3
1826            );
1827        }
1828        ok
1829    }
1830
1831    fn apply_kv_reuse_plan(
1832        &mut self,
1833        reuse_from: usize,
1834        plan: ReusePlan,
1835        layers: &[ReuseLayer],
1836    ) -> bool {
1837        let kv_id = self.graph_kv_id;
1838        match plan {
1839            ReusePlan::Fresh => return false,
1840            ReusePlan::Ready => {}
1841            ReusePlan::Pull(pulls) => {
1842                // Mirrors of one uniform geometry: one batched read serves
1843                // every layer. Per-layer geometry (MiMo-V2: 4/8 KV heads,
1844                // narrow V, sliding rings) is read layer by layer in the
1845                // host layout instead.
1846                let (nkv, hd) = {
1847                    let c = &self.kv_cache.layers[pulls[0].0];
1848                    (c.num_kv_heads, c.head_dim)
1849                };
1850                let uniform = pulls.iter().all(|&(li, _, _)| {
1851                    let c = &self.kv_cache.layers[li];
1852                    (c.num_kv_heads, c.head_dim) == (nkv, hd)
1853                });
1854                let batched = if uniform {
1855                    crate::gpu::graph_kv_read_rows(kv_id, &pulls, nkv, hd)
1856                } else {
1857                    None
1858                };
1859                let rows: Vec<(Vec<f32>, Vec<f32>)> = match batched {
1860                    Some(rows) => rows,
1861                    None => {
1862                        let mut rows = Vec::with_capacity(pulls.len());
1863                        for &(li, from, to) in &pulls {
1864                            let (lnkv, lhd) = {
1865                                let c = &self.kv_cache.layers[li];
1866                                (c.num_kv_heads, c.head_dim)
1867                            };
1868                            let Some((k, v, first_valid)) =
1869                                crate::gpu::graph_kv_pull_host(kv_id, li, from, to, lnkv, lhd)
1870                            else {
1871                                return false;
1872                            };
1873                            // The host continues at `to`: a sliding layer
1874                            // reads back only its last window, a full one
1875                            // every row it lacks.
1876                            let need_from = match self.layer_window(li) {
1877                                Some(w) => from.max((to + 1).saturating_sub(w)),
1878                                None => from,
1879                            };
1880                            if first_valid > need_from {
1881                                return false;
1882                            }
1883                            rows.push((k, v));
1884                        }
1885                        rows
1886                    }
1887                };
1888                for ((li, from, to), (k, v)) in pulls.into_iter().zip(rows) {
1889                    let cache = &mut self.kv_cache.layers[li];
1890                    let row = cache.num_kv_heads * cache.head_dim;
1891                    for p in 0..to - from {
1892                        cache.append(&k[p * row..(p + 1) * row], &v[p * row..(p + 1) * row], &[]);
1893                    }
1894                    if cache.pos_len() != to {
1895                        return false;
1896                    }
1897                }
1898            }
1899        }
1900        // A mirror past the prefix (a greedy burst that ran beyond the stop)
1901        // holds rows of the OLD continuation: rewind it so the next graph
1902        // token re-syncs those positions from the host.
1903        for (li, l) in layers.iter().enumerate() {
1904            if l.full
1905                && l.device_rows.is_some_and(|d| d > reuse_from)
1906                && !crate::gpu::graph_kv_set_stored(kv_id, li, reuse_from)
1907            {
1908                return false;
1909            }
1910        }
1911        true
1912    }
1913
1914    /// Finish a generation lifecycle after the MTP/router owners were
1915    /// detached.  Every terminal path must put those owners back before the
1916    /// pooled pipeline can serve another request.  Graph side channels and
1917    /// device mirrors are cleared on errors and cancellations; a successful
1918    /// generation keeps its decode-ready host cache for KV reuse.
1919    fn finish_generation(
1920        &mut self,
1921        mtp: &mut Option<MtpModule>,
1922        router: &mut Option<crate::swarm::DynRouter>,
1923        clear_sequence: bool,
1924    ) {
1925        // A dynamic route may have switched the overlay before the terminal
1926        // path. Restore the backbone while the detached router is still
1927        // available, because set_active_skill also owns the overlay reset.
1928        if router.is_some() {
1929            let _ = self.set_active_skill(None);
1930        }
1931        // The last speculative round's replay may still be in flight on
1932        // the second queue: whoever reads the host cache after generate()
1933        // returns (session export, the network split's KV wire, a KV
1934        // reuse) must see the final states.
1935        // A replay that failed leaves the GDN owners half-written: fail
1936        // closed and drop the sequence instead of handing the cache on.
1937        #[cfg(target_os = "macos")]
1938        let clear_sequence = clear_sequence || !crate::gpu_metal::wait_replay();
1939        if clear_sequence {
1940            self.clear_sequence_state();
1941            if let Some(m) = mtp.as_mut() {
1942                // The MTP owner is detached while generation runs, so the
1943                // trunk reset above cannot clear its host cache.  Drop its
1944                // partial rows before reattaching it to the pooled pipeline;
1945                // the next request must start from the same empty anchor on
1946                // CPU and on the device mirror.
1947                m.kv.clear();
1948            }
1949            if let Some(m) = self.mtp.as_mut() {
1950                // A non-speculative request leaves the configured MTP owner
1951                // attached.  Clear that dormant cache too when a shared
1952                // generation failure/cancellation resets the sequence.
1953                m.kv.clear();
1954            }
1955        }
1956        self.graph_want_logits = false;
1957        self.graph_head_required = false;
1958        self.graph_logits = None;
1959        self.graph_failed
1960            .store(false, std::sync::atomic::Ordering::Relaxed);
1961        self.cancel
1962            .store(false, std::sync::atomic::Ordering::Relaxed);
1963        self.dyn_router = router.take().or(self.dyn_router.take());
1964        self.mtp = mtp.take().or(self.mtp.take());
1965        self.mtp_graph_mode = None;
1966        self.spec_forced = None;
1967    }
1968
1969    /// Consume a graph failure reported by a forward that returns only a
1970    /// hidden vector.  `forward_ids` is a public Result API, so it must not
1971    /// turn the graph's zero hidden sentinel into a valid lm_head result.
1972    fn check_forward_graph(&mut self, phase: &str, pos: usize) -> Result<(), String> {
1973        if self
1974            .graph_failed
1975            .swap(false, std::sync::atomic::Ordering::Relaxed)
1976        {
1977            self.cancel
1978                .store(false, std::sync::atomic::Ordering::Relaxed);
1979            self.clear_sequence_state();
1980            self.graph_logits = None;
1981            self.graph_want_logits = false;
1982            self.graph_head_required = false;
1983            return Err(format!("GPU graph failed during {phase} at position {pos}"));
1984        }
1985        Ok(())
1986    }
1987
1988    #[cfg(target_os = "macos")]
1989    fn fail_metal_graph(&mut self, reason: &str) {
1990        crate::pipeline::METAL_GRAPH_ERRORS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1991        self.clear_sequence_state();
1992        self.graph_logits = None;
1993        self.graph_failed
1994            .store(true, std::sync::atomic::Ordering::Relaxed);
1995        self.cancel
1996            .store(true, std::sync::atomic::Ordering::Relaxed);
1997        tracing::error!("native Metal TokenGraph failed closed: {reason}");
1998    }
1999
2000    /// Start an NLL/PPL request with all graph side channels in a known
2001    /// state.  A graph failure also raises the cooperative cancel bit; it is
2002    /// consumed here and that graph-induced bit is cleared so an independent
2003    /// request can be reused.  A caller-owned cancellation remains intact.
2004    fn nll_begin(&mut self) -> Result<(), String> {
2005        if self
2006            .graph_failed
2007            .swap(false, std::sync::atomic::Ordering::Relaxed)
2008        {
2009            self.cancel
2010                .store(false, std::sync::atomic::Ordering::Relaxed);
2011            self.clear_sequence_state();
2012            self.graph_logits = None;
2013            self.graph_want_logits = false;
2014            self.graph_head_required = false;
2015            return Err("GPU graph failed before NLL scoring".to_string());
2016        }
2017        self.clear_sequence_state();
2018        self.graph_logits = None;
2019        self.graph_want_logits = false;
2020        self.graph_head_required = false;
2021        Ok(())
2022    }
2023
2024    /// End an NLL/PPL request, including the side channels that are not part
2025    /// of the host KV cache.  This is intentionally explicit instead of
2026    /// relying on a tuple/sentinel return: callers must see every failure.
2027    fn nll_end(&mut self) {
2028        self.clear_sequence_state();
2029        self.graph_logits = None;
2030        self.graph_want_logits = false;
2031        self.graph_head_required = false;
2032        self.graph_failed
2033            .store(false, std::sync::atomic::Ordering::Relaxed);
2034    }
2035
2036    /// Check the graph failure channel at a scoring boundary and leave the
2037    /// pipeline reusable when the device path failed.
2038    fn nll_check_graph(&mut self, phase: &str, pos: usize) -> Result<(), String> {
2039        #[cfg(test)]
2040        if self.nll_test_fail_at == Some(pos) {
2041            self.nll_test_fail_at = None;
2042            self.graph_failed
2043                .store(true, std::sync::atomic::Ordering::Relaxed);
2044            self.cancel
2045                .store(true, std::sync::atomic::Ordering::Relaxed);
2046        }
2047        if self
2048            .graph_failed
2049            .swap(false, std::sync::atomic::Ordering::Relaxed)
2050        {
2051            self.cancel
2052                .store(false, std::sync::atomic::Ordering::Relaxed);
2053            self.clear_sequence_state();
2054            self.graph_logits = None;
2055            self.graph_want_logits = false;
2056            return Err(format!(
2057                "GPU graph failed during NLL {phase} at position {pos}"
2058            ));
2059        }
2060        Ok(())
2061    }
2062
2063    /// Map a virtual layer index to its physical weight index.
2064    /// Looped Transformer (Nanbeige 4.2): 22 physical layers × 2 loops = 44 virtual;
2065    /// virtual layer 23 maps back to physical layer 1 (23 % 22 = 1).
2066    #[inline]
2067    pub fn phys_layer(&self, virtual_idx: usize) -> usize {
2068        virtual_idx % self.physical_layers
2069    }
2070
2071    /// True when `virtual_idx` is the last layer of a loop iteration
2072    /// (used for loop_final_norm insertion).
2073    #[inline]
2074    pub fn is_loop_end(&self, virtual_idx: usize) -> bool {
2075        self.loop_final_norm && (virtual_idx + 1) % self.physical_layers == 0
2076    }
2077
2078    /// Build a pipeline from parts (used by the loader and tests).
2079    #[allow(clippy::too_many_arguments)]
2080
2081    /// Whole-block q1 token graph on the GPU (macOS/Metal): the run of
2082    /// consecutive q1 layers — GDN *and* full attention — starting at
2083    /// `start` executes as few command buffers as the CPU truly needs.
2084    /// Hidden stays device-resident across every layer; the only syncs
2085    /// are before each CPU attend (it needs q/k/v and owns the KV
2086    /// cache) and the final hidden readback. Recurrent states
2087    /// round-trip through shared memory (the CPU stays their owner, so
2088    /// every other path remains coherent). Returns the first layer
2089    /// index NOT covered (== `start` → refused, caller falls through
2090    /// to the per-layer CPU path).
2091    /// Should prefill run position-by-position through the GPU token
2092    /// graph instead of the batched CPU chunk-GEMM? True for q1 GDN
2093    /// hybrids on native Metal: their chunk prefill is walled by the
2094    /// sequential scalar recurrence, so the graph's decode rate wins.
2095    /// NOT for Looped Transformers, despite the per-chunk loop_final_norm
2096    /// sync: the chunk-GEMM amortizes each weight over the whole chunk,
2097    /// which the per-position graph cannot (Nanbeige 4.2 on M4, 512-token
2098    /// prompt: 85 tok/s chunked vs 14 through the graph).
2099    #[cfg(target_os = "macos")]
2100    fn graph_prefill_preferred(&self) -> bool {
2101        let graph_force = crate::gpu::q1_force() || crate::gpu::q2tp_gpu_opt_in();
2102        if !crate::gpu::enabled_here()
2103            || !graph_force
2104            || std::env::var("CMF_GPU_BLOCK")
2105                .map(|v| v == "0")
2106                .unwrap_or(false)
2107            // CMF_PREFILL_GRAPH=0: the chunked prefill (GEMM projections,
2108            // CPU recurrence) instead of the per-position token graph.
2109            || std::env::var("CMF_PREFILL_GRAPH").as_deref() == Ok("0")
2110        {
2111            return false;
2112        }
2113        self.weights
2114            .layers
2115            .iter()
2116            .any(|lw| {
2117                matches!(&lw.attn, AttnKind::LinearGdn(w) if w.in_proj_qkv.metal_graph_parts().is_some())
2118            })
2119    }
2120
2121    /// Prompt ingest through the batched wgpu graph in device-prefix mode:
2122    /// a MoE stack that does not fit the card runs each chunk's leading
2123    /// layers on the device (experts resident) and the rest on the host's
2124    /// batched walk. On by default for models with per-layer attention
2125    /// geometry (MiMo-V2 — its measured default); `CMF_BATCH_PREFIX=1`
2126    /// opts any other MoE model in, `=0` keeps the chunked host prefill.
2127    #[cfg(not(target_os = "macos"))]
2128    fn batch_prefix_prefill(&self) -> bool {
2129        let forced = match std::env::var("CMF_BATCH_PREFIX").as_deref() {
2130            Ok("0") => return false,
2131            Ok("1") => true,
2132            _ => false,
2133        };
2134        crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill)
2135            && crate::gpu::enabled_here()
2136            && !self.graph_refused()
2137            && (forced || self.graph_attn_decline_reason().is_some())
2138            && self.wgpu_graph_attn_decline().is_none()
2139            && self.attn_softcap == 0.0
2140            && self
2141                .weights
2142                .layers
2143                .iter()
2144                .any(|lw| matches!(&lw.ffn, FfnKind::Moe(_)))
2145            && self.automatic_gpu_prefix().is_some()
2146    }
2147
2148    /// Gemma-4's prompt through the batched wgpu graph (`gemma_batch`): a
2149    /// dense stack wholly on a discrete card, whose token graph already
2150    /// decodes it. Not `graph_prefill_preferred` — the host stays the KV
2151    /// owner for reuse and scoring; only generation's ingest changes.
2152    /// `CMF_G4_BATCH=0` keeps the chunked host prefill.
2153    fn g4_batch_prefill(&self) -> bool {
2154        #[cfg(target_os = "macos")]
2155        {
2156            false
2157        }
2158        #[cfg(not(target_os = "macos"))]
2159        {
2160            // An EMPTY layer stack (`all()` is vacuously true) plus a
2161            // gemma4 geometry flag is exactly the gemma-4 E-series
2162            // per-token stack — it has no batch graph rows to ingest.
2163            !self.weights.layers.is_empty()
2164                && !self.per_token_stack()
2165                && (self.global_attn.is_some() || self.attn_v_norm)
2166                && crate::gpu::g4_batch_on()
2167                && crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill)
2168                && crate::gpu::enabled_here()
2169                && crate::gpu::discrete()
2170                && !self.graph_refused()
2171                && !self.o1_active()
2172                && self.attn_softcap == 0.0
2173                && self.wgpu_graph_attn_decline().is_none()
2174                && self.weights.layers.iter().all(|lw| {
2175                    matches!(lw.attn, AttnKind::Full { .. }) && matches!(lw.ffn, FfnKind::Dense(_))
2176                })
2177                && self.automatic_gpu_prefix().is_none()
2178        }
2179    }
2180
2181    /// Gemma-4's batched prefill of `ids` at positions `pos0..`
2182    /// (`g4_batch_prefill`): chunks of `generation_batch_k` rows through the
2183    /// batch graph. Ok((n, rows)): `n` positions ingested; `rows` = the last
2184    /// one's hidden, or every row's (`keep_all`). A refusal stops early
2185    /// (n < ids.len()) after pulling the device rows the host walk needs.
2186    /// Err: a failure after admission — the device state is undefined.
2187    #[cfg(not(target_os = "macos"))]
2188    fn g4_batch_ingest(
2189        &mut self,
2190        ids: &[u32],
2191        pos0: usize,
2192        keep_all: bool,
2193    ) -> Result<(usize, Vec<f32>), String> {
2194        let hs = self.hidden_size;
2195        let chunk = self.generation_batch_k().max(1);
2196        let end_all = pos0 + ids.len();
2197        let mut pos = pos0;
2198        let mut out = Vec::new();
2199        while pos < end_all && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
2200            let end = (pos + chunk).min(end_all);
2201            let bk = end - pos;
2202            let mut hiddens = vec![0f32; bk * hs];
2203            for (j, &id) in ids[pos - pos0..end - pos0].iter().enumerate() {
2204                hiddens[j * hs..(j + 1) * hs].copy_from_slice(&self.embed_single(id));
2205            }
2206            let positions: Vec<usize> = (pos..end).collect();
2207            let t_chunk = std::time::Instant::now();
2208            let outcome = self.try_batch_graph_wgpu(&mut hiddens, &positions, bk, None);
2209            if std::env::var("CMF_GRAPH_PROF").is_ok() {
2210                let ms = t_chunk.elapsed().as_secs_f64() * 1000.0;
2211                eprintln!(
2212                    "batch-chunk: phase=prompt mode=gemma4 k={bk} outcome={outcome:?} {ms:.1} ms ({:.1} tok/s)",
2213                    bk as f64 / (ms / 1000.0)
2214                );
2215            }
2216            match outcome {
2217                crate::gpu::BatchGraphOutcome::Completed => {
2218                    if keep_all {
2219                        out.extend_from_slice(&hiddens);
2220                    } else {
2221                        out = hiddens[(bk - 1) * hs..].to_vec();
2222                    }
2223                    pos = end;
2224                }
2225                crate::gpu::BatchGraphOutcome::Failed => {
2226                    return Err("Gemma-4 batched prefill failed after admission".into());
2227                }
2228                crate::gpu::BatchGraphOutcome::Declined => {
2229                    #[cfg(feature = "gpu")]
2230                    if pos > pos0 {
2231                        self.pull_lagging_host_kv(0, self.num_layers, pos);
2232                    }
2233                    break;
2234                }
2235            }
2236        }
2237        Ok((pos - pos0, out))
2238    }
2239
2240    /// Hidden rows of every position of `ids` through Gemma-4's batched
2241    /// prefill from a fresh sequence (the prefill's teacher-forced logits
2242    /// for parity gates: `logits_from_hidden` per row). None when the model
2243    /// or the card does not take that route, or it refused part way.
2244    pub fn g4_prefill_hidden_rows(&mut self, ids: &[u32]) -> Option<Vec<f32>> {
2245        #[cfg(not(target_os = "macos"))]
2246        {
2247            if ids.is_empty() || !self.g4_batch_prefill() {
2248                return None;
2249            }
2250            self.clear_sequence_state();
2251            match self.g4_batch_ingest(ids, 0, true) {
2252                Ok((n, rows)) if n == ids.len() => Some(rows),
2253                _ => {
2254                    self.clear_sequence_state();
2255                    None
2256                }
2257            }
2258        }
2259        #[cfg(target_os = "macos")]
2260        {
2261            let _ = ids;
2262            None
2263        }
2264    }
2265
2266    #[cfg(not(target_os = "macos"))]
2267    fn graph_prefill_preferred(&self) -> bool {
2268        // Discrete-GPU wgpu whole-token graph: GDN layers carry recurrent state
2269        // (conv ring + delta-rule S) resident on the GPU. A batched CPU prefill
2270        // builds that state on the CPU only, leaving the GPU buffers zeroed at
2271        // decode → garbage. Route GDN-hybrid prefill through the graph one
2272        // position at a time so the resident state is seeded exactly as decode
2273        // will read it. Pure-attention models keep the batched CPU prefill (its
2274        // KV mirror re-syncs from the CPU cache, so no seeding gap).
2275        let graph_on = crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill);
2276        if !graph_on || !crate::gpu::enabled_here() {
2277            return false;
2278        }
2279        // Embryo's phase state is device-owned by the resident graph during
2280        // prefill; the batched CPU path would leave decode seeing a zeroed
2281        // device recurrence.  Route the prompt position-by-position too.
2282        if self.embryo_resident_eligible() {
2283            // The resident graph owns the phase state and anchor KV on the
2284            // device.  A prefill-only graph would leave decode on the host
2285            // with no way to import that state, so keep the whole sequence
2286            // on one owner (or use the ordinary CPU prefill/decode pair).
2287            return crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode);
2288        }
2289        // The descriptor-aware Prism graph now carries both the FWHT/affine
2290        // transforms and resident GDN state, so it is also the exact prefill
2291        // path for this model.  Keeping it here (rather than falling through
2292        // to the CPU chunk walk) is required for a long prompt to seed the
2293        // same device state that decode consumes.
2294        // O(1) needs the CPU prefill: the q-trace that seals the Nyström
2295        // skeleton is recorded there and nowhere else. The GDN half of
2296        // the hybrid loses nothing — the graph's first decode creates
2297        // its (ring, S) entries seeded from `cpu_state`, the same
2298        // handoff every graph run relies on when the entry is fresh.
2299        // Without this line the two designs collide on hybrids and o1
2300        // never becomes graph-portable: prefill through the graph
2301        // records no trace, so views stay None forever.
2302        if self.o1_active() {
2303            return false;
2304        }
2305        // A model the wgpu graphs decline outright would walk its prompt
2306        // one position at a time through a graph that never runs: the
2307        // batched CPU chunk prefill is the right ingest for it.
2308        if self.wgpu_graph_attn_decline().is_some() {
2309            return false;
2310        }
2311        if self
2312            .weights
2313            .layers
2314            .iter()
2315            .any(|lw| matches!(&lw.attn, AttnKind::LinearGdn(_)))
2316        {
2317            return true;
2318        }
2319        // MoE models too: the chunked CPU prefill runs every expert on the
2320        // host (Hy-MT2-30B-A3B on a Xeon: 8 tok/s of ingest against 53 of
2321        // graph decode), while the token graph — and the batched graph under
2322        // CMF_BATCH_K — keep the experts resident. Full attention in the
2323        // graph writes the KV mirror that decode reads, exactly as it does
2324        // for the hybrids' attention layers. Only when the whole stack is
2325        // resident: with a device prefix the per-position walk finishes
2326        // every token on the host, and the chunked prefill (GEMMs on the
2327        // card, the expert loop batched on the host) is the faster ingest
2328        // (the 8 GB ladder point: 7 tok/s chunked against ~1 walked).
2329        self.weights
2330            .layers
2331            .iter()
2332            .any(|lw| matches!(&lw.ffn, FfnKind::Moe(_)))
2333            && self.automatic_gpu_prefix().is_none()
2334    }
2335
2336    #[cfg(target_os = "macos")]
2337    fn q1_graph_gpu(
2338        &mut self,
2339        start: usize,
2340        upto: Option<usize>,
2341        position: usize,
2342        h: &mut [f32],
2343    ) -> usize {
2344        let _mt0 = std::time::Instant::now(); // CMF_METAL_HOSTPROF
2345        use crate::gpu::{AttnGpuLayer, GdnGpuCfg, GdnGpuLayer, GraphDims, MetalFfn, TokenGraph};
2346        const MOE_WORKING_SET_REASON: &str =
2347            "model weights exceed Metal working set for native MoE graph";
2348        // A prior admission check rejected this pipeline. Avoid rebuilding the
2349        // entire 40-layer graph plan at every individual host layer after the
2350        // fallback begins; the per-operation path below stays eligible.
2351        if self
2352            .graph_declines
2353            .borrow()
2354            .iter()
2355            .any(|&(site, reason)| site == "metal block graph" && reason == MOE_WORKING_SET_REASON)
2356        {
2357            return start;
2358        }
2359        let graph_force = crate::gpu::q1_force() || crate::gpu::q2tp_gpu_opt_in();
2360        if self.attn_softcap > 0.0 // capped scores: no graph kernel — CPU path
2361            || !self.metal_rope_scale_ok()
2362            || !crate::gpu::enabled_here()
2363            || !graph_force
2364            || std::env::var("CMF_GPU_BLOCK")
2365                .map(|v| v == "0")
2366                .unwrap_or(false)
2367        {
2368            if std::env::var("CMF_GRAPH_DBG").is_ok() {
2369                eprintln!(
2370                    "block-graph: front gate (softcap={} enabled_here={} graph_force={})",
2371                    self.attn_softcap > 0.0,
2372                    crate::gpu::enabled_here(),
2373                    graph_force,
2374                );
2375            }
2376            if self.graph_head_required {
2377                self.fail_metal_graph("native graph front gate refused");
2378            }
2379            return start;
2380        }
2381        // The graph encodes SiLU or exact-GELU FFNs and attention with an
2382        // explicit model scale. Sliding-window layers with their own RoPE
2383        // table / rotary width ride it too (`metal_graph_swa`): both the
2384        // device attend and the sandwich's CPU attend read each layer's
2385        // window and table. Sandwich norms and other activations fall back
2386        // to the CPU path.
2387        // Gemma-4 (`metal_gemma4`): its sliding layers ride the same door,
2388        // its global layers, V norm, sandwich norms, layer scalar and
2389        // tanh-GELU the `gpu_metal::gemma4` kernels.
2390        let gemma4 = self.metal_gemma4();
2391        let swa_graph = self.metal_graph_swa() || gemma4;
2392        // The normal native GQA kernel carries a learned, value-less
2393        // softmax column.  It also takes the exact per-layer sliding
2394        // window, but its split-K twin has no sink column.  Thus a
2395        // sink-bearing model may use this door only when the ordinary
2396        // sliding geometry has passed `metal_graph_swa` (or when it has no
2397        // sliding layers at all).
2398        let graph_attn_reason = self.graph_attn_decline_reason();
2399        let native_sink_attend = graph_attn_reason == Some("learned attention sinks")
2400            && (swa_graph || (self.swa.is_none() && self.sliding_layers.is_none()));
2401        if (self.swa.is_some() && !swa_graph)
2402            || (self.global_attn.is_some() && !gemma4)
2403            || self.attention_heads_per_layer.is_some()
2404            || (self.attn_v_norm && !gemma4)
2405            // Per-layer KV heads / narrow V stay outside this graph.  A
2406            // learned sink is admitted only through native_sink_attend.
2407            || (graph_attn_reason.is_some() && !swa_graph && !native_sink_attend)
2408            || self.weights.layers.iter().any(|lw| {
2409                ((lw.attn_out_norm.is_some()
2410                    || lw.ffn_out_norm.is_some()
2411                    || lw.layer_scale.is_some())
2412                    && !gemma4)
2413                    || matches!(&lw.ffn, FfnKind::Dense(d)
2414                        if !(matches!(d.act, Act::Silu | Act::Gelu)
2415                            || (gemma4 && d.act == Act::GeluTanh)))
2416            })
2417        {
2418            // The Metal graphs' device attend has no per-layer attention
2419            // geometry (the wgpu graphs do): say so once, by name.
2420            if let Some(reason) = graph_attn_reason.filter(|_| !swa_graph && !native_sink_attend) {
2421                self.note_graph_decline("metal block graph", reason);
2422            }
2423            if std::env::var("CMF_GRAPH_DBG").is_ok() {
2424                eprintln!(
2425                    "block-graph: arch ineligible (swa={} gattn={} hpl={} vnorm={} scale_delta={:.2e})",
2426                    self.swa.is_some(),
2427                    self.global_attn.is_some(),
2428                    self.attention_heads_per_layer.is_some(),
2429                    self.attn_v_norm,
2430                    (self.attn_scale - 1.0 / (self.head_dim as f32).sqrt()).abs(),
2431                );
2432            }
2433            if self.graph_head_required {
2434                self.fail_metal_graph("native graph architecture gate refused");
2435            }
2436            return start;
2437        }
2438        // Looped Transformer: the graph covers ALL loop iterations;
2439        // encode_loop_norm is inserted on-device at each boundary.
2440        let limit = upto
2441            .map(|u| u + 1)
2442            .unwrap_or(self.num_layers)
2443            .min(self.num_layers);
2444
2445        enum Item<'a> {
2446            Gdn {
2447                run: Vec<GdnGpuLayer<'a>>,
2448                first: usize,
2449            },
2450            Attn {
2451                l: AttnGpuLayer<'a>,
2452                li: usize,
2453                q_norm: Option<&'a [f32]>,
2454                k_norm: Option<&'a [f32]>,
2455                output_gate: bool,
2456                /// `self_attn.g_proj` output gate (per-head flag): the
2457                /// sandwich projects the device-normed input on the host
2458                /// and gates the attend's output before O.
2459                proj_gate: Option<(&'a QTensor, bool)>,
2460                /// The same gate's f32 rows `[nh × hidden]` when the device
2461                /// attend can apply it (per-head sigmoid, f32 in RAM).
2462                head_gate_w: Option<&'a [f32]>,
2463                bias: Option<(&'a [f32], &'a [f32], &'a [f32])>,
2464                /// Attend on the device too (no sync): F32 KV, no
2465                /// o1/bias, dims inside the kernels' contract.
2466                full_gpu: bool,
2467            },
2468        }
2469
2470        // Device-attend KERNEL contract, shared by every Full layer. The
2471        // hd>128 default-off POLICY is applied after the scan: it was
2472        // measured on dense models, and a MoE plan inverts it — with the
2473        // experts on device each CPU-attend sandwich costs a
2474        // commit+wait, ~30 submits/token (W2 on M4: 14.7 tok/s
2475        // sandwiched vs 27.1 device-attend vs 18.8 pure CPU).
2476        let attend_mode = std::env::var("CMF_GPU_ATTEND").unwrap_or_else(|_| "auto".into());
2477        let attend_contract = attend_mode != "0"
2478            && attend_mode != "off"
2479            && self.head_dim % 4 == 0
2480            && self.head_dim <= 256
2481            // Flint has explicit no-RoPE layers.  The device RoPE kernel
2482            // accepts rd=0 (zero rotation lanes) and binds a sentinel table;
2483            // retain the historical lane-pair requirement for nonzero rd.
2484            && (self.rotary_dim == 0
2485                || (self.rotary_dim >= 2
2486                    && self.rotary_dim <= self.head_dim
2487                    && (self.rotary_dim / 2) % 32 == 0))
2488            && self.num_kv_heads > 0
2489            && self.num_heads % self.num_kv_heads == 0;
2490
2491        let mut plan: Vec<Item> = Vec::new();
2492        let mut model_ref: Option<std::sync::Arc<cortiq_core::CmfModel>> = None;
2493        // Break-reason diagnostics ride the same env as the plan summary.
2494        let block_diag = std::env::var("CMF_GRAPH_DBG").is_ok();
2495        let mut scan = start;
2496        while scan < limit {
2497            let lw = &self.weights.layers[self.phys_layer(scan)];
2498            let ffn = match &lw.ffn {
2499                FfnKind::Dense(d) if d.segs.is_empty() => {
2500                    let (Some(g), Some(u), Some(dn)) = (
2501                        d.gate_proj.metal_graph_parts(),
2502                        d.up_proj.metal_graph_parts(),
2503                        d.down_proj.metal_graph_parts(),
2504                    ) else {
2505                        if block_diag {
2506                            eprintln!(
2507                                "block-graph: L{scan} FFN trio not graph-mappable — run ends"
2508                            );
2509                        }
2510                        break;
2511                    };
2512                    MetalFfn::Dense {
2513                        gate: g,
2514                        up: u,
2515                        down: dn,
2516                        // The front gate admitted SiLU, exact GELU and
2517                        // (Gemma-4) tanh-GELU only.
2518                        act: d.act.graph_act().map_or(0, |a| a.code() as u8),
2519                    }
2520                }
2521                FfnKind::Moe(m) => {
2522                    // Flint's detached Q4 shared expert is intentionally
2523                    // tried before the generic uniform-bank graph builder.
2524                    // The latter correctly refuses its ragged Q2/Q4 widths;
2525                    // this narrow descriptor keeps the router and selected
2526                    // routed experts device-resident inside a bounded page.
2527                    let ffn = if let Some(flint) = metal_flint_moe_graph_parts(m, self.hidden_size, scan)
2528                    {
2529                        MetalFfn::FlintMoe(flint)
2530                    } else if let Some(moe) = metal_moe_graph_parts(m, self.hidden_size) {
2531                        MetalFfn::Moe(moe)
2532                    } else {
2533                        if block_diag {
2534                            eprintln!(
2535                                "block-graph: L{scan} MoE outside the graph contract — run ends"
2536                            );
2537                        }
2538                        break;
2539                    };
2540                    if let QTensor::Mapped { model, .. } = &m.experts[0].gate_proj {
2541                        model_ref.get_or_insert_with(|| model.clone());
2542                    }
2543                    ffn
2544                }
2545                _ => {
2546                    if block_diag {
2547                        eprintln!("block-graph: L{scan} non-graph FFN — run ends");
2548                    }
2549                    break;
2550                }
2551            };
2552            match &lw.attn {
2553                AttnKind::LinearGdn(w) if self.gdn_cfg.is_some() => {
2554                    let parts = (
2555                        w.in_proj_qkv.metal_graph_parts(),
2556                        w.in_proj_z.metal_graph_parts(),
2557                        w.in_proj_a.f32_parts(),
2558                        w.in_proj_b.f32_parts(),
2559                        w.out_proj.metal_graph_parts(),
2560                    );
2561                    let (Some(qkv), Some(z), Some(a), Some(b), Some(out)) = parts else {
2562                        if block_diag {
2563                            eprintln!(
2564                                "block-graph: L{scan} GDN parts refused (qkv={} z={} a_f32={} b_f32={} out={})",
2565                                w.in_proj_qkv.metal_graph_parts().is_some(),
2566                                w.in_proj_z.metal_graph_parts().is_some(),
2567                                w.in_proj_a.f32_parts().is_some(),
2568                                w.in_proj_b.f32_parts().is_some(),
2569                                w.out_proj.metal_graph_parts().is_some(),
2570                            );
2571                        }
2572                        break;
2573                    };
2574                    if let QTensor::Mapped { model, .. } = &w.in_proj_qkv {
2575                        model_ref.get_or_insert_with(|| model.clone());
2576                    }
2577                    let gl = GdnGpuLayer {
2578                        attn_norm: &lw.input_norm,
2579                        post_norm: &lw.post_norm,
2580                        qkv,
2581                        z,
2582                        a,
2583                        b,
2584                        out,
2585                        ffn,
2586                        conv1d: &w.conv1d,
2587                        a_log: &w.a_log,
2588                        dt_bias: &w.dt_bias,
2589                        gnorm: &w.norm,
2590                    };
2591                    match plan.last_mut() {
2592                        Some(Item::Gdn { run, .. }) => run.push(gl),
2593                        _ => plan.push(Item::Gdn {
2594                            run: vec![gl],
2595                            first: scan,
2596                        }),
2597                    }
2598                }
2599                AttnKind::Full {
2600                    wq,
2601                    wk,
2602                    wv,
2603                    wo,
2604                    q_norm,
2605                    k_norm,
2606                    output_gate,
2607                    softplus_gate,
2608                    bias,
2609                } if (!self.kv_cache.layers[scan].o1_sealed()
2610                    // Sealed o1 stays plannable when the Metal o1 port
2611                    // is on: full_gpu attends through the device state,
2612                    // and any refusal falls to the sandwich, whose CPU
2613                    // core routes sealed layers through the nystrom step.
2614                    || std::env::var("CMF_O1_METAL").as_deref() == Ok("1"))
2615                    // A projected output gate: Spark-X2.5's per-head
2616                    // sigmoid form only (Laguna's softplus gate is
2617                    // unmeasured on these paths and keeps the CPU walk).
2618                    && softplus_gate
2619                        .as_ref()
2620                        .is_none_or(|(_, per_head)| *per_head && self.proj_gate_sigmoid) =>
2621                {
2622                    let parts = (
2623                        wq.metal_graph_parts(),
2624                        wk.metal_graph_parts(),
2625                        wv.metal_graph_parts(),
2626                        wo.metal_graph_parts(),
2627                    );
2628                    let (Some(pq), Some(pk), Some(pv), Some(po)) = parts else {
2629                        break;
2630                    };
2631                    if let QTensor::Mapped { model, .. } = wq {
2632                        model_ref.get_or_insert_with(|| model.clone());
2633                    }
2634                    let cache = &self.kv_cache.layers[scan];
2635                    // O(1) layer on Metal: the device attends through the
2636                    // sealed Nystrom state (opt-in while the port proves
2637                    // itself). Unsealed -> sandwich path = the CPU o1 step.
2638                    let o1_metal = cache.o1.is_some()
2639                        && std::env::var("CMF_O1_METAL").as_deref() == Ok("1")
2640                        && cache.o1_views().is_some();
2641                    // The device attend takes the layer's window and RoPE
2642                    // table, and the per-head gate from f32 rows; a gate
2643                    // held otherwise sandwiches.
2644                    let head_gate_w = softplus_gate
2645                        .as_ref()
2646                        .and_then(|(g, _)| g.f32_parts())
2647                        .filter(|&(_, r, c)| r == self.num_heads && c == self.hidden_size)
2648                        .map(|(d, _, _)| d);
2649                    // Gemma-4's global layers are 512 wide over one KV head
2650                    // (the `gemma4` 16-slot kernels, `CMF_METAL_GEMMA4_W16`).
2651                    let (nkv_g, hd_g) = if gemma4 {
2652                        let (a, b, _) = self.layer_geom(scan);
2653                        (a, b)
2654                    } else {
2655                        (self.num_kv_heads, self.head_dim)
2656                    };
2657                    let full_gpu = attend_contract
2658                        && softplus_gate.is_none() == head_gate_w.is_none()
2659                        && cache.mode == crate::kv_cache::KvMode::F32
2660                        && (cache.o1.is_none() || o1_metal)
2661                        && bias.is_none()
2662                        && (hd_g <= 256 || crate::gpu_metal::gemma4::w16_on())
2663                        && pq.1 == self.num_heads * hd_g * (1 + *output_gate as usize)
2664                        && pk.1 == nkv_g * hd_g
2665                        && pv.1 == nkv_g * hd_g
2666                        && po.2 == self.num_heads * hd_g;
2667                    plan.push(Item::Attn {
2668                        l: AttnGpuLayer {
2669                            attn_norm: &lw.input_norm,
2670                            post_norm: &lw.post_norm,
2671                            wq: pq,
2672                            wk: pk,
2673                            wv: pv,
2674                            wo: po,
2675                            ffn,
2676                            attn_out_norm: lw.attn_out_norm.as_deref(),
2677                            ffn_out_norm: lw.ffn_out_norm.as_deref(),
2678                            layer_scale: lw.layer_scale,
2679                        },
2680                        li: scan,
2681                        q_norm: q_norm.as_deref(),
2682                        k_norm: k_norm.as_deref(),
2683                        output_gate: *output_gate,
2684                        proj_gate: softplus_gate.as_ref().map(|(g, per_head)| (g, *per_head)),
2685                        head_gate_w,
2686                        bias: bias
2687                            .as_ref()
2688                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
2689                        full_gpu,
2690                    });
2691                }
2692                _ => break,
2693            }
2694            scan += 1;
2695        }
2696        let Some(model) = model_ref else {
2697            if std::env::var("CMF_GRAPH_DBG").is_ok() {
2698                eprintln!("q1-graph: no model ref (start {start}, scanned to {scan})");
2699            }
2700            if self.graph_head_required {
2701                self.fail_metal_graph("native graph has no mapped model reference");
2702            }
2703            return start;
2704        };
2705        if plan.is_empty() {
2706            if std::env::var("CMF_GRAPH_DBG").is_ok() {
2707                eprintln!("q1-graph: empty plan at layer {start}");
2708            }
2709            if self.graph_head_required {
2710                self.fail_metal_graph("native graph plan is empty");
2711            }
2712            return start;
2713        }
2714        let has_moe = plan.iter().any(|it| match it {
2715            Item::Gdn { run, .. } => run
2716                .iter()
2717                .any(|l| matches!(l.ffn, MetalFfn::Moe(_) | MetalFfn::FlintMoe(_))),
2718            Item::Attn { l, .. } => matches!(l.ffn, MetalFfn::Moe(_) | MetalFfn::FlintMoe(_)),
2719        });
2720        let has_flint_moe = plan.iter().any(|it| match it {
2721            Item::Gdn { run, .. } => run.iter().any(|l| matches!(l.ffn, MetalFfn::FlintMoe(_))),
2722            Item::Attn { l, .. } => matches!(l.ffn, MetalFfn::FlintMoe(_)),
2723        });
2724        let metal_working_set = crate::gpu_metal::working_set_bytes();
2725        let metal_max_buffer = crate::gpu_metal::max_buffer_bytes();
2726        let model_bytes = model.primary_bytes().len() as u64;
2727        // Flint q2 can sit below recommendedMaxWorkingSetSize on a 24 GB
2728        // M4 yet still exceed maxBufferLength. In that case a normal graph
2729        // would manufacture overlapping whole-file WeightArena windows;
2730        // treat it as the same bounded-page requirement rather than letting
2731        // the generic graph reintroduce broad residency.
2732        let needs_bounded_pager = (metal_working_set != 0 && model_bytes > metal_working_set)
2733            || (metal_max_buffer != 0 && model_bytes > metal_max_buffer);
2734        // An oversized MoE mmap normally has to decline: the jobs kernels
2735        // bind all WeightArena windows.  The opt-in pager takes a different,
2736        // bounded route: a *complete sequence of logical layers* is put in
2737        // one transient no-copy page, graph-finished, dropped, then the outer
2738        // layer loop calls us again for the next page.  That preserves exact
2739        // hidden/GDN/KV handoff while replacing ~40 host-routed expert waits
2740        // with a few page-boundary syncs.  Never use it for a fused-head NLL
2741        // request: that contract explicitly requires a single whole graph.
2742        let pager_cap = if !has_moe {
2743            None
2744        } else if has_flint_moe {
2745            // Do not let generic `CMF_METAL_MOE_PAGER` turn the experimental
2746            // full-bank Flint graph on. Its direct selected-view pager stays
2747            // independently automatic; this path needs its own opt-in.
2748            crate::gpu_metal::flint_moe_pager_cap_bytes(&model)
2749        } else {
2750            crate::gpu_metal::moe_pager_cap_bytes()
2751        };
2752        let pager_eligible =
2753            pager_cap.is_some() && !self.graph_head_required && needs_bounded_pager;
2754        let mut pager_span: Option<(usize, usize)> = None;
2755        if pager_eligible {
2756            let cap = pager_cap.expect("checked above");
2757            // When this page reaches the true model end, include the fused
2758            // lm_head too.  Otherwise `lm_head_ok` would correctly refuse a
2759            // binding outside the page after the layers had already run.
2760            let tail_span = (self.graph_want_logits
2761                && upto.is_none()
2762                && scan == self.num_layers
2763                && std::env::var("CMF_GPU_LMHEAD")
2764                    .map(|v| v != "0")
2765                    .unwrap_or(true))
2766            .then(|| self.weights.lm_head.metal_graph_parts())
2767            .flatten()
2768            .and_then(|lm| crate::gpu_metal::model_tensor_span(&model, lm.0));
2769            let add_layer_span = |span: Option<(usize, usize)>, li: usize| {
2770                let (lo, hi) = crate::gpu_metal::model_layer_span(&model, self.phys_layer(li))?;
2771                Some(match span {
2772                    Some((cur_lo, cur_hi)) => (cur_lo.min(lo), cur_hi.max(hi)),
2773                    None => (lo, hi),
2774                })
2775            };
2776            let page_span = |span: (usize, usize), include_tail: bool| {
2777                let (mut lo, mut hi) = span;
2778                if include_tail {
2779                    if let Some((tail_lo, tail_hi)) = tail_span {
2780                        lo = lo.min(tail_lo);
2781                        hi = hi.max(tail_hi);
2782                    }
2783                }
2784                crate::gpu_metal::page_round_span(lo, hi)
2785            };
2786            let mut selected = 0usize;
2787            let mut selected_span: Option<(usize, usize)> = None;
2788            // `Item::Gdn` is built as a maximal run so the normal resident
2789            // graph can batch its recurrent state work.  A physical CMF can
2790            // nevertheless place that whole run across a page cap while each
2791            // logical layer fits.  Split *only the final admitted run* at a
2792            // layer boundary: the next outer-loop call rebuilds the remaining
2793            // suffix, preserving hidden/state handoff and keeping the normal
2794            // long-run batching within every page.
2795            let mut gdn_prefix: Option<(usize, usize)> = None;
2796            'items: for (i, item) in plan.iter().enumerate() {
2797                match item {
2798                    Item::Attn { li, .. } => {
2799                        let Some(span) = add_layer_span(selected_span, *li) else {
2800                            break;
2801                        };
2802                        let Some((page_lo, page_hi)) = page_span(span, i + 1 == plan.len()) else {
2803                            break;
2804                        };
2805                        if page_hi - page_lo > cap {
2806                            break;
2807                        }
2808                        selected = i + 1;
2809                        selected_span = Some((page_lo, page_hi));
2810                    }
2811                    Item::Gdn { first, run } => {
2812                        // Preserve a complete recurrent run whenever it can
2813                        // occupy a page on its own.  If the preceding items
2814                        // merely used the remaining cap, close that page and
2815                        // let the next q1 invocation retain the full run.
2816                        // Prefix-splitting is only for a run that genuinely
2817                        // cannot fit an empty page.
2818                        let mut whole = None;
2819                        for take in 1..=run.len() {
2820                            whole = add_layer_span(whole, *first + take - 1);
2821                            if whole.is_none() {
2822                                break;
2823                            }
2824                        }
2825                        let whole_fits_empty_page = whole
2826                            .and_then(|span| page_span(span, false))
2827                            .is_some_and(|(lo, hi)| hi - lo <= cap);
2828                        if whole_fits_empty_page {
2829                            let mut span = selected_span;
2830                            for take in 1..=run.len() {
2831                                let Some(next) = add_layer_span(span, *first + take - 1) else {
2832                                    break 'items;
2833                                };
2834                                span = Some(next);
2835                            }
2836                            let Some(span) = span else {
2837                                break;
2838                            };
2839                            let Some((page_lo, page_hi)) = page_span(span, i + 1 == plan.len())
2840                            else {
2841                                break;
2842                            };
2843                            if page_hi - page_lo > cap {
2844                                break;
2845                            }
2846                            selected = i + 1;
2847                            selected_span = Some((page_lo, page_hi));
2848                            continue;
2849                        }
2850                        if selected != 0 {
2851                            break;
2852                        }
2853                        let mut span = None;
2854                        let mut best: Option<(usize, (usize, usize))> = None;
2855                        for take in 1..=run.len() {
2856                            let Some(next) = add_layer_span(span, *first + take - 1) else {
2857                                break;
2858                            };
2859                            // A partial final run cannot encode the lm_head;
2860                            // only its complete form above may include it.
2861                            let Some((page_lo, page_hi)) = page_span(next, false) else {
2862                                break;
2863                            };
2864                            if page_hi - page_lo > cap {
2865                                break;
2866                            }
2867                            span = Some((page_lo, page_hi));
2868                            best = Some((take, (page_lo, page_hi)));
2869                        }
2870                        let Some((take, page)) = best else {
2871                            break;
2872                        };
2873                        selected = i + 1;
2874                        selected_span = Some(page);
2875                        if take != run.len() {
2876                            gdn_prefix = Some((i, take));
2877                            break 'items;
2878                        }
2879                    }
2880                }
2881            }
2882            if selected != 0 {
2883                if let Some((item, take)) = gdn_prefix {
2884                    match &mut plan[item] {
2885                        Item::Gdn { run, .. } => run.truncate(take),
2886                        Item::Attn { .. } => unreachable!("only a GDN run is splittable"),
2887                    }
2888                }
2889                let (page_lo, page_hi) = selected_span.expect("selected page span");
2890                tracing::info!(
2891                    page_start = page_lo,
2892                    page_end = page_hi,
2893                    page_mb = (page_hi - page_lo) / (1024 * 1024),
2894                    cap_mb = cap / (1024 * 1024),
2895                    graph_items = selected,
2896                    "native Metal MoE graph uses transient bounded weight page"
2897                );
2898                plan.truncate(selected);
2899                pager_span = Some((page_lo, page_hi));
2900            } else {
2901                tracing::warn!(
2902                    cap_mb = cap / (1024 * 1024),
2903                    "native Metal MoE pager declined: one graph item does not fit its safe page cap"
2904                );
2905            }
2906        }
2907        // A native MoE graph binds every WeightArena window to its jobs
2908        // kernels. On UMA, an mmap that already exceeds the driver's
2909        // recommended working set will therefore fail after admission, when
2910        // some recurrent/KV scratch has already been touched. Decline before
2911        // allocating/encoding the graph instead; the ordinary per-operation
2912        // route remains available and only materializes the needed window.
2913        // Do not apply this to dense graphs: they bind one projection at a
2914        // time and do not have the MoE jobs' all-window lifetime.
2915        if has_moe && needs_bounded_pager && pager_span.is_none() {
2916            tracing::info!(
2917                model_bytes,
2918                metal_working_set,
2919                "native Metal MoE graph declined before allocation"
2920            );
2921            self.note_graph_decline("metal block graph", MOE_WORKING_SET_REASON);
2922            if self.graph_head_required {
2923                self.fail_metal_graph(MOE_WORKING_SET_REASON);
2924            }
2925            return start;
2926        }
2927        let has_gdn = plan.iter().any(|it| matches!(it, Item::Gdn { .. }));
2928        let dev_attend = attend_contract
2929            && (self.head_dim <= 128
2930                || has_moe
2931                // A GDN hybrid attends on a quarter of its layers: the
2932                // hd>128 caution was measured on pure-dense models where
2933                // gqa_attend dominates, and on Qwen3.8-27B (hd 256, 48
2934                // GDN + 16 attn) the sandwich costs 2x the whole decode
2935                // (1.2 vs 2.21 tok/s measured before the arena fix).
2936                || (self.head_dim <= 256 && has_gdn)
2937                // Sliding-window layers bound most attends by the window:
2938                // Spark-X2.5-1.7B q4tp on the M4 decodes 67 tok/s
2939                // device-attended against 23 sandwiched (28 syncs/token).
2940                || (self.head_dim <= 256 && swa_graph)
2941                || attend_mode == "force"
2942                || attend_mode == "256");
2943        if !dev_attend {
2944            for it in &mut plan {
2945                if let Item::Attn { li, full_gpu, .. } = it {
2946                    // The hd>128 policy is about gqa_attend; an o1 layer
2947                    // attends through its own kernel set.
2948                    let keep_o1 = self.kv_cache.layers[*li].o1.is_some()
2949                        && std::env::var("CMF_O1_METAL").as_deref() == Ok("1");
2950                    if !keep_o1 {
2951                        *full_gpu = false;
2952                    }
2953                }
2954            }
2955        }
2956        if std::env::var("CMF_GRAPH_DBG").is_ok() {
2957            use std::sync::atomic::{AtomicBool, Ordering};
2958            static SAID: AtomicBool = AtomicBool::new(false);
2959            if !SAID.swap(true, Ordering::Relaxed) {
2960                let fg = plan
2961                    .iter()
2962                    .filter(|it| matches!(it, Item::Attn { full_gpu: true, .. }))
2963                    .count();
2964                let att = plan
2965                    .iter()
2966                    .filter(|it| matches!(it, Item::Attn { .. }))
2967                    .count();
2968                eprintln!(
2969                    "q1-graph: plan of {} items from layer {start} to {scan} | dev_attend={dev_attend} full_gpu {fg}/{att} | hd={} rd={} nkv={} nh={}",
2970                    plan.len(),
2971                    self.head_dim,
2972                    self.rotary_dim,
2973                    self.num_kv_heads,
2974                    self.num_heads,
2975                );
2976            }
2977        }
2978        let dims = GraphDims {
2979            hidden: self.hidden_size,
2980            eps: self.rms_eps as f32,
2981            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
2982        };
2983        // The outer `forward_layers_span` scope is one logical token.  This
2984        // guard starts at q1_graph_gpu entry and ends after all page readback,
2985        // so a multi-page token cannot be reported as several whole tokens.
2986        let mut pager_profile_page = pager_span.map(|(page_lo, page_hi)| {
2987            crate::gpu_metal::MoePagerPageScope::new(
2988                _mt0,
2989                position,
2990                start,
2991                page_lo,
2992                page_hi,
2993                // `pager_span` is selected only after a cap was admitted;
2994                // keep the profiler observational if that invariant is ever
2995                // loosened rather than introducing a new inference panic.
2996                pager_cap.unwrap_or(0),
2997            )
2998        });
2999        let graph_new = match pager_span {
3000            Some((page_lo, page_hi)) => TokenGraph::new_paged(&model, dims, h, page_lo, page_hi),
3001            None => TokenGraph::new(&model, dims, h),
3002        };
3003        let Some(mut graph) = graph_new else {
3004            if self.graph_head_required {
3005                self.fail_metal_graph("native TokenGraph allocation refused");
3006            }
3007            return start;
3008        };
3009        if swa_graph && self.head_dim > 128 {
3010            // Spark-X2.5 (hd 256, 4 Q heads per KV head): the GQA-shared
3011            // split-K attend reads each K/V row once for the group instead
3012            // of once per head plus the importance re-read — at depth 1000
3013            // on the M4, 59.5 tok/s against 50.0 with the per-head kernel
3014            // up to the default 512 (every sliding layer sits at <= 512).
3015            graph.set_attend_blk_from(64);
3016        }
3017        let gcfg = self.gdn_cfg.map(|cfg| GdnGpuCfg {
3018            nv: cfg.num_v_heads,
3019            nk: cfg.num_k_heads,
3020            dk: cfg.key_head_dim,
3021            dv: cfg.value_head_dim,
3022            kk: cfg.conv_kernel,
3023            hidden: self.hidden_size,
3024            inter: self.intermediate_size,
3025            c_dim: cfg.conv_dim(),
3026            eps: cfg.rms_eps as f32,
3027            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
3028        });
3029        // Validate the whole plan BEFORE encoding anything: after the
3030        // first sync a refused layer would leave the token
3031        // half-executed, so truncate to the provably encodable prefix.
3032        let mut valid = 0usize;
3033        let mut end = start;
3034        crate::gpu::stageprof(1, _mt0.elapsed()); // конец планирования
3035        if std::env::var("CMF_PLAN_DUMP").is_ok() {
3036            static ONCE: std::sync::Once = std::sync::Once::new();
3037            ONCE.call_once(|| {
3038                for it in &plan {
3039                    match it {
3040                        Item::Gdn { first, run } => {
3041                            eprintln!("plan: Gdn first={first} len={}", run.len())
3042                        }
3043                        Item::Attn { li, full_gpu, .. } => {
3044                            eprintln!("plan: Attn li={li} full_gpu={full_gpu}")
3045                        }
3046                    }
3047                }
3048            });
3049        }
3050        for item in &plan {
3051            let ok = match item {
3052                Item::Gdn { run, .. } => gcfg
3053                    .as_ref()
3054                    .map(|gc| run.iter().all(|l| graph.gdn_ok(l, gc)))
3055                    .unwrap_or(false),
3056                Item::Attn { l, .. } => graph.attn_ok(l),
3057            };
3058            if !ok {
3059                if block_diag {
3060                    eprintln!(
3061                        "block-graph: plan item {} ({}) failed graph preflight",
3062                        valid,
3063                        match item {
3064                            Item::Gdn { run, first } => format!("GDN run L{first}+{}", run.len()),
3065                            Item::Attn { li, .. } => format!("Attn L{li}"),
3066                        }
3067                    );
3068                }
3069                break;
3070            }
3071            valid += 1;
3072            end += match item {
3073                Item::Gdn { run, .. } => run.len(),
3074                Item::Attn { .. } => 1,
3075            };
3076        }
3077        plan.truncate(valid);
3078        if plan.is_empty() {
3079            if self.graph_head_required {
3080                self.fail_metal_graph("native graph preflight produced no valid items");
3081            }
3082            return start;
3083        }
3084
3085        if self.graph_head_required && (upto.is_some() || end != self.num_layers) {
3086            self.fail_metal_graph("fused-head NLL requires a complete 64-layer graph");
3087            return start;
3088        }
3089
3090        // Plain dense decode (every item a device-attended full-attention
3091        // layer with a dense FFN, no O(1) state): the only plan shape the
3092        // masked-nibble q4tp matvec and the concurrent layer encoder were
3093        // measured on (MiniCPM5-2B, Qwen3-0.6B on the M4). Hybrids, MoE and
3094        // o1 layers keep the historical serial path bit for bit.
3095        // Every projection must be ONE dispatch (q1t adds an overlay pass,
3096        // Prism q2tp a transform pass — dependent pairs a concurrent
3097        // encoder would race).
3098        let one_pass = |t: (usize, usize, usize)| {
3099            use cortiq_core::TensorDtype as D;
3100            matches!(
3101                model.tensors[t.0].dtype,
3102                D::Q4TiledP | D::Q4Tiled | D::Q4Block | D::Q8Row | D::Q8_2f | D::Q1 | D::Q1G
3103            )
3104        };
3105        let dense_fast = plan.iter().all(|it| match it {
3106            Item::Attn {
3107                l, li, full_gpu, ..
3108            } => {
3109                *full_gpu
3110                    && self.kv_cache.layers[*li].o1.is_none()
3111                    && [l.wq, l.wk, l.wv, l.wo].into_iter().all(one_pass)
3112                    && match l.ffn {
3113                        MetalFfn::Dense { gate, up, down, .. } => {
3114                            one_pass(gate) && one_pass(up) && one_pass(down)
3115                        }
3116                        _ => false,
3117                    }
3118            }
3119            Item::Gdn { .. } => false,
3120        });
3121        // MoE decode with every layer device-attended (Mellum2.1): the
3122        // routed experts' jobs ladder and the q8_2f attention projections
3123        // take their measured levers (`MOE_*`, `DENSE_Q8R4`).
3124        let moe_plan = !dense_fast
3125            && plan.iter().all(|it| match it {
3126                Item::Attn {
3127                    l, li, full_gpu, ..
3128                } => {
3129                    *full_gpu
3130                        && self.kv_cache.layers[*li].o1.is_none()
3131                        && matches!(l.ffn, MetalFfn::Moe(_) | MetalFfn::FlintMoe(_))
3132                }
3133                Item::Gdn { .. } => false,
3134            });
3135        let ab = crate::gpu_metal::dense_ab_arm().filter(|_| dense_fast || moe_plan);
3136        if block_diag {
3137            use std::sync::atomic::{AtomicBool, Ordering};
3138            static SAID_AB: AtomicBool = AtomicBool::new(false);
3139            if !SAID_AB.swap(true, Ordering::Relaxed) {
3140                eprintln!("q1-graph: dense_fast={dense_fast} moe_plan={moe_plan} ab={ab:?}");
3141            }
3142        }
3143        let _mv_fast = match ab {
3144            Some((bits, _)) => {
3145                graph.set_dense_concurrent_raw(bits & crate::gpu_metal::DENSE_CONC != 0);
3146                crate::gpu_metal::MvFastGuard::set_raw(bits)
3147            }
3148            None if moe_plan => {
3149                graph.set_dense_concurrent(
3150                    crate::gpu_metal::MOE_PLAN_BITS & crate::gpu_metal::DENSE_CONC != 0,
3151                );
3152                crate::gpu_metal::MvFastGuard::set_bits(crate::gpu_metal::MOE_PLAN_BITS)
3153            }
3154            None => {
3155                graph.set_dense_concurrent(dense_fast);
3156                // Spark-X2.5 q8_2f: the four-row q8_2f matvec decodes the
3157                // 1.7B at 40.5 tok/s on the M4 against 27.7 one-row.
3158                let q8r4 = if swa_graph {
3159                    crate::gpu_metal::DENSE_Q8R4
3160                } else {
3161                    0
3162                };
3163                crate::gpu_metal::MvFastGuard::set_bits(if dense_fast {
3164                    crate::gpu_metal::DENSE_MV | crate::gpu_metal::DENSE_FUSE | q8r4
3165                } else {
3166                    0
3167                })
3168            }
3169        };
3170
3171        // RoPE table and rotary width are per layer (`layer_inv_freq`,
3172        // `layer_geom`) inside the loop below.
3173        let pool = self.pool.clone();
3174        let (nh, nkv, hd, hs, eps) = (
3175            self.num_heads,
3176            self.num_kv_heads,
3177            self.head_dim,
3178            self.hidden_size,
3179            self.rms_eps,
3180        );
3181        let norm_style = self.norm_style;
3182        let gemma = norm_style == cortiq_core::NormStyle::Gemma;
3183        let want = self.gdn_cfg.map(|c| c.state_len()).unwrap_or(0);
3184        let kv_id = self.graph_kv_id;
3185        // GDN runs whose states await readback after the next sync
3186        // (device-attended layers add no sync, so several may stack).
3187        let mut pending: Vec<(usize, usize)> = Vec::new();
3188        // Device-attended layers: their K/V/imp are pulled from the
3189        // mirror after the final sync.
3190        let mut dev_attn: Vec<(usize, usize, usize)> = Vec::new();
3191        for item in &plan {
3192            let _xt0 = std::time::Instant::now();
3193            let _xkind: u32 = match item {
3194                Item::Gdn { .. } => 2,
3195                Item::Attn { .. } => 3,
3196            };
3197            // Looped Transformer: insert on-device norm at loop boundaries.
3198            if self.loop_final_norm {
3199                let item_start = match item {
3200                    Item::Gdn { first, .. } => *first,
3201                    Item::Attn { li, .. } => *li,
3202                };
3203                if item_start > start && self.is_loop_end(item_start - 1) {
3204                    graph.encode_loop_norm(&self.weights.final_norm);
3205                }
3206            }
3207            match item {
3208                Item::Gdn { run, first } => {
3209                    for l in &mut self.kv_cache.layers[*first..*first + run.len()] {
3210                        if l.linear_state.len() != want {
3211                            l.linear_state = vec![0f32; want];
3212                        }
3213                    }
3214                    let ro: Vec<&[f32]> = self.kv_cache.layers[*first..*first + run.len()]
3215                        .iter()
3216                        .map(|l| l.linear_state.as_slice())
3217                        .collect();
3218                    let _ig = std::time::Instant::now();
3219                    if !graph.encode_gdn_run(run, &ro, gcfg.as_ref().unwrap()) {
3220                        // Unreachable: the plan was validated above.
3221                        tracing::error!("q1 graph: GDN run refused after validation");
3222                        return start;
3223                    }
3224                    // Early commit: the GPU starts the run while the
3225                    // CPU encodes the next layer (nothing to wait on).
3226                    graph.commit_kind = 2;
3227                    graph.commit();
3228                    crate::gpu::stageprof(0, _ig.elapsed());
3229                    pending.push((*first, run.len()));
3230                }
3231                Item::Attn {
3232                    l,
3233                    li,
3234                    q_norm,
3235                    k_norm,
3236                    output_gate,
3237                    proj_gate,
3238                    head_gate_w,
3239                    bias,
3240                    full_gpu,
3241                } => {
3242                    let _ia = std::time::Instant::now();
3243                    // This layer's attention geometry: its window, RoPE
3244                    // table and rotary width (Spark-X2.5 interleaves
3245                    // 512-window layers rotating all dims at θ 1e4 with
3246                    // full layers rotating a quarter at θ 5e6). Every model
3247                    // without sliding layers reads the global ones here.
3248                    let inv_freq_l = self.layer_inv_freq(*li);
3249                    let rd_l = self.layer_geom(*li).2;
3250                    let window_l = self.layer_window(*li);
3251                    let head_gate_w = *head_gate_w;
3252                    // This layer's KV heads and head width: Gemma-4's global
3253                    // layers (one 512-wide KV head) differ from the base.
3254                    let (nkv, hd) = if gemma4 {
3255                        let (a, b, _) = self.layer_geom(*li);
3256                        (a, b)
3257                    } else {
3258                        (nkv, hd)
3259                    };
3260                    // ── Fully device-resident attention: no sync at all.
3261                    if *full_gpu {
3262                        let cache = &self.kv_cache.layers[*li];
3263                        let o1p = if cache.o1.is_some() {
3264                            match cache.o1_views() {
3265                                Some(views) => Some(crate::gpu::O1AttnParams {
3266                                    views,
3267                                    epoch: self.o1_epoch,
3268                                }),
3269                                // Sealed state gone mid-run: sandwich.
3270                                None => None,
3271                            }
3272                        } else {
3273                            None
3274                        };
3275                        let o1_layer = cache.o1.is_some();
3276                        if o1_layer && o1p.is_none() {
3277                            // fall to the sandwich (CPU o1 step)
3278                        }
3279                        let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
3280                        let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
3281                        let cpu_stored = if o1_layer { 0 } else { cpu_k[0].len() / hd };
3282                        // A trimmed tail always holds the window the
3283                        // device attend reads (`first = rows + 1 − w`).
3284                        debug_assert!(
3285                            cache.base() == 0 || window_l.is_some_and(|w| cpu_stored + 1 >= w)
3286                        );
3287                        let p = crate::gpu::AttnDeviceParams {
3288                            kv_id,
3289                            layer: *li,
3290                            nh,
3291                            nkv,
3292                            hd,
3293                            rd: rd_l,
3294                            position,
3295                            scale: self.attn_scale,
3296                            eps: eps as f32,
3297                            gemma,
3298                            late_qk_norm: self.qk_norm_after_rope,
3299                            output_gate: *output_gate,
3300                            q_norm: *q_norm,
3301                            k_norm: *k_norm,
3302                            inv_freq: &inv_freq_l,
3303                            rope_scale: self.layer_rope_scale(*li),
3304                            cpu_k,
3305                            cpu_v,
3306                            cpu_stored,
3307                            cpu_gen: cache.generation(),
3308                            o1: o1p,
3309                            window: window_l,
3310                            softmax_sink: cache.sinks.as_deref(),
3311                            head_gate: head_gate_w,
3312                            v_norm: self.attn_v_norm,
3313                        };
3314                        let o1_bad = o1_layer && p.o1.is_none();
3315                        if !o1_bad && graph.attn_device_ok(l, &p) && graph.encode_attn_device(l, &p)
3316                        {
3317                            // o1 layers leave no mirror row to pull.
3318                            if p.o1.is_none() {
3319                                dev_attn.push((*li, nkv, hd));
3320                            }
3321                            graph.commit_kind = 3;
3322                            graph.commit();
3323                            // The footer below is skipped by `continue`:
3324                            // account the device-attn item here or its
3325                            // cost hides from the stage profile entirely.
3326                            crate::gpu::stageprof(_xkind, _xt0.elapsed());
3327                            continue;
3328                        }
3329                        // Mirror refused (nothing encoded) → sandwich.
3330                    }
3331                    graph.encode_attn_prefix(l);
3332                    if let Err(err) = graph.sync_checked() {
3333                        self.fail_metal_graph(&err);
3334                        return start;
3335                    }
3336                    if !pending.is_empty() {
3337                        let idxs: Vec<usize> =
3338                            pending.drain(..).flat_map(|(f, n)| f..f + n).collect();
3339                        let mut outs: Vec<&mut [f32]> = self
3340                            .kv_cache
3341                            .layers
3342                            .iter_mut()
3343                            .enumerate()
3344                            .filter(|(i, _)| idxs.binary_search(i).is_ok())
3345                            .map(|(_, s)| s.linear_state.as_mut_slice())
3346                            .collect();
3347                        graph.read_states(&mut outs);
3348                    }
3349                    let mut q_raw = attention::take_buf(l.wq.1);
3350                    let mut k = attention::take_buf(l.wk.1);
3351                    let mut v = attention::take_buf(l.wv.1);
3352                    graph.read_qkv(&mut q_raw, &mut k, &mut v);
3353                    // Projected output gate: g = G·norm(h) on the host, off
3354                    // the normed input the prefix left on the device.
3355                    let mut gate_raw = proj_gate.map(|(gp, _)| {
3356                        let mut normed = attention::take_buf(hs);
3357                        graph.read_normed(&mut normed);
3358                        let mut raw = attention::take_buf(gp.rows());
3359                        gp.matvec(&normed, &mut raw, pool.as_deref());
3360                        attention::recycle_buf(&mut normed);
3361                        raw
3362                    });
3363                    let cfg = QwenAttnCfg {
3364                        num_heads: nh,
3365                        num_kv_heads: nkv,
3366                        head_dim: hd,
3367                        hidden_size: hs,
3368                        position,
3369                        inv_freq: &inv_freq_l,
3370                        rotary_dim: rd_l,
3371                        scale: self.attn_scale,
3372                        softcap: self.attn_softcap,
3373                        window: window_l,
3374                        // Gemma-4 only (the front gate refuses V norm
3375                        // otherwise).
3376                        v_norm: self.attn_v_norm,
3377                        qk_norm_after_rope: self.qk_norm_after_rope,
3378                        gate_sigmoid: self.proj_gate_sigmoid,
3379                        q_norm: *q_norm,
3380                        k_norm: *k_norm,
3381                        output_gate: *output_gate,
3382                        softplus_gate: None,
3383                        rope_scale: self.layer_rope_scale(*li),
3384                        bias: *bias,
3385                        rms_eps: eps,
3386                        norm_style,
3387                        pool: pool.as_deref(),
3388                        v_head_dim: hd,
3389                    };
3390                    // CMF_ATTN_ORACLE=1: diff the device attend against
3391                    // this CPU attend on identical inputs (bring-up).
3392                    let oracle = std::env::var("CMF_ATTN_ORACLE").as_deref() == Ok("1")
3393                        || std::env::var("CMF_ATTN_DUMP").is_ok();
3394                    let _ = full_gpu;
3395                    let oracle_in = oracle.then(|| (q_raw.clone(), k.clone(), v.clone()));
3396                    let mut ao = attention::qwen_attention_core(
3397                        q_raw,
3398                        k,
3399                        v,
3400                        &mut self.kv_cache.layers[*li],
3401                        &cfg,
3402                    );
3403                    // CMF_ATTN_DUMP=<dir>: this token's rope'd Q and the layer's whole
3404                    // K/V cache as raw f32 (offline attention-statistics probes:
3405                    // block bounds, mass concentration). Needs CMF_GPU_ATTEND=0.
3406                    if let Ok(dir) = std::env::var("CMF_ATTN_DUMP") {
3407                        if let Some((qr0, k0, v0)) = oracle_in.clone() {
3408                            let (cq, _cg, _ck, _cv) =
3409                                attention::finish_projection_debug(qr0, k0, v0, &cfg, position);
3410                            let cache = &self.kv_cache.layers[*li];
3411                            let n = cache.head_keys(0).len() / hd;
3412                            let mut bytes: Vec<u8> = Vec::new();
3413                            for v in [nh as u32, nkv as u32, hd as u32, n as u32, position as u32] {
3414                                bytes.extend_from_slice(&v.to_le_bytes());
3415                            }
3416                            for v in &cq {
3417                                bytes.extend_from_slice(&v.to_le_bytes());
3418                            }
3419                            for g in 0..nkv {
3420                                for v in cache.head_keys(g) {
3421                                    bytes.extend_from_slice(&v.to_le_bytes());
3422                                }
3423                            }
3424                            for g in 0..nkv {
3425                                for v in cache.head_values(g) {
3426                                    bytes.extend_from_slice(&v.to_le_bytes());
3427                                }
3428                            }
3429                            let _ =
3430                                std::fs::write(format!("{dir}/L{li}_pos{position}.bin"), &bytes);
3431                        }
3432                    }
3433                    if let Some((qr0, k0, v0)) =
3434                        oracle_in.filter(|_| std::env::var("CMF_ATTN_ORACLE").as_deref() == Ok("1"))
3435                    {
3436                        let (cq, _cg, ck, cv) =
3437                            attention::finish_projection_debug(qr0, k0, v0, &cfg, position);
3438                        let mut h_now = vec![0f32; hs];
3439                        graph.read_h(&mut h_now);
3440                        let cache = &self.kv_cache.layers[*li];
3441                        let n_after = cache.head_keys(0).len() / hd;
3442                        // A sealed O(1) cache may have no dense current-row
3443                        // entry. The oracle is a debug probe, so let it see
3444                        // zero stored exact rows instead of underflowing.
3445                        let stored = n_after.saturating_sub(1);
3446                        let cpu_k: Vec<&[f32]> = (0..nkv)
3447                            .map(|g| &cache.head_keys(g)[..stored * hd])
3448                            .collect();
3449                        let cpu_v: Vec<&[f32]> = (0..nkv)
3450                            .map(|g| &cache.head_values(g)[..stored * hd])
3451                            .collect();
3452                        let p = crate::gpu::AttnDeviceParams {
3453                            kv_id,
3454                            layer: *li,
3455                            nh,
3456                            nkv,
3457                            hd,
3458                            // The layer's own geometry, as the CPU attend
3459                            // above used it (the projected gate is applied
3460                            // after this probe on both sides).
3461                            rd: rd_l,
3462                            position,
3463                            scale: self.attn_scale,
3464                            eps: eps as f32,
3465                            gemma,
3466                            late_qk_norm: self.qk_norm_after_rope,
3467                            output_gate: *output_gate,
3468                            q_norm: *q_norm,
3469                            k_norm: *k_norm,
3470                            inv_freq: &inv_freq_l,
3471                            rope_scale: self.layer_rope_scale(*li),
3472                            cpu_k,
3473                            cpu_v,
3474                            cpu_stored: stored,
3475                            cpu_gen: cache.generation(),
3476                            o1: None,
3477                            window: window_l,
3478                            softmax_sink: cache.sinks.as_deref(),
3479                            head_gate: None,
3480                            v_norm: self.attn_v_norm,
3481                        };
3482                        if let Some((dq, dk, dv, dao)) = graph.debug_attn_device(l, &p, &h_now) {
3483                            let md = |a: &[f32], b: &[f32]| {
3484                                a.iter().zip(b).fold(0f32, |m, (x, y)| m.max((x - y).abs()))
3485                            };
3486                            let nn = |a: &[f32]| a.iter().map(|x| x * x).sum::<f32>().sqrt();
3487                            eprintln!(
3488                                "attn-oracle L{li} pos {position}: |q| {:.2} max|dq| {:.4} | |k| {:.2} max|dk| {:.4} | |v| {:.2} max|dv| {:.4} | |ao| {:.2} max|dao| {:.4}",
3489                                nn(&cq),
3490                                md(&cq, &dq),
3491                                nn(&ck),
3492                                md(&ck, &dk),
3493                                nn(&cv),
3494                                md(&cv, &dv),
3495                                nn(&ao),
3496                                md(&ao, &dao)
3497                            );
3498                        } else {
3499                            eprintln!("attn-oracle L{li}: device probe declined");
3500                        }
3501                    }
3502                    if let (Some(raw), Some((_, per_head))) = (gate_raw.as_deref(), *proj_gate) {
3503                        // V is as wide as the head (the front gate), so ao
3504                        // is nh·hd here, as in `qwen_attention`.
3505                        attention::apply_projected_gate(
3506                            &mut ao,
3507                            raw,
3508                            per_head,
3509                            hd,
3510                            self.proj_gate_sigmoid,
3511                        );
3512                    }
3513                    if let Some(mut raw) = gate_raw.take() {
3514                        attention::recycle_buf(&mut raw);
3515                    }
3516                    graph.encode_attn_suffix(l, &ao);
3517                    // Early commit: the GPU starts O+FFN while the CPU
3518                    // encodes the following GDN run / attention prefix.
3519                    graph.commit();
3520                    attention::recycle_buf(&mut ao);
3521                }
3522            }
3523
3524            crate::gpu::stageprof(_xkind, _xt0.elapsed());
3525        }
3526        // Ride the final norm + lm_head in the same command buffer when
3527        // this run reaches the model's end and the caller wants logits:
3528        // the separate per-op lm_head submit (a full round trip) folds
3529        // into the sync that already happens here.
3530        let mut lm_rows = None;
3531        if self.graph_want_logits
3532            && upto.is_none()
3533            && end == self.num_layers
3534            && std::env::var("CMF_GPU_LMHEAD")
3535                .map(|v| v != "0")
3536                .unwrap_or(true)
3537        {
3538            if let Some(lm) = self.weights.lm_head.metal_graph_parts() {
3539                if graph.lm_head_ok(lm) {
3540                    graph.encode_lm_head(&self.weights.final_norm, lm);
3541                    lm_rows = Some(lm.1);
3542                }
3543            }
3544        }
3545        if self.graph_head_required && lm_rows.is_none() {
3546            METAL_GRAPH_HEAD_MISS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
3547            self.fail_metal_graph("fused graph head was requested but not encodable");
3548            return start;
3549        }
3550        let _sy0 = std::time::Instant::now();
3551        if let Err(err) = graph.sync_checked() {
3552            if let Some(profile) = pager_profile_page.as_mut() {
3553                profile.finish(start, graph.pager_page_stats(), "sync-error");
3554            }
3555            self.fail_metal_graph(&err);
3556            return start;
3557        }
3558        let _rs0 = std::time::Instant::now();
3559        if !pending.is_empty() {
3560            let idxs: Vec<usize> = pending.drain(..).flat_map(|(f, n)| f..f + n).collect();
3561            let mut outs: Vec<&mut [f32]> = self
3562                .kv_cache
3563                .layers
3564                .iter_mut()
3565                .enumerate()
3566                .filter(|(i, _)| idxs.binary_search(i).is_ok())
3567                .map(|(_, s)| s.linear_state.as_mut_slice())
3568                .collect();
3569            graph.read_states(&mut outs);
3570        }
3571        if std::env::var("CMF_GRAPH_HOSTPROF").as_deref() == Ok("1") {
3572            use std::sync::atomic::{AtomicU64, Ordering};
3573            static SY: AtomicU64 = AtomicU64::new(0);
3574            static RS: AtomicU64 = AtomicU64::new(0);
3575            static N: AtomicU64 = AtomicU64::new(0);
3576            SY.fetch_add((_rs0 - _sy0).as_nanos() as u64, Ordering::Relaxed);
3577            RS.fetch_add(_rs0.elapsed().as_nanos() as u64, Ordering::Relaxed);
3578            let n = N.fetch_add(1, Ordering::Relaxed) + 1;
3579            if n % 100 == 0 {
3580                eprintln!(
3581                    "postprof: sync-wait {:.1} ms/tok | read_states {:.1} ms/tok ({n})",
3582                    SY.load(Ordering::Relaxed) as f64 / n as f64 / 1e6,
3583                    RS.load(Ordering::Relaxed) as f64 / n as f64 / 1e6
3584                );
3585            }
3586        }
3587        if let Some(rows) = lm_rows {
3588            crate::gpu::hostprof_encode_done(_mt0);
3589            let mut lg = attention::take_buf(rows.min(self.vocab_size));
3590            graph.read_logits(&mut lg);
3591            crate::gpu::hostprof_total(_mt0);
3592            lg.resize(self.vocab_size, 0.0);
3593            if let Some(c) = self.final_softcap {
3594                softcap_in_place(&mut lg, c, self.pool.as_deref());
3595            }
3596            self.graph_logits = Some(lg);
3597        }
3598        graph.read_h(h);
3599        if self.graph_head_required && self.graph_logits.is_none() {
3600            METAL_GRAPH_HEAD_MISS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
3601            self.fail_metal_graph("fused graph head completed without logits readback");
3602            return start;
3603        }
3604        METAL_GRAPH_TOK_OK.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
3605        METAL_GRAPH_LAYERS.fetch_add(
3606            end.saturating_sub(start) as u64,
3607            std::sync::atomic::Ordering::Relaxed,
3608        );
3609        if self.graph_head_required {
3610            METAL_GRAPH_HEAD_OK.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
3611        }
3612        // Device-attended layers: replay the CPU bookkeeping — append
3613        // the mirror's new K/V row (rope'd on the GPU) into the owner
3614        // cache, then bank this token's attention-importance mass.
3615        for (li, nkv, hd) in dev_attn {
3616            let mut krow = attention::take_buf(nkv * hd);
3617            let mut vrow = attention::take_buf(nkv * hd);
3618            if crate::gpu::kv_mirror_read_last(kv_id, li, nkv, hd, &mut krow, &mut vrow) {
3619                let cache = &mut self.kv_cache.layers[li];
3620                cache.append(&krow, &vrow, &[]);
3621                let n = cache.seq_len;
3622                let mut imp = attention::take_buf(n);
3623                crate::gpu::kv_mirror_take_imp(kv_id, li, &mut imp);
3624                cache.accumulate_imp(&imp);
3625                attention::recycle_buf(&mut imp);
3626            }
3627            attention::recycle_buf(&mut krow);
3628            attention::recycle_buf(&mut vrow);
3629        }
3630        if let Some(profile) = pager_profile_page.as_mut() {
3631            profile.finish(end, graph.pager_page_stats(), "page-complete");
3632        }
3633        if let Some((_, arm)) = ab {
3634            crate::gpu_metal::dense_ab_record(arm, _mt0.elapsed());
3635        }
3636        end
3637    }
3638
3639    pub fn new(
3640        tokenizer: Tokenizer,
3641        weights: PipelineWeights,
3642        hidden_size: usize,
3643        intermediate_size: usize,
3644        num_heads: usize,
3645        num_kv_heads: usize,
3646        head_dim: usize,
3647        num_layers: usize,
3648        physical_layers: usize,
3649        loop_final_norm: bool,
3650        vocab_size: usize,
3651        rms_eps: f64,
3652        rope_base: f32,
3653        norm_style: NormStyle,
3654        max_seq_len: usize,
3655        sampler_config: SamplerConfig,
3656    ) -> Self {
3657        let rng = match sampler_config.seed {
3658            Some(s) => SplitMix64::new(s),
3659            None => SplitMix64::from_entropy(),
3660        };
3661        let inv_freq = std::sync::Arc::new(attention::rope_inv_freq(head_dim, rope_base));
3662        let pool = Pool::from_env();
3663        if let Some(p) = &pool {
3664            tracing::info!("worker pool: {} threads", p.n_workers());
3665            // Keep the workers on the socket that holds the weights.
3666            if let Some(model) = weights
3667                .lm_head
3668                .model_arc()
3669                .or_else(|| weights.embed_tokens.model_arc())
3670            {
3671                let regions: Vec<&[u8]> =
3672                    model.tensors.iter().map(|t| model.entry_bytes(t)).collect();
3673                p.bind_numa(&regions);
3674            }
3675        }
3676        Self {
3677            gpu_plan: None,
3678            tokenizer: std::sync::Arc::new(tokenizer),
3679            kv_cache: KvCache::new(num_layers, num_kv_heads, head_dim, max_seq_len),
3680            sampler_config,
3681            weights,
3682            hidden_size,
3683            intermediate_size,
3684            num_heads,
3685            num_kv_heads,
3686            head_dim,
3687            num_layers,
3688            physical_layers,
3689            loop_final_norm,
3690            vocab_size,
3691            rms_eps,
3692            rope_base,
3693            norm_style,
3694            rotary_dim: head_dim,
3695            attention_heads_per_layer: None,
3696            kv_heads_per_layer: None,
3697            v_head_dim: None,
3698            layer_dump: std::env::var_os("CMF_LAYER_DUMP")
3699                .filter(|v| !v.is_empty())
3700                .map(std::path::PathBuf::from),
3701            graph_declines: std::cell::RefCell::new(Vec::new()),
3702            mimo_moe: Default::default(),
3703            minicpm_tail: Default::default(),
3704            vmf_cfg: None,
3705            gdn_cfg: None,
3706            kda_cfg: None,
3707            g3n: None,
3708            g4e: None,
3709            dsv4: None,
3710            dsv41: None,
3711            dsv41_vision: None,
3712            dsv41_prefill: None,
3713            qwen4_exp: None,
3714            clover: None,
3715            clover_stats: clover::CloverStats::default(),
3716            dsv4_mtp: Vec::new(),
3717            dspark: None,
3718            dspark_pending: Vec::new(),
3719            dspark_hist: Vec::new(),
3720            dspark_real: Vec::new(),
3721            dspark_trunk_picks: Vec::new(),
3722            dspark_exp: Vec::new(),
3723            dspark_draft_ns: 0,
3724            m3_winners: Vec::new(),
3725            m3_counts: Vec::new(),
3726            m3_n_exp: 0,
3727            m3_tokens: 0,
3728            m3_prewarmed: false,
3729            logit_multiplier: None,
3730            cancel: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
3731            graph_failed: std::sync::atomic::AtomicBool::new(false),
3732            kv_history: Vec::new(),
3733            kv_history_device: false,
3734            short_conv_cfg: None,
3735            mtp: None,
3736            mimo_mtp: None,
3737            verify_exact_moe: false,
3738            speculative: std::env::var("CMF_MTP").map(|v| v != "0").unwrap_or(true),
3739            ignore_eos: false,
3740            draft_full_streak: 0,
3741            spec_k_adapt: None,
3742            spec_acc_ewma: 0.7,
3743            rng,
3744            sampler_scratch: SamplerScratch::default(),
3745            spec_forced: None,
3746            spec_q: Vec::new(),
3747            spec_p: Vec::new(),
3748            spec_res: Vec::new(),
3749            spec_qs: Vec::new(),
3750            spec_ps: Vec::new(),
3751            spec_ress: Vec::new(),
3752            mtp_graph_mode: None,
3753            #[cfg(target_os = "macos")]
3754            metal_verify: None,
3755            inv_freq,
3756            ws: ForwardScratch::new(hidden_size),
3757            pool,
3758            model: None,
3759            dyn_force_f32: false,
3760            dyn_skill_layers: Vec::new(),
3761            dyn_active: None,
3762            dyn_blend_loaded: false,
3763            dyn_phi_layer: None,
3764            dyn_phi_ema: Vec::new(),
3765            dyn_phi_seen: 0,
3766            dyn_router: None,
3767            o1_cfg: None,
3768            o1_epoch: 0,
3769            o1_flags: Vec::new(),
3770            trace: false,
3771            calib_temp: 1.0,
3772            confidence_on: true,
3773            embed_multiplier: 1.0,
3774            attn_scale: 1.0 / (head_dim as f32).sqrt(),
3775            swa: None,
3776            swa_trim: None,
3777            sliding_layers: None,
3778            no_rope_layers: None,
3779            anchor_core: None,
3780            bounded_rope: None,
3781            kv_prefix: KvPrefix::default(),
3782            last_prefill_tokens: 0,
3783            inv_freq_local: None,
3784            rotary_dim_local: None,
3785            rope_scale: 1.0,
3786            rope_scale_local: 1.0,
3787            global_attn: None,
3788            inv_freq_global: None,
3789            attn_v_norm: false,
3790            qk_norm_after_rope: false,
3791            proj_gate_sigmoid: false,
3792            final_softcap: None,
3793            head_clusters: None,
3794            attn_softcap: 0.0,
3795            graph_want_logits: false,
3796            graph_head_required: false,
3797            graph_logits: None,
3798            embryo_graph: None,
3799            graph_refused: std::sync::atomic::AtomicBool::new(false),
3800            graph_kv_id: {
3801                static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
3802                NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
3803            },
3804            #[cfg(test)]
3805            nll_test_fail_at: None,
3806            #[cfg(test)]
3807            nll_test_force_serial: false,
3808        }
3809    }
3810
3811    /// Enable/disable per-layer O(1) Nyström attention. Only Full
3812    /// layers are eligible (a linear layer keeps its own operator).
3813    /// Applies to generation (`generate*`/`forward_ids`): the prompt
3814    /// pass stays exact, then the state seals after prefill or at the
3815    /// deferred skeleton-safe boundary for short prompts; decode runs on
3816    /// the O(1) state. Teacher-forced scoring (`ppl_ids`) intentionally
3817    /// stays exact.
3818    pub fn set_o1(&mut self, cfg: Option<crate::nystrom::O1Cfg>) {
3819        if let Err(e) = self.try_set_o1(cfg) {
3820            tracing::error!("{e}");
3821        }
3822    }
3823
3824    /// True when the file carries a native bounded anchor
3825    /// (`arch.anchor_core`): its state is a fixed record the header
3826    /// fixes, and the post-hoc O(1) override is meaningless on it.
3827    pub fn bounded_native(&self) -> bool {
3828        self.anchor_core.is_some()
3829    }
3830
3831    /// Bytes the resident device graph holds for this pipeline's sequence:
3832    /// `(recurrent state, anchor KV/ring)`; None on the host path.
3833    pub fn device_state_bytes(&self) -> Option<(u64, u64)> {
3834        crate::gpu::embryo_device_state_bytes(self.graph_kv_id)
3835    }
3836
3837    /// Why an O(1) override is refused on this pipeline, if it is.
3838    pub fn o1_refusal(&self) -> Option<String> {
3839        self.anchor_core.as_ref().map(|ac| {
3840            format!(
3841                "--o1 / CMF_O1 refused: the anchor is native bounded \
3842                 (anchor_core kind={} window={} sink={}); the file's operator \
3843                 is executed as-is and no post-hoc Nyström overlay applies",
3844                ac.kind, ac.window, ac.sink
3845            )
3846        })
3847    }
3848
3849    /// `set_o1` that reports the refusal instead of logging it.
3850    pub fn try_set_o1(&mut self, cfg: Option<crate::nystrom::O1Cfg>) -> Result<(), String> {
3851        if let Some(c) = &cfg {
3852            if let Some(why) = self.o1_refusal() {
3853                self.o1_flags = Vec::new();
3854                self.o1_cfg = None;
3855                return Err(why);
3856            }
3857            if crate::nystrom::o1_deferred_boundary(c.w, c.sink).is_none() {
3858                self.o1_flags.clear();
3859                self.o1_cfg = None;
3860                return Err(format!(
3861                    "o1 disabled: w + sink + slack + 1 overflows usize (w={}, sink={})",
3862                    c.w, c.sink
3863                ));
3864            }
3865        }
3866        self.o1_flags = match &cfg {
3867            Some(c) => {
3868                let mut flags = c.layer_flags(self.num_layers);
3869                for (li, f) in flags.iter_mut().enumerate() {
3870                    // The Nyström state replaces a full-context plain
3871                    // softmax: a sliding window or a learned sink is not
3872                    // something it can represent, and a V narrower than
3873                    // the head is not what its streaming state stores.
3874                    // Those layers keep exact cache attention.
3875                    if *f
3876                        && (!matches!(
3877                            self.weights.layers[self.phys_layer(li)].attn,
3878                            AttnKind::Full { .. }
3879                        ) || self.layer_window(li).is_some()
3880                            || self.kv_cache.layers[li].sinks.is_some()
3881                            || self.layer_v_dim(li) != self.layer_geom(li).1)
3882                    {
3883                        *f = false;
3884                    }
3885                }
3886                flags
3887            }
3888            None => Vec::new(),
3889        };
3890        if let Some(c) = &cfg {
3891            let n = self.o1_flags.iter().filter(|&&f| f).count();
3892            tracing::info!(
3893                "o1 nystrom attention: {n}/{} layer(s), m={} w={} sink={} rect={:?}",
3894                self.num_layers,
3895                c.m,
3896                c.w,
3897                c.sink,
3898                c.rect
3899            );
3900        }
3901        self.o1_cfg = cfg;
3902        Ok(())
3903    }
3904
3905    /// Install the file's bounded anchor: one fixed-size ring per
3906    /// `AttnKind::Bounded` layer (from the header, not per prompt) and
3907    /// the shared relative-rotation table. Must run after the RoPE setup
3908    /// (`set_rotary`, YaRN) so the table is built from the final
3909    /// `inv_freq`.
3910    pub fn install_bounded(&mut self, cfg: &cortiq_core::AnchorCoreConfig) -> Result<(), String> {
3911        if !cortiq_core::AnchorCoreConfig::KINDS.contains(&cfg.kind.as_str()) {
3912            return Err(format!(
3913                "anchor_core kind '{}' is not executable by this runtime",
3914                cfg.kind
3915            ));
3916        }
3917        if cfg.window == 0 {
3918            return Err("anchor_core.window must be >= 1".into());
3919        }
3920        let mut n = 0usize;
3921        for li in 0..self.num_layers {
3922            let pl = self.phys_layer(li);
3923            if let AttnKind::Bounded(w) = &self.weights.layers[pl].attn {
3924                if w.window != cfg.window || w.sink != cfg.sink {
3925                    return Err(format!(
3926                        "layer {li}: bounded weights (window {} sink {}) disagree with \
3927                         anchor_core (window {} sink {})",
3928                        w.window, w.sink, cfg.window, cfg.sink
3929                    ));
3930                }
3931                self.kv_cache.layers[li].install_bounded(cfg.window);
3932                n += 1;
3933            }
3934        }
3935        if n == 0 {
3936            return Err("anchor_core is present but no layer executes it".into());
3937        }
3938        let rope = crate::bounded::BoundedRope::new(cfg.window, &self.inv_freq, self.rope_scale);
3939        self.bounded_rope = Some(std::sync::Arc::new(rope));
3940        self.anchor_core = Some(cfg.clone());
3941        self.embryo_graph = None;
3942        tracing::info!(
3943            "bounded anchor {}: {n} layer(s), window {} sink {} — {} B of ring per layer",
3944            cfg.kind,
3945            cfg.window,
3946            cfg.sink,
3947            self.kv_cache
3948                .layers
3949                .iter()
3950                .map(|l| l.bounded_state_bytes())
3951                .max()
3952                .unwrap_or(0)
3953        );
3954        Ok(())
3955    }
3956
3957    /// Tag every layer's cache with its wire record kind and the model's
3958    /// operator identity (hash64 of `linear_core_identity` JSON) so the
3959    /// versioned state wire refuses a peer holding another operator.
3960    pub fn install_wire_identity(&mut self, identity: u64) {
3961        for li in 0..self.kv_cache.layers.len() {
3962            let pl = self.phys_layer(li);
3963            let kind = match self.weights.layers.get(pl).map(|l| &l.attn) {
3964                Some(AttnKind::Bounded(_)) => crate::kv_cache::WireKind::Bounded,
3965                Some(AttnKind::Linear(_))
3966                | Some(AttnKind::LinearGdn(_))
3967                | Some(AttnKind::ShortConv(_))
3968                | Some(AttnKind::Kda(_)) => crate::kv_cache::WireKind::Linear,
3969                _ => crate::kv_cache::WireKind::Full,
3970            };
3971            let l = &mut self.kv_cache.layers[li];
3972            l.wire_kind = kind;
3973            l.wire_identity = identity;
3974            // A per-layer geometry (`set_attn_geometry`, Gemma's global
3975            // heads) rebuilds a layer cache with index 0: re-tag it.
3976            l.wire_layer = li as u32;
3977        }
3978    }
3979
3980    /// Forget the reuse keys (legacy `kv_history` and the bounded prefix).
3981    pub fn clear_history(&mut self) {
3982        self.kv_history.clear();
3983        self.kv_history_device = false;
3984        self.kv_prefix.clear();
3985    }
3986
3987    /// Did THIS pipeline's token graph refuse for a structural reason?
3988    /// (Per pipeline: another lane's refusal, or a new pipeline of the
3989    /// same model, never changes it.)
3990    pub fn graph_refused(&self) -> bool {
3991        self.graph_refused
3992            .load(std::sync::atomic::Ordering::Relaxed)
3993    }
3994
3995    /// Remember a structural refusal of this pipeline's token graph.
3996    pub fn mark_graph_refused(&self) {
3997        if !self
3998            .graph_refused
3999            .swap(true, std::sync::atomic::Ordering::Relaxed)
4000        {
4001            tracing::info!(
4002                "token graph: unsupported for this pipeline (seq {}) — not retrying",
4003                self.graph_kv_id
4004            );
4005        }
4006    }
4007
4008    /// Position the resident Embryo graph holds for this pipeline's
4009    /// sequence (`Some(next position)`), `None` when the device holds no
4010    /// image of it (host-owned sequence, or none started).
4011    pub fn device_sequence_position(&self) -> Option<usize> {
4012        crate::gpu::embryo_device_next_position(self.graph_kv_id)
4013    }
4014
4015    /// Is the sequence this pipeline's reuse key describes owned by the
4016    /// device path it would take now? A prefix recorded on the resident
4017    /// graph continues only there (at exactly `n`), a host prefix only on
4018    /// the host; any mismatch means the next turn re-prefills from zero.
4019    fn prefix_owner_matches(&self, n: usize, recorded_on_device: bool) -> bool {
4020        let dev = self.device_sequence_position();
4021        if recorded_on_device {
4022            dev == Some(n) && self.embryo_resident_wanted()
4023        } else {
4024            dev.is_none()
4025        }
4026    }
4027
4028    /// The weights under the sequence changed (a real skill switch): every
4029    /// cached state was computed by other weights. Clear the host KV /
4030    /// ring / recurrent state AND the reuse keys — a surviving `kv_prefix`
4031    /// would let the next turn "extend" a prefix the new weights never
4032    /// saw — drop the packed resident graph (it holds the old FFN
4033    /// tensors; the next build packs the live ones under a fresh id) and
4034    /// reset its device sequence.
4035    pub(crate) fn invalidate_for_weight_change(&mut self) {
4036        self.clear_sequence_state();
4037        self.embryo_graph = None;
4038    }
4039
4040    /// Prompt positions the cache already holds when `input_ids`
4041    /// strictly EXTENDS the consumed prefix (0 otherwise). Bounded-native
4042    /// models answer from the fixed-size `kv_prefix` record; everything
4043    /// else from the legacy `kv_history` vector (which the network split
4044    /// also reads and writes).
4045    fn cached_prefix_len(&self, input_ids: &[u32]) -> usize {
4046        let (n, on_device) = if self.bounded_native() {
4047            (
4048                self.kv_prefix.extension(input_ids),
4049                self.kv_prefix.on_device(),
4050            )
4051        } else {
4052            let h = &self.kv_history;
4053            if !h.is_empty() && h.len() < input_ids.len() && input_ids[..h.len()] == h[..] {
4054                (h.len(), self.kv_history_device)
4055            } else {
4056                (0, false)
4057            }
4058        };
4059        // The owner tag: the state the key describes must live where this
4060        // turn will continue it (R4) — else a fresh sequence.
4061        if n > 0 && !self.prefix_owner_matches(n, on_device) {
4062            tracing::warn!(
4063                "kv-reuse refused: the cached prefix ({n} positions) was built on the {} path, \
4064                 the device now holds {:?} — re-prefilling from zero",
4065                if on_device { "resident device" } else { "host" },
4066                self.device_sequence_position()
4067            );
4068            return 0;
4069        }
4070        n
4071    }
4072
4073    /// Public view of the prefix-reuse decision for `input_ids` (positions
4074    /// the next `generate*` would take from the cache; 0 = fresh sequence).
4075    pub fn reusable_prefix_len(&self, input_ids: &[u32]) -> usize {
4076        self.cached_prefix_len(input_ids)
4077    }
4078
4079    /// Record the forwarded prefix as the next turn's reuse key. A
4080    /// bounded-native model extends the rolling record (`reused` = the
4081    /// positions this turn found cached); others keep the literal vector.
4082    /// Either way the key carries its OWNER: the resident device graph
4083    /// (it holds an image of this sequence) or the host.
4084    fn record_consumed_prefix(&mut self, consumed: &[u32], reused: usize) {
4085        let on_device = self.device_sequence_position().is_some();
4086        if self.bounded_native() {
4087            let keep = reused > 0 && reused == self.kv_prefix.len() && reused <= consumed.len();
4088            let prev_device = self.kv_prefix.on_device();
4089            self.kv_history.clear();
4090            self.kv_history_device = false;
4091            if keep && prev_device == on_device {
4092                self.kv_prefix.extend(&consumed[reused..]);
4093            } else {
4094                self.kv_prefix.set(consumed);
4095            }
4096            self.kv_prefix.set_on_device(on_device);
4097        } else {
4098            self.kv_history = consumed.to_vec();
4099            self.kv_history_device = on_device;
4100        }
4101    }
4102
4103    /// True when at least one layer runs the O(1) kernel.
4104    pub fn o1_active(&self) -> bool {
4105        self.o1_cfg.is_some() && self.o1_flags.iter().any(|&f| f)
4106    }
4107
4108    /// Whether generation's prompt ingest is routed through the whole-token
4109    /// graph.  The bench uses this to label the measured generation prefill
4110    /// honestly; keep the predicate in Pipeline so CLI labels cannot drift
4111    /// from the production route.
4112    /// Positions per batched-graph submit for the prompt: `CMF_BATCH_K`
4113    /// when set (0 = one position at a time through the token graph),
4114    /// otherwise 128 (32 for a MoE stack) on a discrete card whose prompt
4115    /// takes the graph route.
4116    /// The batched graph read a 2048-token prompt at 53 tok/s against 28.5
4117    /// one position at a time on an RTX PRO 4000 (Qwen3.8-27B q4tp: TTFT
4118    /// 39 s against 72), and its states are the speculative verify's,
4119    /// measured identical to the plain path. macOS keeps its own arm.
4120    /// 128, not 32: the q4tp prefill GEMM tiles 64 batch rows, so a
4121    /// 32-row chunk left half of every tile idle. Qwen3.8-27B q4tp on an
4122    /// RTX 3090, 1000-token prompt: 51 tok/s at 32, 82 at 64, 95 at 128,
4123    /// 97 at 256; greedy output byte-identical to the 32-row chunks
4124    /// (every GEMM row and the k-looped recurrences are per position).
4125    pub fn generation_batch_k(&self) -> usize {
4126        if let Some(k) = std::env::var("CMF_BATCH_K")
4127            .ok()
4128            .and_then(|v| v.parse::<usize>().ok())
4129        {
4130            return k;
4131        }
4132        // Gemma-4 (`g4_batch_prefill`): 128-row chunks, the GEMM tile's
4133        // two 64-row halves full.
4134        if self.g4_batch_prefill() {
4135            return 128;
4136        }
4137        #[cfg(not(target_os = "macos"))]
4138        if self.graph_prefill_preferred() && !self.o1_active() {
4139            // MoE stacks keep the 32 their expert-grouped prefill was
4140            // measured at; 128 is measured on the dense hybrids only.
4141            let moe = self
4142                .weights
4143                .layers
4144                .iter()
4145                .any(|lw| matches!(&lw.ffn, FfnKind::Moe(_)));
4146            // q8_2f experts (moe_q82's grouped prefill) read each expert
4147            // once per group of up to 16 of its entries, so the chunk that
4148            // fills those groups is the faster ingest: Mellum2.1 q8_2f on an
4149            // RTX 3090, 4000-token prompt, 503 tok/s at 64 rows, 542 at 128
4150            // (and 542 at 256).
4151            let q82 = crate::gpu::moe_q82_graph_on()
4152                && self.weights.layers.iter().any(|lw| {
4153                    matches!(&lw.ffn, FfnKind::Moe(m)
4154                        if m.experts.first().is_some_and(|e| e.gate_proj.mapped_q8_2f().is_some()))
4155                });
4156            return if q82 {
4157                128
4158            } else if moe {
4159                32
4160            } else {
4161                128
4162            };
4163        }
4164        0
4165    }
4166
4167    pub fn generation_graph_prefill(&self) -> bool {
4168        let graph = self.graph_prefill_preferred();
4169        // On wgpu, an active MTP head now consumes the trunk's graph batches
4170        // and warms its own block from those returned rows.  The selected
4171        // generation measurement is therefore the batched path, even though
4172        // the underlying GDN model still satisfies the graph-prefill
4173        // predicate.  Keep the CLI label tied to the actual route.  Native
4174        // Metal has a separate prefill-batch arm and retains its historical
4175        // label here.
4176        // A batched prompt (`generation_batch_k` > 0) is the batched graph
4177        // for every model on the graph route, not only those with an MTP
4178        // head — the label follows the route.
4179        #[cfg(not(target_os = "macos"))]
4180        if graph && self.generation_batch_k() > 0 && std::env::var("CMF_MTP_CHAIN_PROBE").is_err() {
4181            return false;
4182        }
4183        graph
4184    }
4185
4186    /// Device-side O(1) mirrors currently uploaded for this pipeline's
4187    /// sequence.  The count/bytes are zero before seal or after a fresh
4188    /// reset; callers use this to distinguish logical host state from the
4189    /// GPU allocation that actually serves decode.
4190    pub fn o1_device_stats(&self) -> (usize, u64) {
4191        crate::gpu::o1_device_stats(self.graph_kv_id)
4192    }
4193
4194    /// Arm query collection on the o1 layers (fresh prompt pass).
4195    /// Reset the o1 layers to Collecting for a fresh sequence. Pub for the
4196    /// network split: each side runs the o1 lifecycle over ITS OWN layers
4197    /// (begin before prefill, seal at the prefill barrier).
4198    pub fn o1_begin(&mut self) {
4199        self.o1_begin_with_prefix(None);
4200    }
4201
4202    /// Arm collection and optionally request a positive calibration prefix.
4203    /// The effective barrier is always at least the skeleton-safe floor, so
4204    /// a short requested prefix cannot create an exact-only runtime state.
4205    pub fn o1_begin_with_prefix(&mut self, requested_prefix: Option<usize>) {
4206        if let Some(c) = &self.o1_cfg {
4207            let (m, w, sink, rect) = (c.m, c.w, c.sink, c.rect);
4208            let boundary = requested_prefix.map(|p| {
4209                p.max(
4210                    crate::nystrom::o1_deferred_boundary(w, sink)
4211                        .expect("o1 config boundary validated in set_o1"),
4212                )
4213            });
4214            for (li, &f) in self.o1_flags.iter().enumerate() {
4215                if f {
4216                    self.kv_cache.layers[li].o1_begin_with_boundary(m, w, sink, rect, boundary);
4217                }
4218            }
4219        }
4220    }
4221
4222    /// Effective deferred boundary for a positive prefix request.
4223    fn o1_effective_boundary(&self, requested_prefix: usize) -> Option<usize> {
4224        self.o1_cfg.as_ref().and_then(|c| {
4225            crate::nystrom::o1_deferred_boundary(c.w, c.sink)
4226                .map(|floor| requested_prefix.max(floor))
4227        })
4228    }
4229
4230    fn o1_note_transition(&mut self) {
4231        // Drain every layer's one-shot bit before publishing one pipeline
4232        // epoch. `any()` would short-circuit on the first layer and leak the
4233        // remaining bits into later forwards, causing one epoch per layer.
4234        let mut transitioned = false;
4235        for (li, &flagged) in self.o1_flags.iter().enumerate() {
4236            if flagged {
4237                transitioned |= self.kv_cache.layers[li].take_o1_transition();
4238            }
4239        }
4240        if transitioned {
4241            self.o1_epoch = self.o1_epoch.wrapping_add(1);
4242        }
4243    }
4244
4245    fn o1_pending(&self) -> bool {
4246        self.o1_flags.iter().enumerate().any(|(li, &f)| {
4247            f && self.kv_cache.layers[li].seq_len > 0
4248                && self.kv_cache.layers[li].o1_pending_boundary().is_some()
4249        })
4250    }
4251
4252    fn o1_fail(&mut self, err: String) {
4253        tracing::error!("o1 deferred seal failed; terminating sequence: {err}");
4254        self.clear_sequence_state();
4255        self.graph_failed
4256            .store(true, std::sync::atomic::Ordering::Relaxed);
4257        self.cancel
4258            .store(true, std::sync::atomic::Ordering::Relaxed);
4259    }
4260
4261    /// Seal participating layers while retaining the exact state when the
4262    /// prompt is below the deferred boundary. A split worker may have
4263    /// collecting layers outside its owned span; zero-depth layers remain
4264    /// armed and are intentionally skipped until their peer runs them.
4265    pub fn o1_seal_checked(&mut self) -> Result<bool, String> {
4266        if self.o1_cfg.is_none() {
4267            return Ok(false);
4268        }
4269        let mut participating = false;
4270        for li in 0..self.num_layers {
4271            if !self.o1_flags.get(li).copied().unwrap_or(false) {
4272                continue;
4273            }
4274            if let Some(err) = self.kv_cache.layers[li].take_o1_error() {
4275                return Err(err);
4276            }
4277            if self.kv_cache.layers[li].seq_len == 0 {
4278                continue;
4279            }
4280            participating = true;
4281            let num_heads = self.layer_num_heads(li);
4282            self.kv_cache.layers[li].o1_seal_checked(num_heads)?;
4283        }
4284        self.o1_note_transition();
4285        for li in 0..self.num_layers {
4286            if self.o1_flags.get(li).copied().unwrap_or(false) {
4287                if let Some(err) = self.kv_cache.layers[li].take_o1_error() {
4288                    return Err(err);
4289                }
4290            }
4291        }
4292        Ok(participating
4293            && (0..self.num_layers).all(|li| {
4294                !self.o1_flags.get(li).copied().unwrap_or(false)
4295                    || self.kv_cache.layers[li].seq_len == 0
4296                    || self.kv_cache.layers[li].o1_sealed()
4297            }))
4298    }
4299
4300    /// Complete a deferred boundary after a full position/span forward.
4301    /// This is the pipeline owner for epoch publication and failure cleanup.
4302    fn o1_progress(&mut self) {
4303        if !self.o1_active() {
4304            return;
4305        }
4306        for li in 0..self.num_layers {
4307            if self.o1_flags.get(li).copied().unwrap_or(false) {
4308                if let Some(err) = self.kv_cache.layers[li].take_o1_error() {
4309                    self.o1_fail(err);
4310                    return;
4311                }
4312            }
4313        }
4314        // A qwen_attention row can seal in the middle of a complete layer
4315        // walk. Consume its transition even though the pending boundary has
4316        // already disappeared from the cache.
4317        self.o1_note_transition();
4318        if !self.o1_pending() {
4319            return;
4320        }
4321        if let Err(err) = self.o1_seal_checked() {
4322            self.o1_fail(err);
4323        }
4324    }
4325
4326    /// Turn a deferred O(1) failure raised by a hidden-only forward into the
4327    /// Result error its public batch/span caller must return. The failure
4328    /// path already cleared host/device sequence state; consume only the
4329    /// side-channel marker here and leave the pipeline reusable.
4330    fn check_o1_progress_failure(&mut self, phase: &str) -> Result<(), String> {
4331        if self
4332            .graph_failed
4333            .swap(false, std::sync::atomic::Ordering::Relaxed)
4334        {
4335            self.cancel
4336                .store(false, std::sync::atomic::Ordering::Relaxed);
4337            self.clear_sequence_state();
4338            return Err(format!("{phase}: deferred O(1) transition failed"));
4339        }
4340        Ok(())
4341    }
4342
4343    /// Freeze landmarks + skeleton state after the prompt pass and drop
4344    /// the o1 layers' full KV; decode then runs `step()` per token.
4345    /// Pub for the network split (see `o1_begin`).
4346    pub fn o1_seal(&mut self) {
4347        if let Err(err) = self.o1_seal_checked() {
4348            self.o1_fail(err);
4349        }
4350    }
4351
4352    /// Enable/disable the structured per-token telemetry trace (B4).
4353    pub fn set_trace(&mut self, on: bool) {
4354        self.trace = on;
4355    }
4356
4357    /// Replace all request-scoped sampler options and reset the random stream.
4358    /// This is required for deterministic `seed` semantics in pooled servers.
4359    pub fn set_sampler_config(&mut self, config: SamplerConfig) {
4360        self.rng = match config.seed {
4361            Some(seed) => SplitMix64::new(seed),
4362            None => SplitMix64::from_entropy(),
4363        };
4364        self.sampler_config = config;
4365    }
4366
4367    /// Toggle the per-token confidence reduction (a full-vocab
4368    /// softmax each token). `bench --core` turns it off so the timed
4369    /// loop matches llama-bench's core contract; the result's
4370    /// `confidence` vec is empty while off.
4371    pub fn set_confidence(&mut self, on: bool) {
4372        self.confidence_on = on;
4373    }
4374
4375    /// Set the confidence-calibration temperature (B1). Values ≤0 are
4376    /// clamped to raw (1.0).
4377    pub fn set_calib_temp(&mut self, t: f32) {
4378        self.calib_temp = if t > 1e-3 { t } else { 1.0 };
4379    }
4380
4381    /// The active calibration temperature (1.0 = raw probability).
4382    pub fn calib_temp(&self) -> f32 {
4383        self.calib_temp
4384    }
4385
4386    /// The primary (full-attention) RoPE inverse-frequency table selected
4387    /// from the CMF architecture.  This is read-only diagnostic state for
4388    /// parity checks; local sliding layers may use `inv_freq_local` instead.
4389    pub fn rope_inv_freq(&self) -> &[f32] {
4390        self.inv_freq.as_slice()
4391    }
4392
4393    /// Partial rotary (Qwen3.5): rotate only the first `rotary_dim` dims;
4394    /// the frequency table is rebuilt over the rotary dims.
4395    pub fn set_rotary(&mut self, rotary_dim: usize, base: f32) {
4396        self.rotary_dim = rotary_dim.min(self.head_dim);
4397        self.inv_freq = std::sync::Arc::new(attention::rope_inv_freq(self.rotary_dim, base));
4398        // The packed resident graph owns its own inverse-frequency plane;
4399        // changing RoPE after it was built must not leave a stale device
4400        // model behind the exact host configuration.
4401        self.embryo_graph = None;
4402    }
4403
4404    fn attn_cfg(&self, position: usize) -> QwenAttnCfg<'_> {
4405        QwenAttnCfg {
4406            num_heads: self.num_heads,
4407            num_kv_heads: self.num_kv_heads,
4408            head_dim: self.head_dim,
4409            hidden_size: self.hidden_size,
4410            position,
4411            inv_freq: &self.inv_freq,
4412            rotary_dim: self.rotary_dim,
4413            scale: self.attn_scale,
4414            softcap: self.attn_softcap,
4415            window: None,
4416            v_norm: false,
4417            qk_norm_after_rope: self.qk_norm_after_rope,
4418            gate_sigmoid: self.proj_gate_sigmoid,
4419            q_norm: None,
4420            k_norm: None,
4421            output_gate: false,
4422            softplus_gate: None,
4423            rope_scale: self.rope_scale,
4424            bias: None,
4425            rms_eps: self.rms_eps,
4426            norm_style: self.norm_style,
4427            pool: self.pool.as_deref(),
4428            v_head_dim: self.v_head_dim.unwrap_or(self.head_dim),
4429        }
4430    }
4431
4432    /// Generate text from a plain-text prompt. Streams tokens via `on_token`.
4433    pub fn generate(
4434        &mut self,
4435        prompt: &str,
4436        max_tokens: usize,
4437        task_mask: Option<&TaskMask>,
4438        on_token: Option<TokenCallback>,
4439    ) -> Result<GenerateResult, String> {
4440        let input_ids = self.tokenizer.with_bos(self.tokenizer.encode(prompt));
4441        self.generate_from_ids(&input_ids, max_tokens, task_mask, on_token)
4442    }
4443
4444    /// Generate from a V4.1 multimodal prompt prepared by the vision module.
4445    /// Vision rows are encoded once and fed through the same bounded token walk as text.
4446    pub fn generate_from_vl(
4447        &mut self,
4448        input: &crate::dsv41_vision::PreparedVlInputs,
4449        max_tokens: usize,
4450        task_mask: Option<&TaskMask>,
4451        on_token: Option<TokenCallback>,
4452    ) -> Result<GenerateResult, String> {
4453        let Some(dsv41) = &self.dsv41 else {
4454            return Err("V4.1 multimodal input requires a DeepSeek-V4.1 pipeline".into());
4455        };
4456        if input.token_ids.is_empty() {
4457            return Err("empty V4.1 multimodal prompt".into());
4458        }
4459        if input.token_types.len() != input.token_ids.len() {
4460            return Err(format!(
4461                "V4.1 token type count {} != token count {}",
4462                input.token_types.len(),
4463                input.token_ids.len()
4464            ));
4465        }
4466        let dim = dsv41.2.dim;
4467        let mut embeddings = vec![None; input.token_ids.len()];
4468        let mut participates = vec![true; input.token_ids.len()];
4469        if !input.images.is_empty() {
4470            let vision = self
4471                .dsv41_vision
4472                .as_ref()
4473                .ok_or_else(|| "V4.1 image prompt has no loaded vision tower".to_string())?;
4474            for image in &input.images {
4475                let end = image.start.saturating_add(image.types.len());
4476                if end > input.token_ids.len() {
4477                    return Err(format!(
4478                        "V4.1 image span {}..{} exceeds prompt length {}",
4479                        image.start,
4480                        end,
4481                        input.token_ids.len()
4482                    ));
4483                }
4484                let mut span = vec![0.0f32; image.types.len() * dim];
4485                vision.fill_image_span(image, &mut span, self.pool.as_deref())?;
4486                for (offset, &kind) in image.types.iter().enumerate() {
4487                    let pos = image.start + offset;
4488                    if input.token_types[pos] != kind {
4489                        return Err(format!(
4490                            "V4.1 image type mismatch at position {pos}: {} != {kind}",
4491                            input.token_types[pos]
4492                        ));
4493                    }
4494                    embeddings[pos] = Some(span[offset * dim..(offset + 1) * dim].to_vec());
4495                    participates[pos] = false;
4496                }
4497            }
4498        }
4499        for (pos, &kind) in input.token_types.iter().enumerate() {
4500            if kind == crate::dsv41_vision::TEXT && embeddings[pos].is_some() {
4501                return Err(format!("V4.1 text position {pos} has an image embedding"));
4502            }
4503            if kind != crate::dsv41_vision::TEXT && embeddings[pos].is_none() {
4504                return Err(format!("V4.1 image position {pos} has no image embedding"));
4505            }
4506        }
4507        self.dsv41_prefill = Some((embeddings, participates));
4508        let result = self.generate_from_ids(&input.token_ids, max_tokens, task_mask, on_token);
4509        self.dsv41_prefill = None;
4510        result
4511    }
4512
4513    /// `None` when the mask forbids nothing (see `TaskMask::fully_open`).
4514    fn drop_open_mask<'m>(&self, m: Option<&'m TaskMask>) -> Option<&'m TaskMask> {
4515        m.filter(|m| !m.fully_open(self.intermediate_size, self.num_heads))
4516    }
4517
4518    /// Generate from prepared token ids (e.g. a chat template).
4519    ///
4520    /// With an MTP head, greedy generation without a task mask takes the
4521    /// speculative path: the MTP module drafts the token after next and
4522    /// the main model verifies both in one fused two-position forward
4523    /// (weights streamed once). The output is EXACTLY the vanilla greedy
4524    /// sequence — a rejected draft is rolled back — MTP only buys speed.
4525    pub fn generate_from_ids(
4526        &mut self,
4527        input_ids: &[u32],
4528        max_tokens: usize,
4529        task_mask: Option<&TaskMask>,
4530        on_token: Option<TokenCallback>,
4531    ) -> Result<GenerateResult, String> {
4532        self.generate_with_prompt_rows(input_ids, None, max_tokens, task_mask, on_token)
4533    }
4534
4535    /// Generate from complete prompt embeddings [token_count, hidden_size].
4536    /// Text rows can be obtained with `embed_id`; media rows replace only
4537    /// their expanded placeholder positions. Rows are already scaled and
4538    /// enter `PrefillIn::Hidden`, so a device graph must not re-embed them.
4539    /// Token-only KV reuse is disabled both into and out of this request.
4540    pub fn generate_from_embeds(
4541        &mut self,
4542        input_ids: &[u32],
4543        prompt_rows: &[f32],
4544        max_tokens: usize,
4545        task_mask: Option<&TaskMask>,
4546        on_token: Option<TokenCallback>,
4547    ) -> Result<GenerateResult, String> {
4548        if input_ids.is_empty()
4549            || input_ids.len().checked_mul(self.hidden_size) != Some(prompt_rows.len())
4550        {
4551            return Err("embedded prompt dimensions must be [tokens, hidden_size]".into());
4552        }
4553        if prompt_rows.iter().any(|x| !x.is_finite()) {
4554            return Err("embedded prompt contains non-finite values".into());
4555        }
4556        if !self.can_prefill_batched()
4557            || self.dyn_router.is_some()
4558            || self.o1_active()
4559            || self.mtp.is_some()
4560            || self.gpu_plan.is_some()
4561        {
4562            return Err("embedded prompts require the ordinary transformer path without O(1), dynamic routing, GPU splitting or a generic MTP head".into());
4563        }
4564        self.generate_with_prompt_rows(
4565            input_ids,
4566            Some(prompt_rows),
4567            max_tokens,
4568            task_mask,
4569            on_token,
4570        )
4571    }
4572
4573    fn generate_with_prompt_rows(
4574        &mut self,
4575        input_ids: &[u32],
4576        prompt_rows: Option<&[f32]>,
4577        max_tokens: usize,
4578        task_mask: Option<&TaskMask>,
4579        mut on_token: Option<TokenCallback>,
4580    ) -> Result<GenerateResult, String> {
4581        #[cfg(target_os = "macos")]
4582        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
4583        if std::env::var("CMF_TRACE_H").is_ok() {
4584            eprintln!("input_ids: {input_ids:?}");
4585        }
4586        if input_ids.is_empty() {
4587            return Err("empty prompt: nothing to generate from".to_string());
4588        }
4589        // A prior graph failure is terminal for that sequence but must not
4590        // poison the next independent request.  Keep this flag separate from
4591        // the externally-owned cooperative cancel bit.
4592        self.graph_failed
4593            .store(false, std::sync::atomic::Ordering::Relaxed);
4594        // A mask that forbids nothing still costs every fused path and
4595        // whole-token graph, all of which are gated on `is_none()`. A
4596        // narrowed file whose one segment is always on carries exactly
4597        // such a mask — drop it here rather than pay 5x for a no-op.
4598        let task_mask = self.drop_open_mask(task_mask);
4599        // A block-diffusion model generates whole blocks by denoising
4600        // previews, never one causal token at a time.
4601        if self.clover.is_some() {
4602            if prompt_rows.is_some() || task_mask.is_some() {
4603                return Err("block-diffusion generation takes token prompts without task masks".into());
4604            }
4605            return self.clover_generate(input_ids, max_tokens, on_token);
4606        }
4607
4608        // Cross-turn KV reuse: a chat app resends the whole history
4609        // every turn; when the new ids strictly EXTEND what the cache
4610        // already holds, prefill only the tail — turn latency stays
4611        // proportional to the new text instead of the whole session.
4612        // Extension-only (no rollback), so it is exact for every layer
4613        // kind including recurrent state; MTP/o1/task-mask runs keep
4614        // the fresh-sequence path. CMF_KV_REUSE=0 disables.
4615        let mut reuse_from = {
4616            let on = !std::env::var("CMF_KV_REUSE").is_ok_and(|v| v == "0");
4617            if on
4618                && prompt_rows.is_none()
4619                && task_mask.is_none()
4620                && self.mtp.is_none()
4621                && !(self.mimo_mtp.is_some() && self.speculative)
4622                && self.o1_cfg.is_none()
4623                && self.dsv41.is_none()
4624            {
4625                self.cached_prefix_len(input_ids)
4626            } else {
4627                0
4628            }
4629        };
4630        // The device may own rows the host tail prefill needs (wgpu decode
4631        // writes only its mirror): hand them to the host, or start fresh.
4632        if reuse_from > 0 && !self.prepare_kv_reuse(reuse_from) {
4633            reuse_from = 0;
4634        }
4635        self.last_prefill_tokens = input_ids.len() - reuse_from;
4636        let bounded_native = self.bounded_native();
4637        if reuse_from == 0 {
4638            // Fresh sequence — the cache holds absolute positions.
4639            // MTP never participates in cross-turn KV reuse, so a
4640            // high-vocabulary token from the previous request must not keep
4641            // this request on its expensive full draft head.
4642            self.reset_fresh_mtp_draft_state();
4643            self.clear_sequence_state();
4644        } else if std::env::var("CMF_PREFILL_PROF").is_ok() {
4645            eprintln!(
4646                "kv-reuse: {} of {} prompt positions already cached",
4647                reuse_from,
4648                input_ids.len()
4649            );
4650        }
4651        crate::gpu::graph_race_begin_generation();
4652        // Optional bounded calibration prefix. Keep the requested value
4653        // even when it is longer than the prompt; the collecting layer will
4654        // defer at the effective boundary and remain exact for short input.
4655        let o1_prefill = if self.o1_active() && task_mask.is_none() {
4656            std::env::var("CMF_O1_PREFILL")
4657                .ok()
4658                .and_then(|v| v.parse::<usize>().ok())
4659                .filter(|&p| p > 0)
4660        } else {
4661            None
4662        };
4663        if task_mask.is_none() {
4664            self.o1_begin_with_prefix(o1_prefill);
4665        }
4666
4667        // Speculative decode is off under o1: a rejected draft can't be
4668        // rolled back out of the far accumulators / ring window (the
4669        // Nyström insertion is irreversible by design).
4670        // The wgpu token graph owns a device K/V mirror that speculative
4671        // rollback would desync — the two are mutually exclusive.
4672        let graph_on = crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode);
4673        // Graph speculative decode (`CMF_GRAPH_SPEC=1`): the MTP head
4674        // drafts, ONE batched graph submit verifies the whole chain.
4675        //
4676        // It now PAYS on Qwen3.6-27B / RTX 5090 — 51.1 tok/s against a
4677        // plain 49.4 at k=3, medians of three, 89% of drafts accepted,
4678        // and the greedy continuation is byte-identical to the plain
4679        // path. That took the batch matvec sharing its nibble unpack
4680        // across the batch (`CMF_MV_BK=2`); before it, the same round
4681        // measured 43.6, an 11% LOSS, which is what the earlier note
4682        // here described.
4683        //
4684        // Still opt-in. One model's win is not a default: the verify
4685        // rides `gdn_spec_restore` and a batched frame whose numerics
4686        // are the batch kernels', and that has to be shown on more than
4687        // one architecture before every greedy decode takes it.
4688        // Greedy (with or without penalties) verifies by argmax equality.
4689        // Sampling (temperature > 0) can go through speculative SAMPLING —
4690        // draft from the MTP head's own post-chain distribution, accept
4691        // with min(1, p/q), correct from max(0, p − q); the emitted stream
4692        // is distributed exactly as the plain sampler's — but it is
4693        // OPT-IN (`CMF_GRAPH_SPEC_SAMPLE=1`): measured on Qwen3.8-27B /
4694        // RTX 5090 at the instruct row (0.7 / 0.80 / 20 / presence 1.5)
4695        // it decoded 19-22 tok/s against a plain 40 — nine post-chain
4696        // distributions a round plus a lower acceptance than greedy's,
4697        // against a verify that costs 2.7 single tokens. The greedy arms
4698        // pay +10%; the sampling arm needs a cheaper verify first.
4699        // Native Metal has a fast row-batch verify, but it is NOT admitted
4700        // by default: a real-model exactness check found an emitted greedy
4701        // divergence from the serial path.  Every native-Metal speculative
4702        // arm therefore requires the explicit `CMF_GRAPH_SPEC=1` experiment
4703        // gate until the batch kernels are numerically qualified.  If it is
4704        // explicitly enabled, its sampling arm is useful only for a config
4705        // the SPARSE chain serves (a top-k within `sparse_ok`): without it a
4706        // round builds nine 248k-float distributions on the host, which is
4707        // the 5090's measured loss and not a cost the round-token proxy
4708        // below can see. A top-k-less sampling config keeps the plain path
4709        // unless asked for by name.
4710        #[cfg(target_os = "macos")]
4711        let metal_graph = crate::gpu::q1_force()
4712            && crate::gpu::enabled_here()
4713            && std::env::var("CMF_GPU_BLOCK")
4714                .map(|v| v != "0")
4715                .unwrap_or(true);
4716        // `CMF_GPU_BLOCK=0` declines the ordinary native block route but
4717        // does not change the backend identity: a separately requested wgpu
4718        // graph can still reach `graph_spec_step`, whose native branch uses
4719        // VerifyGraph. Keep the quality policy tied to that identity, not to
4720        // the particular block-route admission above.
4721        #[cfg(target_os = "macos")]
4722        let native_metal_backend = crate::gpu::q1_force();
4723        #[cfg(not(target_os = "macos"))]
4724        let metal_graph = false;
4725        #[cfg(not(target_os = "macos"))]
4726        let native_metal_backend = false;
4727        let spec_sample_env = std::env::var("CMF_GRAPH_SPEC_SAMPLE").ok();
4728        // A round whose cost is the MEASURED one: greedy (argmax rows), or
4729        // sampling through the sparse chain. Anything else pays the dense
4730        // chain's host time, which no proxy can price.
4731        let spec_cheap_round =
4732            self.sampler_config.temperature < 1e-6 || sampler::sparse_ok(&self.sampler_config);
4733        let spec_sampling_ok = self.sampler_config.temperature < 1e-6
4734            || match spec_sample_env.as_deref() {
4735                Some("1") => true,
4736                Some(_) => false,
4737                None => metal_graph && spec_cheap_round,
4738            };
4739        // ON by default for greedy on the wgpu graph: with the draft on
4740        // the graph and the verify bit-exact, it measured 58.7 tok/s
4741        // against a plain 48.1 on Qwen3.8-27B q4tp / RTX 5090 (k=4) and
4742        // 51.1 against 49.4 on Qwen3.6-27B, and a round that stops
4743        // paying turns itself off below (acceptance watchdog).
4744        // `CMF_GRAPH_SPEC=0` disables; `=1` is the explicit opt-in spelling.
4745        // …but only where the batched verify has its register-blocked
4746        // kernel: q4tp dense FFNs (graph kind 6). q4t and q8_2f verify
4747        // through tile GEMMs today and measured a LOSS (q8_2f 22 against
4748        // 29 tok/s), the 2-bit plane the same; those stay opt-in
4749        // (`CMF_GRAPH_SPEC=1`).
4750        // …at least in nine dense FFNs of ten: a healed file carries its
4751        // last two layers at q8_2f, and two tile-GEMM verifies among 64 do
4752        // not change the arithmetic (measured: the healed q4tp file
4753        // decodes at the plain file's rate and would otherwise sit out).
4754        let (mut dense_n, mut dense_q4tp) = (0usize, 0usize);
4755        for lw in &self.weights.layers {
4756            if let FfnKind::Dense(d) = &lw.ffn {
4757                dense_n += 1;
4758                if matches!(d.gate_proj.graph_weight(), Some((_, _, 6, _)))
4759                    && matches!(d.up_proj.graph_weight(), Some((_, _, 6, _)))
4760                    && matches!(d.down_proj.graph_weight(), Some((_, _, 6, _)))
4761                {
4762                    dense_q4tp += 1;
4763                }
4764            }
4765        }
4766        let spec_default_ok = dense_n == 0 || dense_q4tp * 10 >= dense_n * 9;
4767        // Penalties break the draft head's agreement with the trunk (a
4768        // 1.1 repetition penalty measured 2 of 16 accepted): not by
4769        // default there either — off Metal that rule is untouched, and
4770        // suppressed ids keep counting as a penalty there, because no
4771        // measurement on a discrete card says otherwise.
4772        //
4773        // On native Metal the penalized arms DO pay: the draft applies
4774        // the same penalty and the verify scores the penalized rows
4775        // exactly (`greedy_pen`, the plain loop's arithmetic), so the
4776        // text is the plain path's and only the round's shape changes.
4777        // Measured on this M4 — see the report for the interleaved run.
4778        let penalized = !metal_graph
4779            && (self.sampler_config.repetition_penalty != 1.0
4780                || self.sampler_config.presence_penalty != 0.0
4781                || !self.sampler_config.suppress_tokens.is_empty());
4782        // …and not on wgpu-over-Metal: the batched verify graph there
4783        // returned 0 accepted drafts and garbage text on a GDN hybrid
4784        // (16.08, Qwen3.5-0.8B) while Vulkan is bit-exact; the Mac's
4785        // default backend is native Metal without a batch graph anyway.
4786        #[cfg(feature = "gpu")]
4787        let metal_wgpu = graph_on && crate::gpu_wgpu::wgpu_backend_is_metal();
4788        #[cfg(not(feature = "gpu"))]
4789        let metal_wgpu = false;
4790        let spec_env = std::env::var("CMF_GRAPH_SPEC").ok();
4791        let spec_wanted = graph_spec_wanted_by_policy(
4792            spec_env.as_deref(),
4793            spec_default_ok,
4794            penalized,
4795            metal_wgpu,
4796            native_metal_backend,
4797        );
4798        if spec_wanted && metal_wgpu {
4799            tracing::warn!(
4800                "CMF_GRAPH_SPEC forced on wgpu/Metal: the batched verify graph is not \
4801                 verified on this backend (garbage measured on Qwen3.5-0.8B)"
4802            );
4803        }
4804        // Native Metal: the b-row verify graph (`try_batch_graph_metal`)
4805        // stands where the wgpu batch graph stands on discrete cards
4806        // (`metal_graph`, above).
4807        let graph_spec = self.speculative
4808            && (graph_on || metal_graph)
4809            && self.mtp.is_some()
4810            && task_mask.is_none()
4811            && !self.o1_active()
4812            && spec_sampling_ok
4813            && spec_wanted;
4814        // The host fused pair is an optimization, not a verifier. On real
4815        // q4tp Qwen3.5 a near-tied row produced a different greedy argmax
4816        // from two serial target forwards. That makes a seemingly accepted
4817        // draft change text, which is worse than losing speculative speed.
4818        // Keep it explicitly experimental until a model/backend has passed
4819        // a long exact-ID gate; the native Metal batch verifier has its own
4820        // separate graph admission and is not controlled by this switch.
4821        //
4822        // GDN hybrids are also excluded by default: their recurrence is
4823        // sequential, so the pair lane cannot parallelize and measured 2x
4824        // slower on the 35B. `CMF_MTP_PAIR=1` is deliberately the only
4825        // opt-in for the host fused-pair experiment (not `CMF_MTP=1`).
4826        let host_pair_opt_in = std::env::var("CMF_MTP_PAIR").as_deref() == Ok("1");
4827        let pair_pays = host_pair_opt_in
4828            && (self.gdn_cfg.is_none() || std::env::var("CMF_MTP").as_deref() == Ok("1"));
4829        // Native Metal: say the route ONCE (RUST_LOG=info), including the
4830        // deliberate quality gate that keeps the unqualified batch verifier
4831        // off unless the user explicitly requested the experiment.
4832        #[cfg(target_os = "macos")]
4833        if metal_graph {
4834            static SAID: std::sync::Once = std::sync::Once::new();
4835            SAID.call_once(|| {
4836                let spec = if graph_spec {
4837                    let k = std::env::var("CMF_GRAPH_SPEC_K")
4838                        .ok()
4839                        .and_then(|v| v.parse::<usize>().ok())
4840                        .filter(|&v| (1..=8).contains(&v))
4841                        .unwrap_or(7);
4842                    let arm = if self.sampler_config.temperature < 1e-6 {
4843                        "greedy"
4844                    } else {
4845                        "sampling"
4846                    };
4847                    format!(
4848                        "spec k={k} {arm} (batched verify, draft shortlist {}, trial: proxy)",
4849                        Self::draft_vocab_rows(usize::MAX)
4850                    )
4851                } else if !self.speculative {
4852                    "spec off (CMF_MTP=0)".to_string()
4853                } else if self.mtp.is_none() {
4854                    "spec off (no MTP head)".to_string()
4855                } else if !spec_sampling_ok {
4856                    if spec_cheap_round {
4857                        "spec off (CMF_GRAPH_SPEC_SAMPLE=0)".to_string()
4858                    } else {
4859                        "spec off (sampling without a top-k: the dense chain \
4860                         costs more than it saves)"
4861                            .to_string()
4862                    }
4863                } else if !spec_wanted {
4864                    if spec_env.is_none() {
4865                        "spec off (native Metal batch verifier requires CMF_GRAPH_SPEC=1 after exact-ID validation)".to_string()
4866                    } else {
4867                        "spec off (native Metal accepts only exact CMF_GRAPH_SPEC=1)".to_string()
4868                    }
4869                } else if task_mask.is_some() {
4870                    "spec off (task mask)".to_string()
4871                } else if !graph_spec && !pair_pays {
4872                    "spec off (host fused pair requires CMF_MTP_PAIR=1 after exact-ID validation)"
4873                        .to_string()
4874                } else {
4875                    "spec off (O(1) attention)".to_string()
4876                };
4877                let on = |var: &str| {
4878                    if std::env::var(var).as_deref() == Ok("0") {
4879                        "off"
4880                    } else {
4881                        "on"
4882                    }
4883                };
4884                tracing::info!(
4885                    "metal native: {spec}, state4 {}, async replay {}, prefill graph {}, \
4886                     MTP graph {}, attend {}, probe {}",
4887                    if crate::gpu_metal::state4_on() {
4888                        "on"
4889                    } else {
4890                        "off"
4891                    },
4892                    if crate::gpu_metal::async_replay_on() {
4893                        "on"
4894                    } else {
4895                        "off"
4896                    },
4897                    on("CMF_METAL_PREFILL"),
4898                    on("CMF_MTP_GRAPH"),
4899                    std::env::var("CMF_GPU_ATTEND").unwrap_or_else(|_| "auto".into()),
4900                    if crate::gpu::probe_enabled() {
4901                        "bypassed (q1 force)"
4902                    } else {
4903                        "off"
4904                    },
4905                );
4906            });
4907        }
4908        let spec_active = self.speculative
4909            && self.mtp.is_some()
4910            && task_mask.is_none()
4911            && !self.o1_active()
4912            && ((!graph_on && pair_pays && self.sampler_config.temperature < 1e-6) || graph_spec);
4913        // The MTP module is detached during generation so its mutable
4914        // state does not fight the borrow on `self`.
4915        let mut mtp = if spec_active { self.mtp.take() } else { None };
4916        if std::env::var("CMF_MTP_CHAIN_PROBE").is_ok() {
4917            eprintln!(
4918                "mtp-probe gate: spec_active={spec_active} mtp={} speculative={} graph_on={graph_on} temp_ok={}",
4919                mtp.is_some(),
4920                self.speculative,
4921                self.sampler_config.temperature < 1e-6,
4922            );
4923        }
4924        if let Some(m) = &mut mtp {
4925            m.kv.clear();
4926            // The MTP block's own device mirror starts over with its cache.
4927            crate::gpu::graph_kv_reset(self.mtp_kv_id());
4928            self.mtp_graph_mode = None;
4929        }
4930        // MiMo-V2's draft stack: greedy rounds (draft K with the chained
4931        // MTP layers, verify K+1 rows in one batched forward). Sampling
4932        // decodes plain; `CMF_MTP=0` / `CMF_MIMO_MTP=0` turn it off.
4933        let mimo_spec = self.speculative
4934            && self.mimo_mtp.is_some()
4935            && task_mask.is_none()
4936            && !self.o1_active()
4937            && self.dyn_router.is_none()
4938            && self.sampler_config.temperature < 1e-6
4939            && std::env::var("CMF_MIMO_MTP").as_deref() != Ok("0");
4940        if let Some(st) = self.mimo_mtp.as_mut() {
4941            st.reset();
4942            if mimo_spec && std::env::var_os("CMF_MIMO_MTP_PROBE").is_some() {
4943                Self::mimo_mtp_hist_cap(st, input_ids.len());
4944            }
4945        }
4946        // Dynamic router detached during decode (same borrow trick as MTP).
4947        // Speculative decode and dynamic routing are mutually exclusive
4948        // for now — the fused-pair path doesn't carry per-token φ.
4949        let mut router = if mtp.is_none() {
4950            self.dyn_router.take()
4951        } else {
4952            None
4953        };
4954        let mut reuse_from = reuse_from;
4955        if let Some(r) = &mut router {
4956            r.reset(); // active=backbone, matching a fresh overlay
4957            self.dyn_phi_seen = 0; // fresh φ EMA per generation
4958            if self.dyn_active.is_some() {
4959                // A real switch back to the backbone invalidates the
4960                // cache the reuse key was computed against.
4961                let _ = self.set_active_skill(None);
4962                reuse_from = 0;
4963                self.last_prefill_tokens = input_ids.len();
4964            }
4965        }
4966
4967        let mut all_ids = input_ids.to_vec();
4968        let mut generated = 0usize;
4969        let mut finish_reason = "max_tokens".to_string();
4970        let mut drafted = 0usize;
4971        let mut accepted = 0usize;
4972        // DeepSeek-V4's draft quality is strongly content-dependent.  Two
4973        // consecutive paid rounds with no extra token put it on a bounded
4974        // cooldown; predictable text keeps batching, ordinary prose falls
4975        // back to the exact walk instead of paying a slow draft forever.
4976        // Local to one generation so one difficult request cannot poison the
4977        // next one, and deliberately automatic — this is not a user knob.
4978        let mut dsv4_spec_bad = 0usize;
4979        let mut dsv4_spec_retry_at = 0usize;
4980        let mut confidence: Vec<f32> = Vec::new();
4981        let trace_on = self.trace;
4982        let calib_temp = self.calib_temp;
4983        let mut traces: Vec<TokenTrace> = Vec::new();
4984
4985        // ── Prefill: forward each prompt token once, KEEP the last hidden.
4986        //    Dense prefill runs in fused pairs (weights streamed once per
4987        //    two positions — bit-identical to sequential, proven by the
4988        //    pair tests). With MTP: warm the draft head on
4989        //    (hidden_p, token_{p+1}) pairs.
4990        let mut hidden = vec![0.0f32; self.hidden_size];
4991        let mut pos = reuse_from;
4992        // lm_head-in-graph is only sound when the very next logits
4993        // consumer is this loop's own (MTP and skill routing interleave
4994        // other forwards / can swap lm_head between forward and sample).
4995        // CMF_GPU_LMHEAD=0 keeps lm_head off the graph: the token reads back
4996        // the 8 KB hidden instead of ~1 MB of logits, and the head runs on
4997        // the host. A probe for how much of the graph's fixed per-token cost
4998        // is the logits readback (the layer sweep puts that fixed part at
4999        // 3.88 ms of an 18.5 ms frame).
5000        let fuse_lm = mtp.is_none()
5001            && router.is_none()
5002            && std::env::var("CMF_GPU_LMHEAD").as_deref() != Ok("0");
5003        self.graph_logits = None;
5004        self.graph_want_logits = false;
5005        let _tpf = std::time::Instant::now();
5006        let batch_k = self.generation_batch_k();
5007        if let Some(rows) = prompt_rows {
5008            let hs = self.hidden_size;
5009            let chunk = self.prefill_chunk().max(1);
5010            while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
5011                let end = (pos + chunk).min(input_ids.len());
5012                let hb = match self.prefill_input_rows(
5013                    PrefillIn::Hidden(&rows[pos * hs..end * hs]),
5014                    pos,
5015                    task_mask,
5016                ) {
5017                    Ok(hb) => hb,
5018                    Err(err) => {
5019                        self.finish_generation(&mut mtp, &mut router, true);
5020                        return Err(err);
5021                    }
5022                };
5023                if mimo_spec {
5024                    self.mimo_note_rows(&hb, pos);
5025                }
5026                hidden.copy_from_slice(&hb[hb.len() - hs..]);
5027                pos = end;
5028            }
5029        }
5030        // DeepSeek-V4 owns a separate hyper-connection stack. Route it
5031        // before the generic prefill choices: those correctly reject an
5032        // empty `weights.layers`, but their final per-position fallback used
5033        // to consume the whole prompt before `dsv4::forward_chunk` could see
5034        // it. The batch implementation therefore existed without a live
5035        // production entry point.
5036        //
5037        // Bounded chunks preserve cancellation responsiveness. Only the
5038        // prompt's final chunk asks for logits; every earlier head projection
5039        // would produce 129 280 values that no caller reads.
5040        while self.qwen4_exp.is_some()
5041            && mtp.is_none()
5042            && pos < input_ids.len()
5043            && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
5044        {
5045            // The device path takes several prompt tokens per layer frame;
5046            // the host path runs them one by one inside the same call.
5047            let end = (pos + crate::qwen4_exp::prefill_chunk()).min(input_ids.len());
5048            let want_logits = end == input_ids.len();
5049            let mut lg = Vec::new();
5050            if let Some(b) = &mut self.qwen4_exp {
5051                crate::qwen4_exp::forward_tokens(
5052                    &b.0,
5053                    &b.1,
5054                    &b.2,
5055                    &mut b.3,
5056                    &input_ids[pos..end],
5057                    pos,
5058                    &self.inv_freq,
5059                    self.pool.as_deref(),
5060                    &mut lg,
5061                    want_logits,
5062                );
5063            }
5064            if want_logits {
5065                self.graph_logits = Some(lg);
5066            }
5067            pos = end;
5068            hidden.fill(0.0);
5069        }
5070        while self.dsv4.is_some()
5071            && mtp.is_none()
5072            && pos < input_ids.len()
5073            && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
5074        {
5075            let end = (pos + prefill_chunk()).min(input_ids.len());
5076            let ids: Vec<u32> = input_ids[pos..end].to_vec();
5077            let mut lg = Vec::new();
5078            if let Some(b) = &mut self.dsv4 {
5079                let (g, layers, cfg, st) = (&b.0, &b.1, b.2, &mut b.3);
5080                crate::dsv4::forward_chunk(
5081                    g,
5082                    layers,
5083                    &cfg,
5084                    st,
5085                    &ids,
5086                    pos,
5087                    &self.inv_freq,
5088                    self.pool.as_deref(),
5089                    &mut lg,
5090                    end == input_ids.len(),
5091                );
5092            }
5093            if end == input_ids.len() {
5094                self.graph_logits = Some(lg);
5095            }
5096            pos = end;
5097            hidden = vec![0.0; self.hidden_size];
5098        }
5099        let dsv41_prefill = self.dsv41_prefill.take();
5100        while self.dsv41.is_some()
5101            && mtp.is_none()
5102            && pos < input_ids.len()
5103            && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
5104        {
5105            let end = (pos + prefill_chunk()).min(input_ids.len());
5106            let ids: Vec<u32> = input_ids[pos..end].to_vec();
5107            let mut lg = Vec::new();
5108            if let Some(b) = &mut self.dsv41 {
5109                let (g, layers, cfg, st) = (&b.0, &b.1, &b.2, &mut b.3);
5110                if let Some((embeddings, participates)) = dsv41_prefill.as_ref() {
5111                    crate::dsv41::forward_chunk_masked_with_embeddings(
5112                        g,
5113                        layers,
5114                        cfg,
5115                        st,
5116                        &ids,
5117                        pos,
5118                        &embeddings[pos..end],
5119                        &participates[pos..end],
5120                        self.pool.as_deref(),
5121                        &mut lg,
5122                    );
5123                } else {
5124                    crate::dsv41::forward_chunk(
5125                        g,
5126                        layers,
5127                        cfg,
5128                        st,
5129                        &ids,
5130                        pos,
5131                        self.pool.as_deref(),
5132                        &mut lg,
5133                    );
5134                }
5135            }
5136            if end == input_ids.len() {
5137                self.graph_logits = Some(lg);
5138            }
5139            pos = end;
5140            hidden = vec![0.0; self.hidden_size];
5141        }
5142        // With dynamic routing, prefill sequentially so the φ hook fires
5143        // over the PROMPT — the router enters decode with a warm φ (the
5144        // fused-pair path skips the per-layer φ capture). o1 layers
5145        // collect their query trace in both the single and pair paths.
5146        let dyn_prefill = router.is_some();
5147        // Optional bounded calibration prefix for generation.  The normal
5148        // O(1) path seals after the full prompt; this explicit knob instead
5149        // runs only the requested prefix through exact attention, seals the
5150        // Nyström state, and streams the rest of the prompt through the same
5151        // O(1) step used by decode.  It keeps the O(1) layers' Q trace and
5152        // temporary full KV bounded by the prefix while leaving the default
5153        // full-prompt quality profile untouched.
5154        let o1_prefill_limit = o1_prefill
5155            .and_then(|requested| self.o1_effective_boundary(requested))
5156            .map(|boundary| boundary.min(input_ids.len()));
5157        let mut o1_sealed = false;
5158        if let Some(limit) = o1_prefill_limit {
5159            // Reuse the exact batched prefix machinery when available; it
5160            // records the same per-position Q trace as the full prefill.
5161            if self.can_prefill_batched() && limit > 2 {
5162                let chunk = self.prefill_chunk();
5163                let hs = self.hidden_size;
5164                while pos < limit && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
5165                    let end = (pos + chunk).min(limit);
5166                    let hb = self.prefill_batch(&input_ids[pos..end], pos);
5167                    hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
5168                    pos = end;
5169                }
5170            } else {
5171                while pos < limit && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
5172                    hidden = self.forward_layers(&self.embed_single(input_ids[pos]), pos, None);
5173                    pos += 1;
5174                }
5175            }
5176            if pos >= limit {
5177                o1_sealed = match self.o1_seal_checked() {
5178                    Ok(sealed) => sealed,
5179                    Err(err) => {
5180                        self.finish_generation(&mut mtp, &mut router, true);
5181                        return Err(err);
5182                    }
5183                };
5184                tracing::info!(
5185                    "o1 bounded prompt prefix: requested={} effective={} processed={} of {} token(s)",
5186                    o1_prefill.unwrap_or(0),
5187                    self.o1_effective_boundary(o1_prefill.unwrap_or(0))
5188                        .unwrap_or(limit),
5189                    limit,
5190                    input_ids.len()
5191                );
5192            }
5193        }
5194        // q1 hybrids on Metal: the per-position GPU token graph beats
5195        // the CPU chunk-GEMM (whose wall is the sequential scalar GDN
5196        // recurrence), so prefill goes position-by-position through the
5197        // same graph as decode. Pure-attention models keep the batched
5198        // path — there the chunk-GEMM amortization wins.
5199        let graph_prefill = self.graph_prefill_preferred();
5200        // Native Metal, q4tp GDN hybrids: the prompt through the b-row
5201        // rows graph — projections as GEMMs over up to 512 positions, the
5202        // GDN recurrence in registers on the device, K/V rows appended by
5203        // the chunk — instead of one token-graph submit per position (the
5204        // 27B: 8 tok/s → GEMM-bound). The MTP warm-up rows come out of one
5205        // batched run of the block per chunk. Any refusal leaves the rest
5206        // of the prompt to the sequential paths below.
5207        #[cfg(target_os = "macos")]
5208        if task_mask.is_none()
5209            && !dyn_prefill
5210            && (crate::gpu::q1_force() || crate::gpu::q2tp_gpu_opt_in())
5211            && crate::gpu::enabled_here()
5212            && self.gdn_cfg.is_some()
5213            && !self.per_token_stack()
5214            && input_ids.len() > 8
5215            && std::env::var("CMF_MTP_CHAIN_PROBE").is_err()
5216            && std::env::var("CMF_METAL_PREFILL").as_deref() != Ok("0")
5217        {
5218            let chunk: usize = std::env::var("CMF_METAL_PREFILL_CHUNK")
5219                .ok()
5220                .and_then(|v| v.parse().ok())
5221                .filter(|&v| (16..=512).contains(&v))
5222                .unwrap_or(256);
5223            let hs = self.hidden_size;
5224            let _tp = std::time::Instant::now();
5225            while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
5226                let end = (pos + chunk).min(input_ids.len());
5227                let hb = match self.prefill_batch_metal(&input_ids[pos..end], pos) {
5228                    MetalPrefillOutcome::Completed(hb) => hb,
5229                    MetalPrefillOutcome::Declined => break,
5230                    MetalPrefillOutcome::Failed => {
5231                        self.finish_generation(&mut mtp, &mut router, true);
5232                        return Err("ordinary Metal prefill failed after admission".into());
5233                    }
5234                };
5235                if let Some(m) = &mut mtp {
5236                    let n_pairs = if end < input_ids.len() {
5237                        end - pos
5238                    } else {
5239                        end - pos - 1
5240                    };
5241                    if n_pairs > 0 {
5242                        let pairs: Vec<(&[f32], u32)> = (0..n_pairs)
5243                            .map(|j| (&hb[j * hs..(j + 1) * hs], input_ids[pos + j + 1]))
5244                            .collect();
5245                        if !self.mtp_warm_batch_metal(m, &pairs, pos) {
5246                            for (j, (h, t)) in pairs.iter().enumerate() {
5247                                let h = h.to_vec();
5248                                let _ = self.mtp_step(m, &h, *t, pos + j);
5249                            }
5250                        }
5251                    }
5252                }
5253                hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
5254                pos = end;
5255            }
5256            if std::env::var("CMF_PREFILL_PROF").is_ok() {
5257                eprintln!(
5258                    "metal-prefill: {} of {} tokens in {:.1} ms",
5259                    pos,
5260                    input_ids.len(),
5261                    _tp.elapsed().as_secs_f64() * 1e3
5262                );
5263            }
5264        }
5265        // Gemma-4 on a discrete card: the prompt through the batched wgpu
5266        // graph (`g4_batch_ingest`); its K/V land in the device mirrors
5267        // decode reads. A refusal leaves the rest of the prompt to the
5268        // chunked host prefill below.
5269        #[cfg(not(target_os = "macos"))]
5270        if task_mask.is_none()
5271            && !dyn_prefill
5272            && mtp.is_none()
5273            && o1_prefill.is_none()
5274            && batch_k > 0
5275            && pos + 1 < input_ids.len()
5276            && self.g4_batch_prefill()
5277        {
5278            let t_g4 = std::time::Instant::now();
5279            match self.g4_batch_ingest(&input_ids[pos..], pos, false) {
5280                Ok((n, last)) => {
5281                    if n > 0 {
5282                        hidden.copy_from_slice(&last);
5283                    }
5284                    if std::env::var("CMF_PREFILL_PROF").is_ok() {
5285                        eprintln!(
5286                            "gemma4 batch prefill: {n} of {} tokens in {:.1} ms",
5287                            input_ids.len() - pos,
5288                            t_g4.elapsed().as_secs_f64() * 1e3
5289                        );
5290                    }
5291                    pos += n;
5292                }
5293                Err(e) => {
5294                    self.finish_generation(&mut mtp, &mut router, true);
5295                    return Err(e);
5296                }
5297            }
5298        }
5299        self.mimo_moe_prepare();
5300        // A MoE stack larger than the card (MiMo-V2 q4tp on 96 GB): the
5301        // batched wgpu graph runs the device prefix of every chunk — its
5302        // experts resident — and the host's batched layer walk finishes
5303        // the chunk. Any refusal leaves the rest of the prompt to the
5304        // chunked prefill below.
5305        #[cfg(not(target_os = "macos"))]
5306        if task_mask.is_none()
5307            && !dyn_prefill
5308            && !graph_prefill
5309            && mtp.is_none()
5310            && o1_prefill.is_none()
5311            && !self.o1_active()
5312            && input_ids.len() > 2
5313            && self.batch_prefix_prefill()
5314        {
5315            let chunk = self.prefill_chunk().max(1);
5316            let hs = self.hidden_size;
5317            let t_bp = std::time::Instant::now();
5318            let pos0 = pos;
5319            while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
5320                let end = (pos + chunk).min(input_ids.len());
5321                let bk = end - pos;
5322                let mut hiddens = vec![0f32; bk * hs];
5323                for (j, &id) in input_ids[pos..end].iter().enumerate() {
5324                    hiddens[j * hs..(j + 1) * hs].copy_from_slice(&self.embed_single(id));
5325                }
5326                let positions: Vec<usize> = (pos..end).collect();
5327                let mut run = 0usize;
5328                let outcome = self.try_batch_graph_wgpu_prefix(
5329                    &mut hiddens,
5330                    &positions,
5331                    bk,
5332                    None,
5333                    Some(&mut run),
5334                );
5335                match outcome {
5336                    crate::gpu::BatchGraphOutcome::Completed => {
5337                        let hb = if run < self.num_layers {
5338                            self.prefill_batch_span(
5339                                PrefillIn::Hidden(&hiddens),
5340                                pos,
5341                                None,
5342                                run,
5343                                self.num_layers,
5344                            )
5345                        } else {
5346                            hiddens
5347                        };
5348                        if mimo_spec {
5349                            self.mimo_note_rows(&hb, pos);
5350                        }
5351                        hidden.copy_from_slice(&hb[(bk - 1) * hs..]);
5352                        pos = end;
5353                    }
5354                    crate::gpu::BatchGraphOutcome::Failed => {
5355                        self.finish_generation(&mut mtp, &mut router, true);
5356                        return Err("batched prefix prefill failed after admission".into());
5357                    }
5358                    crate::gpu::BatchGraphOutcome::Declined => {
5359                        // Earlier chunks left their prefix rows on the
5360                        // device only: the host walk below needs them.
5361                        #[cfg(feature = "gpu")]
5362                        if pos > pos0 {
5363                            self.pull_lagging_host_kv(0, self.num_layers, pos);
5364                        }
5365                        break;
5366                    }
5367                }
5368            }
5369            if std::env::var("CMF_PREFILL_PROF").is_ok() {
5370                eprintln!(
5371                    "batch-prefix prefill: {} of {} tokens in {:.1} ms",
5372                    pos - pos0,
5373                    input_ids.len(),
5374                    t_bp.elapsed().as_secs_f64() * 1e3
5375                );
5376            }
5377        }
5378        if task_mask.is_none()
5379            && !dyn_prefill
5380            && !graph_prefill
5381            && self.can_prefill_batched()
5382            && !self.per_token_stack()
5383            && o1_prefill.is_none()
5384            && input_ids.len() > 2
5385        {
5386            // Production prefill = the same chunked prefill-GEMM that
5387            // bench/PPL measure (roadmap §3 P0: generation used to warm
5388            // the prompt with the slower pair path — the published
5389            // prefill number didn't match real TTFT). MTP warm-up reads
5390            // each position's hidden straight from the chunk result.
5391            let chunk = self.prefill_chunk();
5392            let hs = self.hidden_size;
5393            while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
5394                let end = (pos + chunk).min(input_ids.len());
5395                let hb = self.prefill_batch(&input_ids[pos..end], pos);
5396                if mimo_spec {
5397                    self.mimo_note_rows(&hb, pos);
5398                }
5399                if let Some(m) = &mut mtp {
5400                    let probe: usize = std::env::var("CMF_MTP_CHAIN_PROBE")
5401                        .ok()
5402                        .and_then(|v| v.parse().ok())
5403                        .unwrap_or(0);
5404                    for p in pos..end {
5405                        if p + 1 < input_ids.len() {
5406                            if probe >= 1 && p + 2 < input_ids.len() {
5407                                // Teacher-forced chain acceptance (see the
5408                                // tail loop's twin): the warm-up row stays,
5409                                // the chain's rows roll back.
5410                                let (d1, mut hx) = self.mtp_step_h(
5411                                    m,
5412                                    &hb[(p - pos) * hs..(p - pos + 1) * hs],
5413                                    input_ids[p + 1],
5414                                    p,
5415                                );
5416                                let mut ok = d1 == input_ids[p + 2];
5417                                Self::chain_probe_note(0, ok);
5418                                let mut d_prev = d1;
5419                                let mut extra = 0usize;
5420                                for j in 1..probe {
5421                                    if p + 2 + j >= input_ids.len() {
5422                                        break;
5423                                    }
5424                                    let (dj, hj) = self.mtp_step_h(m, &hx, d_prev, p + 1 + j);
5425                                    extra += 1;
5426                                    ok = ok && dj == input_ids[p + 2 + j];
5427                                    Self::chain_probe_note(j, ok);
5428                                    d_prev = dj;
5429                                    hx = hj;
5430                                }
5431                                m.kv.truncate_last(extra);
5432                            } else {
5433                                let _ = self.mtp_step(
5434                                    m,
5435                                    &hb[(p - pos) * hs..(p - pos + 1) * hs],
5436                                    input_ids[p + 1],
5437                                    p,
5438                                );
5439                            }
5440                        }
5441                    }
5442                }
5443                hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
5444                pos = end;
5445            }
5446        }
5447        let pair_off = std::env::var("CMF_PAIR").is_ok_and(|v| v == "0");
5448        if task_mask.is_none()
5449            && !dyn_prefill
5450            && !graph_prefill
5451            && !pair_off
5452            && self.pair_supported()
5453            && o1_prefill.is_none()
5454        {
5455            while pos + 1 < input_ids.len()
5456                && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
5457            {
5458                let e1 = self.embed_single(input_ids[pos]);
5459                let e2 = self.embed_single(input_ids[pos + 1]);
5460                let (h1, h2) = self.forward_pair(&e1, &e2, pos);
5461                if mimo_spec {
5462                    self.mimo_note_rows(&h1, pos);
5463                    self.mimo_note_rows(&h2, pos + 1);
5464                }
5465                // Both prefill tokens are real → commit lane-2 states.
5466                self.commit_linear_scratch();
5467                if let Some(m) = &mut mtp {
5468                    let _ = self.mtp_step(m, &h1, input_ids[pos + 1], pos);
5469                    if pos + 2 < input_ids.len() {
5470                        let probe: usize = std::env::var("CMF_MTP_CHAIN_PROBE")
5471                            .ok()
5472                            .and_then(|v| v.parse().ok())
5473                            .unwrap_or(0);
5474                        if probe >= 1 && pos + 3 < input_ids.len() {
5475                            // Same teacher-forced chain table as the tail
5476                            // loop below, fed from the pair path that owns
5477                            // most prefill positions.
5478                            let (d1, mut hx) = self.mtp_step_h(m, &h2, input_ids[pos + 2], pos + 1);
5479                            let mut ok = d1 == input_ids[pos + 3];
5480                            Self::chain_probe_note(0, ok);
5481                            let mut d_prev = d1;
5482                            let mut extra = 0usize;
5483                            for j in 1..probe {
5484                                if pos + 3 + j >= input_ids.len() {
5485                                    break;
5486                                }
5487                                let (dj, hj) = self.mtp_step_h(m, &hx, d_prev, pos + 2 + j);
5488                                extra += 1;
5489                                ok = ok && dj == input_ids[pos + 3 + j];
5490                                Self::chain_probe_note(j, ok);
5491                                d_prev = dj;
5492                                hx = hj;
5493                            }
5494                            m.kv.truncate_last(extra);
5495                        } else {
5496                            let _ = self.mtp_step(m, &h2, input_ids[pos + 2], pos + 1);
5497                        }
5498                    }
5499                }
5500                hidden = h2;
5501                pos += 2;
5502            }
5503        }
5504        // Batched GPU prefill for the wgpu decode graph (GDN hybrids): K prompt
5505        // positions per submit — projections/FFN as GEMMs (weight once per K),
5506        // attention/GDN looped inside — instead of one whole-graph submit per
5507        // position. Falls through to the per-position graph on any refusal.
5508        // Batched prefill is opt-in (CMF_BATCH_K>0). Default 0 = per-position
5509        // graph prefill. (Steady-state decode is provably identical either way —
5510        // token-graph submit and lm_head both unchanged — so this only trades
5511        // prefill wall.)
5512        // A bounded O(1) prefix is the one post-seal prompt interval: only
5513        // admit its batch when the device O(1) route is explicitly enabled and
5514        // every sealed layer exposes a portable view. The same batch size and
5515        // refusal behavior remain the ordinary controls/comparator.
5516        let o1_batch_ready = o1_sealed
5517            && o1_prefill.is_some()
5518            && mtp.is_none()
5519            && std::env::var("CMF_O1_GPU").as_deref() == Ok("1")
5520            && (0..self.num_layers).all(|li| {
5521                let cache = &self.kv_cache.layers[self.phys_layer(li)];
5522                cache.o1.is_none() || cache.o1_views().is_some()
5523            });
5524        // The ordinary graph-prefill route can share each completed trunk
5525        // chunk with an attached MTP head.  Keep chain probing on its
5526        // established per-position path: the probe deliberately needs every
5527        // teacher-forced draft row and its rollback table.
5528        let mtp_batch_prefill = mtp.is_some()
5529            && graph_prefill
5530            && task_mask.is_none()
5531            && !dyn_prefill
5532            && !self.o1_active()
5533            && std::env::var("CMF_MTP_CHAIN_PROBE").is_err();
5534        if batch_k > 0
5535            && (graph_prefill || o1_batch_ready)
5536            && task_mask.is_none()
5537            && (!self.o1_active() || o1_batch_ready)
5538            && (mtp.is_none() || mtp_batch_prefill)
5539            && !dyn_prefill
5540            && pos + 1 < input_ids.len()
5541        {
5542            let hs = self.hidden_size;
5543            let chunk = batch_k;
5544            while pos < input_ids.len() {
5545                let end = (pos + chunk).min(input_ids.len());
5546                let bk = end - pos;
5547                let mut hiddens = vec![0f32; bk * hs];
5548                for (j, &id) in input_ids[pos..end].iter().enumerate() {
5549                    hiddens[j * hs..(j + 1) * hs].copy_from_slice(&self.embed_single(id));
5550                }
5551                let positions: Vec<usize> = (pos..end).collect();
5552                let t_chunk = std::time::Instant::now();
5553                let outcome = self.try_batch_graph_wgpu(&mut hiddens, &positions, bk, None);
5554                let ok_b = outcome == crate::gpu::BatchGraphOutcome::Completed;
5555                if std::env::var("CMF_GRAPH_PROF").is_ok() {
5556                    let ms = t_chunk.elapsed().as_secs_f64() * 1000.0;
5557                    eprintln!(
5558                        "batch-chunk: phase=prompt mode={} k={bk} outcome={outcome:?} {ms:.1} ms ({:.1} tok/s)",
5559                        if o1_batch_ready {
5560                            "o1"
5561                        } else if mtp_batch_prefill {
5562                            "ordinary_mtp"
5563                        } else {
5564                            "ordinary"
5565                        },
5566                        bk as f64 / (ms / 1000.0)
5567                    );
5568                }
5569                {
5570                    use std::sync::atomic::{AtomicBool, Ordering};
5571                    static SAID: AtomicBool = AtomicBool::new(false);
5572                    if !SAID.swap(true, Ordering::Relaxed) {
5573                        if ok_b {
5574                            tracing::info!(
5575                                "batched prefill: ACTIVE mode={} (k={bk})",
5576                                if o1_batch_ready {
5577                                    "o1"
5578                                } else if mtp_batch_prefill {
5579                                    "ordinary_mtp"
5580                                } else {
5581                                    "ordinary"
5582                                }
5583                            );
5584                        } else {
5585                            tracing::warn!("batched prefill {:?} — per-position graph", outcome);
5586                        }
5587                    }
5588                }
5589                if ok_b {
5590                    if mimo_spec {
5591                        self.mimo_note_rows(&hiddens, pos);
5592                    }
5593                    if mtp_batch_prefill {
5594                        let n_pairs = mtp_prefill_pair_count(pos, end, input_ids.len());
5595                        if n_pairs > 0 {
5596                            // `hiddens` is owned by this chunk, so materialize
5597                            // row slices before borrowing the detached MTP
5598                            // module.  The last prompt row has no successor;
5599                            // the helper above is the single source of that
5600                            // boundary rule.
5601                            let rows: Vec<Vec<f32>> = (0..n_pairs)
5602                                .map(|j| hiddens[j * hs..(j + 1) * hs].to_vec())
5603                                .collect();
5604                            let pairs: Vec<(&[f32], u32)> = rows
5605                                .iter()
5606                                .enumerate()
5607                                .map(|(j, row)| (row.as_slice(), input_ids[pos + j + 1]))
5608                                .collect();
5609                            if std::env::var("CMF_GRAPH_PROF").is_ok() {
5610                                eprintln!(
5611                                    "mtp-warm: phase=prompt mode=ordinary_mtp first_pos={} pairs={} last_pos={}",
5612                                    pos,
5613                                    n_pairs,
5614                                    pos + n_pairs - 1,
5615                                );
5616                            }
5617                            let warm_error = if let Some(m) = mtp.as_mut() {
5618                                self.mtp_warm_prefill_pairs(m, &pairs, pos).err()
5619                            } else {
5620                                None
5621                            };
5622                            if let Some(err) = warm_error {
5623                                // The trunk batch was already admitted.  A
5624                                // failed MTP warm-up therefore clears both
5625                                // mirrors and exits; continuing would pair a
5626                                // current trunk state with a stale MTP cache.
5627                                self.finish_generation(&mut mtp, &mut router, true);
5628                                return Err(err.to_string());
5629                            }
5630                        }
5631                    }
5632                    hidden.copy_from_slice(&hiddens[(bk - 1) * hs..]);
5633                    pos = end;
5634                } else if outcome == crate::gpu::BatchGraphOutcome::Failed {
5635                    // A failed batch may have advanced a device recurrent
5636                    // state (ordinary GDN or sealed O(1)). A CPU fallback
5637                    // would then observe stale accumulators, so clear the
5638                    // request state and make the failure explicit.
5639                    self.finish_generation(&mut mtp, &mut router, true);
5640                    return Err(if o1_batch_ready {
5641                        "sealed O(1) batch graph failed after admission".to_string()
5642                    } else {
5643                        "ordinary recurrent batch graph failed after admission".to_string()
5644                    });
5645                } else {
5646                    break; // unsupported → per-position graph handles the rest
5647                }
5648            }
5649        }
5650        // Resident Embryo graph: the prompt in chunks of one submit each
5651        // instead of one whole-graph submit per position; the last chunk
5652        // carries the logits exactly as the per-position walk would.
5653        if graph_prefill
5654            && task_mask.is_none()
5655            && mtp.is_none()
5656            && !dyn_prefill
5657            && pos == 0
5658            && input_ids.len() > 1
5659            && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
5660        {
5661            if let Some(lg) = self.embryo_prefill_chunked(input_ids, 0) {
5662                self.graph_logits = Some(lg);
5663                hidden = vec![0.0; self.hidden_size];
5664                pos = input_ids.len();
5665            }
5666        }
5667        while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
5668            self.graph_want_logits = fuse_lm && pos + 1 == input_ids.len();
5669            hidden = self.forward_layers(&self.embed_single(input_ids[pos]), pos, task_mask);
5670            if mimo_spec {
5671                self.mimo_note_rows(&hidden, pos);
5672            }
5673            if let Some(m) = &mut mtp {
5674                if pos + 1 < input_ids.len() {
5675                    // `CMF_MTP_CHAIN_PROBE=k`: teacher-forced acceptance of a
5676                    // CHAINED draft — iterate the head on its own hidden k
5677                    // deep and score every depth against the prompt's real
5678                    // continuation. The economics of a k-token speculative
5679                    // round stand or fall on this table.
5680                    let probe: usize = std::env::var("CMF_MTP_CHAIN_PROBE")
5681                        .ok()
5682                        .and_then(|v| v.parse().ok())
5683                        .unwrap_or(0);
5684                    if probe >= 1 && pos + 2 < input_ids.len() {
5685                        let (d1, mut hx) = self.mtp_step_h(m, &hidden, input_ids[pos + 1], pos);
5686                        let mut ok = d1 == input_ids[pos + 2];
5687                        Self::chain_probe_note(0, ok);
5688                        let mut d_prev = d1;
5689                        let mut extra = 0usize;
5690                        for j in 1..probe {
5691                            if pos + 2 + j >= input_ids.len() {
5692                                break;
5693                            }
5694                            let (dj, hj) = self.mtp_step_h(m, &hx, d_prev, pos + 1 + j);
5695                            extra += 1;
5696                            ok = ok && dj == input_ids[pos + 2 + j];
5697                            Self::chain_probe_note(j, ok);
5698                            d_prev = dj;
5699                            hx = hj;
5700                        }
5701                        // The chain's rows are speculation, not the prompt —
5702                        // keep only the warmup row the plain path would add.
5703                        m.kv.truncate_last(extra);
5704                    } else {
5705                        let _ = self.mtp_step(m, &hidden, input_ids[pos + 1], pos);
5706                    }
5707                }
5708            }
5709            pos += 1;
5710        }
5711        if std::env::var("CMF_PREFILL_PROF").is_ok() {
5712            eprintln!(
5713                "prefill: {} tokens in {:.1} ms (batch_k={batch_k})",
5714                input_ids.len(),
5715                _tpf.elapsed().as_secs_f64() * 1000.0
5716            );
5717        }
5718        if self
5719            .graph_failed
5720            .swap(false, std::sync::atomic::Ordering::Relaxed)
5721        {
5722            // MTP is detached for speculative generation.  Restore the
5723            // module before returning the terminal graph error; otherwise a
5724            // failed request would silently remove the head from a pooled
5725            // pipeline and the next request would lose its configured route.
5726            self.finish_generation(&mut mtp, &mut router, true);
5727            return Err("GPU token graph failed during prefill".to_string());
5728        }
5729        // Cancelled mid-prefill: the cache holds a partial prompt —
5730        // drop the reuse history and return an empty generation.
5731        if self
5732            .cancel
5733            .swap(false, std::sync::atomic::Ordering::Relaxed)
5734        {
5735            // A cancelled prefill can already have advanced the device
5736            // mirror. Drop the whole partial sequence so a pooled pipeline
5737            // cannot carry that state into its next request.
5738            self.finish_generation(&mut mtp, &mut router, true);
5739            return Ok(GenerateResult {
5740                text: String::new(),
5741                token_ids: Vec::new(),
5742                prompt_tokens: input_ids.len(),
5743                tokens_generated: 0,
5744                finish_reason: "cancelled".to_string(),
5745                mtp_drafted: 0,
5746                mtp_accepted: 0,
5747                token_confidence: Vec::new(),
5748                traces: Vec::new(),
5749            });
5750        }
5751
5752        // Prompt absorbed → freeze the o1 layers' skeletons; from here
5753        // every decode step on those layers is O(W + m·dv + m²).
5754        if !o1_sealed {
5755            match self.o1_seal_checked() {
5756                Ok(_) => {}
5757                Err(err) => {
5758                    self.finish_generation(&mut mtp, &mut router, true);
5759                    return Err(err);
5760                }
5761            }
5762        }
5763
5764        // Commit one token: push, check EOS, stream. Returns false = stop.
5765        macro_rules! commit {
5766            ($id:expr) => {{
5767                all_ids.push($id);
5768                generated += 1;
5769                self.note_draft_id($id);
5770                if self.tokenizer.is_eos($id) && !self.ignore_eos {
5771                    finish_reason = "stop".to_string();
5772                    false
5773                } else {
5774                    let token_text = self.tokenizer.decode_token($id);
5775                    let mut go = true;
5776                    if let Some(ref mut cb) = on_token {
5777                        if !cb(&token_text) {
5778                            finish_reason = "cancelled".to_string();
5779                            go = false;
5780                        }
5781                    }
5782                    go
5783                }
5784            }};
5785        }
5786
5787        // Speculation is decided by MEASUREMENT, not by an acceptance
5788        // model. A k=4 round costs ~3.8 plain tokens on the 5090 (draft
5789        // 6.6 + verify 66.6 + commit 4.8 ms against a 20.6 ms token), so it
5790        // pays only when the head lands ~2.8 of 4 — predictable text (code,
5791        // structured output) does, free prose often does not, and the
5792        // ratio at which the two cross depends on the card and the context
5793        // depth. So: four speculative rounds timed, then eight plain
5794        // tokens timed, and the faster arm runs until a re-check 256
5795        // tokens later (context growth moves the balance). The trial
5796        // costs at most a few tokens of the slower arm per 256.
5797        let mut spec_trial = SpecTrial::Spec {
5798            t0: std::time::Instant::now(),
5799            gen0: generated,
5800            rounds: 0,
5801        };
5802        // The token-count proxy prices a round at ~1.9 plain tokens. That
5803        // holds for the Metal rounds whose cost was measured — greedy and
5804        // the sparse sampling chain — so an expensive round (the dense
5805        // chain, reachable only by `CMF_GRAPH_SPEC_SAMPLE=1`) still times
5806        // the plain path before it decides.
5807        let mut spec_mon = SpecMon {
5808            metal: graph_spec && crate::gpu::q1_force() && spec_cheap_round,
5809            ..SpecMon::default()
5810        };
5811        let mut spec_watchdog_off = false;
5812        // CMF_GRAPH_SPEC_TIME: the round walls so far (round 1 excluded —
5813        // it pays the scratch), for the outlier test on each new one
5814        let mut spec_walls: Vec<f32> = Vec::new();
5815        // ... and the end of the last round: the host time between rounds
5816        // (token commits, streaming, the loop top) is printed at level 2
5817        let mut spec_round_end: Option<std::time::Instant> = None;
5818        if mimo_spec {
5819            if let Ok(path) = std::env::var("CMF_MIMO_MTP_PROBE") {
5820                if let Some(mut st) = self.mimo_mtp.take() {
5821                    self.mimo_mtp_probe(&mut st, input_ids, &path);
5822                    self.mimo_mtp = Some(st);
5823                }
5824            }
5825        }
5826        // ── Decode ──
5827        let mut next_pos = input_ids.len();
5828        'decode: while generated < max_tokens {
5829            if self
5830                .graph_failed
5831                .swap(false, std::sync::atomic::Ordering::Relaxed)
5832            {
5833                // Keep the detached MTP module attached after a terminal
5834                // graph error so the pipeline can be reused for a fresh
5835                // sequence.  `clear_sequence_state` only clears mirrors and
5836                // host KV; it cannot recover a module dropped here.
5837                self.finish_generation(&mut mtp, &mut router, true);
5838                return Err("GPU token graph failed during decode".to_string());
5839            }
5840            if self
5841                .cancel
5842                .swap(false, std::sync::atomic::Ordering::Relaxed)
5843            {
5844                finish_reason = "cancelled".to_string();
5845                break 'decode;
5846            }
5847            // A rejected speculative draft already drew this position's
5848            // token from the residual distribution (graph_spec_step); it
5849            // is committed as-is — sampling again from the row's logits
5850            // would bias the stream toward the target's mode.
5851            if mimo_spec && next_pos > 0 {
5852                // Every path leaves `hidden` = the backbone output at
5853                // next_pos-1; the draft layers read it (idempotent).
5854                self.mimo_note_rows(&hidden, next_pos - 1);
5855            }
5856            let forced = self.spec_forced.take();
5857            let mut logits = match (forced, self.graph_logits.take()) {
5858                (Some(_), _) => Vec::new(),
5859                (None, Some(lg)) => lg,
5860                (None, None) => {
5861                    let _prof = crate::cpuprof::time(crate::cpuprof::Slot::Head);
5862                    // Pooled-tail mode: the head as one layer-less device
5863                    // pass (final-norm + matvec + readback). Any decline —
5864                    // no pool, unresolvable weight, backend off — keeps the
5865                    // host head exactly as before.
5866                    #[cfg(feature = "gpu")]
5867                    let logits_out = if let Some(lg) = self.m3_device_logits(&hidden) {
5868                        lg
5869                    } else {
5870                        inference::rms_norm_into(
5871                            &hidden,
5872                            &self.weights.final_norm,
5873                            self.rms_eps,
5874                            self.norm_style,
5875                            &mut self.ws.n1,
5876                        );
5877                        self.lm_head_forward(&self.ws.n1)
5878                    };
5879                    #[cfg(not(feature = "gpu"))]
5880                    let logits_out = {
5881                        inference::rms_norm_into(
5882                            &hidden,
5883                            &self.weights.final_norm,
5884                            self.rms_eps,
5885                            self.norm_style,
5886                            &mut self.ws.n1,
5887                        );
5888                        self.lm_head_forward(&self.ws.n1)
5889                    };
5890                    logits_out
5891                }
5892            };
5893            // CMF_LOGIT_DUMP=<path>: the first decode step's hidden + logits
5894            // as raw f32 (hidden first) — cross-backend numerics diffing.
5895            if generated
5896                == std::env::var("CMF_LOGIT_DUMP_STEP")
5897                    .ok()
5898                    .and_then(|v| v.parse().ok())
5899                    .unwrap_or(0)
5900            {
5901                if let Ok(path) = std::env::var("CMF_LOGIT_DUMP") {
5902                    let mut bytes: Vec<u8> = Vec::with_capacity((hidden.len() + logits.len()) * 4);
5903                    for v in hidden.iter().chain(logits.iter()) {
5904                        bytes.extend_from_slice(&v.to_le_bytes());
5905                    }
5906                    if let Err(e) = std::fs::write(&path, &bytes) {
5907                        eprintln!("logit dump: failed to write {path}: {e}");
5908                        self.finish_generation(&mut mtp, &mut router, true);
5909                        return Err(format!("logit dump write failed: {e}"));
5910                    }
5911                }
5912            }
5913            // CMF_LOGIT_DUMP_ALL=<dir>: every decode step's logits as raw
5914            // f32, `<dir>/step{n:05}.f32` — step-by-step backend diffing
5915            // (a greedy run on two backends compares until they diverge).
5916            if let Ok(dir) = std::env::var("CMF_LOGIT_DUMP_ALL") {
5917                if !logits.is_empty() {
5918                    let path = std::path::Path::new(&dir).join(format!("step{generated:05}.f32"));
5919                    let bytes: Vec<u8> = logits.iter().flat_map(|v| v.to_le_bytes()).collect();
5920                    if let Err(e) =
5921                        std::fs::create_dir_all(&dir).and_then(|_| std::fs::write(&path, &bytes))
5922                    {
5923                        eprintln!("logit dump: failed to write {}: {e}", path.display());
5924                    }
5925                }
5926            }
5927            let t_next = match forced.or_else(|| forced_ids_at(generated)) {
5928                Some(c) => c,
5929                None => {
5930                    let _prof = crate::cpuprof::time(crate::cpuprof::Slot::Sampler);
5931                    sampler::sample_with_scratch_pool(
5932                        &logits,
5933                        &self.sampler_config,
5934                        self.sampler_config.penalty_past(&all_ids, bounded_native),
5935                        &mut self.rng,
5936                        &mut self.sampler_scratch,
5937                        self.pool.as_deref(),
5938                    )
5939                }
5940            };
5941            if self.confidence_on {
5942                confidence.push(if logits.is_empty() {
5943                    0.0
5944                } else {
5945                    sampler::top1_prob_pool(
5946                        self.pool.as_deref(),
5947                        &mut self.sampler_scratch,
5948                        &logits,
5949                        t_next,
5950                        calib_temp,
5951                    )
5952                });
5953            }
5954            if !logits.is_empty() {
5955                attention::recycle_buf(&mut logits);
5956            }
5957            if trace_on {
5958                // active_skill = the overlay in force while this token was
5959                // generated; recon/switched are filled after the post-emit
5960                // routing eval below (freshest coherence for this token).
5961                let skill = router.as_ref().and_then(|r| r.active_id());
5962                traces.push(TokenTrace {
5963                    t: generated,
5964                    token_id: t_next,
5965                    confidence: confidence.last().copied().unwrap_or(0.0),
5966                    active_skill: skill,
5967                    recon: None,
5968                    switched: false,
5969                });
5970            }
5971            if !commit!(t_next) {
5972                break 'decode;
5973            }
5974            if generated >= max_tokens {
5975                break 'decode;
5976            }
5977
5978            // Catch-all for decode paths that bypass the walks' own trim
5979            // (a graph step, a speculative round).
5980            self.swa_trim_tails();
5981            if self.dsv41.is_none() && self.kv_cache.needs_eviction() {
5982                // Say it ONCE, loudly: past this point the model keeps
5983                // talking but has lost half its context, and on a GDN
5984                // hybrid the graph's device state goes stale on top. The
5985                // Qwen3.8 bring-up spent a day reading this cliff as
5986                // three different model bugs.
5987                static SAID: std::sync::Once = std::sync::Once::new();
5988                SAID.call_once(|| {
5989                    tracing::warn!(
5990                        "KV cache full at {} positions — evicting half; quality \
5991                         will degrade. Raise CMF_MAX_SEQ.",
5992                        self.kv_cache.max_seq_len,
5993                    );
5994                });
5995                let keep = (self.kv_cache.max_seq_len / 2).max(1);
5996                self.kv_cache.evict(keep);
5997            }
5998
5999            // Advance the speculation trial: plain-phase accounting and
6000            // the periodic re-check happen here, on every token.
6001            if graph_spec {
6002                match spec_trial {
6003                    SpecTrial::Plain { t0, gen0 } if spec_mon.plain_done(t0, gen0, generated) => {
6004                        spec_mon.plain_ms =
6005                            t0.elapsed().as_secs_f64() * 1e3 / (generated - gen0) as f64;
6006                        let keep = spec_mon.pays();
6007                        tracing::info!(
6008                            "speculation trial: {:.2} tok/round in {:.1} ms vs plain {:.1} ms/tok — {}",
6009                            spec_mon.tokens,
6010                            spec_mon.round_ms,
6011                            spec_mon.plain_ms,
6012                            if keep { "speculating" } else { "plain" }
6013                        );
6014                        spec_mon.fails = 0;
6015                        spec_trial = SpecTrial::Decided {
6016                            spec: keep,
6017                            recheck_at: if keep { usize::MAX } else { generated + 128 },
6018                        };
6019                    }
6020                    SpecTrial::Decided { recheck_at, .. } if generated >= recheck_at => {
6021                        spec_mon.n = 0;
6022                        spec_mon.zero_accept_rounds = 0;
6023                        spec_trial = SpecTrial::Spec {
6024                            t0: std::time::Instant::now(),
6025                            gen0: generated,
6026                            rounds: 0,
6027                        };
6028                    }
6029                    _ => {}
6030                }
6031                spec_watchdog_off = matches!(
6032                    spec_trial,
6033                    SpecTrial::Plain { .. } | SpecTrial::Decided { spec: false, .. }
6034                );
6035            }
6036            // ── MiMo-V2 draft stack: draft K, verify K+1 rows in one batch ──
6037            if mimo_spec && generated + 1 < max_tokens && next_pos > 0 {
6038                let budget = max_tokens - generated - 1;
6039                if let Some(mut st) = self.mimo_mtp.take() {
6040                    let k = st.depth.min(budget);
6041                    let r = self.mimo_spec_round(&mut st, next_pos, &all_ids, k);
6042                    self.mimo_mtp = Some(st);
6043                    let r = match r {
6044                        Ok(r) => r,
6045                        Err(err) => {
6046                            self.finish_generation(&mut mtp, &mut router, true);
6047                            return Err(err);
6048                        }
6049                    };
6050                    if let Some(r) = r {
6051                        drafted += r.drafted;
6052                        accepted += r.accepted.len();
6053                        let mut stopped = false;
6054                        for &id in &r.accepted {
6055                            if self.confidence_on {
6056                                confidence.push(0.0);
6057                            }
6058                            if !commit!(id) {
6059                                stopped = true;
6060                                break;
6061                            }
6062                        }
6063                        if stopped {
6064                            break 'decode;
6065                        }
6066                        next_pos += r.accepted.len() + 1;
6067                        hidden = r.hidden;
6068                        // The loop top chooses the round's own token from
6069                        // these logits — the same sampler, same history.
6070                        self.graph_logits = Some(r.logits);
6071                        continue 'decode;
6072                    }
6073                }
6074            }
6075            // ── Qwen3.8-Flash-Next draft head: k greedy drafts from the MTP
6076            //    sidecar, one batched verify window on the device ──
6077            #[cfg(feature = "gpu")]
6078            if self.speculative
6079                && self.qwen4_exp.is_some()
6080                && task_mask.is_none()
6081                && self.sampler_config.temperature < 1e-6
6082                && generated + 1 < max_tokens
6083                && next_pos > 0
6084                && std::env::var("CMF_QWEN_MTP").as_deref() != Ok("0")
6085            {
6086                let r = match &mut self.qwen4_exp {
6087                    Some(b) => crate::qwen4_exp::spec_round(
6088                        &b.0,
6089                        &b.1,
6090                        &b.2,
6091                        &mut b.3,
6092                        next_pos,
6093                        &all_ids,
6094                        &self.inv_freq,
6095                        self.pool.as_deref(),
6096                    ),
6097                    None => None,
6098                };
6099                if let Some(r) = r {
6100                    drafted += r.drafted;
6101                    accepted += r.accepted.len();
6102                    let mut stopped = false;
6103                    for &id in &r.accepted {
6104                        if self.confidence_on {
6105                            confidence.push(0.0);
6106                        }
6107                        if !commit!(id) {
6108                            stopped = true;
6109                            break;
6110                        }
6111                    }
6112                    if stopped {
6113                        break 'decode;
6114                    }
6115                    next_pos += r.accepted.len() + 1;
6116                    hidden.fill(0.0);
6117                    self.graph_logits = Some(r.logits);
6118                    continue 'decode;
6119                }
6120            }
6121            match &mut mtp {
6122                // ── Graph speculation: chain-draft, batch-verify on device ──
6123                #[cfg(feature = "gpu")]
6124                Some(m)
6125                    if graph_spec
6126                        && !spec_watchdog_off
6127                        && generated + 1 < max_tokens
6128                        && next_pos > 0 =>
6129                {
6130                    let t_round = std::time::Instant::now();
6131                    if spec_time_level() >= 2 {
6132                        if let Some(t) = spec_round_end.take() {
6133                            eprintln!(
6134                                "spec-gap {:.2} ms (host between rounds)",
6135                                t.elapsed().as_secs_f64() * 1e3
6136                            );
6137                        }
6138                    }
6139                    spec_stamps_begin();
6140                    // device buffers allocated during this round: a
6141                    // first-touch Shared allocation is zero-filled inside
6142                    // the command buffer that uses it, which is what the
6143                    // long outlier rounds were
6144                    #[cfg(target_os = "macos")]
6145                    let allocs0 =
6146                        crate::gpu_metal::IO_BUF_ALLOCS.load(std::sync::atomic::Ordering::Relaxed);
6147                    #[cfg(not(target_os = "macos"))]
6148                    let allocs0 = 0u64;
6149                    if let Some((extra, n_pos, new_h)) = self.graph_spec_step(
6150                        m,
6151                        &hidden,
6152                        t_next,
6153                        next_pos,
6154                        &mut drafted,
6155                        &mut accepted,
6156                        &mut all_ids,
6157                        max_tokens - generated,
6158                    ) {
6159                        next_pos = n_pos;
6160                        hidden = new_h;
6161                        let level = spec_time_level();
6162                        if level > 0 {
6163                            let wall = t_round.elapsed().as_secs_f32() * 1e3;
6164                            let stamps = spec_stamps_take();
6165                            // the running median of the rounds before this
6166                            // one (round 1 pays the scratch: not a sample)
6167                            let median = if spec_walls.len() >= 3 {
6168                                let mut s = spec_walls.clone();
6169                                s.sort_by(|a, b| a.partial_cmp(b).unwrap());
6170                                Some(s[s.len() / 2])
6171                            } else {
6172                                None
6173                            };
6174                            let outlier = median.is_some_and(|m| wall > 1.4 * m);
6175                            #[cfg(target_os = "macos")]
6176                            let allocs = crate::gpu_metal::IO_BUF_ALLOCS
6177                                .load(std::sync::atomic::Ordering::Relaxed)
6178                                - allocs0;
6179                            #[cfg(not(target_os = "macos"))]
6180                            let allocs = allocs0;
6181                            eprintln!(
6182                                "spec-round wall {wall:.1} ms → {} tokens{}{}",
6183                                extra.len() + 1,
6184                                if allocs > 0 {
6185                                    format!(" [{allocs} new device buffers]")
6186                                } else {
6187                                    String::new()
6188                                },
6189                                match (outlier, median) {
6190                                    (true, Some(m)) => format!(" OUTLIER (median {m:.1})"),
6191                                    _ => String::new(),
6192                                }
6193                            );
6194                            if level >= 2 || outlier {
6195                                let sum: f32 = stamps.iter().map(|s| s.1).sum();
6196                                eprintln!(
6197                                    "spec-stamps: {}| untracked {:.1}",
6198                                    spec_stamps_format(&stamps),
6199                                    wall - sum
6200                                );
6201                            }
6202                            if spec_mon.n >= 1 {
6203                                spec_walls.push(wall);
6204                            }
6205                        }
6206                        // One speculative round done: the monitor counts it
6207                        // (round 1 untimed — it pays the batch scratch and
6208                        // the draft mirror), and the trial advances.
6209                        spec_mon.round(t_round.elapsed().as_secs_f64() * 1e3, extra.len() + 1);
6210                        // the round's tokens land in `generated` below; the
6211                        // plain phase must start counting AFTER them
6212                        spec_trial = Self::spec_trial_round(
6213                            spec_trial,
6214                            &mut spec_mon,
6215                            generated + extra.len() + 1,
6216                        );
6217                        let mut stopped = false;
6218                        for &id in &extra {
6219                            if self.confidence_on {
6220                                confidence.push(0.0);
6221                            }
6222                            // `graph_spec_step` commits the accepted MTP
6223                            // drafts as a batch.  They do not pass through
6224                            // the ordinary loop-top sampling block below,
6225                            // which is normally where the telemetry row is
6226                            // created.  Keep the public trace contract
6227                            // (one row per emitted token) here as well.
6228                            //
6229                            // The graph-spec path does not surface a
6230                            // per-row probability for accepted extras.  It
6231                            // deliberately matches the zero placed in the
6232                            // confidence channel above instead of inventing
6233                            // one or adding a separate probability pass.
6234                            if trace_on {
6235                                traces.push(TokenTrace {
6236                                    t: generated,
6237                                    token_id: id,
6238                                    confidence: confidence.last().copied().unwrap_or(0.0),
6239                                    active_skill: None,
6240                                    recon: None,
6241                                    switched: false,
6242                                });
6243                            }
6244                            if !commit!(id) {
6245                                stopped = true;
6246                                break;
6247                            }
6248                        }
6249                        if stopped {
6250                            break 'decode;
6251                        }
6252                        if spec_time_level() >= 2 {
6253                            spec_round_end = Some(std::time::Instant::now());
6254                        }
6255                        continue 'decode;
6256                    }
6257                    if self
6258                        .graph_failed
6259                        .swap(false, std::sync::atomic::Ordering::Relaxed)
6260                    {
6261                        // `graph_spec_step` may have detached MTP while a
6262                        // warm-up was in flight.  Do not reinterpret its
6263                        // terminal device failure as a plain decode step;
6264                        // restore the head, clear both mirrors, and surface
6265                        // one explicit error to the caller.
6266                        self.finish_generation(&mut mtp, &mut router, true);
6267                        return Err("GPU MTP graph failed during speculative decode".to_string());
6268                    }
6269                    // Declined (batch graph refused): plain forward below —
6270                    // and a round that produced one token for the trial's
6271                    // ledger, so a graph that keeps refusing is measured out
6272                    // like a head that keeps missing (it was spinning
6273                    // forever on a file whose batch graph declines).
6274                    // A declined round is not a cheap one-token round — it
6275                    // is a verify that does not exist for this file (a
6276                    // healed q8_2f tail measured 760 drafts, 0 accepted, 33
6277                    // against 48.8 tok/s while the monitor called the draft
6278                    // alone "paying"). Count it as the losing streak in one.
6279                    spec_mon.round(t_round.elapsed().as_secs_f64() * 1e3, 1);
6280                    spec_mon.tokens = 0.0;
6281                    spec_mon.fails = 3;
6282                    spec_trial = Self::spec_trial_round(spec_trial, &mut spec_mon, generated + 1);
6283                    hidden = self.forward_layers(&self.embed_single(t_next), next_pos, task_mask);
6284                    next_pos += 1;
6285                    continue 'decode;
6286                }
6287                // ── Speculative: draft t+2, verify in a fused pair ──
6288                Some(m) if !graph_spec && generated + 1 < max_tokens => {
6289                    let draft = self.mtp_step(m, &hidden, t_next, next_pos - 1);
6290                    drafted += 1;
6291                    let emb1 = self.embed_single(t_next);
6292                    let emb2 = self.embed_single(draft);
6293                    let (h1, h2) = self.forward_pair(&emb1, &emb2, next_pos);
6294
6295                    inference::rms_norm_into(
6296                        &h1,
6297                        &self.weights.final_norm,
6298                        self.rms_eps,
6299                        self.norm_style,
6300                        &mut self.ws.n1,
6301                    );
6302                    let mut logits1 = self.lm_head_forward(&self.ws.n1);
6303                    let t_after = sampler::sample_with_scratch_pool(
6304                        &logits1,
6305                        &self.sampler_config,
6306                        self.sampler_config.penalty_past(&all_ids, bounded_native),
6307                        &mut self.rng,
6308                        &mut self.sampler_scratch,
6309                        self.pool.as_deref(),
6310                    );
6311                    if self.confidence_on {
6312                        confidence.push(sampler::top1_prob_pool(
6313                            self.pool.as_deref(),
6314                            &mut self.sampler_scratch,
6315                            &logits1,
6316                            t_after,
6317                            calib_temp,
6318                        ));
6319                    }
6320                    attention::recycle_buf(&mut logits1);
6321                    if trace_on {
6322                        // Speculative decode is mutually exclusive with
6323                        // dynamic routing (router is None here) — no skill.
6324                        traces.push(TokenTrace {
6325                            t: generated,
6326                            token_id: t_after,
6327                            confidence: confidence.last().copied().unwrap_or(0.0),
6328                            active_skill: None,
6329                            recon: None,
6330                            switched: false,
6331                        });
6332                    }
6333                    let stop = !commit!(t_after);
6334
6335                    if t_after == draft {
6336                        accepted += 1;
6337                        self.commit_linear_scratch();
6338                        let _ = self.mtp_step(m, &h1, t_after, next_pos);
6339                        hidden = h2;
6340                        next_pos += 2;
6341                    } else {
6342                        // The draft lane is wrong: roll its KV entry back.
6343                        for layer in &mut self.kv_cache.layers {
6344                            layer.truncate_last(1);
6345                        }
6346                        if !stop {
6347                            let _ = self.mtp_step(m, &h1, t_after, next_pos);
6348                            hidden = self.forward_layers(
6349                                &self.embed_single(t_after),
6350                                next_pos + 1,
6351                                None,
6352                            );
6353                        }
6354                        next_pos += 2;
6355                    }
6356                    if stop {
6357                        break 'decode;
6358                    }
6359                }
6360                // ── Vanilla: forward the sampled token ──
6361                _ => {
6362                    // ── DeepSeek-V4 speculative decode (CMF_DSV4_SPEC=1):
6363                    // draft five on the card, verify batched, commit the
6364                    // accepted prefix. Greedy only; a rejected token's state
6365                    // is restored and replayed, so output equals the walk. ──
6366                    #[cfg(feature = "gpu")]
6367                    if Self::dsv4_spec_on() && self.dsv4.is_some() {
6368                        static SAID: std::sync::Once = std::sync::Once::new();
6369                        SAID.call_once(|| {
6370                            eprintln!(
6371                                "dsv4-spec gate: mtp={} mask={} router={} trace={} temp={} rep={} ",
6372                                !self.dsv4_mtp.is_empty(),
6373                                task_mask.is_none(),
6374                                router.is_none(),
6375                                !trace_on,
6376                                self.sampler_config.temperature < 1e-6,
6377                                self.sampler_config.repetition_penalty == 1.0,
6378                            );
6379                        });
6380                    }
6381                    #[cfg(feature = "gpu")]
6382                    if Self::dsv4_spec_on()
6383                        && self.dsv4.is_some()
6384                        && !self.dsv4_mtp.is_empty()
6385                        && task_mask.is_none()
6386                        && router.is_none()
6387                        && !trace_on
6388                        && self.sampler_config.temperature < 1e-6
6389                        && self.sampler_config.repetition_penalty == 1.0
6390                        && generated + 1 < max_tokens
6391                        && all_ids.len() >= 2
6392                        && generated >= dsv4_spec_retry_at
6393                    {
6394                        let tip_token = all_ids[all_ids.len() - 2];
6395                        let drafted0 = drafted;
6396                        let round = self.dsv4_spec_step(
6397                            tip_token,
6398                            t_next,
6399                            next_pos,
6400                            max_tokens.saturating_sub(generated),
6401                            &mut drafted,
6402                            &mut accepted,
6403                        );
6404                        if drafted > drafted0 {
6405                            let useful = round.as_ref().is_some_and(|(extra, _)| !extra.is_empty());
6406                            if useful {
6407                                dsv4_spec_bad = 0;
6408                            } else {
6409                                dsv4_spec_bad += 1;
6410                                if dsv4_spec_bad >= 2 {
6411                                    dsv4_spec_bad = 0;
6412                                    dsv4_spec_retry_at = generated.saturating_add(32);
6413                                    tracing::info!(
6414                                        "dsv4: the draft did not pay off twice — exact walk for 32 tokens"
6415                                    );
6416                                }
6417                            }
6418                        }
6419                        if let Some((extra, n_pos)) = round {
6420                            next_pos = n_pos;
6421                            let mut stopped = false;
6422                            for &id in &extra {
6423                                if self.confidence_on {
6424                                    confidence.push(0.0);
6425                                }
6426                                if !commit!(id) {
6427                                    stopped = true;
6428                                    break;
6429                                }
6430                            }
6431                            if stopped {
6432                                break 'decode;
6433                            }
6434                            continue 'decode;
6435                        }
6436                    }
6437                    self.graph_want_logits = fuse_lm;
6438                    // Greedy burst (CMF_MULTISTEP, off unless explicitly set): while
6439                    // nothing observes per-token state — pure argmax sampling,
6440                    // no router/trace/confidence/mask — decode k tokens per
6441                    // submit and commit them wholesale. The trailing normal
6442                    // forward leaves logits for the loop top, as always.
6443                    let mut t_fwd = t_next;
6444                    let pure_greedy = self.sampler_config.temperature < 1e-6
6445                        && self.sampler_config.repetition_penalty == 1.0
6446                        && self.sampler_config.presence_penalty == 0.0
6447                        && self.sampler_config.suppress_tokens.is_empty();
6448                    // Off by default: at every k the burst measured at or
6449                    // below the plain path on this graph shape (k=1 loses
6450                    // the argmax dispatches vs a 1 MB readback, k>=8 loses
6451                    // inter-step drains vs the saved sync). Experimental.
6452                    let burst_k = std::env::var("CMF_MULTISTEP")
6453                        .ok()
6454                        .and_then(|v| v.parse::<usize>().ok())
6455                        .unwrap_or(0);
6456                    if pure_greedy
6457                        && burst_k >= 1
6458                        && fuse_lm
6459                        && task_mask.is_none()
6460                        && router.is_none()
6461                        && !trace_on
6462                        && !self.confidence_on
6463                        // Diagnostics and teacher forcing observe every
6464                        // individual logit row.  A device-only burst returns
6465                        // only ids, so decline it rather than silently
6466                        // skipping requested evidence or forced ids.
6467                        && std::env::var_os("CMF_FORCE_IDS").is_none()
6468                        && std::env::var_os("CMF_LOGIT_DUMP").is_none()
6469                        && std::env::var_os("CMF_LOGIT_DUMP_ALL").is_none()
6470                    {
6471                        let mut stopped = false;
6472                        loop {
6473                            // The outer loop checks cancellation too, but a
6474                            // long run can stay in this burst loop for many
6475                            // submits.  Bound cancellation latency to one
6476                            // exact k-token frame.
6477                            if self
6478                                .cancel
6479                                .swap(false, std::sync::atomic::Ordering::Relaxed)
6480                            {
6481                                finish_reason = "cancelled".to_string();
6482                                stopped = true;
6483                                break;
6484                            }
6485                            let room = max_tokens.saturating_sub(generated);
6486                            if room <= 2 {
6487                                break;
6488                            }
6489                            let k = burst_k.min(room - 1);
6490                            if k < 1 {
6491                                break;
6492                            }
6493                            let Some(ids) = self.try_multi_burst(t_fwd, next_pos, k) else {
6494                                if self
6495                                    .graph_failed
6496                                    .swap(false, std::sync::atomic::Ordering::Relaxed)
6497                                {
6498                                    self.finish_generation(&mut mtp, &mut router, true);
6499                                    return Err(
6500                                        "GPU token graph failed during greedy burst".to_string()
6501                                    );
6502                                }
6503                                break;
6504                            };
6505                            next_pos += k;
6506                            for &id in &ids {
6507                                if !commit!(id) {
6508                                    stopped = true;
6509                                    break;
6510                                }
6511                            }
6512                            if stopped {
6513                                break;
6514                            }
6515                            t_fwd = *ids.last().unwrap();
6516                        }
6517                        if stopped {
6518                            break 'decode;
6519                        }
6520                    }
6521                    // Metal: keep the draft head's cache in step through
6522                    // the trial's plain phase and a paused speculation —
6523                    // the pair (hidden, t_fwd) at next_pos−1, the step the
6524                    // round's draft 0 would take. Without it the head's
6525                    // cache lagged the trunk by every plain token for the
6526                    // rest of the generation: the batched warm-up declined
6527                    // every later round and its rows went one by one (a
6528                    // whole MTP step per accepted token), and the drafts
6529                    // attended a context with those tokens missing.
6530                    #[cfg(target_os = "macos")]
6531                    if graph_spec
6532                        && spec_watchdog_off
6533                        && next_pos > 0
6534                        && self.mtp_graph_mode == Some(true)
6535                        && crate::gpu::q1_force()
6536                    {
6537                        if let Some(m) = mtp.as_mut() {
6538                            let _ = self.mtp_step_metal(m, &hidden, t_fwd, next_pos - 1, false);
6539                        }
6540                    }
6541                    hidden = self.forward_layers(&self.embed_single(t_fwd), next_pos, task_mask);
6542                    next_pos += 1;
6543                    // Dynamic routing: the forward updated φ; ask the
6544                    // router whether to switch skills before the next token.
6545                    if let Some(r) = &mut router {
6546                        let phi = self.dyn_phi_ema.clone();
6547                        let decision = r.step(&phi, generated);
6548                        if let Some(new_active) = decision {
6549                            let _ = self.set_active_skill(new_active);
6550                        }
6551                        // Backfill this token's coherence + switch flag from
6552                        // the just-run eval (freshest measured values).
6553                        if trace_on {
6554                            if let Some(last) = traces.last_mut() {
6555                                let e = r.last_best_e();
6556                                last.recon = e.is_finite().then_some(e);
6557                                last.switched = decision.is_some();
6558                            }
6559                        }
6560                    }
6561                }
6562            }
6563        }
6564
6565        let cancelled = finish_reason == "cancelled";
6566        // A generation during which the router switched weights holds no
6567        // state any single overlay would produce (each switch cleared the
6568        // cache mid-sequence), so it leaves no reuse key behind either.
6569        let dyn_switched = router.as_ref().is_some_and(|r| !r.switches.is_empty());
6570        if mimo_spec {
6571            if let Some(st) = self.mimo_mtp.as_ref() {
6572                let line = st.stats.line();
6573                tracing::info!("{line}");
6574                if std::env::var_os("CMF_MIMO_MTP_STATS").is_some() {
6575                    eprintln!("{line}");
6576                }
6577            }
6578        }
6579        self.finish_generation(&mut mtp, &mut router, cancelled);
6580
6581        let output_ids = &all_ids[input_ids.len()..];
6582        // Forwarded = prompt + all generated but the LAST sampled token
6583        // (emitted without being fed back). Exact only without MTP —
6584        // reuse is gated off when MTP is active.
6585        let forwarded = input_ids.len() + output_ids.len().saturating_sub(1);
6586        // A MiMo speculative round that stopped on an accepted draft (EOS,
6587        // cancel) leaves verify rows past the committed stream in the cache:
6588        // never offer that cache for reuse. Neither does a router that
6589        // switched weights mid-sequence (no single overlay produced it).
6590        let consumed = std::mem::take(&mut all_ids);
6591        if dyn_switched {
6592            self.clear_sequence_state();
6593        } else if cancelled || mimo_spec || prompt_rows.is_some() {
6594            self.clear_history();
6595        } else {
6596            self.record_consumed_prefix(&consumed[..forwarded.min(consumed.len())], reuse_from);
6597        }
6598        all_ids = consumed;
6599        let output_ids = &all_ids[input_ids.len()..];
6600        confidence.truncate(output_ids.len()); // guard against any overshoot
6601        traces.truncate(output_ids.len());
6602        Ok(GenerateResult {
6603            text: self.tokenizer.decode(output_ids),
6604            token_ids: output_ids.to_vec(),
6605            prompt_tokens: input_ids.len(),
6606            tokens_generated: generated,
6607            finish_reason,
6608            mtp_drafted: drafted,
6609            mtp_accepted: accepted,
6610            token_confidence: confidence,
6611            traces,
6612        })
6613    }
6614
6615    /// One MTP step: feed `(hidden_p, token_{p+1})` into the draft head,
6616    /// advance its KV cache at position `p`, return the drafted token
6617    /// for position `p+2`.
6618    fn mtp_step(
6619        &mut self,
6620        m: &mut MtpModule,
6621        hidden: &[f32],
6622        next_token: u32,
6623        position: usize,
6624    ) -> u32 {
6625        self.mtp_step_h(m, hidden, next_token, position).0
6626    }
6627
6628    /// Tally for `CMF_MTP_CHAIN_PROBE`: per depth, how often the CHAIN is
6629    /// still an exact prefix of the real continuation. Printed every 128
6630    /// depth-0 samples so a killed run still shows its table.
6631    fn chain_probe_note(depth: usize, prefix_ok: bool) {
6632        use std::sync::Mutex;
6633        static T: Mutex<Vec<(u64, u64)>> = Mutex::new(Vec::new());
6634        let mut t = T.lock().unwrap();
6635        if t.len() <= depth {
6636            t.resize(depth + 1, (0, 0));
6637        }
6638        t[depth].0 += 1;
6639        t[depth].1 += prefix_ok as u64;
6640        if depth == 0 && t[0].0 % 128 == 0 {
6641            let line: Vec<String> = t
6642                .iter()
6643                .enumerate()
6644                .map(|(d, (n, k))| {
6645                    format!(
6646                        "d{}={:.0}%({n})",
6647                        d + 1,
6648                        100.0 * *k as f64 / (*n).max(1) as f64
6649                    )
6650                })
6651                .collect();
6652            eprintln!("mtp-chain: {}", line.join(" "));
6653        }
6654    }
6655
6656    /// `mtp_step` that also hands back the block's own output hidden — the
6657    /// state a CHAINED draft feeds the next step, the way a multi-token
6658    /// speculative round iterates the head on itself.
6659    /// One MTP block step from (trunk hidden, token): the head's LOGITS
6660    /// and the block's own hidden for chaining. The draft is argmax of the
6661    /// logits on the greedy path and a draw from their post-chain
6662    /// distribution on the sampling path.
6663    fn mtp_step_hl(
6664        &mut self,
6665        m: &mut MtpModule,
6666        hidden: &[f32],
6667        next_token: u32,
6668        position: usize,
6669    ) -> (Vec<f32>, Vec<f32>) {
6670        // The graph arm: the MTP block as a one-layer token graph with the
6671        // head fused — device attention over the block's own KV mirror,
6672        // one submit for block + head, hidden and logits back together.
6673        // Decided once per generation (see `mtp_graph_mode`).
6674        #[cfg(target_os = "macos")]
6675        if self.mtp_graph_mode != Some(false) && crate::gpu::q1_force() {
6676            if let Some(r) = self.mtp_step_metal(m, hidden, next_token, position, true) {
6677                self.mtp_graph_mode = Some(true);
6678                return r;
6679            }
6680            if self.mtp_graph_mode == Some(true) {
6681                tracing::error!("mtp Metal graph failed after admission");
6682                self.clear_sequence_state();
6683                self.graph_failed
6684                    .store(true, std::sync::atomic::Ordering::Relaxed);
6685                self.cancel
6686                    .store(true, std::sync::atomic::Ordering::Relaxed);
6687                return (Vec::new(), Vec::new());
6688            }
6689            self.mtp_graph_mode = Some(false);
6690        }
6691        #[cfg(feature = "gpu")]
6692        if self.mtp_graph_mode != Some(false) {
6693            if !self.mtp_graph_ok(m) {
6694                if self.mtp_graph_mode == Some(true) {
6695                    // A mirror was already admitted, so a capability change
6696                    // cannot safely switch this request to the stale CPU
6697                    // cache.  Keep the same terminal contract as a failed
6698                    // token graph.
6699                    tracing::error!("mtp graph became unavailable after admission");
6700                    self.clear_sequence_state();
6701                    self.graph_failed
6702                        .store(true, std::sync::atomic::Ordering::Relaxed);
6703                    self.cancel
6704                        .store(true, std::sync::atomic::Ordering::Relaxed);
6705                    return (Vec::new(), Vec::new());
6706                }
6707                self.mtp_graph_mode = Some(false);
6708            } else {
6709                if let Some(r) = self.mtp_step_graph(m, hidden, next_token, position) {
6710                    self.mtp_graph_mode = Some(true);
6711                    return r;
6712                }
6713                if self.graph_failed.load(std::sync::atomic::Ordering::Relaxed) {
6714                    // A token graph can have admitted a persistent MTP/GDN
6715                    // mirror before its readback failed.  The CPU MTP cache
6716                    // is not a valid continuation in that state; leave the
6717                    // flag set so the generation caller returns through its
6718                    // terminal error path instead of silently switching
6719                    // arithmetic.
6720                    return (Vec::new(), Vec::new());
6721                }
6722                // `mtp_graph_ok` was true, so a None here means a refusal or
6723                // failure after graph admission.  Do not fall through to a
6724                // CPU cache whose rows may lag the device mirror.
6725                tracing::error!("mtp graph failed or declined after admission");
6726                self.clear_sequence_state();
6727                self.graph_failed
6728                    .store(true, std::sync::atomic::Ordering::Relaxed);
6729                self.cancel
6730                    .store(true, std::sync::atomic::Ordering::Relaxed);
6731                return (Vec::new(), Vec::new());
6732            }
6733        }
6734        // fc concat order is [enorm(embed); hnorm(hidden)] — EMBEDDING
6735        // FIRST. Verified by the oracle (converter/mtp_oracle.py):
6736        // [emb;hid] → 45.8% acceptance, [hid;emb] → 0.00%.
6737        let e = self.embed_single(next_token);
6738        let mut cat = vec![0.0f32; 2 * self.hidden_size];
6739        let (cat_e, cat_h) = cat.split_at_mut(self.hidden_size);
6740        inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
6741        inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
6742        let mut x = vec![0.0f32; self.hidden_size];
6743        m.eh_proj.matvec(&cat, &mut x, self.pool.as_deref());
6744
6745        // One standard transformer block over the MTP's own cache.
6746        let lw = &m.layer;
6747        inference::rms_norm_into(
6748            &x,
6749            &lw.input_norm,
6750            self.rms_eps,
6751            self.norm_style,
6752            &mut self.ws.n1,
6753        );
6754        let attn = match &lw.attn {
6755            // MLA models carry no MTP head; this path cannot see them.
6756            AttnKind::Mla(_) => unreachable!("MLA has no MTP/pair path"),
6757            AttnKind::Kda(_) => unreachable!("KDA has no MTP/pair path"),
6758            AttnKind::Full {
6759                wq,
6760                wk,
6761                wv,
6762                wo,
6763                q_norm,
6764                k_norm,
6765                output_gate,
6766                softplus_gate,
6767                bias,
6768            } => {
6769                let mut cfg = self.attn_cfg(position);
6770                cfg.q_norm = q_norm.as_deref();
6771                cfg.k_norm = k_norm.as_deref();
6772                cfg.output_gate = *output_gate;
6773                cfg.softplus_gate = softplus_gate
6774                    .as_ref()
6775                    .map(|(gate, per_head)| (gate, *per_head));
6776                cfg.bias = bias
6777                    .as_ref()
6778                    .map(|(q, k, v)| (q.as_slice(), k.as_slice(), v.as_slice()));
6779                attention::qwen_attention(&self.ws.n1, wq, wk, wv, wo, &mut m.kv, &cfg)
6780            }
6781            AttnKind::Linear(_)
6782            | AttnKind::LinearGdn(_)
6783            | AttnKind::ShortConv(_)
6784            | AttnKind::Bounded(_) => {
6785                unreachable!("MTP block is full attention")
6786            }
6787        };
6788        for (i, &a) in attn.iter().enumerate() {
6789            x[i] += a;
6790        }
6791        inference::rms_norm_into(
6792            &x,
6793            &lw.post_norm,
6794            self.rms_eps,
6795            self.norm_style,
6796            &mut self.ws.p1,
6797        );
6798        let ffn = ffn_forward(&lw.ffn, &self.ws.p1, self.pool.as_deref(), None);
6799        for (i, &f) in ffn.iter().enumerate() {
6800            x[i] += f;
6801        }
6802
6803        inference::rms_norm_into(
6804            &x,
6805            &m.final_norm,
6806            self.rms_eps,
6807            self.norm_style,
6808            &mut self.ws.n1,
6809        );
6810        let lg = self.lm_head_forward(&self.ws.n1);
6811        (lg, x)
6812    }
6813
6814    /// `mtp_step_hl` reduced to the greedy draft: argmax of the head.
6815    fn mtp_step_h(
6816        &mut self,
6817        m: &mut MtpModule,
6818        hidden: &[f32],
6819        next_token: u32,
6820        position: usize,
6821    ) -> (u32, Vec<f32>) {
6822        let (mut lg, x) = self.mtp_step_hl(m, hidden, next_token, position);
6823        let draft = sampler::argmax(&lg);
6824        attention::recycle_buf(&mut lg);
6825        (draft, x)
6826    }
6827
6828    /// One speculative round for the trial: rounds 1..5 of a `Spec` phase
6829    /// advance it (the monitor already averaged this round); after five,
6830    /// the plain phase runs (once — a known plain rate decides at once);
6831    /// a decided speculation keeps re-checking the rule every round and
6832    /// stops after four losing rounds in a row.
6833    fn spec_trial_round(trial: SpecTrial, mon: &mut SpecMon, generated: usize) -> SpecTrial {
6834        match trial {
6835            SpecTrial::Spec { t0, gen0, rounds } => {
6836                let rounds = rounds + 1;
6837                // A round that accepts no draft still has to run the MTP
6838                // chain and a K+1 verifier, but emits only its verifier tip.
6839                // After two such real rounds the speculative arm cannot win
6840                // on any backend; time plain immediately rather than spend
6841                // the remaining exploratory rounds on the same one-token
6842                // result. A later 128-token re-check still gives changing
6843                // context a chance to re-admit it.
6844                if mon.repeated_zero_acceptance() {
6845                    tracing::info!(
6846                        "speculation trial: no accepted drafts in two rounds — timing plain"
6847                    );
6848                    return SpecTrial::Plain {
6849                        t0: std::time::Instant::now(),
6850                        gen0: generated,
6851                    };
6852                }
6853                if rounds >= 5 {
6854                    if mon.plain_ms > 0.0 {
6855                        let keep = mon.pays();
6856                        mon.fails = 0;
6857                        tracing::info!(
6858                            "speculation re-check: {:.2} tok/round in {:.1} ms vs plain {:.1} ms/tok — {}",
6859                            mon.tokens,
6860                            mon.round_ms,
6861                            mon.plain_ms,
6862                            if keep { "speculating" } else { "plain" }
6863                        );
6864                        SpecTrial::Decided {
6865                            spec: keep,
6866                            recheck_at: if keep { usize::MAX } else { generated + 128 },
6867                        }
6868                    } else if mon.pays() {
6869                        // Metal: the rounds land enough tokens each that no
6870                        // plain measurement is needed — keep speculating,
6871                        // and re-check every round (a losing streak sends
6872                        // the loop to the plain phase, below).
6873                        mon.fails = 0;
6874                        tracing::info!(
6875                            "speculation trial: {:.2} tok/round in {:.1} ms — speculating (plain not timed)",
6876                            mon.tokens,
6877                            mon.round_ms,
6878                        );
6879                        SpecTrial::Decided {
6880                            spec: true,
6881                            recheck_at: usize::MAX,
6882                        }
6883                    } else {
6884                        SpecTrial::Plain {
6885                            t0: std::time::Instant::now(),
6886                            gen0: generated,
6887                        }
6888                    }
6889                } else {
6890                    SpecTrial::Spec { t0, gen0, rounds }
6891                }
6892            }
6893            SpecTrial::Decided { spec: true, .. } => {
6894                if mon.pays() {
6895                    mon.fails = 0;
6896                    trial
6897                } else {
6898                    mon.fails += 1;
6899                    if mon.fails >= 4 {
6900                        if mon.plain_ms <= 0.0 {
6901                            // Metal, plain never timed: four doubtful rounds
6902                            // buy the (bounded) plain measurement, and the
6903                            // exact rule decides from it.
6904                            tracing::info!(
6905                                "speculation doubtful: {:.2} tok/round in {:.1} ms — timing plain",
6906                                mon.tokens,
6907                                mon.round_ms,
6908                            );
6909                            return SpecTrial::Plain {
6910                                t0: std::time::Instant::now(),
6911                                gen0: generated,
6912                            };
6913                        }
6914                        tracing::info!(
6915                            "speculation stopped: {:.2} tok/round in {:.1} ms vs plain {:.1} ms/tok",
6916                            mon.tokens,
6917                            mon.round_ms,
6918                            mon.plain_ms
6919                        );
6920                        SpecTrial::Decided {
6921                            spec: false,
6922                            recheck_at: generated + 128,
6923                        }
6924                    } else {
6925                        trial
6926                    }
6927                }
6928            }
6929            other => other,
6930        }
6931    }
6932
6933    /// The MTP block's device-mirror id: the trunk's id with a high bit,
6934    /// so the (kv_id, layer) mirror keys never collide.
6935    fn mtp_kv_id(&self) -> u64 {
6936        self.graph_kv_id | (1u64 << 40)
6937    }
6938
6939    /// The MTP block's mirror layer index: 0 — its own kv_id keeps it
6940    /// apart from the trunk, and the BATCH graph (the warm-up path) keys
6941    /// its mirrors at layer 0 with no base of its own, so the draft's
6942    /// token graph must key the same slot.
6943    const MTP_LAYER_BASE: usize = 0;
6944
6945    /// The wgpu MTP draft writes speculative rows straight into its device
6946    /// mirror while the CPU owner retains only the real prompt/decode anchor.
6947    /// After verification, move that mirror cursor back to the anchor before
6948    /// replaying accepted pairs.  The next graph append then sees the same
6949    /// contiguous position as the CPU/Metal path without uploading stale
6950    /// speculative rows.
6951    #[cfg(feature = "gpu")]
6952    fn rewind_mtp_graph_mirror(&self, stored: usize) -> bool {
6953        self.mtp_graph_mode != Some(true)
6954            || crate::gpu::graph_kv_set_stored(self.mtp_kv_id(), Self::MTP_LAYER_BASE, stored)
6955    }
6956
6957    /// A speculative verify graph appends the full `k+1` trunk rows before
6958    /// the acceptance count is known.  GDN state already has a snapshot
6959    /// restore; Full-attention mirrors need the matching logical cursor
6960    /// rewind so the next graph call does not reject an ahead-of-position KV
6961    /// cache after a partial acceptance.
6962    #[cfg(feature = "gpu")]
6963    fn rewind_trunk_graph_mirrors(&self, stored: usize) -> bool {
6964        let mut ok = true;
6965        let mut expected = false;
6966        for li in 0..self.num_layers {
6967            if matches!(
6968                self.weights.layers[self.phys_layer(li)].attn,
6969                AttnKind::Full { .. }
6970            ) {
6971                expected = true;
6972                ok &= crate::gpu::graph_kv_set_stored(self.graph_kv_id, li, stored);
6973            }
6974        }
6975        !expected || ok
6976    }
6977
6978    /// Count the recurrent layers participating in the trunk verify graph.
6979    /// Snapshot restore is all-or-nothing across that set; deriving the count
6980    /// from the model keeps the restore contract valid for looped models too.
6981    fn graph_gdn_layer_count(&self) -> usize {
6982        (0..self.num_layers)
6983            .filter(|&li| {
6984                matches!(
6985                    &self.weights.layers[self.phys_layer(li)].attn,
6986                    AttnKind::LinearGdn(_)
6987                )
6988            })
6989            .count()
6990    }
6991
6992    /// The block's input from (trunk hidden, token): eh_proj · [enorm(e);
6993    /// hnorm(h)] — the same arithmetic the per-op path starts with.
6994    fn mtp_block_input(&mut self, m: &MtpModule, hidden: &[f32], next_token: u32) -> Vec<f32> {
6995        let e = self.embed_single(next_token);
6996        let mut cat = vec![0.0f32; 2 * self.hidden_size];
6997        let (cat_e, cat_h) = cat.split_at_mut(self.hidden_size);
6998        inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
6999        inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
7000        let mut x = vec![0.0f32; self.hidden_size];
7001        m.eh_proj.matvec(&cat, &mut x, self.pool.as_deref());
7002        x
7003    }
7004
7005    /// Is the MTP block graphable at all (device up, full attention
7006    /// without softplus, dense FFN)? The plan itself is built per call.
7007    #[cfg(feature = "gpu")]
7008    fn mtp_block_graph_ok(&self, m: &MtpModule) -> bool {
7009        if std::env::var("CMF_MTP_GRAPH").as_deref() == Ok("0") {
7010            return false;
7011        }
7012        if !crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode)
7013            || !crate::gpu::enabled_here()
7014            || self.attn_softcap > 0.0
7015            || self.attention_heads_per_layer.is_some()
7016            // The block graph caches V as wide as K and feeds o_proj
7017            // nh·head_dim; a narrow-V model keeps its MTP block per-op.
7018            || self.v_head_dim.is_some()
7019        {
7020            return false;
7021        }
7022        matches!(
7023            &m.layer.attn,
7024            AttnKind::Full {
7025                softplus_gate: None,
7026                ..
7027            }
7028        ) && matches!(&m.layer.ffn, FfnKind::Dense(_))
7029    }
7030
7031    /// Full MTP token-graph eligibility, including the fused lm-head and all
7032    /// block projection weights.  Keep this distinct from the block-only
7033    /// check: prompt warm-up does not need the head, while a draft step does.
7034    #[cfg(feature = "gpu")]
7035    fn mtp_graph_ok(&self, m: &MtpModule) -> bool {
7036        if !self.mtp_block_graph_ok(m) {
7037            return false;
7038        }
7039        let AttnKind::Full { wq, wk, wv, wo, .. } = &m.layer.attn else {
7040            return false;
7041        };
7042        let FfnKind::Dense(d) = &m.layer.ffn else {
7043            return false;
7044        };
7045        d.segs.is_empty()
7046            && wq.graph_weight().is_some()
7047            && wk.graph_weight().is_some()
7048            && wv.graph_weight().is_some()
7049            && wo.graph_weight().is_some()
7050            && d.gate_proj.graph_weight().is_some()
7051            && d.up_proj.graph_weight().is_some()
7052            && d.down_proj.graph_weight().is_some()
7053            && self.weights.lm_head.graph_weight().is_some()
7054    }
7055
7056    /// One MTP block step on the wgpu token graph: block + fused head in
7057    /// one submit, the block hidden and the logits read back together.
7058    /// None = the graph cannot take this block (softplus gate, non-dense
7059    /// FFN, unquantized head, no device) — the caller keeps the per-op
7060    /// path for the whole generation.
7061    #[cfg(feature = "gpu")]
7062    fn mtp_step_graph(
7063        &mut self,
7064        m: &mut MtpModule,
7065        hidden: &[f32],
7066        next_token: u32,
7067        position: usize,
7068    ) -> Option<(Vec<f32>, Vec<f32>)> {
7069        if !self.mtp_graph_ok(m) {
7070            return None;
7071        }
7072        let lw = &m.layer;
7073        let AttnKind::Full {
7074            wq,
7075            wk,
7076            wv,
7077            wo,
7078            q_norm,
7079            k_norm,
7080            output_gate,
7081            softplus_gate,
7082            bias,
7083        } = &lw.attn
7084        else {
7085            return None;
7086        };
7087        if softplus_gate.is_some() {
7088            return None;
7089        }
7090        let FfnKind::Dense(d) = &lw.ffn else {
7091            return None;
7092        };
7093        if !d.segs.is_empty() {
7094            return None; // tube layers run on the segmented path
7095        }
7096        // The block's input first: it borrows `self` mutably (embed scratch,
7097        // pool), the plan below borrows the weights immutably.
7098        let mut x = self.mtp_block_input(m, hidden, next_token);
7099        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
7100            let (_, i, kind, rs) = t.graph_weight()?;
7101            Some(crate::gpu::GraphW {
7102                idx: i,
7103                kind,
7104                row_scale: rs,
7105                data: &[],
7106                prism: crate::gpu::GraphPrismOp::None,
7107                affine: false,
7108            })
7109        }
7110        let (model, _, _, _) = wq.graph_weight()?;
7111        let model = model.clone();
7112        let (lm_gw, lm_rows) = {
7113            let (_, i, kind, rs) = self.weights.lm_head.graph_weight()?;
7114            // The draft's head over the CMF_DRAFT_VOCAB shortlist (the same
7115            // cut the native Metal draft takes): 662 MB a step on Qwen3.8
7116            // becomes 170 MB at 65536; the verify keeps the full head.
7117            let rows = if kind == 6 {
7118                self.draft_head_rows(self.weights.lm_head.rows())
7119            } else {
7120                self.weights.lm_head.rows()
7121            };
7122            (
7123                crate::gpu::GraphW {
7124                    idx: i,
7125                    kind,
7126                    row_scale: rs,
7127                    data: &[],
7128                    prism: crate::gpu::GraphPrismOp::None,
7129                    affine: false,
7130                },
7131                rows,
7132            )
7133        };
7134        let layer = crate::gpu::GraphLayer {
7135            input_norm: &lw.input_norm,
7136            attn: crate::gpu::GraphAttn::Full {
7137                wq: gw(wq)?,
7138                wk: gw(wk)?,
7139                wv: gw(wv)?,
7140                wo: gw(wo)?,
7141                q_norm: q_norm.as_deref(),
7142                k_norm: k_norm.as_deref(),
7143                late_qk_norm: self.qk_norm_after_rope,
7144                bias: bias
7145                    .as_ref()
7146                    .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
7147                output_gate: *output_gate,
7148                cpu_k: m.kv.k_heads(),
7149                cpu_v: m.kv.v_heads(),
7150                cpu_base: m.kv.base(),
7151                geom: None,
7152                head_gate: None,
7153            },
7154            post_norm: &lw.post_norm,
7155            attn_out_norm: None,
7156            ffn_out_norm: None,
7157            layer_scale: None,
7158            ffn: crate::gpu::GraphFfn::Dense {
7159                gate: gw(&d.gate_proj)?,
7160                up: gw(&d.up_proj)?,
7161                down: gw(&d.down_proj)?,
7162                act: d.act.graph_act()?,
7163            },
7164        };
7165        let nh = self.num_heads;
7166        let (nkv, hd, rd) = self.layer_geom(0);
7167        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
7168        let mut logits = Vec::new();
7169        let ok = crate::gpu::forward_token_graph(
7170            &model,
7171            self.mtp_kv_id(),
7172            std::slice::from_ref(&layer),
7173            &[None],
7174            self.o1_epoch,
7175            &self.inv_freq,
7176            &mut x,
7177            nh,
7178            nkv,
7179            hd,
7180            self.attn_scale,
7181            rd,
7182            self.hidden_size,
7183            // An MTP draft can have an independently compacted FFN. The
7184            // graph must use the checked local tensor width, not the trunk
7185            // arch-wide intermediate size.
7186            d.gate_proj.rows(),
7187            position,
7188            self.kv_cache.max_seq_len,
7189            gemma,
7190            self.rms_eps as f32,
7191            Some((&lm_gw, lm_rows)),
7192            &m.final_norm,
7193            &mut logits,
7194            &[],
7195            1,
7196            None,
7197            None,
7198            None,
7199            Self::MTP_LAYER_BASE,
7200            true,
7201        );
7202        match ok {
7203            crate::gpu::TokenGraphOutcome::Completed => {}
7204            crate::gpu::TokenGraphOutcome::Declined => return None,
7205            crate::gpu::TokenGraphOutcome::Failed => {
7206                // The backend has already admitted persistent state.  Keep
7207                // this distinct from a capability refusal so the caller
7208                // cannot switch to the stale CPU MTP cache.
7209                self.clear_sequence_state();
7210                self.graph_failed
7211                    .store(true, std::sync::atomic::Ordering::Relaxed);
7212                self.cancel
7213                    .store(true, std::sync::atomic::Ordering::Relaxed);
7214                return None;
7215            }
7216        }
7217        // The graph may deliberately evaluate only a prefix of the draft
7218        // head.  Do not turn every omitted token into a zero-logit candidate:
7219        // that can win an all-negative shortlist and collapses acceptance.
7220        pad_mtp_draft_logits(&mut logits, self.vocab_size);
7221        Some((logits, x))
7222    }
7223
7224    /// The warm-ups of one speculative round on the device: every accepted
7225    /// (hidden, token) pair as ONE batched graph run over the MTP block
7226    /// (no head) — its kv_append lands the pairs in the block's mirror.
7227    /// `pairs` are consecutive positions from `first_pos`.  The tri-state
7228    /// result is intentional: a refusal before admission may use the
7229    /// per-row/CPU route, while a failure after admission must terminate the
7230    /// sequence rather than fall through to a stale CPU cache.
7231    #[cfg(feature = "gpu")]
7232    fn mtp_warm_graph(
7233        &mut self,
7234        m: &mut MtpModule,
7235        pairs: &[(&[f32], u32)],
7236        first_pos: usize,
7237    ) -> crate::gpu::BatchGraphOutcome {
7238        if pairs.is_empty() {
7239            return crate::gpu::BatchGraphOutcome::Completed;
7240        }
7241        if !self.mtp_block_graph_ok(m) {
7242            return crate::gpu::BatchGraphOutcome::Declined;
7243        }
7244        let hs = self.hidden_size;
7245        // Block inputs for every pair (eh_proj on the per-op path, one
7246        // matvec each — the plan's own prologue).
7247        let mut hiddens = Vec::with_capacity(pairs.len() * hs);
7248        for (h, t) in pairs {
7249            hiddens.extend_from_slice(&self.mtp_block_input(m, h, *t));
7250        }
7251        let lw = &m.layer;
7252        let AttnKind::Full {
7253            wq,
7254            wk,
7255            wv,
7256            wo,
7257            q_norm,
7258            k_norm,
7259            output_gate,
7260            bias,
7261            ..
7262        } = &lw.attn
7263        else {
7264            return crate::gpu::BatchGraphOutcome::Declined;
7265        };
7266        let FfnKind::Dense(d) = &lw.ffn else {
7267            return crate::gpu::BatchGraphOutcome::Declined;
7268        };
7269        if !d.segs.is_empty() {
7270            return crate::gpu::BatchGraphOutcome::Declined; // tube layers run on the segmented path
7271        }
7272        // The graph has no arm for a projected output gate, and only the
7273        // activations `graph_act` names.
7274        let (
7275            AttnKind::Full {
7276                softplus_gate: None,
7277                ..
7278            },
7279            Some(gact),
7280        ) = (&lw.attn, d.act.graph_act())
7281        else {
7282            return crate::gpu::BatchGraphOutcome::Declined;
7283        };
7284        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
7285            let (_, i, kind, rs) = t.graph_weight()?;
7286            Some(crate::gpu::GraphW {
7287                idx: i,
7288                kind,
7289                row_scale: rs,
7290                data: &[],
7291                prism: crate::gpu::GraphPrismOp::None,
7292                affine: false,
7293            })
7294        }
7295        let Some((model, _, _, _)) = wq.graph_weight() else {
7296            return crate::gpu::BatchGraphOutcome::Declined;
7297        };
7298        let model = model.clone();
7299        let (Some(gwq), Some(gwk), Some(gwv), Some(gwo), Some(gg), Some(gu), Some(gd)) = (
7300            gw(wq),
7301            gw(wk),
7302            gw(wv),
7303            gw(wo),
7304            gw(&d.gate_proj),
7305            gw(&d.up_proj),
7306            gw(&d.down_proj),
7307        ) else {
7308            return crate::gpu::BatchGraphOutcome::Declined;
7309        };
7310        let layer = crate::gpu::GraphLayer {
7311            input_norm: &lw.input_norm,
7312            attn: crate::gpu::GraphAttn::Full {
7313                wq: gwq,
7314                wk: gwk,
7315                wv: gwv,
7316                wo: gwo,
7317                q_norm: q_norm.as_deref(),
7318                k_norm: k_norm.as_deref(),
7319                late_qk_norm: self.qk_norm_after_rope,
7320                bias: bias
7321                    .as_ref()
7322                    .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
7323                output_gate: *output_gate,
7324                cpu_k: m.kv.k_heads(),
7325                cpu_v: m.kv.v_heads(),
7326                cpu_base: m.kv.base(),
7327                geom: None,
7328                head_gate: None,
7329            },
7330            post_norm: &lw.post_norm,
7331            attn_out_norm: None,
7332            ffn_out_norm: None,
7333            layer_scale: None,
7334            ffn: crate::gpu::GraphFfn::Dense {
7335                gate: gg,
7336                up: gu,
7337                down: gd,
7338                act: gact,
7339            },
7340        };
7341        let positions: Vec<usize> = (first_pos..first_pos + pairs.len()).collect();
7342        let nh = self.num_heads;
7343        let (nkv, hd, rd) = self.layer_geom(0);
7344        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
7345        crate::gpu::forward_batch_graph(
7346            &model,
7347            self.mtp_kv_id(),
7348            std::slice::from_ref(&layer),
7349            &self.inv_freq,
7350            &mut hiddens,
7351            nh,
7352            nkv,
7353            hd,
7354            rd,
7355            hs,
7356            // See `mtp_step_graph`: draft FFN geometry is local to this
7357            // block after independent MTP compaction.
7358            d.gate_proj.rows(),
7359            &positions,
7360            self.kv_cache.max_seq_len,
7361            gemma,
7362            self.rms_eps as f32,
7363            self.attn_scale,
7364            pairs.len(),
7365            &[],
7366            0,
7367            None,
7368            None,
7369        )
7370    }
7371
7372    /// Complete an MTP warm-up after the batched graph has refused.  A
7373    /// graphable block is retried one row at a time; once any device row has
7374    /// been admitted, a CPU fallback would observe a stale mirror, so every
7375    /// token-graph refusal is terminal.  If the block is not graphable and no
7376    /// mirror exists yet, warming on the CPU is safe and records the CPU mode
7377    /// for the rest of the generation.
7378    #[cfg(feature = "gpu")]
7379    fn mtp_warm_graph_fallback(
7380        &mut self,
7381        m: &mut MtpModule,
7382        pairs: &[(&[f32], u32)],
7383        first_pos: usize,
7384    ) -> bool {
7385        if pairs.is_empty() {
7386            return true;
7387        }
7388        let graphable = self.mtp_block_graph_ok(m);
7389        if !graphable {
7390            // A previously admitted mirror cannot be made coherent by
7391            // appending to the host cache.  The caller turns this into a
7392            // terminal generation error and clears both mirrors.
7393            if self.mtp_graph_mode == Some(true) {
7394                return false;
7395            }
7396            self.mtp_graph_mode = Some(false);
7397            for (j, (h, t)) in pairs.iter().enumerate() {
7398                self.mtp_warm(m, h, *t, first_pos + j);
7399            }
7400            return true;
7401        }
7402
7403        // The batch refusal is recoverable only through the same device
7404        // state.  Keep rows owned until each token graph has completed; a
7405        // None is treated as unsafe because the token-graph API deliberately
7406        // collapses its backend refusal/failure into that result.
7407        for (j, (h, t)) in pairs.iter().enumerate() {
7408            if self.mtp_step_graph(m, h, *t, first_pos + j).is_none() {
7409                return false;
7410            }
7411        }
7412        self.mtp_graph_mode = Some(true);
7413        true
7414    }
7415
7416    /// Warm a contiguous set of MTP pairs using the existing graph seam, with
7417    /// an all-or-nothing error contract for callers that already admitted the
7418    /// trunk batch.  The non-GPU build keeps the same pair accounting while
7419    /// using the established CPU warm path.
7420    #[cfg(feature = "gpu")]
7421    fn mtp_warm_prefill_pairs(
7422        &mut self,
7423        m: &mut MtpModule,
7424        pairs: &[(&[f32], u32)],
7425        first_pos: usize,
7426    ) -> Result<(), &'static str> {
7427        // Keep unsupported token-graph heads on the established CPU MTP
7428        // route before admitting any block mirror.  Once a device mirror is
7429        // active, the same condition is terminal because CPU rows cannot
7430        // repair its state.
7431        if self.mtp_graph_mode == Some(false) || !self.mtp_graph_ok(m) {
7432            if self.mtp_graph_mode == Some(true) {
7433                return Err("MTP token graph became unavailable after admission");
7434            }
7435            self.mtp_graph_mode = Some(false);
7436            for (j, (h, t)) in pairs.iter().enumerate() {
7437                self.mtp_warm(m, h, *t, first_pos + j);
7438            }
7439            return Ok(());
7440        }
7441        match self.mtp_warm_graph(m, pairs, first_pos) {
7442            crate::gpu::BatchGraphOutcome::Completed => {
7443                if !pairs.is_empty() {
7444                    self.mtp_graph_mode = Some(true);
7445                }
7446                Ok(())
7447            }
7448            crate::gpu::BatchGraphOutcome::Declined => {
7449                if self.mtp_warm_graph_fallback(m, pairs, first_pos) {
7450                    Ok(())
7451                } else {
7452                    Err("MTP warm-up fallback failed after device admission")
7453                }
7454            }
7455            crate::gpu::BatchGraphOutcome::Failed => {
7456                Err("MTP warm batch graph failed after admission")
7457            }
7458        }
7459    }
7460
7461    #[cfg(not(feature = "gpu"))]
7462    fn mtp_warm_prefill_pairs(
7463        &mut self,
7464        m: &mut MtpModule,
7465        pairs: &[(&[f32], u32)],
7466        first_pos: usize,
7467    ) -> Result<(), &'static str> {
7468        for (j, (h, t)) in pairs.iter().enumerate() {
7469            self.mtp_warm(m, h, *t, first_pos + j);
7470        }
7471        Ok(())
7472    }
7473
7474    /// The MTP block alone — advance its KV with a (hidden, token) pair the
7475    /// verify just proved, without paying the head. What keeps the draft's
7476    /// attention context warm between speculative rounds.
7477    fn mtp_warm(&mut self, m: &mut MtpModule, hidden: &[f32], next_token: u32, position: usize) {
7478        let e = self.embed_single(next_token);
7479        let mut cat = vec![0.0f32; 2 * self.hidden_size];
7480        let (cat_e, cat_h) = cat.split_at_mut(self.hidden_size);
7481        inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
7482        inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
7483        let mut x = vec![0.0f32; self.hidden_size];
7484        m.eh_proj.matvec(&cat, &mut x, self.pool.as_deref());
7485        inference::rms_norm_into(
7486            &x,
7487            &m.layer.input_norm,
7488            self.rms_eps,
7489            self.norm_style,
7490            &mut self.ws.n1,
7491        );
7492        let attn = match &m.layer.attn {
7493            AttnKind::Full {
7494                wq,
7495                wk,
7496                wv,
7497                wo,
7498                q_norm,
7499                k_norm,
7500                output_gate,
7501                softplus_gate,
7502                bias,
7503            } => {
7504                let mut cfg = self.attn_cfg(position);
7505                cfg.q_norm = q_norm.as_deref();
7506                cfg.k_norm = k_norm.as_deref();
7507                cfg.output_gate = *output_gate;
7508                cfg.softplus_gate = softplus_gate.as_ref().map(|(g, p)| (g, *p));
7509                cfg.bias = bias
7510                    .as_ref()
7511                    .map(|(q, k, v)| (q.as_slice(), k.as_slice(), v.as_slice()));
7512                attention::qwen_attention(&self.ws.n1, wq, wk, wv, wo, &mut m.kv, &cfg)
7513            }
7514            _ => return,
7515        };
7516        let _ = attn;
7517    }
7518
7519    /// Speculative decode ON the wgpu whole-token graph: draft k with the
7520    /// MTP head, verify all of them plus the tip in ONE batched graph
7521    /// submit whose tail folds the head, commit the accepted prefix and
7522    /// roll the GDN state back to the last real position. Greedy only —
7523    /// output equals the plain graph's token for token, the way the DSV4
7524    /// verify equals the walk.
7525    #[cfg(feature = "gpu")]
7526    #[allow(clippy::too_many_arguments)]
7527    fn graph_spec_step(
7528        &mut self,
7529        m: &mut MtpModule,
7530        hidden: &[f32],
7531        t_next: u32,
7532        next_pos: usize,
7533        drafted: &mut usize,
7534        accepted: &mut usize,
7535        // The committed stream (prompt + generated so far, `t_next`
7536        // included): the sampler chain's penalties read it, and the
7537        // sampling arm extends it with the drafts position by position.
7538        all_ids: &mut Vec<u32>,
7539        // Tokens left before `max_tokens`. A round commits up to k
7540        // accepted drafts, and those positions are already in the cache,
7541        // so the depth is capped here — trimming the output afterwards
7542        // would leave cache rows the committed stream does not have.
7543        room: usize,
7544    ) -> Option<(Vec<u32>, usize, Vec<f32>)> {
7545        // 3 is the measured optimum on Qwen3.6-27B / RTX 5090 (medians
7546        // of three, greedy): 51.1 tok/s against a plain 49.4, where k=2
7547        // gives 46.1, k=4 50.0, k=5 47.4, k=6 45.2. Acceptance is 89-91%
7548        // throughout — what turns the curve over is the verify, which
7549        // costs ~7.4 ms per extra position, and the draft ~3 ms a step.
7550        // 4 since the draft moved onto the graph (Qwen3.8-27B / 5090:
7551        // k=3 51.2, k=4 51.8 with the per-op draft; the graph draft
7552        // halves the draft cost, so the extra draft is cheaper still).
7553        // 5 with the int8 verify (the default: measured 76.5 against
7554        // k=4's 72-74 and k=6's 74 on the 5090), 4 with the f32 one.
7555        #[cfg(target_os = "macos")]
7556        let metal_native = crate::gpu::q1_force();
7557        #[cfg(not(target_os = "macos"))]
7558        let metal_native = false;
7559        #[cfg(feature = "gpu")]
7560        let k_default = if metal_native {
7561            // the Metal verify's GEMM tile is 8 rows wide and flat in b:
7562            // seven drafts + the tip fill it for free
7563            7
7564        } else if crate::gpu_wgpu::verify_i8_on() {
7565            5
7566        } else {
7567            4
7568        };
7569        #[cfg(not(feature = "gpu"))]
7570        let k_default = 4;
7571        let k_env: Option<usize> = std::env::var("CMF_GRAPH_SPEC_K")
7572            .ok()
7573            .and_then(|v| v.parse().ok())
7574            .filter(|&v| (1..=8).contains(&v));
7575        // Adaptive depth: start below the card's flat-verify optimum and
7576        // let the accepted fraction move it — predictable text climbs to
7577        // the old default within a few rounds, prose settles at 2-3 where
7578        // the shorter verify pays.
7579        let (k_start, k_max) = if metal_native {
7580            (7, 7)
7581        } else {
7582            (3, k_default.max(5))
7583        };
7584        let k_full: usize = k_env.unwrap_or_else(|| self.spec_k_adapt.unwrap_or(k_start));
7585        let k_spec = k_full.min(room).max(1);
7586        // a tail round cut short by `room` says nothing about the text:
7587        // it must not move the adaptive depth the next request starts at
7588        let k_capped = k_spec < k_full;
7589        if next_pos == 0 {
7590            return None;
7591        }
7592        let t_round = std::time::Instant::now();
7593        // Submissions per phase — and they say where the round's money is.
7594        // Qwen3.6-27B on an RTX 5090, k=3:
7595        //
7596        //   draft   9.3 ms / 12 submissions   (four per MTP step)
7597        //   verify 52.8 ms /  1               (the batched graph)
7598        //   commit  5.4 ms /  6               (two per warm)
7599        //
7600        // The verify is already one submit. The draft's own work is 834 MB
7601        // a step — 0.8 ms at this card's measured 1056 GB/s — against 3.1
7602        // ms measured, so ~0.58 ms of every step is round trip, not
7603        // arithmetic, and the same holds for the warms. Eighteen round
7604        // trips a round at roughly half a millisecond each is ~11 ms of a
7605        // 68 ms round: fusing the MTP block into ONE submit the way the
7606        // trunk already is projects to ~64 tok/s against today's 50.9.
7607        // That is the largest measured item left on this path.
7608        let subs = || crate::gpu_wgpu::SUBMITS.load(std::sync::atomic::Ordering::Relaxed);
7609        let sub0 = subs();
7610        // Greedy without penalties verifies by argmax equality (bit-exact
7611        // against the plain path). Anything else is speculative SAMPLING:
7612        // each draft is a DRAW from the MTP head's post-chain distribution
7613        // q_j, kept for the accept test; the verify's rows give p_j.
7614        let cfg = self.sampler_config.clone();
7615        let penalized = !(cfg.repetition_penalty == 1.0
7616            && cfg.presence_penalty == 0.0
7617            && cfg.suppress_tokens.is_empty());
7618        // Three verify regimes: plain greedy (argmax of the raw rows),
7619        // greedy WITH penalties (argmax of the penalized rows — a single
7620        // pass each, no distributions), and sampling (draw / accept /
7621        // correct on post-chain distributions).
7622        let greedy_pen = cfg.temperature < 1e-6 && penalized;
7623        let sampling = cfg.temperature >= 1e-6;
7624        // Sampling with a top-k goes through the SPARSE chain: the dense
7625        // one builds nine 248k-float distributions a round (four drafts,
7626        // five verify rows) and measured 19-22 tok/s against a plain 40 —
7627        // the host, not the card. Sparse, the same nine cost tens of
7628        // microseconds each.
7629        let sparse = sampling && sampler::sparse_ok(&cfg);
7630        let base_len = all_ids.len();
7631        if sampling && !sparse && self.spec_q.len() < k_spec {
7632            self.spec_q.resize_with(k_spec, Vec::new);
7633        }
7634        if sparse && self.spec_qs.len() < k_spec {
7635            self.spec_qs.resize_with(k_spec, Vec::new);
7636        }
7637        // Draft the chain: first from the trunk's tip hidden, then the head
7638        // iterating on itself. Rows land in the MTP KV; the chain rows past
7639        // the first are speculation over speculative state and roll back
7640        // below, replaced by verified pairs.
7641        let mut drafts = Vec::with_capacity(k_spec);
7642        let mut hx = hidden.to_vec();
7643        // CMF_SPEC_DBG=1: draft 0 through BOTH MTP arms (graph and per-op)
7644        // from the same inputs — are the arms the difference, or the inputs?
7645        let spec_dbg = std::env::var("CMF_SPEC_DBG").is_ok();
7646        spec_stamp("pro");
7647        // Plain greedy on native Metal: the whole chain as one command
7648        // buffer (device argmax + embedding gather between the steps).
7649        // A decline before commit hands the round to the per-step loop
7650        // below; a failure after commit is terminal, like any graph
7651        // failure after admission.
7652        #[cfg(target_os = "macos")]
7653        if metal_native && !sampling && !greedy_pen && self.mtp_graph_mode != Some(false) {
7654            match self.mtp_draft_chain_metal(m, hidden, t_next, next_pos - 1, k_spec) {
7655                Ok(ids) => {
7656                    self.mtp_graph_mode = Some(true);
7657                    drafts = ids;
7658                }
7659                Err(true) => {
7660                    tracing::error!("mtp Metal draft chain failed after commit");
7661                    self.clear_sequence_state();
7662                    self.graph_failed
7663                        .store(true, std::sync::atomic::Ordering::Relaxed);
7664                    self.cancel
7665                        .store(true, std::sync::atomic::Ordering::Relaxed);
7666                    return None;
7667                }
7668                Err(false) => {}
7669            }
7670        }
7671        for j in drafts.len()..k_spec {
7672            let tok_in = if j == 0 { t_next } else { drafts[j - 1] };
7673            let mut dbg_ref: Option<(Vec<f32>, Vec<f32>)> = None;
7674            if spec_dbg {
7675                let saved = self.mtp_graph_mode;
7676                self.mtp_graph_mode = Some(false);
7677                let r = self.mtp_step_hl(m, &hx, tok_in, next_pos - 1 + j);
7678                self.mtp_graph_mode = saved;
7679                if self.graph_failed.load(std::sync::atomic::Ordering::Relaxed) {
7680                    return None;
7681                }
7682                m.kv.truncate_last(1);
7683                dbg_ref = Some(r);
7684            }
7685            let (mut lg, hj) = self.mtp_step_hl(m, &hx, tok_in, next_pos - 1 + j);
7686            if self.graph_failed.load(std::sync::atomic::Ordering::Relaxed) {
7687                return None;
7688            }
7689            if let Some((lg_cpu, h_cpu)) = dbg_ref {
7690                let n = |v: &[f32]| v.iter().map(|x| x * x).sum::<f32>().sqrt();
7691                let dl = lg
7692                    .iter()
7693                    .zip(&lg_cpu)
7694                    .fold(0f32, |m, (a, b)| m.max((a - b).abs()));
7695                let dh = hj
7696                    .iter()
7697                    .zip(&h_cpu)
7698                    .fold(0f32, |m, (a, b)| m.max((a - b).abs()));
7699                eprintln!(
7700                    "spec-dbg j={j} pos {} tok_in {tok_in}: per-op draft {} graph draft {} | max|dlogit| {dl:.3} | |h_cpu| {:.2} |h_graph| {:.2} max|dh| {dh:.3} | kv rows {}",
7701                    next_pos - 1 + j,
7702                    sampler::argmax(&lg_cpu),
7703                    sampler::argmax(&lg),
7704                    n(&h_cpu),
7705                    n(&hj),
7706                    m.kv.seq_len
7707                );
7708            }
7709            let dj = if sparse {
7710                let mut q = std::mem::take(&mut self.spec_qs[j]);
7711                let ok = sampler::sparse_distribution_into(
7712                    &lg,
7713                    &cfg,
7714                    all_ids,
7715                    &mut self.sampler_scratch,
7716                    self.pool.as_deref(),
7717                    &mut q,
7718                );
7719                let d = if ok {
7720                    sampler::draw_sparse(&q, &mut self.rng)
7721                } else {
7722                    // everything filtered: the dense chain's greedy fallback
7723                    let t = sampler::argmax(&lg);
7724                    q.clear();
7725                    q.push((t, 1.0));
7726                    t
7727                };
7728                self.spec_qs[j] = q;
7729                all_ids.push(d);
7730                d
7731            } else if sampling {
7732                let mut q = std::mem::take(&mut self.spec_q[j]);
7733                sampler::distribution_into(
7734                    &lg,
7735                    &cfg,
7736                    all_ids,
7737                    &mut self.sampler_scratch,
7738                    self.pool.as_deref(),
7739                    &mut q,
7740                );
7741                let d = sampler::draw(&q, &mut self.rng);
7742                self.spec_q[j] = q;
7743                all_ids.push(d); // the next draft's penalties see this one
7744                d
7745            } else if greedy_pen {
7746                let d = sampler::argmax_penalized(
7747                    &lg,
7748                    &cfg,
7749                    all_ids,
7750                    &mut self.sampler_scratch,
7751                    self.pool.as_deref(),
7752                );
7753                all_ids.push(d);
7754                d
7755            } else {
7756                sampler::argmax(&lg)
7757            };
7758            attention::recycle_buf(&mut lg);
7759            drafts.push(dj);
7760            hx = hj;
7761            spec_stamp("d.pick");
7762        }
7763        all_ids.truncate(base_len);
7764        *drafted += k_spec;
7765        let t_draft = t_round.elapsed();
7766        let sub_draft = subs();
7767        // Verify batch: [t_next, d1 .. d_{k-1}] at next_pos.. — every row's
7768        // logits come back from the graph's own head.
7769        let b = k_spec + 1;
7770        let mut hiddens = vec![0.0f32; b * self.hidden_size];
7771        for (i, &t) in std::iter::once(&t_next).chain(drafts.iter()).enumerate() {
7772            let e = self.embed_single(t);
7773            hiddens[i * self.hidden_size..(i + 1) * self.hidden_size].copy_from_slice(&e);
7774        }
7775        let positions: Vec<usize> = (next_pos..next_pos + b).collect();
7776        spec_stamp("v.emb");
7777        let (lm_gw, lm_rows) = {
7778            let (_, i, kind, rs) = self.weights.lm_head.graph_weight()?;
7779            (
7780                crate::gpu::GraphW {
7781                    idx: i,
7782                    kind,
7783                    row_scale: rs,
7784                    data: &[],
7785                    prism: crate::gpu::GraphPrismOp::None,
7786                    affine: false,
7787                },
7788                self.weights.lm_head.rows(),
7789            )
7790        };
7791        let mut logits = Vec::new();
7792        let final_norm = self.weights.final_norm.clone();
7793        // Plain greedy on Metal: the b argmaxes come from the device
7794        // (`argmax_rows` after the head) and the 7.9 MB logits plane is
7795        // never read back — the round's decision needs only the ids, and
7796        // the loop top takes the last verified id as `spec_forced`, which
7797        // is exactly what its argmax of the row would give. The full rows
7798        // stay for anything that reads them: sampling, penalties,
7799        // confidence, the verify oracle, the logit dump.
7800        // `CMF_METAL_DEV_ARGMAX=0` keeps the host path.
7801        #[cfg(target_os = "macos")]
7802        let greedy_dev = metal_native
7803            && !sampling
7804            && !greedy_pen
7805            && !self.confidence_on
7806            && self.final_softcap.is_none()
7807            // The host acceptance argmax scans the WHOLE head row
7808            // (`lm_rows`), the sampler's own row only `vocab_size`: they
7809            // coincide exactly when the head has no padding rows, and
7810            // only then is the device argmax (which scores `vocab_size`)
7811            // bit-identical to both.
7812            && self.vocab_size == lm_rows
7813            && std::env::var_os("CMF_METAL_VERIFY_CHECK").is_none()
7814            && std::env::var_os("CMF_LOGIT_DUMP").is_none()
7815            && std::env::var("CMF_METAL_DEV_ARGMAX").as_deref() != Ok("0");
7816        #[cfg(not(target_os = "macos"))]
7817        let greedy_dev = false;
7818        let mut dev_ids: Vec<u32> = Vec::new();
7819        #[cfg(target_os = "macos")]
7820        let verify_outcome = if metal_native {
7821            let lm = self.weights.lm_head.q1_parts()?;
7822            let n_score = self.vocab_size.min(lm_rows);
7823            self.try_batch_graph_metal(
7824                &mut hiddens,
7825                &positions,
7826                b,
7827                Some((lm, &final_norm, &mut logits)),
7828                if greedy_dev {
7829                    Some((n_score, &mut dev_ids))
7830                } else {
7831                    None
7832                },
7833            )
7834        } else {
7835            self.try_batch_graph_wgpu(
7836                &mut hiddens,
7837                &positions,
7838                b,
7839                Some(crate::gpu::SpecTail {
7840                    lm: lm_gw,
7841                    lm_rows,
7842                    final_norm: &final_norm,
7843                    logits_out: &mut logits,
7844                    pick: None,
7845                }),
7846            )
7847        };
7848        #[cfg(not(target_os = "macos"))]
7849        let verify_outcome = self.try_batch_graph_wgpu(
7850            &mut hiddens,
7851            &positions,
7852            b,
7853            Some(crate::gpu::SpecTail {
7854                lm: lm_gw,
7855                lm_rows,
7856                final_norm: &final_norm,
7857                logits_out: &mut logits,
7858                pick: None,
7859            }),
7860        );
7861        match verify_outcome {
7862            crate::gpu::BatchGraphOutcome::Completed => {}
7863            crate::gpu::BatchGraphOutcome::Declined => {
7864                // The verifier refused before admission.  Its draft MTP
7865                // rows are still device-resident, so rewind the separate
7866                // mirror before the caller takes the exact one-token path.
7867                m.kv.truncate_last(k_spec);
7868                if !metal_native && !self.rewind_mtp_graph_mirror(next_pos) {
7869                    self.clear_sequence_state();
7870                    self.graph_failed
7871                        .store(true, std::sync::atomic::Ordering::Relaxed);
7872                    self.cancel
7873                        .store(true, std::sync::atomic::Ordering::Relaxed);
7874                    tracing::error!("MTP graph mirror rewind failed after verify decline");
7875                }
7876                return None;
7877            }
7878            crate::gpu::BatchGraphOutcome::Failed => {
7879                // A failed batch may have advanced trunk/GDN state.  Clear
7880                // both mirrors and preserve the terminal outcome rather than
7881                // falling through to stale CPU state.
7882                self.clear_sequence_state();
7883                self.graph_failed
7884                    .store(true, std::sync::atomic::Ordering::Relaxed);
7885                self.cancel
7886                    .store(true, std::sync::atomic::Ordering::Relaxed);
7887                tracing::error!("MTP verify batch graph failed after admission");
7888                return None;
7889            }
7890        }
7891        // `CMF_METAL_VERIFY_CHECK=1`: run the same b tokens through the
7892        // plain per-token path and compare each row's argmax + logits with
7893        // the verify's — the bring-up oracle for the batched graph. The
7894        // plain forwards mutate the CPU state; it is snapshotted and put
7895        // back, and the K/V mirrors re-pointed, before the round goes on.
7896        #[cfg(target_os = "macos")]
7897        if metal_native && std::env::var("CMF_METAL_VERIFY_CHECK").as_deref() == Ok("1") {
7898            let snap: Vec<Vec<f32>> = self
7899                .kv_cache
7900                .layers
7901                .iter()
7902                .map(|l| l.linear_state.clone())
7903                .collect();
7904            let attn_lens: Vec<usize> = self.kv_cache.layers.iter().map(|l| l.seq_len).collect();
7905            let toks: Vec<u32> = std::iter::once(t_next)
7906                .chain(drafts.iter().copied())
7907                .collect();
7908            let want_save = self.graph_want_logits;
7909            self.graph_want_logits = false;
7910            for (i, &t) in toks.iter().enumerate() {
7911                let hi = self.forward_layers(&self.embed_single(t), next_pos + i, None);
7912                let _ = self.graph_logits.take();
7913                // CMF_SPEC_PLAIN_HIDDEN=1: the next round drafts from the
7914                // plain path's hidden instead of the verify's (an experiment
7915                // on the chain's sensitivity to the half-GEMM noise)
7916                if std::env::var("CMF_SPEC_PLAIN_HIDDEN").as_deref() == Ok("1") {
7917                    hiddens[i * self.hidden_size..(i + 1) * self.hidden_size].copy_from_slice(&hi);
7918                }
7919                let ref_lg = self.logits_from_hidden(&hi);
7920                let row = &logits[i * lm_rows..(i + 1) * lm_rows];
7921                let ra = sampler::argmax(&ref_lg);
7922                let va = sampler::argmax(row);
7923                let mut md = 0f32;
7924                let mut rms = 0f64;
7925                for j in 0..lm_rows.min(ref_lg.len()) {
7926                    let d = (ref_lg[j] - row[j]).abs();
7927                    md = md.max(d);
7928                    rms += (d as f64) * (d as f64);
7929                }
7930                let mut hd = 0f32;
7931                for j in 0..self.hidden_size {
7932                    hd = hd.max((hi[j] - hiddens[i * self.hidden_size + j]).abs());
7933                }
7934                eprintln!(
7935                    "verify-check row {i} tok {t} pos {}: ref argmax {ra} verify argmax {va} {} | max|dlogit| {md:.3} rms {:.4} | max|dhidden| {hd:.4}",
7936                    next_pos + i,
7937                    if ra == va { "OK" } else { "MISMATCH" },
7938                    (rms / lm_rows as f64).sqrt()
7939                );
7940            }
7941            self.graph_want_logits = want_save;
7942            // restore IN PLACE: the pending verify graph wraps these very
7943            // allocations (zero-copy) — replacing the Vec would strand it
7944            for (l, st) in self.kv_cache.layers.iter_mut().zip(snap) {
7945                if l.linear_state.len() == st.len() {
7946                    l.linear_state.copy_from_slice(&st);
7947                } else {
7948                    l.linear_state = st;
7949                }
7950            }
7951            for (li, (l, n0)) in self.kv_cache.layers.iter_mut().zip(attn_lens).enumerate() {
7952                let extra = l.seq_len.saturating_sub(n0);
7953                if extra > 0 {
7954                    l.truncate_last(extra);
7955                    crate::gpu_metal::kv_mirror_set_stored(self.graph_kv_id, li, n0);
7956                }
7957            }
7958        }
7959        let t_verify = t_round.elapsed();
7960        let sub_verify = subs();
7961        // Acceptance. Greedy: row i's argmax is the trunk's token after
7962        // input i. Sampling: accept draft i with min(1, p_i/q_i), and on
7963        // the first rejection draw the correction from max(0, p_i − q_i)
7964        // — that token is committed by the loop top as-is (spec_forced).
7965        let mut a = 0usize;
7966        let mut forced: Option<u32> = None;
7967        let ids: Vec<u32> = if sparse {
7968            let mut p = std::mem::take(&mut self.spec_ps);
7969            let mut res = std::mem::take(&mut self.spec_ress);
7970            while a < k_spec {
7971                let ok = sampler::sparse_distribution_into(
7972                    &logits[a * lm_rows..(a + 1) * lm_rows],
7973                    &cfg,
7974                    all_ids,
7975                    &mut self.sampler_scratch,
7976                    self.pool.as_deref(),
7977                    &mut p,
7978                );
7979                if !ok {
7980                    let t = sampler::argmax(&logits[a * lm_rows..(a + 1) * lm_rows]);
7981                    p.clear();
7982                    p.push((t, 1.0));
7983                }
7984                match sampler::spec_accept_or_correct_sparse(
7985                    &p,
7986                    &self.spec_qs[a],
7987                    drafts[a],
7988                    &mut self.rng,
7989                    &mut res,
7990                ) {
7991                    None => {
7992                        all_ids.push(drafts[a]);
7993                        a += 1;
7994                    }
7995                    Some(c) => {
7996                        forced = Some(c);
7997                        break;
7998                    }
7999                }
8000            }
8001            all_ids.truncate(base_len);
8002            self.spec_ps = p;
8003            self.spec_ress = res;
8004            drafts.clone()
8005        } else if sampling {
8006            let mut p = std::mem::take(&mut self.spec_p);
8007            let mut res = std::mem::take(&mut self.spec_res);
8008            while a < k_spec {
8009                sampler::distribution_into(
8010                    &logits[a * lm_rows..(a + 1) * lm_rows],
8011                    &cfg,
8012                    all_ids,
8013                    &mut self.sampler_scratch,
8014                    self.pool.as_deref(),
8015                    &mut p,
8016                );
8017                match sampler::spec_accept_or_correct(
8018                    &p,
8019                    &self.spec_q[a],
8020                    drafts[a],
8021                    &mut self.rng,
8022                    &mut res,
8023                    self.pool.as_deref(),
8024                ) {
8025                    None => {
8026                        all_ids.push(drafts[a]);
8027                        a += 1;
8028                    }
8029                    Some(c) => {
8030                        forced = Some(c);
8031                        break;
8032                    }
8033                }
8034            }
8035            all_ids.truncate(base_len);
8036            self.spec_p = p;
8037            self.spec_res = res;
8038            // the accepted drafts ARE the verified tokens after inputs 0..a
8039            drafts.clone()
8040        } else if greedy_pen {
8041            // Row i's penalized argmax, penalties over the stream that
8042            // includes the accepted drafts before it — the plain loop's
8043            // exact arithmetic, one pass per row, no working copy.
8044            let mut ids: Vec<u32> = Vec::with_capacity(b);
8045            for i in 0..b {
8046                let t = sampler::argmax_penalized(
8047                    &logits[i * lm_rows..(i + 1) * lm_rows],
8048                    &cfg,
8049                    all_ids,
8050                    &mut self.sampler_scratch,
8051                    self.pool.as_deref(),
8052                );
8053                ids.push(t);
8054                if i < k_spec && t == drafts[i] {
8055                    all_ids.push(t);
8056                } else {
8057                    break;
8058                }
8059            }
8060            all_ids.truncate(base_len);
8061            while a < k_spec && a < ids.len() && ids[a] == drafts[a] {
8062                a += 1;
8063            }
8064            // rows past the first mismatch were never scored; the loop
8065            // top re-samples the last verified row itself.
8066            ids
8067        } else if greedy_dev && dev_ids.len() == b {
8068            let ids = std::mem::take(&mut dev_ids);
8069            while a < k_spec && ids[a] == drafts[a] {
8070                a += 1;
8071            }
8072            ids
8073        } else {
8074            if logits.len() < b * lm_rows {
8075                // the device argmax was asked for and came back short:
8076                // no rows to fall back on — terminal like a failed batch
8077                self.clear_sequence_state();
8078                self.graph_failed
8079                    .store(true, std::sync::atomic::Ordering::Relaxed);
8080                self.cancel
8081                    .store(true, std::sync::atomic::Ordering::Relaxed);
8082                tracing::error!("Metal verify returned neither logits nor argmax ids");
8083                return None;
8084            }
8085            let ids: Vec<u32> = (0..b)
8086                .map(|i| sampler::argmax(&logits[i * lm_rows..(i + 1) * lm_rows]))
8087                .collect();
8088            while a < k_spec && ids[a] == drafts[a] {
8089                a += 1;
8090            }
8091            ids
8092        };
8093        spec_stamp("acc");
8094        if spec_dbg {
8095            eprintln!(
8096                "spec-dbg round: t_next {t_next} drafts {:?} verified {:?} accepted {a}",
8097                drafts, ids
8098            );
8099        }
8100        // CMF_METAL_VERIFY_CHECK=2: the commit oracle — plain-forward the
8101        // a+1 accepted tokens from a snapshot, then diff the replayed GDN
8102        // states and the appended K/V rows against that.
8103        #[cfg(target_os = "macos")]
8104        let commit_ref: Option<(Vec<Vec<f32>>, Vec<(usize, Vec<f32>, Vec<f32>)>)> = if metal_native
8105            && std::env::var("CMF_METAL_VERIFY_CHECK").as_deref() == Ok("2")
8106        {
8107            let snap: Vec<Vec<f32>> = self
8108                .kv_cache
8109                .layers
8110                .iter()
8111                .map(|l| l.linear_state.clone())
8112                .collect();
8113            let attn_lens: Vec<usize> = self.kv_cache.layers.iter().map(|l| l.seq_len).collect();
8114            let toks: Vec<u32> = std::iter::once(t_next)
8115                .chain(drafts.iter().copied())
8116                .collect();
8117            let want_save = self.graph_want_logits;
8118            self.graph_want_logits = false;
8119            for (i, &t) in toks.iter().take(a + 1).enumerate() {
8120                let _ = self.forward_layers(&self.embed_single(t), next_pos + i, None);
8121                let _ = self.graph_logits.take();
8122            }
8123            self.graph_want_logits = want_save;
8124            let plain_states: Vec<Vec<f32>> = self
8125                .kv_cache
8126                .layers
8127                .iter()
8128                .map(|l| l.linear_state.clone())
8129                .collect();
8130            let (nkv, hd) = (self.num_kv_heads, self.head_dim);
8131            let mut rows = Vec::new();
8132            for (li, (l, n0)) in self
8133                .kv_cache
8134                .layers
8135                .iter_mut()
8136                .zip(attn_lens.iter())
8137                .enumerate()
8138            {
8139                let extra = l.seq_len.saturating_sub(*n0);
8140                if extra > 0 {
8141                    let mut kk = Vec::new();
8142                    let mut vv = Vec::new();
8143                    for g in 0..nkv {
8144                        kk.extend_from_slice(&l.head_keys(g)[n0 * hd..]);
8145                        vv.extend_from_slice(&l.head_values(g)[n0 * hd..]);
8146                    }
8147                    rows.push((li, kk, vv));
8148                    l.truncate_last(extra);
8149                    crate::gpu_metal::kv_mirror_set_stored(self.graph_kv_id, li, *n0);
8150                }
8151            }
8152            for (l, st) in self.kv_cache.layers.iter_mut().zip(snap) {
8153                if l.linear_state.len() == st.len() {
8154                    l.linear_state.copy_from_slice(&st);
8155                } else {
8156                    l.linear_state = st;
8157                }
8158            }
8159            Some((plain_states, rows))
8160        } else {
8161            None
8162        };
8163        let warm_off = std::env::var("CMF_SPEC_WARM").is_ok_and(|v| v == "0");
8164        // Metal: the MTP cache cut and the round's warm-up SUBMIT come
8165        // BEFORE the trunk commit, so the warm-up's command buffer is
8166        // queued ahead of the GDN replay (second queue) and its wait
8167        // below no longer sits behind the replay — measured: the warm-up's
8168        // wait grew with the accepted count exactly like the replay does
8169        // (8 ms at a=1, 17 ms at a=3, 25 ms at a=5 for ~2 ms of its own
8170        // work). The replay now overlaps the warm-up's readback, the
8171        // round's return and the next draft chain.
8172        #[cfg(target_os = "macos")]
8173        let mut warm_pending: Option<MetalWarmPending> = None;
8174        #[cfg(target_os = "macos")]
8175        if metal_native {
8176            m.kv.truncate_last(k_spec.saturating_sub(1));
8177            if self.mtp_graph_mode == Some(true) {
8178                // the mirror rows below the cut are the CPU rows: re-point,
8179                // no re-upload
8180                crate::gpu_metal::kv_mirror_set_stored(
8181                    self.mtp_kv_id(),
8182                    Self::MTP_LAYER_BASE,
8183                    m.kv.seq_len,
8184                );
8185                if !warm_off && a > 0 {
8186                    let pairs: Vec<(&[f32], u32)> = (0..a)
8187                        .map(|j| {
8188                            (
8189                                &hiddens[j * self.hidden_size..(j + 1) * self.hidden_size],
8190                                ids[j],
8191                            )
8192                        })
8193                        .collect();
8194                    warm_pending = self.mtp_warm_batch_submit(m, &pairs, next_pos);
8195                }
8196            }
8197            spec_stamp("c.wsub");
8198        }
8199        // a fully-accepted round needs no restore: every input was real.
8200        #[cfg(target_os = "macos")]
8201        if metal_native {
8202            // the Metal verify never wrote its states: the commit replays the
8203            // accepted prefix into the CPU owners and appends the K/V rows
8204            if !self.metal_verify_commit(a) {
8205                self.clear_sequence_state();
8206                self.graph_failed
8207                    .store(true, std::sync::atomic::Ordering::Relaxed);
8208                self.cancel
8209                    .store(true, std::sync::atomic::Ordering::Relaxed);
8210                tracing::error!("Metal verify state/KV handoff failed after admission");
8211                return None;
8212            }
8213            if let Some((plain_states, rows)) = commit_ref {
8214                crate::gpu_metal::queue_fence();
8215                // the commit's replay runs on the second queue: collect it
8216                // before the oracle reads the CPU owners it writes into
8217                let _ = crate::gpu_metal::wait_replay();
8218                let (nkv, hd) = (self.num_kv_heads, self.head_dim);
8219                let mut worst_s = 0f32;
8220                let mut worst_li = 0usize;
8221                for (li, (l, ps)) in self.kv_cache.layers.iter().zip(&plain_states).enumerate() {
8222                    if l.linear_state.len() != ps.len() || ps.is_empty() {
8223                        continue;
8224                    }
8225                    let d = l
8226                        .linear_state
8227                        .iter()
8228                        .zip(ps)
8229                        .fold(0f32, |m, (x, y)| m.max((x - y).abs()));
8230                    let n = ps.iter().fold(0f32, |m, y| m.max(y.abs()));
8231                    let rel = d / n.max(1e-6);
8232                    if rel > worst_s {
8233                        worst_s = rel;
8234                        worst_li = li;
8235                    }
8236                }
8237                let mut worst_k = 0f32;
8238                for (li, kk, vv) in &rows {
8239                    let l = &self.kv_cache.layers[*li];
8240                    let n0 = l.seq_len - (kk.len() / (nkv * hd));
8241                    let mut ck = Vec::new();
8242                    let mut cv = Vec::new();
8243                    for g in 0..nkv {
8244                        ck.extend_from_slice(&l.head_keys(g)[n0 * hd..]);
8245                        cv.extend_from_slice(&l.head_values(g)[n0 * hd..]);
8246                    }
8247                    if ck.len() == kk.len() {
8248                        let dk = ck
8249                            .iter()
8250                            .zip(kk)
8251                            .fold(0f32, |m, (x, y)| m.max((x - y).abs()));
8252                        let dv = cv
8253                            .iter()
8254                            .zip(vv)
8255                            .fold(0f32, |m, (x, y)| m.max((x - y).abs()));
8256                        worst_k = worst_k.max(dk).max(dv);
8257                    } else {
8258                        eprintln!(
8259                            "commit-check L{li}: kv row count mismatch {} vs {}",
8260                            ck.len(),
8261                            kk.len()
8262                        );
8263                    }
8264                }
8265                eprintln!(
8266                    "commit-check a={a}: worst GDN state rel-max diff {worst_s:.2e} (L{worst_li}) | worst K/V row abs diff {worst_k:.4}"
8267                );
8268            }
8269        }
8270        if !metal_native && a + 1 < b {
8271            let expected_gdn_layers = self.graph_gdn_layer_count();
8272            if expected_gdn_layers > 0
8273                && !crate::gpu::gdn_spec_restore(self.graph_kv_id, a, next_pos, expected_gdn_layers)
8274            {
8275                self.clear_sequence_state();
8276                self.graph_failed
8277                    .store(true, std::sync::atomic::Ordering::Relaxed);
8278                self.cancel
8279                    .store(true, std::sync::atomic::Ordering::Relaxed);
8280                tracing::error!("GDN speculative restore failed after verify");
8281                return None;
8282            }
8283        }
8284        if !metal_native && !self.rewind_trunk_graph_mirrors(next_pos + a + 1) {
8285            // The verify graph committed the full batch, but one of its
8286            // persistent Full-attention mirrors could not be re-pointed to
8287            // the accepted prefix.  Treat that as terminal state failure;
8288            // an exact CPU fallback would otherwise consume stale GDN/KV.
8289            self.clear_sequence_state();
8290            self.graph_failed
8291                .store(true, std::sync::atomic::Ordering::Relaxed);
8292            self.cancel
8293                .store(true, std::sync::atomic::Ordering::Relaxed);
8294            tracing::error!("trunk graph KV rewind failed after speculative verify");
8295            return None;
8296        }
8297        *accepted += a;
8298        // MTP cache: keep the first draft row (its inputs were real), drop
8299        // the chain's, then append the verified pairs the round produced.
8300        // Each of those is a whole MTP block on the per-op path and they
8301        // cost 5.8 ms of a 69 ms round at k=3 — a third of what the
8302        // round's own draft costs. PRICED, and they earn it: skipping
8303        // them (`CMF_SPEC_WARM=0`) drops acceptance from 89% to 81% at
8304        // k=3 and 85% to 74% at k=4, and the tok/s goes nowhere at k=3
8305        // (50.3 against 50.5) and backwards at k=4 (48.1 against 50.1).
8306        // The knob stays so the next person can re-price it after the
8307        // warms are batched instead of assuming either way.
8308        if !metal_native {
8309            // (Metal cut its MTP cache before the trunk commit, above)
8310            m.kv.truncate_last(k_spec.saturating_sub(1));
8311        }
8312        spec_stamp("c.trunc");
8313        if !metal_native
8314            && self.mtp_graph_mode == Some(true)
8315            && !self.rewind_mtp_graph_mirror(next_pos)
8316        {
8317            // The graph draft was admitted, so inability to move its cursor
8318            // back to the real anchor is a state failure, not a capability
8319            // refusal.  Do not warm or continue with a stale mirror.
8320            self.clear_sequence_state();
8321            self.graph_failed
8322                .store(true, std::sync::atomic::Ordering::Relaxed);
8323            self.cancel
8324                .store(true, std::sync::atomic::Ordering::Relaxed);
8325            tracing::error!("MTP graph mirror rewind failed after verify commit");
8326            return None;
8327        }
8328        if !warm_off && a > 0 {
8329            // Graph arm: all accepted pairs in ONE batched run over the
8330            // MTP block; the token graph one by one if the batch declines.
8331            let mut warmed = false;
8332            #[cfg(target_os = "macos")]
8333            if metal_native && self.mtp_graph_mode == Some(true) {
8334                // the batched warm-up was submitted before the trunk
8335                // commit: collect it here; one by one on the token graph
8336                // if it declined (or failed)
8337                warmed = match warm_pending.take() {
8338                    Some(p) => self.mtp_warm_batch_finish(m, p),
8339                    None => false,
8340                };
8341                if !warmed {
8342                    warmed = true;
8343                    for j in 0..a {
8344                        let row =
8345                            hiddens[j * self.hidden_size..(j + 1) * self.hidden_size].to_vec();
8346                        if self
8347                            .mtp_step_metal(m, &row, ids[j], next_pos + j, false)
8348                            .is_none()
8349                        {
8350                            warmed = false;
8351                            break;
8352                        }
8353                    }
8354                }
8355            }
8356            if !warmed && self.mtp_graph_mode != Some(false) && !metal_native {
8357                let rows: Vec<Vec<f32>> = (0..a)
8358                    .map(|j| hiddens[j * self.hidden_size..(j + 1) * self.hidden_size].to_vec())
8359                    .collect();
8360                let pairs: Vec<(&[f32], u32)> = rows
8361                    .iter()
8362                    .zip(ids.iter())
8363                    .map(|(r, &t)| (r.as_slice(), t))
8364                    .collect();
8365                match self.mtp_warm_prefill_pairs(m, &pairs, next_pos) {
8366                    Ok(()) => warmed = true,
8367                    Err(err) => {
8368                        // A warm-up failure after graph admission cannot
8369                        // fall back to `mtp_warm`: the detached CPU cache is
8370                        // not authoritative for the device mirror.  Mark it
8371                        // terminal so the generation caller clears state and
8372                        // returns instead of drafting from stale attention.
8373                        tracing::error!("{err}");
8374                        self.clear_sequence_state();
8375                        self.graph_failed
8376                            .store(true, std::sync::atomic::Ordering::Relaxed);
8377                        self.cancel
8378                            .store(true, std::sync::atomic::Ordering::Relaxed);
8379                        return None;
8380                    }
8381                }
8382            }
8383            if !warmed {
8384                for j in 0..a {
8385                    let row = &hiddens[j * self.hidden_size..(j + 1) * self.hidden_size];
8386                    let row = row.to_vec();
8387                    self.mtp_warm(m, &row, ids[j], next_pos + j);
8388                }
8389            }
8390        }
8391        // The sampler's contract: logits of the LAST verified position —
8392        // unless a rejected draft already drew the correction, in which
8393        // case the loop top commits that token and samples nothing.
8394        spec_stamp("c.warm");
8395        if let Some(c) = forced {
8396            self.spec_forced = Some(c);
8397            self.graph_logits = None;
8398        } else if greedy_dev && logits.is_empty() {
8399            // the row's argmax IS the token the loop top would pick from
8400            // it (plain greedy, no penalties): commit it as forced
8401            self.spec_forced = Some(ids[a]);
8402            self.graph_logits = None;
8403        } else {
8404            let mut row = logits[a * lm_rows..(a + 1) * lm_rows].to_vec();
8405            row.resize(self.vocab_size, 0.0);
8406            if let Some(c) = self.final_softcap {
8407                for l in row.iter_mut() {
8408                    *l = c * (*l / c).tanh();
8409                }
8410            }
8411            self.graph_logits = Some(row);
8412        }
8413        let new_hidden = hiddens[a * self.hidden_size..(a + 1) * self.hidden_size].to_vec();
8414        spec_stamp("c.row");
8415        // Three phases, not two. The round's wall clock was 4 ms longer
8416        // than draft+verify and the difference had nowhere to be seen:
8417        // the accepted prefix re-runs the MTP block once per token to
8418        // keep the draft head's attention cache warm, and the GDN state
8419        // rolls back on any rejection. Both live here, after the verify.
8420        if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
8421            let end = subs();
8422            eprintln!(
8423                "spec-round: draft {:.1} ms/{} sub | verify {:.1} ms/{} sub | \
8424                 commit {:.1} ms/{} sub (accepted {a} of {k_spec}, full-head streak {})",
8425                t_draft.as_secs_f64() * 1e3,
8426                sub_draft - sub0,
8427                (t_verify - t_draft).as_secs_f64() * 1e3,
8428                sub_verify - sub_draft,
8429                (t_round.elapsed() - t_verify).as_secs_f64() * 1e3,
8430                end - sub_verify,
8431                self.draft_full_streak,
8432            );
8433        }
8434        // Native Metal's verify tile is flat in b (eight rows for the price
8435        // of one), so a shorter round only forfeits tokens — measured on
8436        // the M4: an essay round at k=2 still verified in 260 ms. The
8437        // adaptation is for cards whose verify grows with the rows.
8438        if k_env.is_none() && !metal_native && !k_capped {
8439            // Slow average and a wide band: a fast one oscillated 2↔3 on
8440            // an essay every other round (measured), which forfeits the
8441            // draft it just paid for.
8442            let f = a as f32 / k_spec.max(1) as f32;
8443            self.spec_acc_ewma += 0.2 * (f - self.spec_acc_ewma);
8444            let mut k_next = k_spec;
8445            if self.spec_acc_ewma >= 0.75 && k_spec < k_max {
8446                k_next = k_spec + 1;
8447            } else if self.spec_acc_ewma < 0.4 && k_spec > 2 {
8448                k_next = k_spec - 1;
8449            }
8450            if k_next != k_spec {
8451                self.spec_acc_ewma = 0.6;
8452                if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
8453                    eprintln!("spec-k: {k_spec} → {k_next}");
8454                }
8455            }
8456            self.spec_k_adapt = Some(k_next);
8457        }
8458        spec_stamp("end");
8459        Some((drafts[..a].to_vec(), next_pos + a + 1, new_hidden))
8460    }
8461
8462    /// Micro-benchmark: two single-position forwards vs one fused pair
8463    /// from the current cache state (KV rewound after each probe).
8464    /// Returns (two_singles_ms, fused_pair_ms) per probe, or the (0, 0)
8465    /// sentinel when this model has no pair path to measure — the same
8466    /// answer the o1 arm gives, and the bench prints it the same way.
8467    /// (An architecture that loads its own layers leaves `weights.layers`
8468    /// empty; walking it here was an index panic, found by `bench` on
8469    /// deepseek_v4.)
8470    pub fn measure_pair_fusion(&mut self, iters: usize) -> (f64, f64) {
8471        if !self.pair_supported() {
8472            return (0.0, 0.0);
8473        }
8474        // A GDN model that needs an automatic device prefix never uses this
8475        // fused-pair path in normal generation: its recurrence is serial and
8476        // the speculative pair is disabled by default.  More importantly,
8477        // this diagnostic suppresses the token graph, so measuring it would
8478        // stream the non-resident tail through per-op Vulkan just to print a
8479        // non-production number.  Leave inference untouched and report the
8480        // established unavailable sentinel instead.
8481        if self.gdn_cfg.is_some()
8482            && self.graph_prefill_preferred()
8483            && self.automatic_gpu_prefix().is_some()
8484        {
8485            tracing::info!(
8486                "pair micro-benchmark skipped: GDN device-prefix path has no production pair route"
8487            );
8488            return (0.0, 0.0);
8489        }
8490        // This is a host-side pair micro-benchmark. It truncates the host KV
8491        // after every probe, so letting the whole-token graph participate
8492        // would leave its device GDN/KV mirror ahead of the next probe and
8493        // poison the process-wide graph verdict before the real generation
8494        // benchmark starts. Keep the existing per-op/GPU arithmetic while
8495        // suppressing only the stateful token graph for this measurement.
8496        let graph_env = std::env::var_os("CMF_GPU_WGPU_GRAPH");
8497        unsafe { std::env::set_var("CMF_GPU_WGPU_GRAPH", "0") };
8498        let emb1 = self.embed_single(1);
8499        let emb2 = self.embed_single(2);
8500        let pos = self.kv_cache.seq_len();
8501
8502        let t0 = std::time::Instant::now();
8503        for _ in 0..iters {
8504            let _ = self.forward_layers(&emb1, pos, None);
8505            let _ = self.forward_layers(&emb2, pos + 1, None);
8506            for l in &mut self.kv_cache.layers {
8507                l.truncate_last(2);
8508            }
8509        }
8510        let singles_ms = t0.elapsed().as_secs_f64() * 1000.0 / iters as f64;
8511
8512        let t1 = std::time::Instant::now();
8513        for _ in 0..iters {
8514            let _ = self.forward_pair(&emb1, &emb2, pos);
8515            for l in &mut self.kv_cache.layers {
8516                l.truncate_last(2);
8517            }
8518        }
8519        let pair_ms = t1.elapsed().as_secs_f64() * 1000.0 / iters as f64;
8520        match graph_env {
8521            Some(value) => unsafe { std::env::set_var("CMF_GPU_WGPU_GRAPH", value) },
8522            None => unsafe { std::env::remove_var("CMF_GPU_WGPU_GRAPH") },
8523        }
8524        (singles_ms, pair_ms)
8525    }
8526
8527    /// Fused two-position forward: weight rows are streamed from memory
8528    /// once per layer for both positions. Full layers → fused GQA pair;
8529    /// linear layers → vmf_phase pair (lane 2 state is tentative in the
8530    /// per-layer scratch until the draft is accepted).
8531    /// Whether the fused two-position path covers every layer kind in
8532    /// this model. MLA and KDA run per position (their pair arms are
8533    /// unreachable); the seq prefill falls back to singles for them.
8534    fn pair_supported(&self) -> bool {
8535        // An EMPTY layer stack means the architecture loaded its own and
8536        // this path has nothing to walk. Checking that directly, rather
8537        // than naming each such architecture, is what makes the guard hold
8538        // for the next one: `any()` over no layers is false, so a
8539        // feature-by-feature test says "supported" for a model that has no
8540        // layers here at all.
8541        !self.weights.layers.is_empty()
8542            && !self.per_token_stack()
8543            && !self
8544                .weights
8545                .layers
8546                .iter()
8547                .any(|lw| matches!(&lw.attn, AttnKind::Mla(_) | AttnKind::Kda(_)))
8548    }
8549
8550    fn forward_pair(
8551        &mut self,
8552        emb1: &[f32],
8553        emb2: &[f32],
8554        position: usize,
8555    ) -> (Vec<f32>, Vec<f32>) {
8556        // A two-token prompt starts here, not in the layer walk: decide the
8557        // MiMo placement before the pair's per-op MoE uploads any expert.
8558        self.mimo_moe_prepare();
8559        let mut h1 = emb1.to_vec();
8560        let mut h2 = emb2.to_vec();
8561        let (_nkv, _hd, hs, _rd, eps) = (
8562            self.num_kv_heads,
8563            self.head_dim,
8564            self.hidden_size,
8565            self.rotary_dim,
8566            self.rms_eps,
8567        );
8568        let pool = self.pool.clone();
8569        // `forward_pair` bypasses `forward_layers_span`, but its per-op
8570        // body must still obey the ordinary layer placement. In particular,
8571        // an over-capacity Vulkan model has a device prefix and a CPU tail;
8572        // letting the pair walk submit the tail's selected MoE experts would
8573        // stream the whole tail through the residency arena and defeat the
8574        // measured VRAM bound.
8575        let automatic_gpu_prefix = self.automatic_gpu_prefix();
8576
8577        for li in 0..self.num_layers {
8578            let _capacity_tail = automatic_gpu_prefix
8579                .filter(|&prefix| li >= prefix && !self.mimo_moe.is_dynamic(li, false))
8580                .map(|_| crate::gpu::enter_cpu_scope());
8581            // Also makes an explicit CMF_GPU_LAYERS split apply to fused
8582            // pairs, just as it does to the ordinary layer walk.
8583            crate::gpu::set_layer(li as i64);
8584            let lw = &self.weights.layers[self.phys_layer(li)];
8585            // Norms into pipeline scratch (4 allocs/layer on the MTP
8586            // decode hot path before this).
8587            inference::rms_norm_into(
8588                &h1,
8589                &lw.input_norm,
8590                self.rms_eps,
8591                self.norm_style,
8592                &mut self.ws.n1,
8593            );
8594            inference::rms_norm_into(
8595                &h2,
8596                &lw.input_norm,
8597                self.rms_eps,
8598                self.norm_style,
8599                &mut self.ws.n2,
8600            );
8601
8602            let (a1, a2) = match &lw.attn {
8603                AttnKind::Mla(_) => unreachable!("MLA has no MTP/pair path"),
8604                AttnKind::Kda(_) => unreachable!("KDA has no MTP/pair path"),
8605                AttnKind::Bounded(w) => {
8606                    // Two sequential positions of the bounded operator
8607                    // (the ring's causal order is the pair's order).
8608                    let rope = self
8609                        .bounded_rope
8610                        .clone()
8611                        .expect("bounded layer without an installed rotation table");
8612                    let cfg = crate::bounded::BoundedAttnCfg {
8613                        num_heads: self.num_heads,
8614                        num_kv_heads: self.num_kv_heads,
8615                        head_dim: self.head_dim,
8616                        hidden_size: hs,
8617                        scale: self.attn_scale,
8618                        rope: &rope,
8619                        pool: pool.as_deref(),
8620                    };
8621                    let a1 = crate::bounded::bounded_attention(
8622                        &self.ws.n1,
8623                        w,
8624                        &mut self.kv_cache.layers[li],
8625                        &cfg,
8626                    );
8627                    let a2 = crate::bounded::bounded_attention(
8628                        &self.ws.n2,
8629                        w,
8630                        &mut self.kv_cache.layers[li],
8631                        &cfg,
8632                    );
8633                    (a1, a2)
8634                }
8635                AttnKind::Linear(w) => {
8636                    let cfg = self.vmf_cfg.expect("linear layer without vmf_cfg");
8637                    let layer = &mut self.kv_cache.layers[li];
8638                    let (state, scratch) = (&mut layer.linear_state, &mut layer.linear_scratch);
8639                    vmf_phase_pair(
8640                        &self.ws.n1,
8641                        &self.ws.n2,
8642                        w,
8643                        &cfg,
8644                        state,
8645                        scratch,
8646                        self.pool.as_deref(),
8647                    )
8648                }
8649                AttnKind::LinearGdn(w) => {
8650                    let cfg = self.gdn_cfg.expect("gdn layer without gdn_cfg");
8651                    let layer = &mut self.kv_cache.layers[li];
8652                    let (state, scratch) = (&mut layer.linear_state, &mut layer.linear_scratch);
8653                    gdn_pair(
8654                        &self.ws.n1,
8655                        &self.ws.n2,
8656                        w,
8657                        &cfg,
8658                        state,
8659                        scratch,
8660                        self.pool.as_deref(),
8661                    )
8662                }
8663                AttnKind::ShortConv(w) => {
8664                    let cfg = self
8665                        .short_conv_cfg
8666                        .expect("short-conv layer without short_conv_cfg");
8667                    let layer = &mut self.kv_cache.layers[li];
8668                    let (state, scratch) = (&mut layer.linear_state, &mut layer.linear_scratch);
8669                    short_conv_pair(
8670                        &self.ws.n1,
8671                        &self.ws.n2,
8672                        w,
8673                        &cfg,
8674                        state,
8675                        scratch,
8676                        self.pool.as_deref(),
8677                    )
8678                }
8679                AttnKind::Full {
8680                    wq,
8681                    wk,
8682                    wv,
8683                    wo,
8684                    q_norm,
8685                    k_norm,
8686                    output_gate,
8687                    softplus_gate,
8688                    bias,
8689                } => {
8690                    let inv_freq_l = self.layer_inv_freq(li);
8691                    let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
8692                    let cfg = QwenAttnCfg {
8693                        num_heads: self.layer_num_heads(li),
8694                        num_kv_heads: nkv_l,
8695                        head_dim: hd_l,
8696                        hidden_size: hs,
8697                        position,
8698                        inv_freq: &inv_freq_l,
8699                        rotary_dim: rd_l,
8700                        scale: self.attn_scale,
8701                        softcap: self.attn_softcap,
8702                        window: self.layer_window(li),
8703                        v_norm: self.attn_v_norm,
8704                        qk_norm_after_rope: self.qk_norm_after_rope,
8705                        gate_sigmoid: self.proj_gate_sigmoid,
8706                        q_norm: q_norm.as_deref(),
8707                        k_norm: k_norm.as_deref(),
8708                        output_gate: *output_gate,
8709                        softplus_gate: softplus_gate
8710                            .as_ref()
8711                            .map(|(gate, per_head)| (gate, *per_head)),
8712                        rope_scale: self.layer_rope_scale(li),
8713                        bias: bias
8714                            .as_ref()
8715                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
8716                        rms_eps: eps,
8717                        norm_style: self.norm_style,
8718                        pool: pool.as_deref(),
8719                        v_head_dim: self.layer_v_dim(li),
8720                    };
8721                    attention::qwen_attention_pair(
8722                        &self.ws.n1,
8723                        &self.ws.n2,
8724                        wq,
8725                        wk,
8726                        wv,
8727                        wo,
8728                        &mut self.kv_cache.layers[li],
8729                        &cfg,
8730                    )
8731                }
8732            };
8733            let (a1, a2) = match &self.weights.layers[self.phys_layer(li)].attn_out_norm {
8734                Some(w) => (
8735                    inference::rms_norm(&a1, w, self.rms_eps, self.norm_style),
8736                    inference::rms_norm(&a2, w, self.rms_eps, self.norm_style),
8737                ),
8738                None => (a1, a2),
8739            };
8740            for i in 0..self.hidden_size {
8741                h1[i] += a1[i];
8742                h2[i] += a2[i];
8743            }
8744            let (mut a1, mut a2) = (a1, a2);
8745            attention::recycle_buf(&mut a1);
8746            attention::recycle_buf(&mut a2);
8747
8748            let lw = &self.weights.layers[self.phys_layer(li)];
8749            inference::rms_norm_into(
8750                &h1,
8751                &lw.post_norm,
8752                self.rms_eps,
8753                self.norm_style,
8754                &mut self.ws.p1,
8755            );
8756            inference::rms_norm_into(
8757                &h2,
8758                &lw.post_norm,
8759                self.rms_eps,
8760                self.norm_style,
8761                &mut self.ws.p2,
8762            );
8763            let (f1, f2) = match &lw.ffn {
8764                // Dual-branch layers need the raw residuals — run the
8765                // two positions through the same fn decode uses.
8766                FfnKind::DenseMoe(dm) => (
8767                    dense_moe_ffn(
8768                        dm,
8769                        &self.ws.p1,
8770                        &h1,
8771                        self.rms_eps,
8772                        self.norm_style,
8773                        self.pool.as_deref(),
8774                    ),
8775                    dense_moe_ffn(
8776                        dm,
8777                        &self.ws.p2,
8778                        &h2,
8779                        self.rms_eps,
8780                        self.norm_style,
8781                        self.pool.as_deref(),
8782                    ),
8783                ),
8784                FfnKind::Moe(m) if self.mimo_moe.is_dynamic(li, false) => (
8785                    moe_ffn_banked(&mut self.mimo_moe, li, m, &self.ws.p1, self.pool.as_deref()),
8786                    moe_ffn_banked(&mut self.mimo_moe, li, m, &self.ws.p2, self.pool.as_deref()),
8787                ),
8788                _ => ffn_forward_pair(
8789                    &lw.ffn,
8790                    &self.ws.p1,
8791                    &self.ws.p2,
8792                    self.pool.as_deref(),
8793                    None,
8794                ),
8795            };
8796            let (f1, f2) = match &self.weights.layers[self.phys_layer(li)].ffn_out_norm {
8797                Some(w) => (
8798                    inference::rms_norm(&f1, w, self.rms_eps, self.norm_style),
8799                    inference::rms_norm(&f2, w, self.rms_eps, self.norm_style),
8800                ),
8801                None => (f1, f2),
8802            };
8803            for i in 0..self.hidden_size {
8804                h1[i] += f1[i];
8805                h2[i] += f2[i];
8806            }
8807            let (mut f1, mut f2) = (f1, f2);
8808            attention::recycle_buf(&mut f1);
8809            attention::recycle_buf(&mut f2);
8810            if let Some(sc) = self.weights.layers[self.phys_layer(li)].layer_scale {
8811                for i in 0..self.hidden_size {
8812                    h1[i] *= sc;
8813                    h2[i] *= sc;
8814                }
8815            }
8816            // Looped Transformer: apply final norm at the end of each loop iteration.
8817            if self.is_loop_end(li) && li + 1 < self.num_layers {
8818                h1 = inference::rms_norm(
8819                    &h1,
8820                    &self.weights.final_norm,
8821                    self.rms_eps,
8822                    self.norm_style,
8823                );
8824                h2 = inference::rms_norm(
8825                    &h2,
8826                    &self.weights.final_norm,
8827                    self.rms_eps,
8828                    self.norm_style,
8829                );
8830            }
8831        }
8832        crate::gpu::set_layer(-1); // final/O(1) work is outside layer placement
8833        // Real O(1) prefill pairs may also carry tentative lane-2 recurrent
8834        // state. Commit it before publishing the transition epoch so the
8835        // next serial/device row cannot observe a new attention epoch with an
8836        // old GDN state. Speculative pairs run only when O(1) is inactive and
8837        // retain their existing caller-controlled commit/rollback semantics.
8838        if self.o1_active() {
8839            self.commit_linear_scratch();
8840        }
8841        self.o1_progress();
8842        self.swa_trim_tails();
8843        (h1, h2)
8844    }
8845
8846    /// Commit lane-2 linear states after an accepted draft.
8847    fn commit_linear_scratch(&mut self) {
8848        for layer in &mut self.kv_cache.layers {
8849            if !layer.linear_scratch.is_empty() {
8850                std::mem::swap(&mut layer.linear_state, &mut layer.linear_scratch);
8851                layer.linear_scratch.clear();
8852            }
8853        }
8854    }
8855
8856    /// This is the encoder counterpart to `forward_ids`: it runs a fresh
8857    /// sequence through the existing full/GDN hybrid stack and returns the
8858    /// final RMS-normalized hidden row for *every* token.  It is used by
8859    /// component pipelines whose text tower intentionally has no LM head
8860    /// (such as an image-conditioner Qwen3.5 model).  The sequence state is
8861    /// always released before returning, so a large text tower can be dropped
8862    /// immediately after conditioning a subsequent stage.
8863    pub fn encode_hidden_states(&mut self, ids: &[u32]) -> Result<Vec<f32>, String> {
8864        #[cfg(target_os = "macos")]
8865        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
8866        if ids.is_empty() {
8867            return Err("empty id sequence".to_string());
8868        }
8869
8870        self.nll_begin()?;
8871        let result = (|| {
8872            let hidden = self.prefill_input_rows(PrefillIn::Ids(ids), 0, None)?;
8873            self.nll_check_graph("encoder prefill", 0)?;
8874            let expected = ids.len() * self.hidden_size;
8875            if hidden.len() != expected {
8876                return Err(format!(
8877                    "encoder prefill returned {} values for {} rows of hidden size {}",
8878                    hidden.len(),
8879                    ids.len(),
8880                    self.hidden_size
8881                ));
8882            }
8883
8884            let mut out = vec![0.0f32; expected];
8885            for (src, dst) in hidden
8886                .chunks_exact(self.hidden_size)
8887                .zip(out.chunks_exact_mut(self.hidden_size))
8888            {
8889                inference::rms_norm_into(
8890                    src,
8891                    &self.weights.final_norm,
8892                    self.rms_eps,
8893                    self.norm_style,
8894                    dst,
8895                );
8896            }
8897            Ok(out)
8898        })();
8899        self.nll_end();
8900        result
8901    }
8902
8903    /// The last layer's hidden row (before the final norm) for every token
8904    /// of `ids`, from a fresh sequence, through the whole-stack prefill (the
8905    /// device chunk graph where there is one) — what a distillation cache of
8906    /// teacher outputs needs, at prefill speed.
8907    pub fn final_hidden_rows(&mut self, ids: &[u32]) -> Result<Vec<f32>, String> {
8908        #[cfg(target_os = "macos")]
8909        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
8910        if ids.is_empty() {
8911            return Err("empty id sequence".to_string());
8912        }
8913        self.nll_begin()?;
8914        let result = (|| {
8915            let hidden = self.prefill_input_rows(PrefillIn::Ids(ids), 0, None)?;
8916            self.nll_check_graph("hidden prefill", 0)?;
8917            if hidden.len() != ids.len() * self.hidden_size {
8918                return Err(format!("hidden prefill returned {} values for {} rows", hidden.len(), ids.len()));
8919            }
8920            Ok(hidden)
8921        })();
8922        self.nll_end();
8923        result
8924    }
8925
8926    /// Forward a full id sequence from a fresh cache and return the
8927    /// logits after the last position (golden-parity harness, bench).
8928    pub fn forward_ids(
8929        &mut self,
8930        ids: &[u32],
8931        task_mask: Option<&TaskMask>,
8932    ) -> Result<Vec<f32>, String> {
8933        #[cfg(target_os = "macos")]
8934        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
8935        if ids.is_empty() {
8936            return Err("empty id sequence".to_string());
8937        }
8938        self.clear_sequence_state();
8939        self.check_forward_graph("forward_ids setup", 0)?;
8940        if task_mask.is_none() {
8941            self.o1_begin();
8942        }
8943        let mut hidden = vec![0.0f32; self.hidden_size];
8944        let mut pos = 0usize;
8945        if let Some(b) = &mut self.dsv41 {
8946            let pool = self.pool.clone();
8947            let mut logits = Vec::new();
8948            crate::dsv41::forward_chunk(
8949                &b.0,
8950                &b.1,
8951                &b.2,
8952                &mut b.3,
8953                ids,
8954                0,
8955                pool.as_deref(),
8956                &mut logits,
8957            );
8958            if let Err(err) = self.o1_seal_checked() {
8959                self.clear_sequence_state();
8960                return Err(err);
8961            }
8962            return Ok(logits);
8963        }
8964        // Same routing predicate generation uses. Two reasons it must be
8965        // the same one: (1) a GDN hybrid's recurrent state is GPU-
8966        // resident, and a batched CPU prefill would build it on the host
8967        // only — decode then reads buffers the prefill never wrote;
8968        // (2) bench times THIS function and calls the result "prefill",
8969        // so a different path here reports a number production never
8970        // sees (W2 on 2×5090: 8.7 tok/s reported against 125 real).
8971        // Gemma-4: generation's batched-graph ingest (`g4_batch_ingest`),
8972        // so bench and the parity tools time and score the route run/serve
8973        // take; a refusal leaves the rest to the chunked host prefill.
8974        #[cfg(not(target_os = "macos"))]
8975        if task_mask.is_none() && ids.len() > 2 && self.g4_batch_prefill() {
8976            let (n, last) = match self.g4_batch_ingest(ids, 0, false) {
8977                Ok(r) => r,
8978                Err(e) => {
8979                    self.clear_sequence_state();
8980                    return Err(e);
8981                }
8982            };
8983            if n > 0 {
8984                hidden.copy_from_slice(&last);
8985                pos = n;
8986            }
8987        }
8988        if self.can_prefill_batched() && !self.graph_prefill_preferred() && ids.len() > 2 {
8989            // prefill-GEMM in chunks; only the last position's hidden is
8990            // needed. (o1-compatible: the batch path attends per position
8991            // through qwen_attention, which carries the collection hook.)
8992            let chunk = self.prefill_chunk();
8993            let hs = self.hidden_size;
8994            while pos < ids.len() {
8995                let end = (pos + chunk).min(ids.len());
8996                let hb = self.prefill_rows(&ids[pos..end], pos, task_mask)?;
8997                self.check_forward_graph("forward_ids batched prefill", end - 1)?;
8998                hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
8999                pos = end;
9000            }
9001        }
9002        // Same guards as generation's prefill — INCLUDING the graph one.
9003        // The CPU pair walk was intercepting positions that the resident
9004        // token graph would have run itself: on a GDN hybrid over wgpu
9005        // that is 89 ms of host forward against 7 ms of device submit,
9006        // and it made prefill look 12× slower than it is (W2 on an RTX
9007        // 5090, ctx 512: 11.2 tok/s with the walk, 136.6 without).
9008        // CMF_PAIR=0 opts out; a model whose layers live outside
9009        // `weights.layers` has no pair walk to take.
9010        if task_mask.is_none()
9011            && !self.graph_prefill_preferred()
9012            && !std::env::var("CMF_PAIR").is_ok_and(|v| v == "0")
9013            && self.pair_supported()
9014        {
9015            while pos + 1 < ids.len() {
9016                let e1 = self.embed_single(ids[pos]);
9017                let e2 = self.embed_single(ids[pos + 1]);
9018                let (_, h2) = self.forward_pair(&e1, &e2, pos);
9019                self.check_forward_graph("forward_ids pair", pos + 1)?;
9020                self.commit_linear_scratch();
9021                hidden = h2;
9022                pos += 2;
9023            }
9024        }
9025        // Resident Embryo graph: the prompt in chunks of one submit each
9026        // (the same device state and logits as the per-position walk).
9027        if task_mask.is_none() && pos == 0 && ids.len() > 1 {
9028            if let Some(lg) = self.embryo_prefill_chunked(ids, 0) {
9029                self.graph_logits = Some(lg);
9030                hidden = vec![0.0; self.hidden_size];
9031                pos = ids.len();
9032            }
9033        }
9034        while pos < ids.len() {
9035            hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, task_mask);
9036            self.check_forward_graph("forward_ids", pos)?;
9037            pos += 1;
9038        }
9039        if let Some(logits) = self.graph_logits.take() {
9040            // Resident stacks already applied final norm and their head in
9041            // the same submit; do not run a second norm/head over the zero
9042            // hidden sentinel returned by forward_layers_span.
9043            if let Err(err) = self.o1_seal_checked() {
9044                self.clear_sequence_state();
9045                return Err(err);
9046            }
9047            return Ok(logits);
9048        }
9049        // Harness contract: after forward_ids the cache is decode-ready —
9050        // under o1 that means sealed (bench measures the seal as part of
9051        // prefill, honestly).
9052        if let Err(err) = self.o1_seal_checked() {
9053            self.clear_sequence_state();
9054            return Err(err);
9055        }
9056        let normed = inference::rms_norm(
9057            &hidden,
9058            &self.weights.final_norm,
9059            self.rms_eps,
9060            self.norm_style,
9061        );
9062        Ok(self.lm_head_forward(&normed))
9063    }
9064
9065    /// Run the V4.1 stack one token at a time and retain logits for every
9066    /// position. This is a diagnostic surface for comparing a converted
9067    /// checkpoint with a tokenwise reference implementation.
9068    #[doc(hidden)]
9069    pub fn dsv41_serial_logits(&mut self, ids: &[u32]) -> Result<Vec<Vec<f32>>, String> {
9070        #[cfg(target_os = "macos")]
9071        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
9072        if ids.is_empty() {
9073            return Err("empty id sequence".to_string());
9074        }
9075        self.clear_sequence_state();
9076        self.dsv41
9077            .as_ref()
9078            .ok_or_else(|| "dsv41 serial logits require a DeepSeek-V4.1 model".to_string())?;
9079        self.o1_begin();
9080        let rows = {
9081            let pool = self.pool.clone();
9082            let b = self
9083                .dsv41
9084                .as_mut()
9085                .expect("dsv41 checked above; state cannot change during forward");
9086            let mut rows = Vec::with_capacity(ids.len());
9087            for (position, &id) in ids.iter().enumerate() {
9088                let mut logits = Vec::new();
9089                crate::dsv41::forward_token(
9090                    &b.0,
9091                    &b.1,
9092                    &b.2,
9093                    &mut b.3,
9094                    id,
9095                    position,
9096                    pool.as_deref(),
9097                    &mut logits,
9098                );
9099                rows.push(logits);
9100            }
9101            rows
9102        };
9103        self.o1_seal();
9104        Ok(rows)
9105    }
9106
9107    /// GPTQ calibration of DeepSeek-V4.1 (`cortiq quantize-moe`): the MoE
9108    /// input rows and routed expert ids of layers `lo..hi` for `ids` (a fresh
9109    /// sequence), see [`crate::dsv41::Dsv41MoeCapture`].
9110    pub fn dsv41_moe_capture(
9111        &mut self,
9112        ids: &[u32],
9113        lo: usize,
9114        hi: usize,
9115    ) -> Result<crate::dsv41::Dsv41MoeCapture, String> {
9116        #[cfg(target_os = "macos")]
9117        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
9118        if ids.is_empty() {
9119            return Err("empty id sequence".to_string());
9120        }
9121        self.dsv41
9122            .as_ref()
9123            .ok_or_else(|| "not a DeepSeek-V4.1 model".to_string())?;
9124        self.clear_sequence_state();
9125        let res = {
9126            let pool = self.pool.clone();
9127            let b = self.dsv41.as_mut().expect("dsv41 checked above");
9128            crate::dsv41::capture_window(&b.0, &b.1, &b.2, &mut b.3, ids, lo, hi, pool.as_deref())
9129        };
9130        self.clear_sequence_state();
9131        res
9132    }
9133
9134    /// Teacher-forced perplexity over a token sequence (phase-C gate:
9135    /// honest quant comparisons instead of prompt vibes).
9136    ///
9137    /// Attention is EXACT even on a model whose layers are flagged for
9138    /// the O(1) kernel — scoring the backbone is the default on purpose
9139    /// (it is the yardstick). `nll_ids_o1` scores the CONVERTED model.
9140    pub fn ppl_ids(&mut self, ids: &[u32]) -> Result<f64, String> {
9141        let (nll, cnt) = self.nll_ids_from(ids, 0)?;
9142        Ok((nll / cnt.max(1) as f64).exp())
9143    }
9144
9145    /// GPTQ calibration forward: `ids` through layers `0..upto` only, in
9146    /// layer-major chunks of `chunk` positions, with no vocabulary head and
9147    /// no scoring. The Hessian hook (`gptq_capture`) folds every
9148    /// projection's input on the way; the layers above the captured span
9149    /// and the head would only cost time. Falls back to a scored forward
9150    /// where the batched prefill does not apply.
9151    pub fn calib_forward(&mut self, ids: &[u32], upto: usize, chunk: usize) -> Result<(), String> {
9152        if !self.can_prefill_batched() {
9153            return self.nll_ids_from(ids, 0).map(|_| ());
9154        }
9155        self.nll_begin()?;
9156        let result: Result<(), String> = (|| {
9157            let chunk = chunk.max(1);
9158            let mut pos = 0usize;
9159            while pos < ids.len() {
9160                let end = (pos + chunk).min(ids.len());
9161                let _ = self.prefill_batch_span(PrefillIn::Ids(&ids[pos..end]), pos, None, 0, upto);
9162                self.nll_check_graph("calibration prefill", pos)?;
9163                pos = end;
9164            }
9165            Ok(())
9166        })();
9167        self.nll_end();
9168        result
9169    }
9170
9171    /// Layer-streamed calibration of one fresh window: layers `from..mid` run
9172    /// on `input` (the hidden state entering layer `from`; `None` = the
9173    /// token embeddings of `ids`) and their output is returned, then layers
9174    /// `mid..upto` run on that output for the capture hooks (their output is
9175    /// dropped). A caller that keeps the returned state per window runs each
9176    /// layer once over the whole conversion instead of re-running the stack
9177    /// below every layer. With `mid == from` the input is returned as is
9178    /// (`None` input: an empty vector).
9179    pub fn calib_window_span(
9180        &mut self,
9181        ids: &[u32],
9182        input: Option<&[f32]>,
9183        from: usize,
9184        mid: usize,
9185        upto: usize,
9186    ) -> Result<Vec<f32>, String> {
9187        if !self.can_prefill_batched() {
9188            return Err("calib_window_span: this architecture has no batched prefill".into());
9189        }
9190        self.nll_begin()?;
9191        let result: Result<Vec<f32>, String> = (|| {
9192            let h = if mid > from {
9193                let inp = match input {
9194                    Some(h) => PrefillIn::Hidden(h),
9195                    None => PrefillIn::Ids(ids),
9196                };
9197                let h = self.prefill_batch_span(inp, 0, None, from, mid);
9198                self.nll_check_graph("calibration span", 0)?;
9199                h
9200            } else {
9201                input.map(<[f32]>::to_vec).unwrap_or_default()
9202            };
9203            if upto > mid {
9204                let inp = if mid > from || input.is_some() {
9205                    PrefillIn::Hidden(&h)
9206                } else {
9207                    PrefillIn::Ids(ids)
9208                };
9209                let _ = self.prefill_batch_span(inp, 0, None, mid, upto);
9210                self.nll_check_graph("calibration span", 0)?;
9211            }
9212            Ok(h)
9213        })();
9214        self.nll_end();
9215        result
9216    }
9217
9218    /// GPTQ calibration of a Flash-Next-class MoE (`qwen4_exp`): MoE input
9219    /// rows and router logits of layers `lo..hi` for `ids` (a fresh
9220    /// sequence), per layer `[ids.len()][hidden + n_experts]`.
9221    #[cfg(feature = "gpu")]
9222    pub fn qwen4_capture(
9223        &mut self,
9224        ids: &[u32],
9225        lo: usize,
9226        hi: usize,
9227    ) -> Result<Vec<Vec<f32>>, String> {
9228        let b = self.qwen4_exp.as_mut().ok_or("not a qwen4_exp model")?;
9229        crate::qwen4_exp::capture_window(
9230            &b.0,
9231            &b.1,
9232            &b.2,
9233            &mut b.3,
9234            ids,
9235            lo,
9236            hi,
9237            &self.inv_freq,
9238            self.pool.as_deref(),
9239        )
9240    }
9241
9242    /// GPTQ calibration capture of an ordinary MoE stack (Qwen3.5/3.6
9243    /// MoE, Clover): `ids` from position 0 through the batched device graph
9244    /// in prefill-sized chunks, the MoE input rows and router logits of
9245    /// layers `lo..hi` copied out after routing. Returns per layer
9246    /// `[ids.len()][hidden + n_experts]`; the sequence state is left fresh.
9247    #[cfg(feature = "gpu")]
9248    pub fn moe_capture(&mut self, ids: &[u32], lo: usize, hi: usize) -> Result<Vec<Vec<f32>>, String> {
9249        use crate::gpu_wgpu::clover as cap;
9250        let (hs, hi) = (self.hidden_size, hi.min(self.num_layers));
9251        let ne = (lo..hi)
9252            .find_map(|li| match &self.weights.layers[self.phys_layer(li)].ffn {
9253                FfnKind::Moe(m) => Some(m.experts.len()),
9254                _ => None,
9255            })
9256            .ok_or("no MoE layer in the capture range")?;
9257        let chunk = self.prefill_chunk().max(1);
9258        self.clear_sequence_state();
9259        if !cap::moe_capture_begin(lo, hi, chunk, hs, ne) {
9260            return Err("capture unavailable (no wgpu device)".into());
9261        }
9262        let mut out: Vec<Vec<f32>> = (lo..hi).map(|_| Vec::with_capacity(ids.len() * (hs + ne))).collect();
9263        let res = (|| {
9264            for (ci, part) in ids.chunks(chunk).enumerate() {
9265                let mut hiddens = Vec::with_capacity(part.len() * hs);
9266                for &id in part {
9267                    hiddens.extend_from_slice(&self.embed_single(id));
9268                }
9269                let positions: Vec<usize> = (ci * chunk..ci * chunk + part.len()).collect();
9270                match self.try_batch_graph_wgpu(&mut hiddens, &positions, part.len(), None) {
9271                    crate::gpu::BatchGraphOutcome::Completed => {}
9272                    _ => return Err("the batched graph declined the calibration forward (VRAM budget?)".to_string()),
9273                }
9274                let (rows, written) = cap::moe_capture_read(part.len()).ok_or("capture readback")?;
9275                if written < hi - lo {
9276                    return Err(format!("{written} of {} captured layers ran through the device MoE arm", hi - lo));
9277                }
9278                for (o, r) in out.iter_mut().zip(rows) {
9279                    o.extend_from_slice(&r);
9280                }
9281            }
9282            Ok(())
9283        })();
9284        cap::moe_capture_end();
9285        self.clear_sequence_state();
9286        res.map(|_| out)
9287    }
9288
9289    /// Expert directory triples (gate, up, down) of an ordinary MoE layer,
9290    /// routed and shared.
9291    pub fn moe_expert_dirs(
9292        &self,
9293        li: usize,
9294    ) -> Option<(Vec<(usize, usize, usize)>, Option<(usize, usize, usize)>)> {
9295        let FfnKind::Moe(m) = &self.weights.layers.get(self.phys_layer(li))?.ffn else {
9296            return None;
9297        };
9298        let dirs = |d: &DenseFfn| Some((d.gate_proj.model_idx()?, d.up_proj.model_idx()?, d.down_proj.model_idx()?));
9299        let routed = m.experts.iter().map(dirs).collect::<Option<Vec<_>>>()?;
9300        let shared = match &m.shared {
9301            Some((se, _)) => Some(dirs(se)?),
9302            None => None,
9303        };
9304        Some((routed, shared))
9305    }
9306
9307    /// Expert directory triples of a `qwen4_exp` layer (routed, shared).
9308    pub fn qwen4_expert_dirs(
9309        &self,
9310        li: usize,
9311    ) -> Option<(Vec<(usize, usize, usize)>, Option<(usize, usize, usize)>)> {
9312        let b = self.qwen4_exp.as_ref()?;
9313        crate::qwen4_exp::expert_dirs(&b.1, li)
9314    }
9315
9316    /// Teacher-force the installed MTP draft stack and collect each draft
9317    /// FFN's own pre-down activation statistics.
9318    ///
9319    /// This is deliberately separate from [`Self::probe_ffn_mass`]: canonical
9320    /// MTP and MiMo sidecars can have independently compacted FFNs, and MiMo
9321    /// draft layers can differ in width from one another and from the trunk.
9322    /// The returned vector follows the actual draft stack (one entry for a
9323    /// canonical MTP head; one per MiMo draft layer). MiMo layer `k` replays
9324    /// the teacher-forced pairs `(hidden[j], ids[j + k + 1])`.
9325    ///
9326    /// Calibration starts from a fresh sequence and leaves the trunk, draft
9327    /// KVs, history, and device mirrors fresh on both success and error. It
9328    /// is therefore unsuitable for the middle of a conversation whose KV
9329    /// cache must be retained.
9330    pub fn probe_mtp_ffn(&mut self, ids: &[u32]) -> Result<Vec<MtpFfnProbe>, String> {
9331        self.reset_mtp_probe_state();
9332        // CPU scope avoids backend-dependent device math; this guard also
9333        // suppresses gate/oracle sparsity experiments for the duration so the
9334        // statistics and admission replay always execute the full dense FFN.
9335        let _exact_calibration = ExactMtpCalibrationGuard::enter();
9336        let result = if ids.len() < 2 {
9337            Err("MTP FFN probe needs at least two token ids".into())
9338        } else if ids
9339            .iter()
9340            .any(|&id| id as usize >= self.weights.embed_tokens.rows())
9341        {
9342            Err("MTP FFN probe received a token id outside the embedding table".into())
9343        } else if self.mtp.is_some() && self.mimo_mtp.is_some() {
9344            // A synthetic pipeline can carry both mechanisms, but merging
9345            // unlike draft semantics into one compaction plan is unsafe.
9346            Err("MTP FFN probe found both canonical and MiMo draft stacks".into())
9347        } else if self.mimo_mtp.is_some() {
9348            let mut st = self
9349                .mimo_mtp
9350                .take()
9351                .expect("checked MiMo MTP before detaching it");
9352            let result = crate::gpu::cpu_scope(|| {
9353                self.mimo_mtp_ffn_probe_teacher_forced(&mut st, ids)
9354            });
9355            // The owner is detached during the replay, so the common trunk
9356            // reset cannot clear its KVs. Restore it even on an error.
9357            st.reset();
9358            self.mimo_mtp = Some(st);
9359            result
9360        } else if self.mtp.is_some() {
9361            let mut mtp = self
9362                .mtp
9363                .take()
9364                .expect("checked canonical MTP before detaching it");
9365            let result = crate::gpu::cpu_scope(|| self.mtp_ffn_probe_teacher_forced(&mut mtp, ids));
9366            mtp.kv.clear();
9367            self.mtp = Some(mtp);
9368            result
9369        } else {
9370            Err("MTP FFN probe: this pipeline has no draft stack".into())
9371        };
9372        // Cover every early/error arm above: no failed calibration can leak
9373        // a partially populated trunk or draft cache into the next request.
9374        self.reset_mtp_probe_state();
9375        result
9376    }
9377
9378    /// Reset every state owner touched by [`Self::probe_mtp_ffn`].
9379    fn reset_mtp_probe_state(&mut self) {
9380        self.clear_sequence_state();
9381        if let Some(mtp) = self.mtp.as_mut() {
9382            mtp.kv.clear();
9383        }
9384        if let Some(mimo) = self.mimo_mtp.as_mut() {
9385            mimo.reset();
9386        }
9387        self.mtp_graph_mode = None;
9388    }
9389
9390    /// Canonical one-layer MTP teacher-forced replay. Its cache advances for
9391    /// every row so later attention inputs match a real draft warm-up rather
9392    /// than a collection of independent one-token measurements. The trunk is
9393    /// advanced row-by-row here instead of first materializing every hidden
9394    /// state, so canonical calibration remains O(hidden) transient RAM.
9395    fn mtp_ffn_probe_teacher_forced(
9396        &mut self,
9397        mtp: &mut MtpModule,
9398        ids: &[u32],
9399    ) -> Result<Vec<MtpFfnProbe>, String> {
9400        let FfnKind::Dense(dense) = &mtp.layer.ffn else {
9401            return Err("MTP FFN probe: canonical draft FFN is not dense".into());
9402        };
9403        let mut mass = vec![0.0f64; dense.gate_proj.rows()];
9404        let mut squared_mass = vec![0.0f64; dense.gate_proj.rows()];
9405        for position in 0..ids.len() - 1 {
9406            let hidden = self.forward_layers(&self.embed_single(ids[position]), position, None);
9407            if hidden.len() != self.hidden_size {
9408                return Err(format!(
9409                    "MTP FFN probe: trunk hidden width {} != {}",
9410                    hidden.len(),
9411                    self.hidden_size
9412                ));
9413            }
9414            self.mtp_probe_step_ffn(
9415                mtp,
9416                &hidden,
9417                ids[position + 1],
9418                position,
9419                &mut mass,
9420                &mut squared_mass,
9421            )?;
9422        }
9423        Ok(vec![MtpFfnProbe {
9424            mass,
9425            squared_mass,
9426            observed_positions: ids.len() - 1,
9427        }])
9428    }
9429
9430    /// CPU-only MTP block replay with a measurement between post-attention
9431    /// norm and down projection. The ordinary `mtp_step_hl` device path
9432    /// keeps these values resident and therefore cannot truthfully provide a
9433    /// per-neuron calibration statistic.
9434    fn mtp_probe_step_ffn(
9435        &mut self,
9436        mtp: &mut MtpModule,
9437        hidden: &[f32],
9438        next_token: u32,
9439        position: usize,
9440        mass: &mut [f64],
9441        squared_mass: &mut [f64],
9442    ) -> Result<(), String> {
9443        let e = self.embed_single(next_token);
9444        let mut cat = vec![0.0f32; 2 * self.hidden_size];
9445        let (cat_e, cat_h) = cat.split_at_mut(self.hidden_size);
9446        inference::rms_norm_into(&e, &mtp.enorm, self.rms_eps, self.norm_style, cat_e);
9447        inference::rms_norm_into(
9448            hidden,
9449            &mtp.hnorm,
9450            self.rms_eps,
9451            self.norm_style,
9452            cat_h,
9453        );
9454        let mut x = vec![0.0f32; self.hidden_size];
9455        mtp.eh_proj.matvec(&cat, &mut x, self.pool.as_deref());
9456
9457        let lw = &mtp.layer;
9458        inference::rms_norm_into(
9459            &x,
9460            &lw.input_norm,
9461            self.rms_eps,
9462            self.norm_style,
9463            &mut self.ws.n1,
9464        );
9465        let attn = match &lw.attn {
9466            AttnKind::Full {
9467                wq,
9468                wk,
9469                wv,
9470                wo,
9471                q_norm,
9472                k_norm,
9473                output_gate,
9474                softplus_gate,
9475                bias,
9476            } => {
9477                let mut cfg = self.attn_cfg(position);
9478                cfg.q_norm = q_norm.as_deref();
9479                cfg.k_norm = k_norm.as_deref();
9480                cfg.output_gate = *output_gate;
9481                cfg.softplus_gate = softplus_gate
9482                    .as_ref()
9483                    .map(|(gate, per_head)| (gate, *per_head));
9484                cfg.bias = bias
9485                    .as_ref()
9486                    .map(|(q, k, v)| (q.as_slice(), k.as_slice(), v.as_slice()));
9487                attention::qwen_attention(&self.ws.n1, wq, wk, wv, wo, &mut mtp.kv, &cfg)
9488            }
9489            _ => return Err("MTP FFN probe: draft attention is not full attention".into()),
9490        };
9491        for (dst, src) in x.iter_mut().zip(attn) {
9492            *dst += src;
9493        }
9494        inference::rms_norm_into(
9495            &x,
9496            &lw.post_norm,
9497            self.rms_eps,
9498            self.norm_style,
9499            &mut self.ws.p1,
9500        );
9501        let FfnKind::Dense(dense) = &lw.ffn else {
9502            return Err("MTP FFN probe: draft FFN is not dense".into());
9503        };
9504        accumulate_dense_ffn_activation_mass(
9505            dense,
9506            &self.ws.p1,
9507            1,
9508            self.pool.as_deref(),
9509            mass,
9510            squared_mass,
9511        )?;
9512        // Execute the same residual FFN arm as normal draft replay; the
9513        // final logits are not needed for teacher-forced statistics.
9514        let ffn = ffn_forward(&lw.ffn, &self.ws.p1, self.pool.as_deref(), None);
9515        for (dst, src) in x.iter_mut().zip(ffn) {
9516            *dst += src;
9517        }
9518        Ok(())
9519    }
9520
9521    /// DTG-MA calibration pass (Patent 2): run `ids` through the model
9522    /// (CPU path, per position) and return each layer's per-neuron
9523    /// activation mass Σ|silu(gate)·up| — the statistic the task-guided
9524    /// FFN mask is derived from.
9525    pub fn probe_ffn_mass(&mut self, ids: &[u32]) -> Vec<Vec<f64>> {
9526        self.clear_sequence_state();
9527        FFN_PROBE.with(|p| {
9528            *p.borrow_mut() = Some(vec![vec![0f64; self.intermediate_size]; self.num_layers]);
9529        });
9530        crate::gpu::cpu_scope(|| {
9531            for (pos, &id) in ids.iter().enumerate() {
9532                let emb = self.embed_single(id);
9533                let _ = self.forward_layers(&emb, pos, None);
9534            }
9535        });
9536        self.clear_sequence_state();
9537        FFN_PROBE
9538            .with(|p| p.borrow_mut().take())
9539            .unwrap_or_default()
9540    }
9541
9542    /// `probe_ffn_mass` over the BATCHED prefill: same accumulator, one
9543    /// sweep instead of one forward per token. What makes the statistic
9544    /// affordable on a 27B.
9545    pub fn probe_ffn_mass_batch(&mut self, ids: &[u32]) -> Result<Vec<Vec<f64>>, String> {
9546        if let Err(err) = self.nll_begin() {
9547            // A recorder can be left by a caller that was interrupted before
9548            // this request entered its scoring block.  Consume it even when
9549            // the preflight failure prevents initialization of a new one.
9550            let _ = FFN_PROBE.with(|p| p.borrow_mut().take());
9551            self.nll_end();
9552            return Err(err);
9553        }
9554        FFN_PROBE.with(|p| {
9555            *p.borrow_mut() = Some(vec![vec![0f64; self.intermediate_size]; self.num_layers]);
9556        });
9557        let result: Result<(), String> = (|| {
9558            for chunk in ids.chunks(256) {
9559                if chunk.len() < 2 {
9560                    continue;
9561                }
9562                self.nll_ids_masked(chunk, 0, None)?;
9563            }
9564            Ok(())
9565        })();
9566        self.nll_end();
9567        let probe = FFN_PROBE
9568            .with(|p| p.borrow_mut().take())
9569            .unwrap_or_default();
9570        match result {
9571            Ok(()) => Ok(probe),
9572            Err(err) => {
9573                drop(probe);
9574                Err(err)
9575            }
9576        }
9577    }
9578
9579    /// Teacher-forced PPL with a task mask active (sparse execution) —
9580    /// the quality gate for a DTG-MA-masked skill. Sequential per
9581    /// position: the batched prefill path is dense-only.
9582    pub fn ppl_ids_masked(&mut self, ids: &[u32], mask: &TaskMask) -> Result<f64, String> {
9583        self.nll_begin()?;
9584        let result: Result<f64, String> = (|| {
9585            let mut nll = 0f64;
9586            let mut cnt = 0usize;
9587            let mut hidden = vec![0f32; self.hidden_size];
9588            for (pos, &id) in ids.iter().enumerate() {
9589                if pos > 0 {
9590                    inference::rms_norm_into(
9591                        &hidden,
9592                        &self.weights.final_norm,
9593                        self.rms_eps,
9594                        self.norm_style,
9595                        &mut self.ws.n1,
9596                    );
9597                    let mut logits = self.lm_head_forward(&self.ws.n1);
9598                    let max = logits.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
9599                    let sum: f64 = logits.iter().map(|&v| ((v - max) as f64).exp()).sum();
9600                    let p = ((logits[id as usize] - max) as f64).exp() / sum.max(1e-300);
9601                    nll -= p.max(1e-300).ln();
9602                    cnt += 1;
9603                    attention::recycle_buf(&mut logits);
9604                }
9605                let emb = self.embed_single(id);
9606                hidden = self.forward_layers(&emb, pos, Some(mask));
9607                self.nll_check_graph("masked serial forward", pos)?;
9608                // Consume a possible graph logits side channel before the
9609                // next row.  Masked scoring normally disables that route,
9610                // but stale channel state must never survive a request.
9611                let _ = self.graph_logits.take();
9612            }
9613            Ok((nll / cnt.max(1) as f64).exp())
9614        })();
9615        self.nll_end();
9616        result
9617    }
9618
9619    /// Teacher-forced NLL sum + scored-token count over positions
9620    /// `start..len-1`, attention EXACT. Positions below `start` still
9621    /// run — they are the context — they are just not scored, so this
9622    /// pairs with `nll_ids_o1(ids, start)` over the very same tokens.
9623    ///
9624    /// Returning (nll, cnt) rather than a ppl is what lets a windowed
9625    /// caller combine windows before the exp, so every scored token
9626    /// weighs the same regardless of how the windows are cut.
9627    /// `nll_ids_from` with a task mask held active at every position.
9628    ///
9629    /// The batched prefill path does not thread masks, so this walks the
9630    /// per-position forward — slower, but it scores the file exactly the
9631    /// way `run --task` will serve it, which is the point of the gate
9632    /// that calls it. With `None` it defers to the fast path.
9633    /// Masked scoring rides the SAME batched sweep as unmasked scoring —
9634    /// the masked-inference fast path: `prefill_batch_masked` lands the
9635    /// per-visit FFN rows on the activations inside the fused arms. The
9636    /// per-position loop below remains only as the no-batch fallback.
9637    pub fn nll_ids_masked(
9638        &mut self,
9639        ids: &[u32],
9640        start: usize,
9641        task_mask: Option<&TaskMask>,
9642    ) -> Result<(f64, usize), String> {
9643        let task_mask = self.drop_open_mask(task_mask);
9644        self.nll_ids_inner(ids, start, task_mask)
9645    }
9646
9647    pub fn nll_ids_from(&mut self, ids: &[u32], start: usize) -> Result<(f64, usize), String> {
9648        self.nll_ids_inner(ids, start, None)
9649    }
9650
9651    fn nll_ids_inner(
9652        &mut self,
9653        ids: &[u32],
9654        start: usize,
9655        task_mask: Option<&TaskMask>,
9656    ) -> Result<(f64, usize), String> {
9657        self.nll_begin()?;
9658        // Block diffusion on Apple silicon: the Metal executor's causal path.
9659        #[cfg(target_os = "macos")]
9660        if task_mask.is_none() && self.is_block_diffusion() {
9661            if let Some(r) = self.clover_metal_nll(ids, start) {
9662                self.nll_end();
9663                return r;
9664            }
9665        }
9666        let result: Result<(f64, usize), String> = (|| {
9667            let mut nll = 0f64;
9668            let mut cnt = 0usize;
9669            // An unmasked quality run with the resident wgpu graph must score
9670            // the same stateful path used by generation.  The layer-major
9671            // GEMM prefill below is a valid CPU/GEMM oracle, but it seeds
9672            // neither the graph's device GDN state nor its device KV mirrors;
9673            // using it here would silently score a different execution.  Keep
9674            // masked scoring on the exact per-position path as before, and
9675            // let the serial arm below drive the graph-aware scorer.
9676            // Only native Metal has a fused graph lm_head contract.  Vulkan
9677            // and other graph backends may expose hidden state without the
9678            // optional logits side channel; preserve their established CPU
9679            // norm/head fallback instead of turning that valid route into a
9680            // hard missing-logits error.
9681            let (graph_quality, fused_head_quality) = nll_graph_policy(
9682                task_mask.is_none(),
9683                self.graph_prefill_preferred(),
9684                crate::gpu::q1_force(),
9685            );
9686            self.graph_head_required = fused_head_quality;
9687            self.graph_want_logits = fused_head_quality;
9688            #[cfg(target_os = "macos")]
9689            if graph_quality && std::env::var("CMF_METAL_BATCH_NLL").as_deref() != Ok("0") {
9690                match self.nll_batch_metal(ids, start) {
9691                    MetalBatchNllOutcome::Completed(nll, count) => {
9692                        return Ok((nll, count));
9693                    }
9694                    MetalBatchNllOutcome::Declined => {}
9695                    MetalBatchNllOutcome::Failed(err) => return Err(err),
9696                }
9697            }
9698            // CMF_NLL_SERIAL=1 scores position by position through the
9699            // decode path (the wgpu token graph where it admits the model)
9700            // — the perplexity gate of a decode kernel, which the batched
9701            // prefill arm below never runs.
9702            let force_serial = std::env::var("CMF_NLL_SERIAL").as_deref() == Ok("1");
9703            if self.can_prefill_batched() && !graph_quality && !force_serial {
9704                // prefill-GEMM: layer-major position chunks, lm_head batched
9705                // (254MB lm_head read once per chunk, not per position).
9706                // The layer chunk is large (grouping positions by MoE experts
9707                // wins with size), lm_head in sub-blocks (logit buffer
9708                // 32×vocab ≈ 32MB instead of 128×).
9709                // CMF_NLL_CHUNK: positions per prefill step (default 128).
9710                // A device scorer pays one round trip per projection per
9711                // step, so a 512-position step quarters them.
9712                let chunk_len: usize = std::env::var("CMF_NLL_CHUNK")
9713                    .ok()
9714                    .and_then(|v| v.parse().ok())
9715                    .filter(|&c: &usize| c > 0)
9716                    .unwrap_or(128);
9717                const LM_SUB: usize = 32;
9718                let n = ids.len().saturating_sub(1);
9719                let hs = self.hidden_size;
9720                let rows = self.weights.lm_head.rows();
9721                let mut pos = 0usize;
9722                let state_trace = std::env::var("CMF_STATE_TRACE").is_ok();
9723                while pos < n {
9724                    let end = (pos + chunk_len).min(n);
9725                    let bsz = end - pos;
9726                    let hb = self.prefill_rows(&ids[pos..end], pos, task_mask)?;
9727                    self.nll_check_graph("batched prefill", pos)?;
9728                    if state_trace && end % 256 == 0 {
9729                        self.trace_recurrent_state(end);
9730                    }
9731                    let mut k0 = 0usize;
9732                    while k0 < bsz {
9733                        let k1 = (k0 + LM_SUB).min(bsz);
9734                        let sb = k1 - k0;
9735                        // Sub-block entirely below the scored range: the KV
9736                        // it just built is all this pass needed from it.
9737                        if pos + k1 <= start {
9738                            k0 = k1;
9739                            continue;
9740                        }
9741                        let mut normed = vec![0.0f32; sb * hs];
9742                        for k in 0..sb {
9743                            let r = inference::rms_norm(
9744                                &hb[(k0 + k) * hs..(k0 + k + 1) * hs],
9745                                &self.weights.final_norm,
9746                                self.rms_eps,
9747                                self.norm_style,
9748                            );
9749                            normed[k * hs..(k + 1) * hs].copy_from_slice(&r);
9750                        }
9751                        let mut logits = vec![0.0f32; sb * rows];
9752                        self.weights
9753                            .lm_head
9754                            .matmat(&normed, sb, &mut logits, self.pool.as_deref());
9755                        for k in 0..sb {
9756                            if pos + k0 + k < start {
9757                                continue;
9758                            }
9759                            self.nll_check_graph("batched score row", pos + k0 + k)?;
9760                            let lg = &mut logits[k * rows..k * rows + self.vocab_size.min(rows)];
9761                            if let Some(mu) = self.logit_multiplier {
9762                                for v in lg.iter_mut() {
9763                                    *v *= mu;
9764                                }
9765                            }
9766                            // Gemma-class final-logit soft-capping: the
9767                            // decode paths apply it; scoring must too, or
9768                            // the uncapped softmax misprices every token.
9769                            if let Some(c) = self.final_softcap {
9770                                softcap_in_place(lg, c, self.pool.as_deref());
9771                            }
9772                            // Cortiq Embryo hierarchical head: same correction
9773                            // the decode path applies (lm_head_forward).
9774                            if let Some(cm) = self.head_clusters.clone() {
9775                                self.hierarchical_head_logprobs(
9776                                    &normed[k * hs..(k + 1) * hs],
9777                                    &cm,
9778                                    lg,
9779                                );
9780                            }
9781                            let lg = &logits[k * rows..k * rows + self.vocab_size.min(rows)];
9782                            let target = ids[pos + k0 + k + 1] as usize;
9783                            let max = lg.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
9784                            let lse: f64 = lg
9785                                .iter()
9786                                .map(|&v| ((v - max) as f64).exp())
9787                                .sum::<f64>()
9788                                .ln()
9789                                + max as f64;
9790                            nll += lse - lg[target] as f64;
9791                            cnt += 1;
9792                            if std::env::var("CMF_PPL_TRACE").is_ok() {
9793                                let top = lg
9794                                    .iter()
9795                                    .enumerate()
9796                                    .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
9797                                    .map(|(i, _)| i)
9798                                    .unwrap_or(0);
9799                                eprintln!(
9800                                    "BTRACE pos {} target {} nll {:.4} top {} lg_t {:.3} lg_top {:.3}",
9801                                    pos + k0 + k,
9802                                    target,
9803                                    lse - lg[target] as f64,
9804                                    top,
9805                                    lg[target],
9806                                    lg[top]
9807                                );
9808                            }
9809                        }
9810                        k0 = k1;
9811                    }
9812                    pos = end;
9813                }
9814                return Ok((nll, cnt));
9815            }
9816            for pos in 0..ids.len().saturating_sub(1) {
9817                let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, task_mask);
9818                self.nll_check_graph("serial forward", pos)?;
9819                // Architectures whose head lives inside their own stack return
9820                // the logits out of band and a zero hidden — DeepSeek-V4 folds
9821                // its hyper-connection copies between the last layer and the
9822                // norm, so it cannot hand back a vector this loop could use.
9823                // Scoring the zeros gave a perplexity of exactly the vocabulary
9824                // size, which is a uniform distribution reported as a
9825                // measurement. `generate` already reads this channel.
9826                let out_of_band = self.graph_logits.take();
9827                if self.graph_head_required && out_of_band.is_none() {
9828                    METAL_GRAPH_HEAD_MISS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
9829                    return Err(format!(
9830                        "fused Metal graph head did not complete at NLL position {pos}"
9831                    ));
9832                }
9833                if pos < start {
9834                    continue;
9835                }
9836                let logits = match out_of_band {
9837                    Some(lg) => lg,
9838                    None => {
9839                        let normed = inference::rms_norm(
9840                            &hidden,
9841                            &self.weights.final_norm,
9842                            self.rms_eps,
9843                            self.norm_style,
9844                        );
9845                        // lm_head_forward applies the final-logit softcap itself
9846                        // — capping again here double-squashed gemma-class
9847                        // logits (tanh∘tanh) and reported a flattered ppl.
9848                        self.lm_head_forward(&normed)
9849                    }
9850                };
9851                let target = ids[pos + 1] as usize;
9852                let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
9853                let lse: f64 = logits
9854                    .iter()
9855                    .map(|&v| ((v - max) as f64).exp())
9856                    .sum::<f64>()
9857                    .ln()
9858                    + max as f64;
9859                let tok_nll = lse - logits[target] as f64;
9860                if std::env::var("CMF_PPL_TRACE").is_ok() && pos < 10_000 {
9861                    let top = logits
9862                        .iter()
9863                        .enumerate()
9864                        .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
9865                        .map(|(i, _)| i)
9866                        .unwrap_or(0);
9867                    eprintln!(
9868                        "pos {pos:3} tgt {target:6} nll {tok_nll:7.3} | top1 {top:6} lg[t]={:.2} lg[top]={:.2}",
9869                        logits[target], logits[top]
9870                    );
9871                }
9872                nll += tok_nll;
9873                cnt += 1;
9874            }
9875            Ok((nll, cnt))
9876        })();
9877        self.nll_end();
9878        result
9879    }
9880
9881    /// Score one post-layer hidden with the same final norm/head path used by
9882    /// decode. Keeping this in one helper is important for the production
9883    /// batch scorer: its rows stop before the final norm, just like the
9884    /// per-position O(1) path below.
9885    fn nll_from_hidden(&mut self, hidden: &[f32], target: u32, pos: usize) -> f64 {
9886        let normed = inference::rms_norm(
9887            hidden,
9888            &self.weights.final_norm,
9889            self.rms_eps,
9890            self.norm_style,
9891        );
9892        // lm_head_forward applies the final-logit softcap itself — capping
9893        // again here double-squashed gemma-class logits in earlier scorers.
9894        let mut logits = self.lm_head_forward(&normed);
9895        let target = target as usize;
9896        let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
9897        let lse: f64 = logits
9898            .iter()
9899            .map(|&v| ((v - max) as f64).exp())
9900            .sum::<f64>()
9901            .ln()
9902            + max as f64;
9903        let tok_nll = lse - logits[target] as f64;
9904        if std::env::var("CMF_PPL_TRACE").is_ok() && pos < 10_000 {
9905            let top = logits
9906                .iter()
9907                .enumerate()
9908                .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
9909                .map(|(i, _)| i)
9910                .unwrap_or(0);
9911            eprintln!(
9912                "pos {pos:3} tgt {target:6} nll {tok_nll:7.3} | top1 {top:6} lg[t]={:.2} lg[top]={:.2}",
9913                logits[target], logits[top]
9914            );
9915        }
9916        attention::recycle_buf(&mut logits);
9917        tok_nll
9918    }
9919
9920    /// `CMF_STATE_TRACE`: per-layer magnitude of the recurrent record at
9921    /// position `pos` — the whole `linear_state` (vmf: S then the conv
9922    /// ring; GDN: conv ring then S), its recurrent S part alone, the
9923    /// bounded ring, and the last per-position KV row count.  The tool
9924    /// that separated the ~4k perplexity cliff of the 500-step exports
9925    /// (a state that keeps climbing past the trained window) from a
9926    /// runtime boundary; one line per layer, `STATE pos=… layer=…`.
9927    fn trace_recurrent_state(&self, pos: usize) {
9928        let stats = |v: &[f32]| -> (f64, f64) {
9929            if v.is_empty() {
9930                return (0.0, 0.0);
9931            }
9932            let (mut ss, mut mx) = (0f64, 0f64);
9933            for &x in v {
9934                ss += (x as f64) * (x as f64);
9935                mx = mx.max((x as f64).abs());
9936            }
9937            ((ss / v.len() as f64).sqrt(), mx)
9938        };
9939        for (li, l) in self.kv_cache.layers.iter().enumerate() {
9940            let lw = &self.weights.layers[self.phys_layer(li)];
9941            let (kind, s_len) = match &lw.attn {
9942                AttnKind::Linear(_) => ("vmf", self.vmf_cfg.map(|c| c.state_len()).unwrap_or(0)),
9943                AttnKind::LinearGdn(_) => ("gdn", self.gdn_cfg.map(|c| c.state_len()).unwrap_or(0)),
9944                AttnKind::Bounded(_) => ("bounded", 0),
9945                AttnKind::Full { .. } => ("full", 0),
9946                _ => ("other", 0),
9947            };
9948            let (rms, max) = stats(&l.linear_state);
9949            let s_part = if kind == "vmf" {
9950                &l.linear_state[..s_len.min(l.linear_state.len())]
9951            } else if kind == "gdn" {
9952                let ring = l
9953                    .linear_state
9954                    .len()
9955                    .saturating_sub(s_len)
9956                    .min(l.linear_state.len());
9957                &l.linear_state[ring..]
9958            } else {
9959                &l.linear_state[..0]
9960            };
9961            let (s_rms, s_max) = stats(s_part);
9962            let (ring_rms, ring_len) = match &l.bounded {
9963                Some(b) => (stats(&b.ring_k).0, b.ring_k.len()),
9964                None => (0.0, 0),
9965            };
9966            eprintln!(
9967                "STATE pos={pos} layer={li} kind={kind} state_len={} rms={rms:.5} max={max:.4} \
9968                 S_rms={s_rms:.5} S_max={s_max:.4} ring_k_rms={ring_rms:.5} ring_len={ring_len} kv_rows={}",
9969                l.linear_state.len(),
9970                l.seq_len
9971            );
9972        }
9973    }
9974
9975    /// Teacher-forced NLL of the CONVERTED model: the O(1) Nyström path
9976    /// is ACTIVE over the scored positions. Returns `Ok((nll sum, scored
9977    /// count))` over `prefill..len-1` and surfaces a post-mutation batch
9978    /// failure instead of returning a partial score.
9979    ///
9980    /// Runtime discipline, deliberately NOT the matrix probe's: the
9981    /// requested prefix plus any required deferred lead-in run the exact
9982    /// prompt pass — that pass is what freezes the landmarks and M — and
9983    /// every post-seal scored position goes through `NystromState::step()`,
9984    /// the same code decode runs.
9985    /// So the landmarks are PREFILL-frozen (what ships), not
9986    /// full-sequence oracles (what the published probe measured). When the
9987    /// requested prefix is shorter than the bounded transition, rows in the
9988    /// exact lead-in are still scored so the shifted target range is stable.
9989    ///
9990    /// Pair with `nll_ids_from(ids, prefill)` for the exact baseline
9991    /// over the identical token set — that ratio is the honest one.
9992    pub fn nll_ids_o1(&mut self, ids: &[u32], prefill: usize) -> Result<(f64, usize), String> {
9993        // This scorer consumes host hiddens, so never request the optional
9994        // token-graph lm_head side channel. `nll_begin` also consumes a
9995        // prior graph failure and clears only the cancel bit that failure
9996        // raised, leaving a caller-owned cancellation observable.
9997        self.nll_begin()?;
9998        let requested_prefix = (prefill > 0).then_some(prefill);
9999        self.o1_begin_with_prefix(requested_prefix);
10000        let n = ids.len().saturating_sub(1);
10001        let requested_start = prefill.min(n);
10002        // The exact prefix must reach the deferred boundary before a
10003        // collecting layer can convert. Rows between the requested start and
10004        // that boundary remain part of the public NLL range and are scored
10005        // from the same hidden pass below.
10006        let exact_end = if self.o1_active() {
10007            match requested_prefix {
10008                Some(requested) => self.o1_effective_boundary(requested),
10009                None => self
10010                    .o1_cfg
10011                    .as_ref()
10012                    .and_then(|c| crate::nystrom::o1_deferred_boundary(c.w, c.sink)),
10013            }
10014            .unwrap_or(requested_start)
10015            .min(n)
10016        } else {
10017            requested_start
10018        };
10019        let mut nll = 0f64;
10020        let mut cnt = 0usize;
10021
10022        // Exact prompt pass over ids[..exact_end]: the seal consumes its
10023        // q/k/v. Rows at or after requested_start are scored here when the
10024        // bounded lead-in is longer than the caller's requested prefix.
10025        let mut pos = 0usize;
10026        if self.can_prefill_batched() {
10027            const CHUNK: usize = 128;
10028            while pos < exact_end {
10029                let end = (pos + CHUNK).min(exact_end);
10030                let hiddens = self.prefill_batch(&ids[pos..end], pos);
10031                if self
10032                    .graph_failed
10033                    .swap(false, std::sync::atomic::Ordering::Relaxed)
10034                {
10035                    self.cancel
10036                        .store(false, std::sync::atomic::Ordering::Relaxed);
10037                    self.nll_end();
10038                    return Err("GPU graph failed during O(1) NLL prefix".into());
10039                }
10040                for row in 0..end - pos {
10041                    let score_pos = pos + row;
10042                    if score_pos >= requested_start && score_pos < n {
10043                        nll += self.nll_from_hidden(
10044                            &hiddens[row * self.hidden_size..(row + 1) * self.hidden_size],
10045                            ids[score_pos + 1],
10046                            score_pos,
10047                        );
10048                        cnt += 1;
10049                    }
10050                }
10051                pos = end;
10052            }
10053        } else {
10054            while pos < exact_end {
10055                let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, None);
10056                if self
10057                    .graph_failed
10058                    .swap(false, std::sync::atomic::Ordering::Relaxed)
10059                {
10060                    self.cancel
10061                        .store(false, std::sync::atomic::Ordering::Relaxed);
10062                    self.nll_end();
10063                    return Err("GPU graph failed during O(1) NLL prefix".into());
10064                }
10065                if pos >= requested_start && pos < n {
10066                    nll += self.nll_from_hidden(&hidden, ids[pos + 1], pos);
10067                    cnt += 1;
10068                }
10069                pos += 1;
10070            }
10071        }
10072        self.o1_seal_checked().map_err(|err| {
10073            self.nll_end();
10074            err
10075        })?;
10076
10077        // Reuse the production whole-token batch graph for the post-seal
10078        // suffix when the caller explicitly enabled both routes. This is a
10079        // teacher-forced scorer, so every row is ids[pos] and its target is
10080        // ids[pos + 1]; no speculative tail or rollback state is involved.
10081        // A first Declined is safe to handle with the established serial O(1)
10082        // path. Once a chunk completes, however, the device recurrent state
10083        // owns the sequence and a later decline must be terminal rather than
10084        // falling back to stale CPU state.
10085        let batch_k = std::env::var("CMF_BATCH_K")
10086            .ok()
10087            .and_then(|v| v.parse::<usize>().ok())
10088            .unwrap_or(0);
10089        let batch_admitted = batch_k > 0
10090            && self.can_prefill_batched()
10091            && self.o1_active()
10092            && std::env::var("CMF_O1_GPU").as_deref() == Ok("1")
10093            && (0..self.num_layers).all(|li| {
10094                let cache = &self.kv_cache.layers[self.phys_layer(li)];
10095                cache.o1.is_none() || cache.o1_views().is_some()
10096            });
10097        if std::env::var("CMF_GRAPH_PROF").is_ok() {
10098            eprintln!(
10099                "nll-batch: phase=post-seal admission={} requested_k={} scored_rows={}",
10100                batch_admitted,
10101                batch_k,
10102                n.saturating_sub(exact_end),
10103            );
10104        }
10105        let mut batch_completed = false;
10106        if batch_admitted && exact_end < n {
10107            let hs = self.hidden_size;
10108            let mut batch_pos = exact_end;
10109            while batch_pos < n {
10110                let end = (batch_pos + batch_k).min(n);
10111                let bk = end - batch_pos;
10112                let mut hiddens = vec![0.0f32; bk * hs];
10113                for (row, &id) in ids[batch_pos..end].iter().enumerate() {
10114                    hiddens[row * hs..(row + 1) * hs].copy_from_slice(&self.embed_single(id));
10115                }
10116                let positions: Vec<usize> = (batch_pos..end).collect();
10117                let t_batch = std::time::Instant::now();
10118                let outcome = self.try_batch_graph_wgpu(&mut hiddens, &positions, bk, None);
10119                if std::env::var("CMF_GRAPH_PROF").is_ok() {
10120                    let ms = t_batch.elapsed().as_secs_f64() * 1000.0;
10121                    eprintln!(
10122                        "nll-batch: phase=post-seal mode=o1 k={bk} pos={}..{} outcome={outcome:?} {ms:.1} ms ({:.1} tok/s)",
10123                        batch_pos,
10124                        end.saturating_sub(1),
10125                        bk as f64 / (ms / 1000.0),
10126                    );
10127                }
10128                if let Err(err) = self.nll_check_graph("batch graph", batch_pos) {
10129                    self.nll_end();
10130                    return Err(err);
10131                }
10132                match outcome {
10133                    crate::gpu::BatchGraphOutcome::Completed => {
10134                        batch_completed = true;
10135                        for row in 0..bk {
10136                            nll += self.nll_from_hidden(
10137                                &hiddens[row * hs..(row + 1) * hs],
10138                                ids[batch_pos + row + 1],
10139                                batch_pos + row,
10140                            );
10141                            cnt += 1;
10142                        }
10143                        batch_pos = end;
10144                    }
10145                    crate::gpu::BatchGraphOutcome::Declined => {
10146                        if batch_completed {
10147                            self.nll_end();
10148                            return Err(format!(
10149                                "O(1) NLL batch declined after completed chunk at position {batch_pos}"
10150                            ));
10151                        }
10152                        break;
10153                    }
10154                    crate::gpu::BatchGraphOutcome::Failed => {
10155                        self.nll_end();
10156                        return Err(format!(
10157                            "O(1) NLL batch graph failed after admission at position {batch_pos}"
10158                        ));
10159                    }
10160                }
10161            }
10162            if batch_completed && cnt == n.saturating_sub(requested_start) {
10163                self.nll_end();
10164                return Ok((nll, cnt));
10165            }
10166        }
10167
10168        // Serial O(1) fallback/reference. It is intentionally retained when
10169        // batch admission declines before mutation; callers must label this
10170        // CMF_BATCH_K=0/per-position path separately from the production
10171        // whole-token batch route.
10172        for pos in exact_end..n {
10173            let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, None);
10174            if self
10175                .graph_failed
10176                .swap(false, std::sync::atomic::Ordering::Relaxed)
10177            {
10178                self.cancel
10179                    .store(false, std::sync::atomic::Ordering::Relaxed);
10180                self.nll_end();
10181                return Err(format!(
10182                    "GPU graph failed during O(1) NLL serial scoring at position {pos}"
10183                ));
10184            }
10185            nll += self.nll_from_hidden(&hidden, ids[pos + 1], pos);
10186            cnt += 1;
10187        }
10188        self.nll_end();
10189        Ok((nll, cnt))
10190    }
10191
10192    /// Teacher-forced calibration data (B1): for each position, whether the
10193    /// argmax equals the actual next token, and the top-1 softmax prob
10194    /// (top-1 probability) under EACH temperature in `temps` — all from ONE forward
10195    /// pass (argmax/correctness are temperature-invariant; only p_max
10196    /// reshapes). Feeds `cortiq calibrate` (reliability/ECE + temperature
10197    /// fit): is the model's confidence a true property, or does it need a
10198    /// measured scaling?
10199    pub fn calib_ids(&mut self, ids: &[u32], temps: &[f32]) -> (Vec<bool>, Vec<Vec<f32>>) {
10200        self.clear_sequence_state();
10201        let n = ids.len().saturating_sub(1);
10202        let mut correct = Vec::with_capacity(n);
10203        let mut pmax = Vec::with_capacity(n);
10204        for pos in 0..n {
10205            let emb = self.embed_single(ids[pos]);
10206            let hidden = self.forward_layers(&emb, pos, None);
10207            let logits = if let Some(logits) = self.graph_logits.take() {
10208                logits
10209            } else {
10210                let normed = inference::rms_norm(
10211                    &hidden,
10212                    &self.weights.final_norm,
10213                    self.rms_eps,
10214                    self.norm_style,
10215                );
10216                // lm_head_forward applies the final-logit softcap itself —
10217                // capping again here double-squashed gemma-class logits
10218                // (tanh∘tanh) and reported a flattered ppl.
10219                self.lm_head_forward(&normed)
10220            };
10221            let target = ids[pos + 1] as usize;
10222            let (mut amax, mut mval) = (0usize, f32::NEG_INFINITY);
10223            for (i, &v) in logits.iter().enumerate() {
10224                if v > mval {
10225                    mval = v;
10226                    amax = i;
10227                }
10228            }
10229            correct.push(amax == target);
10230            let row: Vec<f32> = temps
10231                .iter()
10232                .map(|&t| {
10233                    let tt = t.max(1e-3);
10234                    let s: f32 = logits.iter().map(|&v| ((v - mval) / tt).exp()).sum();
10235                    1.0 / s.max(1e-12) // numerator at the max is exp(0)=1
10236                })
10237                .collect();
10238            pmax.push(row);
10239        }
10240        self.clear_sequence_state();
10241        (correct, pmax)
10242    }
10243
10244    /// Teacher-forced PPL with the dynamic router driving per-window
10245    /// skill switches (VMF experiment №2 measurement). Sequential (φ
10246    /// must update per token), returns (ppl, switch_count). The router
10247    /// must be enabled (`enable_dynamic_routing`); else this equals
10248    /// plain `ppl_ids`. The active skill when scoring token t shapes the
10249    /// logits for t+1 — on-policy over the held-out text itself.
10250    pub fn ppl_ids_dynamic(&mut self, ids: &[u32]) -> Result<(f64, usize), String> {
10251        if self.dyn_router.is_none() {
10252            return Ok((self.ppl_ids(ids)?, 0));
10253        }
10254        self.nll_begin()?;
10255        let saved_active = self.dyn_active;
10256        let mut router = self
10257            .dyn_router
10258            .take()
10259            .ok_or_else(|| "dynamic router disappeared before PPL scoring".to_string())?;
10260        router.reset();
10261        self.dyn_phi_seen = 0;
10262        let _ = self.set_active_skill(None);
10263
10264        let result: Result<(f64, usize), String> = (|| {
10265            let mut nll = 0f64;
10266            let mut cnt = 0usize;
10267            for pos in 0..ids.len().saturating_sub(1) {
10268                let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, None);
10269                self.nll_check_graph("dynamic serial forward", pos)?;
10270                let out_of_band = self.graph_logits.take();
10271                let mut logits = match out_of_band {
10272                    Some(lg) => lg,
10273                    None => {
10274                        let normed = inference::rms_norm(
10275                            &hidden,
10276                            &self.weights.final_norm,
10277                            self.rms_eps,
10278                            self.norm_style,
10279                        );
10280                        // lm_head_forward applies the final-logit softcap itself —
10281                        // capping again here double-squashed gemma-class logits
10282                        // and reported a flattered ppl.
10283                        self.lm_head_forward(&normed)
10284                    }
10285                };
10286                let target = ids[pos + 1] as usize;
10287                let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
10288                let lse: f64 = logits
10289                    .iter()
10290                    .map(|&v| ((v - max) as f64).exp())
10291                    .sum::<f64>()
10292                    .ln()
10293                    + max as f64;
10294                let tok_nll = lse - logits[target] as f64;
10295                if std::env::var("CMF_PPL_TRACE").is_ok() && pos < 10_000 {
10296                    let top = logits
10297                        .iter()
10298                        .enumerate()
10299                        .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
10300                        .map(|(i, _)| i)
10301                        .unwrap_or(0);
10302                    eprintln!(
10303                        "pos {pos:3} tgt {target:6} nll {tok_nll:7.3} | top1 {top:6} lg[t]={:.2} lg[top]={:.2}",
10304                        logits[target], logits[top]
10305                    );
10306                }
10307                nll += tok_nll;
10308                cnt += 1;
10309                attention::recycle_buf(&mut logits);
10310                // Route on the evolving phi (drives the NEXT token's skill).
10311                let phi = self.dyn_phi_ema.clone();
10312                if let Some(new_active) = router.step(&phi, pos) {
10313                    let _ = self.set_active_skill(new_active);
10314                }
10315            }
10316            Ok(((nll / cnt.max(1) as f64).exp(), router.switches.len()))
10317        })();
10318
10319        // Restore the detached router and the active overlay on both success
10320        // and failure. The scoring state is cleared independently below.
10321        let _ = self.set_active_skill(saved_active);
10322        self.dyn_router = Some(router);
10323        self.nll_end();
10324        result
10325    }
10326
10327    /// Routing probe φ (spec §9): mean-pooled hidden after `layer`.
10328    pub fn probe_phi(&mut self, ids: &[u32], layer: usize) -> Vec<f32> {
10329        self.clear_sequence_state();
10330        let mut acc = vec![0f32; self.hidden_size];
10331        for (pos, &id) in ids.iter().enumerate() {
10332            let h = self.forward_layers_upto(&self.embed_single(id), pos, None, Some(layer));
10333            for (a, v) in acc.iter_mut().zip(&h) {
10334                *a += v;
10335            }
10336        }
10337        let n = ids.len().max(1) as f32;
10338        for a in acc.iter_mut() {
10339            *a /= n;
10340        }
10341        self.clear_sequence_state();
10342        acc
10343    }
10344
10345    /// Router-v2 φ probe (spec §9.4, `phi.pool = "span_mean"`): the hidden
10346    /// AFTER `layer` — the same per-position walk and the same quantity as
10347    /// [`Self::probe_phi`] — averaged over the positions in `span` only
10348    /// (the user text between the template's prefix and suffix ids), NOT
10349    /// unit-normalized (the decision normalizes). The walk stops at
10350    /// `span.end`: causality makes the later positions irrelevant, so the
10351    /// result is bit-identical to probing `ids[..span.end]`. An empty span
10352    /// gives the zero vector, which the decision treats as degenerate.
10353    ///
10354    /// Every sequence state is reset before and after — the host KV/ring/
10355    /// recurrent state, the reuse keys (`kv_history`, `kv_prefix`) and the
10356    /// device graph's sequence — so run it on a pipeline that does not
10357    /// also serve a conversation (its prefix reuse would be lost).
10358    pub fn probe_phi_span(
10359        &mut self,
10360        ids: &[u32],
10361        layer: usize,
10362        span: std::ops::Range<usize>,
10363    ) -> Vec<f32> {
10364        #[cfg(target_os = "macos")]
10365        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
10366        let end = span.end.min(ids.len());
10367        let start = span.start.min(end);
10368        let reset = |p: &mut Self| p.clear_sequence_state();
10369        reset(self);
10370        let mut acc = vec![0f32; self.hidden_size];
10371        for (pos, &id) in ids[..end].iter().enumerate() {
10372            let h = self.forward_layers_upto(&self.embed_single(id), pos, None, Some(layer));
10373            if pos >= start {
10374                for (a, v) in acc.iter_mut().zip(&h) {
10375                    *a += v;
10376                }
10377            }
10378        }
10379        let n = end - start;
10380        if n > 0 {
10381            let n = n as f32;
10382            for a in acc.iter_mut() {
10383                *a /= n;
10384            }
10385        }
10386        reset(self);
10387        acc
10388    }
10389
10390    /// One decode step of the current sequence: forward `token` at
10391    /// `position` (the cache holds positions `[0, position)`, e.g. after
10392    /// [`Self::forward_ids`]) and return the next-token logits — the same
10393    /// forward and head the generation loop runs (resident-graph logits
10394    /// when the graph ran, final norm + lm_head otherwise). The logit-dump
10395    /// tools drive greedy decoding with it so every position is observable.
10396    pub fn decode_step_logits(&mut self, token: u32, position: usize) -> Vec<f32> {
10397        #[cfg(target_os = "macos")]
10398        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
10399        self.graph_logits = None;
10400        let hidden = self.forward_layers(&self.embed_single(token), position, None);
10401        if let Some(logits) = self.graph_logits.take() {
10402            return logits;
10403        }
10404        inference::rms_norm_into(
10405            &hidden,
10406            &self.weights.final_norm,
10407            self.rms_eps,
10408            self.norm_style,
10409            &mut self.ws.n1,
10410        );
10411        self.lm_head_forward(&self.ws.n1)
10412    }
10413
10414    /// Layer-major batched prefill (prefill-GEMM): full-attention —
10415    /// per-position with the existing operators (KV grows naturally,
10416    /// causality preserved), GDN projections / FFN / MoE — batched
10417    /// (a weight row is read from DRAM once per chunk, not per
10418    /// position). Returns the hidden of all positions [b × hidden].
10419    fn prefill_batch(&mut self, ids: &[u32], start_pos: usize) -> Vec<f32> {
10420        self.prefill_batch_masked(ids, start_pos, None)
10421    }
10422
10423    /// `prefill_batch` with a task mask honored on the dense-FFN panels
10424    /// (the masked-inference fast path: full fused compute, mask lands on
10425    /// the activations). The whole-chunk GPU graph is skipped for masked
10426    /// layers by the callers' arms; the per-GEMM device paths stay in
10427    /// play because the zeroing happens on the host between them.
10428    fn prefill_batch_masked(
10429        &mut self,
10430        ids: &[u32],
10431        start_pos: usize,
10432        task_mask: Option<&TaskMask>,
10433    ) -> Vec<f32> {
10434        self.prefill_batch_span(PrefillIn::Ids(ids), start_pos, task_mask, 0, usize::MAX)
10435    }
10436
10437    /// One prompt chunk through the whole stack, post-stack rows out (no
10438    /// final norm) — the ingest generation uses, shared by scoring and
10439    /// `forward_ids` so they measure the same execution: the batched wgpu
10440    /// graph's device prefix plus the host's batched walk for the rest when
10441    /// `batch_prefix_prefill` holds and the graph admits the chunk, else
10442    /// the host's chunked prefill. Err only when a graph that had mutated
10443    /// device state failed.
10444    fn prefill_rows(
10445        &mut self,
10446        ids: &[u32],
10447        pos: usize,
10448        task_mask: Option<&TaskMask>,
10449    ) -> Result<Vec<f32>, String> {
10450        self.prefill_input_rows(PrefillIn::Ids(ids), pos, task_mask)
10451    }
10452
10453    fn prefill_input_rows(
10454        &mut self,
10455        input: PrefillIn<'_>,
10456        pos: usize,
10457        task_mask: Option<&TaskMask>,
10458    ) -> Result<Vec<f32>, String> {
10459        self.mimo_moe_prepare();
10460        let hs = self.hidden_size;
10461        let bk = match input {
10462            PrefillIn::Ids(ids) => ids.len(),
10463            PrefillIn::Hidden(rows) => rows.len() / hs,
10464        };
10465        #[cfg(not(target_os = "macos"))]
10466        if task_mask.is_none()
10467            && !self.o1_active()
10468            && bk > 1
10469            && (self.batch_prefix_prefill()
10470                || (self.verify_exact_moe
10471                    && crate::gpu::enabled_here()
10472                    && crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode)))
10473        {
10474            let mut hiddens = match input {
10475                PrefillIn::Hidden(rows) => rows.to_vec(),
10476                PrefillIn::Ids(ids) => ids.iter().flat_map(|&id| self.embed_single(id)).collect(),
10477            };
10478            let positions: Vec<usize> = (pos..pos + bk).collect();
10479            let mut run = 0usize;
10480            match self.try_batch_graph_wgpu_prefix(
10481                &mut hiddens,
10482                &positions,
10483                bk,
10484                None,
10485                Some(&mut run),
10486            ) {
10487                crate::gpu::BatchGraphOutcome::Completed => {
10488                    let out = if run < self.num_layers {
10489                        self.prefill_batch_span(
10490                            PrefillIn::Hidden(&hiddens),
10491                            pos,
10492                            None,
10493                            run,
10494                            self.num_layers,
10495                        )
10496                    } else {
10497                        hiddens
10498                    };
10499                    return if self.graph_failed.load(std::sync::atomic::Ordering::Relaxed) {
10500                        Err("MiMo attention graph failed after admission".into())
10501                    } else {
10502                        Ok(out)
10503                    };
10504                }
10505                crate::gpu::BatchGraphOutcome::Failed => {
10506                    return Err("batched prefix prefill failed after admission".into());
10507                }
10508                crate::gpu::BatchGraphOutcome::Declined => {
10509                    // Rows an earlier chunk left on the device only.
10510                    #[cfg(feature = "gpu")]
10511                    self.pull_lagging_host_kv(0, self.num_layers, pos);
10512                }
10513            }
10514        }
10515        let out = self.prefill_batch_span(input, pos, task_mask, 0, usize::MAX);
10516        if self.graph_failed.load(std::sync::atomic::Ordering::Relaxed) {
10517            Err("batch tail graph failed after admission".into())
10518        } else {
10519            Ok(out)
10520        }
10521    }
10522
10523    /// The layer-major batched walk over a layer span [from..upto_excl):
10524    /// the whole prefill machinery (chunk graph, batched attends, GEMM
10525    /// panels) for a PARTIAL stack — the network split's prefill rides
10526    /// the same canon as the local one. Input is token ids (embeds
10527    /// itself, coordinator side) or ready boundary hiddens (worker side).
10528    fn prefill_batch_span(
10529        &mut self,
10530        input: PrefillIn<'_>,
10531        start_pos: usize,
10532        task_mask: Option<&TaskMask>,
10533        from: usize,
10534        upto_excl: usize,
10535    ) -> Vec<f32> {
10536        let hs = self.hidden_size;
10537        let b = match input {
10538            PrefillIn::Ids(ids) => ids.len(),
10539            PrefillIn::Hidden(hb) => hb.len() / hs,
10540        };
10541        let upto_excl = upto_excl.min(self.num_layers);
10542        // The CPU embed is deferred: when the chunk graph takes the run
10543        // from layer 0 it gathers the embeddings on the device instead.
10544        // A hidden input is ready by definition.
10545        let mut h: Vec<f32>;
10546        let mut h_ready;
10547        match input {
10548            PrefillIn::Ids(_) => {
10549                h = vec![0.0; b * hs];
10550                h_ready = false;
10551            }
10552            PrefillIn::Hidden(hb) => {
10553                h = hb.to_vec();
10554                h_ready = true;
10555            }
10556        }
10557        let fill_h = |h: &mut Vec<f32>, me: &Self| {
10558            if let PrefillIn::Ids(ids) = input {
10559                for (bi, &id) in ids.iter().enumerate() {
10560                    let e = me.embed_single(id);
10561                    h[bi * hs..(bi + 1) * hs].copy_from_slice(&e);
10562                }
10563                if let Ok(tp) = std::env::var("CMF_TRACE_POS") {
10564                    if let Ok(t) = tp.parse::<usize>() {
10565                        if t >= start_pos && t < start_pos + ids.len() {
10566                            let bi = t - start_pos;
10567                            let row = &h[bi * hs..(bi + 1) * hs];
10568                            let n: f32 = row.iter().map(|x| x * x).sum::<f32>().sqrt();
10569                            eprintln!(
10570                                "BATCH pos {t} embed: id {} |h| = {n:.6} h0 {:.6} h1 {:.6} | b={} start={start_pos} ids[..8]={:?}",
10571                                ids[bi],
10572                                row[0],
10573                                row[1],
10574                                ids.len(),
10575                                &ids[..ids.len().min(8)]
10576                            );
10577                        }
10578                    }
10579                }
10580            }
10581        };
10582        let (_nkv, _hd, _rd, eps) = (
10583            self.num_kv_heads,
10584            self.head_dim,
10585            self.rotary_dim,
10586            self.rms_eps,
10587        );
10588        let pool = self.pool.clone();
10589        let norm_style = self.norm_style;
10590        self.mimo_moe_prepare();
10591        let automatic_gpu_prefix = self.automatic_gpu_prefix();
10592
10593        #[cfg(target_os = "macos")]
10594        let mut chunk_skip_until = 0usize;
10595        for li in from..upto_excl {
10596            let _capacity_tail = automatic_gpu_prefix
10597                .filter(|&prefix| {
10598                    li >= prefix && !(self.verify_exact_moe && self.mimo_moe.is_dynamic(li, false))
10599                })
10600                .map(|_| crate::gpu::enter_cpu_scope());
10601            crate::gpu::set_layer(li as i64); // layer-split GPU/CPU
10602            // Spark-X2.5: the layer's int8 matrix-unit GEMMs take the
10603            // `zi_mm` tile, bit-identical to the 64x64 kernel and about
10604            // three times its rate (`gpu_wgpu::zi_gemm_route`).
10605            #[cfg(feature = "gpu")]
10606            let _fast_gemm = self
10607                .proj_gate_sigmoid
10608                .then(crate::gpu::enter_prefill_fast_gemm);
10609            // GPU chunk graph (default-on under CMF_GPU=1): a run of
10610            // consecutive eligible layers for the whole chunk in ONE
10611            // Metal submission — norm, QKV, RoPE with fused mirror
10612            // append, causal attend, O, FFN, hidden device-resident
10613            // across the run. Any refusal falls through to the CPU path.
10614            #[cfg(target_os = "macos")]
10615            if task_mask.is_none() {
10616                if li < chunk_skip_until {
10617                    continue;
10618                }
10619                // Device-side embedding needs a q8_row embedding matrix;
10620                // with any other layout the CPU fills `h` first and the
10621                // graph starts from a ready hidden (refusing the whole
10622                // run over the embedding alone kept q4t models — the
10623                // whole Nanbeige/Bonsai class — on the CPU prefill).
10624                if !h_ready && li == 0 && self.weights.embed_tokens.q8_row_parts().is_none() {
10625                    fill_h(&mut h, self);
10626                    h_ready = true;
10627                }
10628                let ids_for_embed = match input {
10629                    PrefillIn::Ids(ids) => (!h_ready && li == 0).then_some(ids),
10630                    PrefillIn::Hidden(_) => None,
10631                };
10632                let end = self.chunk_run_gpu(li, &mut h, b, start_pos, ids_for_embed, upto_excl);
10633                if end > li {
10634                    h_ready = true;
10635                    chunk_skip_until = end;
10636                    // Looped Transformer: the graph stopped at a loop
10637                    // boundary — apply final norm before the next iteration.
10638                    if self.is_loop_end(end - 1) && end < self.num_layers {
10639                        for bi in 0..b {
10640                            let normed = inference::rms_norm(
10641                                &h[bi * hs..(bi + 1) * hs],
10642                                &self.weights.final_norm,
10643                                eps,
10644                                norm_style,
10645                            );
10646                            h[bi * hs..(bi + 1) * hs].copy_from_slice(&normed);
10647                        }
10648                    }
10649                    continue;
10650                }
10651            }
10652            if !h_ready {
10653                fill_h(&mut h, self);
10654                h_ready = true;
10655            }
10656            if task_mask.is_none() && self.verify_exact_moe {
10657                let positions: Vec<_> = (start_pos..start_pos + b).collect();
10658                match self.mimo_graph_layer_rows(li, &mut h, &positions) {
10659                    crate::gpu::BatchGraphOutcome::Completed => continue,
10660                    crate::gpu::BatchGraphOutcome::Failed => return h,
10661                    crate::gpu::BatchGraphOutcome::Declined => {}
10662                }
10663            }
10664            #[cfg(feature = "gpu")]
10665            self.pull_lagging_host_kv(li, li + 1, start_pos);
10666            let lw = &self.weights.layers[self.phys_layer(li)];
10667            let t_attn = std::time::Instant::now();
10668            // ── attention ──
10669            match &lw.attn {
10670                AttnKind::Kda(w) => {
10671                    // Projections batched, recurrence sequential.
10672                    let cfg = self.kda_cfg.expect("kda layer without kda_cfg");
10673                    let mut normed = vec![0.0f32; b * hs];
10674                    for bi in 0..b {
10675                        inference::rms_norm_into(
10676                            &h[bi * hs..(bi + 1) * hs],
10677                            &lw.input_norm,
10678                            eps,
10679                            norm_style,
10680                            &mut normed[bi * hs..(bi + 1) * hs],
10681                        );
10682                    }
10683                    let attn = crate::linear_core::kda_forward_batch(
10684                        &normed,
10685                        b,
10686                        w,
10687                        &cfg,
10688                        &mut self.kv_cache.layers[li].linear_state,
10689                        pool.as_deref(),
10690                    );
10691                    for (dst, &a) in h.iter_mut().zip(&attn) {
10692                        *dst += a;
10693                    }
10694                }
10695                AttnKind::LinearGdn(w) => {
10696                    // Projections batched, recurrence sequential.
10697                    let cfg = self.gdn_cfg.expect("gdn layer without gdn_cfg");
10698                    let mut normed = vec![0.0f32; b * hs];
10699                    for bi in 0..b {
10700                        let r = inference::rms_norm(
10701                            &h[bi * hs..(bi + 1) * hs],
10702                            &lw.input_norm,
10703                            eps,
10704                            norm_style,
10705                        );
10706                        normed[bi * hs..(bi + 1) * hs].copy_from_slice(&r);
10707                    }
10708                    let attn = crate::linear_core::gdn_forward_batch(
10709                        &normed,
10710                        b,
10711                        w,
10712                        &cfg,
10713                        &mut self.kv_cache.layers[li].linear_state,
10714                        pool.as_deref(),
10715                    );
10716                    for (dst, &a) in h.iter_mut().zip(&attn) {
10717                        *dst += a;
10718                    }
10719                }
10720                AttnKind::ShortConv(w) => {
10721                    // Projections batched over the chunk; the conv walks the
10722                    // contiguous positions in order (same ring as decode).
10723                    let cfg = self
10724                        .short_conv_cfg
10725                        .expect("short-conv layer without short_conv_cfg");
10726                    let mut normed = vec![0.0f32; b * hs];
10727                    for bi in 0..b {
10728                        inference::rms_norm_into(
10729                            &h[bi * hs..(bi + 1) * hs],
10730                            &lw.input_norm,
10731                            eps,
10732                            norm_style,
10733                            &mut normed[bi * hs..(bi + 1) * hs],
10734                        );
10735                    }
10736                    let attn = short_conv_forward_batch(
10737                        &normed,
10738                        b,
10739                        w,
10740                        &cfg,
10741                        &mut self.kv_cache.layers[li].linear_state,
10742                        pool.as_deref(),
10743                    );
10744                    for (dst, &a) in h.iter_mut().zip(&attn) {
10745                        *dst += a;
10746                    }
10747                }
10748                AttnKind::Mla(w) => {
10749                    // Per-position prefill (correctness first; latent
10750                    // batching is a later optimization).
10751                    let inv_freq_l = self.layer_inv_freq(li);
10752                    let rs = self.layer_rope_scale(li);
10753                    let mut normed = vec![0.0f32; hs];
10754                    for bi in 0..b {
10755                        inference::rms_norm_into(
10756                            &h[bi * hs..(bi + 1) * hs],
10757                            &lw.input_norm,
10758                            eps,
10759                            norm_style,
10760                            &mut normed,
10761                        );
10762                        let ao = mla_attention(
10763                            w,
10764                            &normed,
10765                            &mut self.kv_cache.layers[li],
10766                            start_pos + bi,
10767                            &inv_freq_l,
10768                            rs,
10769                            eps,
10770                            pool.as_deref(),
10771                        );
10772                        for (dst, &a) in h[bi * hs..(bi + 1) * hs].iter_mut().zip(&ao) {
10773                            *dst += a;
10774                        }
10775                    }
10776                }
10777                AttnKind::Full {
10778                    wq,
10779                    wk,
10780                    wv,
10781                    wo,
10782                    q_norm,
10783                    k_norm,
10784                    output_gate,
10785                    softplus_gate,
10786                    bias,
10787                } => {
10788                    // Chunk-GEMM QKV/O; per-position causal attention
10789                    // inside (roadmap §3 P0 — full-attention prefill no
10790                    // longer re-reads the projection weights b times).
10791                    let mut normed = vec![0.0f32; b * hs];
10792                    for bi in 0..b {
10793                        inference::rms_norm_into(
10794                            &h[bi * hs..(bi + 1) * hs],
10795                            &lw.input_norm,
10796                            eps,
10797                            norm_style,
10798                            &mut normed[bi * hs..(bi + 1) * hs],
10799                        );
10800                    }
10801                    let inv_freq_l = self.layer_inv_freq(li);
10802                    let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
10803                    let cfg = QwenAttnCfg {
10804                        num_heads: self.layer_num_heads(li),
10805                        num_kv_heads: nkv_l,
10806                        head_dim: hd_l,
10807                        hidden_size: hs,
10808                        position: start_pos,
10809                        inv_freq: &inv_freq_l,
10810                        rotary_dim: rd_l,
10811                        scale: self.attn_scale,
10812                        softcap: self.attn_softcap,
10813                        window: self.layer_window(li),
10814                        v_norm: self.attn_v_norm,
10815                        qk_norm_after_rope: self.qk_norm_after_rope,
10816                        gate_sigmoid: self.proj_gate_sigmoid,
10817                        q_norm: q_norm.as_deref(),
10818                        k_norm: k_norm.as_deref(),
10819                        output_gate: *output_gate,
10820                        softplus_gate: softplus_gate
10821                            .as_ref()
10822                            .map(|(gate, per_head)| (gate, *per_head)),
10823                        rope_scale: self.layer_rope_scale(li),
10824                        bias: bias
10825                            .as_ref()
10826                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
10827                        rms_eps: eps,
10828                        norm_style,
10829                        pool: pool.as_deref(),
10830                        v_head_dim: self.layer_v_dim(li),
10831                    };
10832                    #[cfg(feature = "gpu")]
10833                    let _mirror = self
10834                        .prefill_mirror_target(li)
10835                        .map(crate::gpu::enter_prefill_mirror);
10836                    let mut attn = attention::qwen_attention_batch(
10837                        &normed,
10838                        b,
10839                        wq,
10840                        wk,
10841                        wv,
10842                        wo,
10843                        &mut self.kv_cache.layers[li],
10844                        &cfg,
10845                    );
10846                    if let Some(w) = &lw.attn_out_norm {
10847                        for bi in 0..b {
10848                            inference::rms_norm_into(
10849                                &attn[bi * hs..(bi + 1) * hs],
10850                                w,
10851                                eps,
10852                                norm_style,
10853                                &mut normed[bi * hs..(bi + 1) * hs],
10854                            );
10855                        }
10856                        attn.copy_from_slice(&normed);
10857                    }
10858                    for (dst, &a) in h.iter_mut().zip(&attn) {
10859                        *dst += a;
10860                    }
10861                }
10862                AttnKind::Bounded(w) => {
10863                    // Chunk-GEMM projections, the bounded operator per
10864                    // position over ring + chunk — never a growing KV.
10865                    let mut normed = vec![0.0f32; b * hs];
10866                    for bi in 0..b {
10867                        inference::rms_norm_into(
10868                            &h[bi * hs..(bi + 1) * hs],
10869                            &lw.input_norm,
10870                            eps,
10871                            norm_style,
10872                            &mut normed[bi * hs..(bi + 1) * hs],
10873                        );
10874                    }
10875                    let rope = self
10876                        .bounded_rope
10877                        .clone()
10878                        .expect("bounded layer without an installed rotation table");
10879                    let cfg = crate::bounded::BoundedAttnCfg {
10880                        num_heads: self.num_heads,
10881                        num_kv_heads: self.num_kv_heads,
10882                        head_dim: self.head_dim,
10883                        hidden_size: hs,
10884                        scale: self.attn_scale,
10885                        rope: &rope,
10886                        pool: pool.as_deref(),
10887                    };
10888                    let mut attn = crate::bounded::bounded_attention_batch(
10889                        &normed,
10890                        b,
10891                        w,
10892                        &mut self.kv_cache.layers[li],
10893                        &cfg,
10894                    );
10895                    if let Some(wn) = &lw.attn_out_norm {
10896                        for bi in 0..b {
10897                            inference::rms_norm_into(
10898                                &attn[bi * hs..(bi + 1) * hs],
10899                                wn,
10900                                eps,
10901                                norm_style,
10902                                &mut normed[bi * hs..(bi + 1) * hs],
10903                            );
10904                        }
10905                        attn.copy_from_slice(&normed);
10906                    }
10907                    for (dst, &a) in h.iter_mut().zip(&attn) {
10908                        *dst += a;
10909                    }
10910                    attention::recycle_buf(&mut attn);
10911                }
10912                AttnKind::Linear(w) => {
10913                    for bi in 0..b {
10914                        let normed = inference::rms_norm(
10915                            &h[bi * hs..(bi + 1) * hs],
10916                            &lw.input_norm,
10917                            eps,
10918                            norm_style,
10919                        );
10920                        vmf_phase_forward(
10921                            &normed,
10922                            w,
10923                            &self.vmf_cfg.expect("linear layer without vmf_cfg"),
10924                            &mut self.kv_cache.layers[li].linear_state,
10925                            pool.as_deref(),
10926                        )
10927                        .iter()
10928                        .enumerate()
10929                        .for_each(|(i, &a)| h[bi * hs + i] += a);
10930                    }
10931                }
10932            }
10933
10934            // ── FFN batched ──
10935            let lw = &self.weights.layers[self.phys_layer(li)];
10936            let mut post = vec![0.0f32; b * hs];
10937            for bi in 0..b {
10938                let r =
10939                    inference::rms_norm(&h[bi * hs..(bi + 1) * hs], &lw.post_norm, eps, norm_style);
10940                post[bi * hs..(bi + 1) * hs].copy_from_slice(&r);
10941            }
10942            // A restrictive per-visit FFN row lands on the activations
10943            // inside the dense arm; an all-open row costs nothing.
10944            let mask_row = task_mask
10945                .filter(|m| m.ffn_active_count(li) < self.intermediate_size)
10946                .and_then(|m| m.ffn_masks.get(li))
10947                .map(|v| v.as_slice());
10948            let attn_ns = t_attn.elapsed().as_nanos() as u64;
10949            let t_ffn = std::time::Instant::now();
10950            let mut ffn = match &lw.ffn {
10951                FfnKind::Dense(d) if !d.segs.is_empty() => {
10952                    tube_ffn(d, &post, b, pool.as_deref(), mask_row)
10953                }
10954                FfnKind::Dense(d) => dense_ffn_batch(d, &post, b, pool.as_deref(), mask_row),
10955                FfnKind::Moe(m) if self.verify_exact_moe && self.mimo_moe.is_dynamic(li, false) => {
10956                    moe_ffn_banked_rows(&mut self.mimo_moe, li, m, &post, b, hs, pool.as_deref())
10957                }
10958                FfnKind::Moe(m) if self.verify_exact_moe => {
10959                    moe_ffn_rows_exact(m, &post, b, hs, pool.as_deref())
10960                }
10961                // Keep prompt expert panels off the projection arena and
10962                // use their routes to prime the model-wide bank.
10963                FfnKind::Moe(m) if self.mimo_moe.is_dynamic(li, false) => {
10964                    let before = m.stats.borrow().clone();
10965                    let out = crate::gpu::cpu_scope(|| {
10966                        moe_ffn_batch(m, &post, b, hs, pool.as_deref(), None)
10967                    });
10968                    self.mimo_moe.prime(li, m, &before);
10969                    out
10970                }
10971                FfnKind::Moe(m) => moe_ffn_batch(m, &post, b, hs, pool.as_deref(), None),
10972                // Dual-branch layers run per position (the expert branch
10973                // reads the raw residual — nothing to batch yet).
10974                FfnKind::DenseMoe(dm) => {
10975                    let mut out = vec![0.0f32; b * hs];
10976                    for bi in 0..b {
10977                        let r = dense_moe_ffn(
10978                            dm,
10979                            &post[bi * hs..(bi + 1) * hs],
10980                            &h[bi * hs..(bi + 1) * hs],
10981                            eps,
10982                            norm_style,
10983                            pool.as_deref(),
10984                        );
10985                        out[bi * hs..(bi + 1) * hs].copy_from_slice(&r);
10986                    }
10987                    out
10988                }
10989            };
10990            if prefill_prof_on() {
10991                PREFILL_SPLIT[0].fetch_add(attn_ns, std::sync::atomic::Ordering::Relaxed);
10992                PREFILL_SPLIT[1].fetch_add(
10993                    t_ffn.elapsed().as_nanos() as u64,
10994                    std::sync::atomic::Ordering::Relaxed,
10995                );
10996            }
10997            if let Some(w) = &lw.ffn_out_norm {
10998                for bi in 0..b {
10999                    inference::rms_norm_into(
11000                        &ffn[bi * hs..(bi + 1) * hs],
11001                        w,
11002                        eps,
11003                        norm_style,
11004                        &mut post[bi * hs..(bi + 1) * hs],
11005                    );
11006                }
11007                ffn.copy_from_slice(&post);
11008            }
11009            for (dst, &f) in h.iter_mut().zip(&ffn) {
11010                *dst += f;
11011            }
11012            if let Some(sc) = lw.layer_scale {
11013                for v in h.iter_mut() {
11014                    *v *= sc;
11015                }
11016            }
11017            // CMF_LAYER_DUMP: every position's hidden after layer li.
11018            if self.layer_dump.is_some() {
11019                for bi in 0..b {
11020                    self.dump_layer_row(start_pos + bi, li, &h[bi * hs..(bi + 1) * hs]);
11021                }
11022            }
11023            if let Ok(tp) = std::env::var("CMF_TRACE_POS") {
11024                if let Ok(t) = tp.parse::<usize>() {
11025                    if t >= start_pos && t < start_pos + b {
11026                        let bi = t - start_pos;
11027                        let row = &h[bi * hs..(bi + 1) * hs];
11028                        let n: f32 = row.iter().map(|x| x * x).sum::<f32>().sqrt();
11029                        eprintln!(
11030                            "BATCH pos {t} after layer {li}: |h| = {n:.6} h0 {:.6} h1 {:.6}",
11031                            row[0], row[1]
11032                        );
11033                    }
11034                }
11035            }
11036            // CMF_DEBUG_LAYERS=1: per-layer hidden-state health of the
11037            // LAST prompt position — the knife for "which layer type
11038            // breaks first" on a new architecture.
11039            if std::env::var("CMF_DEBUG_LAYERS").is_ok() {
11040                let row = &h[(b - 1) * hs..b * hs];
11041                let rms =
11042                    (row.iter().map(|&v| (v as f64) * (v as f64)).sum::<f64>() / hs as f64).sqrt();
11043                let mx = row.iter().fold(0f32, |m, &v| m.max(v.abs()));
11044                eprintln!(
11045                    "layer {li:>3} {:>10} ffn={:<5} rms={rms:>12.4} max={mx:>12.4}",
11046                    match &self.weights.layers[self.phys_layer(li)].attn {
11047                        AttnKind::LinearGdn(_) => "gdn",
11048                        AttnKind::Linear(_) => "vmf",
11049                        AttnKind::ShortConv(_) => "conv",
11050                        _ => "attn",
11051                    },
11052                    match &lw.ffn {
11053                        FfnKind::Moe(_) => "moe",
11054                        FfnKind::Dense(_) => "dense",
11055                        FfnKind::DenseMoe(_) => "dense+moe",
11056                    },
11057                );
11058            }
11059            // Looped Transformer: apply final norm at the end of each loop iteration.
11060            if self.is_loop_end(li) && li + 1 < self.num_layers {
11061                for bi in 0..b {
11062                    let normed = inference::rms_norm(
11063                        &h[bi * hs..(bi + 1) * hs],
11064                        &self.weights.final_norm,
11065                        eps,
11066                        norm_style,
11067                    );
11068                    h[bi * hs..(bi + 1) * hs].copy_from_slice(&normed);
11069                }
11070            }
11071            if std::env::var("CMF_TRACE_H").is_ok() {
11072                let n = h[..hs].iter().map(|v| v.abs()).sum::<f32>() / hs as f32;
11073                let mx = h[..hs].iter().fold(0.0f32, |a, &v| a.max(v.abs()));
11074                eprintln!(
11075                    "layer {li}: mean|h|={n:.4} max|h|={mx:.2} scale={:?}",
11076                    lw.layer_scale
11077                );
11078            }
11079        }
11080        crate::gpu::set_layer(-1); // lm_head/final ops outside layer-split
11081        if prefill_prof_on() {
11082            let ms = |a: &std::sync::atomic::AtomicU64| {
11083                a.load(std::sync::atomic::Ordering::Relaxed) as f64 / 1e6
11084            };
11085            let sp = &attention::ATTN_SPLIT;
11086            let (qkv_enq, qkv_wait, ca_wait, zi) = wgpu_prefill_counters();
11087            eprintln!(
11088                "prefill-split: attention {:.1} ms (proj {:.1} [qkv enqueue {:.1} wait {:.1}, \
11089                 host gate {:.1}], host loop {:.1}, attend {:.1} \
11090                 [q pack {:.1}, device {:.1} of which readback {:.1}], o-proj {:.1}), \
11091                 ffn {:.1} ms (cumulative; zi_mm GEMMs {zi})",
11092                ms(&PREFILL_SPLIT[0]),
11093                ms(&sp[0]),
11094                qkv_enq,
11095                qkv_wait,
11096                ms(&sp[6]),
11097                ms(&sp[1]),
11098                ms(&sp[2]),
11099                ms(&sp[4]),
11100                ms(&sp[5]),
11101                ca_wait,
11102                ms(&sp[3]),
11103                ms(&PREFILL_SPLIT[1]),
11104            );
11105            let [calls, attended, wo, ring, reseed, f16] = wgpu_mirror_counters();
11106            eprintln!(
11107                "prefill-mirror: {calls} calls, {attended} attended ({wo} with O on the card), \
11108                 {ring} ring refusals, {reseed} reseeds, {f16} f16 fallbacks, {} upload \
11109                 fallbacks (cumulative)",
11110                attention::MIRROR_UPLOADS.load(std::sync::atomic::Ordering::Relaxed),
11111            );
11112        }
11113        // A batched span owns a complete set of positions. Publish any
11114        // collecting→sealed transition only after every layer has finished;
11115        // callers that cross into serial/device work must see the new epoch
11116        // before this function returns.
11117        self.o1_progress();
11118        // Every layer of the chunk has appended and attended its rows.
11119        self.swa_trim_tails();
11120        h
11121    }
11122
11123    /// Embed a single token.
11124    fn embed_single(&self, id: u32) -> Vec<f32> {
11125        let mut out = vec![0.0f32; self.hidden_size];
11126        if (id as usize) < self.weights.embed_tokens.rows() {
11127            self.weights.embed_tokens.row_f32(id as usize, &mut out);
11128        }
11129        if self.embed_multiplier != 1.0 {
11130            for v in out.iter_mut() {
11131                *v *= self.embed_multiplier;
11132            }
11133        }
11134        // DeepSeek-V4's hash layers route by TOKEN ID, so the id has to
11135        // reach the forward. It rides in slot 0 (the forward re-reads the
11136        // real embedding itself from the table).
11137        if self.dsv4.is_some() || self.dsv41.is_some() || self.qwen4_exp.is_some() {
11138            let mut v = vec![0.0f32; self.hidden_size.max(1)];
11139            v[0] = id as f32;
11140            return v;
11141        }
11142        // Gemma-3n: the per-layer-embedding half needs the token ID, so
11143        // it rides appended to the embedding; the g3n forward splits it.
11144        // Gemma-4 E-series PLE: same contract, its own table.
11145        if let Some(b) = &self.g4e {
11146            return b.0.extend_embedding(id, &out, self.pool.as_deref());
11147        }
11148        if let Some(b) = &self.g3n {
11149            return b.0.extend_embedding(id, &out, self.pool.as_deref());
11150        }
11151        out
11152    }
11153
11154    /// A run of consecutive prefill layers on the GPU for the whole
11155    /// chunk (default-on under CMF_GPU=1; CMF_GPU_CHUNK=0 disables).
11156    /// Eligibility per layer: q8_row weights, plain full attention
11157    /// (no output gate), F32 KV, no o1/masks/gemma extras. Returns the
11158    /// first layer index NOT processed (== `li0` when the run is empty).
11159    #[cfg(target_os = "macos")]
11160    fn chunk_run_gpu(
11161        &mut self,
11162        li0: usize,
11163        h: &mut [f32],
11164        b: usize,
11165        pos0: usize,
11166        embed_ids: Option<&[u32]>,
11167        cap: usize,
11168    ) -> usize {
11169        // (The old streaming attend needed a depth bound at ~1k; the
11170        // GEMM attention scales like the CPU path and lifted it.)
11171        // CMF_GPU_CHUNK=0 disables the graph.
11172        // Gemma-4 (`metal_gemma4`) brings its own kernels for the global
11173        // layers, V norm, attention scale and sandwich (`ChunkGemma4`).
11174        // `CMF_METAL_GEMMA4_CHUNK=0` keeps its prompt on the host walk.
11175        let gemma4 =
11176            self.metal_gemma4() && std::env::var("CMF_METAL_GEMMA4_CHUNK").as_deref() != Ok("0");
11177        if !crate::gpu::enabled_here()
11178            || std::env::var("CMF_GPU_CHUNK")
11179                .map(|v| v == "0")
11180                .unwrap_or(false)
11181            || b < 32
11182            // Sliding windows with per-layer RoPE ride the chunk graph
11183            // (`metal_graph_swa`: causal_softmax_win, per-layer tables).
11184            || (self.swa.is_some() && !self.metal_graph_swa() && !gemma4)
11185            || (self.global_attn.is_some() && !gemma4)
11186            // Chunk attention has no value-less learned-sink column. q1
11187            // decode can carry it through ordinary `gqa_attend`, but prompt
11188            // chunks must stay on the exact host attention path.
11189            || self.kv_cache.layers.iter().any(|l| l.sinks.is_some())
11190            // Per-layer KV heads and narrow V remain outside this chunk
11191            // graph (the q1 sliding-native door checks the same geometry).
11192            || (self.graph_attn_decline_reason().is_some() && !self.metal_graph_swa() && !gemma4)
11193            // Collection owns the exact Q trace and boundary conversion;
11194            // this chunk graph appends dense KV without feeding that trace.
11195            || self.o1_active()
11196            || (self.attn_v_norm && !gemma4)
11197            || !self.metal_rope_scale_ok()
11198            || ((self.attn_scale - 1.0 / (self.head_dim as f32).sqrt()).abs() > 1e-9 && !gemma4)
11199        {
11200            return li0;
11201        }
11202        let Some(model) = self.model.clone() else {
11203            return li0;
11204        };
11205        let (nh, nkv, hd, hs) = (
11206            self.num_heads,
11207            self.num_kv_heads,
11208            self.head_dim,
11209            self.hidden_size,
11210        );
11211        // Collect the longest run of consecutive eligible layers.
11212        // Looped Transformer: stop at the loop boundary so the CPU can
11213        // apply loop_final_norm between iterations.
11214        let loop_end = if self.loop_final_norm {
11215            ((li0 / self.physical_layers) + 1) * self.physical_layers
11216        } else {
11217            self.num_layers
11218        };
11219        let run_end = self.num_layers.min(loop_end).min(cap);
11220        // Each layer's own RoPE table (Spark-X2.5: sliding and full layers
11221        // differ); the global one for every model without sliding layers.
11222        // Declared before `layers`, which borrows it (and drops a routing
11223        // closure on MoE layers).
11224        let tables: Vec<std::sync::Arc<Vec<f32>>> =
11225            (li0..run_end).map(|li| self.layer_inv_freq(li)).collect();
11226        let mut layers: Vec<crate::gpu_metal::ChunkLayer> = Vec::new();
11227        let mut stored_at: Vec<usize> = Vec::new();
11228        for li in li0..run_end {
11229            let lw = &self.weights.layers[self.phys_layer(li)];
11230            if (lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some())
11231                && !gemma4
11232            {
11233                break;
11234            }
11235            let AttnKind::Full {
11236                wq,
11237                wk,
11238                wv,
11239                wo,
11240                q_norm,
11241                k_norm,
11242                output_gate: false,
11243                softplus_gate,
11244                bias,
11245            } = &lw.attn
11246            else {
11247                break;
11248            };
11249            // Spark-X2.5's per-head sigmoid g_proj gate, from f32 rows.
11250            let head_gate = match softplus_gate {
11251                None => None,
11252                Some((g, true)) if self.proj_gate_sigmoid => match g.f32_parts() {
11253                    Some((d, r, c)) if r == nh && c == hs => Some(d),
11254                    _ => break,
11255                },
11256                Some(_) => break,
11257            };
11258            // The FFN: a dense SiLU/GELU trio, or routed q4tp experts
11259            // (Mellum2.1: softmax router, no shared expert) whose first
11260            // trio stands in for the shape checks.
11261            let (gate_t, up_t, down_t, ffn_act, moe) = match &lw.ffn {
11262                FfnKind::Dense(d)
11263                    if (matches!(d.act, Act::Silu | Act::Gelu)
11264                        || (gemma4 && d.act == Act::GeluTanh))
11265                        && d.segs.is_empty() =>
11266                {
11267                    (&d.gate_proj, &d.up_proj, &d.down_proj, d.act, None)
11268                }
11269                FfnKind::Moe(m) => match chunk_moe_parts(m, hs) {
11270                    Some((router, experts, q8)) => {
11271                        let e0 = &m.experts[0];
11272                        let route = Box::new(move |lg: &[f32]| {
11273                            // `moe_ffn_batch`'s routing and weights, row by row.
11274                            let (idx, p, wsum) = moe_route(lg, m, None);
11275                            let mut st = m.stats.borrow_mut();
11276                            if st.len() < m.experts.len() {
11277                                st.resize(m.experts.len(), 0);
11278                            }
11279                            for &e in &idx {
11280                                st[e] += 1;
11281                            }
11282                            let w = idx.iter().map(|&e| p[e] / wsum).collect();
11283                            (idx, w)
11284                        });
11285                        (
11286                            &e0.gate_proj,
11287                            &e0.up_proj,
11288                            &e0.down_proj,
11289                            Act::Silu,
11290                            Some(crate::gpu_metal::ChunkMoe {
11291                                router,
11292                                experts,
11293                                q8,
11294                                route,
11295                            }),
11296                        )
11297                    }
11298                    None => break,
11299                },
11300                _ => break,
11301            };
11302            // q8_row (row_scale populated), q8_2f (plus its input field),
11303            // or q4_tiled / q4tp (row_scale empty — their scales are in the
11304            // payload). Mixing across the seven projections of one layer is
11305            // fine; the encoder branches per weight on the tensor's dtype.
11306            // Anything else refuses.
11307            fn cw(t: &QTensor) -> Option<(usize, usize, usize, &[f32])> {
11308                t.q8_row_parts()
11309                    .or_else(|| t.q8_2f_parts().map(|(i, r, c, rs, _)| (i, r, c, rs)))
11310                    .or_else(|| t.q4t_parts().map(|(i, r, c)| (i, r, c, &[][..])))
11311                    .or_else(|| t.q4tp_parts().map(|(i, r, c)| (i, r, c, &[][..])))
11312            }
11313            fn cf(t: &QTensor) -> &[f32] {
11314                t.q8_2f_parts().map_or(&[][..], |(_, _, _, _, cf)| cf)
11315            }
11316            let col = [cf(wq), cf(wk), cf(wv), cf(wo)];
11317            // q8_2f projections ride the chunk graph on the MoE layers it
11318            // was measured on (Mellum2.1); dense q8_2f stacks keep the host
11319            // walk until they are measured too. That includes a q8_2f
11320            // gate/up/down behind q4/q8_row attention: the chunk FFN has no
11321            // x·col staging, so it would run those weights without their
11322            // input field (0.8.14 refused every q8_2f weight here).
11323            // Gemma-4 reads every q8_2f weight through the blob GEMM with its
11324            // own field (`ChunkGemma4`), so its dense q8_2f stack rides.
11325            if moe.is_none()
11326                && !gemma4
11327                && (col.iter().any(|c| !c.is_empty())
11328                    || [gate_t, up_t, down_t].iter().any(|t| !cf(t).is_empty()))
11329            {
11330                break;
11331            }
11332            let parts = (
11333                cw(wq),
11334                cw(wk),
11335                cw(wv),
11336                cw(wo),
11337                cw(gate_t),
11338                cw(up_t),
11339                cw(down_t),
11340            );
11341            let (Some(pq), Some(pk), Some(pv), Some(po), Some(pg), Some(pu), Some(pd)) = parts
11342            else {
11343                break;
11344            };
11345            let layer = &self.kv_cache.layers[li];
11346            if layer.mode != crate::kv_cache::KvMode::F32 || layer.o1.is_some() {
11347                break;
11348            }
11349            // A trimmed tail holds every row the chunk's first query reads.
11350            debug_assert!(
11351                layer.base() == 0
11352                    || self
11353                        .layer_window(li)
11354                        .is_some_and(|w| layer.head_len(0) + 1 >= w)
11355            );
11356            stored_at.push(layer.head_len(0));
11357            // Per-layer geometry for Gemma-4 (512-wide global layers over
11358            // one KV head); every other model keeps the pipeline-wide one.
11359            let (nkv, hd) = if gemma4 {
11360                let (a, bb, _) = self.layer_geom(li);
11361                (a, bb)
11362            } else {
11363                (nkv, hd)
11364            };
11365            layers.push(crate::gpu_metal::ChunkLayer {
11366                model: &model,
11367                kv_id: self.graph_kv_id,
11368                layer: li,
11369                wq: pq,
11370                wk: pk,
11371                wv: pv,
11372                wo: po,
11373                gate: pg,
11374                up: pu,
11375                down: pd,
11376                input_norm: &lw.input_norm,
11377                post_norm: &lw.post_norm,
11378                bias: bias
11379                    .as_ref()
11380                    .map(|(a, bb, cc)| (a.as_slice(), bb.as_slice(), cc.as_slice())),
11381                q_norm: q_norm.as_deref(),
11382                k_norm: k_norm.as_deref(),
11383                inv_freq: &tables[li - li0],
11384                rope_scale: self.layer_rope_scale(li),
11385                rd: self.layer_geom(li).2,
11386                nh,
11387                nkv,
11388                hd,
11389                hs,
11390                inter: gate_t.rows(),
11391                gemma: matches!(self.norm_style, cortiq_core::NormStyle::Gemma),
11392                late_qk_norm: self.qk_norm_after_rope,
11393                eps: self.rms_eps as f32,
11394                window: self.layer_window(li),
11395                head_gate,
11396                act: ffn_act.graph_act().map_or(0, |a| a.code() as u8),
11397                g4: gemma4.then(|| crate::gpu_metal::ChunkGemma4 {
11398                    v_norm: self.attn_v_norm,
11399                    scale: self.attn_scale,
11400                    attn_out_norm: lw.attn_out_norm.as_deref(),
11401                    ffn_out_norm: lw.ffn_out_norm.as_deref(),
11402                    layer_scale: lw.layer_scale,
11403                }),
11404                col,
11405                moe,
11406            });
11407        }
11408        if layers.is_empty() {
11409            return li0;
11410        }
11411        // KV row width per layer (uniform but for Gemma-4).
11412        let rows_kv: Vec<(usize, usize)> = layers.iter().map(|l| (l.nkv, l.nkv * l.hd)).collect();
11413        let mut store: Vec<(Vec<f32>, Vec<f32>, Vec<f32>)> = stored_at
11414            .iter()
11415            .zip(&rows_kv)
11416            .map(|(&st, &(_, row))| (vec![0f32; b * row], vec![0f32; b * row], vec![0f32; st + b]))
11417            .collect();
11418        let mut io: Vec<crate::gpu_metal::ChunkIo> = Vec::with_capacity(layers.len());
11419        for (i, (ok, ov, oi)) in store.iter_mut().enumerate() {
11420            let li = layers[i].layer;
11421            let layer = &self.kv_cache.layers[li];
11422            let nkv = rows_kv[i].0;
11423            io.push(crate::gpu_metal::ChunkIo {
11424                cpu_stored: stored_at[i],
11425                cpu_gen: layer.generation(),
11426                cpu_k: (0..nkv).map(|g| layer.head_keys(g)).collect(),
11427                cpu_v: (0..nkv).map(|g| layer.head_values(g)).collect(),
11428                out_k: ok,
11429                out_v: ov,
11430                imp: oi,
11431            });
11432        }
11433        let n_run = layers.len();
11434        let last = layers.last().map(|l| l.layer + 1).unwrap_or(li0);
11435        // Device-side embedding when the run starts the model and the
11436        // embedding matrix is q8_row-mapped.
11437        let ep = embed_ids.and_then(|ids| {
11438            self.weights
11439                .embed_tokens
11440                .q8_row_parts()
11441                .map(|(idx, rows, _c, rs)| crate::gpu_metal::ChunkEmbed {
11442                    idx,
11443                    rows,
11444                    row_scale: rs,
11445                    ids,
11446                    mult: self.embed_multiplier,
11447                })
11448        });
11449        if embed_ids.is_some() && ep.is_none() {
11450            return li0;
11451        }
11452        if !crate::gpu_metal::chunk_run_gpu(&layers, &mut io, h, b, pos0, ep.as_ref()) {
11453            return li0;
11454        }
11455        drop(io);
11456        drop(layers);
11457        // CPU caches stay the owners of record: append the chunk rows
11458        // and bank the importance masses per layer.
11459        for (i, (ok, ov, oi)) in store.iter().enumerate().take(n_run) {
11460            let li = li0 + i;
11461            let row = rows_kv[i].1;
11462            let layer = &mut self.kv_cache.layers[li];
11463            for bi in 0..b {
11464                layer.append(
11465                    &ok[bi * row..(bi + 1) * row],
11466                    &ov[bi * row..(bi + 1) * row],
11467                    &[],
11468                );
11469            }
11470            layer.accumulate_imp(oi);
11471        }
11472        last
11473    }
11474
11475    /// Is layer `li` a sliding-window (local-RoPE) layer? Gemma-3:
11476    /// every `pattern`-th layer is global, the rest are local.
11477    fn layer_is_local(&self, li: usize) -> bool {
11478        if let Some(layers) = &self.sliding_layers {
11479            return layers.get(li).copied().unwrap_or(false);
11480        }
11481        match self.swa {
11482            Some((_, pattern)) => (li + 1) % pattern.max(1) != 0,
11483            None => false,
11484        }
11485    }
11486
11487    /// The RoPE table for layer `li` (local layers may have their own;
11488    /// Gemma-4 global layers use the proportional padded table).
11489    fn layer_inv_freq(&self, li: usize) -> std::sync::Arc<Vec<f32>> {
11490        if self.layer_is_local(li) {
11491            if let Some(f) = &self.inv_freq_local {
11492                return f.clone();
11493            }
11494        } else if let Some(f) = &self.inv_freq_global {
11495            return f.clone();
11496        }
11497        self.inv_freq.clone()
11498    }
11499
11500    /// The attend window for layer `li` (None = full context).
11501    fn layer_window(&self, li: usize) -> Option<usize> {
11502        self.swa
11503            .and_then(|(w, _)| self.layer_is_local(li).then_some(w))
11504    }
11505
11506    /// Drop every sliding layer's rows its window can no longer read
11507    /// (`LayerKvCache::trim_window`). Call only at a safe point — after a
11508    /// complete position / pair / chunk walk, never between a layer's
11509    /// appends and its attend. One comparison per layer below the trigger.
11510    ///
11511    /// Off with an MTP head: its verify oracles and rollbacks snapshot and
11512    /// compare per-layer row counts as positions (`CMF_METAL_VERIFY_CHECK`,
11513    /// the MTP caches), which a trimmed tail would break.
11514    fn swa_trim_tails(&mut self) {
11515        let Some((slack, align)) = self.swa_trim else {
11516            return;
11517        };
11518        if self.mtp.is_some() || self.mimo_mtp.is_some() || !self.dsv4_mtp.is_empty() {
11519            return;
11520        }
11521        for li in 0..self.num_layers.min(self.kv_cache.layers.len()) {
11522            if let Some(w) = self.layer_window(li) {
11523                self.kv_cache.layers[li].trim_window(w, slack, align);
11524            }
11525        }
11526    }
11527
11528    fn layer_num_heads(&self, li: usize) -> usize {
11529        self.attention_heads_per_layer
11530            .as_ref()
11531            .and_then(|v| v.get(li).copied())
11532            .unwrap_or(self.num_heads)
11533    }
11534
11535    fn layer_rope_scale(&self, li: usize) -> f32 {
11536        if self.layer_is_local(li) {
11537            self.rope_scale_local
11538        } else {
11539            self.rope_scale
11540        }
11541    }
11542
11543    /// Attention geometry of layer `li`: (num_kv_heads, head_dim,
11544    /// rotary_dim). Gemma-4 global layers override all three.
11545    fn layer_geom(&self, li: usize) -> (usize, usize, usize) {
11546        if !self.layer_is_local(li) {
11547            if let Some((ghd, gkv)) = self.global_attn {
11548                return (gkv, ghd, ghd);
11549            }
11550        }
11551        let rd = if self.layer_is_local(li) {
11552            self.rotary_dim_local.unwrap_or(self.rotary_dim)
11553        } else {
11554            self.rotary_dim
11555        };
11556        // A layer without positional encoding rotates nothing.
11557        let rd = if self
11558            .no_rope_layers
11559            .as_ref()
11560            .and_then(|v| v.get(li))
11561            .copied()
11562            == Some(true)
11563        {
11564            0
11565        } else {
11566            rd
11567        };
11568        (self.layer_num_kv_heads(li), self.head_dim, rd)
11569    }
11570
11571    /// KV heads of layer `li` (virtual index): the per-layer count when the
11572    /// model has one (MiMo-V2), else the uniform `num_kv_heads`.
11573    fn layer_num_kv_heads(&self, li: usize) -> usize {
11574        self.kv_heads_per_layer
11575            .as_ref()
11576            .and_then(|v| v.get(self.phys_layer(li)).copied())
11577            .unwrap_or(self.num_kv_heads)
11578    }
11579
11580    /// V head width of layer `li` (≤ its head_dim).
11581    fn layer_v_dim(&self, li: usize) -> usize {
11582        let (_, hd, _) = self.layer_geom(li);
11583        self.v_head_dim.unwrap_or(hd).min(hd)
11584    }
11585
11586    /// Install a per-layer KV geometry: KV heads per PHYSICAL layer and/or
11587    /// a V head width narrower than `head_dim` (MiMo-V2). Validates it and
11588    /// reshapes the caches of every layer whose KV head count differs from
11589    /// `num_kv_heads`. The loader and the tests share this one path, so a
11590    /// hand-built pipeline cannot hold a geometry the loader would refuse.
11591    /// Call before the first forward (it drops cached rows of reshaped
11592    /// layers). Refuses combinations whose paths would read it wrong:
11593    /// Gemma-4 global layers and MLA carry their own geometry.
11594    pub fn set_attn_geometry(
11595        &mut self,
11596        kv_heads_per_layer: Option<Vec<usize>>,
11597        v_head_dim: Option<usize>,
11598    ) -> Result<(), String> {
11599        if kv_heads_per_layer.is_some() || v_head_dim.is_some() {
11600            if self.global_attn.is_some() {
11601                return Err(
11602                    "per-layer KV heads / v_head_dim cannot combine with Gemma-4 global \
11603                     attention geometry"
11604                        .into(),
11605                );
11606            }
11607            if self
11608                .weights
11609                .layers
11610                .iter()
11611                .any(|lw| matches!(lw.attn, AttnKind::Mla(_)))
11612            {
11613                return Err("per-layer KV heads / v_head_dim cannot combine with MLA".into());
11614            }
11615        }
11616        if let Some(vd) = v_head_dim {
11617            if vd == 0 || vd > self.head_dim {
11618                return Err(format!(
11619                    "v_head_dim {vd} must be in 1..={} (head_dim)",
11620                    self.head_dim
11621                ));
11622            }
11623        }
11624        if let Some(v) = &kv_heads_per_layer {
11625            if v.len() != self.physical_layers {
11626                return Err(format!(
11627                    "kv_heads_per_layer has {} entries, expected {} layers",
11628                    v.len(),
11629                    self.physical_layers
11630                ));
11631            }
11632            for (li, &nkv) in v.iter().enumerate() {
11633                let is_attn = matches!(
11634                    self.weights.layers.get(li).map(|lw| &lw.attn),
11635                    Some(AttnKind::Full { .. }) | None
11636                );
11637                if !is_attn {
11638                    continue;
11639                }
11640                let nh = self
11641                    .attention_heads_per_layer
11642                    .as_ref()
11643                    .and_then(|h| h.get(li).copied())
11644                    .unwrap_or(self.num_heads);
11645                if nkv == 0 || nh % nkv != 0 {
11646                    return Err(format!(
11647                        "layer {li}: {nkv} KV heads must be nonzero and divide {nh} Q heads"
11648                    ));
11649                }
11650            }
11651        }
11652        self.kv_heads_per_layer = kv_heads_per_layer;
11653        self.v_head_dim = v_head_dim.filter(|&vd| vd != self.head_dim);
11654        if self.kv_heads_per_layer.is_some() {
11655            for li in 0..self.kv_cache.layers.len() {
11656                let full = matches!(
11657                    self.weights
11658                        .layers
11659                        .get(self.phys_layer(li))
11660                        .map(|lw| &lw.attn),
11661                    Some(AttnKind::Full { .. })
11662                );
11663                let nkv = self.layer_num_kv_heads(li);
11664                let cache = &self.kv_cache.layers[li];
11665                if full && (cache.num_kv_heads != nkv || cache.head_dim != self.head_dim) {
11666                    let sinks = cache.sinks.clone();
11667                    self.kv_cache.layers[li] =
11668                        crate::kv_cache::LayerKvCache::new(nkv, self.head_dim);
11669                    self.kv_cache.layers[li].sinks = sinks;
11670                }
11671            }
11672        }
11673        Ok(())
11674    }
11675
11676    /// Attach learned attention-sink logits (one per Q head) to PHYSICAL
11677    /// layer `phys` — every virtual layer that runs it. The loader calls
11678    /// this for each `model.layers.N.self_attn.sinks` tensor.
11679    pub fn set_layer_sinks(&mut self, phys: usize, sinks: Vec<f32>) -> Result<(), String> {
11680        let Some(lw) = self.weights.layers.get(phys) else {
11681            return Err(format!("sinks for layer {phys}: no such layer"));
11682        };
11683        if !matches!(lw.attn, AttnKind::Full { .. }) {
11684            return Err(format!(
11685                "sinks for layer {phys}: only softmax (Full) attention layers take sinks"
11686            ));
11687        }
11688        let nh = self
11689            .attention_heads_per_layer
11690            .as_ref()
11691            .and_then(|h| h.get(phys).copied())
11692            .unwrap_or(self.num_heads);
11693        if sinks.len() != nh {
11694            return Err(format!(
11695                "sinks for layer {phys}: {} values, expected one per Q head ({nh})",
11696                sinks.len()
11697            ));
11698        }
11699        if let Some(bad) = sinks.iter().find(|v| !v.is_finite()) {
11700            return Err(format!("sinks for layer {phys}: non-finite value {bad}"));
11701        }
11702        for li in 0..self.kv_cache.layers.len() {
11703            if self.phys_layer(li) == phys {
11704                self.kv_cache.layers[li].sinks = Some(sinks.clone());
11705            }
11706        }
11707        Ok(())
11708    }
11709
11710    /// Why the GPU attention graphs cannot serve this model, if they
11711    /// cannot: the wgpu whole-token and batched graphs, the greedy
11712    /// multi-burst, the q1 attention dropin and the Metal block/chunk/rows
11713    /// graphs all assume ONE (num_kv_heads, head_dim) geometry, V heads as
11714    /// wide as K, a single RoPE table, full-context attention and a plain
11715    /// softmax. A model outside that contract runs on the CPU layer walk
11716    /// (and the per-op GPU matvecs) until a graph learns it — never on a
11717    /// graph that would read it wrong. None = no attention-level reason
11718    /// (the graph builders still check weights and layer kinds).
11719    pub fn graph_attn_decline_reason(&self) -> Option<&'static str> {
11720        if self.kv_heads_per_layer.is_some() {
11721            return Some("per-layer KV head counts");
11722        }
11723        if self.v_head_dim.is_some_and(|vd| vd != self.head_dim) {
11724            return Some("V heads narrower than Q/K heads");
11725        }
11726        if self.kv_cache.layers.iter().any(|l| l.sinks.is_some()) {
11727            return Some("learned attention sinks");
11728        }
11729        if self.swa.is_some() || self.sliding_layers.is_some() {
11730            return Some("sliding-window layers");
11731        }
11732        None
11733    }
11734
11735    /// Can the Metal kernels apply this model's RoPE amplitude? Every
11736    /// rotation kernel (`attn_rope_qkn[_b]`, `chunk_rope_kv`) multiplies the
11737    /// rotated pair by the layer's `rope_scale` (YaRN `attention_factor`,
11738    /// Mellum2.1's global layers) exactly where the CPU's
11739    /// `rope_rotate_scaled` does. With the Q/K norm AFTER RoPE the kernels'
11740    /// shared sum of squares would be the unscaled one, so that pairing
11741    /// (no known model) declines.
11742    #[cfg(target_os = "macos")]
11743    fn metal_rope_scale_ok(&self) -> bool {
11744        !(self.qk_norm_after_rope
11745            && (0..self.num_layers).any(|li| self.layer_rope_scale(li) != 1.0))
11746    }
11747
11748    /// Can the Metal block graph carry this model's sliding-window layers?
11749    /// Both of its attention forms take a layer's own window, RoPE table
11750    /// and rotary width: the device attend (`AttnDeviceParams::window`, the
11751    /// layer's `inv_freq`/`rd`) and the sandwich's host attend (exactly the
11752    /// CPU path's attention). True when the sliding window is the model's
11753    /// only attention-level decline (Spark-X2.5). Learned, value-less
11754    /// sinks may accompany that geometry: q1's ordinary device GQA
11755    /// includes the sink in its softmax denominator and disables split-K.
11756    /// Per-layer KV heads, narrow V and Gemma-4 global geometry still
11757    /// decline; a YaRN amplitude per layer rides along
11758    /// (`metal_rope_scale_ok`).
11759    ///
11760    /// Per-layer Q heads (Laguna) and capped scores (Gemma-2) decline here
11761    /// too, with V norm (Gemma-4): the chunk prefill's own gate checks
11762    /// neither of the first two, and before this door opened its `swa`
11763    /// refusal kept every sliding model with them off the chunk graph.
11764    #[cfg(target_os = "macos")]
11765    fn metal_graph_swa(&self) -> bool {
11766        (self.swa.is_some() || self.sliding_layers.is_some())
11767            && self.attention_heads_per_layer.is_none()
11768            && !self.attn_v_norm
11769            && self.attn_softcap == 0.0
11770            && self.kv_heads_per_layer.is_none()
11771            && self.v_head_dim.map_or(true, |vd| vd == self.head_dim)
11772            && self.global_attn.is_none()
11773            && self.inv_freq_global.is_none()
11774            && self.metal_rope_scale_ok()
11775            && !(0..self.num_layers).any(|li| {
11776                self.layer_is_local(li)
11777                    && self.inv_freq_local.is_none()
11778                    && self.rotary_dim_local.is_some_and(|r| r != self.rotary_dim)
11779            })
11780    }
11781
11782    /// Can the Metal graphs carry this Gemma-4 model (`gpu_metal::gemma4`)?
11783    /// Its sliding layers (window, local RoPE table, 256-wide heads)
11784    /// device-attend like Spark-X2.5's; the 512-wide global layers (one KV
11785    /// head, proportional RoPE in the zero-padded table) take the 16-slot
11786    /// kernels; the V norm, sandwich norms, layer scalar and tanh-GELU come
11787    /// from the Gemma-4 library. Dense FFNs only (no Gemma-4 MoE measured).
11788    /// `CMF_METAL_GEMMA4=0` declines it as before (CPU attention).
11789    #[cfg(target_os = "macos")]
11790    fn metal_gemma4(&self) -> bool {
11791        crate::gpu_metal::gemma4::enabled()
11792            && (self.global_attn.is_some() || self.attn_v_norm)
11793            && self.attention_heads_per_layer.is_none()
11794            && self.kv_heads_per_layer.is_none()
11795            && self.v_head_dim.is_none()
11796            && !self.kv_cache.layers.iter().any(|l| l.sinks.is_some())
11797            && self.attn_softcap == 0.0
11798            && !self.qk_norm_after_rope
11799            && self.metal_rope_scale_ok()
11800            && self
11801                .weights
11802                .layers
11803                .iter()
11804                .all(|lw| matches!(&lw.ffn, FfnKind::Dense(d) if d.segs.is_empty()))
11805            && !(0..self.num_layers).any(|li| {
11806                self.layer_is_local(li)
11807                    && self.inv_freq_local.is_none()
11808                    && self.rotary_dim_local.is_some_and(|r| r != self.rotary_dim)
11809            })
11810    }
11811
11812    /// Why the WGPU graphs (whole-token, batched prefill, greedy burst)
11813    /// cannot run this model's attention, if they cannot. Per-layer KV
11814    /// heads, V narrower than K, learned sinks and sliding windows ride
11815    /// their per-layer geometry (`GraphAttnGeom`, the ATTEND_X kernels);
11816    /// what that geometry does not express keeps the decline, by name.
11817    /// None for every model with one attention geometry.
11818    pub fn wgpu_graph_attn_decline(&self) -> Option<&'static str> {
11819        // Keep the ordinary (uniform) fast arms off YaRN until they carry a
11820        // scale field too. A non-uniform geometry gets a `GraphAttnGeom` and
11821        // is handled below by the generic graph kernels.
11822        let geometry_reason = self.graph_attn_decline_reason();
11823        let has_scaled_rope = (0..self.num_layers).any(|li| self.layer_rope_scale(li) != 1.0);
11824        if has_scaled_rope && geometry_reason.is_none() {
11825            return Some("scaled RoPE without per-layer geometry");
11826        }
11827        // For Q/K norm after partial RoPE, the scale changes only the rotary
11828        // slice's RMS denominator. The generic graph deliberately declines
11829        // that uncommon combination until it carries a separate rotary sum;
11830        // Mellum has q/k norm before RoPE, so it remains on this path.
11831        if has_scaled_rope && self.qk_norm_after_rope {
11832            return Some("scaled RoPE with post-RoPE Q/K norm");
11833        }
11834        geometry_reason?;
11835        // Gemma-4: global layers 512 wide over 256-wide sliding ones
11836        // (`GraphAttnGeom::hd`), the scale-less V norm (`v_norm`), and the
11837        // sandwich norms + layer scalar (`GraphLayer`) ride the TOKEN graph.
11838        // The batch graph has none of the three and refuses them itself
11839        // (`batch_graph_gemma_decline`).
11840        if self.attention_heads_per_layer.is_some() {
11841            return Some("per-layer Q head counts with per-layer geometry");
11842        }
11843        // Sandwich norms are measured on Gemma-4 only (the humanizer port,
11844        // against HF transformers); Gemma-2/3 keep the per-op path until
11845        // they are measured the same way.
11846        let gemma4 = self.global_attn.is_some() || self.attn_v_norm;
11847        if !gemma4
11848            && self.weights.layers.iter().any(|lw| {
11849                lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some()
11850            })
11851        {
11852            return Some("sandwich norms / layer scale with per-layer geometry");
11853        }
11854        // `GraphAttnGeom::rope_scale` carries YaRN's post-rotation
11855        // attention-amplitude factor to the WGPU RoPE kernel. It is not an
11856        // angular position scale, so it must not be folded into `invf`.
11857        if self.weights.layers.iter().any(|lw| {
11858            matches!(
11859                &lw.attn,
11860                AttnKind::Full {
11861                    output_gate: true,
11862                    ..
11863                }
11864            )
11865        }) && self.v_head_dim.is_some()
11866        {
11867            return Some("gated attention with V narrower than K");
11868        }
11869        if (0..self.num_layers).any(|li| {
11870            self.layer_is_local(li)
11871                && self.inv_freq_local.is_none()
11872                && self.rotary_dim_local.is_some_and(|r| r != self.rotary_dim)
11873        }) {
11874            return Some("local rotary width without a local RoPE table");
11875        }
11876        None
11877    }
11878
11879    /// What the wgpu BATCH graph (and the attention-only singleton graphs)
11880    /// cannot run of Gemma-4, which the token graph does run: per-layer
11881    /// head widths, the V norm, sandwich norms and the layer scalar.
11882    fn batch_graph_gemma_decline(&self) -> Option<&'static str> {
11883        // Gemma-4 rides `gemma_batch` (per-layer head width, V norm,
11884        // sandwich norms, layer scalar) unless CMF_G4_BATCH=0.
11885        if (self.global_attn.is_some() || self.attn_v_norm) && crate::gpu::g4_batch_on() {
11886            return None;
11887        }
11888        if self.global_attn.is_some() {
11889            return Some("per-layer head width (Gemma-4 global layers)");
11890        }
11891        if self.attn_v_norm {
11892            return Some("V norm");
11893        }
11894        if self.weights.layers.iter().any(|lw| {
11895            lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some()
11896        }) {
11897            return Some("sandwich norms / layer scale");
11898        }
11899        None
11900    }
11901
11902    /// The wgpu graphs' attention geometry for layer `li` (virtual index):
11903    /// Some only for a model whose layers do not share one (MiMo-V2) — KV
11904    /// heads, V width, rotary width and RoPE table, window and sinks of
11905    /// THIS layer, exactly what the CPU attention reads for it.
11906    fn graph_attn_geom(&self, li: usize) -> Option<crate::gpu::GraphAttnGeom<'_>> {
11907        self.graph_attn_decline_reason()?;
11908        let (nkv, hd, rd) = self.layer_geom(li);
11909        let invf: &[f32] = if self.layer_is_local(li) {
11910            match &self.inv_freq_local {
11911                Some(f) => f.as_slice(),
11912                None => self.inv_freq.as_slice(),
11913            }
11914        } else {
11915            match &self.inv_freq_global {
11916                Some(f) => f.as_slice(),
11917                None => self.inv_freq.as_slice(),
11918            }
11919        };
11920        Some(crate::gpu::GraphAttnGeom {
11921            nkv,
11922            hd,
11923            dv: self.layer_v_dim(li),
11924            rd,
11925            invf,
11926            rope_scale: self.layer_rope_scale(li),
11927            window: self.layer_window(li),
11928            sink: self.kv_cache.layers[li].sinks.as_deref(),
11929            v_norm: self.attn_v_norm,
11930        })
11931    }
11932
11933    /// The decode graph's device K/V mirror that layer `li`'s batched
11934    /// prefill appends its chunk to and attends against (wgpu), instead
11935    /// of uploading the layer's whole K/V prefix every chunk and the whole
11936    /// cache again at the first decode token. Only where the token graph
11937    /// will read that very mirror: the decode graph on, not refused, and
11938    /// this layer's geometry what it requests (`graph_attn_geom`: KV heads,
11939    /// V as wide as K, the window as a ring, no sink).
11940    ///
11941    /// Spark-X2.5 only (`proj_gate_sigmoid`), the one architecture whose
11942    /// output was measured identical on it. `CMF_PREFILL_MIRROR=0` keeps
11943    /// the host upload (A/B).
11944    #[cfg(feature = "gpu")]
11945    fn prefill_mirror_target(&self, li: usize) -> Option<crate::gpu::PrefillMirror> {
11946        if !self.proj_gate_sigmoid
11947            || std::env::var("CMF_PREFILL_MIRROR").as_deref() == Ok("0")
11948            || !crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode)
11949            || self.graph_refused()
11950            || self.o1_active()
11951            || self.attn_softcap > 0.0
11952            || self.wgpu_graph_attn_decline().is_some()
11953        {
11954            return None;
11955        }
11956        let g = self.graph_attn_geom(li)?;
11957        let (nkv, hd, _) = self.layer_geom(li);
11958        // The token graph passes the call-wide head width (layer 0's).
11959        if g.nkv != nkv || g.dv != hd || g.sink.is_some() || hd != self.layer_geom(0).1 {
11960            return None;
11961        }
11962        Some(crate::gpu::PrefillMirror {
11963            kv_id: self.graph_kv_id,
11964            layer: li,
11965            limit: self.kv_cache.max_seq_len,
11966        })
11967    }
11968
11969    /// Bring the host KV cache of every Full-attention layer in
11970    /// `[from, upto)` up to `position` rows from the wgpu mirrors, where a
11971    /// device graph advanced a layer that the host is about to run: a
11972    /// device prefix that shrank since the prompt (or a batched prefill
11973    /// prefix longer than the decode one). A layer whose mirror does not
11974    /// hold the missing rows is left alone. Rows a sliding layer's ring
11975    /// no longer holds come back as zeros — outside every window that
11976    /// will read them.
11977    #[cfg(feature = "gpu")]
11978    fn pull_lagging_host_kv(&mut self, from: usize, upto: usize, position: usize) {
11979        let kv_id = self.graph_kv_id;
11980        for li in from..upto.min(self.num_layers) {
11981            if !matches!(
11982                self.weights.layers[self.phys_layer(li)].attn,
11983                AttnKind::Full { .. }
11984            ) {
11985                continue;
11986            }
11987            // Absolute: a trimmed sliding tail holds positions base.. only.
11988            let host = self.kv_cache.layers[li].pos_len();
11989            if host >= position {
11990                continue;
11991            }
11992            let Some(dev) = crate::gpu::graph_kv_stored(kv_id, li) else {
11993                continue;
11994            };
11995            let to = dev.min(position);
11996            if to <= host {
11997                continue;
11998            }
11999            let (nkv, hd) = {
12000                let c = &self.kv_cache.layers[li];
12001                (c.num_kv_heads, c.head_dim)
12002            };
12003            let Some((k, v, first_valid)) =
12004                crate::gpu::graph_kv_pull_host(kv_id, li, host, to, nkv, hd)
12005            else {
12006                continue;
12007            };
12008            // A sliding layer only ever reads its last `window` rows; a
12009            // full-context layer needs every row it did not have.
12010            let need_from = match self.layer_window(li) {
12011                Some(w) => host.max((position + 1).saturating_sub(w)),
12012                None => host,
12013            };
12014            if first_valid > need_from {
12015                tracing::warn!(
12016                    "layer {li}: device KV rows {host}..{to} no longer resident \
12017                     (from {first_valid}); host attention will miss them"
12018                );
12019            }
12020            let row = nkv * hd;
12021            let cache = &mut self.kv_cache.layers[li];
12022            for p in 0..to - host {
12023                cache.append(&k[p * row..(p + 1) * row], &v[p * row..(p + 1) * row], &[]);
12024            }
12025        }
12026    }
12027
12028    /// Log (once per graph site and pipeline) that `site` declined for
12029    /// `reason`. The lines are kept so a caller or a test can read them.
12030    fn note_graph_decline(&self, site: &'static str, reason: &'static str) {
12031        let mut seen = self.graph_declines.borrow_mut();
12032        if !seen.iter().any(|&(s, r)| s == site && r == reason) {
12033            tracing::warn!("{site} declined: {reason} (CPU attention path)");
12034            seen.push((site, reason));
12035        }
12036    }
12037
12038    /// The GPU-graph declines this pipeline has logged so far, as the
12039    /// logged lines.
12040    pub fn graph_declines(&self) -> Vec<String> {
12041        self.graph_declines
12042            .borrow()
12043            .iter()
12044            .map(|(site, reason)| format!("{site} declined: {reason} (CPU attention path)"))
12045            .collect()
12046    }
12047
12048    /// Does layer `li` have the plain attention geometry the historical
12049    /// head-masked f32 path (`multi_head_attention`) assumes — pipeline-wide
12050    /// KV heads / head_dim / RoPE table, full context, no sink, V as wide
12051    /// as K? Anything else runs the dense `qwen_attention` instead.
12052    /// `CMF_LAYER_DUMP` writer (see `Pipeline::layer_dump`): one position's
12053    /// hidden after layer `li` as raw little-endian f32 into
12054    /// `<dir>/p{pos:06}_l{li:02}.f32`. A failed write is reported once and
12055    /// never stops the forward — the dump is a diagnostic.
12056    fn dump_layer_row(&self, pos: usize, li: usize, row: &[f32]) {
12057        let Some(dir) = &self.layer_dump else {
12058            return;
12059        };
12060        let mut bytes = Vec::with_capacity(row.len() * 4);
12061        for v in row {
12062            bytes.extend_from_slice(&v.to_le_bytes());
12063        }
12064        let path = dir.join(format!("p{pos:06}_l{li:02}.f32"));
12065        if let Err(e) = std::fs::create_dir_all(dir).and_then(|_| std::fs::write(&path, &bytes)) {
12066            use std::sync::atomic::{AtomicBool, Ordering};
12067            static SAID: AtomicBool = AtomicBool::new(false);
12068            if !SAID.swap(true, Ordering::Relaxed) {
12069                tracing::error!("CMF_LAYER_DUMP: cannot write {}: {e}", path.display());
12070            }
12071        }
12072    }
12073
12074    /// Decide the MiMo-V2 expert placement once (`crate::mimo_moe`). Any
12075    /// other model turns the slot off on the first call.
12076    fn mimo_moe_prepare(&mut self) {
12077        if !self.mimo_moe.is_undecided() {
12078            return;
12079        }
12080        let slot = {
12081            let layers: Vec<(usize, &MoeFfn)> = (0..self.num_layers)
12082                .filter_map(
12083                    |li| match &self.weights.layers.get(self.phys_layer(li))?.ffn {
12084                        FfnKind::Moe(m) => Some((li, m)),
12085                        _ => None,
12086                    },
12087                )
12088                .collect();
12089            // One bank lives on one device: an in-process multi-GPU split
12090            // keeps the whole-layer path.
12091            if layers.is_empty()
12092                || self.physical_layers != self.num_layers
12093                || self.gpu_plan.is_some()
12094            {
12095                crate::mimo_moe::Slot::Off
12096            } else {
12097                // Whether a whole-token graph could run this model's layers
12098                // (then a whole-layer prefix is one submit, not per-layer
12099                // fences).
12100                let graph_prefix =
12101                    self.wgpu_graph_attn_decline().is_none() && crate::gpu::wgpu_graph_default();
12102                crate::mimo_moe::Slot::decide(&layers, self.num_layers, graph_prefix)
12103            }
12104        };
12105        self.mimo_moe = slot;
12106    }
12107
12108    #[cfg(test)]
12109    pub(crate) fn test_graph_kv_id(&self) -> u64 {
12110        self.graph_kv_id
12111    }
12112
12113    /// Dynamic MiMo layer: one device attention graph, followed by a bank
12114    /// frame. Both decode and short verification use this same attention
12115    /// path and absolute layer key; the host KV may intentionally lag.
12116    pub(crate) fn mimo_graph_layer_rows(
12117        &mut self,
12118        li: usize,
12119        h: &mut [f32],
12120        positions: &[usize],
12121    ) -> crate::gpu::BatchGraphOutcome {
12122        use crate::gpu::BatchGraphOutcome as Out;
12123        let b = positions.len();
12124        if !(1..=4).contains(&b)
12125            || h.len() != b * self.hidden_size
12126            || !self.mimo_moe.is_dynamic(li, true)
12127            || !crate::gpu::enabled_here()
12128            || !crate::gpu::wgpu_active()
12129            || self.o1_active()
12130            || self.physical_layers != self.num_layers
12131            // The pair-fusion diagnostic (and an explicit graph-off run)
12132            // rewinds only host KV. A hidden singleton attention graph here
12133            // would leave device mirrors ahead of the next host position.
12134            || std::env::var("CMF_GPU_WGPU_GRAPH").as_deref() == Ok("0")
12135            || std::env::var("CMF_MIMO_ATTN_GRAPH").as_deref() == Ok("0")
12136            || self.wgpu_graph_attn_decline().is_some()
12137        {
12138            return Out::Declined;
12139        }
12140        let attn_started = std::time::Instant::now();
12141        let outcome = {
12142            let lw = &self.weights.layers[li];
12143            if lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some() {
12144                return Out::Declined;
12145            }
12146            let FfnKind::Moe(m) = &lw.ffn else {
12147                return Out::Declined;
12148            };
12149            let AttnKind::Full {
12150                wq,
12151                wk,
12152                wv,
12153                wo,
12154                q_norm,
12155                k_norm,
12156                output_gate,
12157                softplus_gate,
12158                bias,
12159            } = &lw.attn
12160            else {
12161                return Out::Declined;
12162            };
12163            if *output_gate || softplus_gate.is_some() {
12164                return Out::Declined;
12165            }
12166            let Some(model) = wq.graph_weight().map(|(m, ..)| m.clone()).or_else(|| {
12167                m.experts
12168                    .first()?
12169                    .gate_proj
12170                    .mapped_q4tp()
12171                    .map(|(m, _)| m.clone())
12172            }) else {
12173                return Out::Declined;
12174            };
12175            fn gw<'a>(
12176                t: &'a QTensor,
12177                owner: &std::sync::Arc<cortiq_core::CmfModel>,
12178            ) -> Option<crate::gpu::GraphW<'a>> {
12179                if let Some((m, idx, kind, rs)) = t.graph_weight() {
12180                    if m.uid() != owner.uid() || t.has_prism_contract() {
12181                        return None;
12182                    }
12183                    return Some(crate::gpu::GraphW {
12184                        idx,
12185                        kind,
12186                        row_scale: rs,
12187                        data: &[],
12188                        prism: crate::gpu::GraphPrismOp::None,
12189                        affine: false,
12190                    });
12191                }
12192                t.as_f32().map(|data| crate::gpu::GraphW {
12193                    idx: 0,
12194                    kind: 4,
12195                    row_scale: &[],
12196                    data,
12197                    prism: crate::gpu::GraphPrismOp::None,
12198                    affine: false,
12199                })
12200            }
12201            let (Some(q), Some(k), Some(v), Some(o)) = (
12202                gw(wq, &model),
12203                gw(wk, &model),
12204                gw(wv, &model),
12205                gw(wo, &model),
12206            ) else {
12207                return Out::Declined;
12208            };
12209            let layer = crate::gpu::GraphLayer {
12210                input_norm: &lw.input_norm,
12211                post_norm: &lw.post_norm,
12212                attn_out_norm: None,
12213                ffn_out_norm: None,
12214                layer_scale: None,
12215                ffn: crate::gpu::GraphFfn::AttentionOnly,
12216                attn: crate::gpu::GraphAttn::Full {
12217                    wq: q,
12218                    wk: k,
12219                    wv: v,
12220                    wo: o,
12221                    q_norm: q_norm.as_deref(),
12222                    k_norm: k_norm.as_deref(),
12223                    late_qk_norm: self.qk_norm_after_rope,
12224                    bias: bias
12225                        .as_ref()
12226                        .map(|(q, k, v)| (q.as_slice(), k.as_slice(), v.as_slice())),
12227                    output_gate: false,
12228                    cpu_k: self.kv_cache.layers[li].k_heads(),
12229                    cpu_v: self.kv_cache.layers[li].v_heads(),
12230                    cpu_base: self.kv_cache.layers[li].base(),
12231                    geom: self.graph_attn_geom(li),
12232                    head_gate: None,
12233                },
12234            };
12235            let (nkv, hd, rd) = self.layer_geom(li);
12236            crate::gpu::forward_batch_graph_at(
12237                &model,
12238                self.graph_kv_id,
12239                li,
12240                &[layer],
12241                &self.inv_freq,
12242                h,
12243                self.layer_num_heads(li),
12244                nkv,
12245                hd,
12246                rd,
12247                self.hidden_size,
12248                1,
12249                positions,
12250                self.kv_cache.max_seq_len,
12251                self.norm_style == cortiq_core::NormStyle::Gemma,
12252                self.rms_eps as f32,
12253                self.attn_scale,
12254                b,
12255                &[],
12256                self.o1_epoch,
12257                None,
12258                None,
12259            )
12260        };
12261        match outcome {
12262            Out::Completed => {}
12263            Out::Declined => return Out::Declined,
12264            Out::Failed => {
12265                self.graph_failed
12266                    .store(true, std::sync::atomic::Ordering::Relaxed);
12267                return Out::Failed;
12268            }
12269        }
12270        let attn_ns = attn_started.elapsed().as_nanos() as u64;
12271        let hs = self.hidden_size;
12272        let lw = &self.weights.layers[li];
12273        let FfnKind::Moe(m) = &lw.ffn else {
12274            unreachable!()
12275        };
12276        let mut post = vec![0.0; h.len()];
12277        for (x, y) in h.chunks_exact(hs).zip(post.chunks_exact_mut(hs)) {
12278            inference::rms_norm_into(x, &lw.post_norm, self.rms_eps, self.norm_style, y);
12279        }
12280        let mut ffn = if b == 1 {
12281            moe_ffn_banked(&mut self.mimo_moe, li, m, &post, self.pool.as_deref())
12282        } else {
12283            moe_ffn_banked_rows(
12284                &mut self.mimo_moe,
12285                li,
12286                m,
12287                &post,
12288                b,
12289                hs,
12290                self.pool.as_deref(),
12291            )
12292        };
12293        for (x, &f) in h.iter_mut().zip(&ffn) {
12294            *x += f;
12295        }
12296        attention::recycle_buf(&mut ffn);
12297        if self.layer_dump.is_some() {
12298            for (&pos, row) in positions.iter().zip(h.chunks_exact(hs)) {
12299                self.dump_layer_row(pos, li, row);
12300            }
12301        }
12302        crate::mimo_moe::note_attention_graph(b, attn_ns);
12303        Out::Completed
12304    }
12305
12306    fn layer_attn_plain(&self, li: usize) -> bool {
12307        self.kv_heads_per_layer.is_none()
12308            && self.v_head_dim.is_none()
12309            && self.global_attn.is_none()
12310            && self.layer_window(li).is_none()
12311            && self.kv_cache.layers[li].sinks.is_none()
12312    }
12313
12314    /// Forward one position through all layers (hybrid dispatch).
12315    fn forward_layers(
12316        &mut self,
12317        hidden: &[f32],
12318        position: usize,
12319        task_mask: Option<&TaskMask>,
12320    ) -> Vec<f32> {
12321        let out = self.forward_layers_upto(hidden, position, task_mask, None);
12322        self.o1_progress();
12323        self.swa_trim_tails();
12324        out
12325    }
12326
12327    // ── Network pipeline-split building blocks (coordinator/worker) ──
12328    // A remote worker owns layers [from ..= upto] and their KV; the
12329    // coordinator owns the rest plus embed / final norm / head. Attention
12330    // causality is per-layer, so a whole prompt's boundary hiddens ship
12331    // as one batch and decode ships one vector per token.
12332
12333    /// Embed one token id (embed multiplier applied).
12334    pub fn embed_id(&self, id: u32) -> Vec<f32> {
12335        self.embed_single(id)
12336    }
12337
12338    /// Refuse the archs/modes whose forward cannot be cut at a layer
12339    /// boundary. Loud by design: a split that silently changed the math
12340    /// would be a chimera.
12341    pub fn split_supported(&self) -> Result<(), String> {
12342        if self.dsv4.is_some() {
12343            return Err(
12344                "network split: DeepSeek-V4 runs its own fused stack (not splittable yet)".into(),
12345            );
12346        }
12347        if self.dsv41.is_some() {
12348            return Err(
12349                "network split: DeepSeek-V4.1 owns the shared CED/CSA2 state (not splittable)"
12350                    .into(),
12351            );
12352        }
12353        if self.qwen4_exp.is_some() {
12354            return Err(
12355                "network split: Qwen3.8-Flash-Next hyper/QSA stack is not splittable yet".into(),
12356            );
12357        }
12358        if self.per_token_stack() {
12359            return Err(
12360                "network split: the per-token stack (gemma-3n AltUp / gemma-4 E-series) is not splittable yet".into(),
12361            );
12362        }
12363        Ok(())
12364    }
12365
12366    /// Forward `hidden` through layers [from ..= upto] at `position`,
12367    /// appending those layers' KV/state. Both split sides call this
12368    /// over their own range; a task mask applies to the span's own
12369    /// layers (each side masks what it runs).
12370    pub fn forward_span(
12371        &mut self,
12372        hidden: &[f32],
12373        position: usize,
12374        from: usize,
12375        upto: usize,
12376        task_mask: Option<&TaskMask>,
12377    ) -> Result<Vec<f32>, String> {
12378        self.split_supported()?;
12379        if from > upto || upto >= self.num_layers {
12380            return Err(format!(
12381                "forward_span: layer range {from}..={upto} outside 0..{}",
12382                self.num_layers
12383            ));
12384        }
12385        if hidden.len() != self.hidden_size {
12386            return Err(format!(
12387                "forward_span: hidden len {} ≠ hidden_size {}",
12388                hidden.len(),
12389                self.hidden_size
12390            ));
12391        }
12392        let out = self.forward_layers_span(hidden, position, task_mask, from, Some(upto));
12393        self.o1_progress();
12394        self.swa_trim_tails();
12395        if self
12396            .graph_failed
12397            .swap(false, std::sync::atomic::Ordering::Relaxed)
12398        {
12399            self.cancel
12400                .store(false, std::sync::atomic::Ordering::Relaxed);
12401            self.clear_sequence_state();
12402            return Err("forward_span: deferred O(1) transition failed".into());
12403        }
12404        Ok(out)
12405    }
12406
12407    /// Final norm + lm_head over a boundary hidden (the final-logit
12408    /// softcap is applied by lm_head_forward itself).
12409    pub fn logits_from_hidden(&mut self, hidden: &[f32]) -> Vec<f32> {
12410        let normed = inference::rms_norm(
12411            hidden,
12412            &self.weights.final_norm,
12413            self.rms_eps,
12414            self.norm_style,
12415        );
12416        self.lm_head_forward(&normed)
12417    }
12418
12419    /// Sample the next token with this pipeline's sampler state.
12420    pub fn sample_next(&mut self, logits: &[f32], past_tokens: &[u32]) -> u32 {
12421        sampler::sample_with_scratch(
12422            logits,
12423            &self.sampler_config,
12424            past_tokens,
12425            &mut self.rng,
12426            &mut self.sampler_scratch,
12427        )
12428    }
12429
12430    /// Fresh sequence: clear KV, reuse history and device mirrors.
12431    pub fn reset_session(&mut self) {
12432        self.clear_sequence_state();
12433    }
12434
12435    /// Batched span prefill from token ids (coordinator side): embed +
12436    /// layers [0 ..= upto]; returns the boundary hiddens of ALL positions
12437    /// (ids.len() × hidden). Rides the same layer-major machinery as the
12438    /// local prefill; falls back to the per-position walk under
12439    /// CMF_PREFILL=seq.
12440    pub fn prefill_span_ids(
12441        &mut self,
12442        ids: &[u32],
12443        start_pos: usize,
12444        upto: usize,
12445        task_mask: Option<&TaskMask>,
12446    ) -> Result<Vec<f32>, String> {
12447        self.split_supported()?;
12448        if upto >= self.num_layers {
12449            return Err(format!(
12450                "prefill_span_ids: upto {upto} outside 0..{}",
12451                self.num_layers
12452            ));
12453        }
12454        // Same predicate as the whole-stack prefill: a span whose GDN
12455        // state lives on the device must walk positions through the
12456        // graph, not through the batched CPU span.
12457        if self.can_prefill_batched() && !self.graph_prefill_preferred() {
12458            let out =
12459                self.prefill_batch_span(PrefillIn::Ids(ids), start_pos, task_mask, 0, upto + 1);
12460            self.check_o1_progress_failure("prefill_span_ids")?;
12461            Ok(out)
12462        } else {
12463            let hs = self.hidden_size;
12464            let mut out = Vec::with_capacity(ids.len() * hs);
12465            for (i, &id) in ids.iter().enumerate() {
12466                let emb = self.embed_id(id);
12467                out.extend_from_slice(&self.forward_span(
12468                    &emb,
12469                    start_pos + i,
12470                    0,
12471                    upto,
12472                    task_mask,
12473                )?);
12474            }
12475            Ok(out)
12476        }
12477    }
12478
12479    /// Batched span prefill from boundary hiddens (worker side): layers
12480    /// [from ..= upto] for every position in the batch; returns the batch.
12481    pub fn prefill_span_hidden(
12482        &mut self,
12483        hidden: &[f32],
12484        start_pos: usize,
12485        from: usize,
12486        upto: usize,
12487        task_mask: Option<&TaskMask>,
12488    ) -> Result<Vec<f32>, String> {
12489        self.split_supported()?;
12490        let hs = self.hidden_size;
12491        if hidden.is_empty() || hidden.len() % hs != 0 {
12492            return Err(format!(
12493                "prefill_span_hidden: {} floats is not a multiple of hidden {hs}",
12494                hidden.len()
12495            ));
12496        }
12497        if from > upto || upto >= self.num_layers {
12498            return Err(format!(
12499                "prefill_span_hidden: layer range {from}..={upto} outside 0..{}",
12500                self.num_layers
12501            ));
12502        }
12503        if self.can_prefill_batched() && !self.graph_prefill_preferred() {
12504            let out = self.prefill_batch_span(
12505                PrefillIn::Hidden(hidden),
12506                start_pos,
12507                task_mask,
12508                from,
12509                upto + 1,
12510            );
12511            self.check_o1_progress_failure("prefill_span_hidden")?;
12512            Ok(out)
12513        } else {
12514            let b = hidden.len() / hs;
12515            let mut out = Vec::with_capacity(hidden.len());
12516            for i in 0..b {
12517                let h = self.forward_span(
12518                    &hidden[i * hs..(i + 1) * hs],
12519                    start_pos + i,
12520                    from,
12521                    upto,
12522                    task_mask,
12523                )?;
12524                out.extend_from_slice(&h);
12525            }
12526            Ok(out)
12527        }
12528    }
12529
12530    /// Build the whole-token wgpu graph for a pure-attention q1 model (every
12531    /// layer Full q1 + dense q1 FFN, no gate/bias). Returns the post-stack
12532    /// hidden (caller does final norm + lm_head), or None to fall back.
12533    fn try_token_graph_wgpu(
12534        &self,
12535        hidden: &[f32],
12536        position: usize,
12537        logits_out: &mut Vec<f32>,
12538        layers_run: &mut usize,
12539    ) -> Option<Result<Vec<f32>, ()>> {
12540        self.try_token_graph_wgpu_steps(
12541            hidden,
12542            position,
12543            logits_out,
12544            1,
12545            None,
12546            Some(layers_run),
12547            0,
12548            self.num_layers,
12549        )
12550    }
12551
12552    /// The span twin (network split): the graph covers [from..upto_excl)
12553    /// — one submit per SEGMENT per token. lm_head folds in only when
12554    /// the span reaches the last layer.
12555    fn try_token_graph_wgpu_span(
12556        &self,
12557        hidden: &[f32],
12558        position: usize,
12559        logits_out: &mut Vec<f32>,
12560        from: usize,
12561        upto_excl: usize,
12562        layers_run: &mut usize,
12563    ) -> Option<Result<Vec<f32>, ()>> {
12564        self.try_token_graph_wgpu_steps(
12565            hidden,
12566            position,
12567            logits_out,
12568            1,
12569            None,
12570            Some(layers_run),
12571            from,
12572            upto_excl,
12573        )
12574    }
12575
12576    /// The pooled tail's device lm_head: final-norm + head as one
12577    /// layer-less graph pass, so decode stops paying the double-digit
12578    /// millisecond host matvec every token. `None` keeps the host head.
12579    #[cfg(feature = "gpu")]
12580    fn m3_device_logits(&self, hidden: &[f32]) -> Option<Vec<f32>> {
12581        if !crate::minicpm_tail::minimax_pool_on()
12582            || !crate::gpu::enabled_here()
12583            || self.head_clusters.is_some()
12584            || self.graph_failed.load(std::sync::atomic::Ordering::Relaxed)
12585            || std::env::var("CMF_GPU_LMHEAD").map(|v| v != "0") == Ok(false)
12586        {
12587            return None;
12588        }
12589        let (model, i, kind, rs) = self
12590            .weights
12591            .lm_head
12592            .graph_weight()
12593            .or_else(|| self.weights.lm_head.graph_weight_descriptor())?;
12594        let name = &model.tensors[i].name;
12595        let prism = if crate::prism::is_inverse_embedding(&model, name) {
12596            crate::gpu::GraphPrismOp::InverseEmbedding
12597        } else if crate::prism::is_forward_weight(&model, name) {
12598            crate::gpu::GraphPrismOp::Forward
12599        } else {
12600            crate::gpu::GraphPrismOp::None
12601        };
12602        let gw = crate::gpu::GraphW {
12603            idx: i,
12604            kind,
12605            row_scale: rs,
12606            data: &[],
12607            prism,
12608            affine: crate::prism::is_affine_target(&model, name),
12609        };
12610        let rows = self.weights.lm_head.rows();
12611        let mut h = hidden.to_vec();
12612        let mut lg = Vec::new();
12613        let mut layers_run = 0usize;
12614        let outcome = crate::gpu::forward_token_graph(
12615            &model,
12616            self.graph_kv_id,
12617            &[],
12618            &[],
12619            self.o1_epoch,
12620            &self.inv_freq,
12621            &mut h,
12622            self.num_heads,
12623            self.num_kv_heads,
12624            self.head_dim,
12625            self.attn_scale,
12626            self.head_dim,
12627            self.hidden_size,
12628            self.intermediate_size,
12629            0,
12630            self.kv_cache.max_seq_len,
12631            self.norm_style == cortiq_core::NormStyle::Gemma,
12632            self.rms_eps as f32,
12633            Some((&gw, rows)),
12634            &self.weights.final_norm,
12635            &mut lg,
12636            &[],
12637            1,
12638            None,
12639            None,
12640            Some(&mut layers_run),
12641            0,
12642            false,
12643        );
12644        if outcome != crate::gpu::TokenGraphOutcome::Completed || lg.is_empty() {
12645            return None;
12646        }
12647        // The same post-steps the host head applies — keep sampling
12648        // identical whichever arm produced the logits.
12649        lg.resize(self.vocab_size, 0.0);
12650        if let Some(m) = self.logit_multiplier {
12651            for l in lg.iter_mut() {
12652                *l *= m;
12653            }
12654        }
12655        if let Some(c) = self.final_softcap {
12656            softcap_in_place(&mut lg, c, self.pool.as_deref());
12657        }
12658        Some(lg)
12659    }
12660
12661    /// Record one token-layer's winners into the route-frequency profile and
12662    /// write it out every 16 tokens when `CMF_M3_PROFILE_SAVE=<path>` is set.
12663    fn m3_note_route(&mut self, li: usize, winners: &[u32]) {
12664        if winners.is_empty() {
12665            return;
12666        }
12667        if self.m3_n_exp == 0 {
12668            self.m3_n_exp = crate::minicpm_tail::minimax_n_experts(&self.weights.layers, self.phys_layer(0)).max(1);
12669            self.m3_counts = vec![0; self.num_layers * self.m3_n_exp];
12670        }
12671        for &e in winners {
12672            let e = e as usize;
12673            if e < self.m3_n_exp && li < self.num_layers {
12674                self.m3_counts[li * self.m3_n_exp + e] = self.m3_counts[li * self.m3_n_exp + e].saturating_add(1);
12675            }
12676        }
12677        if li + 1 == self.num_layers {
12678            self.m3_tokens += 1;
12679            if self.m3_tokens % 16 == 0 {
12680                if let Ok(path) = std::env::var("CMF_M3_PROFILE_SAVE") {
12681                    let bytes: Vec<u8> = self.m3_counts.iter().flat_map(|c| c.to_le_bytes()).collect();
12682                    let _ = std::fs::write(&path, bytes);
12683                }
12684            }
12685        }
12686    }
12687
12688    /// Admit the most frequent (layer, expert) pairs of a saved profile into
12689    /// the bank before the first token: `CMF_M3_PROFILE=<path>` names the
12690    /// file, `CMF_M3_PREWARM_N` (default 300) bounds how many experts go in.
12691    /// The bank's own eviction stays in charge; prewarm only chooses order.
12692    fn m3_prewarm(&mut self) {
12693        let Ok(path) = std::env::var("CMF_M3_PROFILE") else {
12694            return;
12695        };
12696        let Ok(bytes) = std::fs::read(&path) else {
12697            tracing::warn!("m3 prewarm: cannot read profile {path}");
12698            return;
12699        };
12700        let counts: Vec<u32> = bytes
12701            .chunks_exact(4)
12702            .map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]))
12703            .collect();
12704        let n_exp = crate::minicpm_tail::minimax_n_experts(&self.weights.layers, self.phys_layer(0)).max(1);
12705        if counts.len() != self.num_layers * n_exp {
12706            tracing::warn!("m3 prewarm: profile size {} does not match {}x{}", counts.len(), self.num_layers, n_exp);
12707            return;
12708        }
12709        let limit: usize = std::env::var("CMF_M3_PREWARM_N")
12710            .ok()
12711            .and_then(|v| v.parse().ok())
12712            .unwrap_or(300);
12713        let mut ranked: Vec<(u32, usize, usize)> = counts
12714            .iter()
12715            .enumerate()
12716            .filter(|(_, c)| **c > 0)
12717            .map(|(i, &c)| (c, i / n_exp, i % n_exp))
12718            .collect();
12719        ranked.sort_unstable_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(&b.1)).then(a.2.cmp(&b.2)));
12720        ranked.truncate(limit);
12721        let mut per_layer: Vec<Vec<usize>> = vec![Vec::new(); self.num_layers];
12722        for (_, li, e) in ranked {
12723            per_layer[li].push(e);
12724        }
12725        let mut admitted = 0usize;
12726        for (li, ids) in per_layer.iter().enumerate() {
12727            if ids.is_empty() {
12728                continue;
12729            }
12730            let lw = &self.weights.layers[self.phys_layer(li)];
12731            let FfnKind::Moe(m) = &lw.ffn else {
12732                continue;
12733            };
12734            // Arming installs the layer's layout; nothing predicted yet.
12735            let _ = crate::minicpm_tail::minimax_pool_arm(m, li, self.num_layers, &[]);
12736            if crate::minicpm_tail::minimax_pool_ensure(m, li, self.num_layers, ids) {
12737                admitted += ids.len();
12738            }
12739        }
12740        tracing::info!("m3 prewarm: admitted {admitted} experts from {path}");
12741    }
12742
12743    /// One MiniMax tail layer through the pooled token-graph arm: attention,
12744    /// the pre-FFN norm, device routing and the descriptor-indexed expert
12745    /// bank in ONE submit (one sync instead of host attention + a MoE
12746    /// frame), with cold experts finished on the host against the read-back
12747    /// FFN input and admitted for the next token.
12748    ///
12749    /// `Ok(true)` — the layer is done (hidden replaced). `Ok(false)` —
12750    /// decline before any device mutation; the ordinary host path owns this
12751    /// position. `Err(())` — the device failed after admission; the caller
12752    /// must stop the sequence exactly like a failed span run.
12753    #[cfg(feature = "gpu")]
12754    fn m3_pooled_layer(&mut self, li: usize, position: usize, h: &mut Vec<f32>) -> Result<bool, ()> {
12755        if self.o1_active() || self.attn_softcap > 0.0 {
12756            return Ok(false);
12757        }
12758        if !self.m3_prewarmed {
12759            self.m3_prewarmed = true;
12760            self.m3_prewarm();
12761        }
12762        let frame = {
12763            let lw = &self.weights.layers[self.phys_layer(li)];
12764            let FfnKind::Moe(m) = &lw.ffn else {
12765                return Ok(false);
12766            };
12767            if !crate::minicpm_tail::MiniCpmTail::is_requested_target(m) {
12768                return Ok(false);
12769            }
12770            let t_arm = std::time::Instant::now();
12771            if self.m3_winners.len() != self.num_layers {
12772                self.m3_winners = vec![Vec::new(); self.num_layers];
12773            }
12774            let pred = std::mem::take(&mut self.m3_winners[li]);
12775            let armed = crate::minicpm_tail::minimax_pool_arm(m, li, self.num_layers, &pred);
12776            if armed.is_none() {
12777                self.m3_winners[li] = pred; // declined before anything ran
12778            }
12779            M3_ARM_NS.fetch_add(t_arm.elapsed().as_nanos() as u64, std::sync::atomic::Ordering::Relaxed);
12780            match armed {
12781                Some(frame) => frame,
12782                // First-call refusal (geometry/bank): the host layer still
12783                // runs; nothing was mutated.
12784                None => return Ok(false),
12785            }
12786        };
12787        crate::gpu_wgpu::pooled_frame_enter(frame);
12788        // The pooled call must not fold lm_head: its cold experts have to
12789        // land before any final norm. The decode loop re-arms this flag for
12790        // the next position anyway.
12791        let want_logits = self.graph_want_logits;
12792        self.graph_want_logits = false;
12793        let mut lg = Vec::new();
12794        let mut gl = 0usize;
12795        let t_span = std::time::Instant::now();
12796        let span = crate::gpu::gpu_scope(|| {
12797            self.try_token_graph_wgpu_span(h, position, &mut lg, li, li + 1, &mut gl)
12798        });
12799        self.graph_want_logits = want_logits;
12800        M3_SPAN_NS.fetch_add(t_span.elapsed().as_nanos() as u64, std::sync::atomic::Ordering::Relaxed);
12801        let hh = match span {
12802            Some(Ok(hh)) => hh,
12803            Some(Err(())) => return Err(()),
12804            None => {
12805                // Declined before submission (seed/resolve/refusal): the
12806                // device mirror has not moved, so the host path is exact.
12807                return Ok(false);
12808            }
12809        };
12810        if gl != 1 {
12811            // A single-layer run that reports no layer mutated nothing we
12812            // may build on; anything else is a contract break.
12813            return if gl == 0 { Ok(false) } else { Err(()) };
12814        }
12815        *h = hh;
12816        let winners = crate::gpu_wgpu::pooled_frame_take_winners();
12817        self.m3_note_route(li, &winners);
12818        if self.m3_winners.len() != self.num_layers {
12819            self.m3_winners = vec![Vec::new(); self.num_layers];
12820        }
12821        self.m3_winners[li] = winners;
12822        let mut cold = crate::gpu_wgpu::pooled_frame_take_cold();
12823        // A non-finite parsed weight would poison the residual for the rest
12824        // of the sequence — drop such entries loudly instead.
12825        cold.retain(|&(_, w)| {
12826            if w.is_finite() {
12827                true
12828            } else {
12829                tracing::error!("pooled tail: non-finite cold weight from expert route");
12830                false
12831            }
12832        });
12833        if cold.is_empty() {
12834            return Ok(true);
12835        }
12836        let x = crate::gpu_wgpu::pooled_frame_take_input();
12837        if x.len() != self.hidden_size {
12838            return Err(());
12839        }
12840        // Cold experts: on the device first (admit, then one prepared frame
12841        // over exactly those slots — one submit for the layer). The host
12842        // computes them only when the device declines, and then they are
12843        // admitted for the next token. `CMF_M3_COLD_CPU=1` forces the host
12844        // arm for an A/B measurement.
12845        let lw = &self.weights.layers[self.phys_layer(li)];
12846        let FfnKind::Moe(m) = &lw.ffn else {
12847            return Err(());
12848        };
12849        let t_cold = std::time::Instant::now();
12850        let force_host = std::env::var("CMF_M3_COLD_CPU").as_deref() == Ok("1");
12851        let mut out = crate::attention::take_buf(self.hidden_size);
12852        let on_device = !force_host
12853            && crate::minicpm_tail::minimax_cold_device(m, li, self.num_layers, &cold, &x, &mut out);
12854        if !on_device {
12855            crate::attention::recycle_buf(&mut out);
12856            let jobs: Vec<(&crate::pipeline::DenseFfn, f32)> = cold
12857                .iter()
12858                .map(|&(e, w)| m.experts.get(e).map(|ex| (ex, w)).ok_or(()))
12859                .collect::<Result<_, ()>>()?;
12860            out = crate::gpu::cpu_scope(|| {
12861                crate::pipeline::moe_cold_experts_cpu(&jobs, &x, self.pool.as_deref())
12862            });
12863            let ids: Vec<usize> = cold.iter().map(|&(e, _)| e).collect();
12864            let _ = crate::minicpm_tail::minimax_pool_ensure(m, li, self.num_layers, &ids);
12865        }
12866        M3_COLD_NS.fetch_add(t_cold.elapsed().as_nanos() as u64, std::sync::atomic::Ordering::Relaxed);
12867        M3_COLD_N.fetch_add(cold.len() as u64, std::sync::atomic::Ordering::Relaxed);
12868        if out.len() != h.len() {
12869            crate::attention::recycle_buf(&mut out);
12870            return Err(());
12871        }
12872        for (dst, src) in h.iter_mut().zip(out.iter()) {
12873            *dst += src;
12874        }
12875        crate::attention::recycle_buf(&mut out);
12876        if on_device {
12877            M3_COLD_DEV_N.fetch_add(cold.len() as u64, std::sync::atomic::Ordering::Relaxed);
12878        }
12879        Ok(true)
12880    }
12881
12882    #[cfg(not(feature = "gpu"))]
12883    fn m3_pooled_layer(
12884        &mut self,
12885        _li: usize,
12886        _position: usize,
12887        _h: &mut Vec<f32>,
12888    ) -> Result<bool, ()> {
12889        Ok(false)
12890    }
12891
12892    /// Greedy burst: forward `t_next` and let the device pick + re-embed
12893    /// the next k−1 tokens — k frames, ONE submit, k ids back. The ZML
12894    /// trade, on wgpu. None ⇒ caller keeps the per-token path.
12895    fn try_multi_burst(&self, t_next: u32, position: usize, k: usize) -> Option<Vec<u32>> {
12896        if self.o1_active() || self.attn_softcap > 0.0 {
12897            return None;
12898        }
12899        // The burst builds the whole-token graph; attention the graph's
12900        // per-layer geometry cannot express keeps the per-token path.
12901        if let Some(reason) = self.wgpu_graph_attn_decline() {
12902            self.note_graph_decline("wgpu multi-burst", reason);
12903            return None;
12904        }
12905        let graph_on = crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode);
12906        if !graph_on || self.graph_refused() {
12907            // Same memo as the decode site: this path builds the very
12908            // same graph, so a model it cannot build for must not be
12909            // walked again here either. Missing this guard was worth
12910            // 2.5x on an Adreno — 0.361 tok/s against 0.905 — because
12911            // the burst retried per token what decode had already given
12912            // up on.
12913            return None;
12914        }
12915        let emb = self.embed_single(t_next);
12916        let mut lg = Vec::new();
12917        let mut ids = Vec::new();
12918        match self.try_token_graph_wgpu_steps(
12919            &emb,
12920            position,
12921            &mut lg,
12922            k,
12923            Some(&mut ids),
12924            None,
12925            0,
12926            self.num_layers,
12927        ) {
12928            Some(Ok(_)) => {}
12929            Some(Err(())) => {
12930                // Preserve the backend's post-admission failure through the
12931                // Option-based burst API.  The decode caller consumes this
12932                // flag and clears the sequence instead of falling through
12933                // to a stale CPU recurrent state.
12934                self.graph_failed
12935                    .store(true, std::sync::atomic::Ordering::Relaxed);
12936                return None;
12937            }
12938            None => return None,
12939        }
12940        (ids.len() == k).then_some(ids)
12941    }
12942
12943    /// Multi-step greedy: k whole frames in ONE submit, argmax and re-embed
12944    /// on the device. `ids_out` receives the k winner ids; the hidden/logits
12945    /// outputs are NOT produced in that mode.
12946    fn try_token_graph_wgpu_steps(
12947        &self,
12948        hidden: &[f32],
12949        position: usize,
12950        logits_out: &mut Vec<f32>,
12951        steps: usize,
12952        ids_out: Option<&mut Vec<u32>>,
12953        layers_run: Option<&mut usize>,
12954        from: usize,
12955        upto_excl: usize,
12956    ) -> Option<Result<Vec<f32>, ()>> {
12957        // The bank has already reserved its VRAM. Never build a second
12958        // all-expert arena across bank-owned layers (including bursts).
12959        let upto_excl = match self.mimo_moe.graph_prefix_end() {
12960            Some(end) if end < upto_excl => {
12961                if steps != 1 || layers_run.is_none() || from >= end {
12962                    return None;
12963                }
12964                end
12965            }
12966            _ => upto_excl,
12967        };
12968        // O(1) Nyström decode runs off the sealed state, not the KV cache the
12969        // graph mirrors — never take the graph while o1 is active.
12970        let o1_gpu = std::env::var("CMF_O1_GPU").as_deref() == Ok("1");
12971        if (self.o1_active() && !o1_gpu) || self.attn_softcap > 0.0 {
12972            // Softcapped scores have no graph kernel yet — CPU owns them.
12973            // o1 rides the graph only behind CMF_O1_GPU=1 while the port
12974            // proves itself; without it the CPU path owns o1 as before.
12975            return None;
12976        }
12977        // Per-layer KV heads, narrow V, sinks and sliding windows ride
12978        // `GraphAttn::Full::geom` (the ATTEND_X kernels). Anything that
12979        // geometry cannot express declines here, by name — before the
12980        // per-layer gate existed a sliding/sink model ran the graph as
12981        // full-context attention, fluent and wrong. The caller memoizes
12982        // the refusal.
12983        if let Some(reason) = self.wgpu_graph_attn_decline() {
12984            self.note_graph_decline("wgpu token graph", reason);
12985            return None;
12986        }
12987        // Per-layer sealed o1 state for the graph. During prefill the
12988        // state is still Collecting -> views are None -> the graph
12989        // refuses below and the CPU prefill records the q trace and
12990        // seals, exactly as the o1 design requires.
12991        let o1_views: Vec<Option<Vec<crate::nystrom::O1DeviceView<'_>>>> = (from..upto_excl)
12992            .map(|li| {
12993                if !o1_gpu {
12994                    return None;
12995                }
12996                self.kv_cache.layers[self.phys_layer(li)].o1_views()
12997            })
12998            .collect();
12999        if self.o1_active() && o1_gpu {
13000            // Any o1 layer not sealed (or degenerate exact-only) keeps the
13001            // whole token on the CPU: half-graph forwards would desync.
13002            let want: usize = (from..upto_excl)
13003                .filter(|li| self.kv_cache.layers[self.phys_layer(*li)].o1.is_some())
13004                .count();
13005            let have = o1_views.iter().filter(|v| v.is_some()).count();
13006            if want == 0 || have != want {
13007                // The silent twin of the gpu-side o1 gates, found the
13008                // same way: a 15x decode drop with an empty log. Views
13009                // stay None until the layer's state SEALS, so `have`
13010                // lagging `want` early in a run is the o1 design working
13011                // — but it must say so, or the next reader spends a
13012                // night proving the kernels innocent.
13013                // On CHANGE, not once: the first decline is the legal
13014                // unsealed prefill, and a once-print buries the state
13015                // that matters — what the count reads AFTER the seal.
13016                use std::sync::atomic::{AtomicUsize, Ordering};
13017                static LAST: AtomicUsize = AtomicUsize::new(usize::MAX);
13018                let code = have * 1000 + want;
13019                if LAST.swap(code, Ordering::Relaxed) != code {
13020                    tracing::warn!(
13021                        "o1 graph: {have} of {want} layers sealed — per-op until all seal"
13022                    );
13023                }
13024                return None;
13025            }
13026        }
13027        let nh = self.num_heads;
13028        let (nkv, hd, rd) = self.layer_geom(0);
13029        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
13030        let mut layers = Vec::with_capacity(upto_excl - from);
13031        let mut model = None;
13032        let dbg = std::env::var("CMF_GRAPH_DEBUG").is_ok();
13033        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
13034            // `blr` rides as GRAPH_KIND_BLR (two stage dispatches).
13035            if let Some((m, i, kind, rs)) = t.graph_weight_or_blr() {
13036                let name = &m.tensors[i].name;
13037                let prism = if crate::prism::is_inverse_embedding(m, name) {
13038                    crate::gpu::GraphPrismOp::InverseEmbedding
13039                } else if crate::prism::is_forward_weight(m, name) {
13040                    crate::gpu::GraphPrismOp::Forward
13041                } else {
13042                    crate::gpu::GraphPrismOp::None
13043                };
13044                return Some(crate::gpu::GraphW {
13045                    idx: i,
13046                    kind,
13047                    row_scale: rs,
13048                    data: &[],
13049                    prism,
13050                    affine: crate::prism::is_affine_target(m, name),
13051                });
13052            }
13053            // Small unquantized projections (GDN in_proj_a/b) stay f32.
13054            match t.as_f32() {
13055                Some(d) => Some(crate::gpu::GraphW {
13056                    idx: 0,
13057                    kind: 4,
13058                    row_scale: &[],
13059                    data: d,
13060                    prism: crate::gpu::GraphPrismOp::None,
13061                    affine: false,
13062                }),
13063                None => {
13064                    if std::env::var_os("CMF_BATCH_DEBUG").is_some() {
13065                        eprintln!("batch graph: weight has no graph/f32 representation");
13066                    }
13067                    None
13068                }
13069            }
13070        }
13071        for li in from..upto_excl {
13072            let lw = &self.weights.layers[self.phys_layer(li)];
13073            if dbg {
13074                let ak = match &lw.attn {
13075                    AttnKind::Mla(_) => "Mla".into(),
13076                    AttnKind::Full {
13077                        output_gate, bias, ..
13078                    } => format!("Full gate={output_gate} bias={}", bias.is_some()),
13079                    AttnKind::LinearGdn(_) => "LinearGdn".into(),
13080                    AttnKind::Kda(_) => "Kda".into(),
13081                    AttnKind::Linear(_) => "Linear".into(),
13082                    AttnKind::ShortConv(_) => "ShortConv".into(),
13083                    AttnKind::Bounded(_) => "Bounded".into(),
13084                };
13085                let fk = match &lw.ffn {
13086                    FfnKind::Dense(_) => "Dense",
13087                    FfnKind::Moe(_) => "Moe",
13088                    FfnKind::DenseMoe(_) => "DenseMoe",
13089                };
13090                eprintln!("graph L{li}: attn={ak} ffn={fk}");
13091            }
13092            let gffn = match &lw.ffn {
13093                FfnKind::DenseMoe(_) => return None, // dual branch: CPU path
13094                // A tube layer is several matrices, not one — the
13095                // whole-layer graph has no shape for it yet.
13096                FfnKind::Dense(d) if !d.segs.is_empty() => return None,
13097                FfnKind::Dense(d) => {
13098                    // An activation the graph has no kernel arm for keeps
13099                    // the CPU/per-op owner, by name — the dense graph FFN
13100                    // used to compute SiLU for whatever the model asked.
13101                    let Some(act) = d.act.wgpu_graph_act() else {
13102                        self.note_graph_decline(
13103                            "wgpu token graph",
13104                            "dense FFN activation without a graph kernel",
13105                        );
13106                        return None;
13107                    };
13108                    crate::gpu::GraphFfn::Dense {
13109                        gate: gw(&d.gate_proj)?,
13110                        up: gw(&d.up_proj)?,
13111                        down: gw(&d.down_proj)?,
13112                        act,
13113                    }
13114                }
13115                FfnKind::Moe(m) => {
13116                    // Adaptive τ and expert masks keep the CPU path, where
13117                    // they are implemented. Sigmoid routing with a selection
13118                    // bias (LFM2-MoE / DeepSeek noaux_tc), a routed scale ≠ 1
13119                    // and an UNGATED shared expert (HunYuan hy_v3: ×2.826 on
13120                    // the routed mix, the shared expert at weight 1) are all
13121                    // graphed — before, every such token fell to the per-op
13122                    // path whole (145 submits/token on Hy-MT2-30B-A3B).
13123                    if m.route_tau.is_some() || m.mask.is_some() {
13124                        return None;
13125                    }
13126                    let shared = m.shared.as_ref();
13127                    // The resident MoE kernels do not yet carry the
13128                    // descriptor-aware transform through router/shared-gate
13129                    // selection.  Refuse the complete layer instead of
13130                    // scoring with an untransformed Prism plane (the dense
13131                    // path has an explicit FWHT boundary below).
13132                    let inter = m.experts.first()?.gate_proj.rows();
13133                    // Flint's shared expert is a 1024-wide SwiGLU while its
13134                    // routed bank is 512-wide.  The resident MoE shader
13135                    // indexes a uniform-width packed bank, so keep that
13136                    // expert out of the routed slots and add it through the
13137                    // dense q4tp path after routed accumulation.  This is
13138                    // also the safe route for a mixed q2-routed/q4-shared
13139                    // artifact: neither layout nor row stride is faked.
13140                    let detached_shared = match shared {
13141                        Some((se, None))
13142                            if se.gate_proj.rows() != inter || se.up_proj.rows() != inter =>
13143                        {
13144                            if !matches!(se.act, Act::Silu)
13145                                || se.gate_proj.mapped_q4tp().is_none()
13146                                || se.up_proj.mapped_q4tp().is_none()
13147                                || se.down_proj.mapped_q4tp().is_none()
13148                            {
13149                                return None;
13150                            }
13151                            Some(crate::gpu::GraphMoeDetachedShared {
13152                                gate: gw(&se.gate_proj)?,
13153                                up: gw(&se.up_proj)?,
13154                                down: gw(&se.down_proj)?,
13155                                width: se.gate_proj.rows(),
13156                            })
13157                        }
13158                        // A separately shaped gated shared expert needs a
13159                        // second sigmoid gate path.  Do not turn it into an
13160                        // ungated one merely to make the graph run.
13161                        Some((se, Some(_)))
13162                            if se.gate_proj.rows() != inter || se.up_proj.rows() != inter =>
13163                        {
13164                            return None;
13165                        }
13166                        _ => None,
13167                    };
13168                    let has_shared = shared.is_some() && detached_shared.is_none();
13169                    let shared_gated = has_shared && matches!(shared, Some((_, Some(_))));
13170                    let sgate = match shared {
13171                        Some((_, Some(sg))) if has_shared => gw(sg)?,
13172                        // No gate, no shared expert, or a detached shared
13173                        // expert: the router only keeps the binding total;
13174                        // the selection shader does not read it.
13175                        _ => gw(&m.router)?,
13176                    };
13177                    let router = gw(&m.router)?;
13178                    // The resident MoE kernels do not yet carry the
13179                    // descriptor-aware transform through router/shared-gate
13180                    // selection.  Refuse the complete layer instead of
13181                    // scoring with an untransformed Prism plane (the dense
13182                    // path has an explicit FWHT boundary below).
13183                    if router.prism != crate::gpu::GraphPrismOp::None
13184                        || sgate.prism != crate::gpu::GraphPrismOp::None
13185                        || router.affine
13186                        || sgate.affine
13187                    {
13188                        tracing::warn!(
13189                            "resident MoE declined: Prism/affine router or shared gate transform is not implemented"
13190                        );
13191                        return None;
13192                    }
13193                    let mut experts = Vec::with_capacity(m.experts.len() + 1);
13194                    // q4t or q4tp, but not both in one layer — the kernels
13195                    // are picked per layer, not per expert.
13196                    let mut q4tp: Option<bool> = None;
13197                    // The mixed 2-bit profile: q2tp gate/up over a q4tp
13198                    // down. Uniform across the layer, like `q4tp` itself.
13199                    let mut gu_q2: Option<bool> = None;
13200                    // q8_2f experts (gate, up and down): their own kernels.
13201                    let mut q82: Option<bool> = None;
13202                    // One MoE gate/up kernel serves the complete packed bank.
13203                    // Bind an activation only when every routed (and in-bank
13204                    // shared) expert owns the same exact WGPU contract.
13205                    let mut moe_act = None;
13206                    for e in m
13207                        .experts
13208                        .iter()
13209                        .chain(shared.filter(|_| has_shared).map(|(se, _)| se).into_iter())
13210                    {
13211                        let Some(e_act) = e.act.wgpu_graph_act() else {
13212                            return None;
13213                        };
13214                        match moe_act {
13215                            Some(existing) if existing != e_act => return None,
13216                            None => moe_act = Some(e_act),
13217                            _ => {}
13218                        }
13219                        if e.gate_proj.rows() != inter || e.up_proj.rows() != inter {
13220                            return None;
13221                        }
13222                        // Expert tensors are packed into one resident buffer
13223                        // and the MoE kernels have no transform slot per
13224                        // expert.  Keep the CPU/per-op owner for Prism or
13225                        // affine experts rather than silently using raw bytes.
13226                        for expert_weight in [&e.gate_proj, &e.up_proj, &e.down_proj] {
13227                            let Some((em, ei, _, _)) = expert_weight
13228                                .graph_weight()
13229                                .or_else(|| expert_weight.graph_weight_descriptor())
13230                            else {
13231                                return None;
13232                            };
13233                            let name = &em.tensors[ei].name;
13234                            if crate::prism::is_forward_weight(em, name)
13235                                || crate::prism::is_inverse_embedding(em, name)
13236                                || crate::prism::is_affine_target(em, name)
13237                            {
13238                                tracing::warn!(
13239                                    "resident MoE declined: expert Prism/affine transform is not implemented"
13240                                );
13241                                return None;
13242                            }
13243                        }
13244                        let (mm, gi, ui, di, is_p, is_q2, is_8) = match e.gate_proj.mapped_q4t() {
13245                            Some((mm, gi)) => (
13246                                mm,
13247                                gi,
13248                                e.up_proj.mapped_q4t()?.1,
13249                                e.down_proj.mapped_q4t()?.1,
13250                                false,
13251                                false,
13252                                false,
13253                            ),
13254                            None => match e.gate_proj.mapped_q2tp() {
13255                                Some((mm, gi)) => (
13256                                    mm,
13257                                    gi,
13258                                    e.up_proj.mapped_q2tp()?.1,
13259                                    e.down_proj.mapped_q4tp()?.1,
13260                                    true,
13261                                    true,
13262                                    false,
13263                                ),
13264                                None => match e.gate_proj.mapped_q4tp() {
13265                                    Some((mm, gi)) => (
13266                                        mm,
13267                                        gi,
13268                                        e.up_proj.mapped_q4tp()?.1,
13269                                        e.down_proj.mapped_q4tp()?.1,
13270                                        true,
13271                                        false,
13272                                        false,
13273                                    ),
13274                                    // q8_2f experts: all three projections,
13275                                    // each with its own column field.
13276                                    None => {
13277                                        if !crate::gpu::moe_q82_graph_on() {
13278                                            return None;
13279                                        }
13280                                        let (mm, gi) = e.gate_proj.mapped_q8_2f()?;
13281                                        (
13282                                            mm,
13283                                            gi,
13284                                            e.up_proj.mapped_q8_2f()?.1,
13285                                            e.down_proj.mapped_q8_2f()?.1,
13286                                            false,
13287                                            false,
13288                                            true,
13289                                        )
13290                                    }
13291                                },
13292                            },
13293                        };
13294                        if *q4tp.get_or_insert(is_p) != is_p
13295                            || *gu_q2.get_or_insert(is_q2) != is_q2
13296                            || *q82.get_or_insert(is_8) != is_8
13297                        {
13298                            // The shared expert rides in the same packed
13299                            // buffer as the routed ones, so a layer that
13300                            // mixes layouts cannot be indexed by one stride.
13301                            // Say so: the symptom is a whole model quietly
13302                            // running its MoE on the CPU.
13303                            tracing::warn!(
13304                                "MoE layer mixes expert layouts (q4tp={is_p}, q2tp gate/up={is_q2}, q8_2f={is_8})                                  — every expert of a layer, INCLUDING the shared one, must share                                  a layout. The whole-token graph declines this layer."
13305                            );
13306                            return None;
13307                        }
13308                        model.get_or_insert_with(|| mm.clone());
13309                        experts.push((gi, ui, di));
13310                    }
13311                    crate::gpu::GraphFfn::Moe {
13312                        router,
13313                        shared_gate: sgate,
13314                        experts,
13315                        n_exp: m.experts.len(),
13316                        // CMF_TOPK_PROBE: timing probe only — output is WRONG.
13317                        // Fewer experts shrink the MoE arithmetic while the
13318                        // dispatch count stays identical, which is the only
13319                        // clean way to tell a launch-bound decode from a
13320                        // compute-bound one.
13321                        top_k: std::env::var("CMF_TOPK_PROBE")
13322                            .ok()
13323                            .and_then(|v| v.parse::<usize>().ok())
13324                            .filter(|k| *k > 0 && *k <= m.top_k)
13325                            .unwrap_or(m.top_k),
13326                        inter,
13327                        act: moe_act?,
13328                        norm_topk: m.norm_topk_prob,
13329                        q4tp: q4tp?,
13330                        gu_q2: gu_q2.unwrap_or(false),
13331                        q82: q82.unwrap_or(false),
13332                        sigmoid: m.router_sigmoid,
13333                        sigmoid_denom_eps: m.sigmoid_denom_eps,
13334                        bias: m.expert_bias.as_deref(),
13335                        has_shared,
13336                        detached_shared,
13337                        shared_gated,
13338                        route_scale: m.routed_scaling,
13339                    }
13340                }
13341            };
13342            let attn = match &lw.attn {
13343                AttnKind::Full {
13344                    wq,
13345                    wk,
13346                    wv,
13347                    wo,
13348                    q_norm,
13349                    k_norm,
13350                    output_gate,
13351                    softplus_gate,
13352                    bias,
13353                } => {
13354                    if self.attention_heads_per_layer.is_some() {
13355                        return None;
13356                    }
13357                    // A projected output gate rides the graph in one form:
13358                    // Spark-X2.5's head-wise sigmoid (one g_proj row per Q
13359                    // head). Laguna's softplus gate keeps the CPU path.
13360                    let head_gate = match softplus_gate {
13361                        None => None,
13362                        Some((g, true)) if self.proj_gate_sigmoid && !*output_gate => Some(gw(g)?),
13363                        Some(_) => {
13364                            self.note_graph_decline(
13365                                "wgpu token graph",
13366                                "projected softplus / per-element output gate",
13367                            );
13368                            return None;
13369                        }
13370                    };
13371                    let (m, _, _, _) = wq.graph_weight_or_blr()?;
13372                    model = Some(m.clone());
13373                    crate::gpu::GraphAttn::Full {
13374                        wq: gw(wq)?,
13375                        wk: gw(wk)?,
13376                        wv: gw(wv)?,
13377                        wo: gw(wo)?,
13378                        q_norm: q_norm.as_deref(),
13379                        k_norm: k_norm.as_deref(),
13380                        late_qk_norm: self.qk_norm_after_rope,
13381                        bias: bias
13382                            .as_ref()
13383                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
13384                        output_gate: *output_gate,
13385                        cpu_k: self.kv_cache.layers[li].k_heads(),
13386                        cpu_v: self.kv_cache.layers[li].v_heads(),
13387                        cpu_base: self.kv_cache.layers[li].base(),
13388                        geom: self.graph_attn_geom(li),
13389                        head_gate,
13390                    }
13391                }
13392                AttnKind::LinearGdn(w) => {
13393                    let cfg = self.gdn_cfg?;
13394                    let (m, _, _, _) = w.in_proj_qkv.graph_weight_or_blr()?;
13395                    model = Some(m.clone());
13396                    crate::gpu::GraphAttn::Gdn {
13397                        qkv: gw(&w.in_proj_qkv)?,
13398                        z: gw(&w.in_proj_z)?,
13399                        a: gw(&w.in_proj_a)?,
13400                        b: gw(&w.in_proj_b)?,
13401                        out: gw(&w.out_proj)?,
13402                        conv1d: &w.conv1d,
13403                        a_log: &w.a_log,
13404                        dt_bias: &w.dt_bias,
13405                        norm: &w.norm,
13406                        nv: cfg.num_v_heads,
13407                        nk: cfg.num_k_heads,
13408                        dk: cfg.key_head_dim,
13409                        dv: cfg.value_head_dim,
13410                        kk: cfg.conv_kernel,
13411                        cpu_state: &self.kv_cache.layers[self.phys_layer(li)].linear_state,
13412                        preview: None,
13413                    }
13414                }
13415                AttnKind::ShortConv(w) => {
13416                    let cfg = self.short_conv_cfg?;
13417                    let (m, _, _, _) = w
13418                        .in_proj
13419                        .graph_weight()
13420                        .or_else(|| w.in_proj.graph_weight_descriptor())?;
13421                    model = Some(m.clone());
13422                    crate::gpu::GraphAttn::ShortConv {
13423                        inp: gw(&w.in_proj)?,
13424                        out: gw(&w.out_proj)?,
13425                        taps: &w.conv,
13426                        kernel: cfg.kernel,
13427                        cpu_state: &self.kv_cache.layers[self.phys_layer(li)].linear_state,
13428                    }
13429                }
13430                _ => return None,
13431            };
13432            layers.push(crate::gpu::GraphLayer {
13433                input_norm: &lw.input_norm,
13434                attn,
13435                post_norm: &lw.post_norm,
13436                attn_out_norm: lw.attn_out_norm.as_deref(),
13437                ffn_out_norm: lw.ffn_out_norm.as_deref(),
13438                layer_scale: lw.layer_scale,
13439                ffn: gffn,
13440            });
13441        }
13442        let model = model?;
13443        // Fold final-norm + lm_head into the graph when this call wants logits
13444        // and the lm_head is a graphable (quantized) weight — the graph then
13445        // reads back logits (into logits_out) instead of the hidden, dropping
13446        // the separate CPU/GPU lm_head op + its sync. Never the f32 fallback:
13447        // an unquantized lm_head is vocab·hidden and must not be uploaded.
13448        let lm_gw = if upto_excl == self.num_layers
13449            && self.graph_want_logits
13450            && std::env::var("CMF_GPU_LMHEAD")
13451                .map(|v| v != "0")
13452                .unwrap_or(true)
13453        {
13454            self.weights
13455                .lm_head
13456                .graph_weight()
13457                .or_else(|| self.weights.lm_head.graph_weight_descriptor())
13458                .map(|(m, i, kind, rs)| {
13459                    let name = &m.tensors[i].name;
13460                    let prism = if crate::prism::is_inverse_embedding(m, name) {
13461                        crate::gpu::GraphPrismOp::InverseEmbedding
13462                    } else if crate::prism::is_forward_weight(m, name) {
13463                        crate::gpu::GraphPrismOp::Forward
13464                    } else {
13465                        crate::gpu::GraphPrismOp::None
13466                    };
13467                    (
13468                        crate::gpu::GraphW {
13469                            idx: i,
13470                            kind,
13471                            row_scale: rs,
13472                            data: &[],
13473                            prism,
13474                            affine: crate::prism::is_affine_target(m, name),
13475                        },
13476                        self.weights.lm_head.rows(),
13477                    )
13478                })
13479        } else {
13480            None
13481        };
13482        let lm = lm_gw.as_ref().map(|(gw, rows)| (gw, *rows));
13483        // Multi-step re-embeds the winner on the device.
13484        let emb_gw = if steps > 1 {
13485            self.weights
13486                .embed_tokens
13487                .graph_weight()
13488                .or_else(|| self.weights.embed_tokens.graph_weight_descriptor())
13489                .map(|(m, i, kind, rs)| {
13490                    let name = &m.tensors[i].name;
13491                    let prism = if crate::prism::is_inverse_embedding(m, name) {
13492                        crate::gpu::GraphPrismOp::InverseEmbedding
13493                    } else if crate::prism::is_forward_weight(m, name) {
13494                        crate::gpu::GraphPrismOp::Forward
13495                    } else {
13496                        crate::gpu::GraphPrismOp::None
13497                    };
13498                    (
13499                        crate::gpu::GraphW {
13500                            idx: i,
13501                            kind,
13502                            row_scale: rs,
13503                            data: &[],
13504                            prism,
13505                            affine: crate::prism::is_affine_target(m, name),
13506                        },
13507                        self.weights.embed_tokens.rows(),
13508                        self.embed_multiplier,
13509                    )
13510                })
13511        } else {
13512            None
13513        };
13514
13515        // Loop boundaries: virtual layer indices after which final_norm is
13516        // applied (mid-stack only; the GLOBAL last layer's norm folds into
13517        // lm_head). Span-relative — the executor compares its enumerate
13518        // index. A span ending mid-stack keeps its boundary norm even when
13519        // it is the span's own last layer.
13520        let loop_norm_at: Vec<usize> = if self.loop_final_norm {
13521            (from..upto_excl.min(self.num_layers - 1))
13522                .filter(|&li| (li + 1) % self.physical_layers == 0)
13523                .map(|li| li - from)
13524                .collect()
13525        } else {
13526            Vec::new()
13527        };
13528        let mut h = hidden.to_vec();
13529        // The normal decode path only needs the fused lm-head logits.  A
13530        // CMF_LOGIT_DUMP diagnostic, however, promises a prompt-boundary
13531        // post-stack hidden alongside those logits; request the existing
13532        // second readback only for that explicit probe instead of dumping
13533        // the input copy left in `h` by a folded-head graph.
13534        let dump_hidden = std::env::var_os("CMF_LOGIT_DUMP").is_some();
13535        let outcome = crate::gpu::forward_token_graph(
13536            &model,
13537            self.graph_kv_id,
13538            &layers,
13539            &o1_views,
13540            self.o1_epoch,
13541            &self.inv_freq,
13542            &mut h,
13543            nh,
13544            nkv,
13545            hd,
13546            self.attn_scale,
13547            rd,
13548            self.hidden_size,
13549            self.intermediate_size,
13550            position,
13551            self.kv_cache.max_seq_len,
13552            gemma,
13553            self.rms_eps as f32,
13554            lm,
13555            &self.weights.final_norm,
13556            logits_out,
13557            &loop_norm_at,
13558            steps,
13559            emb_gw.as_ref().map(|(gw, rows, m)| (gw, *rows, *m)),
13560            ids_out,
13561            layers_run,
13562            from,
13563            dump_hidden,
13564        );
13565        match outcome {
13566            crate::gpu::TokenGraphOutcome::Completed => Some(Ok(h)),
13567            crate::gpu::TokenGraphOutcome::Failed => Some(Err(())),
13568            crate::gpu::TokenGraphOutcome::Declined => None,
13569        }
13570    }
13571
13572    /// Batched prefill: k contiguous prompt positions through the whole wgpu
13573    /// graph in ONE submit (projections/FFN as GEMMs). `hiddens` is [k·hidden]
13574    /// in/out (embeddings in, layer output out); KV mirror / GDN state advance.
13575    /// false ⇒ unsupported → caller keeps the per-position graph.
13576    /// The b-row Metal graph plan for the whole model: every layer as a
13577    /// GDN run or a full-attention item, all-or-nothing (a layer outside the
13578    /// graph's contract → None, the caller runs plain). Shared by the
13579    /// speculative verify and the batched prefill.
13580    #[cfg(target_os = "macos")]
13581    #[allow(clippy::type_complexity)]
13582    fn metal_rows_plan(
13583        &self,
13584    ) -> Option<(
13585        Vec<MetalRowsItem<'_>>,
13586        std::sync::Arc<cortiq_core::CmfModel>,
13587        Option<crate::gpu_metal::GdnGpuCfg>,
13588    )> {
13589        use crate::gpu_metal::{AttnGpuLayer, GdnGpuCfg, GdnGpuLayer, MetalFfn};
13590        let graph_force = crate::gpu::q1_force() || crate::gpu::q2tp_gpu_opt_in();
13591        if !graph_force
13592            || !crate::gpu::enabled_here()
13593            || std::env::var("CMF_GPU_BLOCK")
13594                .map(|v| v == "0")
13595                .unwrap_or(false)
13596            || self.attn_softcap > 0.0
13597            || self.o1_active()
13598            || self.swa.is_some()
13599            || self.global_attn.is_some()
13600            || self.attention_heads_per_layer.is_some()
13601            // per-layer KV heads, narrow V, learned sinks (MiMo-V2)
13602            || self.graph_attn_decline_reason().is_some()
13603            || self.attn_v_norm
13604            || self.loop_final_norm
13605            || !self.metal_rope_scale_ok()
13606        {
13607            return None;
13608        }
13609        let attend_contract = self.head_dim % 4 == 0
13610            && self.head_dim <= 256
13611            && self.rotary_dim >= 2
13612            && self.rotary_dim <= self.head_dim
13613            && (self.rotary_dim / 2) % 32 == 0
13614            && self.num_kv_heads > 0
13615            && self.num_heads % self.num_kv_heads == 0;
13616        if !attend_contract {
13617            return None;
13618        }
13619        let mut plan: Vec<MetalRowsItem> = Vec::new();
13620        let mut model_ref: Option<std::sync::Arc<cortiq_core::CmfModel>> = None;
13621        for li in 0..self.num_layers {
13622            let lw = &self.weights.layers[self.phys_layer(li)];
13623            if lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some() {
13624                return None;
13625            }
13626            let ffn = match &lw.ffn {
13627                FfnKind::Dense(d) if d.act == Act::Silu && d.segs.is_empty() => {
13628                    let (Some(g), Some(u), Some(dn)) = (
13629                        d.gate_proj.metal_graph_parts(),
13630                        d.up_proj.metal_graph_parts(),
13631                        d.down_proj.metal_graph_parts(),
13632                    ) else {
13633                        return None;
13634                    };
13635                    MetalFfn::Dense {
13636                        gate: g,
13637                        up: u,
13638                        down: dn,
13639                        act: 0, // SiLU only (the arm above)
13640                    }
13641                }
13642                _ => return None,
13643            };
13644            match &lw.attn {
13645                AttnKind::LinearGdn(w) if self.gdn_cfg.is_some() => {
13646                    let (Some(qkv), Some(z), Some(a), Some(bb), Some(out)) = (
13647                        w.in_proj_qkv.metal_graph_parts(),
13648                        w.in_proj_z.metal_graph_parts(),
13649                        w.in_proj_a.f32_parts(),
13650                        w.in_proj_b.f32_parts(),
13651                        w.out_proj.metal_graph_parts(),
13652                    ) else {
13653                        return None;
13654                    };
13655                    if let QTensor::Mapped { model, .. } = &w.in_proj_qkv {
13656                        model_ref.get_or_insert_with(|| model.clone());
13657                    }
13658                    let gl = GdnGpuLayer {
13659                        attn_norm: &lw.input_norm,
13660                        post_norm: &lw.post_norm,
13661                        qkv,
13662                        z,
13663                        a,
13664                        b: bb,
13665                        out,
13666                        ffn,
13667                        conv1d: &w.conv1d,
13668                        a_log: &w.a_log,
13669                        dt_bias: &w.dt_bias,
13670                        gnorm: &w.norm,
13671                    };
13672                    match plan.last_mut() {
13673                        Some(MetalRowsItem::Gdn { run, .. }) => run.push(gl),
13674                        _ => plan.push(MetalRowsItem::Gdn {
13675                            run: vec![gl],
13676                            first: li,
13677                        }),
13678                    }
13679                }
13680                AttnKind::Full {
13681                    wq,
13682                    wk,
13683                    wv,
13684                    wo,
13685                    q_norm,
13686                    k_norm,
13687                    output_gate,
13688                    softplus_gate: None,
13689                    bias: None,
13690                } => {
13691                    let (Some(pq), Some(pk), Some(pv), Some(po)) = (
13692                        wq.metal_graph_parts(),
13693                        wk.metal_graph_parts(),
13694                        wv.metal_graph_parts(),
13695                        wo.metal_graph_parts(),
13696                    ) else {
13697                        return None;
13698                    };
13699                    if let QTensor::Mapped { model, .. } = wq {
13700                        model_ref.get_or_insert_with(|| model.clone());
13701                    }
13702                    let cache = &self.kv_cache.layers[li];
13703                    if cache.mode != crate::kv_cache::KvMode::F32 || cache.o1.is_some() {
13704                        return None;
13705                    }
13706                    plan.push(MetalRowsItem::Attn {
13707                        l: AttnGpuLayer {
13708                            attn_norm: &lw.input_norm,
13709                            post_norm: &lw.post_norm,
13710                            wq: pq,
13711                            wk: pk,
13712                            wv: pv,
13713                            wo: po,
13714                            ffn,
13715                            attn_out_norm: None,
13716                            ffn_out_norm: None,
13717                            layer_scale: None,
13718                        },
13719                        li,
13720                        q_norm: q_norm.as_deref(),
13721                        k_norm: k_norm.as_deref(),
13722                        output_gate: *output_gate,
13723                    });
13724                }
13725                _ => return None,
13726            }
13727        }
13728        let model = model_ref?;
13729        let gcfg = self.gdn_cfg.map(|cfg| GdnGpuCfg {
13730            nv: cfg.num_v_heads,
13731            nk: cfg.num_k_heads,
13732            dk: cfg.key_head_dim,
13733            dv: cfg.value_head_dim,
13734            kk: cfg.conv_kernel,
13735            hidden: self.hidden_size,
13736            inter: self.intermediate_size,
13737            c_dim: cfg.conv_dim(),
13738            eps: cfg.rms_eps as f32,
13739            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
13740        });
13741        Some((plan, model, gcfg))
13742    }
13743
13744    /// `AttnDeviceParams` for a plan item over the CPU cache as it stands.
13745    #[cfg(target_os = "macos")]
13746    #[allow(clippy::too_many_arguments)]
13747    fn metal_attn_params<'a>(
13748        li: usize,
13749        cache: &'a crate::kv_cache::LayerKvCache,
13750        q_norm: Option<&'a [f32]>,
13751        k_norm: Option<&'a [f32]>,
13752        output_gate: bool,
13753        inv_freq: &'a [f32],
13754        rope_scale: f32,
13755        geom: (usize, usize, usize, usize),
13756        pos0: usize,
13757        kv_id: u64,
13758        scale: f32,
13759        eps: f32,
13760        gemma: bool,
13761        late_qk_norm: bool,
13762    ) -> (crate::gpu_metal::AttnDeviceParams<'a>, usize) {
13763        let (nh, nkv, hd, rd) = geom;
13764        let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
13765        let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
13766        let cpu_stored = cpu_k[0].len() / hd;
13767        (
13768            crate::gpu_metal::AttnDeviceParams {
13769                kv_id,
13770                layer: li,
13771                nh,
13772                nkv,
13773                hd,
13774                rd,
13775                position: pos0,
13776                scale,
13777                eps,
13778                gemma,
13779                late_qk_norm,
13780                output_gate,
13781                q_norm,
13782                k_norm,
13783                inv_freq,
13784                rope_scale,
13785                cpu_k,
13786                cpu_v,
13787                cpu_stored,
13788                cpu_gen: cache.generation(),
13789                o1: None,
13790                window: None,
13791                softmax_sink: cache.sinks.as_deref(),
13792                head_gate: None,
13793                v_norm: false,
13794            },
13795            cpu_stored,
13796        )
13797    }
13798
13799    /// Run the rows plan over `hiddens` (b rows at `pos0..`): validate,
13800    /// encode every item, optionally the head, sync. Returns the graph
13801    /// (for the commit / state finish) plus the GDN layer indices and the
13802    /// attention layers with the row count they were encoded against.
13803    #[cfg(target_os = "macos")]
13804    #[allow(clippy::type_complexity)]
13805    fn metal_rows_run(
13806        &mut self,
13807        hiddens: &mut [f32],
13808        pos0: usize,
13809        b: usize,
13810        prefill: bool,
13811        spec: Option<((usize, usize, usize), &[f32], &mut Vec<f32>)>,
13812        // Greedy verify: (row length scored, the b argmax ids out) — the
13813        // head's argmax runs on the device and the logits plane is NOT
13814        // read back (`spec.2` stays empty).
13815        mut argmax_out: Option<(usize, &mut Vec<u32>)>,
13816    ) -> MetalRowsRun {
13817        use crate::gpu_metal::{GraphDims, VerifyGraph};
13818        // The previous round's commit may still be replaying into the
13819        // trunk GDN owners on the second queue: this graph reads them
13820        // (zero-copy wraps) and may reallocate them below — collect the
13821        // replay first. Normally already complete (the draft chain ran
13822        // in between); a failed replay is terminal like a failed commit.
13823        if !crate::gpu_metal::wait_replay() {
13824            tracing::error!("Metal rows graph: the pending async replay failed");
13825            return MetalRowsRun::Failed;
13826        }
13827        spec_stamp("v.wait");
13828        // Seed the GDN recurrent records the rows graph reads — GDN layers
13829        // ONLY (the record the CPU path would allocate anyway).  Sizing
13830        // every layer here planted a zero GDN-sized record on a bounded
13831        // anchor of a natively bounded file this path then refused, and
13832        // that record counted as recurrent state on macOS.
13833        let want = self.gdn_cfg.map(|c| c.state_len()).unwrap_or(0);
13834        if want > 0 {
13835            let phys = self.physical_layers.max(1);
13836            for (li, l) in self.kv_cache.layers.iter_mut().enumerate() {
13837                let is_gdn = self
13838                    .weights
13839                    .layers
13840                    .get(li % phys)
13841                    .is_some_and(|lw| matches!(lw.attn, AttnKind::LinearGdn(_)));
13842                if is_gdn && l.linear_state.len() != want {
13843                    l.linear_state = vec![0f32; want];
13844                }
13845            }
13846        }
13847        let Some((plan, model, gcfg)) = self.metal_rows_plan() else {
13848            return MetalRowsRun::Declined;
13849        };
13850        spec_stamp("v.plan");
13851        let dims = GraphDims {
13852            hidden: self.hidden_size,
13853            eps: self.rms_eps as f32,
13854            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
13855        };
13856        let Some(mut graph) = (if prefill {
13857            VerifyGraph::new_prefill(&model, dims, hiddens, b)
13858        } else {
13859            VerifyGraph::new(&model, dims, hiddens, b)
13860        }) else {
13861            return MetalRowsRun::Declined;
13862        };
13863        let geom = (
13864            self.num_heads,
13865            self.num_kv_heads,
13866            self.head_dim,
13867            self.rotary_dim,
13868        );
13869        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
13870        let eps = self.rms_eps as f32;
13871        let kv_id = self.graph_kv_id;
13872        let inv_freq = self.inv_freq.clone();
13873        for item in &plan {
13874            let ok = match item {
13875                MetalRowsItem::Gdn { run, .. } => gcfg
13876                    .as_ref()
13877                    .map(|gc| run.iter().all(|l| graph.gdn_ok(l, gc)))
13878                    .unwrap_or(false),
13879                MetalRowsItem::Attn {
13880                    l,
13881                    li,
13882                    q_norm,
13883                    k_norm,
13884                    output_gate,
13885                } => {
13886                    let (p, _) = Self::metal_attn_params(
13887                        *li,
13888                        &self.kv_cache.layers[*li],
13889                        *q_norm,
13890                        *k_norm,
13891                        *output_gate,
13892                        &inv_freq,
13893                        self.layer_rope_scale(*li),
13894                        geom,
13895                        pos0,
13896                        kv_id,
13897                        self.attn_scale,
13898                        eps,
13899                        gemma,
13900                        self.qk_norm_after_rope,
13901                    );
13902                    graph.attn_ok(l, &p)
13903                }
13904            };
13905            if !ok {
13906                use std::sync::atomic::{AtomicBool, Ordering};
13907                static SAID: AtomicBool = AtomicBool::new(false);
13908                if !SAID.swap(true, Ordering::Relaxed) {
13909                    tracing::warn!("metal rows graph: a layer failed preflight — declining");
13910                }
13911                return MetalRowsRun::Declined;
13912            }
13913        }
13914        let lm = match &spec {
13915            Some((lm, _, _)) => {
13916                if !graph.lm_head_ok(*lm) {
13917                    return MetalRowsRun::Declined;
13918                }
13919                Some(*lm)
13920            }
13921            None => None,
13922        };
13923        let mut gdn_layers = Vec::new();
13924        let mut attn_layers = Vec::new();
13925        for item in &plan {
13926            match item {
13927                MetalRowsItem::Gdn { run, first } => {
13928                    let ro: Vec<&[f32]> = self.kv_cache.layers[*first..*first + run.len()]
13929                        .iter()
13930                        .map(|l| l.linear_state.as_slice())
13931                        .collect();
13932                    if !graph.encode_gdn_run_b(run, &ro, gcfg.as_ref().unwrap()) {
13933                        return MetalRowsRun::Declined;
13934                    }
13935                    gdn_layers.extend(*first..*first + run.len());
13936                }
13937                MetalRowsItem::Attn {
13938                    l,
13939                    li,
13940                    q_norm,
13941                    k_norm,
13942                    output_gate,
13943                } => {
13944                    let (p, cpu_stored) = Self::metal_attn_params(
13945                        *li,
13946                        &self.kv_cache.layers[*li],
13947                        *q_norm,
13948                        *k_norm,
13949                        *output_gate,
13950                        &inv_freq,
13951                        self.layer_rope_scale(*li),
13952                        geom,
13953                        pos0,
13954                        kv_id,
13955                        self.attn_scale,
13956                        eps,
13957                        gemma,
13958                        self.qk_norm_after_rope,
13959                    );
13960                    if !graph.encode_attn_b(l, &p) {
13961                        return MetalRowsRun::Declined;
13962                    }
13963                    attn_layers.push((*li, cpu_stored));
13964                }
13965            }
13966        }
13967        if let (Some(lm), Some((_, final_norm, _))) = (lm, spec.as_ref()) {
13968            if !graph.encode_lm_head_b(final_norm, lm) {
13969                return MetalRowsRun::Declined;
13970            }
13971            // The device argmax is an OPTIMISATION, never a reason to
13972            // decline the round: if it will not encode, drop it and read
13973            // the logits plane back the old way (the head is encoded
13974            // either way, so the rows are there).
13975            if let Some((n, _)) = argmax_out.as_ref() {
13976                if !graph.encode_argmax_b(*n) {
13977                    argmax_out = None;
13978                }
13979            }
13980        }
13981        spec_stamp("v.enc");
13982        if !graph.sync() {
13983            return MetalRowsRun::Failed;
13984        }
13985        spec_stamp("v.gpu");
13986        match (spec, argmax_out) {
13987            (Some(_), Some((_, ids))) => {
13988                ids.resize(b, 0);
13989                if !graph.read_argmax(ids) {
13990                    return MetalRowsRun::Failed;
13991                }
13992                spec_stamp("v.am");
13993            }
13994            (Some((lm, _, logits)), None) => {
13995                logits.resize(b * lm.1, 0.0);
13996                if !graph.read_logits(logits) {
13997                    return MetalRowsRun::Failed;
13998                }
13999                spec_stamp("v.lg");
14000            }
14001            (None, _) => {}
14002        }
14003        if !graph.read_hidden(hiddens) {
14004            return MetalRowsRun::Failed;
14005        }
14006        spec_stamp("v.hid");
14007        MetalRowsRun::Completed(MetalVerifyPending {
14008            graph,
14009            gdn_layers,
14010            attn_layers,
14011        })
14012    }
14013
14014    /// Native-Metal twin of `try_batch_graph_wgpu`: the b rows through the
14015    /// whole model on the `VerifyGraph` (one submit), the head folded in
14016    /// when `spec` asks; `hiddens` come back as the last layer's output
14017    /// rows, `spec.2` as `[b][lm_rows]` logits. The graph is parked in
14018    /// `metal_verify` for `metal_verify_commit`.
14019    #[cfg(target_os = "macos")]
14020    fn try_batch_graph_metal(
14021        &mut self,
14022        hiddens: &mut [f32],
14023        positions: &[usize],
14024        b: usize,
14025        spec: Option<((usize, usize, usize), &[f32], &mut Vec<f32>)>,
14026        argmax_out: Option<(usize, &mut Vec<u32>)>,
14027    ) -> crate::gpu::BatchGraphOutcome {
14028        let _t0 = std::time::Instant::now();
14029        if positions.len() != b
14030            || positions.windows(2).any(|w| w[1] != w[0] + 1)
14031            || hiddens.len() != b * self.hidden_size
14032        {
14033            return crate::gpu::BatchGraphOutcome::Declined;
14034        }
14035        let pending = match self.metal_rows_run(hiddens, positions[0], b, false, spec, argmax_out) {
14036            MetalRowsRun::Declined => return crate::gpu::BatchGraphOutcome::Declined,
14037            MetalRowsRun::Failed => return crate::gpu::BatchGraphOutcome::Failed,
14038            MetalRowsRun::Completed(pending) => pending,
14039        };
14040        if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
14041            eprintln!(
14042                "metal-verify: {:.1} ms | b={b}",
14043                _t0.elapsed().as_secs_f64() * 1e3
14044            );
14045        }
14046        self.metal_verify = Some(pending);
14047        crate::gpu::BatchGraphOutcome::Completed
14048    }
14049
14050    /// Batched prefill on the Metal rows graph: `ids` (≤ 512) at
14051    /// `start_pos..`, states written in place, K/V rows appended to the
14052    /// CPU caches; optional final norm/head logits are returned in `spec`.
14053    /// Declined means no command buffer was admitted; Failed is terminal.
14054    #[cfg(target_os = "macos")]
14055    fn prefill_rows_metal(
14056        &mut self,
14057        ids: &[u32],
14058        start_pos: usize,
14059        spec: Option<((usize, usize, usize), &[f32], &mut Vec<f32>)>,
14060    ) -> MetalPrefillOutcome {
14061        let b = ids.len();
14062        if b == 0 || b > 512 {
14063            return MetalPrefillOutcome::Declined;
14064        }
14065        METAL_PREFILL_CHUNKS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
14066        let with_head = spec.is_some();
14067        let hs = self.hidden_size;
14068        let mut hiddens = vec![0f32; b * hs];
14069        for (j, &id) in ids.iter().enumerate() {
14070            let e = self.embed_single(id);
14071            hiddens[j * hs..(j + 1) * hs].copy_from_slice(&e);
14072        }
14073        let mut pending = match self.metal_rows_run(&mut hiddens, start_pos, b, true, spec, None) {
14074            MetalRowsRun::Declined => return MetalPrefillOutcome::Declined,
14075            MetalRowsRun::Failed => {
14076                METAL_PREFILL_ERRORS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
14077                return MetalPrefillOutcome::Failed;
14078            }
14079            MetalRowsRun::Completed(pending) => pending,
14080        };
14081        // states are final: copy them to the owners
14082        let idxs = pending.gdn_layers.clone();
14083        let mut outs: Vec<&mut [f32]> = self
14084            .kv_cache
14085            .layers
14086            .iter_mut()
14087            .enumerate()
14088            .filter(|(i, _)| idxs.binary_search(i).is_ok())
14089            .map(|(_, l)| l.linear_state.as_mut_slice())
14090            .collect();
14091        if !pending.graph.finish_states(&mut outs) {
14092            METAL_PREFILL_ERRORS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
14093            return MetalPrefillOutcome::Failed;
14094        }
14095        let (nkv, hd) = (self.num_kv_heads, self.head_dim);
14096        // Read every layer before mutating any CPU cache.  A missing mirror
14097        // row is a terminal graph failure, not a reason to append a partial
14098        // prefix and replay the remainder serially.
14099        let mut rows = Vec::with_capacity(pending.attn_layers.len());
14100        for (li, cpu_stored) in &pending.attn_layers {
14101            let mut kbuf = vec![0f32; b * nkv * hd];
14102            let mut vbuf = vec![0f32; b * nkv * hd];
14103            if !crate::gpu_metal::kv_mirror_read_rows(
14104                self.graph_kv_id,
14105                *li,
14106                nkv,
14107                hd,
14108                *cpu_stored,
14109                b,
14110                &mut kbuf,
14111                &mut vbuf,
14112            ) {
14113                METAL_PREFILL_ERRORS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
14114                return MetalPrefillOutcome::Failed;
14115            }
14116            rows.push((*li, *cpu_stored, kbuf, vbuf));
14117        }
14118        for (li, cpu_stored, kbuf, vbuf) in rows {
14119            let cache = &mut self.kv_cache.layers[li];
14120            for r in 0..b {
14121                cache.append(
14122                    &kbuf[r * nkv * hd..(r + 1) * nkv * hd],
14123                    &vbuf[r * nkv * hd..(r + 1) * nkv * hd],
14124                    &[],
14125                );
14126            }
14127            crate::gpu_metal::kv_mirror_set_stored(self.graph_kv_id, li, cpu_stored + b);
14128        }
14129        METAL_PREFILL_ROWS.fetch_add(b as u64, std::sync::atomic::Ordering::Relaxed);
14130        if with_head {
14131            METAL_PREFILL_HEAD_ROWS.fetch_add(b as u64, std::sync::atomic::Ordering::Relaxed);
14132        }
14133        MetalPrefillOutcome::Completed(hiddens)
14134    }
14135
14136    #[cfg(target_os = "macos")]
14137    fn prefill_batch_metal(&mut self, ids: &[u32], start_pos: usize) -> MetalPrefillOutcome {
14138        self.prefill_rows_metal(ids, start_pos, None)
14139    }
14140
14141    /// Exact teacher-forced NLL through the ordinary Metal rows graph.  This
14142    /// is intentionally separate from the serial TokenGraph scorer: every
14143    /// chunk owns a real b-row graph/head completion and the recurrent/KV
14144    /// handoff is committed before the next chunk begins.
14145    #[cfg(target_os = "macos")]
14146    fn nll_batch_metal(&mut self, ids: &[u32], start: usize) -> MetalBatchNllOutcome {
14147        if ids.len() < 2 || self.o1_active() || self.head_clusters.is_some() {
14148            return MetalBatchNllOutcome::Declined;
14149        }
14150        let Some(lm) = self.weights.lm_head.metal_graph_parts() else {
14151            return MetalBatchNllOutcome::Declined;
14152        };
14153        let chunk = std::env::var("CMF_METAL_PREFILL_CHUNK")
14154            .ok()
14155            .and_then(|v| v.parse::<usize>().ok())
14156            .filter(|&v| (1..=512).contains(&v))
14157            .unwrap_or(32);
14158        let final_norm = self.weights.final_norm.clone();
14159        let mut nll = 0.0f64;
14160        let mut count = 0usize;
14161        let mut pos = 0usize;
14162        let mut completed = 0usize;
14163        while pos < ids.len() {
14164            let end = (pos + chunk).min(ids.len());
14165            let mut logits = Vec::new();
14166            let outcome =
14167                self.prefill_rows_metal(&ids[pos..end], pos, Some((lm, &final_norm, &mut logits)));
14168            match outcome {
14169                MetalPrefillOutcome::Declined => {
14170                    return if completed == 0 {
14171                        MetalBatchNllOutcome::Declined
14172                    } else {
14173                        MetalBatchNllOutcome::Failed(format!(
14174                            "ordinary Metal NLL batch declined after {completed} chunks"
14175                        ))
14176                    };
14177                }
14178                MetalPrefillOutcome::Failed => {
14179                    return MetalBatchNllOutcome::Failed(
14180                        "ordinary Metal NLL batch failed after admission".to_string(),
14181                    );
14182                }
14183                MetalPrefillOutcome::Completed(_) => {}
14184            }
14185            completed += 1;
14186            let vocab = self.vocab_size.min(lm.1);
14187            if logits.len() != (end - pos) * lm.1 || vocab == 0 {
14188                return MetalBatchNllOutcome::Failed(
14189                    "ordinary Metal NLL head returned an invalid shape".to_string(),
14190                );
14191            }
14192            for row in 0..(end - pos) {
14193                let absolute = pos + row;
14194                if absolute < start || absolute + 1 >= ids.len() {
14195                    continue;
14196                }
14197                let lg = &mut logits[row * lm.1..row * lm.1 + vocab];
14198                if let Some(mu) = self.logit_multiplier {
14199                    for v in lg.iter_mut() {
14200                        *v *= mu;
14201                    }
14202                }
14203                if let Some(c) = self.final_softcap {
14204                    softcap_in_place(lg, c, self.pool.as_deref());
14205                }
14206                let target = ids[absolute + 1] as usize;
14207                if target >= vocab {
14208                    return MetalBatchNllOutcome::Failed(format!(
14209                        "target token {target} exceeds Metal head rows {vocab}"
14210                    ));
14211                }
14212                let max = lg.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
14213                let lse: f64 = lg
14214                    .iter()
14215                    .map(|&v| ((v - max) as f64).exp())
14216                    .sum::<f64>()
14217                    .ln()
14218                    + max as f64;
14219                nll += lse - lg[target] as f64;
14220                count += 1;
14221            }
14222            pos = end;
14223        }
14224        MetalBatchNllOutcome::Completed(nll, count)
14225    }
14226
14227    /// Commit a Metal verify round: replay the GDN recurrences over the
14228    /// `a + 1` accepted positions into the CPU states, append the accepted
14229    /// K/V rows from the mirrors to the CPU caches, re-point the mirrors.
14230    #[cfg(target_os = "macos")]
14231    fn metal_verify_commit(&mut self, a: usize) -> bool {
14232        let Some(mut pending) = self.metal_verify.take() else {
14233            return false;
14234        };
14235        let n = a + 1;
14236        // encode order == ascending layer order (the plan walks 0..layers)
14237        let idxs = pending.gdn_layers.clone();
14238        let mut outs: Vec<&mut [f32]> = self
14239            .kv_cache
14240            .layers
14241            .iter_mut()
14242            .enumerate()
14243            .filter(|(i, _)| idxs.binary_search(i).is_ok())
14244            .map(|(_, l)| l.linear_state.as_mut_slice())
14245            .collect();
14246        if !pending.graph.commit(n, &mut outs) {
14247            return false;
14248        }
14249        spec_stamp("c.replay");
14250        let (nkv, hd) = (self.num_kv_heads, self.head_dim);
14251        // Read every layer before mutating any CPU cache.  Missing rows are
14252        // terminal after the replay has executed; never append a partial KV
14253        // prefix and continue on a serial path.
14254        let mut rows = Vec::with_capacity(pending.attn_layers.len());
14255        for (li, cpu_stored) in &pending.attn_layers {
14256            let mut kbuf = vec![0f32; n * nkv * hd];
14257            let mut vbuf = vec![0f32; n * nkv * hd];
14258            if !crate::gpu_metal::kv_mirror_read_rows(
14259                self.graph_kv_id,
14260                *li,
14261                nkv,
14262                hd,
14263                *cpu_stored,
14264                n,
14265                &mut kbuf,
14266                &mut vbuf,
14267            ) {
14268                return false;
14269            }
14270            rows.push((*li, *cpu_stored, kbuf, vbuf));
14271        }
14272        for (li, cpu_stored, kbuf, vbuf) in rows {
14273            let cache = &mut self.kv_cache.layers[li];
14274            for r in 0..n {
14275                cache.append(
14276                    &kbuf[r * nkv * hd..(r + 1) * nkv * hd],
14277                    &vbuf[r * nkv * hd..(r + 1) * nkv * hd],
14278                    &[],
14279                );
14280            }
14281            crate::gpu_metal::kv_mirror_set_stored(self.graph_kv_id, li, cpu_stored + n);
14282        }
14283        spec_stamp("c.kv");
14284        true
14285    }
14286
14287    /// The round's warm-ups as ONE b-row graph run over the MTP block on
14288    /// Metal: `pairs` = (trunk hidden, next token) at consecutive positions
14289    /// from `first_pos`; the block's input projection is folded in. This
14290    /// half encodes and SUBMITS (no wait); `mtp_warm_batch_finish` waits
14291    /// and pulls the appended K/V rows into the CPU MTP cache. None = the
14292    /// graph declined (nothing submitted, nothing appended).
14293    #[cfg(target_os = "macos")]
14294    fn mtp_warm_batch_submit(
14295        &mut self,
14296        m: &mut MtpModule,
14297        pairs: &[(&[f32], u32)],
14298        first_pos: usize,
14299    ) -> Option<MetalWarmPending> {
14300        use crate::gpu_metal::{AttnDeviceParams, AttnGpuLayer, GraphDims, MetalFfn, VerifyGraph};
14301        let b = pairs.len();
14302        if b == 0 || b > 512 || m.kv.mode != crate::kv_cache::KvMode::F32 || m.kv.o1.is_some() {
14303            return None;
14304        }
14305        let AttnKind::Full {
14306            wq,
14307            wk,
14308            wv,
14309            wo,
14310            q_norm,
14311            k_norm,
14312            output_gate,
14313            softplus_gate: None,
14314            bias: None,
14315        } = &m.layer.attn
14316        else {
14317            return None;
14318        };
14319        let FfnKind::Dense(d) = &m.layer.ffn else {
14320            return None;
14321        };
14322        if !d.segs.is_empty() {
14323            return None;
14324        }
14325        let (Some(pq), Some(pk), Some(pv), Some(po)) =
14326            (wq.q1_parts(), wk.q1_parts(), wv.q1_parts(), wo.q1_parts())
14327        else {
14328            return None;
14329        };
14330        let (Some(g), Some(u), Some(dn)) = (
14331            d.gate_proj.q1_parts(),
14332            d.up_proj.q1_parts(),
14333            d.down_proj.q1_parts(),
14334        ) else {
14335            return None;
14336        };
14337        let Some(eh) = m.eh_proj.q1_parts() else {
14338            return None;
14339        };
14340        let QTensor::Mapped { model, .. } = wq else {
14341            return None;
14342        };
14343        let model = model.clone();
14344        let hs = self.hidden_size;
14345        // [enorm(embed(tok)); hnorm(hidden)] rows
14346        let mut cat = vec![0f32; b * 2 * hs];
14347        for (j, (h, tok)) in pairs.iter().enumerate() {
14348            let e = self.embed_single(*tok);
14349            let (ce, ch) = cat[j * 2 * hs..(j + 1) * 2 * hs].split_at_mut(hs);
14350            inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, ce);
14351            inference::rms_norm_into(h, &m.hnorm, self.rms_eps, self.norm_style, ch);
14352        }
14353        let dims = GraphDims {
14354            hidden: hs,
14355            eps: self.rms_eps as f32,
14356            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
14357        };
14358        spec_stamp("w.cat");
14359        let Some(mut graph) = VerifyGraph::new_via_proj(&model, dims, eh, &cat, b) else {
14360            return None;
14361        };
14362        spec_stamp("w.new");
14363        let l = AttnGpuLayer {
14364            attn_norm: &m.layer.input_norm,
14365            post_norm: &m.layer.post_norm,
14366            wq: pq,
14367            wk: pk,
14368            wv: pv,
14369            wo: po,
14370            ffn: MetalFfn::Dense {
14371                gate: g,
14372                up: u,
14373                down: dn,
14374                act: (d.act == Act::Gelu) as u8,
14375            },
14376            attn_out_norm: None,
14377            ffn_out_norm: None,
14378            layer_scale: None,
14379        };
14380        let (nh, nkv, hd, rd) = (
14381            self.num_heads,
14382            self.num_kv_heads,
14383            self.head_dim,
14384            self.rotary_dim,
14385        );
14386        let inv_freq = self.inv_freq.clone();
14387        let cpu_stored;
14388        {
14389            let cache = &m.kv;
14390            let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
14391            let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
14392            cpu_stored = cpu_k[0].len() / hd;
14393            // The cache may LAG the position (rows nobody warmed): the
14394            // pairs land at cpu_stored.. with their true RoPE positions
14395            // first_pos.., exactly what the one-by-one warm does. A cache
14396            // AHEAD of the position is a real inconsistency.
14397            if cpu_stored > first_pos {
14398                spec_stamp("w.decl");
14399                return None;
14400            }
14401            let p = AttnDeviceParams {
14402                kv_id: self.mtp_kv_id(),
14403                layer: Self::MTP_LAYER_BASE,
14404                nh,
14405                nkv,
14406                hd,
14407                rd,
14408                position: first_pos,
14409                scale: self.attn_scale,
14410                eps: self.rms_eps as f32,
14411                gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
14412                late_qk_norm: self.qk_norm_after_rope,
14413                output_gate: *output_gate,
14414                q_norm: q_norm.as_deref(),
14415                k_norm: k_norm.as_deref(),
14416                inv_freq: &inv_freq,
14417                rope_scale: self.rope_scale,
14418                cpu_k,
14419                cpu_v,
14420                cpu_stored,
14421                cpu_gen: cache.generation(),
14422                o1: None,
14423                window: None,
14424                softmax_sink: None,
14425                head_gate: None,
14426                v_norm: false,
14427            };
14428            if !graph.attn_ok(&l, &p) || !graph.encode_attn_b(&l, &p) {
14429                return None;
14430            }
14431        }
14432        spec_stamp("w.enc");
14433        if !graph.submit() {
14434            return None;
14435        }
14436        spec_stamp("w.sub");
14437        Some(MetalWarmPending {
14438            graph,
14439            cpu_stored,
14440            b,
14441        })
14442    }
14443
14444    /// Submit and finish in one call (the prefill's MTP warm-up, where
14445    /// nothing runs in between).
14446    #[cfg(target_os = "macos")]
14447    fn mtp_warm_batch_metal(
14448        &mut self,
14449        m: &mut MtpModule,
14450        pairs: &[(&[f32], u32)],
14451        first_pos: usize,
14452    ) -> bool {
14453        match self.mtp_warm_batch_submit(m, pairs, first_pos) {
14454            Some(p) => self.mtp_warm_batch_finish(m, p),
14455            None => false,
14456        }
14457    }
14458
14459    /// Second half of the batched warm-up: wait for the submitted graph,
14460    /// pull its b appended K/V rows into the CPU MTP cache, re-point the
14461    /// mirror. False = the command buffer failed or the rows are missing
14462    /// (nothing appended; the caller falls back to the one-by-one warm).
14463    #[cfg(target_os = "macos")]
14464    fn mtp_warm_batch_finish(&mut self, m: &mut MtpModule, pending: MetalWarmPending) -> bool {
14465        let MetalWarmPending {
14466            mut graph,
14467            cpu_stored,
14468            b,
14469        } = pending;
14470        let (nkv, hd) = (self.num_kv_heads, self.head_dim);
14471        if !graph.sync() {
14472            return false;
14473        }
14474        spec_stamp("w.gpu");
14475        let mut kbuf = vec![0f32; b * nkv * hd];
14476        let mut vbuf = vec![0f32; b * nkv * hd];
14477        if !crate::gpu_metal::kv_mirror_read_rows(
14478            self.mtp_kv_id(),
14479            Self::MTP_LAYER_BASE,
14480            nkv,
14481            hd,
14482            cpu_stored,
14483            b,
14484            &mut kbuf,
14485            &mut vbuf,
14486        ) {
14487            return false;
14488        }
14489        for r in 0..b {
14490            m.kv.append(
14491                &kbuf[r * nkv * hd..(r + 1) * nkv * hd],
14492                &vbuf[r * nkv * hd..(r + 1) * nkv * hd],
14493                &[],
14494            );
14495        }
14496        crate::gpu_metal::kv_mirror_set_stored(
14497            self.mtp_kv_id(),
14498            Self::MTP_LAYER_BASE,
14499            cpu_stored + b,
14500        );
14501        spec_stamp("w.kv");
14502        true
14503    }
14504
14505    /// A committed token id from the high table (Cyrillic, CJK and the
14506    /// like sit above 131072 in Qwen's vocabulary; Latin subwords past
14507    /// the 65536 cut are rare enough to lose as rejected drafts) switches
14508    /// the draft to the full head for the next 16 tokens; other ids count
14509    /// down. On an M4 the full 660 MB head costs 5.5 ms a draft step
14510    /// against 1.4 for the shortlist, so the streak is kept short.
14511    pub(crate) fn note_draft_id(&mut self, id: u32) {
14512        let cut = Self::draft_vocab_rows(usize::MAX).max(131_072);
14513        if (id as usize) >= cut {
14514            self.draft_full_streak = 16;
14515        } else {
14516            self.draft_full_streak = self.draft_full_streak.saturating_sub(1);
14517        }
14518    }
14519
14520    /// The draft head's rows for the next step: the shortlist, or the full
14521    /// head while `draft_full_streak` runs.
14522    fn draft_head_rows(&self, head_rows: usize) -> usize {
14523        if self.draft_full_streak > 0 {
14524            head_rows
14525        } else {
14526            Self::draft_vocab_rows(head_rows)
14527        }
14528    }
14529
14530    /// Draft-head shortlist size: `CMF_DRAFT_VOCAB` rows (default 65536,
14531    /// capped at the head; 0 = full head).
14532    fn draft_vocab_rows(head_rows: usize) -> usize {
14533        static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
14534        let n = *N.get_or_init(|| {
14535            std::env::var("CMF_DRAFT_VOCAB")
14536                .ok()
14537                .and_then(|v| v.parse().ok())
14538                .unwrap_or(65536)
14539        });
14540        if n == 0 { head_rows } else { n.min(head_rows) }
14541    }
14542
14543    /// One MTP block step on the native Metal token graph: block input on
14544    /// the host, the attention layer + FFN device-resident over the MTP
14545    /// mirror, the head folded in when `want_logits`. The appended K/V row
14546    /// is pulled into the CPU MTP cache (owner of record) after the sync.
14547    #[cfg(target_os = "macos")]
14548    fn mtp_step_metal(
14549        &mut self,
14550        m: &mut MtpModule,
14551        hidden: &[f32],
14552        next_token: u32,
14553        position: usize,
14554        want_logits: bool,
14555    ) -> Option<(Vec<f32>, Vec<f32>)> {
14556        use crate::gpu_metal::{AttnDeviceParams, AttnGpuLayer, GraphDims, MetalFfn, TokenGraph};
14557        if std::env::var("CMF_MTP_GRAPH").as_deref() == Ok("0")
14558            || !crate::gpu::q1_force()
14559            || !crate::gpu::enabled_here()
14560            || self.attn_softcap > 0.0
14561            || self.attention_heads_per_layer.is_some()
14562            || m.kv.mode != crate::kv_cache::KvMode::F32
14563            || m.kv.o1.is_some()
14564        {
14565            return None;
14566        }
14567        let AttnKind::Full {
14568            wq,
14569            wk,
14570            wv,
14571            wo,
14572            q_norm,
14573            k_norm,
14574            output_gate,
14575            softplus_gate: None,
14576            bias: None,
14577        } = &m.layer.attn
14578        else {
14579            return None;
14580        };
14581        let FfnKind::Dense(d) = &m.layer.ffn else {
14582            return None;
14583        };
14584        if d.act != Act::Silu || !d.segs.is_empty() {
14585            return None;
14586        }
14587        let (pq, pk, pv, po) = (
14588            wq.q1_parts()?,
14589            wk.q1_parts()?,
14590            wv.q1_parts()?,
14591            wo.q1_parts()?,
14592        );
14593        let (g, u, dn) = (
14594            d.gate_proj.q1_parts()?,
14595            d.up_proj.q1_parts()?,
14596            d.down_proj.q1_parts()?,
14597        );
14598        let QTensor::Mapped { model, .. } = wq else {
14599            return None;
14600        };
14601        let model = model.clone();
14602        let lm = if want_logits {
14603            Some(self.weights.lm_head.q1_parts()?)
14604        } else {
14605            None
14606        };
14607        let dims = GraphDims {
14608            hidden: self.hidden_size,
14609            eps: self.rms_eps as f32,
14610            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
14611        };
14612        // The block input `eh_proj · [enorm(e); hnorm(h)]` rides in the
14613        // graph (one submit a step); the host per-op matvec if it cannot.
14614        let hs = self.hidden_size;
14615        let mut x = vec![0f32; hs];
14616        // Let an eligible q4tp MTP draft use the measured masked-nibble
14617        // matvec.  Unlike fused epilogues, this reduces the draft's
14618        // projection work without changing its residual-update schedule;
14619        // the target verifier remains the authority over emitted tokens.
14620        // The guard keeps `CMF_METAL_MVFAST=0` as an immediate rollback
14621        // switch and restores the caller's setting on every early return.
14622        let _mtp_fast = crate::gpu_metal::MvFastGuard::set_bits(
14623            crate::gpu_metal::DENSE_MV,
14624        );
14625        let mut graph = TokenGraph::new(&model, dims, &x)?;
14626        let mut folded = false;
14627        if let Some(eh) = m.eh_proj.q1_parts() {
14628            let e = self.embed_single(next_token);
14629            let mut cat = vec![0.0f32; 2 * hs];
14630            let (cat_e, cat_h) = cat.split_at_mut(hs);
14631            inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
14632            inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
14633            folded = graph.encode_input_proj(eh, &cat);
14634        }
14635        if !folded {
14636            x = self.mtp_block_input(m, hidden, next_token);
14637            graph = TokenGraph::new(&model, dims, &x)?;
14638        }
14639        spec_stamp("d.in");
14640        let l = AttnGpuLayer {
14641            attn_norm: &m.layer.input_norm,
14642            post_norm: &m.layer.post_norm,
14643            wq: pq,
14644            wk: pk,
14645            wv: pv,
14646            wo: po,
14647            ffn: MetalFfn::Dense {
14648                gate: g,
14649                up: u,
14650                down: dn,
14651                act: (d.act == Act::Gelu) as u8,
14652            },
14653            attn_out_norm: None,
14654            ffn_out_norm: None,
14655            layer_scale: None,
14656        };
14657        let (nh, nkv, hd, rd) = (
14658            self.num_heads,
14659            self.num_kv_heads,
14660            self.head_dim,
14661            self.rotary_dim,
14662        );
14663        let inv_freq = self.inv_freq.clone();
14664        {
14665            let cache = &m.kv;
14666            let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
14667            let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
14668            let cpu_stored = cpu_k[0].len() / hd;
14669            let p = AttnDeviceParams {
14670                kv_id: self.mtp_kv_id(),
14671                layer: Self::MTP_LAYER_BASE,
14672                nh,
14673                nkv,
14674                hd,
14675                rd,
14676                position,
14677                scale: self.attn_scale,
14678                eps: self.rms_eps as f32,
14679                gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
14680                late_qk_norm: self.qk_norm_after_rope,
14681                output_gate: *output_gate,
14682                q_norm: q_norm.as_deref(),
14683                k_norm: k_norm.as_deref(),
14684                inv_freq: &inv_freq,
14685                rope_scale: self.rope_scale,
14686                cpu_k,
14687                cpu_v,
14688                cpu_stored,
14689                cpu_gen: cache.generation(),
14690                o1: None,
14691                window: None,
14692                softmax_sink: None,
14693                head_gate: None,
14694                v_norm: false,
14695            };
14696            if !graph.attn_device_ok(&l, &p) || !graph.encode_attn_device(&l, &p) {
14697                return None;
14698            }
14699        }
14700        // The draft's head over a vocabulary SHORTLIST (the first
14701        // CMF_DRAFT_VOCAB rows — BPE ids run roughly by merge rank, so the
14702        // low ids carry the mass): the verify keeps the full head, so a true
14703        // token past the cut is only a rejected draft, never a wrong token.
14704        // 662 MB a step on Qwen3.8 becomes 170 MB at 65536.
14705        let draft_rows = if let Some(lm) = lm {
14706            self.draft_head_rows(lm.1)
14707        } else {
14708            0
14709        };
14710        if let Some(lm) = lm {
14711            if !graph.lm_head_ok(lm) {
14712                return None;
14713            }
14714            if draft_rows < lm.1 {
14715                if !graph.encode_lm_head_part(&m.final_norm, lm, draft_rows) {
14716                    return None;
14717                }
14718            } else {
14719                graph.encode_lm_head(&m.final_norm, lm);
14720            }
14721        }
14722        spec_stamp("d.enc");
14723        if graph.sync_checked().is_err() {
14724            return None;
14725        }
14726        spec_stamp("d.gpu");
14727        let mut logits = Vec::new();
14728        if let Some(lm) = lm {
14729            let n_read = draft_rows.min(lm.1).min(self.vocab_size);
14730            logits = attention::take_buf(n_read);
14731            graph.read_logits(&mut logits);
14732            // ids past the shortlist: never drafted (−∞ in every chain)
14733            pad_mtp_draft_logits(&mut logits, self.vocab_size);
14734        }
14735        graph.finish(&mut x);
14736        let mut krow = attention::take_buf(nkv * hd);
14737        let mut vrow = attention::take_buf(nkv * hd);
14738        if crate::gpu_metal::kv_mirror_read_last(
14739            self.mtp_kv_id(),
14740            Self::MTP_LAYER_BASE,
14741            nkv,
14742            hd,
14743            &mut krow,
14744            &mut vrow,
14745        ) {
14746            m.kv.append(&krow, &vrow, &[]);
14747        }
14748        attention::recycle_buf(&mut krow);
14749        attention::recycle_buf(&mut vrow);
14750        spec_stamp("d.rd");
14751        Some((logits, x))
14752    }
14753
14754    /// `CMF_MTP_CHAIN=0` keeps the per-step draft (one submit and one
14755    /// host round trip per MTP step); the default drafts the whole chain
14756    /// in one command buffer when the round is plain greedy.
14757    ///
14758    /// Measured on an M4 (24 GB), Qwen3.8-27B q4tp, P3 at 160 tokens,
14759    /// k=7, six runs per arm alternating inside one lock window — the
14760    /// round's draft phase (median over the 34 rounds of a run) is
14761    /// 34.5 ms per round old against 30.1 new, i.e. 4.93 → 4.31 ms per
14762    /// draft step. That is the whole prize: the 7 submits cost ~0.6 ms
14763    /// each in host and submit latency and nothing else changes —
14764    /// acceptance (3.41 of 7) and tokens per round (4.41) are identical,
14765    /// and the round is 289 → 285 ms, decode 13.8 → 14.0 tok/s.
14766    fn mtp_chain_on() -> bool {
14767        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
14768        *ON.get_or_init(|| std::env::var("CMF_MTP_CHAIN").as_deref() != Ok("0"))
14769    }
14770
14771    /// The round's k greedy drafts as ONE command buffer on Metal: the MTP
14772    /// block k times back to back, each step's token embedding gathered
14773    /// on the device from the argmax the step before it wrote, the head
14774    /// over the round's shortlist (or the full head during a full-head
14775    /// streak — decided once, before the chain, exactly as the per-step
14776    /// path decides it per step, since `draft_full_streak` only moves on
14777    /// a commit). One wait, then the k ids and the k appended K/V rows
14778    /// come back; the CPU MTP cache ends where k `mtp_step_metal` calls
14779    /// would have left it. `Err(false)` = declined before anything was
14780    /// committed (the per-step path takes the round); `Err(true)` = the
14781    /// command buffer failed after commit.
14782    #[cfg(target_os = "macos")]
14783    fn mtp_draft_chain_metal(
14784        &mut self,
14785        m: &mut MtpModule,
14786        hidden: &[f32],
14787        t_next: u32,
14788        position: usize,
14789        k: usize,
14790    ) -> Result<Vec<u32>, bool> {
14791        use crate::gpu_metal::{AttnDeviceParams, AttnGpuLayer, GraphDims, MetalFfn, TokenGraph};
14792        if k == 0
14793            || k > 64
14794            || !Self::mtp_chain_on()
14795            || std::env::var("CMF_MTP_GRAPH").as_deref() == Ok("0")
14796            || !crate::gpu::q1_force()
14797            || !crate::gpu::enabled_here()
14798            || self.attn_softcap > 0.0
14799            || self.attention_heads_per_layer.is_some()
14800            || m.kv.mode != crate::kv_cache::KvMode::F32
14801            || m.kv.o1.is_some()
14802            // the chain gathers embeddings itself: only the plain table
14803            || self.dsv4.is_some()
14804            || self.dsv41.is_some()
14805            || self.qwen4_exp.is_some()
14806            || self.per_token_stack()
14807        {
14808            return Err(false);
14809        }
14810        let AttnKind::Full {
14811            wq,
14812            wk,
14813            wv,
14814            wo,
14815            q_norm,
14816            k_norm,
14817            output_gate,
14818            softplus_gate: None,
14819            bias: None,
14820        } = &m.layer.attn
14821        else {
14822            return Err(false);
14823        };
14824        let FfnKind::Dense(d) = &m.layer.ffn else {
14825            return Err(false);
14826        };
14827        if d.act != Act::Silu || !d.segs.is_empty() {
14828            return Err(false);
14829        }
14830        let (Some(pq), Some(pk), Some(pv), Some(po)) =
14831            (wq.q1_parts(), wk.q1_parts(), wv.q1_parts(), wo.q1_parts())
14832        else {
14833            return Err(false);
14834        };
14835        let (Some(g), Some(u), Some(dn)) = (
14836            d.gate_proj.q1_parts(),
14837            d.up_proj.q1_parts(),
14838            d.down_proj.q1_parts(),
14839        ) else {
14840            return Err(false);
14841        };
14842        let (Some(eh), Some(lm)) = (m.eh_proj.q1_parts(), self.weights.lm_head.q1_parts()) else {
14843            return Err(false);
14844        };
14845        let QTensor::Mapped { model, .. } = wq else {
14846            return Err(false);
14847        };
14848        let model = model.clone();
14849        // the embedding table: a q4tp tensor of the SAME blob, no Prism
14850        // inverse-embedding post-pass
14851        let QTensor::Mapped {
14852            model: em,
14853            idx: eidx,
14854            dtype: cortiq_core::TensorDtype::Q4TiledP,
14855            ..
14856        } = &self.weights.embed_tokens
14857        else {
14858            return Err(false);
14859        };
14860        if !std::sync::Arc::ptr_eq(em, &model)
14861            || crate::prism::is_inverse_embedding(&model, &model.tensors[*eidx].name)
14862        {
14863            return Err(false);
14864        }
14865        let embed = (
14866            *eidx,
14867            self.weights.embed_tokens.rows(),
14868            self.weights.embed_tokens.cols(),
14869        );
14870        if embed.2 != self.hidden_size || hidden.len() != self.hidden_size {
14871            return Err(false);
14872        }
14873        let dims = GraphDims {
14874            hidden: self.hidden_size,
14875            eps: self.rms_eps as f32,
14876            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
14877        };
14878        // Keep the chained MTP draft on the masked-nibble Metal matvec
14879        // path.  This scope covers every encode in the chain; the guard
14880        // restores its predecessor when this round returns or declines.
14881        let _mtp_fast = crate::gpu_metal::MvFastGuard::set_bits(
14882            crate::gpu_metal::DENSE_MV,
14883        );
14884        let Some(mut graph) = TokenGraph::new(&model, dims, hidden) else {
14885            return Err(false);
14886        };
14887        if !graph.chain_embed_ok(embed) || !graph.lm_head_ok(lm) {
14888            return Err(false);
14889        }
14890        let l = AttnGpuLayer {
14891            attn_norm: &m.layer.input_norm,
14892            post_norm: &m.layer.post_norm,
14893            wq: pq,
14894            wk: pk,
14895            wv: pv,
14896            wo: po,
14897            ffn: MetalFfn::Dense {
14898                gate: g,
14899                up: u,
14900                down: dn,
14901                act: (d.act == Act::Gelu) as u8,
14902            },
14903            attn_out_norm: None,
14904            ffn_out_norm: None,
14905            layer_scale: None,
14906        };
14907        let (nh, nkv, hd, rd) = (
14908            self.num_heads,
14909            self.num_kv_heads,
14910            self.head_dim,
14911            self.rotary_dim,
14912        );
14913        let inv_freq = self.inv_freq.clone();
14914        let draft_rows = self.draft_head_rows(lm.1);
14915        let n_arg = draft_rows.min(lm.1).min(self.vocab_size);
14916        if n_arg == 0 {
14917            return Err(false);
14918        }
14919        // `CMF_MTP_CHAIN_SPLIT=1` commits each step as it is encoded, so
14920        // the GPU starts on step 0 while the host is still encoding step
14921        // 1 — a probe for whether the host encode is on the critical
14922        // path. It is not: three runs each, draft 30.0 ms per round split
14923        // against 30.1 whole, and the whole chain's host encode measures
14924        // 0.3 ms against a 29.7 ms wait. Kept as a probe, off by default.
14925        let split = std::env::var("CMF_MTP_CHAIN_SPLIT").as_deref() == Ok("1");
14926        let t_chain = std::time::Instant::now();
14927        graph.chain_ids_init(t_next, k);
14928        let cpu_stored;
14929        {
14930            let cache = &m.kv;
14931            let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
14932            let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
14933            cpu_stored = cpu_k[0].len() / hd;
14934            for j in 0..k {
14935                if !graph.encode_chain_input(
14936                    embed,
14937                    j as u32,
14938                    &m.enorm,
14939                    &m.hnorm,
14940                    self.embed_multiplier,
14941                    eh,
14942                ) {
14943                    return Err(false);
14944                }
14945                // step j's mirror row: the mirror is re-pointed at the CPU
14946                // rows before step 0 and advances by one per step; its
14947                // resync (never taken past step 0) reads the CPU rows
14948                let p = AttnDeviceParams {
14949                    kv_id: self.mtp_kv_id(),
14950                    layer: Self::MTP_LAYER_BASE,
14951                    nh,
14952                    nkv,
14953                    hd,
14954                    rd,
14955                    position: position + j,
14956                    scale: self.attn_scale,
14957                    eps: self.rms_eps as f32,
14958                    gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
14959                    late_qk_norm: self.qk_norm_after_rope,
14960                    output_gate: *output_gate,
14961                    q_norm: q_norm.as_deref(),
14962                    k_norm: k_norm.as_deref(),
14963                    inv_freq: &inv_freq,
14964                    rope_scale: self.rope_scale,
14965                    cpu_k: cpu_k.clone(),
14966                    cpu_v: cpu_v.clone(),
14967                    cpu_stored: cpu_stored + j,
14968                    cpu_gen: cache.generation(),
14969                    o1: None,
14970                    window: None,
14971                    softmax_sink: None,
14972                    head_gate: None,
14973                    v_norm: false,
14974                };
14975                if !graph.attn_device_ok(&l, &p) || !graph.encode_attn_device(&l, &p) {
14976                    return Err(false);
14977                }
14978                if draft_rows < lm.1 {
14979                    if !graph.encode_lm_head_part(&m.final_norm, lm, draft_rows) {
14980                        return Err(false);
14981                    }
14982                } else {
14983                    graph.encode_lm_head(&m.final_norm, lm);
14984                }
14985                if !graph.encode_argmax(n_arg, j as u32 + 1) {
14986                    return Err(false);
14987                }
14988                if split {
14989                    // CMF_MTP_CHAIN_SPLIT=1: commit every step so the GPU
14990                    // starts on step 0 while the host encodes the rest
14991                    graph.commit();
14992                }
14993            }
14994        }
14995        let t_enc = t_chain.elapsed();
14996        if graph.sync_checked().is_err() {
14997            return Err(true);
14998        }
14999        if std::env::var_os("CMF_GRAPH_SPEC_TIME").is_some() {
15000            eprintln!(
15001                "mtp-chain: encode {:.1} ms | wait {:.1} ms (k={k}, head rows {draft_rows}{})",
15002                t_enc.as_secs_f64() * 1e3,
15003                (t_chain.elapsed() - t_enc).as_secs_f64() * 1e3,
15004                if split { ", split" } else { "" }
15005            );
15006        }
15007        let mut ids = vec![0u32; k];
15008        if !graph.chain_ids_read(&mut ids) {
15009            return Err(true);
15010        }
15011        let mut kbuf = vec![0f32; k * nkv * hd];
15012        let mut vbuf = vec![0f32; k * nkv * hd];
15013        if !crate::gpu_metal::kv_mirror_read_rows(
15014            self.mtp_kv_id(),
15015            Self::MTP_LAYER_BASE,
15016            nkv,
15017            hd,
15018            cpu_stored,
15019            k,
15020            &mut kbuf,
15021            &mut vbuf,
15022        ) {
15023            return Err(true);
15024        }
15025        for r in 0..k {
15026            m.kv.append(
15027                &kbuf[r * nkv * hd..(r + 1) * nkv * hd],
15028                &vbuf[r * nkv * hd..(r + 1) * nkv * hd],
15029                &[],
15030            );
15031        }
15032        Ok(ids)
15033    }
15034
15035    fn try_batch_graph_wgpu(
15036        &self,
15037        hiddens: &mut [f32],
15038        positions: &[usize],
15039        k: usize,
15040        spec: Option<crate::gpu::SpecTail<'_>>,
15041    ) -> crate::gpu::BatchGraphOutcome {
15042        self.try_batch_graph_wgpu_prefix(hiddens, positions, k, spec, None)
15043    }
15044
15045    /// `try_batch_graph_wgpu` with the device-prefix mode: `layers_run`
15046    /// Some lets a stack that does not fit run its leading layers (the
15047    /// token graph's prefix rule) and reports how many; `hiddens` then
15048    /// holds the boundary rows and the caller runs the rest on the host.
15049    fn try_batch_graph_wgpu_prefix(
15050        &self,
15051        hiddens: &mut [f32],
15052        positions: &[usize],
15053        k: usize,
15054        spec: Option<crate::gpu::SpecTail<'_>>,
15055        layers_run: Option<&mut usize>,
15056    ) -> crate::gpu::BatchGraphOutcome {
15057        let graph_end = match self.mimo_moe.graph_prefix_end() {
15058            Some(end) if end < self.num_layers => {
15059                if layers_run.is_none() || spec.is_some() || end == 0 {
15060                    return crate::gpu::BatchGraphOutcome::Declined;
15061                }
15062                end
15063            }
15064            _ => self.num_layers,
15065        };
15066        let _tb = std::time::Instant::now();
15067        let batch_debug = std::env::var_os("CMF_BATCH_DEBUG").is_some();
15068        if self.attn_softcap > 0.0 {
15069            return crate::gpu::BatchGraphOutcome::Declined; // capped scores: no graph kernel — CPU path
15070        }
15071        // Same attention contract as the token graph: per-layer geometry
15072        // rides `geom`, anything it cannot express declines by name.
15073        if let Some(reason) = self
15074            .wgpu_graph_attn_decline()
15075            .or_else(|| self.batch_graph_gemma_decline())
15076        {
15077            self.note_graph_decline("wgpu batch graph", reason);
15078            return crate::gpu::BatchGraphOutcome::Declined;
15079        }
15080        let nh = self.num_heads;
15081        let (nkv, hd, rd) = self.layer_geom(0);
15082        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
15083        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
15084            if let Some((m, i, kind, rs)) = t.graph_weight().or_else(|| t.graph_weight_descriptor())
15085            {
15086                let name = &m.tensors[i].name;
15087                let prism = if crate::prism::is_inverse_embedding(m, name) {
15088                    crate::gpu::GraphPrismOp::InverseEmbedding
15089                } else if crate::prism::is_forward_weight(m, name) {
15090                    crate::gpu::GraphPrismOp::Forward
15091                } else {
15092                    crate::gpu::GraphPrismOp::None
15093                };
15094                return Some(crate::gpu::GraphW {
15095                    idx: i,
15096                    kind,
15097                    row_scale: rs,
15098                    data: &[],
15099                    prism,
15100                    affine: crate::prism::is_affine_target(m, name),
15101                });
15102            }
15103            if std::env::var_os("CMF_BATCH_DEBUG").is_some() {
15104                eprintln!(
15105                    "batch graph: tensor has no graph descriptor/f32 fallback rows={} cols={}",
15106                    t.rows(),
15107                    t.cols()
15108                );
15109            }
15110            t.as_f32().map(|d| crate::gpu::GraphW {
15111                idx: 0,
15112                kind: 4,
15113                row_scale: &[],
15114                data: d,
15115                prism: crate::gpu::GraphPrismOp::None,
15116                affine: false,
15117            })
15118        }
15119        let built: Option<(
15120            Vec<crate::gpu::GraphLayer<'_>>,
15121            std::sync::Arc<cortiq_core::CmfModel>,
15122        )> = (|| {
15123            let mut layers = Vec::with_capacity(graph_end);
15124            let mut model = None;
15125            for li in 0..graph_end {
15126                let lw = &self.weights.layers[self.phys_layer(li)];
15127                // MoE routes per token, so its experts are encoded token by
15128                // token inside the batched submit while attention and the
15129                // projections stay GEMMs. Refusing MoE here is what left
15130                // prefill running one position at a time: 33 tok/s against
15131                // 54 on decode, i.e. reading the prompt was slower than
15132                // writing the answer.
15133                let gffn = match &lw.ffn {
15134                    FfnKind::Dense(d) if !d.segs.is_empty() => {
15135                        if batch_debug {
15136                            eprintln!("batch graph: dense segmented FFN at layer {li}");
15137                        }
15138                        return None;
15139                    }
15140                    FfnKind::Dense(d) => {
15141                        let Some(act) = d.act.wgpu_graph_act() else {
15142                            if batch_debug {
15143                                eprintln!(
15144                                    "batch graph: dense FFN activation {:?} without a graph kernel at layer {li}",
15145                                    d.act
15146                                );
15147                            }
15148                            return None;
15149                        };
15150                        crate::gpu::GraphFfn::Dense {
15151                            gate: gw(&d.gate_proj)?,
15152                            up: gw(&d.up_proj)?,
15153                            down: gw(&d.down_proj)?,
15154                            act,
15155                        }
15156                    }
15157                    FfnKind::Moe(m) => {
15158                        // Adaptive τ and expert masks stay on the CPU path.
15159                        // Sigmoid scores, the selection bias, a routed scale
15160                        // ≠ 1 and an ungated shared expert (hy_v3) ride the
15161                        // same flags word as the token graph — before, this
15162                        // refusal sent every Hy-MT2-30B prompt to the chunked
15163                        // fallback (8 tok/s of ingest against 53 of decode).
15164                        if m.route_tau.is_some() || m.mask.is_some() {
15165                            return None;
15166                        }
15167                        // A shared expert rides as slot top_k (gated or
15168                        // not is a flag on the select kernel); without one
15169                        // (MiMo-V2, LFM2-MoE) the kernels run top_k slots.
15170                        let shared = m.shared.as_ref();
15171                        let has_shared = shared.is_some();
15172                        let shared_gated = matches!(shared, Some((_, Some(_))));
15173                        let sgate = match shared {
15174                            Some((_, Some(sg))) => gw(sg)?,
15175                            // Ungated or absent: the router plane stands in
15176                            // so the plumbing stays total; the kernel pins
15177                            // weight 1 or never reads it.
15178                            _ => gw(&m.router)?,
15179                        };
15180                        let router = gw(&m.router)?;
15181                        // The batch MoE kernels still consume raw per-token
15182                        // rows and do not carry the descriptor-aware Prism
15183                        // transform/affine bit for router or shared-gate
15184                        // planes.  Refuse rather than route an untransformed
15185                        // source activation.
15186                        if router.prism != crate::gpu::GraphPrismOp::None
15187                            || router.affine
15188                            || sgate.prism != crate::gpu::GraphPrismOp::None
15189                            || sgate.affine
15190                        {
15191                            return None;
15192                        }
15193                        let inter = m.experts.first()?.gate_proj.rows();
15194                        let mut experts = Vec::with_capacity(m.experts.len() + 1);
15195                        let mut q4tp: Option<bool> = None;
15196                        let mut gu_q2: Option<bool> = None;
15197                        let mut q82: Option<bool> = None;
15198                        // The packed MoE graph has one gate/up program per
15199                        // layer, so accept only one exact activation contract
15200                        // across routed and in-bank shared experts.
15201                        let mut moe_act = None;
15202                        for e in m.experts.iter().chain(shared.map(|(se, _)| se)) {
15203                            let Some(e_act) = e.act.wgpu_graph_act() else {
15204                                return None;
15205                            };
15206                            match moe_act {
15207                                Some(existing) if existing != e_act => return None,
15208                                None => moe_act = Some(e_act),
15209                                _ => {}
15210                            }
15211                            if e.gate_proj.rows() != inter || e.up_proj.rows() != inter {
15212                                return None;
15213                            }
15214                            // Same ladder as the token graph: q4t → q2tp
15215                            // (mixed profile: 2-bit gate/up over a q4tp
15216                            // down) → q4tp → q8_2f. Uniform across the layer.
15217                            let (mm, gi, ui, di, is_p, is_q2, is_8) = match e.gate_proj.mapped_q4t()
15218                            {
15219                                Some((mm, gi)) => (
15220                                    mm,
15221                                    gi,
15222                                    e.up_proj.mapped_q4t()?.1,
15223                                    e.down_proj.mapped_q4t()?.1,
15224                                    false,
15225                                    false,
15226                                    false,
15227                                ),
15228                                None => match e.gate_proj.mapped_q2tp() {
15229                                    Some((mm, gi)) => (
15230                                        mm,
15231                                        gi,
15232                                        e.up_proj.mapped_q2tp()?.1,
15233                                        e.down_proj.mapped_q4tp()?.1,
15234                                        true,
15235                                        true,
15236                                        false,
15237                                    ),
15238                                    None => match e.gate_proj.mapped_q4tp() {
15239                                        Some((mm, gi)) => (
15240                                            mm,
15241                                            gi,
15242                                            e.up_proj.mapped_q4tp()?.1,
15243                                            e.down_proj.mapped_q4tp()?.1,
15244                                            true,
15245                                            false,
15246                                            false,
15247                                        ),
15248                                        None => {
15249                                            if !crate::gpu::moe_q82_graph_on() {
15250                                                return None;
15251                                            }
15252                                            let (mm, gi) = e.gate_proj.mapped_q8_2f()?;
15253                                            (
15254                                                mm,
15255                                                gi,
15256                                                e.up_proj.mapped_q8_2f()?.1,
15257                                                e.down_proj.mapped_q8_2f()?.1,
15258                                                false,
15259                                                false,
15260                                                true,
15261                                            )
15262                                        }
15263                                    },
15264                                },
15265                            };
15266                            if *q4tp.get_or_insert(is_p) != is_p
15267                                || *gu_q2.get_or_insert(is_q2) != is_q2
15268                                || *q82.get_or_insert(is_8) != is_8
15269                            {
15270                                return None;
15271                            }
15272                            if [gi, ui, di].into_iter().any(|idx| {
15273                                mm.tensors.get(idx).is_some_and(|t| {
15274                                    crate::prism::is_forward_weight(mm, &t.name)
15275                                        || crate::prism::is_affine_target(mm, &t.name)
15276                                })
15277                            }) {
15278                                return None;
15279                            }
15280                            model.get_or_insert_with(|| mm.clone());
15281                            experts.push((gi, ui, di));
15282                        }
15283                        crate::gpu::GraphFfn::Moe {
15284                            router,
15285                            shared_gate: sgate,
15286                            experts,
15287                            n_exp: m.experts.len(),
15288                            top_k: m.top_k,
15289                            inter,
15290                            act: moe_act?,
15291                            norm_topk: m.norm_topk_prob,
15292                            q4tp: q4tp?,
15293                            gu_q2: gu_q2.unwrap_or(false),
15294                            q82: q82.unwrap_or(false),
15295                            sigmoid: m.router_sigmoid,
15296                            sigmoid_denom_eps: m.sigmoid_denom_eps,
15297                            bias: m.expert_bias.as_deref(),
15298                            has_shared,
15299                            detached_shared: None,
15300                            shared_gated,
15301                            route_scale: m.routed_scaling,
15302                        }
15303                    }
15304                    _ => return None,
15305                };
15306                let attn = match &lw.attn {
15307                    AttnKind::Full {
15308                        wq,
15309                        wk,
15310                        wv,
15311                        wo,
15312                        q_norm,
15313                        k_norm,
15314                        output_gate,
15315                        softplus_gate,
15316                        bias,
15317                    } => {
15318                        if softplus_gate.is_some() || self.attention_heads_per_layer.is_some() {
15319                            if batch_debug {
15320                                eprintln!(
15321                                    "batch graph: unsupported Full attention gate at layer {li} softplus={} heads={}",
15322                                    softplus_gate.is_some(),
15323                                    self.attention_heads_per_layer.is_some()
15324                                );
15325                            }
15326                            return None;
15327                        }
15328                        let (m, _, _, _) =
15329                            wq.graph_weight().or_else(|| wq.graph_weight_descriptor())?;
15330                        model = Some(m.clone());
15331                        crate::gpu::GraphAttn::Full {
15332                            wq: gw(wq)?,
15333                            wk: gw(wk)?,
15334                            wv: gw(wv)?,
15335                            wo: gw(wo)?,
15336                            q_norm: q_norm.as_deref(),
15337                            k_norm: k_norm.as_deref(),
15338                            late_qk_norm: self.qk_norm_after_rope,
15339                            bias: bias
15340                                .as_ref()
15341                                .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
15342                            output_gate: *output_gate,
15343                            cpu_k: self.kv_cache.layers[li].k_heads(),
15344                            cpu_v: self.kv_cache.layers[li].v_heads(),
15345                            cpu_base: self.kv_cache.layers[li].base(),
15346                            geom: self.graph_attn_geom(li),
15347                            // The batched graph has no head-gate arm; a
15348                            // gated layer is refused above.
15349                            head_gate: None,
15350                        }
15351                    }
15352                    AttnKind::LinearGdn(w) => {
15353                        let Some(cfg) = self.gdn_cfg else {
15354                            if batch_debug {
15355                                eprintln!("batch graph: no GDN config at layer {li}");
15356                            }
15357                            return None;
15358                        };
15359                        let (m, _, _, _) = w
15360                            .in_proj_qkv
15361                            .graph_weight()
15362                            .or_else(|| w.in_proj_qkv.graph_weight_descriptor())?;
15363                        model = Some(m.clone());
15364                        crate::gpu::GraphAttn::Gdn {
15365                            qkv: gw(&w.in_proj_qkv)?,
15366                            z: gw(&w.in_proj_z)?,
15367                            a: gw(&w.in_proj_a)?,
15368                            b: gw(&w.in_proj_b)?,
15369                            out: gw(&w.out_proj)?,
15370                            conv1d: &w.conv1d,
15371                            a_log: &w.a_log,
15372                            dt_bias: &w.dt_bias,
15373                            norm: &w.norm,
15374                            nv: cfg.num_v_heads,
15375                            nk: cfg.num_k_heads,
15376                            dk: cfg.key_head_dim,
15377                            dv: cfg.value_head_dim,
15378                            kk: cfg.conv_kernel,
15379                            cpu_state: &self.kv_cache.layers[self.phys_layer(li)].linear_state,
15380                            preview: self.clover.as_ref().and_then(|c| c.graph_preview(li)),
15381                        }
15382                    }
15383                    _ => return None,
15384                };
15385                // Gemma-4's sandwich norms and layer scalar (`gemma_batch`);
15386                // every other model hands the graph none, as before.
15387                let g4 = self.global_attn.is_some() || self.attn_v_norm;
15388                layers.push(crate::gpu::GraphLayer {
15389                    input_norm: &lw.input_norm,
15390                    attn,
15391                    post_norm: &lw.post_norm,
15392                    attn_out_norm: if g4 {
15393                        lw.attn_out_norm.as_deref()
15394                    } else {
15395                        None
15396                    },
15397                    ffn_out_norm: if g4 { lw.ffn_out_norm.as_deref() } else { None },
15398                    layer_scale: if g4 { lw.layer_scale } else { None },
15399                    ffn: gffn,
15400                });
15401            }
15402            Some((layers, model?))
15403        })();
15404        let Some((layers, model)) = built else {
15405            {
15406                use std::sync::atomic::{AtomicBool, Ordering};
15407                static SAID: AtomicBool = AtomicBool::new(false);
15408                if !SAID.swap(true, Ordering::Relaxed) {
15409                    tracing::warn!("batch graph: BUILDER refused (layer weights/kinds)");
15410                }
15411            }
15412            return crate::gpu::BatchGraphOutcome::Declined;
15413        };
15414        if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
15415            eprintln!("batch-build: {:.1} ms", _tb.elapsed().as_secs_f64() * 1e3);
15416        }
15417        crate::gpu::forward_batch_graph(
15418            &model,
15419            self.graph_kv_id,
15420            &layers,
15421            &self.inv_freq,
15422            hiddens,
15423            nh,
15424            nkv,
15425            hd,
15426            rd,
15427            self.hidden_size,
15428            self.intermediate_size,
15429            positions,
15430            self.kv_cache.max_seq_len,
15431            gemma,
15432            self.rms_eps as f32,
15433            self.attn_scale,
15434            k,
15435            &(0..graph_end)
15436                .map(|li| self.kv_cache.layers[self.phys_layer(li)].o1_views())
15437                .collect::<Vec<_>>(),
15438            self.o1_epoch,
15439            spec,
15440            layers_run,
15441        )
15442    }
15443
15444    /// Same, stopping after layer `upto` inclusive (routing probe φ).
15445    /// `CMF_DSV4_DRAFT_PROBE=1` — grade the draft against what the trunk goes on
15446    /// to produce. Off by default; it runs a whole draft per decoded token.
15447    fn draft_probe() -> bool {
15448        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
15449        *ON.get_or_init(|| std::env::var("CMF_DSV4_DRAFT_PROBE").is_ok_and(|v| v != "0"))
15450    }
15451
15452    /// `CMF_DSV4_DRAFT_PROBE=1`: measure how much of the draft the trunk
15453    /// would have agreed with, WITHOUT verifying or rolling anything back.
15454    ///
15455    /// The number this produces decides the whole speculation design — at
15456    /// acceptance a, a block of B positions yields 1 + a + a² + ... tokens
15457    /// per trunk pass — so it is worth measuring before any of the machinery
15458    /// that would exploit it exists. Each draft is parked with the position
15459    /// it was made at, and graded as the real tokens arrive.
15460    /// `CMF_DSV4_SPEC=1` — the DeepSeek-V4 speculative decode: draft five
15461    /// on the card, verify them in one batched trunk pass, commit the
15462    /// accepted prefix, roll the rest back.
15463    #[cfg(feature = "gpu")]
15464    fn dsv4_spec_on() -> bool {
15465        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
15466        *ON.get_or_init(|| {
15467            // Test-only runtime gate: model loading still performs the same
15468            // reservation and trunk packing, which gives rollback parity a
15469            // topology-identical non-speculative control arm.
15470            if let Ok(v) = std::env::var("CMF_DSV4_SPEC_RUN") {
15471                return v != "0";
15472            }
15473            // An explicit value is a diagnostic force/escape hatch.  With no
15474            // knob, speculation is eligible only when model loading reserved
15475            // its bounded pack.  On small q4tp cards the geometric reserve
15476            // gate deliberately leaves this at zero: trying to build DSpark
15477            // after the exact trunk filled VRAM is both slower and a device
15478            // OOM (measured on A40).
15479            std::env::var("CMF_DSV4_SPEC")
15480                .map(|v| v != "0")
15481                .unwrap_or_else(|_| {
15482                    crate::gpu_wgpu::DRAFT_RESERVE.load(std::sync::atomic::Ordering::Relaxed) > 0
15483                })
15484        })
15485    }
15486
15487    /// One speculative round at the decode tip. `t_next` is the token the
15488    /// sampler just committed for `next_pos`. Returns the EXTRA accepted
15489    /// tokens (possibly none) and the new position, with `graph_logits`
15490    /// left holding the last accepted position's logits — exactly what the
15491    /// loop top expects. `None` means "speculate not this round": nothing
15492    /// was committed, the caller forwards normally.
15493    #[cfg(feature = "gpu")]
15494    fn dsv4_spec_step(
15495        &mut self,
15496        tip_token: u32,
15497        t_next: u32,
15498        next_pos: usize,
15499        max_extra: usize,
15500        drafted: &mut usize,
15501        accepted_ctr: &mut usize,
15502    ) -> Option<(Vec<u32>, usize)> {
15503        let t_all = std::time::Instant::now();
15504        if std::env::var("CMF_DSV4_SPEC_TIME").is_ok() {
15505            thread_local! {
15506                static LAST: std::cell::Cell<Option<std::time::Instant>> =
15507                    const { std::cell::Cell::new(None) };
15508            }
15509            LAST.with(|l| {
15510                if let Some(prev) = l.get() {
15511                    eprintln!(
15512                        "between rounds {:.1} ms",
15513                        prev.elapsed().as_secs_f64() * 1e3
15514                    );
15515                }
15516                l.set(Some(std::time::Instant::now()));
15517            });
15518        }
15519        if std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok() {
15520            eprintln!("spec_step: enter pos={next_pos}");
15521        }
15522        let n_layers = self.dsv4.as_ref().map(|b| b.1.len())?;
15523        let cfg = self.dsv4.as_ref().map(|b| b.2)?;
15524        // The draft state and its capture, armed exactly as the probe does.
15525        if self.dspark.is_none() {
15526            let t = crate::dsv4::dspark_targets(&self.dsv4_mtp, &cfg, n_layers);
15527            if t.is_empty() {
15528                return None;
15529            }
15530            crate::dsv4::dspark_arm(&t, cfg.dim);
15531            self.dspark = Some(crate::dsv4::DsparkState::new(
15532                self.dsv4_mtp.len(),
15533                &cfg,
15534                t.len(),
15535            ));
15536        }
15537        let targets = crate::dsv4::dspark_targets(&self.dsv4_mtp, &cfg, n_layers);
15538        let pack = crate::dsv4::dspark_pack_get(&self.dsv4_mtp, &cfg);
15539        if pack.is_none() && std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok() {
15540            eprintln!("spec_step: the pack was not built (targets {targets:?})");
15541        }
15542        let pack = pack?;
15543        let block = crate::dsv4::dspark_block();
15544        let b_box = self.dsv4.as_mut()?;
15545        let (g, layers, st) = (&b_box.0, &b_box.1, &mut b_box.3);
15546        let ds = self.dspark.as_mut()?;
15547        // The tip's captures: either this token ran on a normal path that
15548        // filled the thread-local, or the previous spec round left them.
15549        let dbg = std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok();
15550        if !crate::dsv4::dspark_take(&mut ds.main_hidden) && !ds.have_hidden {
15551            if dbg {
15552                eprintln!("spec_step: no capture");
15553            }
15554            return None;
15555        }
15556        ds.have_hidden = true;
15557        let tip_pos = next_pos.checked_sub(1)?;
15558        let draft_started = std::time::Instant::now();
15559        let mut conf = Vec::new();
15560        let props = crate::dsv4::dspark_draft_gpu(
15561            g,
15562            &self.dsv4_mtp,
15563            &cfg,
15564            ds,
15565            pack,
15566            st.kv_id,
15567            tip_token,
15568            tip_pos,
15569            self.pool.as_deref(),
15570            &mut conf,
15571        );
15572        self.dspark_draft_ns += draft_started.elapsed().as_nanos();
15573        *drafted += block;
15574        if props.is_empty() || props[0] != t_next {
15575            if dbg {
15576                eprintln!(
15577                    "spec_step: draft {} (props0={:?} t_next={t_next})",
15578                    if props.is_empty() {
15579                        "empty"
15580                    } else {
15581                        "miss"
15582                    },
15583                    props.first()
15584                );
15585            }
15586            return None;
15587        }
15588        // `fed[0]` is `t_next`, which the outer loop has already committed;
15589        // only `fed[1..]` become additional output tokens. Cap the verify
15590        // transaction itself to the caller's remaining output budget instead
15591        // of merely truncating the returned vector: otherwise the KV/state
15592        // would advance past `max_tokens` and a 64-token request could return
15593        // 66 tokens (and poison a reused session with two invisible steps).
15594        let mut k_verify = crate::dsv4::dspark_verify_k()
15595            .min(props.len())
15596            .min(max_extra.saturating_add(1));
15597        // Adaptive depth: positions the draft itself doubts are paid for on
15598        // every verify and delivered almost never (natural-text survival
15599        // [.67 .50 .29 .08 .04]). `CMF_DSPARK_CONF_MIN=p` trims the fed
15600        // prefix at the first proposal whose confidence drops below p; on
15601        // predictable text the confidences stay high and nothing changes.
15602        let conf_min = {
15603            static M: std::sync::OnceLock<f32> = std::sync::OnceLock::new();
15604            *M.get_or_init(|| {
15605                std::env::var("CMF_DSPARK_CONF_MIN")
15606                    .ok()
15607                    .and_then(|v| v.parse().ok())
15608                    .unwrap_or(0.0)
15609            })
15610        };
15611        if conf_min > 0.0 && conf.len() >= props.len() {
15612            let mut keep = 1usize;
15613            while keep < k_verify && conf.get(keep).copied().unwrap_or(0.0) >= conf_min {
15614                keep += 1;
15615            }
15616            k_verify = k_verify.min(keep.max(2));
15617        }
15618        if k_verify < 2 {
15619            return None;
15620        }
15621        let mut fed = Vec::with_capacity(k_verify);
15622        fed.push(t_next);
15623        fed.extend_from_slice(&props[1..k_verify]);
15624        let mut argmax = Vec::new();
15625        let mut logits_all = Vec::new();
15626        let mut walked = Vec::new();
15627        let txn = crate::dsv4::dsv4_verify_chunk(
15628            g,
15629            layers,
15630            &cfg,
15631            st,
15632            &fed,
15633            next_pos,
15634            &self.inv_freq,
15635            self.pool.as_deref(),
15636            &targets,
15637            &mut argmax,
15638            &mut logits_all,
15639            &mut walked,
15640        );
15641        if txn.is_none() && dbg {
15642            eprintln!("spec_step: verify declined");
15643        }
15644        let txn = txn?;
15645        let spec_gpu_end = txn.gpu_end;
15646        let b = fed.len();
15647        let mut accepted = 1usize;
15648        while accepted < b && fed[accepted] == argmax[accepted - 1] {
15649            accepted += 1;
15650        }
15651        // `CMF_DSV4_SPEC_FORCE_REJECT=1` — accept nothing beyond the known
15652        // token, every round: the pure rollback exerciser. The output must
15653        // stay byte-identical to the plain walk; anything else is a
15654        // transaction bug, isolated from the acceptance logic.
15655        if std::env::var("CMF_DSV4_SPEC_FORCE_REJECT").is_ok_and(|v| v != "0") {
15656            accepted = 1;
15657        }
15658        if std::env::var("CMF_DSV4_SPEC_TRACE").is_ok() {
15659            eprintln!("spec@{next_pos}: fed={fed:?} argmax={argmax:?} accepted={accepted}");
15660        }
15661        let t_fin = std::time::Instant::now();
15662        if !crate::dsv4::dsv4_spec_finish(
15663            g,
15664            layers,
15665            &cfg,
15666            st,
15667            txn,
15668            accepted,
15669            &fed,
15670            &self.inv_freq,
15671            self.pool.as_deref(),
15672        ) {
15673            tracing::warn!("dsv4: the speculative rollback failed — the state is suspect");
15674            return None;
15675        }
15676        if std::env::var("CMF_DSV4_SPEC_TIME").is_ok() {
15677            eprintln!(
15678                "finish(k={accepted}): {:.1} ms",
15679                t_fin.elapsed().as_secs_f64() * 1e3
15680            );
15681        }
15682        *accepted_ctr += accepted - 1;
15683        // Captures per accepted token: device targets photographed by the
15684        // batch, host targets from the verify's own walk. The last one
15685        // becomes the new tip's draft input; every one owes the ring an
15686        // entry for its position.
15687        let (hc, dim) = (cfg.hc_mult, cfg.dim);
15688        // Complete-chain layers are photographed by the fused submission;
15689        // partial device layers overwrite that slot after exact host cold-
15690        // expert correction.  Thus every target in the contiguous device
15691        // prefix has a valid per-token capture.
15692        let dev_caps: Vec<usize> = targets
15693            .iter()
15694            .copied()
15695            .filter(|&t| t < spec_gpu_end)
15696            .collect();
15697        let mut caps_all = vec![0.0f32; dev_caps.len() * b * hc * dim];
15698        if !crate::gpu_wgpu::dsv4_spec_cap_read_all(b, dev_caps.len(), hc * dim, &mut caps_all) {
15699            return None;
15700        }
15701        for t in 0..accepted {
15702            let tip = t + 1 == accepted;
15703            for (slot, &tl) in targets.iter().enumerate() {
15704                if let Some(di) = dev_caps.iter().position(|&d| d == tl) {
15705                    let lo = (di * b + t) * hc * dim;
15706                    crate::dsv4::dspark_capture(
15707                        &caps_all[lo..lo + hc * dim],
15708                        &cfg,
15709                        slot,
15710                        &mut ds.main_hidden,
15711                    );
15712                } else if tip
15713                    && crate::dsv4::dspark_peek_slot(slot, dim, {
15714                        let lo = slot * dim;
15715                        &mut ds.main_hidden[lo..lo + dim]
15716                    })
15717                {
15718                    // The tip's host-layer captures are the walk's own
15719                    // per-layer notes — exact. (The walk that ran last ended
15720                    // on exactly this token, on both the accept-all and the
15721                    // rollback path.)
15722                } else {
15723                    // Intermediate tokens: the post-tail state stands in for
15724                    // the per-layer capture on host targets below the last
15725                    // layer. Ring-entry quality only; the tip is exact.
15726                    crate::dsv4::dspark_capture(
15727                        &walked[t * hc * dim..(t + 1) * hc * dim],
15728                        &cfg,
15729                        slot,
15730                        &mut ds.main_hidden,
15731                    );
15732                }
15733            }
15734            crate::dsv4::dspark_ring_append(
15735                g,
15736                &self.dsv4_mtp,
15737                &cfg,
15738                ds,
15739                next_pos + t,
15740                self.pool.as_deref(),
15741            );
15742        }
15743        let row = logits_all[(accepted - 1) * cfg.vocab..accepted * cfg.vocab].to_vec();
15744        self.graph_logits = Some(row);
15745        // The speculative loop never runs the probe, so the trunk tally has
15746        // no other place to cycle. Armed only when someone asked for the
15747        // dump; the host tail is the only tallying path here, which is
15748        // precisely the population a partial pack would serve.
15749        if std::env::var("CMF_DSV4_TRUNK_PICK_DUMP").is_ok() {
15750            crate::dsv4::trunk_freq_note(&crate::dsv4::pick_tally_take());
15751            crate::dsv4::pick_tally_arm();
15752        }
15753        if std::env::var("CMF_DSV4_SPEC_TIME").is_ok() {
15754            eprintln!(
15755                "spec_step total {:.1} ms (k={accepted})",
15756                t_all.elapsed().as_secs_f64() * 1e3
15757            );
15758        }
15759        Some((fed[1..accepted].to_vec(), next_pos + accepted))
15760    }
15761
15762    fn dspark_probe(&mut self, position: usize, token_id: u32) {
15763        if self.dsv4_mtp.is_empty() || !Self::draft_probe() {
15764            return;
15765        }
15766        // What the trunk just routed to, for this token.
15767        let trunk_now = crate::dsv4::pick_tally_take();
15768        crate::dsv4::trunk_freq_note(&trunk_now);
15769        if !trunk_now.is_empty() {
15770            self.dspark_trunk_picks.push(trunk_now);
15771            let keep = crate::dsv4::dspark_block();
15772            if self.dspark_trunk_picks.len() > keep {
15773                self.dspark_trunk_picks.remove(0);
15774            }
15775        }
15776        // Grade whatever is waiting: the token just decoded sits at
15777        // `position`, so it answers the draft made at `position - 1 - i`.
15778        for p in std::mem::take(&mut self.dspark_pending) {
15779            let Some(i) = position.checked_sub(p.0 + 1) else {
15780                continue;
15781            };
15782            let mut p = p;
15783            if i < p.1.len() {
15784                if p.2 && p.1[i] == token_id {
15785                    p.3 = i + 1;
15786                } else {
15787                    p.2 = false;
15788                }
15789                if i + 1 < p.1.len() {
15790                    self.dspark_pending.push(p);
15791                    continue;
15792                }
15793            }
15794            self.dspark_hist.push(p.3);
15795            self.dspark_real.push(token_id);
15796        }
15797        let Some(b) = &mut self.dsv4 else { return };
15798        let (g, layers, cfg) = (&b.0, &b.1, b.2);
15799        let n_layers = layers.len();
15800        if self.dspark.is_none() {
15801            let t = crate::dsv4::dspark_targets(&self.dsv4_mtp, &cfg, n_layers);
15802            if t.is_empty() {
15803                return;
15804            }
15805            eprintln!(
15806                "DSpark: capture from layers {t:?}, block {}",
15807                crate::dsv4::dspark_block()
15808            );
15809            crate::dsv4::dspark_arm(&t, cfg.dim);
15810            self.dspark = Some(crate::dsv4::DsparkState::new(
15811                self.dsv4_mtp.len(),
15812                &cfg,
15813                t.len(),
15814            ));
15815        }
15816        let ds = self.dspark.as_mut().unwrap();
15817        if !crate::dsv4::dspark_take(&mut ds.main_hidden) {
15818            return; // this token ran on a path that captures nothing
15819        }
15820        let mut conf = Vec::new();
15821        crate::dsv4::pick_tally_arm();
15822        // The trunk has already consumed the adaptive VRAM budget. Until the
15823        // draft owns an explicit bounded device pack, its tensors are an
15824        // out-of-core CPU/disk tier by contract: never let per-op probes try
15825        // to squeeze another multi-gigabyte MTP expert cache onto the card.
15826        let draft_started = std::time::Instant::now();
15827        #[cfg(feature = "gpu")]
15828        let gpu_draft = crate::dsv4::dspark_gpu_on();
15829        #[cfg(not(feature = "gpu"))]
15830        let gpu_draft = false;
15831        let props = if gpu_draft {
15832            #[cfg(feature = "gpu")]
15833            {
15834                let kv_id = b.3.kv_id;
15835                match crate::dsv4::dspark_pack_get(&self.dsv4_mtp, &cfg) {
15836                    Some(pk) => crate::dsv4::dspark_draft_gpu(
15837                        g,
15838                        &self.dsv4_mtp,
15839                        &cfg,
15840                        ds,
15841                        pk,
15842                        kv_id,
15843                        token_id,
15844                        position,
15845                        self.pool.as_deref(),
15846                        &mut conf,
15847                    ),
15848                    None => Vec::new(),
15849                }
15850            }
15851            #[cfg(not(feature = "gpu"))]
15852            Vec::new()
15853        } else {
15854            crate::gpu::cpu_scope(|| {
15855                crate::dsv4::dspark_draft(
15856                    g,
15857                    &self.dsv4_mtp,
15858                    &cfg,
15859                    ds,
15860                    token_id,
15861                    position,
15862                    self.pool.as_deref(),
15863                    &mut conf,
15864                )
15865            })
15866        };
15867        self.dspark_draft_ns += draft_started.elapsed().as_nanos();
15868        let draft_picks = crate::dsv4::pick_tally_take();
15869        crate::dsv4::dspark_freq_note(&draft_picks);
15870        // Re-arm for the NEXT trunk token; the probe runs after the forward,
15871        // so this is the only place that can.
15872        crate::dsv4::pick_tally_arm();
15873        if !props.is_empty() {
15874            // Two ratios, side by side: what a batched verify over the trunk
15875            // would read against what it asks for, and the same for the
15876            // draft's three stages. Near 1.0 means a batch amortises nothing.
15877            let (tu, tt) = {
15878                let flat: Vec<(usize, Vec<usize>)> = self
15879                    .dspark_trunk_picks
15880                    .iter()
15881                    .flat_map(|v| v.iter().cloned())
15882                    .collect();
15883                // Per layer, across the window of tokens.
15884                let mut per: std::collections::HashMap<usize, Vec<usize>> =
15885                    std::collections::HashMap::new();
15886                for (li, picks) in flat {
15887                    per.entry(li).or_default().extend(picks);
15888                }
15889                let n = per.len().max(1);
15890                let mut u = 0usize;
15891                let mut t = 0usize;
15892                for (_, v) in per {
15893                    t += v.len();
15894                    u += v.iter().collect::<std::collections::HashSet<_>>().len();
15895                }
15896                (u / n, t / n)
15897            };
15898            let (du, dt) = crate::dsv4::tally_unique(&draft_picks);
15899            self.dspark_exp.push((tu, tt, du, dt));
15900            self.dspark_pending.push((position, props, true, 0));
15901        }
15902        if self.dspark_hist.len() >= 8 && self.dspark_hist.len() % 8 == 0 {
15903            let n = self.dspark_hist.len() as f32;
15904            let mean: f32 = self.dspark_hist.iter().sum::<usize>() as f32 / n;
15905            let block = crate::dsv4::dspark_block();
15906            let mut at = vec![0usize; block + 1];
15907            for &k in &self.dspark_hist {
15908                at[k] += 1;
15909            }
15910            // Prefix survival: S_i = P(the first i positions all held).
15911            let mut surv = Vec::with_capacity(block);
15912            for i in 1..=block {
15913                let k = at[i..].iter().sum::<usize>() as f32 / n;
15914                surv.push(format!("{k:.2}"));
15915            }
15916            let distinct = self
15917                .dspark_real
15918                .iter()
15919                .collect::<std::collections::HashSet<_>>()
15920                .len();
15921            let (tu, tt, du, dt) = self.dspark_exp.iter().fold((0, 0, 0, 0), |a, b| {
15922                (a.0 + b.0, a.1 + b.1, a.2 + b.2, a.3 + b.3)
15923            });
15924            let m = self.dspark_exp.len().max(1);
15925            eprintln!(
15926                "DSpark: {} drafts, {mean:.2} of {block} accepted on average \
15927                 (tokens per pass {:.2}), distribution {at:?}, survival [{}]",
15928                self.dspark_hist.len(),
15929                mean + 1.0,
15930                surv.join(" ")
15931            );
15932            eprintln!(
15933                "DSpark: {distinct} distinct tokens of {} (degeneracy), \
15934                 trunk experts {}/{} per layer over {block} tokens, \
15935                 draft {}/{} per block, draft {:.2} ms/block",
15936                self.dspark_real.len(),
15937                tu / m,
15938                tt / m,
15939                du / m,
15940                dt / m,
15941                self.dspark_draft_ns as f64 / self.dspark_exp.len().max(1) as f64 / 1e6
15942            );
15943        }
15944    }
15945
15946    fn forward_layers_upto(
15947        &mut self,
15948        hidden: &[f32],
15949        position: usize,
15950        task_mask: Option<&TaskMask>,
15951        upto: Option<usize>,
15952    ) -> Vec<f32> {
15953        // In-process multi-GPU: each segment runs pinned to its card,
15954        // and the only thing crossing the boundary is one hidden vector
15955        // that never leaves this address space. Same layer split the
15956        // network mode does, minus the second process, the socket, the
15957        // serialization and the dir_hash handshake.
15958        if let Some(plan) = self.gpu_plan.clone() {
15959            if upto.is_none() && plan.len() > 1 {
15960                let mut h = hidden.to_vec();
15961                for &(dev, from, upto_incl) in plan.iter() {
15962                    h = crate::gpu::with_device(dev, || {
15963                        self.forward_layers_span(&h, position, task_mask, from, Some(upto_incl))
15964                    });
15965                }
15966                return h;
15967            }
15968        }
15969        self.forward_layers_span(hidden, position, task_mask, 0, upto)
15970    }
15971
15972    /// Split this pipeline's layer stack across local GPUs: segment i
15973    /// runs on `devices[i]`. Contiguous and even by layer count — the
15974    /// VRAM-weighted planner is the next step, and an uneven card pair
15975    /// is why it will be needed. `None` clears the plan.
15976    pub fn set_gpu_plan(&mut self, devices: Option<&[usize]>) -> Result<(), String> {
15977        self.set_gpu_plan_at(devices, None)
15978    }
15979
15980    /// The same, with an explicit first boundary (`--peer-split`): card
15981    /// 0 takes layers `[0..at)`, the rest split what remains. Uneven
15982    /// cards, or an attention-heavy head, are why this knob exists.
15983    pub fn set_gpu_plan_at(
15984        &mut self,
15985        devices: Option<&[usize]>,
15986        at: Option<usize>,
15987    ) -> Result<(), String> {
15988        let Some(devs) = devices.filter(|d| d.len() > 1) else {
15989            self.gpu_plan = None;
15990            return Ok(());
15991        };
15992        self.split_supported()?;
15993        let n = self.num_layers;
15994        if devs.len() > n {
15995            return Err(format!("{} devices for {n} layers", devs.len()));
15996        }
15997        if let Some(k) = at {
15998            if k == 0 || k >= n {
15999                return Err(format!("split at {k}: the model has {n} layers"));
16000            }
16001            if devs.len() == 2 {
16002                self.gpu_plan = Some(std::sync::Arc::new(vec![
16003                    (devs[0], 0, k - 1),
16004                    (devs[1], k, n - 1),
16005                ]));
16006                return Ok(());
16007            }
16008            return Err(format!(
16009                "an explicit split point takes exactly 2 devices, got {}",
16010                devs.len()
16011            ));
16012        }
16013        let per = n.div_ceil(devs.len());
16014        let mut plan = Vec::with_capacity(devs.len());
16015        let mut from = 0usize;
16016        for &d in devs {
16017            if from >= n {
16018                break;
16019            }
16020            let upto = (from + per - 1).min(n - 1);
16021            plan.push((d, from, upto));
16022            from = upto + 1;
16023        }
16024        self.gpu_plan = Some(std::sync::Arc::new(plan));
16025        Ok(())
16026    }
16027
16028    /// The active in-process split, if any: (device, first layer, last).
16029    pub fn gpu_plan(&self) -> Option<Vec<(usize, usize, usize)>> {
16030        self.gpu_plan.as_ref().map(|p| p.as_ref().clone())
16031    }
16032
16033    /// Layer span [from ..= upto] (upto None = last layer): the building
16034    /// block the network pipeline-split rides on. `from > 0` skips the
16035    /// arch escape hatches (the pub `forward_span` refuses those archs
16036    /// first) and the whole-token graph — the plain per-layer loop is
16037    /// the canonical executor for a partial stack.
16038    fn embryo_qtensor_f32(t: &QTensor) -> Vec<f32> {
16039        if let Some(x) = t.as_f32() {
16040            return x.to_vec();
16041        }
16042        let mut out = vec![0.0; t.rows() * t.cols()];
16043        for r in 0..t.rows() {
16044            t.row_f32(r, &mut out[r * t.cols()..(r + 1) * t.cols()]);
16045        }
16046        out
16047    }
16048
16049    fn embryo_resident_eligible(&self) -> bool {
16050        // One mixer family per file: vmf_phase (kind 0/1) or
16051        // gated_delta_net (kind 4); the anchors are full (2) or bounded (3).
16052        if (self.vmf_cfg.is_none() && self.gdn_cfg.is_none())
16053            || self.num_layers != self.physical_layers
16054            || self.loop_final_norm
16055            || self.weights.layers.len() != self.num_layers
16056            || self.head_clusters.is_none()
16057            || self.final_softcap.is_some()
16058            || self.logit_multiplier.is_some()
16059            || self.attn_softcap != 0.0
16060            || self.mtp.is_some()
16061            || self.per_token_stack()
16062            || self.dsv4.is_some()
16063            || self.dsv41.is_some()
16064            || self.qwen4_exp.is_some()
16065            // Dynamic routing swaps FFN weights mid-sequence under the
16066            // packed graph; a blend has no single overlay. A STATIC skill
16067            // (`from_model_with_skill`) is fine: the pack reads the live
16068            // `weights.layers[*].ffn`, i.e. the skill's tensors, and a
16069            // later `set_active_skill` change drops the pack
16070            // (`invalidate_for_weight_change`).
16071            || self.dyn_router.is_some()
16072            || self.dyn_phi_layer.is_some()
16073            || self.dyn_blend_loaded
16074            || self.o1_cfg.is_some()
16075            || self.swa.is_some()
16076            || self.sliding_layers.is_some()
16077            || self.global_attn.is_some()
16078            || self.attention_heads_per_layer.is_some()
16079            || self.attn_v_norm
16080            || self
16081                .kv_cache
16082                .layers
16083                .iter()
16084                .any(|l| l.mode != crate::kv_cache::KvMode::F32)
16085            || self.rope_scale != 1.0
16086            || self.rope_scale_local != 1.0
16087            || self.attn_scale != 1.0 / (self.head_dim as f32).sqrt()
16088            || self.hidden_size == 0
16089            || self.hidden_size > 1024
16090            || self.intermediate_size > 1024
16091            || self.num_heads == 0
16092            || self.num_kv_heads == 0
16093            || self.num_heads % self.num_kv_heads != 0
16094            || self.num_heads.saturating_mul(self.head_dim) > 1024
16095            || self.num_kv_heads.saturating_mul(self.head_dim) > 1024
16096            || self.vocab_size == 0
16097            || self.kv_cache.max_seq_len == 0
16098            || self.rotary_dim == 0
16099            || self.rotary_dim > self.head_dim
16100            || self.rotary_dim % 2 != 0
16101            || self.inv_freq.len() < self.rotary_dim / 2
16102        {
16103            return false;
16104        }
16105        // The resident shader is deliberately an f32 profile.  Dequantizing
16106        // a Q4/Q8 tensor into the packed buffer would silently change the
16107        // operator relative to the CPU quantized path, so quantized CMFs
16108        // retain the exact ordinary executor instead of claiming parity.
16109        // Measured consequence (RTX PRO 4000, S4 bounded export requantized
16110        // with `cortiq requant --quant q4tp-quantize`): `eligible=false`,
16111        // the generic wgpu whole-token graph refuses too, and the per-op
16112        // path decodes at ~73 tok/s against ~200 tok/s on the CPU q4tp
16113        // path — a q4tp Embryo-O1 file is a CPU artifact today; the
16114        // resident graph serves the f32 export.
16115        if self.weights.lm_head.as_f32().is_none()
16116            || self.weights.embed_tokens.as_f32().is_none()
16117            || self.weights.lm_head.rows() < self.vocab_size
16118            || self.weights.lm_head.cols() != self.hidden_size
16119            || self.weights.embed_tokens.rows() < self.vocab_size
16120            || self.weights.embed_tokens.cols() != self.hidden_size
16121            || self.weights.final_norm.len() != self.hidden_size
16122        {
16123            return false;
16124        }
16125        if let Some(cfg) = self.vmf_cfg {
16126            if cfg.num_heads.saturating_mul(cfg.nphase) > 1024
16127                || cfg.num_heads.saturating_mul(cfg.nphase.saturating_add(1)) > 1024
16128                || cfg.num_heads.saturating_mul(cfg.value_head_dim) > 1024
16129                || cfg.state_len() == 0
16130            {
16131                return false;
16132            }
16133        }
16134        if let Some(g) = self.gdn_cfg {
16135            // The resident GDN kernels (gpu_wgpu.rs `embryo_core_gdn_*`):
16136            // fused projection ≤ 2048 rows, nv·dv ≤ 1024, dk ≤ 128 lanes,
16137            // dv ≤ 256 lanes in vec4 rows, SiLU output gate (the Embryo
16138            // export), same rms eps as the stack.
16139            if g.num_v_heads == 0
16140                || g.num_k_heads == 0
16141                || g.num_v_heads % g.num_k_heads != 0
16142                || g.key_head_dim == 0
16143                || g.key_head_dim > 128
16144                || g.value_head_dim == 0
16145                || g.value_head_dim > 256
16146                || g.value_head_dim % 4 != 0
16147                || g.conv_kernel == 0
16148                || g.num_v_heads > 512
16149                || g.num_v_heads.saturating_mul(g.value_head_dim) > 1024
16150                || g.conv_dim() > 2048
16151                || g.conv_dim() % 4 != 0
16152                || g.hidden_size != self.hidden_size
16153                || g.output_gate_sigmoid
16154                || g.rms_eps != self.rms_eps
16155                || g.state_len() == 0
16156            {
16157                return false;
16158            }
16159        }
16160        let mut full_seen = false;
16161        for lw in &self.weights.layers {
16162            if lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some() {
16163                return false;
16164            }
16165            match &lw.attn {
16166                AttnKind::LinearGdn(w) => {
16167                    let Some(g) = self.gdn_cfg else {
16168                        return false;
16169                    };
16170                    let (nv, dv, kk) = (g.num_v_heads, g.value_head_dim, g.conv_kernel);
16171                    if w.in_proj_qkv.rows() != g.conv_dim()
16172                        || w.in_proj_qkv.cols() != self.hidden_size
16173                        || w.in_proj_qkv.as_f32().is_none()
16174                        || w.in_proj_z.rows() != nv * dv
16175                        || w.in_proj_z.cols() != self.hidden_size
16176                        || w.in_proj_z.as_f32().is_none()
16177                        || w.in_proj_a.rows() != nv
16178                        || w.in_proj_a.cols() != self.hidden_size
16179                        || w.in_proj_a.as_f32().is_none()
16180                        || w.in_proj_b.rows() != nv
16181                        || w.in_proj_b.cols() != self.hidden_size
16182                        || w.in_proj_b.as_f32().is_none()
16183                        || w.conv1d.len() != g.conv_dim() * kk
16184                        || w.a_log.len() != nv
16185                        || w.dt_bias.len() != nv
16186                        || w.norm.len() != dv
16187                        || w.out_proj.rows() != self.hidden_size
16188                        || w.out_proj.cols() != nv * dv
16189                        || w.out_proj.as_f32().is_none()
16190                    {
16191                        return false;
16192                    }
16193                }
16194                AttnKind::Linear(w) => {
16195                    let Some(cfg) = self.vmf_cfg else {
16196                        return false;
16197                    };
16198                    if w.thq.rows() != cfg.num_heads * cfg.nphase
16199                        || w.thq.cols() != self.hidden_size
16200                        || w.thq.as_f32().is_none()
16201                        || w.thk.rows() != cfg.num_heads * cfg.nphase
16202                        || w.thk.cols() != self.hidden_size
16203                        || w.thk.as_f32().is_none()
16204                        || w.v_proj.rows() != cfg.num_heads * cfg.value_head_dim
16205                        || w.v_proj.cols() != self.hidden_size
16206                        || w.v_proj.as_f32().is_none()
16207                        || w.out_proj.rows() != self.hidden_size
16208                        || w.out_proj.cols() != cfg.num_heads * cfg.value_head_dim
16209                        || w.out_proj.as_f32().is_none()
16210                        || w.decay.len() != cfg.num_heads * 2 * cfg.nphase
16211                    {
16212                        return false;
16213                    }
16214                    if let Some((kg, kb)) = &w.k_gate {
16215                        if kg.rows() != cfg.num_heads
16216                            || kg.cols() != self.hidden_size
16217                            || kg.as_f32().is_none()
16218                            || kb.len() != cfg.num_heads
16219                        {
16220                            return false;
16221                        }
16222                    }
16223                    if let Some(conv) = &w.conv {
16224                        if conv.len() % self.hidden_size != 0 || conv.len() / self.hidden_size < 2 {
16225                            return false;
16226                        }
16227                    }
16228                }
16229                AttnKind::Full {
16230                    wq,
16231                    wk,
16232                    wv,
16233                    wo,
16234                    q_norm,
16235                    k_norm,
16236                    output_gate,
16237                    softplus_gate,
16238                    bias,
16239                } => {
16240                    if full_seen
16241                        || q_norm.is_some()
16242                        || k_norm.is_some()
16243                        || *output_gate
16244                        || softplus_gate.is_some()
16245                        || bias.is_some()
16246                        || wq.as_f32().is_none()
16247                        || wk.as_f32().is_none()
16248                        || wv.as_f32().is_none()
16249                        || wo.as_f32().is_none()
16250                        || wq.rows() != self.num_heads * self.head_dim
16251                        || wk.rows() != self.num_kv_heads * self.head_dim
16252                        || wv.rows() != self.num_kv_heads * self.head_dim
16253                        || wq.cols() != self.hidden_size
16254                        || wk.cols() != self.hidden_size
16255                        || wv.cols() != self.hidden_size
16256                        || wo.rows() != self.hidden_size
16257                        || wo.cols() != self.num_heads * self.head_dim
16258                    {
16259                        return false;
16260                    }
16261                    full_seen = true;
16262                }
16263                AttnKind::Bounded(w) => {
16264                    // The resident bounded attend scores S + W lanes in one
16265                    // 256-lane chunk; the format caps S + W at 160.
16266                    let Some(ac) = self.anchor_core.as_ref() else {
16267                        return false;
16268                    };
16269                    if self.bounded_rope.is_none()
16270                        || w.window != ac.window
16271                        || w.sink != ac.sink
16272                        || w.window == 0
16273                        || w.window + w.sink > 256
16274                        || w.sink_k.len() != self.num_kv_heads * w.sink * self.head_dim
16275                        || w.sink_v.len() != self.num_kv_heads * w.sink * self.head_dim
16276                        || w.wq.as_f32().is_none()
16277                        || w.wk.as_f32().is_none()
16278                        || w.wv.as_f32().is_none()
16279                        || w.wo.as_f32().is_none()
16280                        || w.wq.rows() != self.num_heads * self.head_dim
16281                        || w.wk.rows() != self.num_kv_heads * self.head_dim
16282                        || w.wv.rows() != self.num_kv_heads * self.head_dim
16283                        || w.wq.cols() != self.hidden_size
16284                        || w.wk.cols() != self.hidden_size
16285                        || w.wv.cols() != self.hidden_size
16286                        || w.wo.rows() != self.hidden_size
16287                        || w.wo.cols() != self.num_heads * self.head_dim
16288                    {
16289                        return false;
16290                    }
16291                }
16292                _ => return false,
16293            }
16294            match &lw.ffn {
16295                FfnKind::Dense(d) => {
16296                    if d.act != Act::Silu
16297                        || !d.segs.is_empty()
16298                        || d.gate_proj.as_f32().is_none()
16299                        || d.up_proj.as_f32().is_none()
16300                        || d.down_proj.as_f32().is_none()
16301                        || d.gate_proj.rows() != self.intermediate_size
16302                        || d.gate_proj.cols() != self.hidden_size
16303                        || d.up_proj.rows() != self.intermediate_size
16304                        || d.up_proj.cols() != self.hidden_size
16305                        || d.down_proj.rows() != self.hidden_size
16306                        || d.down_proj.cols() != self.intermediate_size
16307                    {
16308                        return false;
16309                    }
16310                }
16311                FfnKind::Moe(m) => {
16312                    if m.resonance.is_none()
16313                        || m.top_k != 1
16314                        || m.router_sigmoid
16315                        || !m.norm_topk_prob
16316                        || m.expert_bias.is_some()
16317                        || m.routed_scaling != 1.0
16318                        || m.route_tau.is_some()
16319                        || m.shared.is_none()
16320                        || m.mask.is_some()
16321                        || m.per_expert_scale.is_some()
16322                        || m.router_input_norm
16323                        || m.experts.is_empty()
16324                        || m.experts.len() > 8
16325                    {
16326                        return false;
16327                    }
16328                    let r = m.resonance.as_ref().unwrap();
16329                    if r.mu.len() != m.experts.len() * self.hidden_size
16330                        || r.bias.len() != m.experts.len()
16331                        || r.u.len() != m.experts.len() * r.k * self.hidden_size
16332                        || r.k > 128
16333                    {
16334                        return false;
16335                    }
16336                    let Some((shared, gate)) = &m.shared else {
16337                        return false;
16338                    };
16339                    if gate.is_some() || shared.act != Act::Silu || !shared.segs.is_empty() {
16340                        return false;
16341                    }
16342                    if shared.gate_proj.as_f32().is_none()
16343                        || shared.up_proj.as_f32().is_none()
16344                        || shared.down_proj.as_f32().is_none()
16345                        || shared.gate_proj.rows() != self.intermediate_size
16346                        || shared.gate_proj.cols() != self.hidden_size
16347                        || shared.up_proj.rows() != self.intermediate_size
16348                        || shared.up_proj.cols() != self.hidden_size
16349                        || shared.down_proj.rows() != self.hidden_size
16350                        || shared.down_proj.cols() != self.intermediate_size
16351                    {
16352                        return false;
16353                    }
16354                    for e in &m.experts {
16355                        if e.act != Act::Silu
16356                            || !e.segs.is_empty()
16357                            || e.gate_proj.as_f32().is_none()
16358                            || e.up_proj.as_f32().is_none()
16359                            || e.down_proj.as_f32().is_none()
16360                            || e.gate_proj.rows() != self.intermediate_size
16361                            || e.gate_proj.cols() != self.hidden_size
16362                            || e.up_proj.rows() != self.intermediate_size
16363                            || e.up_proj.cols() != self.hidden_size
16364                            || e.down_proj.rows() != self.hidden_size
16365                            || e.down_proj.cols() != self.intermediate_size
16366                        {
16367                            return false;
16368                        }
16369                    }
16370                }
16371                FfnKind::DenseMoe(_) => return false,
16372            }
16373            if lw.input_norm.len() != self.hidden_size || lw.post_norm.len() != self.hidden_size {
16374                return false;
16375            }
16376        }
16377        if full_seen && self.anchor_core.is_some() {
16378            return false;
16379        }
16380        full_seen || self.num_layers > 0
16381    }
16382
16383    /// The resident Embryo graph is the owner of this pipeline's forward:
16384    /// the same gate `forward_layers_span` applies before handing a token
16385    /// to `forward_embryo_graph` (both graph phases on, the explicit
16386    /// opt-in, a wgpu device, no earlier refusal, an eligible stack).
16387    fn embryo_resident_wanted(&self) -> bool {
16388        crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode)
16389            && crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill)
16390            && matches!(
16391                std::env::var("CMF_EMBRYO_RESIDENT").as_deref(),
16392                Ok("1") | Ok("parallel")
16393            )
16394            && crate::gpu::enabled_here()
16395            && !self.graph_refused()
16396            && self.embryo_resident_eligible()
16397    }
16398
16399    /// Chunked prefill on the resident graph: `ids` from `start` in
16400    /// chunks of `EMBRYO_CHUNK_MAX`, one submit each, projections/FFN as
16401    /// chunk GEMMs and the recurrent layers walked in time on the device.
16402    /// Returns the last position's logits when the whole span ran there.
16403    /// `None` = refused before any device work (the per-position path
16404    /// takes the span).  `CMF_EMBRYO_CHUNK=0` keeps the per-position
16405    /// prefill (A/B and the parity reference).
16406    fn embryo_prefill_chunked(&mut self, ids: &[u32], start: usize) -> Option<Vec<f32>> {
16407        if ids.len() < 2
16408            || std::env::var("CMF_EMBRYO_CHUNK").as_deref() == Ok("0")
16409            || !self.embryo_resident_wanted()
16410        {
16411            return None;
16412        }
16413        let model = self.ensure_embryo_graph()?;
16414        let cmax = std::env::var("CMF_EMBRYO_CHUNK")
16415            .ok()
16416            .and_then(|v| v.parse::<usize>().ok())
16417            .filter(|&v| v >= 1)
16418            .unwrap_or(crate::gpu::EMBRYO_CHUNK_MAX)
16419            .min(crate::gpu::EMBRYO_CHUNK_MAX);
16420        let hs = self.hidden_size;
16421        let n = ids.len();
16422        let mut pos = start;
16423        let mut last = None;
16424        let mut rows = Vec::with_capacity(cmax * hs);
16425        while pos < n {
16426            let end = (pos + cmax).min(n);
16427            rows.clear();
16428            for &id in &ids[pos..end] {
16429                rows.extend_from_slice(&self.embed_single(id));
16430            }
16431            let mut lg = Vec::new();
16432            if !crate::gpu::forward_embryo_graph_chunk(
16433                &model,
16434                self.graph_kv_id,
16435                &rows,
16436                pos,
16437                end - pos,
16438                &mut lg,
16439            ) {
16440                if pos == start {
16441                    if std::env::var("CMF_EMBRYO_DBG").as_deref() == Ok("1") {
16442                        eprintln!("embryo-dbg: chunk prefill refused at position {pos}");
16443                    }
16444                    return None;
16445                }
16446                // The device sequence advanced through the earlier chunks;
16447                // a host continuation would mix two owners of the state.
16448                // Fail this sequence alone, leaving no stale state or key.
16449                self.kv_cache.clear();
16450                self.clear_history();
16451                crate::gpu::graph_kv_reset(self.graph_kv_id);
16452                panic!(
16453                    "resident Embryo chunk prefill refused at position {pos}; refusing an in-flight host fallback"
16454                );
16455            }
16456            last = Some(lg);
16457            pos = end;
16458        }
16459        if std::env::var("CMF_EMBRYO_DBG").as_deref() == Ok("1") {
16460            eprintln!(
16461                "embryo-dbg: chunk prefill {} ids from {start} in {} submits (chunk {cmax})",
16462                n - start,
16463                (n - start).div_ceil(cmax)
16464            );
16465        }
16466        last
16467    }
16468
16469    fn ensure_embryo_graph(&mut self) -> Option<std::sync::Arc<crate::gpu::EmbryoGraphModel>> {
16470        if self.embryo_graph.is_none() && self.embryo_resident_eligible() {
16471            const UMAX: u32 = u32::MAX;
16472            const HEADER: usize = crate::gpu::EMBRYO_META_HEADER;
16473            const REC: usize = 64;
16474            struct Pack {
16475                data: Vec<f32>,
16476            }
16477            impl Pack {
16478                fn put(&mut self, x: &[f32]) -> u32 {
16479                    if x.is_empty() {
16480                        return u32::MAX;
16481                    }
16482                    let off = self.data.len();
16483                    self.data.extend_from_slice(x);
16484                    off as u32
16485                }
16486            }
16487            // The mixer family of the file: vmf_phase geometry fills the
16488            // phase header words, gated_delta_net fills words 24..29.  A
16489            // file has exactly one linear core, so at most one is live.
16490            let vmf = self.vmf_cfg;
16491            let gdn = self.gdn_cfg;
16492            let mut pack = Pack { data: Vec::new() };
16493            let mut meta = vec![0u32; HEADER];
16494            meta[0] = self.hidden_size as u32;
16495            meta[1] = self.intermediate_size as u32;
16496            meta[2] = self.vocab_size as u32;
16497            meta[3] = self.num_layers as u32;
16498            meta[4] = vmf.map(|c| c.num_heads).unwrap_or(0) as u32;
16499            meta[5] = vmf.map(|c| c.nphase).unwrap_or(0) as u32;
16500            meta[6] = vmf.map(|c| c.value_head_dim).unwrap_or(0) as u32;
16501            if let Some(g) = gdn {
16502                meta[24] = g.num_v_heads as u32;
16503                meta[25] = g.num_k_heads as u32;
16504                meta[26] = g.key_head_dim as u32;
16505                meta[27] = g.value_head_dim as u32;
16506                meta[28] = g.conv_kernel as u32;
16507                meta[29] = g.conv_dim() as u32;
16508            }
16509            meta[7] = self.num_heads as u32;
16510            meta[8] = self.num_kv_heads as u32;
16511            meta[9] = self.head_dim as u32;
16512            meta[10] = self.kv_cache.max_seq_len as u32;
16513            let clusters = self.head_clusters.as_ref().unwrap();
16514            let cluster_count = clusters.len() / self.hidden_size;
16515            if clusters.len() % self.hidden_size != 0
16516                || cluster_count == 0
16517                || cluster_count > 1024
16518                || self.vocab_size % cluster_count != 0
16519                || self.weights.lm_head.rows() < self.vocab_size
16520                || self.weights.final_norm.len() != self.hidden_size
16521            {
16522                return None;
16523            }
16524            meta[11] = cluster_count as u32;
16525            meta[12] = (self.vocab_size / cluster_count) as u32;
16526            meta[13] = vmf.map(|c| c.state_len()).unwrap_or(0) as u32;
16527            meta[16] = self.rotary_dim as u32;
16528            meta[17] = matches!(self.norm_style, NormStyle::Gemma) as u32;
16529            meta[19] = (self.rms_eps as f32).to_bits();
16530            let max_conv = self
16531                .weights
16532                .layers
16533                .iter()
16534                .filter_map(|lw| match &lw.attn {
16535                    AttnKind::Linear(w) => w.conv.as_ref().map(|v| v.len() / self.hidden_size),
16536                    _ => None,
16537                })
16538                .max()
16539                .unwrap_or(1);
16540            // One state slot per recurrent layer: the phase state plus its
16541            // hidden-wide conv ring, or the GDN record `[conv ring | S]`
16542            // (`GdnCfg::state_len`).  A file carries one mixer family, so
16543            // the stride is exactly that family's record and the device
16544            // state buffer equals the header's recurrent bytes.
16545            let phase_stride = vmf
16546                .map(|c| c.state_len() + max_conv.saturating_sub(1) * self.hidden_size)
16547                .unwrap_or(0);
16548            let gdn_stride = gdn.map(|g| g.state_len()).unwrap_or(0);
16549            let state_stride = phase_stride.max(gdn_stride);
16550            // Bounded genome: the KV plane of an anchor is its ring
16551            // `[kvh][W][hd]` K + V, and only anchors own one.  Legacy full
16552            // anchors keep the `max_seq` planes indexed by layer.
16553            let bounded = self.anchor_core.clone();
16554            let (anchor_window, anchor_sink) = bounded
16555                .as_ref()
16556                .map(|ac| (ac.window, ac.sink))
16557                .unwrap_or((0, 0));
16558            let kv_stride = if bounded.is_some() {
16559                2usize
16560                    .saturating_mul(self.num_kv_heads)
16561                    .saturating_mul(anchor_window)
16562                    .saturating_mul(self.head_dim)
16563            } else {
16564                2usize
16565                    .saturating_mul(self.num_kv_heads)
16566                    .saturating_mul(self.kv_cache.max_seq_len)
16567                    .saturating_mul(self.head_dim)
16568            };
16569            meta[14] = state_stride as u32;
16570            meta[15] = kv_stride as u32;
16571            meta[18] = anchor_window as u32;
16572            meta[20] = anchor_sink as u32;
16573            meta[21] = match &self.bounded_rope {
16574                Some(rope) => {
16575                    // [W][half] cos then [W][half] sin, one contiguous table.
16576                    let off = pack.put(&rope.cos);
16577                    let _ = pack.put(&rope.sin);
16578                    off
16579                }
16580                None => UMAX,
16581            };
16582            let mut full_seen = false;
16583            let mut bounded_seen = 0usize;
16584            // Recurrent state slots belong to mixer layers only (phase or
16585            // GDN): an anchor owns a ring, not a state stride, so the
16586            // device state buffer is exactly the header's recurrent bytes.
16587            let mut phase_seen = 0usize;
16588            let mut gdn_seen = 0usize;
16589            for (li, lw) in self.weights.layers.iter().enumerate() {
16590                let base = meta.len();
16591                meta.resize(base + REC, UMAX);
16592                meta[base] = match &lw.attn {
16593                    AttnKind::Linear(w) if w.phase_delta => 1,
16594                    AttnKind::Linear(_) => 0,
16595                    AttnKind::Full { .. } => 2,
16596                    AttnKind::Bounded(_) => 3,
16597                    AttnKind::LinearGdn(_) => 4,
16598                    _ => UMAX,
16599                };
16600                meta[base + 1] = pack.put(&lw.input_norm);
16601                meta[base + 2] = pack.put(&lw.post_norm);
16602                meta[base + 25] = match &lw.attn {
16603                    AttnKind::Linear(_) => {
16604                        let off = ((phase_seen + gdn_seen) * state_stride) as u32;
16605                        phase_seen += 1;
16606                        off
16607                    }
16608                    AttnKind::LinearGdn(_) => {
16609                        let off = ((phase_seen + gdn_seen) * state_stride) as u32;
16610                        gdn_seen += 1;
16611                        off
16612                    }
16613                    _ => UMAX,
16614                };
16615                match &lw.attn {
16616                    AttnKind::LinearGdn(w) => {
16617                        // Layer record words 56..63 + 29, as the resident
16618                        // kernels read them (gpu_wgpu.rs `embryo_core_gdn_*`).
16619                        meta[base + 56] = pack.put(&Self::embryo_qtensor_f32(&w.in_proj_qkv));
16620                        meta[base + 57] = pack.put(&Self::embryo_qtensor_f32(&w.in_proj_z));
16621                        meta[base + 58] = pack.put(&Self::embryo_qtensor_f32(&w.in_proj_a));
16622                        meta[base + 59] = pack.put(&Self::embryo_qtensor_f32(&w.in_proj_b));
16623                        meta[base + 60] = pack.put(&w.conv1d);
16624                        meta[base + 61] = pack.put(&w.a_log);
16625                        meta[base + 62] = pack.put(&w.dt_bias);
16626                        meta[base + 63] = pack.put(&w.norm);
16627                        meta[base + 29] = pack.put(&Self::embryo_qtensor_f32(&w.out_proj));
16628                        meta[base + 24] = 0;
16629                    }
16630                    AttnKind::Linear(w) => {
16631                        meta[base + 3] = pack.put(&Self::embryo_qtensor_f32(&w.thq));
16632                        meta[base + 4] = pack.put(&Self::embryo_qtensor_f32(&w.thk));
16633                        meta[base + 5] = pack.put(&Self::embryo_qtensor_f32(&w.v_proj));
16634                        meta[base + 6] = pack.put(&Self::embryo_qtensor_f32(&w.out_proj));
16635                        let decay: Vec<f32> = w.decay.iter().map(|&x| x as f32).collect();
16636                        meta[base + 7] = pack.put(&decay);
16637                        if let Some((kg, kb)) = &w.k_gate {
16638                            meta[base + 8] = pack.put(&Self::embryo_qtensor_f32(kg));
16639                            meta[base + 9] = pack.put(kb);
16640                        }
16641                        if let Some(conv) = &w.conv {
16642                            meta[base + 10] = pack.put(conv);
16643                            meta[base + 24] = (conv.len() / self.hidden_size) as u32;
16644                        } else {
16645                            meta[base + 24] = 0;
16646                        }
16647                    }
16648                    AttnKind::Full { wq, wk, wv, wo, .. } => {
16649                        full_seen = true;
16650                        meta[base + 11] = pack.put(&Self::embryo_qtensor_f32(wq));
16651                        meta[base + 12] = pack.put(&Self::embryo_qtensor_f32(wk));
16652                        meta[base + 13] = pack.put(&Self::embryo_qtensor_f32(wv));
16653                        meta[base + 14] = pack.put(&Self::embryo_qtensor_f32(wo));
16654                        meta[base + 26] = (li * kv_stride) as u32;
16655                    }
16656                    AttnKind::Bounded(w) => {
16657                        meta[base + 11] = pack.put(&Self::embryo_qtensor_f32(&w.wq));
16658                        meta[base + 12] = pack.put(&Self::embryo_qtensor_f32(&w.wk));
16659                        meta[base + 13] = pack.put(&Self::embryo_qtensor_f32(&w.wv));
16660                        meta[base + 14] = pack.put(&Self::embryo_qtensor_f32(&w.wo));
16661                        // Ring slot of this anchor (anchors only, packed).
16662                        meta[base + 26] = (bounded_seen * kv_stride) as u32;
16663                        meta[base + 27] = pack.put(&w.sink_k);
16664                        meta[base + 28] = pack.put(&w.sink_v);
16665                        bounded_seen += 1;
16666                    }
16667                    _ => return None,
16668                }
16669                match &lw.ffn {
16670                    FfnKind::Dense(d) => {
16671                        meta[base + 15] = 0;
16672                        meta[base + 16] = 0;
16673                        meta[base + 21] = pack.put(&Self::embryo_qtensor_f32(&d.gate_proj));
16674                        meta[base + 22] = pack.put(&Self::embryo_qtensor_f32(&d.up_proj));
16675                        meta[base + 23] = pack.put(&Self::embryo_qtensor_f32(&d.down_proj));
16676                    }
16677                    FfnKind::Moe(m) => {
16678                        let r = m.resonance.as_ref().unwrap();
16679                        let (shared, _) = m.shared.as_ref().unwrap();
16680                        meta[base + 15] = 1;
16681                        meta[base + 16] = m.experts.len() as u32;
16682                        meta[base + 17] = pack.put(&r.mu);
16683                        meta[base + 18] = pack.put(&r.u);
16684                        meta[base + 19] = pack.put(&r.bias);
16685                        meta[base + 20] = r.k as u32;
16686                        // Word 30: the growth shell as the runtime applies
16687                        // it now (`+inf` on trunk rows, the stored finite
16688                        // shell on grown rows, all `+inf` under
16689                        // `CMF_GROWTH_SHELL=off`), followed by one `−∞`
16690                        // sentinel at index E the kernel writes as the
16691                        // score of an expert outside its shell — WGSL has
16692                        // no infinity literal, so the value travels as
16693                        // data (`embryo_core_route_finalize`).
16694                        let mut shell = r.effective_shell(m.experts.len());
16695                        shell.push(f32::NEG_INFINITY);
16696                        meta[base + 30] = pack.put(&shell);
16697                        meta[base + 21] = pack.put(&Self::embryo_qtensor_f32(&shared.gate_proj));
16698                        meta[base + 22] = pack.put(&Self::embryo_qtensor_f32(&shared.up_proj));
16699                        meta[base + 23] = pack.put(&Self::embryo_qtensor_f32(&shared.down_proj));
16700                        for (e, ex) in m.experts.iter().enumerate() {
16701                            meta[base + 32 + e * 3] =
16702                                pack.put(&Self::embryo_qtensor_f32(&ex.gate_proj));
16703                            meta[base + 33 + e * 3] =
16704                                pack.put(&Self::embryo_qtensor_f32(&ex.up_proj));
16705                            meta[base + 34 + e * 3] =
16706                                pack.put(&Self::embryo_qtensor_f32(&ex.down_proj));
16707                        }
16708                    }
16709                    FfnKind::DenseMoe(_) => return None,
16710                }
16711            }
16712            if !full_seen && self.num_layers == 0 {
16713                return None;
16714            }
16715            let id = {
16716                static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
16717                NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
16718            };
16719            let model = crate::gpu::EmbryoGraphModel {
16720                id,
16721                hidden: self.hidden_size,
16722                intermediate: self.intermediate_size,
16723                vocab: self.vocab_size,
16724                layers: self.num_layers,
16725                phase_heads: vmf.map(|c| c.num_heads).unwrap_or(0),
16726                nphase: vmf.map(|c| c.nphase).unwrap_or(0),
16727                phase_dv: vmf.map(|c| c.value_head_dim).unwrap_or(0),
16728                anchor_q_heads: self.num_heads,
16729                anchor_kv_heads: self.num_kv_heads,
16730                anchor_head_dim: self.head_dim,
16731                rotary_dim: self.rotary_dim,
16732                max_seq: self.kv_cache.max_seq_len,
16733                cluster_count,
16734                cluster_size: self.vocab_size / cluster_count,
16735                phase_state_len: vmf.map(|c| c.state_len()).unwrap_or(0),
16736                state_stride,
16737                kv_stride,
16738                norm_gemma: matches!(self.norm_style, NormStyle::Gemma),
16739                phase_mass: vmf.map(|c| c.phase_mass).unwrap_or(0.0),
16740                weights: pack.data,
16741                meta,
16742                lm_head: Self::embryo_qtensor_f32(&self.weights.lm_head),
16743                clusters: clusters.as_ref().clone(),
16744                final_norm: self.weights.final_norm.clone(),
16745                inv_freq: self.inv_freq.as_ref().clone(),
16746                bounded: bounded.is_some(),
16747                kv_layers: if bounded.is_some() {
16748                    bounded_seen
16749                } else {
16750                    self.num_layers
16751                },
16752                state_layers: phase_seen + gdn_seen,
16753                anchor_window,
16754                anchor_sink,
16755                phase_layers: phase_seen,
16756                gdn_layers: gdn_seen,
16757                gdn_heads: gdn.map(|g| g.num_v_heads).unwrap_or(0),
16758                gdn_k_heads: gdn.map(|g| g.num_k_heads).unwrap_or(0),
16759                gdn_dk: gdn.map(|g| g.key_head_dim).unwrap_or(0),
16760                gdn_dv: gdn.map(|g| g.value_head_dim).unwrap_or(0),
16761                gdn_kk: gdn.map(|g| g.conv_kernel).unwrap_or(0),
16762            };
16763            self.embryo_graph = Some(std::sync::Arc::new(model));
16764        }
16765        self.embryo_graph.clone()
16766    }
16767
16768    fn forward_layers_span(
16769        &mut self,
16770        hidden: &[f32],
16771        position: usize,
16772        task_mask: Option<&TaskMask>,
16773        from: usize,
16774        upto: Option<usize>,
16775    ) -> Vec<f32> {
16776        #[cfg(target_os = "macos")]
16777        let _metal_pager_token_prof = crate::gpu_metal::MoePagerTokenScope::new(
16778            position,
16779            from,
16780            upto.map_or(self.num_layers, |u| u.saturating_add(1)),
16781        );
16782        debug_assert!(
16783            from == 0
16784                || (self.dsv4.is_none()
16785                    && self.dsv41.is_none()
16786                    && self.qwen4_exp.is_none()
16787                    && !self.per_token_stack())
16788        );
16789        // Every plain forward — the whole-token Metal graph (`q1_graph_gpu`
16790        // wraps the GDN owners zero-copy and reallocates them on a size
16791        // change) and the CPU layer loop (reads/swaps `linear_state`) —
16792        // must see the previous speculative commit's asynchronous replay
16793        // complete. One mutex probe when nothing is pending.
16794        #[cfg(target_os = "macos")]
16795        if !crate::gpu_metal::wait_replay() {
16796            self.fail_metal_graph("the pending async replay failed before a plain forward");
16797            return vec![0.0; self.hidden_size];
16798        }
16799        if let Some(b) = &mut self.qwen4_exp {
16800            let _ = (task_mask, upto);
16801            let token_id = hidden.first().copied().unwrap_or(0.0) as u32;
16802            let mut logits = Vec::new();
16803            crate::qwen4_exp::forward_token(
16804                &b.0,
16805                &b.1,
16806                &b.2,
16807                &mut b.3,
16808                token_id,
16809                position,
16810                &self.inv_freq,
16811                self.pool.as_deref(),
16812                &mut logits,
16813                true,
16814            );
16815            self.graph_logits = Some(logits);
16816            return vec![0.0; self.hidden_size];
16817        }
16818        // DeepSeek-V4 runs its own stack: the state is hc_mult copies, and
16819        // the forward returns LOGITS, not a hidden — the head is inside it
16820        // (the final fold sits between the last layer and the norm). The
16821        // token id rides in `hidden[0]`, written by embed_single, because
16822        // the hash layers route by id rather than by content.
16823        if let Some(b) = &mut self.dsv4 {
16824            let _ = (task_mask, upto);
16825            let token_id = hidden.first().copied().unwrap_or(0.0) as u32;
16826            let (g, layers, cfg, st) = (&b.0, &b.1, b.2, &mut b.3);
16827            st.pos = position;
16828            let mut logits = Vec::new();
16829            crate::dsv4::forward_token(
16830                g,
16831                layers,
16832                &cfg,
16833                st,
16834                token_id,
16835                &self.inv_freq,
16836                self.pool.as_deref(),
16837                &mut logits,
16838            );
16839            self.graph_logits = Some(logits);
16840            self.dspark_probe(position, token_id);
16841            // The caller expects a hidden; the logits went out of band, as
16842            // with the fused lm_head path.
16843            return vec![0.0; self.hidden_size];
16844        }
16845        // DeepSeek-V4.1 owns its complete stack and emits logits out of band.
16846        if let Some(b) = &mut self.dsv41 {
16847            let _ = (task_mask, upto);
16848            let token_id = hidden.first().copied().unwrap_or(0.0) as u32;
16849            let mut logits = Vec::new();
16850            crate::dsv41::forward_token(
16851                &b.0,
16852                &b.1,
16853                &b.2,
16854                &mut b.3,
16855                token_id,
16856                position,
16857                self.pool.as_deref(),
16858                &mut logits,
16859            );
16860            self.graph_logits = Some(logits);
16861            return vec![0.0; self.hidden_size];
16862        }
16863        // Gemma-3n runs its own stack (4 AltUp replicas don't fit this
16864        // loop); `hidden` is the extended embedding from embed_single.
16865        // Gemma-4 E-series: same hand-off — its dual-geometry attention
16866        // with shared-KV tail layers and the PLE injection live in
16867        // `g4e_forward`.
16868        if let Some(b) = &self.g4e {
16869            let _ = (task_mask, upto);
16870            return crate::g4e::g4e_forward(
16871                &b.0,
16872                &b.1,
16873                hidden,
16874                position,
16875                &mut self.kv_cache.layers,
16876                self.num_heads,
16877                self.num_kv_heads,
16878                self.pool.as_deref(),
16879            );
16880        }
16881        if let Some(b) = &self.g3n {
16882            let _ = (task_mask, upto);
16883            return crate::g3n::g3n_forward(
16884                &b.0,
16885                &b.1,
16886                hidden,
16887                position,
16888                &mut self.kv_cache.layers,
16889                self.num_heads,
16890                self.num_kv_heads,
16891                self.head_dim,
16892                self.pool.as_deref(),
16893            );
16894        }
16895        // Cortiq Embryo owns a separate resident graph: phase recurrent
16896        // state, resonance routing, the GQA anchor KV and hierarchical head
16897        // all execute in one Vulkan submit. It is limited to a complete
16898        // unmasked stack; spans and task masks retain the exact host path.
16899        // `CMF_EMBRYO_DBG=1` names the gate that keeps a token off the
16900        // resident graph — every refusal below is otherwise silent.
16901        if from == 0
16902            && upto.is_none()
16903            && task_mask.is_none()
16904            && self.anchor_core.is_some()
16905            && std::env::var("CMF_EMBRYO_DBG").as_deref() == Ok("1")
16906        {
16907            static ONCE: std::sync::Once = std::sync::Once::new();
16908            ONCE.call_once(|| {
16909                eprintln!(
16910                    "embryo-dbg: graph_on decode={} prefill={} resident_env={:?} enabled_here={} \
16911                     unsupported={} eligible={}",
16912                    crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode),
16913                    crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill),
16914                    std::env::var("CMF_EMBRYO_RESIDENT").ok(),
16915                    crate::gpu::enabled_here(),
16916                    self.graph_refused(),
16917                    self.embryo_resident_eligible(),
16918                );
16919            });
16920        }
16921        if from == 0
16922            && upto.is_none()
16923            && task_mask.is_none()
16924            // Embryo's recurrent/KV state has no host import path.  Do not
16925            // seed it for a prefill-only graph and then silently decode from
16926            // an empty CPU cache; both phases must select the resident owner.
16927            && crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode)
16928            && crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill)
16929            // This whole-token Embryo path remains explicitly opt-in.
16930            // `CMF_GPU_WGPU_GRAPH=1` still enables the mature generic graph,
16931            // but must not silently select this model-specific resident path.
16932            && matches!(
16933                std::env::var("CMF_EMBRYO_RESIDENT").as_deref(),
16934                Ok("1") | Ok("parallel")
16935            )
16936            && crate::gpu::enabled_here()
16937            && !self.graph_refused()
16938            // Sequence owner: past position zero the device continues only
16939            // a sequence it holds. A host-owned sequence (the graph refused
16940            // at its start, or its prefix was prefilled on the host) keeps
16941            // the host path to its end — never a device attempt at p > 0
16942            // over an empty device image.
16943            && (position == 0 || self.device_sequence_position().is_some())
16944            && self.embryo_resident_eligible()
16945            && let Some(model) = self.ensure_embryo_graph()
16946        {
16947            let mut lg = Vec::new();
16948            if crate::gpu::forward_embryo_graph(&model, self.graph_kv_id, hidden, position, &mut lg)
16949            {
16950                self.graph_logits = Some(lg);
16951                return vec![0.0; self.hidden_size];
16952            }
16953            if std::env::var("CMF_EMBRYO_DBG").as_deref() == Ok("1") {
16954                eprintln!("embryo-dbg: forward_embryo_graph refused at position {position}");
16955            }
16956            // The refusal is THIS pipeline's: falling through once is safe
16957            // at position zero (the host owns the sequence from here),
16958            // while a refusal at a later position of a device-owned
16959            // sequence would mix a host KV/state path with a partial
16960            // device sequence.
16961            self.mark_graph_refused();
16962            if position != 0 {
16963                // Fail this sequence alone and leave nothing stale behind:
16964                // no reuse key, no host or device state for the next
16965                // request on this slot to "extend".
16966                self.kv_cache.clear();
16967                self.clear_history();
16968                crate::gpu::graph_kv_reset(self.graph_kv_id);
16969                panic!(
16970                    "resident Embryo graph refused at position {position}; refusing an in-flight host fallback"
16971                );
16972            }
16973        }
16974        let mut h = hidden.to_vec();
16975        // MiMo-V2 expert placement: decided before the graph or the per-op
16976        // arena can claim the budget the expert bank needs.
16977        self.mimo_moe_prepare();
16978        let _mimo_q8 = self
16979            .mimo_moe
16980            .is_on()
16981            .then(crate::qtensor::enter_full_gpu_q8_scope);
16982        // Split borrows: copy scalars / clone handles so the per-layer
16983        // cfg does not hold `&self` while the KV cache is `&mut`.
16984        let (nh, _nkv, _hd, hs, _rd, eps) = (
16985            self.num_heads,
16986            self.num_kv_heads,
16987            self.head_dim,
16988            self.hidden_size,
16989            self.rotary_dim,
16990            self.rms_eps,
16991        );
16992        let pool = self.pool.clone();
16993        // Opt-in wgpu token-graph attention (discrete Vulkan/DX12): the whole
16994        // attention sub-block runs resident in one submit. Off by default.
16995        // Whole-token wgpu graph: eligibility + arbitration.
16996        //  - explicit CMF_GPU_WGPU_GRAPH forces it on/off;
16997        //  - discrete adapters (4090: decode 76 -> 137 tok/s) and GDN
16998        //    hybrids (recurrent state device-resident, no CPU twin to
16999        //    race) TRUST it;
17000        //  - integrated/mobile adapters RACE it against the normal path
17001        //    at generation granularity (gpu::graph_race_*) — tiled
17002        //    mobile GPUs can turn the ~300-dispatch graph into seconds
17003        //    per token, while a fast phone GPU keeps its win.
17004        let graph_env = std::env::var("CMF_GPU_WGPU_GRAPH").ok();
17005        let graph_on = match graph_env.as_deref() {
17006            Some("0") => false,
17007            Some("prefill") => false, // decode keeps the per-op path
17008            Some(_) => true,
17009            // Unset: same discrete-only default as every other graph
17010            // site. "Is the GPU on" used to stand in here — which made
17011            // the 0.2 tok/s whole-token graph race-eligible on mobile
17012            // adapters and cost 12-14× on first tokens (cmfmobile
17013            // TUNING.md); integrated GPUs keep the per-op probe path.
17014            None => crate::gpu::wgpu_graph_default(),
17015        };
17016        let graph_trusted =
17017            graph_env.is_some() || crate::gpu::wgpu_graph_default() || self.gdn_cfg.is_some();
17018        let race_eligible =
17019            graph_on && upto.is_none() && task_mask.is_none() && from == 0 && !self.graph_refused();
17020        let mut tail_start = 0usize;
17021        if race_eligible && crate::gpu::graph_race_use_graph(graph_trusted) {
17022            let t_graph = std::time::Instant::now();
17023            let mut lg = Vec::new();
17024            let mut gl = 0usize;
17025            let built = self.try_token_graph_wgpu(hidden, position, &mut lg, &mut gl);
17026            let declined = built.is_none();
17027            let built = match built {
17028                Some(Ok(hh)) => Some(hh),
17029                Some(Err(())) => {
17030                    // O(1) state was admitted before the device failure; the
17031                    // CPU mirrors are stale by construction.  Clear the whole
17032                    // sequence and stop rather than walking that stale state.
17033                    self.clear_sequence_state();
17034                    self.graph_failed
17035                        .store(true, std::sync::atomic::Ordering::Relaxed);
17036                    self.cancel
17037                        .store(true, std::sync::atomic::Ordering::Relaxed);
17038                    tracing::error!("token graph failed after admission; sequence state cleared");
17039                    return vec![0.0; self.hidden_size];
17040                }
17041                None => None,
17042            };
17043            // Past the transient guards (o1 still collecting, a softcap)
17044            // a refusal is about the weights and will never change —
17045            // remember it instead of walking every layer again next
17046            // token.
17047            if declined && !self.o1_active() && self.attn_softcap == 0.0 {
17048                self.mark_graph_refused();
17049            }
17050            graph_note(built.is_some(), gl, self.num_layers);
17051            if let Some(hh) = built {
17052                let dur = t_graph.elapsed();
17053                if std::env::var("CMF_GRAPH_PROF").is_ok() {
17054                    eprintln!("graph-call: {:.2} ms total", dur.as_secs_f64() * 1000.0);
17055                }
17056                if gl > 0 && gl < self.num_layers {
17057                    // Device prefix: the graph ran layers 0..gl and handed
17058                    // back the boundary hidden — the loop below owns the
17059                    // tail. The prefix layers' KV/state advanced on the
17060                    // device; the tail's advances on the host below. One
17061                    // boundary crossing per token.
17062                    h = hh;
17063                    tail_start = gl;
17064                } else if graph_trusted || !crate::gpu::graph_race_first_token_hopeless(dur) {
17065                    if !graph_trusted {
17066                        crate::gpu::graph_race_record(true, dur);
17067                    }
17068                    if !lg.is_empty() {
17069                        // Graph produced logits (final-norm + lm_head folded in) —
17070                        // pad/cap to vocab and hand them to the sampler directly.
17071                        lg.resize(self.vocab_size, 0.0);
17072                        if let Some(c) = self.final_softcap {
17073                            softcap_in_place(&mut lg, c, self.pool.as_deref());
17074                        }
17075                        self.graph_logits = Some(lg);
17076                    }
17077                    return hh;
17078                }
17079                // Hopeless first graph token: discard it and fall through
17080                // to the normal path. Safe exactly here — the prompt KV is
17081                // still CPU-owned (chunked prefill), so recomputing this
17082                // position is exact; the mirror's extra row is never read
17083                // (the race just settled on the normal path).
17084            }
17085        }
17086        // KIMI-LINEAR HAS NO SPLIT BUG. The 2.6× reported from the
17087        // model rotation (12.2 tok/s on one card against 4.6 on two)
17088        // was a single measurement of a model whose arm arbitration is
17089        // borderline, and it did not survive repetition. Three runs an
17090        // arm, same binary, back to back:
17091        //   probe on : 1 GPU 9.5 / 5.7 / 5.9   2 GPU 7.8 / 13.0 / 13.3
17092        //   pinned   : 1 GPU 5.6 / 5.3 / 5.2   2 GPU 3.5 / 4.2 / 3.4
17093        // With the arms pinned the split costs about 1.45×, which is
17094        // what a layer split costs. With the probe free, TWO CARDS RUN
17095        // FASTER — because for this model the CPU arm wins some op
17096        // classes and the probe finds that.
17097        //
17098        // Two things do stand, and both are measured. The token graph
17099        // builds NOTHING here (`covered 0 of 14 layers [0..14)`), so
17100        // every layer walks per-op on either arm — that is where the
17101        // headroom is, not in the split. And this model's benchmark is
17102        // unusable without `CMF_GPU_PROBE=0`: the arbitration alone
17103        // moves it by more than 2×.
17104        //
17105        // Span runs (network split): the graph covers exactly [from..=upto]
17106        // — one submit per SEGMENT per token. No race: its state is global
17107        // and calibrated on full stacks, so spans take the graph only where
17108        // it is trusted by default (discrete adapters / CMF_GPU_WGPU_GRAPH).
17109        let span = from > 0 || upto.is_some();
17110        if span && graph_on && task_mask.is_none() && graph_trusted {
17111            let upto_excl = upto.map_or(self.num_layers, |u| u + 1);
17112            let mut lg = Vec::new();
17113            let mut gl = 0usize;
17114            let span_res =
17115                self.try_token_graph_wgpu_span(hidden, position, &mut lg, from, upto_excl, &mut gl);
17116            let span_res = match span_res {
17117                Some(Ok(hh)) => Some(hh),
17118                Some(Err(())) => {
17119                    self.clear_sequence_state();
17120                    self.graph_failed
17121                        .store(true, std::sync::atomic::Ordering::Relaxed);
17122                    self.cancel
17123                        .store(true, std::sync::atomic::Ordering::Relaxed);
17124                    tracing::error!(
17125                        "span token graph failed after admission; sequence state cleared"
17126                    );
17127                    return vec![0.0; self.hidden_size];
17128                }
17129                None => None,
17130            };
17131            graph_note(span_res.is_some(), gl, upto_excl - from);
17132            if std::env::var("CMF_GPU_DEBUG").is_ok() {
17133                // How much of the span the graph actually covered. A
17134                // prefix of nothing means every layer walks per-op and
17135                // the split's extra cost is elsewhere.
17136                static SEEN: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
17137                if SEEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 {
17138                    eprintln!(
17139                        "span graph: covered {gl} of {} layers [{from}..{upto_excl}) res={}",
17140                        upto_excl - from,
17141                        span_res.is_some()
17142                    );
17143                }
17144            }
17145            if let Some(hh) = span_res {
17146                if gl == upto_excl - from {
17147                    if !lg.is_empty() {
17148                        lg.resize(self.vocab_size, 0.0);
17149                        if let Some(c) = self.final_softcap {
17150                            softcap_in_place(&mut lg, c, self.pool.as_deref());
17151                        }
17152                        self.graph_logits = Some(lg);
17153                    }
17154                    crate::gpu::set_layer(-1);
17155                    return hh;
17156                }
17157                // Partial device prefix of the span: CPU owns the tail.
17158                h = hh;
17159                tail_start = from + gl;
17160            }
17161        }
17162        // Layers the host is about to run whose device mirror moved ahead
17163        // of the host cache (a device prefix that shrank since the prompt,
17164        // a batched-prefill prefix longer than this token's): bring their
17165        // rows over first. One comparison per layer when nothing lags.
17166        let t_race_cpu = (race_eligible && !graph_trusted).then(std::time::Instant::now);
17167
17168        // A partial graph is an explicit GPU-prefix / CPU-tail split. Keep
17169        // the tail PURE host-side: letting its QTensor hooks re-enter the
17170        // residency arena streams every omitted layer through Vulkan and the
17171        // driver's freed-allocation cache can grow to the full model size
17172        // (25.4 GiB observed with a 14 GiB budget on Granite 30B Q8_2F).
17173        // With a MiMo expert bank the tail is not a whole-layer host
17174        // stream: its experts run from the bank (never the arena) and its
17175        // projections stay per-op on the device, which the bank's placement
17176        // left room for.
17177        let host_tail = tail_start > from;
17178        let _host_tail = (host_tail && !self.mimo_moe.is_on()).then(crate::gpu::enter_cpu_scope);
17179        let automatic_gpu_prefix = self.automatic_gpu_prefix();
17180
17181        let _prof_layers = crate::cpuprof::time(crate::cpuprof::Slot::Layers);
17182        #[cfg(target_os = "macos")]
17183        let mut gpu_skip_until = 0usize;
17184        for li in tail_start.max(from)..self.num_layers {
17185            let _capacity_tail = automatic_gpu_prefix
17186                .filter(|&prefix| li >= prefix && !self.mimo_moe.is_dynamic(li, host_tail))
17187                .map(|_| crate::gpu::enter_cpu_scope());
17188            crate::gpu::set_layer(li as i64); // layer-split GPU/CPU (CMF_GPU_LAYERS)
17189            if let Some(u) = upto {
17190                if li > u {
17191                    break;
17192                }
17193            }
17194            if let Some(mask) = task_mask {
17195                if !mask.layer_alive(li) {
17196                    continue; // dead layer: residual pass-through
17197                }
17198            }
17199            // MiniMax's dynamic tail: attention + pooled MoE of this layer as
17200            // ONE device frame (single-layer graph call), cold experts on
17201            // the host. Any decline falls through to the ordinary host
17202            // layer; a post-admission device failure stops the sequence.
17203            if host_tail && task_mask.is_none() {
17204                match self.m3_pooled_layer(li, position, &mut h) {
17205                    Ok(true) => {
17206                        crate::gpu::set_layer(-1);
17207                        continue;
17208                    }
17209                    Ok(false) => {}
17210                    Err(()) => {
17211                        self.clear_sequence_state();
17212                        self.graph_failed
17213                            .store(true, std::sync::atomic::Ordering::Relaxed);
17214                        self.cancel
17215                            .store(true, std::sync::atomic::Ordering::Relaxed);
17216                        tracing::error!(
17217                            "pooled tail token graph failed after admission; sequence state cleared"
17218                        );
17219                        return vec![0.0; self.hidden_size];
17220                    }
17221                }
17222            }
17223            // Whole-block q1 token graph: a run of consecutive q1
17224            // layers — GDN and full attention — executes with one sync
17225            // per CPU attend instead of per op (macOS/Metal).
17226            #[cfg(target_os = "macos")]
17227            {
17228                if li < gpu_skip_until {
17229                    continue;
17230                }
17231                if task_mask.is_none() {
17232                    let end = self.q1_graph_gpu(li, upto, position, &mut h);
17233                    if self.graph_failed.load(std::sync::atomic::Ordering::Relaxed) {
17234                        // The graph may have mutated device state before a
17235                        // command-buffer error. Never continue with a CPU
17236                        // tail or read a stale host mirror after admission.
17237                        return vec![0.0; self.hidden_size];
17238                    }
17239                    if end > li {
17240                        gpu_skip_until = end;
17241                        // Looped Transformer: the graph stopped at a loop
17242                        // boundary — apply final norm before the next iteration.
17243                        if self.is_loop_end(end - 1) && end < self.num_layers {
17244                            h = inference::rms_norm(
17245                                &h,
17246                                &self.weights.final_norm,
17247                                self.rms_eps,
17248                                self.norm_style,
17249                            );
17250                        }
17251                        continue;
17252                    }
17253                }
17254            }
17255
17256            if task_mask.is_none() {
17257                match self.mimo_graph_layer_rows(li, &mut h, &[position]) {
17258                    crate::gpu::BatchGraphOutcome::Completed => continue,
17259                    crate::gpu::BatchGraphOutcome::Failed => return vec![0.0; self.hidden_size],
17260                    crate::gpu::BatchGraphOutcome::Declined => {}
17261                }
17262            }
17263            #[cfg(feature = "gpu")]
17264            self.pull_lagging_host_kv(li, li + 1, position);
17265            let lw = &self.weights.layers[self.phys_layer(li)];
17266            if let Ok(tp) = std::env::var("CMF_TRACE_POS") {
17267                if tp.parse::<usize>().ok() == Some(position) {
17268                    let n: f32 = h.iter().map(|x| x * x).sum::<f32>().sqrt();
17269                    eprintln!(
17270                        "TRACE pos {position} layer {li}: |h| = {n:.6} h0 {:.6} h1 {:.6}",
17271                        h[0], h[1]
17272                    );
17273                }
17274            }
17275            // Norm into the pipeline scratch — the returning rms_norm
17276            // allocated twice per layer per token (roadmap §3 P0).
17277            let prof = crate::cpuprof::time(crate::cpuprof::Slot::Norms);
17278            inference::rms_norm_into(
17279                &h,
17280                &lw.input_norm,
17281                self.rms_eps,
17282                self.norm_style,
17283                &mut self.ws.n1,
17284            );
17285            drop(prof);
17286
17287            let attn_out = match &lw.attn {
17288                AttnKind::Mla(w) => {
17289                    let inv_freq_l = self.layer_inv_freq(li);
17290                    let rs = self.layer_rope_scale(li);
17291                    let eps = self.rms_eps;
17292                    let pool = self.pool.clone();
17293                    mla_attention(
17294                        w,
17295                        &self.ws.n1,
17296                        &mut self.kv_cache.layers[li],
17297                        position,
17298                        &inv_freq_l,
17299                        rs,
17300                        eps,
17301                        pool.as_deref(),
17302                    )
17303                }
17304                AttnKind::Linear(w) => {
17305                    let cfg = self.vmf_cfg.expect("linear layer without vmf_cfg");
17306                    vmf_phase_forward(
17307                        &self.ws.n1,
17308                        w,
17309                        &cfg,
17310                        &mut self.kv_cache.layers[li].linear_state,
17311                        self.pool.as_deref(),
17312                    )
17313                }
17314                AttnKind::Kda(w) => {
17315                    let cfg = self.kda_cfg.expect("kda layer without kda_cfg");
17316                    crate::linear_core::kda_forward(
17317                        &self.ws.n1,
17318                        w,
17319                        &cfg,
17320                        &mut self.kv_cache.layers[li].linear_state,
17321                        self.pool.as_deref(),
17322                    )
17323                }
17324                AttnKind::LinearGdn(w) => {
17325                    let cfg = self.gdn_cfg.expect("gdn layer without gdn_cfg");
17326                    gdn_forward(
17327                        &self.ws.n1,
17328                        w,
17329                        &cfg,
17330                        &mut self.kv_cache.layers[li].linear_state,
17331                        self.pool.as_deref(),
17332                    )
17333                }
17334                AttnKind::ShortConv(w) => {
17335                    let cfg = self
17336                        .short_conv_cfg
17337                        .expect("short-conv layer without short_conv_cfg");
17338                    short_conv_forward(
17339                        &self.ws.n1,
17340                        w,
17341                        &cfg,
17342                        &mut self.kv_cache.layers[li].linear_state,
17343                        self.pool.as_deref(),
17344                    )
17345                }
17346                AttnKind::Bounded(w) => {
17347                    // Natively bounded anchor: insert into the ring, attend
17348                    // over sinks ∪ window. No position, nothing appended.
17349                    let rope = self
17350                        .bounded_rope
17351                        .clone()
17352                        .expect("bounded layer without an installed rotation table");
17353                    let cfg = crate::bounded::BoundedAttnCfg {
17354                        num_heads: self.num_heads,
17355                        num_kv_heads: self.num_kv_heads,
17356                        head_dim: self.head_dim,
17357                        hidden_size: hs,
17358                        scale: self.attn_scale,
17359                        rope: &rope,
17360                        pool: pool.as_deref(),
17361                    };
17362                    crate::bounded::bounded_attention(
17363                        &self.ws.n1,
17364                        w,
17365                        &mut self.kv_cache.layers[li],
17366                        &cfg,
17367                    )
17368                }
17369                AttnKind::Full {
17370                    wq,
17371                    wk,
17372                    wv,
17373                    wo,
17374                    q_norm,
17375                    k_norm,
17376                    output_gate,
17377                    softplus_gate,
17378                    bias,
17379                } if self.kv_cache.layers[li].o1_sealed() => {
17380                    // O(1) override: decode on the sealed Nyström state
17381                    // instead of the growing KV cache.
17382                    let inv_freq_l = self.layer_inv_freq(li);
17383                    let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
17384                    let cfg = QwenAttnCfg {
17385                        num_heads: self.layer_num_heads(li),
17386                        num_kv_heads: nkv_l,
17387                        head_dim: hd_l,
17388                        hidden_size: hs,
17389                        position,
17390                        inv_freq: &inv_freq_l,
17391                        rotary_dim: rd_l,
17392                        scale: self.attn_scale,
17393                        softcap: self.attn_softcap,
17394                        window: None,
17395                        v_norm: self.attn_v_norm,
17396                        qk_norm_after_rope: self.qk_norm_after_rope,
17397                        gate_sigmoid: self.proj_gate_sigmoid,
17398                        q_norm: q_norm.as_deref(),
17399                        k_norm: k_norm.as_deref(),
17400                        output_gate: *output_gate,
17401                        softplus_gate: softplus_gate
17402                            .as_ref()
17403                            .map(|(gate, per_head)| (gate, *per_head)),
17404                        rope_scale: self.layer_rope_scale(li),
17405                        bias: bias
17406                            .as_ref()
17407                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
17408                        rms_eps: eps,
17409                        norm_style: self.norm_style,
17410                        pool: pool.as_deref(),
17411                        v_head_dim: self.layer_v_dim(li),
17412                    };
17413                    attention::qwen_attention_nystrom(
17414                        &self.ws.n1,
17415                        wq,
17416                        wk,
17417                        wv,
17418                        wo,
17419                        &mut self.kv_cache.layers[li],
17420                        &cfg,
17421                    )
17422                }
17423                AttnKind::Full {
17424                    wq,
17425                    wk,
17426                    wv,
17427                    wo,
17428                    q_norm,
17429                    k_norm,
17430                    output_gate,
17431                    softplus_gate,
17432                    bias,
17433                } => 'attn: {
17434                    // wgpu token-graph attention (opt-in): whole sub-block in
17435                    // one submit, device K/V mirror. q1 only, no gate/bias/mask.
17436                    // Its kernel has no window, sink or narrow-V slot and
17437                    // one mirror geometry: such models stay on the CPU attend.
17438                    let dropin_reason =
17439                        graph_on.then(|| self.graph_attn_decline_reason()).flatten();
17440                    if let Some(reason) = dropin_reason {
17441                        self.note_graph_decline("wgpu attn dropin", reason);
17442                    }
17443                    if graph_on
17444                        && dropin_reason.is_none()
17445                        && !*output_gate
17446                        && softplus_gate.is_none()
17447                        && self.attention_heads_per_layer.is_none()
17448                        && bias.is_none()
17449                        && task_mask.is_none()
17450                    {
17451                        let inv_freq_l = self.layer_inv_freq(li);
17452                        let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
17453                        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
17454                        if let (Some((gm, qi)), Some((_, ki)), Some((_, vi)), Some((_, oi))) = (
17455                            wq.mapped_q1(),
17456                            wk.mapped_q1(),
17457                            wv.mapped_q1(),
17458                            wo.mapped_q1(),
17459                        ) {
17460                            let gm = gm.clone();
17461                            let mut out = vec![0f32; hs];
17462                            let cache = &self.kv_cache.layers[li];
17463                            if crate::gpu::attn_dropin(
17464                                &gm,
17465                                self.graph_kv_id,
17466                                li,
17467                                &self.ws.n1,
17468                                qi,
17469                                ki,
17470                                vi,
17471                                oi,
17472                                q_norm.as_deref(),
17473                                k_norm.as_deref(),
17474                                self.qk_norm_after_rope,
17475                                &inv_freq_l,
17476                                nh,
17477                                nkv_l,
17478                                hd_l,
17479                                rd_l,
17480                                hs,
17481                                position,
17482                                self.kv_cache.max_seq_len,
17483                                gemma,
17484                                eps as f32,
17485                                cache.k_heads(),
17486                                cache.v_heads(),
17487                                &mut out,
17488                            ) {
17489                                break 'attn out;
17490                            }
17491                        }
17492                    }
17493                    let masked = task_mask
17494                        .map(|m| m.head_flags(li, self.num_heads).iter().any(|&a| !a))
17495                        .unwrap_or(false);
17496                    let f32_view = (wq.as_f32(), wk.as_f32(), wv.as_f32(), wo.as_f32());
17497                    // The masked kernel knows one pipeline-wide geometry and
17498                    // RoPE table, no window and no sink.
17499                    let plain = self.layer_attn_plain(li);
17500                    match (masked, f32_view) {
17501                        // Historical masked path (f32 slices; the loader
17502                        // keeps masked models in f32).
17503                        (true, (Some(q), Some(k), Some(v), Some(o))) if plain => {
17504                            let active_heads = task_mask.unwrap().head_flags(li, self.num_heads);
17505                            attention::multi_head_attention(
17506                                &self.ws.n1,
17507                                q,
17508                                k,
17509                                v,
17510                                o,
17511                                &mut self.kv_cache.layers[li],
17512                                self.num_heads,
17513                                self.num_kv_heads,
17514                                self.head_dim,
17515                                self.hidden_size,
17516                                position,
17517                                &active_heads,
17518                                &self.inv_freq,
17519                            )
17520                        }
17521                        (masked, _) => {
17522                            if masked {
17523                                tracing::warn!(
17524                                    "layer {li}: head mask on quantized weights or on a \
17525                                     window/sink/per-layer-geometry layer not supported \
17526                                     yet — executing dense"
17527                                );
17528                            }
17529                            let inv_freq_l = self.layer_inv_freq(li);
17530                            let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
17531                            let cfg = QwenAttnCfg {
17532                                num_heads: self.layer_num_heads(li),
17533                                num_kv_heads: nkv_l,
17534                                head_dim: hd_l,
17535                                hidden_size: hs,
17536                                position,
17537                                inv_freq: &inv_freq_l,
17538                                rotary_dim: rd_l,
17539                                scale: self.attn_scale,
17540                                softcap: self.attn_softcap,
17541                                window: self.layer_window(li),
17542                                v_norm: self.attn_v_norm,
17543                                qk_norm_after_rope: self.qk_norm_after_rope,
17544                                gate_sigmoid: self.proj_gate_sigmoid,
17545                                q_norm: q_norm.as_deref(),
17546                                k_norm: k_norm.as_deref(),
17547                                output_gate: *output_gate,
17548                                softplus_gate: softplus_gate
17549                                    .as_ref()
17550                                    .map(|(gate, per_head)| (gate, *per_head)),
17551                                rope_scale: self.layer_rope_scale(li),
17552                                bias: bias
17553                                    .as_ref()
17554                                    .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
17555                                rms_eps: eps,
17556                                norm_style: self.norm_style,
17557                                pool: pool.as_deref(),
17558                                v_head_dim: self.layer_v_dim(li),
17559                            };
17560                            attention::qwen_attention(
17561                                &self.ws.n1,
17562                                wq,
17563                                wk,
17564                                wv,
17565                                wo,
17566                                &mut self.kv_cache.layers[li],
17567                                &cfg,
17568                            )
17569                        }
17570                    }
17571                }
17572            };
17573            // Gemma sandwich norm: normalize the attention branch before
17574            // it joins the residual stream.
17575            let attn_out = match &self.weights.layers[self.phys_layer(li)].attn_out_norm {
17576                Some(w) => inference::rms_norm(&attn_out, w, self.rms_eps, self.norm_style),
17577                None => attn_out,
17578            };
17579            let lw = &self.weights.layers[self.phys_layer(li)];
17580            let prof = crate::cpuprof::time(crate::cpuprof::Slot::Norms);
17581            inference::add_rmsnorm_fused_into(
17582                &mut h,
17583                &attn_out,
17584                &lw.post_norm,
17585                self.rms_eps,
17586                self.norm_style,
17587                &mut self.ws.p1,
17588            );
17589            drop(prof);
17590            let mut attn_out = attn_out;
17591            attention::recycle_buf(&mut attn_out);
17592            let post_normed = &self.ws.p1;
17593
17594            let ffn_masked = task_mask
17595                .map(|m| m.ffn_active_count(li) < self.intermediate_size)
17596                .unwrap_or(false);
17597            // One masked dense CONTRACT, dispatched by cost. The
17598            // activation-zeroing arm (the batched sweep's, validated
17599            // against the replica to 0.8%) computes the FULL fused FFN
17600            // and zeroes the dead — right whenever most neurons live.
17601            // The sparse arm reads ONLY active rows and down columns —
17602            // per-row dots are slower per element than the fused kernel,
17603            // so it pays only once the mask is deep enough. The 0.5
17604            // crossover is first-principles (fused kernels run ~2x the
17605            // per-row dot throughput); a shallow specialist (95% alive)
17606            // stays fused, a --target-sparsity bake flips arms on its
17607            // own weight.
17608            let ffn_out = match (ffn_masked, &lw.ffn) {
17609                // A defragged tube layer answers its own mask: the core
17610                // always runs, each tube runs when its bit is on, and
17611                // the tubes that are off are never read from the mmap.
17612                (_, FfnKind::Dense(d)) if !d.segs.is_empty() => {
17613                    let row = task_mask
17614                        .and_then(|tm| tm.ffn_masks.get(li))
17615                        .map(|v| v.as_slice());
17616                    tube_ffn(d, post_normed, 1, self.pool.as_deref(), row)
17617                }
17618                (true, FfnKind::Dense(d)) => {
17619                    let tm = task_mask.unwrap();
17620                    let alive = tm.ffn_active_count(li);
17621                    let deep = alive * 2 <= self.intermediate_size;
17622                    if deep && d.down_proj.sparse_col_ok() && !d.gate_proj.has_prism_contract() {
17623                        let active = tm.ffn_active_indices(li);
17624                        sparse_ffn_quant(
17625                            d,
17626                            post_normed,
17627                            &active,
17628                            self.hidden_size,
17629                            self.pool.as_deref(),
17630                        )
17631                    } else if deep
17632                        && let (Some(g), Some(u), Some(dn)) = (
17633                            d.gate_proj.as_f32(),
17634                            d.up_proj.as_f32(),
17635                            d.down_proj.as_f32(),
17636                        )
17637                    {
17638                        let active = tm.ffn_active_indices(li);
17639                        inference::sparse_ffn_forward(
17640                            post_normed,
17641                            g,
17642                            u,
17643                            dn,
17644                            self.hidden_size,
17645                            self.intermediate_size,
17646                            &active,
17647                            self.pool.as_deref(),
17648                        )
17649                    } else {
17650                        let row = tm.ffn_masks.get(li).map(|v| v.as_slice());
17651                        dense_ffn_batch(d, post_normed, 1, self.pool.as_deref(), row)
17652                    }
17653                }
17654                (true, FfnKind::Moe(m)) => {
17655                    // MoE is sparse by expert selection; a task mask
17656                    // narrows the ROUTABLE set via its expert fields
17657                    // (spec §5) when it carries them.
17658                    let allowed = task_mask.and_then(|tm| tm.expert_flags(li, m.experts.len()));
17659                    ffn_forward(
17660                        &lw.ffn,
17661                        post_normed,
17662                        self.pool.as_deref(),
17663                        allowed.as_deref(),
17664                    )
17665                }
17666                (true, FfnKind::DenseMoe(dm)) => dense_moe_ffn(
17667                    dm,
17668                    post_normed,
17669                    &h,
17670                    self.rms_eps,
17671                    self.norm_style,
17672                    self.pool.as_deref(),
17673                ),
17674                (false, _) => match &lw.ffn {
17675                    FfnKind::DenseMoe(dm) => dense_moe_ffn(
17676                        dm,
17677                        post_normed,
17678                        &h,
17679                        self.rms_eps,
17680                        self.norm_style,
17681                        self.pool.as_deref(),
17682                    ),
17683                    FfnKind::Moe(m)
17684                        if task_mask.is_none()
17685                            && host_tail
17686                            && li >= tail_start
17687                            && crate::minicpm_tail::MiniCpmTail::is_requested_target(m) =>
17688                    {
17689                        moe_ffn_minicpm_tail(
17690                            &mut self.minicpm_tail,
17691                            li,
17692                            self.num_layers,
17693                            m,
17694                            post_normed,
17695                            self.pool.as_deref(),
17696                        )
17697                    }
17698                    FfnKind::Moe(m)
17699                        if task_mask.is_none() && self.mimo_moe.is_dynamic(li, host_tail) =>
17700                    {
17701                        moe_ffn_banked(&mut self.mimo_moe, li, m, post_normed, self.pool.as_deref())
17702                    }
17703                    _ => {
17704                        let allowed = match (&lw.ffn, task_mask) {
17705                            (FfnKind::Moe(m), Some(tm)) => tm.expert_flags(li, m.experts.len()),
17706                            _ => None,
17707                        };
17708                        ffn_forward(
17709                            &lw.ffn,
17710                            post_normed,
17711                            self.pool.as_deref(),
17712                            allowed.as_deref(),
17713                        )
17714                    }
17715                },
17716            };
17717            let ffn_out = match &self.weights.layers[self.phys_layer(li)].ffn_out_norm {
17718                Some(w) => inference::rms_norm(&ffn_out, w, self.rms_eps, self.norm_style),
17719                None => ffn_out,
17720            };
17721            for (i, &f) in ffn_out.iter().enumerate() {
17722                h[i] += f;
17723            }
17724            let mut ffn_out = ffn_out;
17725            attention::recycle_buf(&mut ffn_out);
17726
17727            // Gemma-4: the layer output is scaled by a learned scalar.
17728            if let Some(sc) = self.weights.layers[self.phys_layer(li)].layer_scale {
17729                for v in h.iter_mut() {
17730                    *v *= sc;
17731                }
17732            }
17733            // CMF_LAYER_DUMP: this position's hidden after layer li.
17734            if self.layer_dump.is_some() {
17735                self.dump_layer_row(position, li, &h);
17736            }
17737
17738            // Looped Transformer: apply final norm at the end of each loop iteration.
17739            // Nanbeige 4.2: after layer 21 (virtual), apply norm before looping back to layer 0.
17740            if self.is_loop_end(li) && li + 1 < self.num_layers {
17741                h = inference::rms_norm(
17742                    &h,
17743                    &self.weights.final_norm,
17744                    self.rms_eps,
17745                    self.norm_style,
17746                );
17747            }
17748
17749            // Dynamic routing φ capture (on-policy): the
17750            // EMA of the post-residual hidden at the router's phi_layer,
17751            // updated as the context evolves during decode.
17752            if self.dyn_phi_layer == Some(li) {
17753                self.update_dyn_phi(&h);
17754            }
17755        }
17756        crate::gpu::set_layer(-1); // layers done — lm_head outside layer-split
17757        if let Some(t) = t_race_cpu {
17758            crate::gpu::graph_race_record(false, t.elapsed());
17759        }
17760
17761        h
17762    }
17763
17764    /// EMA of φ at the router layer (rolling, weight 0.2 = ~5-token
17765    /// horizon). First observation seeds it exactly.
17766    fn update_dyn_phi(&mut self, h: &[f32]) {
17767        const A: f32 = 0.2;
17768        if self.dyn_phi_ema.len() != h.len() {
17769            self.dyn_phi_ema = vec![0.0; h.len()];
17770            self.dyn_phi_seen = 0;
17771        }
17772        if self.dyn_phi_seen == 0 {
17773            self.dyn_phi_ema.copy_from_slice(h);
17774        } else {
17775            for (e, &v) in self.dyn_phi_ema.iter_mut().zip(h) {
17776                *e = (1.0 - A) * *e + A * v;
17777            }
17778        }
17779        self.dyn_phi_seen += 1;
17780    }
17781
17782    /// Current router φ (EMA at phi_layer); empty until first capture.
17783    pub fn dyn_phi(&self) -> &[f32] {
17784        &self.dyn_phi_ema
17785    }
17786
17787    /// Enable/disable φ capture at the router layer, reset the EMA.
17788    pub fn set_dyn_phi_layer(&mut self, layer: Option<usize>) {
17789        self.dyn_phi_layer = layer;
17790        self.dyn_phi_ema.clear();
17791        self.dyn_phi_seen = 0;
17792    }
17793
17794    /// Skills eligible for dynamic switching: (index, id, phi_layer).
17795    pub fn dynamic_skills(&self) -> Vec<(usize, String, usize)> {
17796        let Some(model) = &self.model else {
17797            return Vec::new();
17798        };
17799        model
17800            .header
17801            .skills
17802            .iter()
17803            .enumerate()
17804            .filter_map(|(i, sk)| {
17805                // A v2 record routes only through the request-level
17806                // backbone-gated decision (its status and gate are
17807                // checked there), never per token.
17808                if sk.is_v2() {
17809                    return None;
17810                }
17811                let ok = matches!(self.dyn_skill_layers.get(i), Some(Some(_)));
17812                let sel = sk.selection.as_ref()?;
17813                (ok).then(|| (i, sk.id.clone(), sel.phi_layer))
17814            })
17815            .collect()
17816    }
17817
17818    /// Index of the currently overlaid skill (None = backbone).
17819    pub fn active_skill(&self) -> Option<usize> {
17820        self.dyn_active
17821    }
17822
17823    /// Enable dynamic per-token skill routing: build the hysteresis
17824    /// router from the container's routable skills, start φ capture at
17825    /// their (shared) phi_layer. Returns the number of routable skills
17826    /// (0 = nothing to route; router stays off). Idempotent.
17827    pub fn enable_dynamic_routing(&mut self) -> usize {
17828        use crate::swarm::{DynRouter, RoutableSkill};
17829        let Some(model) = self.model.clone() else {
17830            return 0;
17831        };
17832        // Router policy v2 (spec §9.4) routes per REQUEST: the backbone is
17833        // the default and only the backbone-gated decision may pick a
17834        // skill. A per-token switch would bypass that gate (and change
17835        // the O(1) state mid-sequence), so the hysteresis router never
17836        // runs on such a file; the caller keeps the request-level
17837        // decision.
17838        if let Some(r) = &model.header.router {
17839            tracing::warn!(
17840                "dynamic routing disabled: this file declares router policy '{}' with \
17841                 granularity \"{}\" — the request-level decision applies instead",
17842                r.policy,
17843                r.granularity
17844            );
17845            return 0;
17846        }
17847        // Format-v2 skill records (bit SKILLS_V2) without a router policy:
17848        // their status/gate contract ("auto-routing requires active +
17849        // measured") lives in the backbone-gated decision only — the
17850        // hysteresis router would switch into a quarantined record
17851        // (fail-open). Refuse the whole file, not just its v2 records.
17852        if model.required_features & cortiq_core::format::features::SKILLS_V2 != 0
17853            || model.header.skills.iter().any(|s| s.is_v2())
17854        {
17855            tracing::warn!(
17856                "dynamic routing disabled: this file carries format-v2 skill records \
17857                 (SKILLS_V2) — they route per request through a router policy only"
17858            );
17859            return 0;
17860        }
17861        // A blend materialized f32 working tensors into the layers; there
17862        // is no single skill index to revert from → refuse (honest).
17863        if self.dyn_blend_loaded {
17864            tracing::warn!("dynamic routing unavailable on a blend-loaded pipeline");
17865            return 0;
17866        }
17867        // A statically-overlaid skill that is NOT FFN-eligible can't be
17868        // cheaply reverted at generation start → refuse rather than
17869        // silently keep it overlaid.
17870        if let Some(a) = self.dyn_active {
17871            if !matches!(self.dyn_skill_layers.get(a), Some(Some(_))) {
17872                tracing::warn!("loaded skill is not FFN-eligible — dynamic routing unavailable");
17873                return 0;
17874            }
17875        }
17876        let hidden = self.hidden_size;
17877        let mut skills = Vec::new();
17878        for (idx, id, _phi) in self.dynamic_skills() {
17879            if let Some(sel) = model.header.skills[idx].selection.as_ref() {
17880                if let Some(rs) = RoutableSkill::from_descriptor(idx, id, sel, hidden) {
17881                    skills.push(rs);
17882                }
17883            }
17884        }
17885        if skills.is_empty() {
17886            return 0;
17887        }
17888        // Skills should share a phi_layer; warn (not fail) if they don't.
17889        let phi = skills[0].phi_layer;
17890        if skills.iter().any(|s| s.phi_layer != phi) {
17891            tracing::warn!("routable skills disagree on phi_layer; using {phi}");
17892        }
17893        let n = skills.len();
17894        self.set_dyn_phi_layer(Some(phi));
17895        self.dyn_router = Some(DynRouter::new(skills));
17896        n
17897    }
17898
17899    /// Human-readable switch log from the last dynamic-routed generation.
17900    pub fn route_switches(&self) -> Vec<(usize, Option<String>, Option<String>)> {
17901        self.dyn_router
17902            .as_ref()
17903            .map(|r| r.switches.clone())
17904            .unwrap_or_default()
17905    }
17906
17907    /// LM head: hidden → logits [vocab_size]. The dominant matvec of
17908    /// every decode step — row-parallel on the worker pool.
17909    fn lm_head_forward(&self, hidden: &[f32]) -> Vec<f32> {
17910        let _mimo_q8 = self
17911            .mimo_moe
17912            .is_on()
17913            .then(crate::qtensor::enter_full_gpu_q8_scope);
17914        let rows = self.weights.lm_head.rows();
17915        let mut logits = attention::take_buf(rows.min(self.vocab_size));
17916        // Banked MiMo uses the same exact projection family for the
17917        // plain/draft head and the batched verification head. Read both
17918        // scale planes in-place instead of preparing per-op scale buffers.
17919        let served = self.mimo_moe.is_on()
17920            && crate::gpu::mimo_q8_short_enabled()
17921            && rows == self.vocab_size
17922            && !self.weights.lm_head.has_prism_contract()
17923            && self
17924                .weights
17925                .lm_head
17926                .graph_weight()
17927                .is_some_and(|(model, idx, kind, _)| {
17928                    kind == 7
17929                        && crate::gpu::q82_short_rows(
17930                            model,
17931                            idx,
17932                            hidden,
17933                            1,
17934                            rows,
17935                            self.hidden_size,
17936                            &mut logits,
17937                        )
17938                });
17939        if !served {
17940            self.weights
17941                .lm_head
17942                .matvec(hidden, &mut logits, self.pool.as_deref());
17943        }
17944        logits.resize(self.vocab_size, 0.0);
17945        if let Some(m) = self.logit_multiplier {
17946            for l in logits.iter_mut() {
17947                *l *= m;
17948            }
17949        }
17950        if let Some(c) = self.final_softcap {
17951            softcap_in_place(&mut logits, c, self.pool.as_deref());
17952        }
17953        if let Some(cm) = self.head_clusters.as_ref() {
17954            self.hierarchical_head_logprobs(hidden, cm, &mut logits);
17955        }
17956        logits
17957    }
17958
17959    /// Two-level head (Cortiq Embryo): in place, logits[v] ← log p(v) =
17960    /// (lc[c] − lse(lc)) + (logit[v] − lse over v's cluster block), c = v / S.
17961    fn hierarchical_head_logprobs(&self, hidden: &[f32], cm: &[f32], logits: &mut [f32]) {
17962        let h = hidden.len();
17963        let ncl = cm.len() / h.max(1);
17964        if ncl == 0 || logits.len() % ncl != 0 {
17965            return;
17966        }
17967        let cs = logits.len() / ncl;
17968        // cluster logits + log-softmax
17969        let mut lc = vec![0.0f32; ncl];
17970        for c in 0..ncl {
17971            let row = &cm[c * h..(c + 1) * h];
17972            let mut s = 0.0f32;
17973            for j in 0..h {
17974                s += row[j] * hidden[j];
17975            }
17976            lc[c] = s;
17977        }
17978        let mx = lc.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
17979        let lse: f32 = mx + lc.iter().map(|v| (v - mx).exp()).sum::<f32>().ln();
17980        for c in 0..ncl {
17981            let blk = &mut logits[c * cs..(c + 1) * cs];
17982            let bm = blk.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
17983            let bl: f32 = bm + blk.iter().map(|v| (v - bm).exp()).sum::<f32>().ln();
17984            let add = lc[c] - lse - bl;
17985            for v in blk.iter_mut() {
17986                *v += add;
17987            }
17988        }
17989    }
17990
17991    /// Prefill `ids` and return the next-token logits — what the model
17992    /// would predict next, WITHOUT committing to generation (introspection
17993    /// for `cortiq explain`). Clears and repopulates the KV cache; leaves
17994    /// the active overlay untouched.
17995    pub fn prefill_next_logits(&mut self, ids: &[u32], task_mask: Option<&TaskMask>) -> Vec<f32> {
17996        #[cfg(target_os = "macos")]
17997        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
17998        self.clear_sequence_state();
17999        // This helper is used by the pooled classification endpoint, where
18000        // every request is a fresh sequence. The shared reset also clears the
18001        // wgpu token graph's device-side recurrent state.
18002        crate::gpu::graph_race_begin_generation();
18003        if task_mask.is_none() {
18004            self.o1_begin();
18005        }
18006        let mut hidden = vec![0.0f32; self.hidden_size];
18007        for (pos, &id) in ids.iter().enumerate() {
18008            let emb = self.embed_single(id);
18009            hidden = self.forward_layers(&emb, pos, task_mask);
18010        }
18011        if let Err(err) = self.o1_seal_checked() {
18012            self.o1_fail(err);
18013        }
18014        // Stacks that own their head (V4, V4.1, Qwen3.8-Flash-Next, GLM-5)
18015        // return a zero hidden and hand the logits out of band.
18016        if let Some(logits) = self.graph_logits.take() {
18017            return logits;
18018        }
18019        inference::rms_norm_into(
18020            &hidden,
18021            &self.weights.final_norm,
18022            self.rms_eps,
18023            self.norm_style,
18024            &mut self.ws.n1,
18025        );
18026        self.lm_head_forward(&self.ws.n1)
18027    }
18028}
18029
18030/// Convenience: deterministic tiny pipeline for tests.
18031pub fn create_test_pipeline(
18032    hidden_size: usize,
18033    intermediate_size: usize,
18034    num_heads: usize,
18035    num_kv_heads: usize,
18036    head_dim: usize,
18037    num_layers: usize,
18038    vocab_size: usize,
18039) -> Pipeline {
18040    // Small pseudo-random weights: constant weights make attention
18041    // degenerate and hide indexing bugs.
18042    let synth = |n: usize, salt: usize| -> Vec<f32> {
18043        (0..n)
18044            .map(|i| (((i * 31 + salt * 17 + 7) % 97) as f32 / 97.0 - 0.5) * 0.2)
18045            .collect()
18046    };
18047    let qt = |rows: usize, cols: usize, salt: usize| -> QTensor {
18048        QTensor::from_f32(synth(rows * cols, salt), rows, cols)
18049    };
18050    let layer_weights: Vec<LayerWeights> = (0..num_layers)
18051        .map(|li| LayerWeights {
18052            input_norm: vec![1.0; hidden_size],
18053            post_norm: vec![1.0; hidden_size],
18054            attn_out_norm: None,
18055            ffn_out_norm: None,
18056            layer_scale: None,
18057            ffn: FfnKind::Dense(DenseFfn {
18058                gate_proj: qt(intermediate_size, hidden_size, li * 10 + 5),
18059                up_proj: qt(intermediate_size, hidden_size, li * 10 + 6),
18060                down_proj: qt(hidden_size, intermediate_size, li * 10 + 7),
18061                act: Act::Silu,
18062                down_t: None,
18063                segs: Vec::new(),
18064            }),
18065            attn: AttnKind::Full {
18066                bias: None,
18067                wq: qt(num_heads * head_dim, hidden_size, li * 10 + 1),
18068                wk: qt(num_kv_heads * head_dim, hidden_size, li * 10 + 2),
18069                wv: qt(num_kv_heads * head_dim, hidden_size, li * 10 + 3),
18070                wo: qt(hidden_size, num_heads * head_dim, li * 10 + 4),
18071                q_norm: None,
18072                k_norm: None,
18073                output_gate: false,
18074                softplus_gate: None,
18075            },
18076        })
18077        .collect();
18078
18079    Pipeline::new(
18080        Tokenizer::byte_level(),
18081        PipelineWeights {
18082            embed_tokens: qt(vocab_size, hidden_size, 100),
18083            layers: layer_weights,
18084            lm_head: qt(vocab_size, hidden_size, 200),
18085            final_norm: vec![1.0; hidden_size],
18086        },
18087        hidden_size,
18088        intermediate_size,
18089        num_heads,
18090        num_kv_heads,
18091        head_dim,
18092        num_layers,
18093        num_layers, // physical_layers = num_layers (non-looped)
18094        false,      // loop_final_norm
18095        vocab_size,
18096        1e-6,
18097        10_000.0,
18098        NormStyle::Qwen,
18099        4096,
18100        SamplerConfig {
18101            seed: Some(42),
18102            ..Default::default()
18103        },
18104    )
18105}
18106
18107/// Batched dense-FFN: gate/up/down via matmat (element-wise the same
18108/// math as b × dense_ffn — the same dot kernels).
18109/// One mask bit, LSB-first per byte — `TaskMask::ffn_active_indices`'s
18110/// convention.
18111#[inline]
18112fn mask_bit(row: &[u8], j: usize) -> bool {
18113    (row.get(j >> 3).copied().unwrap_or(0) >> (j & 7)) & 1 != 0
18114}
18115
18116/// Zero the CLOSED neurons' activations in a [rows × inter] panel — the
18117/// masked-inference fast path's whole trick: full fused quant compute,
18118/// then the mask lands on the ACTIVATIONS, which is arithmetically the
18119/// pruned network without touching a quantized weight byte. Whole open
18120/// bytes (0xFF = 8 open neurons) skip in one test.
18121/// `CMF_FFN_MASK_GAIN` — Patent 12 FIG. 4, variance-preserving
18122/// rescaling: truncation removes a share of the layer's output energy,
18123/// so the survivors are scaled up to put the variance back where the
18124/// downstream norm expects it. A scalar here; per layer it is
18125/// `sqrt(total energy / kept energy)`.
18126fn mask_gain() -> f32 {
18127    static G: std::sync::OnceLock<f32> = std::sync::OnceLock::new();
18128    *G.get_or_init(|| {
18129        std::env::var("CMF_FFN_MASK_GAIN")
18130            .ok()
18131            .and_then(|v| v.parse().ok())
18132            .unwrap_or(1.0)
18133    })
18134}
18135
18136fn zero_masked_cols(g: &mut [f32], rows: usize, inter: usize, row: &[u8]) {
18137    // With CMF_FFN_MEANFILL a closed neuron contributes its average
18138    // instead of nothing — same bytes read, one constant restored.
18139    let fill = meanfill().and_then(|(i, v)| {
18140        let li = crate::gpu::cur_layer();
18141        (*i == inter && li >= 0).then(|| &v[li as usize * inter..(li as usize + 1) * inter])
18142    });
18143    for r in 0..rows {
18144        let base = r * inter;
18145        for (bi, &byte) in row.iter().enumerate() {
18146            if byte == 0xFF {
18147                continue;
18148            }
18149            let j0 = bi * 8;
18150            for bit in 0..8 {
18151                let j = j0 + bit;
18152                if j < inter && byte & (1 << bit) == 0 {
18153                    g[base + j] = fill.map_or(0.0, |f| f[j]);
18154                }
18155            }
18156        }
18157    }
18158    let gain = mask_gain();
18159    if gain != 1.0 {
18160        for v in g[..rows * inter].iter_mut() {
18161            *v *= gain;
18162        }
18163    }
18164}
18165
18166/// True when neuron `i`'s bit is set (no mask = everything runs).
18167#[inline]
18168fn tube_bit(row: Option<&[u8]>, i: usize) -> bool {
18169    row.is_none_or(|r| mask_bit(r, i))
18170}
18171
18172/// Every bit below `n` set — the common case for a tube file's CORE,
18173/// where only the tube bits vary per task.
18174fn all_bits_on(row: &[u8], n: usize) -> bool {
18175    (0..n).all(|i| mask_bit(row, i))
18176}
18177
18178/// `CMF_TUBE_TOPK` — how many tubes a TOKEN may open (0 = the task mask
18179/// decides alone). This is the dense FFN read as a mixture: the tubes
18180/// are the experts a k-means over `gate_proj` rows found, and the token
18181/// picks among them. `CMF_TUBE_SCORE=gate` scores a tube by its own
18182/// gate (realizable: only `up`/`down` of the losers go unread),
18183/// `=oracle` scores by the true `silu(gate)·up` mass (the ceiling —
18184/// only `down` is saved, and the selection has read what it predicts).
18185fn tube_topk() -> usize {
18186    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
18187    *K.get_or_init(|| {
18188        std::env::var("CMF_TUBE_TOPK")
18189            .ok()
18190            .and_then(|v| v.parse().ok())
18191            .unwrap_or(0)
18192    })
18193}
18194
18195fn tube_score_oracle() -> bool {
18196    static O: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
18197    *O.get_or_init(|| std::env::var("CMF_TUBE_SCORE").is_ok_and(|v| v == "oracle"))
18198}
18199
18200/// The routed arm of `tube_ffn`: a token opens only its best `k` tubes.
18201/// At `b == 1` (decode) the losers are genuinely never read — that is
18202/// the speed. At `b > 1` (the scoring sweep) every tube is computed and
18203/// the losers' activations are zeroed instead: same arithmetic, so the
18204/// perplexity is the routed model's, measured without a per-token
18205/// gather in the middle of a GEMM.
18206fn tube_ffn_routed(
18207    d: &DenseFfn,
18208    xs: &[f32],
18209    b: usize,
18210    pool: Option<&Pool>,
18211    mask_row: Option<&[u8]>,
18212    k: usize,
18213) -> Vec<f32> {
18214    let hidden = d.down_proj.rows();
18215    let core = d.gate_proj.rows();
18216    let core_full = mask_row.is_none_or(|r| all_bits_on(r, core));
18217    let mut out = match (b, core_full, mask_row) {
18218        (1, true, _) => dense_ffn(d, xs, pool),
18219        (1, false, Some(row)) => dense_ffn_masked(d, xs, pool, row),
18220        (_, true, _) => dense_ffn_batch(d, xs, b, pool, None),
18221        (_, false, row) => dense_ffn_batch(d, xs, b, pool, row),
18222    };
18223    let cand: Vec<usize> = (0..d.segs.len())
18224        .filter(|&i| tube_bit(mask_row, d.segs[i].start))
18225        .collect();
18226    if cand.is_empty() {
18227        return out;
18228    }
18229    // gate (and, where the score or the batch needs it, up) per tube.
18230    // The SCORE is taken at the point the serving path could take it:
18231    // off the gate alone, or off the finished activation for the oracle.
18232    let oracle = tube_score_oracle();
18233    let mut acts: Vec<Vec<f32>> = Vec::with_capacity(cand.len());
18234    let mut scores = vec![0f32; b * cand.len()];
18235    for (ci, &i) in cand.iter().enumerate() {
18236        let seg = &d.segs[i];
18237        let w = seg.width;
18238        let mut g = vec![0.0f32; b * w];
18239        if b == 1 {
18240            seg.gate.matvec(xs, &mut g, pool);
18241        } else {
18242            seg.gate.matmat(xs, b, &mut g, pool);
18243        }
18244        for v in g.iter_mut() {
18245            *v = Act::Silu.combine(*v, 1.0);
18246        }
18247        if !oracle {
18248            for t in 0..b {
18249                scores[t * cand.len() + ci] =
18250                    g[t * w..(t + 1) * w].iter().map(|v| v * v).sum::<f32>();
18251            }
18252        }
18253        if oracle || b > 1 {
18254            let mut u = vec![0.0f32; b * w];
18255            if b == 1 {
18256                seg.up.matvec(xs, &mut u, pool);
18257            } else {
18258                seg.up.matmat(xs, b, &mut u, pool);
18259            }
18260            for (a, &v) in g.iter_mut().zip(u.iter()) {
18261                *a *= v;
18262            }
18263            if oracle {
18264                for t in 0..b {
18265                    scores[t * cand.len() + ci] =
18266                        g[t * w..(t + 1) * w].iter().map(|v| v * v).sum::<f32>();
18267                }
18268            }
18269        }
18270        acts.push(g);
18271    }
18272    // per-token scores and the winners
18273    let keep = k.min(cand.len());
18274    let mut scratch: Vec<f32> = Vec::new();
18275    for t in 0..b {
18276        let mut sc: Vec<(f32, usize)> = (0..cand.len())
18277            .map(|ci| (scores[t * cand.len() + ci], ci))
18278            .collect();
18279        sc.sort_unstable_by(|x, y| y.0.total_cmp(&x.0));
18280        let mut alive = vec![false; cand.len()];
18281        for &(_, ci) in sc.iter().take(keep) {
18282            alive[ci] = true;
18283        }
18284        if b > 1 {
18285            for (ci, a) in acts.iter_mut().enumerate() {
18286                if !alive[ci] {
18287                    let w = d.segs[cand[ci]].width;
18288                    a[t * w..(t + 1) * w].fill(0.0);
18289                }
18290            }
18291        } else {
18292            // decode: finish only the winners — the losers' up/down
18293            // (and, with the gate score, everything but their gate)
18294            // are never touched.
18295            for (ci, &i) in cand.iter().enumerate() {
18296                if !alive[ci] {
18297                    continue;
18298                }
18299                let seg = &d.segs[i];
18300                let w = seg.width;
18301                let g = &mut acts[ci];
18302                if !tube_score_oracle() {
18303                    scratch.clear();
18304                    scratch.resize(w, 0.0);
18305                    seg.up.matvec(xs, &mut scratch, pool);
18306                    for (a, &v) in g.iter_mut().zip(scratch.iter()) {
18307                        *a *= v;
18308                    }
18309                }
18310                let mut acc = vec![0.0f32; hidden];
18311                seg.down.matvec(g, &mut acc, pool);
18312                for (o, a) in out.iter_mut().zip(&acc) {
18313                    *o += *a;
18314                }
18315            }
18316        }
18317    }
18318    if b > 1 {
18319        for (ci, &i) in cand.iter().enumerate() {
18320            let seg = &d.segs[i];
18321            let mut acc = vec![0.0f32; b * hidden];
18322            seg.down.matmat(&acts[ci], b, &mut acc, pool);
18323            for (o, a) in out.iter_mut().zip(&acc) {
18324                *o += *a;
18325            }
18326        }
18327    }
18328    out
18329}
18330
18331/// FFN of a defragged tube layer: the always-on core plus the tubes the
18332/// task mask switches on. Each tube is a normal tensor triple, so the
18333/// same kernels run it and an inactive tube's bytes are never read —
18334/// that is the whole point of the defrag (a scattered mask cannot skip
18335/// bytes; a contiguous one is just a smaller matrix).
18336fn tube_ffn(
18337    d: &DenseFfn,
18338    xs: &[f32],
18339    b: usize,
18340    pool: Option<&Pool>,
18341    mask_row: Option<&[u8]>,
18342) -> Vec<f32> {
18343    if tube_topk() > 0 {
18344        return tube_ffn_routed(d, xs, b, pool, mask_row, tube_topk());
18345    }
18346    let hidden = d.down_proj.rows();
18347    let core = d.gate_proj.rows();
18348    let core_full = mask_row.is_none_or(|r| all_bits_on(r, core));
18349    let mut out = match (b, core_full, mask_row) {
18350        (1, true, _) => dense_ffn(d, xs, pool),
18351        (1, false, Some(row)) => dense_ffn_masked(d, xs, pool, row),
18352        (_, true, _) => dense_ffn_batch(d, xs, b, pool, None),
18353        (_, false, row) => dense_ffn_batch(d, xs, b, pool, row),
18354    };
18355    TUBE_SCRATCH.with(|sc| {
18356        let mut sc = sc.borrow_mut();
18357        let [g, u, acc] = &mut *sc;
18358        for seg in &d.segs {
18359            if !tube_bit(mask_row, seg.start) {
18360                continue;
18361            }
18362            let w = seg.width;
18363            g.resize(b * w, 0.0);
18364            if b == 1
18365                && d.act == Act::Silu
18366                && QTensor::matvec_silu_mul(&seg.gate, &seg.up, xs, g, pool)
18367            {
18368                // g holds silu(gate)·up.
18369            } else {
18370                u.resize(b * w, 0.0);
18371                if b == 1 {
18372                    QTensor::matvec_many([&seg.gate, &seg.up], xs, [g, u], pool);
18373                } else {
18374                    seg.gate.matmat(xs, b, g, pool);
18375                    seg.up.matmat(xs, b, u, pool);
18376                }
18377                for i in 0..b * w {
18378                    g[i] = d.act.combine(g[i], u[i]);
18379                }
18380            }
18381            acc.resize(b * hidden, 0.0);
18382            acc.fill(0.0);
18383            if b == 1 {
18384                seg.down.matvec(g, acc, pool);
18385            } else {
18386                seg.down.matmat(g, b, acc, pool);
18387            }
18388            for (o, a) in out.iter_mut().zip(acc.iter()) {
18389                *o += *a;
18390            }
18391        }
18392        out
18393    })
18394}
18395
18396thread_local! {
18397    /// gate / up / down-accumulator scratch for the tube loop — a tube
18398    /// runs once per layer per token, and a fresh Vec each time is a
18399    /// malloc per tube per layer per token.
18400    static TUBE_SCRATCH: std::cell::RefCell<[Vec<f32>; 3]> =
18401        const { std::cell::RefCell::new([Vec::new(), Vec::new(), Vec::new()]) };
18402}
18403
18404/// CMF_PREFILL_PROF: cumulative ns of the batched walk's attention and
18405/// FFN halves (all layers, all chunks).
18406static PREFILL_SPLIT: [std::sync::atomic::AtomicU64; 2] = [
18407    std::sync::atomic::AtomicU64::new(0),
18408    std::sync::atomic::AtomicU64::new(0),
18409];
18410
18411/// The wgpu side of `CMF_PREFILL_PROF`: q|k|v GEMMs' enqueue and readback
18412/// wait, the chunk attend's readback wait (ms, cumulative) and the int8
18413/// GEMMs the `zi_mm` route took.
18414fn wgpu_prefill_counters() -> (f64, f64, f64, u64) {
18415    #[cfg(feature = "gpu")]
18416    {
18417        use std::sync::atomic::Ordering::Relaxed;
18418        let ms = |a: &std::sync::atomic::AtomicU64| a.load(Relaxed) as f64 / 1e6;
18419        (
18420            ms(&crate::gpu_wgpu::GEMM_MANY_NS[0]),
18421            ms(&crate::gpu_wgpu::GEMM_MANY_NS[1]),
18422            ms(&crate::gpu_wgpu::CHUNK_ATTEND_WAIT_NS),
18423            crate::gpu_wgpu::ZI_GEMM_CALLS.load(Relaxed),
18424        )
18425    }
18426    #[cfg(not(feature = "gpu"))]
18427    (0.0, 0.0, 0.0, 0)
18428}
18429
18430/// The wgpu prefill mirror attend's outcome counters
18431/// (`gpu_wgpu::MIRROR_EVENTS`).
18432fn wgpu_mirror_counters() -> [u64; 6] {
18433    #[cfg(feature = "gpu")]
18434    {
18435        crate::gpu_wgpu::MIRROR_EVENTS
18436            .each_ref()
18437            .map(|a| a.load(std::sync::atomic::Ordering::Relaxed))
18438    }
18439    #[cfg(not(feature = "gpu"))]
18440    [0; 6]
18441}
18442
18443fn prefill_prof_on() -> bool {
18444    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
18445    *ON.get_or_init(|| std::env::var_os("CMF_PREFILL_PROF").is_some())
18446}
18447
18448/// Accumulate the dense FFN activations which feed the down projection.
18449///
18450/// MTP calibration intentionally calls this independently of the trunk's
18451/// `FFN_PROBE` TLS recorder: an MTP sidecar can use a different FFN width,
18452/// and borrowing the trunk's layer-numbered accumulator would silently turn
18453/// draft statistics into trunk proxies.  The caller owns the execution scope
18454/// (the public MTP probe uses `gpu::cpu_scope`) and supplies one accumulator
18455/// per actual MTP FFN channel.
18456pub(crate) fn accumulate_dense_ffn_activation_mass(
18457    d: &DenseFfn,
18458    xs: &[f32],
18459    b: usize,
18460    pool: Option<&Pool>,
18461    mass: &mut [f64],
18462    squared_mass: &mut [f64],
18463) -> Result<(), String> {
18464    let inter = d.gate_proj.rows();
18465    let hidden = d.gate_proj.cols();
18466    if mass.len() != inter || squared_mass.len() != inter {
18467        return Err(format!(
18468            "MTP FFN probe: accumulator widths ({}, {}) != gate width {inter}",
18469            mass.len(),
18470            squared_mass.len()
18471        ));
18472    }
18473    if d.up_proj.rows() != inter
18474        || d.up_proj.cols() != hidden
18475        || d.down_proj.cols() != inter
18476        || d.down_proj.rows() != hidden
18477    {
18478        return Err("MTP FFN probe: inconsistent dense FFN shapes".into());
18479    }
18480    if !d.segs.is_empty() {
18481        return Err("MTP FFN probe: tube-segmented draft FFNs are not supported".into());
18482    }
18483    if xs.len() != b.saturating_mul(hidden) {
18484        return Err(format!(
18485            "MTP FFN probe: {} input values for {b} × {hidden} rows",
18486            xs.len()
18487        ));
18488    }
18489    if b == 0 {
18490        return Ok(());
18491    }
18492    let mut gate = vec![0.0f32; b * inter];
18493    let mut up = vec![0.0f32; b * inter];
18494    d.gate_proj.matmat(xs, b, &mut gate, pool);
18495    d.up_proj.matmat(xs, b, &mut up, pool);
18496    for row in 0..b {
18497        for i in 0..inter {
18498            let a = d.act.combine(gate[row * inter + i], up[row * inter + i]);
18499            let a = a as f64;
18500            mass[i] += a.abs();
18501            squared_mass[i] += a * a;
18502        }
18503    }
18504    Ok(())
18505}
18506
18507fn dense_ffn_batch(
18508    d: &DenseFfn,
18509    xs: &[f32],
18510    b: usize,
18511    pool: Option<&Pool>,
18512    mask_row: Option<&[u8]>,
18513) -> Vec<f32> {
18514    let inter = d.gate_proj.rows();
18515    let hidden = d.down_proj.rows();
18516    // Fused on-device SwiGLU when the device is in play: three separate
18517    // `matmat` calls are three round trips per layer, and the gate/up
18518    // panels (b × inter — 22 MB each at a 512-token chunk) cross the bus
18519    // twice for nothing. The kernel already existed for the image DiT;
18520    // the LLM prefill was simply never wired to it. A task mask needs the
18521    // activations on the host between the halves, so it keeps the CPU
18522    // arm below.
18523    // SiLU/GELU have device arms everywhere they are admitted.  MiniMax
18524    // M3's frozen OAI contract has an additional WGPU-only arm; callers on
18525    // native Metal still get an honest decline from `q4_ffn_act` rather than
18526    // an accidental SiLU evaluation.
18527    let fused_act = d.act.wgpu_graph_act();
18528    if mask_row.is_none()
18529        && fused_act.is_some()
18530        && b >= 32
18531        && crate::gpu::enabled_here()
18532        && !crate::gpu::mm_killed()
18533        // The refit pass needs this layer's activations on the host; the
18534        // fused chain keeps them on the device. Refusing it here costs
18535        // one round trip and keeps every GEMM on the card — the
18536        // alternative was running the whole calibration on the CPU.
18537        && refit_dir().is_none()
18538        // Same for the mass/hit probes. The accumulator at the bottom of
18539        // this function only sees `g` when `g` came back to the host, so
18540        // a fused batch would leave it summing nothing — a probe that
18541        // reports zeros rather than failing, which is worse.
18542        && !ffn_probe_active()
18543        // GPTQ calibration folds every projection's input on the host
18544        // (`QTensor::matmat`); the fused chain would hand gate/up/down
18545        // their inputs on the card and leave the FFN uncalibrated.
18546        && !crate::gptq_capture::capturing()
18547    {
18548        if let (Some((model, w1)), Some((_, w3)), Some((_, w2))) = (
18549            d.gate_proj.mapped_q4t(),
18550            d.up_proj.mapped_q4t(),
18551            d.down_proj.mapped_q4t(),
18552        ) {
18553            let mut out = vec![0.0f32; b * hidden];
18554            let act = fused_act.expect("checked above");
18555            if crate::gpu::q4_ffn_act(
18556                model, w1, w3, w2, xs, b, hidden, inter, false, act, &mut out,
18557            ) {
18558                return out;
18559            }
18560        }
18561        // The q4tp twin (same kernel family, scale from the row ladder) —
18562        // the DiT has run it in production since the pipeline containers;
18563        // the LLM prefill was simply never wired to it, so a q4tp model's
18564        // prefill panels stayed on the CPU.
18565        if let (Some((model, w1)), Some((_, w3)), Some((_, w2))) = (
18566            d.gate_proj.mapped_q4tp(),
18567            d.up_proj.mapped_q4tp(),
18568            d.down_proj.mapped_q4tp(),
18569        ) {
18570            let mut out = vec![0.0f32; b * hidden];
18571            let act = fused_act.expect("checked above");
18572            if crate::gpu::q4_ffn_act(model, w1, w3, w2, xs, b, hidden, inter, true, act, &mut out)
18573            {
18574                return out;
18575            }
18576        }
18577        // Every other device codec — int8, or gate/up and down in different
18578        // codecs: the three GEMMs with the panels kept on the card. Without
18579        // it a q8_2f prefill read both b·inter panels home, folded them on
18580        // one host thread, and sent the result back for down.
18581        // CMF_FFN_KEEP=0 keeps the per-GEMM path (A/B).
18582        if let (true, Some((model, w1)), Some((_, w3)), Some((_, w2))) = (
18583            std::env::var("CMF_FFN_KEEP").as_deref() != Ok("0"),
18584            d.gate_proj.mapped_device_gemm(),
18585            d.up_proj.mapped_device_gemm(),
18586            d.down_proj.mapped_device_gemm(),
18587        ) {
18588            let mut out = vec![0.0f32; b * hidden];
18589            let act = fused_act.expect("checked above");
18590            if crate::gpu::ffn_act_keep(model, w1, w3, w2, xs, b, hidden, inter, act, &mut out) {
18591                return out;
18592            }
18593        }
18594    }
18595    let mut g = vec![0.0f32; b * inter];
18596    d.gate_proj.matmat(xs, b, &mut g, pool);
18597    let mut u = vec![0.0f32; b * inter];
18598    d.up_proj.matmat(xs, b, &mut u, pool);
18599    if gate_topk() > 0 && d.act == Act::Silu {
18600        for t in 0..b {
18601            let row = &mut g[t * inter..(t + 1) * inter];
18602            for v in row.iter_mut() {
18603                *v = Act::Silu.combine(*v, 1.0);
18604            }
18605            keep_top_k(row, gate_topk());
18606        }
18607        for i in 0..b * inter {
18608            g[i] *= u[i];
18609        }
18610    } else {
18611        for i in 0..b * inter {
18612            g[i] = d.act.combine(g[i], u[i]);
18613        }
18614    }
18615    if let Some(row) = mask_row {
18616        zero_masked_cols(&mut g, b, inter, row);
18617    }
18618    if oracle_topk() > 0 {
18619        for t in 0..b {
18620            keep_top_k(&mut g[t * inter..(t + 1) * inter], oracle_topk());
18621        }
18622    }
18623    let mut out = vec![0.0f32; b * hidden];
18624    d.down_proj.matmat(&g, b, &mut out, pool);
18625    if refit_dir().is_some() {
18626        let li = crate::gpu::cur_layer();
18627        if li >= 0 {
18628            refit_accumulate(li as usize, &g, b, inter, &out, hidden, pool);
18629        }
18630    }
18631    // The DTG-MA probe, on the batched path: one prefill sweep gives the
18632    // same per-neuron statistic the per-position probe does, and on a 27B
18633    // that is minutes instead of hours.
18634    FFN_PROBE.with(|pr| {
18635        if let Some(acc) = pr.borrow_mut().as_mut() {
18636            let li = crate::gpu::cur_layer();
18637            if li < 0 {
18638                return;
18639            }
18640            let Some(row) = acc.get_mut(li as usize) else {
18641                return;
18642            };
18643            let sq = probe_sq();
18644            for t in 0..b {
18645                for (a, &v) in row.iter_mut().zip(&g[t * inter..(t + 1) * inter]) {
18646                    *a += if sq {
18647                        (v as f64) * (v as f64)
18648                    } else {
18649                        (v as f64).abs()
18650                    };
18651                }
18652            }
18653        }
18654    });
18655    out
18656}
18657
18658/// Batched MoE-FFN: router batched, positions are GROUPED by expert —
18659/// an expert's weights are read once for all its positions in the chunk
18660/// (the main prefill-GEMM win on MoE: 960MB/token of 35B experts).
18661/// Accumulate per-channel activation energy for `CMF_RMS_TRACE`.
18662fn accumulate_act(m: &MoeFfn, xs: &[f32], b: usize) {
18663    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
18664    static DUMP: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
18665    let on = *ON.get_or_init(|| std::env::var("CMF_RMS_TRACE").is_ok());
18666    let dump = *DUMP.get_or_init(|| std::env::var("CMF_ACT_DUMP").is_ok());
18667    if (!on && !dump) || b == 0 {
18668        return;
18669    }
18670    let hidden = xs.len() / b;
18671    if on {
18672        let mut acc = m.act_sq.borrow_mut();
18673        if acc.len() < hidden {
18674            acc.resize(hidden, 0.0);
18675        }
18676        for t in 0..b {
18677            let row = &xs[t * hidden..(t + 1) * hidden];
18678            for (a, &v) in acc.iter_mut().zip(row) {
18679                *a += (v as f64) * (v as f64);
18680            }
18681        }
18682    }
18683    if dump {
18684        // Cap the capture: the covariance needs a few thousand rows, and a
18685        // whole prefill of every layer would be gigabytes for no extra rank.
18686        let cap: usize = std::env::var("CMF_ACT_DUMP_ROWS")
18687            .ok()
18688            .and_then(|v| v.parse().ok())
18689            .unwrap_or(4096);
18690        let mut rows = m.act_rows.borrow_mut();
18691        if rows.len() < cap * hidden {
18692            let take = b.min((cap * hidden - rows.len()) / hidden.max(1));
18693            rows.extend_from_slice(&xs[..take * hidden]);
18694        }
18695    }
18696}
18697
18698/// Send-able cursor over a Vec-of-Vecs: each pool worker writes only its
18699/// own slots (disjoint by construction in the caller).
18700#[derive(Clone, Copy)]
18701struct SendVecs(*mut Vec<f32>);
18702unsafe impl Send for SendVecs {}
18703unsafe impl Sync for SendVecs {}
18704impl SendVecs {
18705    #[inline]
18706    fn at(self, i: usize) -> *mut Vec<f32> {
18707        unsafe { self.0.add(i) }
18708    }
18709}
18710
18711fn moe_ffn_batch(
18712    m: &MoeFfn,
18713    xs: &[f32],
18714    b: usize,
18715    hidden: usize,
18716    pool: Option<&Pool>,
18717    allowed: Option<&[bool]>,
18718) -> Vec<f32> {
18719    accumulate_act(m, xs, b);
18720    let ne = m.experts.len();
18721    let mut logits = vec![0.0f32; b * ne];
18722    match &m.resonance {
18723        Some(r) => {
18724            let hdim = xs.len() / b.max(1);
18725            for bi in 0..b {
18726                r.scores(
18727                    &xs[bi * hdim..(bi + 1) * hdim],
18728                    &mut logits[bi * ne..(bi + 1) * ne],
18729                );
18730            }
18731        }
18732        None => m.router.matmat(xs, b, &mut logits, pool),
18733    }
18734
18735    // Assignments: expert → [(position, weight)] — same routing as
18736    // moe_ffn, per position (see `moe_route`).
18737    let mut assign: Vec<Vec<(usize, f32)>> = vec![Vec::new(); ne];
18738    {
18739        let mut st = m.stats.borrow_mut();
18740        if st.len() < ne {
18741            st.resize(ne, 0);
18742        }
18743        for bi in 0..b {
18744            let (idx, p, wsum) = moe_route(&logits[bi * ne..(bi + 1) * ne], m, allowed);
18745            for &e in &idx {
18746                st[e] += 1;
18747                assign[e].push((bi, p[e] / wsum));
18748            }
18749        }
18750    }
18751
18752    let mut out = vec![0.0f32; b * hidden];
18753    let cols = m.experts[0].gate_proj.cols();
18754    let run_expert = |d: &DenseFfn, list: &[(usize, f32)], out: &mut [f32]| {
18755        let sb = list.len();
18756        let mut sub = vec![0.0f32; sb * cols];
18757        for (k, &(bi, _)) in list.iter().enumerate() {
18758            sub[k * cols..(k + 1) * cols].copy_from_slice(&xs[bi * cols..(bi + 1) * cols]);
18759        }
18760        let eo = dense_ffn_batch(d, &sub, sb, pool, None);
18761        for (k, &(bi, w)) in list.iter().enumerate() {
18762            for i in 0..hidden {
18763                out[bi * hidden + i] += w * eo[k * hidden + i];
18764            }
18765        }
18766    };
18767    // Routed experts: the panels are TINY (b·top_k spread over every
18768    // expert — a few positions each), so a pool dispatch per expert is
18769    // pure barrier cost. Invert the parallelism: workers take WHOLE
18770    // experts (serial math inside), then one deterministic scatter in
18771    // expert order — the exact accumulation order the serial loop had.
18772    let active: Vec<usize> = (0..ne).filter(|&e| !assign[e].is_empty()).collect();
18773    if pool.is_some() && active.len() >= 8 {
18774        let mut panels: Vec<Vec<f32>> = vec![Vec::new(); active.len()];
18775        {
18776            let panel_ptr = SendVecs(panels.as_mut_ptr());
18777            // Capture only the expert table: `m` itself carries RefCell
18778            // stats and must not cross the pool boundary.
18779            let experts = &m.experts;
18780            let (active_r, assign_r) = (&active, &assign);
18781            let inherit_cpu = crate::gpu::inherit_cpu_scope();
18782            let run = |start: usize, end: usize| {
18783                let _cpu_scope = inherit_cpu();
18784                // Native Metal: the per-op device helpers (`q4tp_ffn`,
18785                // `q8_matmat`, …) stage through size-keyed shared io
18786                // buffers and are not re-entrant — two workers whose experts
18787                // drew the same row count wrote one buffer at once, so a
18788                // Mellum2.1 prompt came out different on every run. The
18789                // workers keep whole experts on the CPU there; the device
18790                // prefill is the MoE chunk graph.
18791                #[cfg(target_os = "macos")]
18792                let _metal_cpu = crate::gpu::enter_cpu_scope();
18793                for ai in start..end {
18794                    let e = active_r[ai];
18795                    let list = &assign_r[e];
18796                    let sb = list.len();
18797                    let mut sub = vec![0.0f32; sb * cols];
18798                    for (k, &(bi, _)) in list.iter().enumerate() {
18799                        sub[k * cols..(k + 1) * cols]
18800                            .copy_from_slice(&xs[bi * cols..(bi + 1) * cols]);
18801                    }
18802                    // SAFETY: each worker owns a disjoint panels[ai].
18803                    unsafe {
18804                        *panel_ptr.at(ai) = dense_ffn_batch(&experts[e], &sub, sb, None, None);
18805                    }
18806                }
18807            };
18808            match pool {
18809                Some(p) => p.run_rows(active.len(), &run),
18810                None => run(0, active.len()),
18811            }
18812        }
18813        for (ai, &e) in active.iter().enumerate() {
18814            for (k, &(bi, w)) in assign[e].iter().enumerate() {
18815                let eo = &panels[ai][k * hidden..(k + 1) * hidden];
18816                for i in 0..hidden {
18817                    out[bi * hidden + i] += w * eo[i];
18818                }
18819            }
18820        }
18821    } else {
18822        for &e in &active {
18823            run_expert(&m.experts[e], &assign[e], &mut out);
18824        }
18825    }
18826    if let Some((se, gate)) = &m.shared {
18827        let all: Vec<(usize, f32)> = if let Some(gate) = gate {
18828            let mut gl = vec![0.0f32; b];
18829            gate.matmat(xs, b, &mut gl, pool);
18830            (0..b)
18831                .map(|bi| (bi, 1.0 / (1.0 + (-gl[bi]).exp())))
18832                .collect()
18833        } else {
18834            (0..b).map(|bi| (bi, 1.0)).collect()
18835        };
18836        run_expert(se, &all, &mut out);
18837    }
18838    out
18839}
18840
18841/// Decode-exact multi-token MoE — the MiMo speculative verify's FFN. Row
18842/// `r` of the result is bit-identical to `moe_ffn(m, x_r)` on the CPU
18843/// (`moe_ffn_cpu` → `moe_ffn_cpu_batched`): router matvec per row, the same
18844/// routing, the same int8 gate/up/SiLU and down terms
18845/// (`QTensor::moe_gate_up_rows` / `moe_down_rows`), and the row's experts
18846/// summed in ITS route order from 0. What the rows share is the weight
18847/// traffic: each routed expert is read once for every row that picked it.
18848/// (`moe_ffn_batch`, the prompt path, groups the same way but sums in
18849/// expert-index order and runs blocked kernels on wide groups — close, not
18850/// bit-equal to decode.) Any layer the kernels do not cover, or a device
18851/// that could answer `moe_ffn` itself, walks `moe_ffn` row by row.
18852fn moe_ffn_rows_exact(
18853    m: &MoeFfn,
18854    xs: &[f32],
18855    b: usize,
18856    hidden: usize,
18857    pool: Option<&Pool>,
18858) -> Vec<f32> {
18859    let mut out = vec![0.0f32; b * hidden];
18860    let per_row = |out: &mut [f32]| {
18861        for r in 0..b {
18862            let o = moe_ffn(m, &xs[r * hidden..(r + 1) * hidden], pool, None);
18863            out[r * hidden..(r + 1) * hidden].copy_from_slice(&o);
18864        }
18865    };
18866    let covered = !crate::gpu::enabled_here()
18867        && moe_batch_enabled()
18868        && m.shared.is_none()
18869        && m.resonance.is_none()
18870        && FFN_PROBE.with(|pr| pr.borrow().is_none())
18871        && m.experts.iter().all(|d| d.act == Act::Silu);
18872    if !covered {
18873        per_row(&mut out);
18874        return out;
18875    }
18876    let ne = m.experts.len();
18877    // Routing, row by row, exactly as `moe_ffn`.
18878    let mut routes: Vec<(Vec<usize>, Vec<f32>)> = Vec::with_capacity(b);
18879    for r in 0..b {
18880        let x = &xs[r * hidden..(r + 1) * hidden];
18881        accumulate_act(m, x, 1);
18882        let mut logits = vec![0.0f32; ne];
18883        m.router.matvec(x, &mut logits, pool);
18884        let (idx, p, wsum) = moe_route(&logits, m, None);
18885        {
18886            let mut st = m.stats.borrow_mut();
18887            if st.len() < ne {
18888                st.resize(ne, 0);
18889            }
18890            for &e in &idx {
18891                st[e] += 1;
18892            }
18893        }
18894        let w: Vec<f32> = idx
18895            .iter()
18896            .map(|&e| p[e] / wsum * m.per_expert_scale.as_ref().map_or(1.0, |v| v[e]))
18897            .collect();
18898        routes.push((idx, w));
18899    }
18900    if routes.iter().any(|(idx, _)| idx.is_empty()) {
18901        per_row(&mut out);
18902        return out;
18903    }
18904    // Group the (row, expert) picks by expert, in first-seen order.
18905    let mut experts: Vec<usize> = Vec::new();
18906    let mut groups: Vec<Vec<usize>> = Vec::new();
18907    for (r, (idx, _)) in routes.iter().enumerate() {
18908        for &e in idx {
18909            match experts.iter().position(|&x| x == e) {
18910                Some(g) => groups[g].push(r),
18911                None => {
18912                    experts.push(e);
18913                    groups.push(vec![r]);
18914                }
18915            }
18916        }
18917    }
18918    let n_pairs: usize = groups.iter().map(|g| g.len()).sum();
18919    let inter = m.experts[experts[0]].gate_proj.rows();
18920    let pairs: Vec<(&QTensor, &QTensor)> = experts
18921        .iter()
18922        .map(|&e| (&m.experts[e].gate_proj, &m.experts[e].up_proj))
18923        .collect();
18924    let mut gs: Vec<Vec<f32>> = (0..n_pairs).map(|_| vec![0f32; inter]).collect();
18925    if !QTensor::moe_gate_up_rows(&pairs, &groups, xs, &mut gs, pool) {
18926        per_row(&mut out);
18927        return out;
18928    }
18929    let downs: Vec<&QTensor> = experts.iter().map(|&e| &m.experts[e].down_proj).collect();
18930    let lens: Vec<usize> = groups.iter().map(|g| g.len()).collect();
18931    let mut ds: Vec<Vec<f32>> = (0..n_pairs).map(|_| vec![0f32; hidden]).collect();
18932    if !QTensor::moe_down_rows(&downs, &lens, &gs, &mut ds, pool) {
18933        per_row(&mut out);
18934        return out;
18935    }
18936    // Where each (row, expert) term landed in the flat pair list.
18937    let mut slot = std::collections::HashMap::with_capacity(n_pairs);
18938    let mut p = 0usize;
18939    for (g, &e) in experts.iter().enumerate() {
18940        for &r in &groups[g] {
18941            slot.insert((r, e), p);
18942            p += 1;
18943        }
18944    }
18945    for (r, (idx, w)) in routes.iter().enumerate() {
18946        let terms: Vec<(&[f32], f32)> = idx
18947            .iter()
18948            .zip(w)
18949            .map(|(&e, &we)| (ds[slot[&(r, e)]].as_slice(), we))
18950            .collect();
18951        let row = &mut out[r * hidden..(r + 1) * hidden];
18952        for (i, dst) in row.iter_mut().enumerate() {
18953            // `moe_down_many`'s per-row sum: from 0, in route order.
18954            let mut acc = 0f32;
18955            for (d, we) in &terms {
18956                acc += we * d[i];
18957            }
18958            *dst = acc;
18959        }
18960    }
18961    out
18962}
18963
18964/// Gemma's final-logit soft-capping, `c·tanh(x/c)`, over a whole vocab row —
18965/// element for element the serial expression, split across the pool: at a
18966/// 262k vocab the serial `tanh` pass was ~5 ms of a 36 ms decode token.
18967pub(crate) fn softcap_in_place(v: &mut [f32], c: f32, pool: Option<&Pool>) {
18968    match pool {
18969        Some(p) if v.len() >= 1 << 15 => {
18970            let base = v.as_mut_ptr() as usize;
18971            p.run_rows(v.len(), &|s, e| {
18972                // SAFETY: run_rows hands out disjoint [s, e) ranges of `v`,
18973                // which outlives the (blocking) dispatch.
18974                let sl =
18975                    unsafe { std::slice::from_raw_parts_mut((base as *mut f32).add(s), e - s) };
18976                for x in sl {
18977                    *x = c * (*x / c).tanh();
18978                }
18979            });
18980        }
18981        _ => {
18982            for x in v.iter_mut() {
18983                *x = c * (*x / c).tanh();
18984            }
18985        }
18986    }
18987}
18988
18989thread_local! {
18990    /// gate/up activation scratch for the dense FFN paths (single uses
18991    /// two slots, the fused pair all four) — these were fresh
18992    /// intermediate-size Vecs on every layer of every token.
18993    static FFN_SCRATCH: std::cell::RefCell<[Vec<f32>; 4]> =
18994        const { std::cell::RefCell::new([Vec::new(), Vec::new(), Vec::new(), Vec::new()]) };
18995}
18996
18997/// Dense SwiGLU FFN through QTensor matvecs (any storage).
18998fn dense_ffn(d: &DenseFfn, x: &[f32], pool: Option<&Pool>) -> Vec<f32> {
18999    // Per-token sparsity, when the file was built for it: gate first,
19000    // then only the chosen neurons' up/down rows leave the mmap.
19001    if gate_topk() > 0
19002        && let Some(out) = dense_ffn_dynamic(d, x, pool, gate_topk())
19003    {
19004        return out;
19005    }
19006    // Whole-FFN GPU submit (этап 4.2 increment): gate → silu·up → down
19007    // chained in ONE command buffer with the intermediate activations
19008    // resident on the device — 3 per-op polls become 1 per layer. The
19009    // moe_block backend already implements exactly this chain; a dense
19010    // FFN is one expert with weight 1. Runtime probe: the chain still
19011    // pays one submit+poll per layer — alternate it against the pure-CPU
19012    // FFN and keep whichever is faster on this machine.
19013    // q1 FFNs offload at any practical size: the q1 CPU kernel is
19014    // compute-bound, so the UMA threshold logic does not apply — the
19015    // probe measures and decides either way.
19016    // The fused GPU block has no descriptor-aware Prism path: it would either
19017    // consume an unrotated activation or decline after inspecting the mixed
19018    // q2tp/q4tp tensors.  Do not let that structural refusal enter the FFN
19019    // probe's CPU_ONLY scope; the ordinary body below dispatches each matrix
19020    // through QTensor::matvec, which owns the signed FWHT + affine q2tp route.
19021    let prism_body = d.gate_proj.has_prism_contract()
19022        || d.up_proj.has_prism_contract()
19023        || d.down_proj.has_prism_contract();
19024    if !prism_body
19025        && crate::gpu::enabled_here()
19026        && (d.gate_proj.rows() >= crate::gpu::min_rows() || d.gate_proj.is_q1())
19027    {
19028        let arm = if d.gate_proj.is_q1() && crate::gpu::q1_force() {
19029            crate::gpu::ProbeArm::Gpu
19030        } else {
19031            crate::gpu::probe_arm(crate::gpu::OpClass::Ffn)
19032        };
19033        match arm {
19034            crate::gpu::ProbeArm::Gpu => {
19035                let t0 = std::time::Instant::now();
19036                if let Some(out) = dense_ffn_gpu(d, x, pool) {
19037                    crate::gpu::probe_record(crate::gpu::OpClass::Ffn, true, t0.elapsed());
19038                    return out;
19039                }
19040                // Declined: no timing exists, so say so. Silence here is
19041                // what left `ffn` undecided for 9000 calls and cost a
19042                // failed device attempt on half of them.
19043                crate::gpu::probe_note_decline(crate::gpu::OpClass::Ffn);
19044            }
19045            crate::gpu::ProbeArm::CpuTimed => {
19046                let t0 = std::time::Instant::now();
19047                let out = crate::gpu::cpu_scope(|| dense_ffn_cpu(d, x, pool));
19048                crate::gpu::probe_record(crate::gpu::OpClass::Ffn, false, t0.elapsed());
19049                return out;
19050            }
19051            crate::gpu::ProbeArm::Cpu => {
19052                return crate::gpu::cpu_scope(|| dense_ffn_cpu(d, x, pool));
19053            }
19054        }
19055    }
19056    dense_ffn_cpu(d, x, pool)
19057}
19058
19059/// The pure-CPU dense-FFN body (also the fallback of every GPU refusal).
19060fn dense_ffn_cpu(d: &DenseFfn, x: &[f32], pool: Option<&Pool>) -> Vec<f32> {
19061    let inter = d.gate_proj.rows();
19062    FFN_SCRATCH.with(|s| {
19063        let mut s = s.borrow_mut();
19064        let [g, u, ..] = &mut *s;
19065        g.resize(inter, 0.0);
19066        // Fused gate+up+silu: one dispatch, no separate silu pass.
19067        // Falls back to matvec_many + silu loop for unsupported dtypes.
19068        if gate_topk() > 0 {
19069            // Gate first, select, and only then pay for `up`: the
19070            // measurement arm computes both and zeroes the losers, which
19071            // is the same arithmetic.
19072            u.resize(inter, 0.0);
19073            QTensor::matvec_many([&d.gate_proj, &d.up_proj], x, [g, u], pool);
19074            for i in 0..inter {
19075                g[i] = Act::Silu.combine(g[i], 1.0);
19076            }
19077            keep_top_k(g, gate_topk());
19078            for i in 0..inter {
19079                g[i] *= u[i];
19080            }
19081        } else if d.act == Act::Silu && {
19082            let _prof = crate::cpuprof::time(crate::cpuprof::Slot::FfnGateUp);
19083            QTensor::matvec_silu_mul(&d.gate_proj, &d.up_proj, x, g, pool)
19084        } {
19085            // g now holds silu(gate)·up directly.
19086        } else {
19087            u.resize(inter, 0.0);
19088            // Multi-matrix job: gate+up under one pool dispatch.
19089            let _prof = crate::cpuprof::time(crate::cpuprof::Slot::FfnGateUp);
19090            QTensor::matvec_many([&d.gate_proj, &d.up_proj], x, [g, u], pool);
19091            let _c = crate::cpuprof::time(crate::cpuprof::Slot::FfnCombine);
19092            for i in 0..inter {
19093                g[i] = d.act.combine(g[i], u[i]);
19094            }
19095        }
19096        // DTG-MA bake probe (Patent 2): accumulate this layer's
19097        // per-neuron activation mass while a probe pass is active.
19098        // `CMF_FFN_PROBE_TOPK=k` switches the statistic from mass to a
19099        // HIT COUNT — how many tokens rank the neuron in their own top
19100        // k. Mass asks "how loud is this neuron overall", the count
19101        // asks "how often does this task actually need it", and the two
19102        // rank neurons differently whenever a few tokens are loud.
19103        FFN_PROBE.with(|pr| {
19104            if let Some(acc) = pr.borrow_mut().as_mut() {
19105                let li = crate::gpu::cur_layer();
19106                if li >= 0 {
19107                    if let Some(row) = acc.get_mut(li as usize) {
19108                        match probe_topk() {
19109                            0 if probe_sq() => {
19110                                for (a, &v) in row.iter_mut().zip(g.iter()) {
19111                                    *a += (v as f64) * (v as f64);
19112                                }
19113                            }
19114                            0 if probe_signed() => {
19115                                for (a, &v) in row.iter_mut().zip(g.iter()) {
19116                                    *a += v as f64;
19117                                }
19118                            }
19119                            0 => {
19120                                for (a, &v) in row.iter_mut().zip(g.iter()) {
19121                                    *a += (v as f64).abs();
19122                                }
19123                            }
19124                            k => {
19125                                let n = g.len();
19126                                let k = k.min(n);
19127                                let mut mag: Vec<f32> = g.iter().map(|v| v.abs()).collect();
19128                                let (_, kth, _) = mag.select_nth_unstable_by(k - 1, |a, b| {
19129                                    b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
19130                                });
19131                                let thr = *kth;
19132                                for (a, &v) in row.iter_mut().zip(g.iter()) {
19133                                    if v.abs() >= thr {
19134                                        *a += 1.0;
19135                                    }
19136                                }
19137                            }
19138                        }
19139                    }
19140                }
19141            }
19142        });
19143        if oracle_topk() > 0 {
19144            keep_top_k(g, oracle_topk());
19145        }
19146        {
19147            let li = crate::gpu::cur_layer();
19148            if li >= 0 {
19149                adump_row(li as usize, g);
19150            }
19151        }
19152        let mut out = attention::take_buf(d.down_proj.rows());
19153        let _prof = crate::cpuprof::time(crate::cpuprof::Slot::FfnDown);
19154        d.down_proj.matvec(g, &mut out, pool);
19155        out
19156    })
19157}
19158
19159/// Online accumulators for the AWNP refit of a narrowed FFN.
19160///
19161/// The refit needs `Gss = A_SᵀA_S` and `YA = YᵀA_S` per layer, where `A_S`
19162/// are the calibration activations of the KEPT neurons and `Y` the full
19163/// FFN output. Both are small enough to hold; the thing that is not is
19164/// the activations they are built from — a 27B layer would dump a
19165/// gigabyte per thousand tokens. So they are accumulated as the
19166/// calibration runs and written once at the end.
19167///
19168/// `CMF_FFN_REFIT=<dir>` holds `support.<L>.u32` (a u32 count then the
19169/// kept indices) for every layer to accumulate; `CMF_FFN_REFIT_FROM/TO`
19170/// bound the layer span so the accumulators fit in RAM.
19171pub struct RefitAcc {
19172    pub support: Vec<u32>,
19173    pub gss: Vec<f32>,
19174    pub ya: Vec<f32>,
19175    pub hidden: usize,
19176    pub tokens: u64,
19177    /// Activations staged transposed ([ns, t] and [hidden, t]) until the
19178    /// batch is worth a GEMM. The product costs `ns²` to move and add
19179    /// REGARDLESS of how many tokens went into it, so folding 16 chunks
19180    /// into one call cuts that cost 16× — it was 15 TB of traffic per
19181    /// calibration pass at one call per 256 tokens.
19182    pub buf_g: Vec<f32>,
19183    pub buf_o: Vec<f32>,
19184    pub buf_t: usize,
19185}
19186
19187/// The product buffer is SHARED across layers — one 473 MB allocation,
19188/// not one per layer (that was 30 GB of nothing on a 64-layer model).
19189/// It lives under the same lock as the accumulators.
19190type RefitState = (std::collections::HashMap<usize, RefitAcc>, Vec<f32>);
19191
19192static REFIT: std::sync::OnceLock<Option<(String, std::sync::Mutex<RefitState>)>> =
19193    std::sync::OnceLock::new();
19194
19195/// Is an FFN probe accumulator installed on this thread? The fused GPU
19196/// FFN must decline while one is, or the probe silently measures zero.
19197fn ffn_probe_active() -> bool {
19198    FFN_PROBE.with(|p| p.borrow().is_some())
19199}
19200
19201fn refit_dir() -> Option<&'static (String, std::sync::Mutex<RefitState>)> {
19202    REFIT
19203        .get_or_init(|| {
19204            std::env::var("CMF_FFN_REFIT").ok().map(|d| {
19205                (
19206                    d,
19207                    std::sync::Mutex::new((std::collections::HashMap::new(), Vec::new())),
19208                )
19209            })
19210        })
19211        .as_ref()
19212}
19213
19214/// Accumulate one prefill panel into the layer's refit statistics.
19215fn refit_accumulate(
19216    li: usize,
19217    g: &[f32],
19218    b: usize,
19219    inter: usize,
19220    out: &[f32],
19221    hidden: usize,
19222    pool: Option<&Pool>,
19223) {
19224    let Some((dir, map)) = refit_dir() else {
19225        return;
19226    };
19227    static SPAN: std::sync::OnceLock<(usize, usize)> = std::sync::OnceLock::new();
19228    let (from, to) = *SPAN.get_or_init(|| {
19229        let g = |k: &str, d: usize| {
19230            std::env::var(k)
19231                .ok()
19232                .and_then(|v| v.parse().ok())
19233                .unwrap_or(d)
19234        };
19235        (
19236            g("CMF_FFN_REFIT_FROM", 0),
19237            g("CMF_FFN_REFIT_TO", usize::MAX),
19238        )
19239    });
19240    if li < from || li > to {
19241        return;
19242    }
19243    let mut guard = map.lock().unwrap();
19244    let (map, shared) = &mut *guard;
19245    let acc = match map.entry(li) {
19246        std::collections::hash_map::Entry::Occupied(e) => e.into_mut(),
19247        std::collections::hash_map::Entry::Vacant(e) => {
19248            let path = format!("{dir}/support.{li}.u32");
19249            let Ok(bytes) = std::fs::read(&path) else {
19250                eprintln!("refit: no {path} — layer {li} skipped");
19251                return;
19252            };
19253            let n = u32::from_le_bytes(bytes[0..4].try_into().unwrap()) as usize;
19254            let support: Vec<u32> = bytes[4..4 + n * 4]
19255                .chunks_exact(4)
19256                .map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]]))
19257                .collect();
19258            eprintln!(
19259                "refit: layer {li} support {n} ({:.0} MB of accumulator)",
19260                (n * n + hidden * n) as f64 * 4.0 / 1e6
19261            );
19262            e.insert(RefitAcc {
19263                gss: vec![0.0; n * n],
19264                ya: vec![0.0; hidden * n],
19265                buf_g: Vec::new(),
19266                buf_o: Vec::new(),
19267                buf_t: 0,
19268                support,
19269                hidden,
19270                tokens: 0,
19271            })
19272        }
19273    };
19274    let ns = acc.support.len();
19275    // Stage this chunk transposed; the GEMM fires once the batch is full.
19276    let cap = refit_batch();
19277    if acc.buf_g.is_empty() {
19278        acc.buf_g = vec![0.0; ns * cap];
19279        acc.buf_o = vec![0.0; hidden * cap];
19280    }
19281    let take = b.min(cap - acc.buf_t);
19282    for t in 0..take {
19283        let col = acc.buf_t + t;
19284        for (j, &n) in acc.support.iter().enumerate() {
19285            acc.buf_g[j * cap + col] = g[t * inter + n as usize];
19286        }
19287        for h in 0..hidden {
19288            acc.buf_o[h * cap + col] = out[t * hidden + h];
19289        }
19290    }
19291    acc.buf_t += take;
19292    acc.tokens += take as u64;
19293    if acc.buf_t < cap {
19294        return;
19295    }
19296    let bt = acc.buf_t;
19297    acc.buf_t = 0;
19298    // The GEMM WRITES its C (it zeroes the accumulators it uses), so the
19299    // chunk product lands in scratch and is added on — the one thing that
19300    // silently turns a Gram over 13 000 tokens into a Gram over 256.
19301    // Both products are `C[n, m] += X[n, b] · Yᵀ[b, m]` with X and Y
19302    // stored row-major [·, b] — exactly `gemm_nt_f32`'s shape, so the
19303    // card does them when it is up (this is the whole calibration's
19304    // cost: O(|S|²) per token, 2.9 PFLOP for a 27B pass). The tiled CPU
19305    // loop stays as the fallback. Neither accumulates, so the product
19306    // lands in scratch and is added on.
19307    let RefitAcc {
19308        gss,
19309        ya,
19310        buf_g,
19311        buf_o,
19312        ..
19313    } = acc;
19314    let need = (ns * ns).max(hidden * ns);
19315    if shared.len() < need {
19316        shared.resize(need, 0.0);
19317    }
19318    let scratch = &mut shared[..];
19319    let _ = bt;
19320    if crate::gpu::gemm_nt_f32_transient(buf_g, buf_g, &mut scratch[..ns * ns], ns, cap, ns) {
19321        add_into(gss, &scratch[..ns * ns], pool);
19322        if crate::gpu::gemm_nt_f32_transient(
19323            buf_o,
19324            buf_g,
19325            &mut scratch[..hidden * ns],
19326            hidden,
19327            cap,
19328            ns,
19329        ) {
19330            add_into(ya, &scratch[..hidden * ns], pool);
19331        } else {
19332            accum_outer_t(ya, hidden, ns, cap, buf_o, buf_g, pool);
19333        }
19334    } else {
19335        accum_outer_t(gss, ns, ns, cap, buf_g, buf_g, pool);
19336        accum_outer_t(ya, hidden, ns, cap, buf_o, buf_g, pool);
19337    }
19338    // No zeroing: the batch is always filled exactly (cap is a multiple
19339    // of the prefill chunk), and a memset of 178 MB a layer would cost
19340    // more than the GEMM.
19341}
19342
19343/// `CMF_FFN_REFIT_BATCH` — tokens staged before each GEMM (default 4096).
19344fn refit_batch() -> usize {
19345    static B: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
19346    *B.get_or_init(|| {
19347        std::env::var("CMF_FFN_REFIT_BATCH")
19348            .ok()
19349            .and_then(|v| v.parse().ok())
19350            .unwrap_or(4096)
19351    })
19352}
19353
19354/// `c[m, n] += Σ_t left[m, t]·right[n, t]` — both operands transposed,
19355/// the CPU fallback for the staged batch.
19356fn accum_outer_t(
19357    c: &mut [f32],
19358    m: usize,
19359    n: usize,
19360    b: usize,
19361    left: &[f32],
19362    right: &[f32],
19363    pool: Option<&Pool>,
19364) {
19365    let ptr = SendMut(c.as_mut_ptr());
19366    let body = |i: usize| {
19367        let ptr = &ptr;
19368        let row = unsafe { std::slice::from_raw_parts_mut(ptr.0.add(i * n), n) };
19369        for t in 0..b {
19370            let a = left[i * b + t];
19371            if a == 0.0 {
19372                continue;
19373            }
19374            for (j, o) in row.iter_mut().enumerate() {
19375                *o += a * right[j * b + t];
19376            }
19377        }
19378    };
19379    match pool {
19380        Some(p) if m > 1 => p.run_rows(m, &|s, e| {
19381            for i in s..e {
19382                body(i);
19383            }
19384        }),
19385        _ => {
19386            for i in 0..m {
19387                body(i);
19388            }
19389        }
19390    }
19391}
19392
19393/// `dst += src`, spread over the pool — at 118 M floats a layer this is
19394/// not a loop to leave on one core.
19395fn add_into(dst: &mut [f32], src: &[f32], pool: Option<&Pool>) {
19396    let n = dst.len().min(src.len());
19397    match pool {
19398        Some(p) if n >= 1 << 16 => {
19399            let ptr = SendMut(dst.as_mut_ptr());
19400            let f = |s: usize, e: usize| {
19401                let ptr = &ptr;
19402                for blk in s..e {
19403                    let (a, b) = (blk * 4096, ((blk + 1) * 4096).min(n));
19404                    for i in a..b {
19405                        unsafe { *ptr.0.add(i) += src[i] };
19406                    }
19407                }
19408            };
19409            p.run_rows(n.div_ceil(4096), &f);
19410        }
19411        _ => {
19412            for (d, v) in dst.iter_mut().zip(&src[..n]) {
19413                *d += *v;
19414            }
19415        }
19416    }
19417}
19418
19419/// `c[m, n] += Σ_t left[t, m]·right[t, n]`, with `left` stored [m, t] and
19420/// `right` [t, n]. Tiled over the rows of `c` so a tile stays in cache
19421/// while each token's `right` row streams past it once, and parallel
19422/// over tiles.
19423fn accum_outer(
19424    c: &mut [f32],
19425    m: usize,
19426    n: usize,
19427    b: usize,
19428    left: &[f32],
19429    right: &[f32],
19430    pool: Option<&Pool>,
19431) {
19432    const TILE: usize = 32;
19433    let tiles = m.div_ceil(TILE);
19434    let cp = SendMut(c.as_mut_ptr());
19435    let body = |ti: usize| {
19436        let cp = &cp;
19437        let i0 = ti * TILE;
19438        let i1 = (i0 + TILE).min(m);
19439        for t in 0..b {
19440            let r = &right[t * n..t * n + n];
19441            for i in i0..i1 {
19442                let a = left[i * b + t];
19443                if a == 0.0 {
19444                    continue;
19445                }
19446                // SAFETY: tiles partition c's rows; workers never overlap.
19447                let row = unsafe { std::slice::from_raw_parts_mut(cp.0.add(i * n), n) };
19448                for (o, v) in row.iter_mut().zip(r) {
19449                    *o += a * *v;
19450                }
19451            }
19452        }
19453    };
19454    match pool {
19455        Some(p) if tiles > 1 => p.run_rows(tiles, &|s, e| {
19456            for ti in s..e {
19457                body(ti);
19458            }
19459        }),
19460        _ => {
19461            for ti in 0..tiles {
19462                body(ti);
19463            }
19464        }
19465    }
19466}
19467
19468/// Write what the calibration accumulated: `gss.<L>.f32` and `ya.<L>.f32`.
19469pub fn refit_flush() -> usize {
19470    let Some((dir, map)) = refit_dir() else {
19471        return 0;
19472    };
19473    let guard = map.lock().unwrap();
19474    let mut n = 0;
19475    for (li, acc) in guard.0.iter() {
19476        // A silently truncated write here is a Gram that reshapes to
19477        // nothing an hour later — say it out loud instead.
19478        let w = |name: &str, v: &[f32]| {
19479            let path = format!("{dir}/{name}.{li}.f32");
19480            let bytes: Vec<u8> = v.iter().flat_map(|x| x.to_le_bytes()).collect();
19481            match std::fs::write(&path, &bytes) {
19482                Ok(()) => {}
19483                Err(e) => eprintln!(
19484                    "refit: FAILED to write {path} ({} MB): {e}",
19485                    bytes.len() / 1_000_000
19486                ),
19487            }
19488        };
19489        w("gss", &acc.gss);
19490        w("ya", &acc.ya);
19491        println!(
19492            "refit L{li}: {} support, {} tokens, hidden {}",
19493            acc.support.len(),
19494            acc.tokens,
19495            acc.hidden
19496        );
19497        n += 1;
19498    }
19499    n
19500}
19501
19502/// `CMF_FFN_ADUMP=<prefix>` — append every probed token's FFN activation
19503/// row to `<prefix>.<layer>.f16`. The co-activation record: which
19504/// neurons fire together, which is what a tube has to group if a token
19505/// is ever going to open one tube instead of sixteen.
19506fn adump_row(li: usize, g: &[f32]) {
19507    use std::io::Write as _;
19508    static FILES: std::sync::OnceLock<
19509        Option<(
19510            String,
19511            std::sync::Mutex<std::collections::HashMap<usize, std::fs::File>>,
19512        )>,
19513    > = std::sync::OnceLock::new();
19514    let Some((prefix, map)) = FILES
19515        .get_or_init(|| {
19516            std::env::var("CMF_FFN_ADUMP")
19517                .ok()
19518                .map(|p| (p, std::sync::Mutex::new(std::collections::HashMap::new())))
19519        })
19520        .as_ref()
19521    else {
19522        return;
19523    };
19524    // `CMF_FFN_ADUMP_FROM/_TO` narrow the dump to a layer span, so a big
19525    // calibration run fits on disk in a few passes instead of one.
19526    static SPAN: std::sync::OnceLock<(usize, usize)> = std::sync::OnceLock::new();
19527    let (from, to) = *SPAN.get_or_init(|| {
19528        let g = |k: &str, d: usize| {
19529            std::env::var(k)
19530                .ok()
19531                .and_then(|v| v.parse().ok())
19532                .unwrap_or(d)
19533        };
19534        (
19535            g("CMF_FFN_ADUMP_FROM", 0),
19536            g("CMF_FFN_ADUMP_TO", usize::MAX),
19537        )
19538    });
19539    if li < from || li > to {
19540        return;
19541    }
19542    let mut map = map.lock().unwrap();
19543    let f = map.entry(li).or_insert_with(|| {
19544        std::fs::File::create(format!("{prefix}.{li}.f16")).expect("adump file")
19545    });
19546    let mut bytes = Vec::with_capacity(g.len() * 2);
19547    for v in g {
19548        bytes.extend_from_slice(&cortiq_core::quant::f32_to_f16(*v).to_le_bytes());
19549    }
19550    let _ = f.write_all(&bytes);
19551}
19552
19553/// `CMF_FFN_ORACLE_TOPK` — keep only the k largest |silu(g)·u| of each
19554/// token and zero the rest. Not a serving mode: it is the CEILING of
19555/// contextual sparsity — what a per-token router would be chasing —
19556/// measured by cheating, since the selection reads the very activations
19557/// it would have to predict.
19558fn oracle_topk() -> usize {
19559    if exact_mtp_calibration_active() {
19560        return 0;
19561    }
19562    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
19563    *K.get_or_init(|| {
19564        std::env::var("CMF_FFN_ORACLE_TOPK")
19565            .ok()
19566            .and_then(|v| v.parse().ok())
19567            .unwrap_or(0)
19568    })
19569}
19570
19571/// `CMF_FFN_GATE_TOPK` — the REALIZABLE cousin of the oracle: rank the
19572/// neurons by their gate alone (which the kernel has computed anyway
19573/// before it reads `up`), keep the k best, and drop the rest. Every
19574/// dropped neuron's `up` row and `down` column stay unread, so this is
19575/// the sparsity a serving path can actually take without a router.
19576fn gate_topk() -> usize {
19577    if exact_mtp_calibration_active() {
19578        return 0;
19579    }
19580    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
19581    *K.get_or_init(|| {
19582        std::env::var("CMF_FFN_GATE_TOPK")
19583            .ok()
19584            .and_then(|v| v.parse().ok())
19585            .unwrap_or(0)
19586    })
19587}
19588
19589/// `CMF_FFN_GATE_BLOCK` — select in blocks of B neurons instead of one
19590/// by one. A scattered per-neuron choice cannot be read efficiently (a
19591/// row at a time, no prefetch runway); a block of 32 is a contiguous
19592/// 32-row slab of `up` and of the transposed `down`, which the ordinary
19593/// kernels stream. The question the measurement answers is what the
19594/// block costs in quality.
19595fn gate_block() -> usize {
19596    static B: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
19597    *B.get_or_init(|| {
19598        std::env::var("CMF_FFN_GATE_BLOCK")
19599            .ok()
19600            .and_then(|v| v.parse().ok())
19601            .unwrap_or(1)
19602    })
19603}
19604
19605/// Zero all but the `k` largest BLOCKS (by summed square) of a row.
19606fn keep_top_blocks(g: &mut [f32], keep_n: usize, block: usize) {
19607    let n = g.len();
19608    let nb = n.div_ceil(block);
19609    let kb = (keep_n.div_ceil(block)).clamp(1, nb);
19610    if kb >= nb {
19611        return;
19612    }
19613    let mut score: Vec<f32> = (0..nb)
19614        .map(|b| {
19615            g[b * block..((b + 1) * block).min(n)]
19616                .iter()
19617                .map(|v| v * v)
19618                .sum::<f32>()
19619        })
19620        .collect();
19621    let mut ord = score.clone();
19622    let (_, kth, _) = ord.select_nth_unstable_by(kb - 1, |a, b| {
19623        b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
19624    });
19625    let thr = *kth;
19626    for b in 0..nb {
19627        if score[b] < thr {
19628            g[b * block..((b + 1) * block).min(n)].fill(0.0);
19629        }
19630    }
19631    score.clear();
19632}
19633
19634/// Zero all but the `k` largest magnitudes of one token's activation row.
19635fn keep_top_k(g: &mut [f32], k: usize) {
19636    if gate_block() > 1 {
19637        return keep_top_blocks(g, k, gate_block());
19638    }
19639    let n = g.len();
19640    if k == 0 || k >= n {
19641        return;
19642    }
19643    let mut mag: Vec<f32> = g.iter().map(|v| v.abs()).collect();
19644    let (_, kth, _) = mag.select_nth_unstable_by(k - 1, |a, b| {
19645        b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
19646    });
19647    let thr = *kth;
19648    for v in g.iter_mut() {
19649        if v.abs() < thr {
19650            *v = 0.0;
19651        }
19652    }
19653}
19654
19655/// `CMF_FFN_PROBE_SQ` — accumulate Σa², so the dump divided by the token
19656/// count and square-rooted is the RMS activation trace Patent 12 weights
19657/// its matrices by.
19658fn probe_sq() -> bool {
19659    static S: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
19660    *S.get_or_init(|| std::env::var("CMF_FFN_PROBE_SQ").is_ok())
19661}
19662
19663/// `CMF_FFN_PROBE_SIGNED` — accumulate the SIGNED activation sum
19664/// instead of its magnitude: what a dropped neuron contributes ON
19665/// AVERAGE, which is the bias a narrowed FFN can add back for free.
19666fn probe_signed() -> bool {
19667    static S: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
19668    *S.get_or_init(|| std::env::var("CMF_FFN_PROBE_SIGNED").is_ok())
19669}
19670
19671/// `CMF_FFN_MEANFILL=<file>` — a masked-out neuron contributes its MEAN
19672/// activation instead of zero (`u32 layers, u32 inter, f32[…]`, the mass
19673/// dump layout, holding per-neuron means). Dropping a neuron outright
19674/// also drops its average contribution, which shifts the layer output by
19675/// a constant; filling the mean back is one add per layer and costs no
19676/// bytes off the bus. This is the measurement arm — in a tube file the
19677/// same correction ships as a per-task bias vector.
19678fn meanfill() -> Option<&'static (usize, Vec<f32>)> {
19679    static M: std::sync::OnceLock<Option<(usize, Vec<f32>)>> = std::sync::OnceLock::new();
19680    M.get_or_init(|| {
19681        let p = std::env::var("CMF_FFN_MEANFILL").ok()?;
19682        let b = std::fs::read(&p).ok()?;
19683        let inter = u32::from_le_bytes(b[4..8].try_into().ok()?) as usize;
19684        let vals: Vec<f32> = b[8..]
19685            .chunks_exact(4)
19686            .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
19687            .collect();
19688        eprintln!("meanfill: {} value(s), inter {inter}", vals.len());
19689        Some((inter, vals))
19690    })
19691    .as_ref()
19692}
19693
19694/// `CMF_FFN_PROBE_TOPK` — 0 (default) = accumulate mass, k>0 = count
19695/// how often a neuron lands in a token's top k.
19696fn probe_topk() -> usize {
19697    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
19698    *K.get_or_init(|| {
19699        std::env::var("CMF_FFN_PROBE_TOPK")
19700            .ok()
19701            .and_then(|v| v.parse().ok())
19702            .unwrap_or(0)
19703    })
19704}
19705
19706thread_local! {
19707    /// DTG-MA activation probe: per-layer per-neuron Σ|silu(g)·u|
19708    /// accumulator, alive only during `Pipeline::probe_ffn_mass`.
19709    static FFN_PROBE: std::cell::RefCell<Option<Vec<Vec<f64>>>> =
19710        const { std::cell::RefCell::new(None) };
19711}
19712
19713/// Per-token structured sparsity, paid for in bytes.
19714///
19715/// The gate is the cheapest third of an FFN and it already says which
19716/// neurons matter: `silu(gate)` near zero means the neuron contributes
19717/// nothing whatever `up` says. So compute every gate, keep the `k`
19718/// loudest, and read ONLY those neurons' `up` rows and `down` rows —
19719/// the latter needs `down_proj` stored transposed, otherwise a neuron's
19720/// down weights are a strided column and "reading only those" costs a
19721/// full cache line each.
19722///
19723/// Returns `None` when the file has no transposed `down` (the caller
19724/// then runs the ordinary dense path).
19725fn dense_ffn_dynamic(d: &DenseFfn, x: &[f32], pool: Option<&Pool>, k: usize) -> Option<Vec<f32>> {
19726    // The scatter path reads individual rows/columns and cannot express the
19727    // per-matrix signed FWHT boundary.  Let the descriptor-aware dense path
19728    // handle Prism files rather than silently running an unrotated sparse
19729    // approximation.
19730    if d.gate_proj.has_prism_contract()
19731        || d.up_proj.has_prism_contract()
19732        || d.down_proj.has_prism_contract()
19733    {
19734        return None;
19735    }
19736    let dt = d.down_t.as_ref()?;
19737    let inter = d.gate_proj.rows();
19738    let hidden = dt.cols();
19739    if k == 0 || k >= inter || d.act != Act::Silu {
19740        return None;
19741    }
19742    DYN_SCRATCH.with(|sc| {
19743        let mut sc = sc.borrow_mut();
19744        let DynScratch {
19745            g,
19746            mag,
19747            live,
19748            parts,
19749        } = &mut *sc;
19750        g.resize(inter, 0.0);
19751        d.gate_proj.matvec(x, g, pool);
19752        for v in g.iter_mut() {
19753            *v = inference::silu(*v);
19754        }
19755        // The k-th largest |silu(gate)| is the threshold; ties keep more,
19756        // which is the safe side.
19757        mag.clear();
19758        mag.extend(g.iter().map(|v| v.abs()));
19759        let (_, kth, _) = mag.select_nth_unstable_by(k - 1, |a, b| {
19760            b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
19761        });
19762        let thr = *kth;
19763        live.clear();
19764        live.extend((0..inter as u32).filter(|&n| g[n as usize].abs() >= thr));
19765        let mut out = vec![0.0f32; hidden];
19766        match pool {
19767            Some(p) if live.len() >= 64 => {
19768                let nw = p.n_workers() + 1;
19769                parts.clear();
19770                parts.resize(nw * hidden, 0.0);
19771                let ptr = SendMut(parts.as_mut_ptr());
19772                let n = live.len();
19773                let live_ref: &[u32] = live;
19774                let g_ref: &[f32] = g;
19775                p.run(&|w, workers| {
19776                    let chunk = n.div_ceil(workers);
19777                    let (s, e) = (w * chunk, ((w + 1) * chunk).min(n));
19778                    if s >= e {
19779                        return;
19780                    }
19781                    WORKER_SCRATCH.with(|ws| {
19782                        let mut ws = ws.borrow_mut();
19783                        let [scratch, acc] = &mut *ws;
19784                        scratch.resize(hidden.max(x.len()), 0.0);
19785                        acc.clear();
19786                        acc.resize(hidden, 0.0);
19787                        for (o, &nrm) in live_ref[s..e].iter().enumerate() {
19788                            // One neuron of runway: the next row's lines
19789                            // start moving while this one is multiplied.
19790                            if let Some(&nx) = live_ref[s..e].get(o + 1) {
19791                                d.up_proj.prefetch_row(nx as usize);
19792                                dt.prefetch_row(nx as usize);
19793                            }
19794                            let idx = nrm as usize;
19795                            let up = d.up_proj.row_dot(idx, x, scratch);
19796                            let a = g_ref[idx] * up;
19797                            if a != 0.0 {
19798                                dt.add_row_scaled(idx, a, acc, scratch);
19799                            }
19800                        }
19801                        for (j, v) in acc.iter().enumerate() {
19802                            unsafe { *ptr.at(w * hidden + j) = *v };
19803                        }
19804                    });
19805                });
19806                for w in 0..nw {
19807                    for (j, o) in out.iter_mut().enumerate() {
19808                        *o += parts[w * hidden + j];
19809                    }
19810                }
19811            }
19812            _ => {
19813                WORKER_SCRATCH.with(|ws| {
19814                    let mut ws = ws.borrow_mut();
19815                    let [scratch, _acc] = &mut *ws;
19816                    scratch.resize(hidden.max(x.len()), 0.0);
19817                    for &nrm in live.iter() {
19818                        let idx = nrm as usize;
19819                        let up = d.up_proj.row_dot(idx, x, scratch);
19820                        let a = g[idx] * up;
19821                        if a != 0.0 {
19822                            dt.add_row_scaled(idx, a, &mut out, scratch);
19823                        }
19824                    }
19825                });
19826            }
19827        }
19828        Some(out)
19829    })
19830}
19831
19832/// Caller-side scratch of the dynamic path — one allocation per thread,
19833/// not one per layer per token (that alone cost a third of the decode).
19834struct DynScratch {
19835    g: Vec<f32>,
19836    mag: Vec<f32>,
19837    live: Vec<u32>,
19838    parts: Vec<f32>,
19839}
19840
19841thread_local! {
19842    static DYN_SCRATCH: std::cell::RefCell<DynScratch> = const {
19843        std::cell::RefCell::new(DynScratch {
19844            g: Vec::new(),
19845            mag: Vec::new(),
19846            live: Vec::new(),
19847            parts: Vec::new(),
19848        })
19849    };
19850    /// Pool-worker scratch: the row buffer and this worker's partial sum.
19851    static WORKER_SCRATCH: std::cell::RefCell<[Vec<f32>; 2]> =
19852        const { std::cell::RefCell::new([Vec::new(), Vec::new()]) };
19853}
19854
19855/// `dense_ffn_cpu` with a per-visit mask landing on the activations —
19856/// the masked-inference fast path's decode arm. Full fused quant
19857/// compute, closed neurons zeroed before down: arithmetically the
19858/// pruned network, no dequant, no weight bytes touched.
19859fn dense_ffn_masked(d: &DenseFfn, x: &[f32], pool: Option<&Pool>, mask_row: &[u8]) -> Vec<f32> {
19860    let inter = d.gate_proj.rows();
19861    FFN_SCRATCH.with(|s| {
19862        let mut s = s.borrow_mut();
19863        let [g, u, ..] = &mut *s;
19864        g.resize(inter, 0.0);
19865        if d.act == Act::Silu && QTensor::matvec_silu_mul(&d.gate_proj, &d.up_proj, x, g, pool) {
19866            // g holds silu(gate)·up.
19867        } else {
19868            u.resize(inter, 0.0);
19869            QTensor::matvec_many([&d.gate_proj, &d.up_proj], x, [g, u], pool);
19870            for i in 0..inter {
19871                g[i] = d.act.combine(g[i], u[i]);
19872            }
19873        }
19874        zero_masked_cols(g, 1, inter, mask_row);
19875        let mut out = attention::take_buf(d.down_proj.rows());
19876        d.down_proj.matvec(g, &mut out, pool);
19877        out
19878    })
19879}
19880
19881/// Dense FFN as one GPU submission via the MoE block path (single
19882/// expert, weight 1.0): gate → silu·up → down chained in one command
19883/// buffer, intermediate activations device-resident. None → weights
19884/// not q8-mapped in the primary shard / over the VRAM budget / backend
19885/// refusal → honest CPU path.
19886fn dense_ffn_gpu(d: &DenseFfn, x: &[f32], _pool: Option<&Pool>) -> Option<Vec<f32>> {
19887    if d.gate_proj.has_prism_contract()
19888        || d.up_proj.has_prism_contract()
19889        || d.down_proj.has_prism_contract()
19890    {
19891        return None;
19892    }
19893    // The GPU block applies the standard graph activations plus the exact
19894    // MiniMax-M3 OAI contract; any other activation stays on the CPU.
19895    d.act.moe_job_graph_contract()?;
19896    // Threshold: tiny FFNs are not worth a submission (q1 excepted —
19897    // see the caller's gate).
19898    if d.gate_proj.rows() < crate::gpu::min_rows() && !d.gate_proj.is_q1() {
19899        return None;
19900    }
19901    let mut jobs: Vec<crate::gpu::MoeJob> = Vec::with_capacity(1);
19902    let mut model_ref = None;
19903    moe_push_job(d, x, 1.0, &mut jobs, &mut model_ref)?;
19904    let model = model_ref?;
19905    let hidden = jobs[0].down.1;
19906    let mut out = attention::take_buf(hidden);
19907    if crate::gpu::moe_block(&model, &jobs, &mut out) {
19908        Some(out)
19909    } else {
19910        let mut out = out;
19911        attention::recycle_buf(&mut out);
19912        None
19913    }
19914}
19915
19916/// q8-mapped primary-shard tensor parts for a GPU job: q8_2f carries
19917/// its column field, q8_row runs with empty col slices (the backend
19918/// skips the multiply). Shared by the MoE block and the dense-FFN
19919/// single-job path.
19920#[allow(clippy::type_complexity)]
19921#[allow(clippy::type_complexity)]
19922pub(crate) fn moe_parts(
19923    t: &QTensor,
19924) -> Option<(
19925    &std::sync::Arc<cortiq_core::CmfModel>,
19926    usize,
19927    usize,
19928    usize,
19929    &[f32],
19930    &[f32],
19931    bool,
19932    bool,
19933    bool,
19934)> {
19935    match t {
19936        QTensor::Mapped {
19937            model,
19938            idx,
19939            dtype: dt @ (cortiq_core::TensorDtype::Q8_2f | cortiq_core::TensorDtype::Q8Row),
19940            rows,
19941            cols,
19942            row_scale,
19943            col_field,
19944            ..
19945        } if (*dt == cortiq_core::TensorDtype::Q8Row) || !col_field.is_empty() => Some((
19946            model, *idx, *rows, *cols, row_scale, col_field, false, false, false,
19947        )),
19948        // q1: tile-embedded scales — empty rs/col slices, raw xs.
19949        QTensor::Mapped {
19950            model,
19951            idx,
19952            dtype: cortiq_core::TensorDtype::Q1,
19953            rows,
19954            cols,
19955            ..
19956        } => Some((
19957            model,
19958            *idx,
19959            *rows,
19960            *cols,
19961            &[][..],
19962            &[][..],
19963            true,
19964            false,
19965            false,
19966        )),
19967        // q4_tiled: 18-byte tiles with embedded f16 scales — raw xs.
19968        QTensor::Mapped {
19969            model,
19970            idx,
19971            dtype: cortiq_core::TensorDtype::Q4Tiled,
19972            rows,
19973            cols,
19974            ..
19975        } => Some((
19976            model,
19977            *idx,
19978            *rows,
19979            *cols,
19980            &[][..],
19981            &[][..],
19982            false,
19983            true,
19984            false,
19985        )),
19986        // q4tp: same raw-xs contract, different stride and scale plane.
19987        QTensor::Mapped {
19988            model,
19989            idx,
19990            dtype: cortiq_core::TensorDtype::Q4TiledP,
19991            rows,
19992            cols,
19993            ..
19994        } => Some((
19995            model,
19996            *idx,
19997            *rows,
19998            *cols,
19999            &[][..],
20000            &[][..],
20001            false,
20002            true,
20003            false,
20004        )),
20005        // q2tp: the 2-bit expert plane of the mixed profile — q4 family
20006        // for stride bookkeeping, flagged q2 so the trio validation can
20007        // demand a q4tp down.
20008        QTensor::Mapped {
20009            model,
20010            idx,
20011            dtype: cortiq_core::TensorDtype::Q2TiledP,
20012            rows,
20013            cols,
20014            ..
20015        } => Some((
20016            model,
20017            *idx,
20018            *rows,
20019            *cols,
20020            &[][..],
20021            &[][..],
20022            false,
20023            true,
20024            true,
20025        )),
20026        _ => None,
20027    }
20028}
20029
20030/// The routed experts of a MoE layer as the Metal chunk prefill takes
20031/// them: f32 router rows and every expert's q4tp (gate, up, down) tensor
20032/// indices, uniform SiLU shapes. Only the plain routed form — no shared
20033/// expert, no per-expert scale, no resonance or τ router, no router input
20034/// norm; the batched host walk keeps the rest. The routing itself stays the
20035/// host's (`moe_route`), so mask and selection bias need nothing here.
20036#[cfg(target_os = "macos")]
20037#[allow(clippy::type_complexity)]
20038fn chunk_moe_parts(
20039    m: &MoeFfn,
20040    hidden: usize,
20041) -> Option<(&[f32], Vec<(usize, usize, usize)>, bool)> {
20042    if m.shared.is_some()
20043        || m.per_expert_scale.is_some()
20044        || m.resonance.is_some()
20045        || m.route_tau.is_some()
20046        || m.router_input_norm
20047        || m.experts.is_empty()
20048        || m.top_k == 0
20049        || std::env::var("CMF_RMS_TRACE").is_ok()
20050        || std::env::var("CMF_ACT_DUMP").is_ok()
20051        || std::env::var("CMF_METAL_MOE_PREFILL").as_deref() == Ok("0")
20052    {
20053        return None;
20054    }
20055    let (rf, rr, rc) = m.router.f32_parts()?;
20056    if rr != m.experts.len() || rc != hidden {
20057        return None;
20058    }
20059    let inter = m.experts[0].gate_proj.rows();
20060    // All-q8_2f experts (Mellum2.1 q8_2f) ride the `moe_q8` GEMMs;
20061    // `CMF_METAL_MOE_Q8=0` keeps them on the host walk.
20062    let q8 = crate::gpu_metal::moe_q8::enabled() && m.experts[0].gate_proj.q8_2f_parts().is_some();
20063    let experts = m
20064        .experts
20065        .iter()
20066        .map(|e| {
20067            if e.act != Act::Silu
20068                || !e.segs.is_empty()
20069                || e.gate_proj.rows() != inter
20070                || e.up_proj.rows() != inter
20071                || e.down_proj.cols() != inter
20072                || e.gate_proj.cols() != hidden
20073                || e.down_proj.rows() != hidden
20074            {
20075                return None;
20076            }
20077            if q8 {
20078                let q8i = |t: &QTensor| t.q8_2f_parts().map(|p| p.0);
20079                return Some((q8i(&e.gate_proj)?, q8i(&e.up_proj)?, q8i(&e.down_proj)?));
20080            }
20081            Some((
20082                e.gate_proj.mapped_q4tp()?.1,
20083                e.up_proj.mapped_q4tp()?.1,
20084                e.down_proj.mapped_q4tp()?.1,
20085            ))
20086        })
20087        .collect::<Option<Vec<_>>>()?;
20088    Some((rf, experts, q8))
20089}
20090
20091/// Map a MoE onto the Metal token graph's contract: f32 router, a
20092/// shared expert (gated — Qwen — or ungated at weight 1 — DeepSeek-V3 /
20093/// HunYuan hy_v3), softmax or sigmoid scores with an optional selection
20094/// bias and routed scale, experts uniformly q4tp (or the mixed profile:
20095/// q2tp gate/up over a q4tp down). τ routers, masks, per-expert scales
20096/// and Gemma's router-input norm refuse here — those semantics stay on
20097/// the CPU path.
20098/// Map Flint's released ragged MoE into the native page graph.  This stays
20099/// separate from `metal_moe_graph_parts`: its six routed slots are Q2TP and
20100/// 512-wide, while its ungated shared SwiGLU is Q4TP and 1024-wide.  Feeding
20101/// the shared tuple into the uniform jobs descriptor would make the shader
20102/// use the routed stride on a different payload.
20103#[cfg(target_os = "macos")]
20104fn metal_flint_moe_graph_parts(
20105    m: &MoeFfn,
20106    hidden: usize,
20107    scratch_key: usize,
20108) -> Option<crate::gpu::GpuFlintMoe<'_>> {
20109    use std::sync::Arc;
20110
20111    const HIDDEN: usize = 2_048;
20112    const ROUTED_INTER: usize = 512;
20113    const SHARED_INTER: usize = 1_024;
20114    const EXPERTS: usize = 256;
20115    const TOP_K: usize = 6;
20116
20117    if hidden != HIDDEN || !strict_flint_metal_pager_candidate(m) {
20118        return None;
20119    }
20120    let routed0 = m.experts.first()?;
20121    let (model, _) = routed0.gate_proj.mapped_q2tp()?;
20122    let (router, router_rows, router_cols) = m.router.f32_parts()?;
20123    if router_rows != EXPERTS || router_cols != HIDDEN {
20124        return None;
20125    }
20126    if m.expert_bias.as_ref().is_some_and(|b| b.len() != EXPERTS) {
20127        return None;
20128    }
20129    let same_q2 = |t: &QTensor| -> Option<usize> {
20130        let (owner, idx) = t.mapped_q2tp()?;
20131        Arc::ptr_eq(&owner, &model).then_some(idx)
20132    };
20133    let same_q4 = |t: &QTensor| -> Option<usize> {
20134        let (owner, idx) = t.mapped_q4tp()?;
20135        Arc::ptr_eq(&owner, &model).then_some(idx)
20136    };
20137    let routed = |e: &DenseFfn| -> Option<(usize, usize, usize)> {
20138        if e.act != Act::Silu
20139            || !e.segs.is_empty()
20140            || e.gate_proj.rows() != ROUTED_INTER
20141            || e.gate_proj.cols() != HIDDEN
20142            || e.up_proj.rows() != ROUTED_INTER
20143            || e.up_proj.cols() != HIDDEN
20144            || e.down_proj.rows() != HIDDEN
20145            || e.down_proj.cols() != ROUTED_INTER
20146        {
20147            return None;
20148        }
20149        Some((
20150            same_q2(&e.gate_proj)?,
20151            same_q2(&e.up_proj)?,
20152            same_q4(&e.down_proj)?,
20153        ))
20154    };
20155    let shared = |e: &DenseFfn| -> Option<(usize, usize, usize)> {
20156        if e.act != Act::Silu
20157            || !e.segs.is_empty()
20158            || e.gate_proj.rows() != SHARED_INTER
20159            || e.gate_proj.cols() != HIDDEN
20160            || e.up_proj.rows() != SHARED_INTER
20161            || e.up_proj.cols() != HIDDEN
20162            || e.down_proj.rows() != HIDDEN
20163            || e.down_proj.cols() != SHARED_INTER
20164        {
20165            return None;
20166        }
20167        Some((
20168            same_q4(&e.gate_proj)?,
20169            same_q4(&e.up_proj)?,
20170            same_q4(&e.down_proj)?,
20171        ))
20172    };
20173    let experts = m.experts.iter().map(routed).collect::<Option<Vec<_>>>()?;
20174    if experts.len() != EXPERTS {
20175        return None;
20176    }
20177    let (shared_ffn, shared_gate) = m.shared.as_ref()?;
20178    if shared_gate.is_some() {
20179        return None;
20180    }
20181    Some(crate::gpu::GpuFlintMoe {
20182        router,
20183        bias: m.expert_bias.as_deref(),
20184        experts,
20185        scratch_key,
20186        shared: shared(shared_ffn)?,
20187        top_k: m.top_k,
20188        norm_topk: m.norm_topk_prob,
20189        route_scale: m.routed_scaling,
20190        sigmoid: m.router_sigmoid,
20191        sigmoid_denom_eps: m.sigmoid_denom_eps,
20192    })
20193}
20194
20195#[cfg(target_os = "macos")]
20196fn metal_moe_graph_parts(m: &MoeFfn, hidden: usize) -> Option<crate::gpu::GpuMoe<'_>> {
20197    if m.router_input_norm
20198        || m.route_tau.is_some()
20199        || m.mask.is_some()
20200        || m.per_expert_scale.is_some()
20201        || m.experts.is_empty()
20202        || m.top_k == 0
20203        || m.resonance.is_some()
20204    {
20205        return None;
20206    }
20207    // Without a shared expert the select kernel fills the k routed jobs
20208    // only (flag 32). Measured on Mellum2.1's softmax router; the sigmoid /
20209    // biased routers without one (LFM2-MoE) keep the CPU path until they
20210    // are measured too.
20211    let (sh, sg) = match &m.shared {
20212        Some((sh, sg)) => (Some(sh), sg.as_ref()),
20213        None if !m.router_sigmoid && m.expert_bias.is_none() => (None, None),
20214        None => return None,
20215    };
20216    let (rf, rr, rc) = m.router.f32_parts()?;
20217    if rr != m.experts.len() || rc != hidden {
20218        return None;
20219    }
20220    let shared_gated = sg.is_some();
20221    let sf = match sg {
20222        Some(sg) => {
20223            let (sf, sr, sc) = sg.f32_parts()?;
20224            if sr * sc != hidden {
20225                return None;
20226            }
20227            sf
20228        }
20229        // Ungated: the router's first row stands in for the gate matvec
20230        // (its logit is never read — the kernel pins weight 1).
20231        None => &rf[..hidden],
20232    };
20233    if let Some(b) = &m.expert_bias {
20234        if b.len() != m.experts.len() {
20235            return None;
20236        }
20237    }
20238    let inter = m.experts[0].gate_proj.rows();
20239    // The first expert's gate decides the profile; every trio (shared
20240    // included) must agree — the jobs ladder flips ONE kernel for all.
20241    let gu_q2 = m.experts[0].gate_proj.mapped_q2tp().is_some();
20242    // All-q8_2f routed experts (Mellum2.1 q8_2f: no shared expert, the
20243    // contract they were measured on) take the `moe_q8` kernels;
20244    // `CMF_METAL_MOE_Q8=0` keeps them off the graph (per-op path).
20245    let q8 = crate::gpu_metal::moe_q8::enabled()
20246        && m.shared.is_none()
20247        && m.experts[0].gate_proj.q8_2f_parts().is_some();
20248    let trio = |e: &DenseFfn| -> Option<(usize, usize, usize)> {
20249        if e.act != Act::Silu
20250            || e.gate_proj.rows() != inter
20251            || e.gate_proj.cols() != hidden
20252            || e.up_proj.rows() != inter
20253            || e.up_proj.cols() != hidden
20254            || e.down_proj.rows() != hidden
20255            || e.down_proj.cols() != inter
20256        {
20257            return None;
20258        }
20259        if q8 {
20260            let q8i = |t: &QTensor| t.q8_2f_parts().map(|p| p.0);
20261            return Some((q8i(&e.gate_proj)?, q8i(&e.up_proj)?, q8i(&e.down_proj)?));
20262        }
20263        let pick = |t: &QTensor| -> Option<usize> {
20264            if gu_q2 {
20265                t.mapped_q2tp().map(|(_, i)| i)
20266            } else {
20267                t.mapped_q4tp().map(|(_, i)| i)
20268            }
20269        };
20270        Some((
20271            pick(&e.gate_proj)?,
20272            pick(&e.up_proj)?,
20273            e.down_proj.mapped_q4tp().map(|(_, i)| i)?,
20274        ))
20275    };
20276    let experts = m.experts.iter().map(trio).collect::<Option<Vec<_>>>()?;
20277    let shared = match sh {
20278        Some(sh) => Some(trio(sh)?),
20279        None => None,
20280    };
20281    Some(crate::gpu::GpuMoe {
20282        router: rf,
20283        sgate: sf,
20284        experts,
20285        shared,
20286        n_exp: m.experts.len(),
20287        top_k: m.top_k,
20288        inter,
20289        norm_topk: m.norm_topk_prob,
20290        route_scale: m.routed_scaling,
20291        gu_q2,
20292        q8,
20293        sigmoid: m.router_sigmoid,
20294        sigmoid_denom_eps: m.sigmoid_denom_eps,
20295        bias: m.expert_bias.as_deref(),
20296        shared_gated,
20297    })
20298}
20299
20300/// `CMF_FORCE_IDS=<file>` (diagnostics): decode step n commits the n-th
20301/// whitespace-separated token id of the file instead of sampling, while the
20302/// logits are still computed (and dumped by `CMF_LOGIT_DUMP_ALL`) — one
20303/// backend teacher-forced along another's greedy sequence, so top-1
20304/// agreement can be counted past a near-tie divergence.
20305fn forced_ids_at(step: usize) -> Option<u32> {
20306    static IDS: std::sync::OnceLock<Vec<u32>> = std::sync::OnceLock::new();
20307    IDS.get_or_init(|| {
20308        std::env::var("CMF_FORCE_IDS")
20309            .ok()
20310            .and_then(|p| std::fs::read_to_string(p).ok())
20311            .map(|s| {
20312                s.split_whitespace()
20313                    .filter_map(|t| t.parse().ok())
20314                    .collect()
20315            })
20316            .unwrap_or_default()
20317    })
20318    .get(step)
20319    .copied()
20320}
20321
20322/// Build one gate/up/down GPU job from three tensors. `moe_push_job` is the
20323/// DenseFfn-shaped caller; architectures that keep their experts in their own
20324/// structs (DeepSeek-V4) come here directly.
20325pub(crate) fn moe_push_job_parts<'a>(
20326    gate: &'a QTensor,
20327    up: &'a QTensor,
20328    down: &'a QTensor,
20329    x: &[f32],
20330    w: f32,
20331    swiglu_limit: f32,
20332    jobs: &mut Vec<crate::gpu::MoeJob<'a>>,
20333    model_ref: &mut Option<std::sync::Arc<cortiq_core::CmfModel>>,
20334) -> Option<()> {
20335    use crate::qtensor::prescale;
20336    let (gm, gi, gr, gc, grs, gcf, gq1, gq4, gq2) = moe_parts(gate)?;
20337    let (_, ui, ur, uc, urs, ucf, uq1, uq4, uq2) = moe_parts(up)?;
20338    let (_, di, dr, dc, drs, dcf, dq1, dq4, dq2) = moe_parts(down)?;
20339    if gq1 != uq1 || uq1 != dq1 || gq4 != uq4 || uq4 != dq4 || gq2 != uq2 {
20340        return None; // mixed-dtype trio — honest CPU path
20341    }
20342    // The 2-bit profile is gate/up q2tp over a PLAIN q4tp down; any other
20343    // 2-bit arrangement stays on the CPU.
20344    if gq2 && (dq2 || !dq4 || down.mapped_q4tp().is_none()) {
20345        return None;
20346    }
20347    if !gq2 && dq2 {
20348        return None;
20349    }
20350    model_ref.get_or_insert_with(|| gm.clone());
20351    let dt = |cf: &[f32]| {
20352        if cf.is_empty() {
20353            cortiq_core::TensorDtype::Q8Row
20354        } else {
20355            cortiq_core::TensorDtype::Q8_2f
20356        }
20357    };
20358    jobs.push(crate::gpu::MoeJob {
20359        gate: (gi, gr, gc, grs),
20360        up: (ui, ur, uc, urs),
20361        down: (di, dr, dc, drs),
20362        xs_gate: prescale(x, gcf, dt(gcf)).into_owned(),
20363        xs_up: prescale(x, ucf, dt(ucf)).into_owned(),
20364        down_col: dcf,
20365        w,
20366        q1: gq1,
20367        q4t: gq4 && !gq2 && gate.mapped_q4tp().is_none(),
20368        q4tp: gq4 && (gq2 || gate.mapped_q4tp().is_some()),
20369        gu_q2: gq2,
20370        swiglu_limit,
20371        act: 0,
20372    });
20373    Some(())
20374}
20375
20376/// Build one gate/up/down GPU job (see `moe_parts`).
20377fn moe_push_job<'a>(
20378    d: &'a DenseFfn,
20379    x: &[f32],
20380    w: f32,
20381    jobs: &mut Vec<crate::gpu::MoeJob<'a>>,
20382    model_ref: &mut Option<std::sync::Arc<cortiq_core::CmfModel>>,
20383) -> Option<()> {
20384    use crate::qtensor::prescale;
20385    // The block's act(gate)·up pass takes an exact device contract.  For
20386    // ordinary SiLU/GELU this is a zero clamp; MiniMax M3 contributes its
20387    // dedicated OAI marker and frozen 7.0 source limit.  Anything without a
20388    // device contract stays on the CPU.
20389    let (graph_act, swiglu_limit) = d.act.moe_job_graph_contract()?;
20390    let act = graph_act.code();
20391    let (gm, gi, gr, gc, grs, gcf, gq1, gq4, gq2) = moe_parts(&d.gate_proj)?;
20392    let (_, ui, ur, uc, urs, ucf, uq1, uq4, uq2) = moe_parts(&d.up_proj)?;
20393    let (_, di, dr, dc, drs, dcf, dq1, dq4, dq2) = moe_parts(&d.down_proj)?;
20394    if gq1 != uq1 || uq1 != dq1 || gq4 != uq4 || uq4 != dq4 || gq2 != uq2 {
20395        return None; // mixed-dtype trio — honest CPU path
20396    }
20397    if gq2 && (dq2 || !dq4 || d.down_proj.mapped_q4tp().is_none()) {
20398        return None;
20399    }
20400    if !gq2 && dq2 {
20401        return None;
20402    }
20403    model_ref.get_or_insert_with(|| gm.clone());
20404    let gdt = if gcf.is_empty() {
20405        cortiq_core::TensorDtype::Q8Row
20406    } else {
20407        cortiq_core::TensorDtype::Q8_2f
20408    };
20409    let udt = if ucf.is_empty() {
20410        cortiq_core::TensorDtype::Q8Row
20411    } else {
20412        cortiq_core::TensorDtype::Q8_2f
20413    };
20414    jobs.push(crate::gpu::MoeJob {
20415        gate: (gi, gr, gc, grs),
20416        up: (ui, ur, uc, urs),
20417        down: (di, dr, dc, drs),
20418        xs_gate: prescale(x, gcf, gdt).into_owned(),
20419        xs_up: prescale(x, ucf, udt).into_owned(),
20420        down_col: dcf,
20421        w,
20422        q1: gq1,
20423        q4t: gq4 && !gq2 && d.gate_proj.mapped_q4tp().is_none(),
20424        q4tp: gq4 && (gq2 || d.gate_proj.mapped_q4tp().is_some()),
20425        gu_q2: gq2,
20426        swiglu_limit,
20427        act,
20428    });
20429    Some(())
20430}
20431
20432/// Sparse dense-FFN directly on QUANTIZED weights (mask × mmap): reads
20433/// ONLY the active neurons' gate/up rows and down columns from the mmap
20434/// — no full-matrix dequant, no f32 model copy. This is what lets a
20435/// masked big model run at quantized RSS (the historical mask path
20436/// forced the whole model to f32). Semantics identical to the f32
20437/// sparse path within quant tolerance.
20438fn sparse_ffn_quant(
20439    d: &DenseFfn,
20440    x: &[f32],
20441    active: &[u16],
20442    hidden: usize,
20443    pool: Option<&Pool>,
20444) -> Vec<f32> {
20445    let n = active.len();
20446    let inter = d.gate_proj.rows();
20447    let mut act = vec![0.0f32; n];
20448    // Scratch is needed if EITHER projection is group-packed (q4/vbit);
20449    // gate/up normally share a dtype but sizing on both is robust.
20450    let need_scratch = !(d.gate_proj.sparse_col_ok() && d.up_proj.sparse_col_ok());
20451    let compute = |ai: usize| -> f32 {
20452        let idx = active[ai] as usize;
20453        if idx >= inter {
20454            return 0.0; // defensive parity with the f32 sparse path
20455        }
20456        let mut s = if need_scratch {
20457            vec![0.0f32; hidden]
20458        } else {
20459            Vec::new()
20460        };
20461        let gate = d.gate_proj.row_dot(idx, x, &mut s);
20462        let up = d.up_proj.row_dot(idx, x, &mut s);
20463        d.act.combine(gate, up)
20464    };
20465    match pool {
20466        Some(p) if n >= 256 => {
20467            let ptr = SendMut(act.as_mut_ptr());
20468            p.run(&|widx, nw| {
20469                let chunk = n.div_ceil(nw);
20470                let (s, e) = (widx * chunk, ((widx + 1) * chunk).min(n));
20471                for ai in s..e {
20472                    unsafe { *ptr.at(ai) = compute(ai) };
20473                }
20474            });
20475        }
20476        _ => {
20477            for (ai, a) in act.iter_mut().enumerate() {
20478                *a = compute(ai);
20479            }
20480        }
20481    }
20482    // Scatter through active down columns (reads only those columns).
20483    let mut out = vec![0.0f32; hidden];
20484    for (ai, &idx) in active.iter().enumerate() {
20485        let w = act[ai];
20486        if w.abs() >= 1e-12 && (idx as usize) < inter {
20487            d.down_proj.add_col_scaled(idx as usize, w, &mut out);
20488        }
20489    }
20490    out
20491}
20492
20493/// Test-only re-export of the private sparse-quant FFN (mask × mmap gate).
20494#[doc(hidden)]
20495pub fn sparse_ffn_quant_for_test(
20496    d: &DenseFfn,
20497    x: &[f32],
20498    active: &[u16],
20499    hidden: usize,
20500) -> Vec<f32> {
20501    sparse_ffn_quant(d, x, active, hidden, None)
20502}
20503
20504/// Dequantize a DenseFfn's three matrices to f32 (transient; only the
20505/// q4/vbit-masked fallback uses it — the memory-lean path is
20506/// sparse_ffn_quant). Reuses row_f32 row-by-row.
20507fn dequant_dense_f32(d: &DenseFfn) -> (Vec<f32>, Vec<f32>, Vec<f32>) {
20508    let deq = |t: &QTensor| -> Vec<f32> {
20509        let (rows, cols) = (t.rows(), t.cols());
20510        let mut out = vec![0.0f32; rows * cols];
20511        for r in 0..rows {
20512            t.row_f32(r, &mut out[r * cols..(r + 1) * cols]);
20513        }
20514        out
20515    };
20516    (deq(&d.gate_proj), deq(&d.up_proj), deq(&d.down_proj))
20517}
20518
20519/// Pointer wrapper for the worker-pool scatter (same pattern as qtensor).
20520struct SendMut(*mut f32);
20521unsafe impl Send for SendMut {}
20522unsafe impl Sync for SendMut {}
20523impl SendMut {
20524    #[inline]
20525    // Deliberate unsynchronized scatter: pool workers write disjoint indices
20526    // in parallel, so returning `&mut` from `&self` is intentional here.
20527    #[allow(clippy::mut_from_ref)]
20528    unsafe fn at(&self, i: usize) -> &mut f32 {
20529        unsafe { &mut *self.0.add(i) }
20530    }
20531}
20532
20533/// Router → (selected experts in torch.topk order, per-expert score
20534/// vector, normalizer). The final weight of expert `e` is `p[e] / wsum`.
20535///
20536/// Two regimes share this. Qwen: softmax over ALL experts, top-k of the
20537/// probabilities, optional renorm — `router_sigmoid=false`, no bias,
20538/// scale 1 → bit-identical to the historical path. LFM2-MoE /
20539/// DeepSeek-V3 `noaux_tc`: per-expert sigmoid scores, an optional
20540/// selection bias (top-k CHOICE only; weights stay unbiased), an
20541/// architecture-specified renorm floor and a routed scale.  The ordinary
20542/// loader contract is `1e-6`; Flint and MiniMax M3.1 preserve their
20543/// reference runtime's selected-score `sum + 1e-20`.
20544pub(crate) fn moe_route(
20545    logits: &[f32],
20546    m: &MoeFfn,
20547    allowed: Option<&[bool]>,
20548) -> (Vec<usize>, Vec<f32>, f32) {
20549    moe_route_with_eps(logits, m, allowed, m.sigmoid_denom_eps)
20550}
20551
20552/// Router implementation with an explicit sigmoid renormalization floor.
20553///
20554/// Flint and MiniMax M3.1 reference runtimes compute
20555/// `sum(selected_scores) + 1e-20` in FP32. Using the generic 1e-6 floor is
20556/// not a harmless tolerance difference when all logits are very negative: it
20557/// collapses the routed branch toward zero instead of normalizing the selected
20558/// experts. Keeping the epsilon parameter here avoids changing the established
20559/// Qwen/LFM2 contract while allowing each architecture to preserve its own
20560/// numerical semantics.
20561pub(crate) fn moe_route_with_eps(
20562    logits: &[f32],
20563    m: &MoeFfn,
20564    allowed: Option<&[bool]>,
20565    sigmoid_denom_eps: f32,
20566) -> (Vec<usize>, Vec<f32>, f32) {
20567    let ne = logits.len();
20568    // Expert restriction: the static env mask (CMF_MOE_MASK) AND the
20569    // active task mask's expert fields (spec §5) both narrow the
20570    // candidate set; selection happens over the admitted experts only.
20571    // With norm_topk the kept weights renormalize below; without it
20572    // the excluded mass is honestly dropped.
20573    let admit = |e: usize| {
20574        m.mask.as_ref().is_none_or(|mk| mk[e])
20575            && allowed.is_none_or(|a| a.get(e).copied().unwrap_or(false))
20576    };
20577    // The resonance router (spec §9.5.1) selects by the RAW score: the
20578    // trainer (`resonance_winner`) and the resident graph
20579    // (`embryo_core_route_pick`) take the first maximum of the scores
20580    // and run the winner with weight 1.0. Selecting through the softmax
20581    // instead is not the same decision: `exp(l − max)` rounds two scores
20582    // closer than 2^-25 (possible below |score| 0.25) to the same 1.0, and
20583    // the lower index would take a token whose score is strictly smaller
20584    // — the trainer's trace and the graph would disagree with this path.
20585    // `−∞` (outside the shell) never wins; with no finite admitted expert
20586    // the generic path below degrades to uniform. The winner's
20587    // probability is 1.0 by construction (a one-hot `p`), so its
20588    // renormalized weight is `routed_scaling` on both norm_topk settings.
20589    if m.resonance.is_some() && m.top_k == 1 {
20590        let mut best: Option<usize> = None;
20591        for e in (0..ne).filter(|&e| admit(e)) {
20592            let l = logits[e];
20593            if l == f32::NEG_INFINITY || l.is_nan() {
20594                continue;
20595            }
20596            if best.is_none_or(|b| l > logits[b]) {
20597                best = Some(e);
20598            }
20599        }
20600        if let Some(b) = best {
20601            let mut p = vec![0.0f32; ne];
20602            p[b] = 1.0;
20603            return (vec![b], p, 1.0 / m.routed_scaling);
20604        }
20605    }
20606    // A `−∞` logit (a grown expert outside its shell, `Resonance::scores`)
20607    // takes probability 0 on both paths: sigmoid(−∞) = 0, exp(−∞ − max) = 0
20608    // — top-1 is the best FINITE expert, its renormalized weight exactly
20609    // 1.0. Every expert at −∞ cannot happen (trunk experts have no shell);
20610    // should it, the softmax would be NaN, so it degrades to uniform.
20611    let p: Vec<f32> = if m.router_sigmoid {
20612        logits.iter().map(|&l| 1.0 / (1.0 + (-l).exp())).collect()
20613    } else {
20614        let mx = logits.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
20615        if mx == f32::NEG_INFINITY {
20616            vec![1.0 / ne.max(1) as f32; ne]
20617        } else {
20618            let mut e: Vec<f32> = logits.iter().map(|&l| (l - mx).exp()).collect();
20619            let s: f32 = e.iter().sum();
20620            for v in &mut e {
20621                *v /= s;
20622            }
20623            e
20624        }
20625    };
20626    let mut idx: Vec<usize> = (0..ne).filter(|&e| admit(e)).collect();
20627    // Descending by selection score, lower index wins ties (torch.topk).
20628    match &m.expert_bias {
20629        Some(b) => idx.sort_unstable_by(|&x, &y| {
20630            (p[y] + b[y])
20631                .partial_cmp(&(p[x] + b[x]))
20632                .unwrap()
20633                .then(x.cmp(&y))
20634        }),
20635        None => idx.sort_unstable_by(|&x, &y| p[y].partial_cmp(&p[x]).unwrap().then(x.cmp(&y))),
20636    }
20637    idx.truncate(m.top_k);
20638    // Adaptive τ-routing: trim the tail experts once the kept mass is
20639    // enough. wsum below renormalizes over the KEPT set, so the output
20640    // stays a proper weighted average.
20641    if let Some(tau) = m.route_tau {
20642        let total: f32 = idx.iter().map(|&e| p[e]).sum();
20643        if total > 0.0 {
20644            let mut acc = 0.0f32;
20645            let mut keep = idx.len();
20646            for (i, &e) in idx.iter().enumerate() {
20647                acc += p[e];
20648                if acc >= tau * total {
20649                    keep = i + 1;
20650                    break;
20651                }
20652            }
20653            idx.truncate(keep);
20654        }
20655    }
20656    let wsum: f32 = if m.norm_topk_prob {
20657        let s: f32 = idx.iter().map(|&e| p[e]).sum();
20658        // Sigmoid routers use their architecture's reference floor; the
20659        // softmax path's probs already sum near 1, so it stays exactly as
20660        // before.
20661        (if m.router_sigmoid {
20662            s + sigmoid_denom_eps
20663        } else {
20664            s
20665        }) / m.routed_scaling
20666    } else {
20667        1.0 / m.routed_scaling
20668    };
20669    (idx, p, wsum)
20670}
20671
20672/// See the call site: one `layer:e1,e2,…` line per routed token.
20673fn moe_trace(idx: &[usize]) {
20674    moe_trace_at(crate::gpu::cur_layer() as i32, idx)
20675}
20676
20677/// The same, for callers that know their layer (DSV4 owns its layers and
20678/// never sets the pipeline's current-layer marker).
20679pub(crate) fn moe_trace_at(li: i32, idx: &[usize]) {
20680    use std::io::Write;
20681    static F: std::sync::OnceLock<Option<std::sync::Mutex<std::fs::File>>> =
20682        std::sync::OnceLock::new();
20683    let Some(f) = F.get_or_init(|| {
20684        let p = std::env::var("CMF_MOE_TRACE").ok()?;
20685        Some(std::sync::Mutex::new(
20686            std::fs::OpenOptions::new()
20687                .create(true)
20688                .append(true)
20689                .open(p)
20690                .ok()?,
20691        ))
20692    }) else {
20693        return;
20694    };
20695    let ids: Vec<String> = idx.iter().map(|e| e.to_string()).collect();
20696    let _ = writeln!(f.lock().unwrap(), "{li}:{}", ids.join(","));
20697}
20698
20699/// MoE FFN: router → top-k experts (see `moe_route`). Only selected
20700/// experts' pages are touched in mmap.
20701pub(crate) fn moe_ffn(
20702    m: &MoeFfn,
20703    x: &[f32],
20704    pool: Option<&Pool>,
20705    allowed: Option<&[bool]>,
20706) -> Vec<f32> {
20707    let r = moe_ffn_route(m, x, pool, allowed);
20708    moe_ffn_experts(m, x, &r, pool)
20709}
20710
20711/// One token's host route through a MoE layer: the chosen experts in
20712/// selection order, the per-expert scores and the normalizer (see
20713/// `moe_route`), plus the raw router logits.
20714pub(crate) struct MoeRoute {
20715    pub idx: Vec<usize>,
20716    pub p: Vec<f32>,
20717    pub wsum: f32,
20718    pub logits: Vec<f32>,
20719}
20720
20721/// The routing half of `moe_ffn`, shared by every executor of the chosen
20722/// experts (the host/per-op path below and the MiMo dynamic device cache,
20723/// `crate::mimo_moe`): activation accounting, router logits, `moe_route`,
20724/// the selection statistics and the `CMF_MOE_TRACE` line — so switching
20725/// executors can never change which experts a token gets.
20726pub(crate) fn moe_ffn_route(
20727    m: &MoeFfn,
20728    x: &[f32],
20729    pool: Option<&Pool>,
20730    allowed: Option<&[bool]>,
20731) -> MoeRoute {
20732    accumulate_act(m, x, 1);
20733    let ne = m.experts.len();
20734    let mut logits = vec![0.0f32; ne];
20735    match &m.resonance {
20736        Some(r) => r.scores(x, &mut logits),
20737        None => m.router.matvec(x, &mut logits, pool),
20738    }
20739    let (idx, p, wsum) = moe_route(&logits, m, allowed);
20740    {
20741        let mut st = m.stats.borrow_mut();
20742        if st.len() < ne {
20743            st.resize(ne, 0);
20744        }
20745        for &e in &idx {
20746            st[e] += 1;
20747        }
20748    }
20749    // `CMF_MOE_TRACE=<file>`: append one line per (layer, token) with the
20750    // selected expert ids. The cumulative `stats` above answer "which
20751    // experts are popular"; a residency design needs the question they
20752    // cannot answer — whether CONSECUTIVE tokens reuse experts (the
20753    // temporal locality an LRU cache lives on, FreeToken §4).
20754    moe_trace(&idx);
20755    MoeRoute {
20756        idx,
20757        p,
20758        wsum,
20759        logits,
20760    }
20761}
20762
20763/// Flint's released mixed MoE profile has a native-Metal selected-expert
20764/// pager with a different numerical contract from the generic CPU probe:
20765/// the pager keeps activations in f32, whereas the fast host arm normally
20766/// uses its A8W8 activation path.  Timing those two approximations against
20767/// one another can make the probe reject the exact pager even when it is the
20768/// faster exact-f32 implementation.  Keep this gate deliberately narrower
20769/// than the pager itself; the pager revalidates every selected tuple before
20770/// it binds a no-copy view and an honest refusal still takes the CPU path.
20771#[cfg(target_os = "macos")]
20772fn strict_flint_metal_pager_candidate(m: &MoeFfn) -> bool {
20773    const HIDDEN: usize = 2_048;
20774    const ROUTED_INTER: usize = 512;
20775    const SHARED_INTER: usize = 1_024;
20776    const EXPERTS: usize = 256;
20777    const TOP_K: usize = 6;
20778
20779    let Some(routed) = m.experts.first() else {
20780        return false;
20781    };
20782    let Some((model, _)) = routed.gate_proj.mapped_q2tp() else {
20783        return false;
20784    };
20785    let Some((shared, shared_gate)) = &m.shared else {
20786        return false;
20787    };
20788    if shared_gate.is_some()
20789        || model.header.arch.arch_name != "flint"
20790        || model.header.arch.prism_hadamard.is_some()
20791        || model.header.arch.hidden_size != HIDDEN
20792        || m.experts.len() != EXPERTS
20793        || m.top_k != TOP_K
20794        || !m.router_sigmoid
20795        || !m.norm_topk_prob
20796        || m.sigmoid_denom_eps.to_bits() != (1e-20f32).to_bits()
20797        || m.routed_scaling.to_bits() != 1.0f32.to_bits()
20798        || m.route_tau.is_some()
20799        || m.mask.is_some()
20800        || m.per_expert_scale.is_some()
20801        || m.router_input_norm
20802        || m.resonance.is_some()
20803        || !m.grown.is_empty()
20804    {
20805        return false;
20806    }
20807
20808    let routed_q2 = routed.act == Act::Silu
20809        && routed.segs.is_empty()
20810        && routed.gate_proj.rows() == ROUTED_INTER
20811        && routed.gate_proj.cols() == HIDDEN
20812        && routed.up_proj.rows() == ROUTED_INTER
20813        && routed.up_proj.cols() == HIDDEN
20814        && routed.down_proj.rows() == HIDDEN
20815        && routed.down_proj.cols() == ROUTED_INTER
20816        && routed.up_proj.mapped_q2tp().is_some()
20817        && routed.down_proj.mapped_q4tp().is_some();
20818    let shared_q4 = shared.act == Act::Silu
20819        && shared.segs.is_empty()
20820        && shared.gate_proj.rows() == SHARED_INTER
20821        && shared.gate_proj.cols() == HIDDEN
20822        && shared.up_proj.rows() == SHARED_INTER
20823        && shared.up_proj.cols() == HIDDEN
20824        && shared.down_proj.rows() == HIDDEN
20825        && shared.down_proj.cols() == SHARED_INTER
20826        && shared.gate_proj.mapped_q4tp().is_some()
20827        && shared.up_proj.mapped_q4tp().is_some()
20828        && shared.down_proj.mapped_q4tp().is_some();
20829    routed_q2 && shared_q4
20830}
20831
20832/// The expert half of `moe_ffn`: run a route's experts on the per-op GPU
20833/// block or the host.
20834pub(crate) fn moe_ffn_experts(
20835    m: &MoeFfn,
20836    x: &[f32],
20837    r: &MoeRoute,
20838    pool: Option<&Pool>,
20839) -> Vec<f32> {
20840    let (idx, p, wsum) = (&r.idx, &r.p, r.wsum);
20841    // D5: the whole layer MoE block in one GPU command buffer (experts — the
20842    // same mmap via a no-copy buffer; intermediate activations on the GPU).
20843    // MoE owns a separate probe class from dense FFN: its selected-span pager
20844    // has materially different residency and latency costs.
20845    if crate::gpu::enabled_here() {
20846        #[cfg(target_os = "macos")]
20847        if crate::gpu_metal::enabled() && strict_flint_metal_pager_candidate(m) {
20848            // Do not compare the pager's exact-f32 result with the CPU's
20849            // optional A8W8 activation approximation.  The strict pager is
20850            // the validated Flint path; if a directory/view/device check
20851            // declines, preserve the existing CPU fallback rather than
20852            // leaving the probe in a half-sampled state.
20853            return moe_ffn_gpu(m, x, &idx, &p, wsum, pool).unwrap_or_else(|| {
20854                crate::gpu::cpu_scope(|| moe_ffn_cpu(m, x, &idx, &p, wsum, pool))
20855            });
20856        }
20857        match crate::gpu::probe_arm(crate::gpu::OpClass::Moe) {
20858            crate::gpu::ProbeArm::Gpu => {
20859                let t0 = std::time::Instant::now();
20860                if let Some(out) = moe_ffn_gpu(m, x, &idx, &p, wsum, pool) {
20861                    crate::gpu::probe_record(crate::gpu::OpClass::Moe, true, t0.elapsed());
20862                    return out;
20863                }
20864                crate::gpu::probe_note_decline(crate::gpu::OpClass::Moe);
20865            }
20866            crate::gpu::ProbeArm::CpuTimed => {
20867                let t0 = std::time::Instant::now();
20868                let out = crate::gpu::cpu_scope(|| moe_ffn_cpu(m, x, &idx, &p, wsum, pool));
20869                crate::gpu::probe_record(crate::gpu::OpClass::Moe, false, t0.elapsed());
20870                return out;
20871            }
20872            crate::gpu::ProbeArm::Cpu => {
20873                return crate::gpu::cpu_scope(|| moe_ffn_cpu(m, x, &idx, &p, wsum, pool));
20874            }
20875        }
20876    }
20877    moe_ffn_cpu(m, x, &idx, &p, wsum, pool)
20878}
20879
20880/// One MiniCPM CPU-tail MoE through its opt-in dynamic bank, or through the
20881/// same-route host fallback.  The route is formed exactly once so statistics,
20882/// tracing, and model semantics remain owned by `moe_ffn_route`.
20883fn moe_ffn_minicpm_tail(
20884    slot: &mut crate::minicpm_tail::MiniCpmTail,
20885    li: usize,
20886    total_layers: usize,
20887    m: &MoeFfn,
20888    x: &[f32],
20889    pool: Option<&Pool>,
20890) -> Vec<f32> {
20891    let route = moe_ffn_route(m, x, pool, None);
20892    match slot.forward(li, m, x, &route, pool, total_layers) {
20893        Some(out) => out,
20894        // A cache refusal must be observationally identical to the generic
20895        // CPU-tail path. The surrounding host-tail scope already disables the
20896        // per-op arena, so do not introduce a float-activation policy change
20897        // merely because this explicit opt-in cache declined a model/layout.
20898        None => moe_ffn_experts(m, x, &route, pool),
20899    }
20900}
20901
20902/// One MoE token through the MiMo expert bank (`crate::mimo_moe`), or —
20903/// when the bank does not serve it — through the host path with the SAME
20904/// route, so the routing statistics and `CMF_MOE_TRACE` see it once.
20905fn moe_ffn_banked(
20906    slot: &mut crate::mimo_moe::Slot,
20907    li: usize,
20908    m: &MoeFfn,
20909    x: &[f32],
20910    pool: Option<&Pool>,
20911) -> Vec<f32> {
20912    let t0 = std::time::Instant::now();
20913    let r = moe_ffn_route(m, x, pool, None);
20914    slot.note_route(t0.elapsed().as_nanos() as u64);
20915    match slot.forward(li, m, x, &r, pool) {
20916        Some(out) => out,
20917        None => crate::qtensor::float_activations_scope(|| {
20918            crate::gpu::cpu_scope(|| moe_ffn_experts(m, x, &r, pool))
20919        }),
20920    }
20921}
20922
20923/// Verify rows share a bank frame; routing and fallback are decode's.
20924fn moe_ffn_banked_rows(
20925    slot: &mut crate::mimo_moe::Slot,
20926    li: usize,
20927    m: &MoeFfn,
20928    xs: &[f32],
20929    b: usize,
20930    hidden: usize,
20931    pool: Option<&Pool>,
20932) -> Vec<f32> {
20933    let t0 = std::time::Instant::now();
20934    let routes: Vec<_> = xs
20935        .chunks_exact(hidden)
20936        .map(|x| moe_ffn_route(m, x, pool, None))
20937        .collect();
20938    slot.note_route(t0.elapsed().as_nanos() as u64);
20939    if let Some(out) = slot.forward_rows(li, m, xs, &routes, pool) {
20940        return out;
20941    }
20942    let mut out = Vec::with_capacity(b * hidden);
20943    for (x, r) in xs.chunks_exact(hidden).zip(&routes) {
20944        let row = slot.forward(li, m, x, r, pool).unwrap_or_else(|| {
20945            // A failed bank must not stream missing experts into the arena.
20946            crate::qtensor::float_activations_scope(|| {
20947                crate::gpu::cpu_scope(|| moe_ffn_experts(m, x, r, pool))
20948            })
20949        });
20950        out.extend(row);
20951    }
20952    out
20953}
20954
20955/// One-shot report of whether the whole-token wgpu graph actually formed.
20956/// A refusal silently reverts to the per-op path, which is how a model can
20957/// look "GPU-accelerated" while every layer walks the host.  A device prefix
20958/// is tracked separately because it still pays a host boundary for the tail.
20959fn graph_note(built: bool, layers_run: usize, total_layers: usize) {
20960    use std::sync::atomic::{AtomicBool, Ordering};
20961    if built {
20962        GRAPH_TOK_OK.fetch_add(1, Ordering::Relaxed);
20963        if total_layers > 0 && layers_run < total_layers {
20964            GRAPH_TOK_PREFIX.fetch_add(1, Ordering::Relaxed);
20965        } else {
20966            GRAPH_TOK_FULL.fetch_add(1, Ordering::Relaxed);
20967        }
20968    } else {
20969        GRAPH_TOK_MISS.fetch_add(1, Ordering::Relaxed);
20970    }
20971    static SAID: AtomicBool = AtomicBool::new(false);
20972    if !SAID.swap(true, Ordering::Relaxed) {
20973        if built {
20974            tracing::info!("wgpu whole-token graph: ACTIVE");
20975        } else {
20976            tracing::warn!("wgpu whole-token graph refused — per-op path");
20977        }
20978    }
20979}
20980
20981/// Whole-token graph outcomes, process-wide: a benchmark that claims a
20982/// GPU number while MISS climbs is measuring the CPU — the honest-bench
20983/// contract makes that an error, not a footnote.
20984pub static GRAPH_TOK_OK: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
20985pub static GRAPH_TOK_MISS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
20986/// Graph calls that returned a hidden after running only a leading device
20987/// prefix.  These are valid hybrid executions but must not be reported as a
20988/// full GPU graph in benchmark evidence.
20989pub static GRAPH_TOK_PREFIX: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
20990/// Graph calls that covered the complete requested layer span.
20991pub static GRAPH_TOK_FULL: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
20992
20993/// Native Metal TokenGraph completion counters. These are incremented only
20994/// after checked command-buffer completion and successful readback, so a
20995/// fused-head NLL report can prove the route rather than infer it from env.
20996pub static METAL_GRAPH_TOK_OK: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
20997pub static METAL_GRAPH_HEAD_OK: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
20998pub static METAL_GRAPH_HEAD_MISS: std::sync::atomic::AtomicU64 =
20999    std::sync::atomic::AtomicU64::new(0);
21000pub static METAL_GRAPH_LAYERS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
21001pub static METAL_GRAPH_ERRORS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
21002/// Ordinary native-Metal rows-prefill admissions and completed rows.  These
21003/// counters are separate from TokenGraph token/head counts so a batch NLL
21004/// receipt cannot accidentally claim serial execution as batched.
21005pub static METAL_PREFILL_CHUNKS: std::sync::atomic::AtomicU64 =
21006    std::sync::atomic::AtomicU64::new(0);
21007pub static METAL_PREFILL_ROWS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
21008pub static METAL_PREFILL_HEAD_ROWS: std::sync::atomic::AtomicU64 =
21009    std::sync::atomic::AtomicU64::new(0);
21010pub static METAL_PREFILL_ERRORS: std::sync::atomic::AtomicU64 =
21011    std::sync::atomic::AtomicU64::new(0);
21012
21013/// `CMF_MOE_BATCH=0` restores the per-expert serial loop — the A/B lever
21014/// for the batched kernel, and how its bit-identity is checked.
21015fn moe_batch_enabled() -> bool {
21016    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
21017    *ON.get_or_init(|| std::env::var("CMF_MOE_BATCH").as_deref() != Ok("0"))
21018}
21019
21020/// Two-dispatch CPU MoE: every routed expert (and the shared one) fused
21021/// into one gate/up/SiLU dispatch and one down dispatch, instead of two
21022/// pool barriers per expert. Bit-identical to the serial loop below —
21023/// see `moe_gate_up_many` / `moe_down_many`. `None` = the batched kernel
21024/// does not cover this layer, walk the serial path.
21025fn moe_ffn_cpu_batched(
21026    m: &MoeFfn,
21027    x: &[f32],
21028    idx: &[usize],
21029    p: &[f32],
21030    wsum: f32,
21031    pool: Option<&Pool>,
21032) -> Option<Vec<f32>> {
21033    if idx.is_empty() || !moe_batch_enabled() {
21034        return None;
21035    }
21036    // The bake probe reads per-neuron activation mass out of the
21037    // single-expert path; batching would skip it. Rare and offline —
21038    // hand those runs to the serial loop.
21039    if FFN_PROBE.with(|pr| pr.borrow().is_some()) {
21040        return None;
21041    }
21042    let n = idx.len() + usize::from(m.shared.is_some());
21043    let mut pairs = Vec::with_capacity(n);
21044    let mut downs = Vec::with_capacity(n);
21045    let mut ws = Vec::with_capacity(n);
21046    for &e in idx {
21047        let d = &m.experts[e];
21048        if d.act != Act::Silu {
21049            return None;
21050        }
21051        pairs.push((&d.gate_proj, &d.up_proj));
21052        downs.push(&d.down_proj);
21053        ws.push(p[e] / wsum * m.per_expert_scale.as_ref().map_or(1.0, |v| v[e]));
21054    }
21055    // The shared expert goes last, matching the serial loop's order —
21056    // the f32 accumulation order is part of the bit-identity claim.
21057    if let Some((se, gate)) = &m.shared {
21058        if se.act != Act::Silu {
21059            return None;
21060        }
21061        let g = gate.as_ref().map_or(1.0, |gate| {
21062            let mut gl = [0.0f32; 1];
21063            gate.matvec(x, &mut gl, pool);
21064            1.0 / (1.0 + (-gl[0]).exp())
21065        });
21066        pairs.push((&se.gate_proj, &se.up_proj));
21067        downs.push(&se.down_proj);
21068        ws.push(g);
21069    }
21070    let inter = pairs[0].0.rows();
21071    let mut gs: Vec<Vec<f32>> = (0..pairs.len()).map(|_| vec![0f32; inter]).collect();
21072    if !QTensor::moe_gate_up_many(&pairs, x, &mut gs, pool) {
21073        return None;
21074    }
21075    let mut out = attention::take_buf(x.len());
21076    if !QTensor::moe_down_many(&downs, &gs, &ws, &mut out, pool) {
21077        attention::recycle_buf(&mut out);
21078        return None;
21079    }
21080    Some(out)
21081}
21082
21083/// Exact CPU completion for the routed experts a dynamic device cache did
21084/// not contain. The weights are already the router's final normalized mix.
21085/// Keeping this independent of `MoeFfn` makes the job `Sync`: its routing
21086/// statistics live in a `RefCell`, while the immutable expert tensors can be
21087/// evaluated safely in parallel with the GPU's resident subset.
21088/// Pooled-tail stage timers, reported per token by the bench JSON
21089/// (`m3_span_ms_per_tok`, `m3_cold_ms_per_tok`, `m3_arm_ms_per_tok`).
21090pub static M3_SPAN_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
21091pub static M3_COLD_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
21092pub static M3_ARM_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
21093pub static M3_COLD_N: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
21094pub static M3_COLD_DEV_N: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
21095
21096pub(crate) fn moe_cold_experts_cpu(
21097    experts: &[(&DenseFfn, f32)],
21098    x: &[f32],
21099    pool: Option<&Pool>,
21100) -> Vec<f32> {
21101    let mut out = attention::take_buf(x.len());
21102    if experts.is_empty() {
21103        return out;
21104    }
21105    let pairs: Vec<_> = experts
21106        .iter()
21107        .map(|(e, _)| (&e.gate_proj, &e.up_proj))
21108        .collect();
21109    let downs: Vec<_> = experts.iter().map(|(e, _)| &e.down_proj).collect();
21110    let weights: Vec<_> = experts.iter().map(|(_, w)| *w).collect();
21111    let inter = experts[0].0.gate_proj.rows();
21112    let mut activations: Vec<Vec<f32>> = (0..experts.len()).map(|_| vec![0.0; inter]).collect();
21113    // The batched gate/up kernels implement PLAIN SiLU only (their legacy
21114    // helper passes limit 0 and no activation code). MiniMax's SwiGLU-OAI
21115    // (alpha 1.702, shifted up) must take the per-expert path that reads
21116    // each tensor's own activation — otherwise every cold expert computes
21117    // silu and the residual walks away from the reference (measured: the
21118    // pooled tail shipped cos 0.047 hidden while the resident path was
21119    // bit-close).
21120    let silu_only = experts
21121        .iter()
21122        .all(|(e, _)| matches!(e.act, crate::pipeline::Act::Silu));
21123    if silu_only
21124        && QTensor::moe_gate_up_many(&pairs, x, &mut activations, pool)
21125        && QTensor::moe_down_many(&downs, &activations, &weights, &mut out, pool)
21126    {
21127        return out;
21128    }
21129    out.fill(0.0);
21130    for &(expert, weight) in experts {
21131        let mut one = dense_ffn(expert, x, pool);
21132        for (o, v) in out.iter_mut().zip(&one) {
21133            *o += weight * v;
21134        }
21135        attention::recycle_buf(&mut one);
21136    }
21137    out
21138}
21139
21140/// Cold part of a short bank batch. Share each expert's weight stream
21141/// across its tokens, but reduce contributions in each token's route order.
21142/// On an unsupported CPU/layout, retain the single-token cold kernels.
21143pub(crate) fn moe_cold_experts_rows_cpu(
21144    jobs: &[Vec<(&DenseFfn, f32)>],
21145    xs: &[f32],
21146    hidden: usize,
21147    pool: Option<&Pool>,
21148) -> Vec<f32> {
21149    let mut out = vec![0.0; xs.len()];
21150    let mut experts: Vec<&DenseFfn> = Vec::new();
21151    let mut groups: Vec<Vec<usize>> = Vec::new();
21152    let mut terms = vec![Vec::new(); jobs.len()];
21153    for (r, row) in jobs.iter().enumerate() {
21154        for &(e, w) in row {
21155            let g = match experts.iter().position(|&d| std::ptr::eq(d, e)) {
21156                Some(g) => g,
21157                None => {
21158                    experts.push(e);
21159                    groups.push(Vec::new());
21160                    groups.len() - 1
21161                }
21162            };
21163            terms[r].push((g, groups[g].len(), w));
21164            groups[g].push(r);
21165        }
21166    }
21167    if experts.is_empty() {
21168        return out;
21169    }
21170    let pairs: Vec<_> = experts.iter().map(|e| (&e.gate_proj, &e.up_proj)).collect();
21171    let downs: Vec<_> = experts.iter().map(|e| &e.down_proj).collect();
21172    let lens: Vec<_> = groups.iter().map(Vec::len).collect();
21173    let count: usize = lens.iter().sum();
21174    let mut acts = vec![vec![0.0; experts[0].gate_proj.rows()]; count];
21175    let mut ds = vec![vec![0.0; hidden]; count];
21176    if QTensor::moe_gate_up_rows(&pairs, &groups, xs, &mut acts, pool)
21177        && QTensor::moe_down_rows(&downs, &lens, &acts, &mut ds, pool)
21178    {
21179        let mut offset = 0;
21180        let offsets: Vec<_> = lens
21181            .iter()
21182            .map(|&n| {
21183                let start = offset;
21184                offset += n;
21185                start
21186            })
21187            .collect();
21188        for (r, terms) in terms.iter().enumerate() {
21189            for &(g, slot, w) in terms {
21190                for (o, &v) in out[r * hidden..(r + 1) * hidden]
21191                    .iter_mut()
21192                    .zip(&ds[offsets[g] + slot])
21193                {
21194                    *o += w * v;
21195                }
21196            }
21197        }
21198    } else {
21199        for (r, jobs) in jobs.iter().enumerate() {
21200            if !jobs.is_empty() {
21201                let mut row = moe_cold_experts_cpu(jobs, &xs[r * hidden..(r + 1) * hidden], pool);
21202                out[r * hidden..(r + 1) * hidden].copy_from_slice(&row);
21203                attention::recycle_buf(&mut row);
21204            }
21205        }
21206    }
21207    out
21208}
21209
21210/// The pure-CPU MoE expert loop (also the fallback of every GPU refusal).
21211fn moe_ffn_cpu(
21212    m: &MoeFfn,
21213    x: &[f32],
21214    idx: &[usize],
21215    p: &[f32],
21216    wsum: f32,
21217    pool: Option<&Pool>,
21218) -> Vec<f32> {
21219    if let Some(out) = moe_ffn_cpu_batched(m, x, idx, p, wsum, pool) {
21220        return out;
21221    }
21222    let mut out = attention::take_buf(x.len());
21223    for &e in idx {
21224        let mut eo = dense_ffn(&m.experts[e], x, pool);
21225        let w = p[e] / wsum * m.per_expert_scale.as_ref().map_or(1.0, |v| v[e]);
21226        for i in 0..out.len() {
21227            out[i] += w * eo[i];
21228        }
21229        attention::recycle_buf(&mut eo);
21230    }
21231    if let Some((se, gate)) = &m.shared {
21232        let mut so = dense_ffn(se, x, pool);
21233        let g = gate.as_ref().map_or(1.0, |gate| {
21234            let mut gl = [0.0f32; 1];
21235            gate.matvec(x, &mut gl, pool);
21236            1.0 / (1.0 + (-gl[0]).exp())
21237        });
21238        for i in 0..out.len() {
21239            out[i] += g * so[i];
21240        }
21241        attention::recycle_buf(&mut so);
21242    }
21243    out
21244}
21245
21246/// DeepSeek-V2 MLA forward, expand-to-MHA form (see `AttnKind::Mla`):
21247/// per token the latent expands to every head's K/V and the ordinary
21248/// cache + grouped attend do the rest. K head layout is [rope | nope]
21249/// (rotary_dim = qk_rope rotates the shared rope key and each q head's
21250/// prefix); V rows are zero-padded to the K head_dim inside the cache
21251/// and the pad is sliced off before O. Attention importance is not
21252/// accumulated for MLA yet (no eviction interplay).
21253#[allow(clippy::too_many_arguments)]
21254pub(crate) fn mla_attention(
21255    w: &MlaWeights,
21256    normed: &[f32],
21257    cache: &mut crate::kv_cache::LayerKvCache,
21258    position: usize,
21259    inv_freq: &[f32],
21260    rope_scale: f32,
21261    eps: f64,
21262    pool: Option<&Pool>,
21263) -> Vec<f32> {
21264    let (nh, dr, dn, dv, lora) = (w.nh, w.qk_rope, w.qk_nope, w.v_dim, w.lora);
21265    let hd = dr + dn;
21266    let mut q = vec![0.0f32; nh * hd];
21267    match (&w.q_a, &w.q_a_norm) {
21268        (Some(qa), Some(qn)) => {
21269            let mut t = vec![0.0f32; qa.rows()];
21270            qa.matvec(normed, &mut t, pool);
21271            let tn = inference::rms_norm(&t, qn, eps, NormStyle::Qwen);
21272            w.q_proj.matvec(&tn, &mut q, pool);
21273        }
21274        _ => w.q_proj.matvec(normed, &mut q, pool),
21275    }
21276    let mut ca = vec![0.0f32; lora + dr];
21277    w.kv_a.matvec(normed, &mut ca, pool);
21278    let (c_lat, k_rope) = ca.split_at_mut(lora);
21279    let latn = inference::rms_norm(c_lat, &w.kv_a_norm, eps, NormStyle::Qwen);
21280    let mut kvb = vec![0.0f32; nh * (dn + dv)];
21281    w.kv_b.matvec(&latn, &mut kvb, pool);
21282    if !w.nope {
21283        attention::rope_rotate_scaled(k_rope, position, inv_freq, rope_scale);
21284    }
21285    for h in 0..nh {
21286        if !w.nope {
21287            attention::rope_rotate_scaled(
21288                &mut q[h * hd..h * hd + dr],
21289                position,
21290                inv_freq,
21291                rope_scale,
21292            );
21293        }
21294    }
21295    let mut k = vec![0.0f32; nh * hd];
21296    let mut v = vec![0.0f32; nh * hd];
21297    for h in 0..nh {
21298        k[h * hd..h * hd + dr].copy_from_slice(k_rope);
21299        k[h * hd + dr..(h + 1) * hd].copy_from_slice(&kvb[h * (dn + dv)..h * (dn + dv) + dn]);
21300        v[h * hd..h * hd + dv].copy_from_slice(&kvb[h * (dn + dv) + dn..(h + 1) * (dn + dv)]);
21301    }
21302    cache.append(&k, &v, &vec![true; nh]);
21303    let (ao, mut imp) = attention::attend_all_heads(&q, cache, nh, 1, hd, w.scale, None, 0.0);
21304    attention::recycle_buf(&mut imp);
21305    let mut ov = vec![0.0f32; nh * dv];
21306    for h in 0..nh {
21307        ov[h * dv..(h + 1) * dv].copy_from_slice(&ao[h * hd..h * hd + dv]);
21308    }
21309    let mut out = vec![0.0f32; w.o_proj.rows()];
21310    w.o_proj.matvec(&ov, &mut out, pool);
21311    out
21312}
21313
21314/// Gemma-4 dual-branch FFN (spec: see `FfnKind::DenseMoe`). The dense
21315/// branch reads the pre-FFN-normed activation; the router and the
21316/// expert branch read the RAW residual — the router through a
21317/// scale-less rms norm (its constant gain is folded into the weights),
21318/// the experts through `pre_norm_2`. CPU path; GPU graphs refuse the
21319/// layer kind honestly.
21320fn dense_moe_ffn(
21321    dm: &DenseMoeFfn,
21322    x_normed: &[f32],
21323    h_raw: &[f32],
21324    eps: f64,
21325    norm_style: NormStyle,
21326    pool: Option<&Pool>,
21327) -> Vec<f32> {
21328    let mut d = dense_ffn(&dm.dense, x_normed, pool);
21329    d = inference::rms_norm(&d, &dm.post_norm_1, eps, norm_style);
21330    let m = &dm.moe;
21331    let ne = m.experts.len();
21332    let mut logits = vec![0.0f32; ne];
21333    if m.router_input_norm {
21334        let ss: f32 = h_raw.iter().map(|v| v * v).sum::<f32>() / h_raw.len() as f32;
21335        let inv = 1.0 / (ss + eps as f32).sqrt();
21336        let xr: Vec<f32> = h_raw.iter().map(|v| v * inv).collect();
21337        m.router.matvec(&xr, &mut logits, pool);
21338    } else {
21339        m.router.matvec(h_raw, &mut logits, pool);
21340    }
21341    let (idx, p, wsum) = moe_route(&logits, m, None);
21342    {
21343        let mut st = m.stats.borrow_mut();
21344        if st.len() < ne {
21345            st.resize(ne, 0);
21346        }
21347        for &e in &idx {
21348            st[e] += 1;
21349        }
21350    }
21351    let x2 = inference::rms_norm(h_raw, &dm.pre_norm_2, eps, norm_style);
21352    let mo = moe_ffn_cpu(m, &x2, &idx, &p, wsum, pool);
21353    let mo = inference::rms_norm(&mo, &dm.post_norm_2, eps, norm_style);
21354    for (di, mi) in d.iter_mut().zip(&mo) {
21355        *di += mi;
21356    }
21357    d
21358}
21359
21360/// Building the MoE-layer GPU jobs: all selected experts (+shared) must
21361/// be q8_2f-Mapped from the primary mapping; otherwise None → CPU path.
21362/// One-shot report of why the MoE GPU block refused. A silent `?` here
21363/// sends every expert to the CPU with nothing in the logs to say so —
21364/// which is exactly how a q4tp MoE model looked "GPU-accelerated" while
21365/// running entirely on the host.
21366fn moe_gpu_refused(why: &'static str) {
21367    use std::sync::atomic::{AtomicBool, Ordering};
21368    static SAID: AtomicBool = AtomicBool::new(false);
21369    if !SAID.swap(true, Ordering::Relaxed) {
21370        tracing::warn!("MoE GPU block refused ({why}) — experts run on the CPU");
21371    }
21372}
21373
21374fn moe_ffn_gpu(
21375    m: &MoeFfn,
21376    x: &[f32],
21377    idx: &[usize],
21378    p: &[f32],
21379    wsum: f32,
21380    pool: Option<&Pool>,
21381) -> Option<Vec<f32>> {
21382    use crate::gpu::MoeJob;
21383
21384    let mut jobs: Vec<MoeJob> = Vec::with_capacity(idx.len() + 1);
21385    let mut model_ref = None;
21386    for &e in idx {
21387        if moe_push_job(&m.experts[e], x, p[e] / wsum, &mut jobs, &mut model_ref).is_none() {
21388            moe_gpu_refused("push_job(expert)");
21389            return None;
21390        }
21391    }
21392    if let Some((se, gate)) = &m.shared {
21393        let g = gate.as_ref().map_or(1.0, |gate| {
21394            let mut gl = [0.0f32; 1];
21395            gate.matvec(x, &mut gl, pool);
21396            1.0 / (1.0 + (-gl[0]).exp())
21397        });
21398        if moe_push_job(se, x, g, &mut jobs, &mut model_ref).is_none() {
21399            moe_gpu_refused("push_job(shared)");
21400            return None;
21401        }
21402    }
21403    let Some(model) = model_ref else {
21404        moe_gpu_refused("no model_ref");
21405        return None;
21406    };
21407    let hidden = jobs[0].down.1;
21408    let mut out = vec![0.0f32; hidden];
21409    if crate::gpu::moe_block(&model, &jobs, &mut out) {
21410        Some(out)
21411    } else {
21412        moe_gpu_refused("gpu::moe_block");
21413        None
21414    }
21415}
21416
21417/// Single-position FFN dispatch.
21418fn ffn_forward(
21419    ffn: &FfnKind,
21420    x: &[f32],
21421    pool: Option<&Pool>,
21422    experts_allowed: Option<&[bool]>,
21423) -> Vec<f32> {
21424    match ffn {
21425        FfnKind::Dense(d) if !d.segs.is_empty() => tube_ffn(d, x, 1, pool, None),
21426        FfnKind::Dense(d) => dense_ffn(d, x, pool),
21427        FfnKind::Moe(m) => moe_ffn(m, x, pool, experts_allowed),
21428        // Dual-branch layers need the raw residual — their callers
21429        // dispatch dense_moe_ffn directly; the auxiliary paths that land
21430        // here (MTP draft, o1 replay) do not co-occur with gemma-4 MoE.
21431        FfnKind::DenseMoe(_) => unreachable!("DenseMoe dispatches via dense_moe_ffn"),
21432    }
21433}
21434
21435/// Fused two-position FFN: gate/up/down streamed once (dense). MoE
21436/// falls back to two singles — expert sets differ per position, there
21437/// is nothing to fuse.
21438fn ffn_forward_pair(
21439    ffn: &FfnKind,
21440    x1: &[f32],
21441    x2: &[f32],
21442    pool: Option<&Pool>,
21443    experts_allowed: Option<&[bool]>,
21444) -> (Vec<f32>, Vec<f32>) {
21445    let d = match ffn {
21446        // A tube layer has nothing to fuse across the pair — the tubes
21447        // are separate matrices; two singles are the honest path.
21448        FfnKind::Dense(d) if !d.segs.is_empty() => {
21449            return (
21450                tube_ffn(d, x1, 1, pool, None),
21451                tube_ffn(d, x2, 1, pool, None),
21452            );
21453        }
21454        FfnKind::Dense(d) => d,
21455        FfnKind::Moe(m) => {
21456            return (
21457                moe_ffn(m, x1, pool, experts_allowed),
21458                moe_ffn(m, x2, pool, experts_allowed),
21459            );
21460        }
21461        FfnKind::DenseMoe(_) => unreachable!("DenseMoe dispatches via dense_moe_ffn"),
21462    };
21463    let inter = d.gate_proj.rows();
21464    FFN_SCRATCH.with(|s| {
21465        let mut s = s.borrow_mut();
21466        let [g1, g2, u1, u2] = &mut *s;
21467        g1.resize(inter, 0.0);
21468        g2.resize(inter, 0.0);
21469        u1.resize(inter, 0.0);
21470        u2.resize(inter, 0.0);
21471        // Multi-matrix pair job: gate+up under one pool dispatch
21472        // (o1s = lane-1 outputs across tensors, o2s = lane-2).
21473        QTensor::matvec2_many(
21474            [&d.gate_proj, &d.up_proj],
21475            x1,
21476            x2,
21477            [g1.as_mut_slice(), u1.as_mut_slice()],
21478            [g2.as_mut_slice(), u2.as_mut_slice()],
21479            pool,
21480        );
21481        for i in 0..inter {
21482            g1[i] = d.act.combine(g1[i], u1[i]);
21483            g2[i] = d.act.combine(g2[i], u2[i]);
21484        }
21485        let mut o1 = attention::take_buf(d.down_proj.rows());
21486        let mut o2 = attention::take_buf(d.down_proj.rows());
21487        d.down_proj.matvec2(g1, g2, &mut o1, &mut o2, pool);
21488        (o1, o2)
21489    })
21490}
21491
21492#[cfg(test)]
21493mod tests {
21494    use std::ffi::OsString;
21495    use std::sync::Mutex;
21496
21497    // Environment switches are process-global. Keep the speculative-path
21498    // tests serialized and restore every prior value even during unwinding.
21499    static MTP_ENV_LOCK: Mutex<()> = Mutex::new(());
21500
21501    struct ScopedTestEnv {
21502        previous: Vec<(&'static str, Option<OsString>)>,
21503    }
21504
21505    impl ScopedTestEnv {
21506        fn set(vars: &[(&'static str, Option<&'static str>)]) -> Self {
21507            let previous = vars
21508                .iter()
21509                .map(|(name, _)| (*name, std::env::var_os(name)))
21510                .collect();
21511            for &(name, value) in vars {
21512                unsafe {
21513                    match value {
21514                        Some(value) => std::env::set_var(name, value),
21515                        None => std::env::remove_var(name),
21516                    }
21517                }
21518            }
21519            Self { previous }
21520        }
21521    }
21522
21523    impl Drop for ScopedTestEnv {
21524        fn drop(&mut self) {
21525            for (name, value) in self.previous.drain(..).rev() {
21526                unsafe {
21527                    match value {
21528                        Some(value) => std::env::set_var(name, value),
21529                        None => std::env::remove_var(name),
21530                    }
21531                }
21532            }
21533        }
21534    }
21535
21536    #[test]
21537    fn mtp_draft_shortlist_masks_unevaluated_vocab_rows() {
21538        use super::pad_mtp_draft_logits;
21539
21540        let mut logits = vec![-3.0, -1.0, -2.0];
21541        pad_mtp_draft_logits(&mut logits, 6);
21542        assert_eq!(&logits[..3], &[-3.0, -1.0, -2.0]);
21543        assert!(logits[3..].iter().all(|&x| x == f32::NEG_INFINITY));
21544        assert_eq!(
21545            logits
21546                .iter()
21547                .enumerate()
21548                .max_by(|(_, a), (_, b)| a.total_cmp(b))
21549                .map(|(i, _)| i),
21550            Some(1),
21551            "an unevaluated row cannot win an all-negative shortlist"
21552        );
21553    }
21554
21555    #[test]
21556    fn speculative_trial_fast_exits_after_two_zero_acceptance_rounds() {
21557        use super::{Pipeline, SpecMon, SpecTrial};
21558
21559        let mut mon = SpecMon::default();
21560        let start = std::time::Instant::now();
21561        mon.round(1.0, 1);
21562        let first = Pipeline::spec_trial_round(
21563            SpecTrial::Spec {
21564                t0: start,
21565                gen0: 0,
21566                rounds: 0,
21567            },
21568            &mut mon,
21569            1,
21570        );
21571        assert!(matches!(first, SpecTrial::Spec { rounds: 1, .. }));
21572
21573        mon.round(1.0, 1);
21574        let second = Pipeline::spec_trial_round(first, &mut mon, 2);
21575        assert!(
21576            matches!(second, SpecTrial::Plain { gen0: 2, .. }),
21577            "two verifier-tip-only rounds must stop the losing MTP trial"
21578        );
21579
21580        // One successful draft resets the streak, so a viable head retains
21581        // the normal five-round measurement budget.
21582        let mut viable = SpecMon::default();
21583        viable.round(1.0, 1);
21584        viable.round(1.0, 2);
21585        let continued = Pipeline::spec_trial_round(
21586            SpecTrial::Spec {
21587                t0: start,
21588                gen0: 0,
21589                rounds: 1,
21590            },
21591            &mut viable,
21592            3,
21593        );
21594        assert!(matches!(continued, SpecTrial::Spec { rounds: 2, .. }));
21595    }
21596
21597    #[test]
21598    fn fresh_mtp_request_does_not_inherit_full_head_streak() {
21599        let mut p = spark_test_pipeline(None);
21600        let head_rows = 248_070;
21601        p.draft_full_streak = 7;
21602        assert_eq!(p.draft_head_rows(head_rows), head_rows);
21603
21604        p.reset_fresh_mtp_draft_state();
21605        assert_eq!(
21606            p.draft_head_rows(head_rows),
21607            super::Pipeline::draft_vocab_rows(head_rows),
21608            "a fresh request returns to its configured shortlist"
21609        );
21610
21611        // A high-id first token of the NEW request still restores the
21612        // full-head protection immediately.
21613        p.note_draft_id(u32::MAX);
21614        assert_eq!(p.draft_head_rows(head_rows), head_rows);
21615    }
21616
21617    #[test]
21618    fn native_metal_batch_spec_requires_exact_explicit_opt_in() {
21619        use super::graph_spec_wanted_by_policy;
21620
21621        // A capable, otherwise-default-eligible native Metal model stays on
21622        // the serial path until the user deliberately asks for the
21623        // experimental verifier.
21624        assert!(!graph_spec_wanted_by_policy(None, true, false, false, true));
21625        assert!(!graph_spec_wanted_by_policy(
21626            Some("anything-but-1"),
21627            true,
21628            false,
21629            false,
21630            true,
21631        ));
21632        assert!(graph_spec_wanted_by_policy(
21633            Some("1"),
21634            false,
21635            true,
21636            false,
21637            true,
21638        ));
21639        assert!(!graph_spec_wanted_by_policy(
21640            Some("0"),
21641            true,
21642            false,
21643            false,
21644            true,
21645        ));
21646
21647        // Do not regress the established discrete/wgpu policy while closing
21648        // the native-Metal quality gate.
21649        assert!(graph_spec_wanted_by_policy(None, true, false, false, false));
21650        assert!(graph_spec_wanted_by_policy(
21651            Some("legacy-force"),
21652            false,
21653            true,
21654            true,
21655            false,
21656        ));
21657    }
21658
21659    #[test]
21660    fn encoder_hidden_states_are_normalized_and_reusable() {
21661        let mut p = super::create_test_pipeline(8, 12, 2, 1, 4, 2, 32);
21662        let ids = [1, 7, 11, 19];
21663
21664        let first = p.encode_hidden_states(&ids).expect("encoder forward");
21665        let second = p
21666            .encode_hidden_states(&ids)
21667            .expect("reusable encoder forward");
21668        assert_eq!(first.len(), ids.len() * 8);
21669        assert!(first.iter().all(|v| v.is_finite()));
21670        assert_eq!(
21671            first, second,
21672            "encoder forward must start from a fresh state"
21673        );
21674
21675        // An encoder call must not leave stale KV/GDN state behind for the
21676        // regular generative API sharing the same Pipeline implementation.
21677        assert!(p.forward_ids(&ids, None).is_ok());
21678    }
21679
21680    /// The 0.7.6 prefill-chunk rule: a plain dense stack wholly on a
21681    /// discrete card reads the prompt in wide chunks on x86; every other
21682    /// case keeps the width it had (the GDN-hybrid, MoE and DeepSeek paths
21683    /// were tuned on hardware not measured for this change).
21684    #[test]
21685    fn prefill_chunk_rule_widens_only_dense_on_discrete() {
21686        use super::{ChunkHost, ChunkStackFacts, DISCRETE_DENSE_PREFILL_CHUNK, prefill_chunk_rule};
21687        let dense_card = ChunkStackFacts {
21688            plain_dense: true,
21689            discrete: true,
21690            gpu_on: true,
21691            ..Default::default()
21692        };
21693        assert!(dense_card.dense_on_discrete());
21694        // The bug: a dense Llama on a Vulkan RTX 3090 got 48.
21695        assert_eq!(
21696            prefill_chunk_rule(None, ChunkHost::Other, dense_card.dense_on_discrete()),
21697            DISCRETE_DENSE_PREFILL_CHUNK
21698        );
21699        assert!(DISCRETE_DENSE_PREFILL_CHUNK > 48);
21700        for (label, facts) in [
21701            (
21702                "GDN hybrid / MoE / DeepSeek stack",
21703                ChunkStackFacts {
21704                    plain_dense: false,
21705                    ..dense_card
21706                },
21707            ),
21708            (
21709                "integrated GPU",
21710                ChunkStackFacts {
21711                    discrete: false,
21712                    ..dense_card
21713                },
21714            ),
21715            (
21716                "CPU only",
21717                ChunkStackFacts {
21718                    gpu_on: false,
21719                    discrete: false,
21720                    ..dense_card
21721                },
21722            ),
21723            (
21724                "capacity split",
21725                ChunkStackFacts {
21726                    capacity_split: true,
21727                    ..dense_card
21728                },
21729            ),
21730            (
21731                "multi-GPU plan",
21732                ChunkStackFacts {
21733                    multi_gpu: true,
21734                    ..dense_card
21735                },
21736            ),
21737            (
21738                "O(1) layers",
21739                ChunkStackFacts {
21740                    o1: true,
21741                    ..dense_card
21742                },
21743            ),
21744        ] {
21745            assert!(!facts.dense_on_discrete(), "{label}");
21746            assert_eq!(
21747                prefill_chunk_rule(None, ChunkHost::Other, facts.dense_on_discrete()),
21748                48,
21749                "{label} keeps the historical x86 chunk"
21750            );
21751        }
21752        // Other hosts are untouched whatever the model.
21753        for dense in [false, true] {
21754            assert_eq!(prefill_chunk_rule(None, ChunkHost::Macos, dense), 512);
21755            assert_eq!(prefill_chunk_rule(None, ChunkHost::Aarch64, dense), 256);
21756        }
21757        // CMF_PREFILL_CHUNK still wins everywhere (and is clamped to ≥ 1).
21758        for host in [ChunkHost::Macos, ChunkHost::Aarch64, ChunkHost::Other] {
21759            for dense in [false, true] {
21760                assert_eq!(prefill_chunk_rule(Some(48), host, dense), 48);
21761                assert_eq!(prefill_chunk_rule(Some(0), host, dense), 1);
21762            }
21763        }
21764    }
21765
21766    #[test]
21767    fn kv_reuse_plan_pulls_rows_decode_wrote_only_on_the_device() {
21768        use super::{ReuseLayer, ReusePlan, kv_reuse_plan};
21769        let full = |host_rows, device_rows| ReuseLayer {
21770            full: true,
21771            host_rows,
21772            device_rows,
21773            device_state: false,
21774        };
21775        // Turn 1: 300-token prompt prefilled on the host, 40 tokens decoded
21776        // by the wgpu graph into the device mirror only. Turn 2 reuses 339.
21777        assert_eq!(
21778            kv_reuse_plan(339, &[full(300, Some(339)), full(300, Some(339))]),
21779            ReusePlan::Pull(vec![(0, 300, 339), (1, 300, 339)])
21780        );
21781        // CPU / Metal: the host owner already holds every forwarded row.
21782        assert_eq!(kv_reuse_plan(339, &[full(339, None)]), ReusePlan::Ready);
21783        // A mirror past the prefix is fine for the host (it gets rewound).
21784        assert_eq!(
21785            kv_reuse_plan(339, &[full(339, Some(345))]),
21786            ReusePlan::Ready
21787        );
21788        // GPU prefix / CPU tail: only the device layers lag.
21789        assert_eq!(
21790            kv_reuse_plan(339, &[full(300, Some(339)), full(339, None)]),
21791            ReusePlan::Pull(vec![(0, 300, 339)])
21792        );
21793        // The device cannot supply the missing rows: never continue.
21794        assert_eq!(
21795            kv_reuse_plan(339, &[full(300, Some(320))]),
21796            ReusePlan::Fresh
21797        );
21798        assert_eq!(kv_reuse_plan(339, &[full(300, None)]), ReusePlan::Fresh);
21799        assert_eq!(kv_reuse_plan(339, &[full(350, None)]), ReusePlan::Fresh);
21800        // A recurrent state advanced on the device cannot be handed to a
21801        // host prefill (it is not rewindable and the host copy is stale).
21802        let conv = |device_state| ReuseLayer {
21803            full: false,
21804            host_rows: 0,
21805            device_rows: None,
21806            device_state,
21807        };
21808        assert_eq!(
21809            kv_reuse_plan(339, &[conv(true), full(300, Some(339))]),
21810            ReusePlan::Fresh
21811        );
21812        assert_eq!(
21813            kv_reuse_plan(339, &[conv(false), full(339, None)]),
21814            ReusePlan::Ready
21815        );
21816    }
21817
21818    #[test]
21819    fn nll_graph_policy_scopes_only_the_fused_head() {
21820        for (label, unmasked, prefer_graph, native_metal, want_graph, want_head) in [
21821            // A Vulkan/Wgpu hidden-only graph remains the quality route.
21822            ("vulkan graph", true, true, false, true, false),
21823            // Native Metal adds the strict fused graph-head contract.
21824            ("native Metal graph", true, true, true, true, true),
21825            // Masked NLL and the explicit non-graph fallback remain unchanged.
21826            ("masked", false, true, false, false, false),
21827            ("graph disabled", true, false, true, false, false),
21828        ] {
21829            let (graph_quality, graph_head_required) =
21830                super::nll_graph_policy(unmasked, prefer_graph, native_metal);
21831            assert_eq!(graph_quality, want_graph, "{label}: graph quality");
21832            assert_eq!(graph_head_required, want_head, "{label}: fused head");
21833        }
21834    }
21835
21836    #[test]
21837    fn mtp_prefill_pair_boundaries_skip_only_final_prompt_row() {
21838        assert_eq!(mtp_prefill_pair_count(0, 128, 256), 128);
21839        assert_eq!(mtp_prefill_pair_count(128, 256, 256), 127);
21840        assert_eq!(mtp_prefill_pair_count(0, 256, 256), 255);
21841        assert_eq!(mtp_prefill_pair_count(256, 256, 256), 0);
21842        assert_eq!(mtp_prefill_pair_count(300, 320, 256), 0);
21843    }
21844
21845    #[test]
21846    fn cancel_flag_stops_generation() {
21847        let mut p = create_test_pipeline(16, 32, 2, 2, 8, 2, 32);
21848        // Set before the call: the prefill loops honour it, the run
21849        // returns immediately with the cancelled reason and no tokens.
21850        p.cancel.store(true, std::sync::atomic::Ordering::Relaxed);
21851        let r = p.generate_from_ids(&[1, 2, 3], 8, None, None).unwrap();
21852        assert_eq!(r.finish_reason, "cancelled");
21853        assert!(
21854            r.token_ids.is_empty(),
21855            "no tokens after cancel: {:?}",
21856            r.token_ids
21857        );
21858        assert_eq!(p.kv_cache.seq_len(), 0);
21859        assert!(p.kv_history.is_empty());
21860        assert!(!p.graph_want_logits);
21861        assert!(p.graph_logits.is_none());
21862        // Flag auto-cleared: the next call generates normally.
21863        let r2 = p.generate_from_ids(&[1, 2, 3], 4, None, None).unwrap();
21864        assert_ne!(r2.finish_reason, "cancelled");
21865    }
21866    use super::*;
21867
21868    #[test]
21869    fn minimax_m3_arch_selects_checked_oai_activation_only() {
21870        // Keep this fixture minimal: `Act::from_arch_full` needs only the
21871        // architecture id and activation name, while serde supplies stable
21872        // defaults for unrelated CMF fields.
21873        let arch = |arch_name: &str, hidden_act: &str| {
21874            serde_json::from_value::<cortiq_core::ModelArch>(serde_json::json!({
21875                "arch_name": arch_name,
21876                "hidden_size": 8,
21877                "intermediate_size": 16,
21878                "num_layers": 1,
21879                "num_attention_heads": 1,
21880                "num_kv_heads": 1,
21881                "head_dim": 8,
21882                "vocab_size": 16,
21883                "layer_types": ["FullAttention"],
21884                "rms_norm_eps": 1e-6,
21885                "max_position_embeddings": 16,
21886                "hidden_act": hidden_act
21887            }))
21888            .expect("minimal architecture")
21889        };
21890        let minimax = arch("minimax_m3_vl", "swigluoai");
21891        assert_eq!(
21892            Act::from_arch_full(&minimax),
21893            Act::SwiGluOai {
21894                alpha: 1.702,
21895                limit: 7.0,
21896            }
21897        );
21898
21899        // The same spelling must not turn into MiniMax math on a generic
21900        // model.  Only the source-validated M3 architecture owns it.
21901        let unrelated = arch("qwen2", "swigluoai");
21902        assert_eq!(Act::from_arch_full(&unrelated), Act::Silu);
21903
21904        // Conversely, an M3-tagged header with a normal activation keeps the
21905        // normal path; this prevents the architecture id from overriding the
21906        // explicit serialized contract.
21907        let non_oai = arch("minimax_m3_vl", "gelu");
21908        assert_eq!(Act::from_arch_full(&non_oai), Act::Gelu);
21909    }
21910
21911    #[test]
21912    fn minimax_swiglu_oai_act_matches_scalar_contract() {
21913        let act = Act::swiglu_oai(1.702, 7.0);
21914        let got = act.combine(10.0, -10.0);
21915        let expected = crate::inference::swiglu_oai(10.0, -10.0, 1.702, 1.0, Some(7.0));
21916        assert!((got - expected).abs() < 1e-6);
21917        assert_eq!(act.graph_act(), None, "no inexact generic graph fallback");
21918        assert_eq!(
21919            act.wgpu_graph_act(),
21920            Some(crate::gpu::GraphAct::SwiGluOaiM3),
21921            "the WGPU-only arm accepts only the frozen M3 constants"
21922        );
21923        assert_eq!(
21924            Act::swiglu_oai(1.701, 7.0).wgpu_graph_act(),
21925            None,
21926            "nearby OAI constants must not borrow MiniMax's kernel"
21927        );
21928        assert_eq!(
21929            act.moe_job_graph_contract(),
21930            Some((crate::gpu::GraphAct::SwiGluOaiM3, 7.0)),
21931            "the direct MoE path carries the frozen clamp with the marker"
21932        );
21933        assert_eq!(
21934            Act::swiglu_oai(1.702, 6.999).moe_job_graph_contract(),
21935            None,
21936            "a nearby clamp must not borrow MiniMax's device contract"
21937        );
21938
21939        // `apply` is the gate half only; `combine` owns the up clamp and
21940        // +1 offset. The two must not accidentally collapse to ordinary SiLU.
21941        let gate_only = act.apply(-10.0);
21942        let expected_gate = -10.0 / (1.0 + (17.02f32).exp());
21943        assert!((gate_only - expected_gate).abs() < 1e-10);
21944    }
21945
21946    #[test]
21947    fn explicit_sliding_schedule_selects_local_rope_profile() {
21948        // Mellum alternates three local 1,024-token layers with one global
21949        // YaRN layer.  An explicit schedule deliberately overrides the
21950        // cadence sentinel in `swa`, so test both RoPE table and scale
21951        // dispatch rather than merely checking that the metadata loaded.
21952        let mut p = create_test_pipeline(32, 64, 4, 1, 8, 4, 32);
21953        p.swa = Some((1024, usize::MAX));
21954        p.sliding_layers = Some(vec![true, true, true, false]);
21955        p.inv_freq = std::sync::Arc::new(vec![0.125; 4]);
21956        p.inv_freq_local = Some(std::sync::Arc::new(vec![0.25; 4]));
21957        p.rope_scale = 1.277_258_9;
21958        p.rope_scale_local = 1.0;
21959
21960        for li in 0..3 {
21961            assert_eq!(p.layer_window(li), Some(1024), "local layer {li}");
21962            assert_eq!(p.layer_inv_freq(li).as_slice(), &[0.25; 4]);
21963            assert_eq!(p.layer_rope_scale(li), 1.0, "local layer {li}");
21964        }
21965        assert_eq!(p.layer_window(3), None);
21966        assert_eq!(p.layer_inv_freq(3).as_slice(), &[0.125; 4]);
21967        assert_eq!(p.layer_rope_scale(3), 1.277_258_9);
21968    }
21969
21970    /// sparse_ffn_quant must equal a dense FFN where inactive neurons are
21971    /// zeroed (mask × mmap correctness). On F32 tensors this is EXACT —
21972    /// it validates the row_dot / add_col_scaled / scatter indexing, the
21973    /// bug-prone part. The q8 branches reuse the golden-tested linear
21974    /// The per-token sparse path reads a transposed `down`; it must
21975    /// agree with the arm that computes everything and zeroes the
21976    /// losers, or the speed measurement is measuring a different model.
21977    #[test]
21978    fn dynamic_ffn_equals_the_zeroing_arm() {
21979        let (hidden, inter) = (8usize, 32usize);
21980        let synth = |n: usize, salt: usize| -> Vec<f32> {
21981            (0..n)
21982                .map(|i| (((i * 29 + salt * 13 + 7) % 89) as f32 / 89.0 - 0.5) * 0.6)
21983                .collect()
21984        };
21985        let down = synth(hidden * inter, 3);
21986        let mut down_t = vec![0.0f32; inter * hidden];
21987        for r in 0..hidden {
21988            for c in 0..inter {
21989                down_t[c * hidden + r] = down[r * inter + c];
21990            }
21991        }
21992        let d = DenseFfn {
21993            gate_proj: QTensor::from_f32(synth(inter * hidden, 1), inter, hidden),
21994            up_proj: QTensor::from_f32(synth(inter * hidden, 2), inter, hidden),
21995            down_proj: QTensor::from_f32(down.clone(), hidden, inter),
21996            act: Act::Silu,
21997            down_t: Some(QTensor::from_f32(down_t, inter, hidden)),
21998            segs: Vec::new(),
21999        };
22000        let x = synth(hidden, 11);
22001        let k = 12usize;
22002        let got = dense_ffn_dynamic(&d, &x, None, k).expect("down_t present");
22003        // Reference: full compute, keep the k loudest |silu(gate)|.
22004        let mut g = vec![0.0f32; inter];
22005        d.gate_proj.matvec(&x, &mut g, None);
22006        let mut u = vec![0.0f32; inter];
22007        d.up_proj.matvec(&x, &mut u, None);
22008        for v in g.iter_mut() {
22009            *v = inference::silu(*v);
22010        }
22011        keep_top_k(&mut g, k);
22012        for i in 0..inter {
22013            g[i] *= u[i];
22014        }
22015        let mut want = vec![0.0f32; hidden];
22016        d.down_proj.matvec(&g, &mut want, None);
22017        for (a, b) in want.iter().zip(&got) {
22018            assert!((a - b).abs() < 1e-5, "dynamic {b} vs reference {a}");
22019        }
22020    }
22021
22022    /// A tube layer is the same layer, re-cut. With every tube open the
22023    /// answer must equal the dense FFN over the concatenated neurons
22024    /// (the permutation is an identity on the layer's function); with a
22025    /// tube closed it must equal the dense FFN with those neurons
22026    /// zeroed — the mask semantics, now paid for in bytes not read.
22027    #[test]
22028    fn tube_ffn_open_equals_dense_and_closed_equals_masked() {
22029        let (hidden, core, tube) = (8usize, 12usize, 8usize);
22030        let inter = core + tube;
22031        let synth = |n: usize, salt: usize| -> Vec<f32> {
22032            (0..n)
22033                .map(|i| (((i * 41 + salt * 17 + 5) % 97) as f32 / 97.0 - 0.5) * 0.5)
22034                .collect()
22035        };
22036        let (g_all, u_all) = (synth(inter * hidden, 1), synth(inter * hidden, 2));
22037        let d_all = synth(hidden * inter, 3);
22038        // The dense layer, and the same weights cut into core + tube.
22039        let dense = DenseFfn {
22040            gate_proj: QTensor::from_f32(g_all.clone(), inter, hidden),
22041            up_proj: QTensor::from_f32(u_all.clone(), inter, hidden),
22042            down_proj: QTensor::from_f32(d_all.clone(), hidden, inter),
22043            act: Act::Silu,
22044            down_t: None,
22045            segs: Vec::new(),
22046        };
22047        let rows =
22048            |v: &[f32], a: usize, b: usize| -> Vec<f32> { v[a * hidden..b * hidden].to_vec() };
22049        let cols = |v: &[f32], a: usize, b: usize| -> Vec<f32> {
22050            let mut o = Vec::with_capacity(hidden * (b - a));
22051            for r in 0..hidden {
22052                o.extend_from_slice(&v[r * inter + a..r * inter + b]);
22053            }
22054            o
22055        };
22056        let tubed = DenseFfn {
22057            down_t: None,
22058            gate_proj: QTensor::from_f32(rows(&g_all, 0, core), core, hidden),
22059            up_proj: QTensor::from_f32(rows(&u_all, 0, core), core, hidden),
22060            down_proj: QTensor::from_f32(cols(&d_all, 0, core), hidden, core),
22061            act: Act::Silu,
22062            segs: vec![FfnSeg {
22063                gate: QTensor::from_f32(rows(&g_all, core, inter), tube, hidden),
22064                up: QTensor::from_f32(rows(&u_all, core, inter), tube, hidden),
22065                down: QTensor::from_f32(cols(&d_all, core, inter), hidden, tube),
22066                start: core,
22067                width: tube,
22068            }],
22069        };
22070        let x = synth(hidden, 7);
22071        let want = dense_ffn(&dense, &x, None);
22072        let got = tube_ffn(&tubed, &x, 1, None, None);
22073        for (a, b) in want.iter().zip(&got) {
22074            assert!((a - b).abs() < 1e-5, "open tube: {a} vs {b}");
22075        }
22076        // Closed tube: bits on for the core, off for the tube.
22077        let mut bits = vec![0u8; inter.div_ceil(8)];
22078        for n in 0..core {
22079            bits[n / 8] |= 1 << (n % 8);
22080        }
22081        let closed = tube_ffn(&tubed, &x, 1, None, Some(&bits));
22082        let masked = dense_ffn_masked(&dense, &x, None, &bits);
22083        for (a, b) in masked.iter().zip(&closed) {
22084            assert!((a - b).abs() < 1e-5, "closed tube: {a} vs {b}");
22085        }
22086        // The batched arm must agree with the single-position one.
22087        let batch = tube_ffn(&tubed, &x, 1, None, Some(&bits));
22088        for (a, b) in closed.iter().zip(&batch) {
22089            assert_eq!(a, b, "batch arm disagrees with decode arm");
22090        }
22091    }
22092
22093    /// scale, structurally identical to the matvec kernels.
22094    #[test]
22095    fn sparse_ffn_quant_equals_dense_with_inactive_zeroed() {
22096        let (hidden, inter) = (16usize, 40usize);
22097        let synth = |n: usize, salt: usize| -> Vec<f32> {
22098            (0..n)
22099                .map(|i| (((i * 37 + salt * 11 + 3) % 101) as f32 / 101.0 - 0.5) * 0.4)
22100                .collect()
22101        };
22102        let d = DenseFfn {
22103            gate_proj: QTensor::from_f32(synth(inter * hidden, 1), inter, hidden),
22104            up_proj: QTensor::from_f32(synth(inter * hidden, 2), inter, hidden),
22105            down_proj: QTensor::from_f32(synth(hidden * inter, 3), hidden, inter),
22106            act: Act::Silu,
22107            down_t: None,
22108            segs: Vec::new(),
22109        };
22110        let x = synth(hidden, 9);
22111        // Active = every 3rd neuron.
22112        let active: Vec<u16> = (0..inter as u16).filter(|i| i % 3 == 0).collect();
22113
22114        let sparse = sparse_ffn_quant(&d, &x, &active, hidden, None);
22115
22116        // Reference: full dense FFN but g[i]=0 for inactive neurons.
22117        let mut g = vec![0.0f32; inter];
22118        d.gate_proj.matvec(&x, &mut g, None);
22119        let mut u = vec![0.0f32; inter];
22120        d.up_proj.matvec(&x, &mut u, None);
22121        let act_set: std::collections::HashSet<u16> = active.iter().copied().collect();
22122        for i in 0..inter {
22123            g[i] = if act_set.contains(&(i as u16)) {
22124                inference::silu(g[i]) * u[i]
22125            } else {
22126                0.0
22127            };
22128        }
22129        let mut reference = vec![0.0f32; hidden];
22130        d.down_proj.matvec(&g, &mut reference, None);
22131
22132        let max_d = sparse
22133            .iter()
22134            .zip(&reference)
22135            .map(|(a, b)| (a - b).abs())
22136            .fold(0.0f32, f32::max);
22137        assert!(max_d < 1e-5, "sparse != dense-zeroed: max|Δ| = {max_d}");
22138    }
22139
22140    /// Attach a synthetic MTP head. `inter` deliberately stays independent
22141    /// from the trunk width so calibration tests can catch a trunk-proxy bug.
22142    fn attach_test_mtp_width(p: &mut Pipeline, inter: usize) {
22143        let (h, inter, heads, kv, hd) = (
22144            p.hidden_size,
22145            inter,
22146            p.num_heads,
22147            p.num_kv_heads,
22148            p.head_dim,
22149        );
22150        let synth = |n: usize, salt: usize| -> Vec<f32> {
22151            (0..n)
22152                .map(|i| (((i * 29 + salt * 23 + 5) % 101) as f32 / 101.0 - 0.5) * 0.2)
22153                .collect()
22154        };
22155        let qt = |rows: usize, cols: usize, salt: usize| -> QTensor {
22156            QTensor::from_f32(synth(rows * cols, salt), rows, cols)
22157        };
22158        p.mtp = Some(MtpModule {
22159            enorm: vec![1.0; h],
22160            hnorm: vec![1.0; h],
22161            eh_proj: qt(h, 2 * h, 301),
22162            layer: LayerWeights {
22163                input_norm: vec![1.0; h],
22164                post_norm: vec![1.0; h],
22165                attn_out_norm: None,
22166                ffn_out_norm: None,
22167                layer_scale: None,
22168                ffn: FfnKind::Dense(DenseFfn {
22169                    gate_proj: qt(inter, h, 315),
22170                    up_proj: qt(inter, h, 316),
22171                    down_proj: qt(h, inter, 317),
22172                    act: Act::Silu,
22173                    down_t: None,
22174                    segs: Vec::new(),
22175                }),
22176                attn: AttnKind::Full {
22177                    bias: None,
22178                    wq: qt(heads * hd, h, 311),
22179                    wk: qt(kv * hd, h, 312),
22180                    wv: qt(kv * hd, h, 313),
22181                    wo: qt(h, heads * hd, 314),
22182                    q_norm: None,
22183                    k_norm: None,
22184                    output_gate: false,
22185                    softplus_gate: None,
22186                },
22187            },
22188            final_norm: vec![1.0; h],
22189            kv: crate::kv_cache::LayerKvCache::new(kv, hd),
22190        });
22191    }
22192
22193    /// Attach a synthetic MTP head with the trunk's conventional width.
22194    fn attach_test_mtp(p: &mut Pipeline) {
22195        attach_test_mtp_width(p, p.intermediate_size);
22196    }
22197
22198    #[test]
22199    fn canonical_mtp_ffn_probe_uses_draft_width_and_cleans_state() {
22200        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 64);
22201        // This differs from the trunk FFN width (16). A result shaped like
22202        // the trunk probe would be a silent, dangerous compaction bug.
22203        attach_test_mtp_width(&mut p, 11);
22204        let ids = [3, 9, 4, 12, 7, 2];
22205        let trunk = p.probe_ffn_mass(&ids);
22206        assert_eq!(trunk[0].len(), p.intermediate_size);
22207
22208        let got = p.probe_mtp_ffn(&ids).expect("canonical MTP probe");
22209        assert_eq!(got.len(), 1);
22210        assert_eq!(got[0].mass.len(), 11);
22211        assert_eq!(got[0].squared_mass.len(), 11);
22212        assert_eq!(got[0].observed_positions, ids.len() - 1);
22213        assert!(got[0].mass.iter().any(|&v| v > 0.0));
22214        assert!(got[0]
22215            .mass
22216            .iter()
22217            .zip(&got[0].squared_mass)
22218            .all(|(&m, &s)| m.is_finite() && s.is_finite() && m >= 0.0 && s >= 0.0));
22219        assert_eq!(p.kv_cache.seq_len(), 0, "probe must leave a fresh trunk");
22220        assert_eq!(p.mtp.as_ref().unwrap().kv.seq_len, 0, "and fresh MTP KV");
22221
22222        // Force an error *after* the MTP attention has appended a draft KV
22223        // row. This proves the error path clears detached draft state, not
22224        // merely the easy invalid-token preflight branch.
22225        let h = p.hidden_size;
22226        let FfnKind::Dense(dense) = &mut p.mtp.as_mut().unwrap().layer.ffn else {
22227            panic!("synthetic MTP must be dense")
22228        };
22229        dense.segs.push(FfnSeg {
22230            gate: QTensor::from_f32(vec![0.0; h], 1, h),
22231            up: QTensor::from_f32(vec![0.0; h], 1, h),
22232            down: QTensor::from_f32(vec![0.0; h], h, 1),
22233            start: dense.gate_proj.rows(),
22234            width: 1,
22235        });
22236        let err = p.probe_mtp_ffn(&ids);
22237        assert!(err
22238            .unwrap_err()
22239            .contains("tube-segmented draft FFNs are not supported"));
22240        assert_eq!(p.kv_cache.seq_len(), 0);
22241        assert_eq!(p.mtp.as_ref().unwrap().kv.seq_len, 0);
22242    }
22243
22244    #[test]
22245    fn speculative_equals_vanilla_greedy() {
22246        let _env_lock = MTP_ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
22247        // Exercise the experimental host pair deterministically; the native
22248        // and wgpu batch graphs have independent coverage.
22249        let _env = ScopedTestEnv::set(&[
22250            ("CMF_GPU", Some("0")),
22251            ("CMF_GPU_WGPU_GRAPH", Some("0")),
22252            ("CMF_MTP_PAIR", Some("1")),
22253        ]);
22254        let run = |spec: bool| {
22255            let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
22256            p.sampler_config.temperature = 0.0;
22257            attach_test_mtp(&mut p);
22258            p.speculative = spec;
22259            let r = p.generate("abcdef", 12, None, None).unwrap();
22260            (r.token_ids, r.mtp_drafted, r.mtp_accepted)
22261        };
22262        let (vanilla, d0, _) = run(false);
22263        let (spec, d1, a1) = run(true);
22264        assert_eq!(d0, 0, "vanilla path must not draft");
22265        assert!(d1 > 0, "speculative path must draft");
22266        assert!(a1 <= d1, "accepted drafts cannot exceed proposals");
22267        assert_eq!(
22268            vanilla, spec,
22269            "speculative must reproduce the exact greedy sequence (accepted {a1}/{d1})"
22270        );
22271    }
22272
22273    #[test]
22274    fn speculative_accepts_constant_oracle() {
22275        let _env_lock = MTP_ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
22276        let _env = ScopedTestEnv::set(&[
22277            ("CMF_GPU", Some("0")),
22278            ("CMF_GPU_WGPU_GRAPH", Some("0")),
22279            ("CMF_MTP_PAIR", Some("1")),
22280        ]);
22281        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
22282        p.sampler_config.temperature = 0.0;
22283        p.sampler_config.repetition_penalty = 1.0;
22284        // Constant lm_head → every logit equal → both the main model and
22285        // the draft head argmax to token 0: acceptance must be 100%.
22286        p.weights.lm_head = QTensor::from_f32(vec![0.01; 64 * 8], 64, 8);
22287        attach_test_mtp(&mut p);
22288        p.speculative = true;
22289        let r = p.generate("abcd", 10, None, None).unwrap();
22290        assert!(r.mtp_drafted > 0);
22291        assert_eq!(
22292            r.mtp_accepted, r.mtp_drafted,
22293            "constant logits → every draft accepted"
22294        );
22295        // Ties resolve to the same token in both the main and draft
22296        // heads — the sequence is one repeated token.
22297        assert!(r.token_ids.windows(2).all(|w| w[0] == w[1]));
22298    }
22299
22300    #[test]
22301    fn host_fused_mtp_pair_is_opt_in() {
22302        let _env_lock = MTP_ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
22303        let _env = ScopedTestEnv::set(&[
22304            ("CMF_GPU", Some("0")),
22305            ("CMF_GPU_WGPU_GRAPH", Some("0")),
22306            ("CMF_MTP_PAIR", None),
22307        ]);
22308        let run = |spec: bool| {
22309            let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
22310            p.sampler_config.temperature = 0.0;
22311            attach_test_mtp(&mut p);
22312            p.speculative = spec;
22313            let r = p.generate("abcdef", 12, None, None).unwrap();
22314            (r.token_ids, r.mtp_drafted, r.mtp_accepted)
22315        };
22316        let (plain, d0, _) = run(false);
22317        let (guarded, d1, a1) = run(true);
22318        assert_eq!(d0, 0);
22319        assert_eq!(d1, 0, "host MTP pair must remain opt-in");
22320        assert_eq!(a1, 0);
22321        assert_eq!(guarded, plain, "the safe default is ordinary greedy decode");
22322    }
22323
22324    #[test]
22325    fn empty_prompt_is_an_error_not_a_panic() {
22326        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 260);
22327        let r = p.generate("", 4, None, None);
22328        assert!(r.is_err(), "empty prompt must be a clean error");
22329    }
22330
22331    #[test]
22332    fn every_token_enters_kv_exactly_once() {
22333        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
22334        // Greedy so no RNG variance; byte tokenizer → 3 prompt tokens.
22335        p.sampler_config.temperature = 0.0;
22336        let r = p.generate("abc", 2, None, None).unwrap();
22337        assert_eq!(r.prompt_tokens, 3);
22338        // prompt(3) + first sampled token forwarded before second logits:
22339        // step0 samples from prefill hidden (no extra forward), then
22340        // forwards t1 → cache 4; step1 samples, loop ends (max_tokens).
22341        assert_eq!(
22342            p.kv_cache.seq_len(),
22343            3 + r.tokens_generated - 1,
22344            "each token must be cached exactly once (v1 cached the last prompt token twice)"
22345        );
22346    }
22347
22348    #[test]
22349    fn generation_is_reproducible_with_seed() {
22350        let run = || {
22351            let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
22352            p.generate("hello", 8, None, None).unwrap().token_ids
22353        };
22354        assert_eq!(run(), run());
22355    }
22356
22357    #[test]
22358    fn resetting_sampler_restarts_the_seeded_stream() {
22359        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
22360        let config = SamplerConfig {
22361            seed: Some(1234),
22362            ..SamplerConfig::default()
22363        };
22364        p.set_sampler_config(config.clone());
22365        let first = p.generate("hello", 8, None, None).unwrap().token_ids;
22366        p.set_sampler_config(config);
22367        let second = p.generate("hello", 8, None, None).unwrap().token_ids;
22368        assert_eq!(first, second);
22369    }
22370
22371    #[test]
22372    fn eviction_bounds_the_cache() {
22373        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 260);
22374        p.kv_cache.max_seq_len = 6;
22375        p.sampler_config.temperature = 0.0;
22376        let _ = p.generate("abcd", 12, None, None).unwrap();
22377        assert!(
22378            p.kv_cache.seq_len() <= 6 + 1,
22379            "cache must stay bounded by max_seq_len (got {})",
22380            p.kv_cache.seq_len()
22381        );
22382    }
22383
22384    #[test]
22385    fn confidence_matches_tokens_and_is_a_probability() {
22386        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
22387        p.sampler_config.temperature = 0.0;
22388        p.sampler_config.repetition_penalty = 1.0;
22389        let r = p.generate("abcd", 10, None, None).unwrap();
22390        assert_eq!(
22391            r.token_confidence.len(),
22392            r.token_ids.len(),
22393            "one confidence per emitted token"
22394        );
22395        for &c in &r.token_confidence {
22396            assert!((0.0..=1.0).contains(&c), "confidence out of [0,1]: {c}");
22397        }
22398        // top1_prob is a valid softmax probability.
22399        let logits = [1.0f32, 3.0, 0.5, 3.0];
22400        let p0 = top1_prob_t(&logits, 1, 1.0);
22401        let p1 = top1_prob_t(&logits, 3, 1.0);
22402        assert!((p0 - p1).abs() < 1e-6, "equal logits → equal prob");
22403        assert!(p0 > 0.0 && p0 < 1.0);
22404        // Calibration temperature > 1 softens an over-confident peak.
22405        let sharp = top1_prob_t(&logits, 1, 1.0);
22406        let soft = top1_prob_t(&logits, 1, 2.0);
22407        assert!(soft < sharp, "higher temperature lowers peak confidence");
22408    }
22409
22410    #[test]
22411    fn trace_is_opt_in_and_parallels_the_output() {
22412        // Off by default: the runtime is silent unless observation asked.
22413        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
22414        p.sampler_config.temperature = 0.0;
22415        p.sampler_config.repetition_penalty = 1.0;
22416        let r = p.generate("abcd", 10, None, None).unwrap();
22417        assert!(r.traces.is_empty(), "trace must be empty unless enabled");
22418
22419        // On: exactly one row per emitted token, aligned with the output.
22420        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
22421        p.sampler_config.temperature = 0.0;
22422        p.sampler_config.repetition_penalty = 1.0;
22423        p.set_trace(true);
22424        let r = p.generate("abcd", 10, None, None).unwrap();
22425        assert_eq!(r.traces.len(), r.token_ids.len(), "one trace row per token");
22426        for (i, tr) in r.traces.iter().enumerate() {
22427            assert_eq!(tr.t, i, "trace index is sequential");
22428            assert_eq!(tr.token_id, r.token_ids[i], "trace token_id matches output");
22429            assert_eq!(
22430                tr.confidence, r.token_confidence[i],
22431                "trace confidence matches the confidence channel"
22432            );
22433            // No dynamic router in this pipeline → no skill, no coherence.
22434            assert!(tr.active_skill.is_none() && tr.recon.is_none() && !tr.switched);
22435        }
22436    }
22437
22438    #[test]
22439    fn explain_prefill_logits_match_greedy_first_token() {
22440        // `cortiq explain` shows the next-token distribution from
22441        // prefill_next_logits; its argmax must equal what greedy generate
22442        // actually emits first — otherwise explain would lie.
22443        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
22444        p.sampler_config.temperature = 0.0;
22445        p.sampler_config.repetition_penalty = 1.0;
22446        let ids = p.tokenizer.encode("abcd");
22447        let logits = p.prefill_next_logits(&ids, None);
22448        let argmax = logits
22449            .iter()
22450            .enumerate()
22451            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
22452            .unwrap()
22453            .0 as u32;
22454        let r = p.generate("abcd", 1, None, None).unwrap();
22455        assert_eq!(
22456            argmax, r.token_ids[0],
22457            "explain preview must match greedy emit"
22458        );
22459    }
22460
22461    #[test]
22462    fn laguna_shared_expert_is_unconditionally_added() {
22463        let matrix = |values: Vec<f32>| QTensor::from_f32(values, 2, 2);
22464        let identity = || matrix(vec![1.0, 0.0, 0.0, 1.0]);
22465        let zero_dense = || DenseFfn {
22466            gate_proj: matrix(vec![0.0; 4]),
22467            up_proj: matrix(vec![0.0; 4]),
22468            down_proj: matrix(vec![0.0; 4]),
22469            act: Act::Silu,
22470            down_t: None,
22471            segs: Vec::new(),
22472        };
22473        let shared = DenseFfn {
22474            gate_proj: identity(),
22475            up_proj: identity(),
22476            down_proj: identity(),
22477            act: Act::Silu,
22478            down_t: None,
22479            segs: Vec::new(),
22480        };
22481        let x = [1.0, 2.0];
22482        let expected = dense_ffn(&shared, &x, None);
22483        let moe = MoeFfn {
22484            router: QTensor::from_f32(vec![0.0, 0.0], 1, 2),
22485            experts: vec![zero_dense()],
22486            top_k: 1,
22487            norm_topk_prob: true,
22488            router_sigmoid: true,
22489            sigmoid_denom_eps: 1e-6,
22490            expert_bias: None,
22491            routed_scaling: 1.0,
22492            route_tau: None,
22493            shared: Some((shared, None)),
22494            stats: std::cell::RefCell::new(Vec::new()),
22495            act_sq: std::cell::RefCell::new(Vec::new()),
22496            act_rows: std::cell::RefCell::new(Vec::new()),
22497            mask: None,
22498            per_expert_scale: None,
22499            router_input_norm: false,
22500            resonance: None,
22501            grown: Vec::new(),
22502        };
22503        let actual = moe_ffn_cpu(&moe, &x, &[0], &[0.0], 1.0, None);
22504        for (actual, expected) in actual.iter().zip(expected) {
22505            assert!((actual - expected).abs() < 1e-6);
22506        }
22507    }
22508
22509    /// A tiny MiMo-V2-shaped stack (the M3 fixture): layers [full, sliding,
22510    /// sliding, full]; 4 Q heads over 1 (full) / 2 (sliding) KV heads;
22511    /// head_dim 8 with 4-wide V heads; partial rotary 4 at θ 1e7 (full) /
22512    /// 1e4 (sliding); window 3; learned sinks on the sliding layers; layer
22513    /// 0 a dense FFN, layers 1..3 sigmoid-routed MoE with a selection bias
22514    /// (4 experts, top-2, renormalized, no shared expert). Geometry and
22515    /// sinks go through the same `set_attn_geometry` / `set_layer_sinks`
22516    /// the loader calls.
22517    fn mimo_test_pipeline() -> Pipeline {
22518        let (hs, inter, nh, hd, vd, vocab) = (16usize, 24usize, 4usize, 8usize, 4usize, 64usize);
22519        let kvh = [1usize, 2, 2, 1];
22520        let synth = |n: usize, salt: usize| -> Vec<f32> {
22521            (0..n)
22522                .map(|i| (((i * 31 + salt * 17 + 7) % 97) as f32 / 97.0 - 0.5) * 0.4)
22523                .collect()
22524        };
22525        let qt = |rows: usize, cols: usize, salt: usize| {
22526            QTensor::from_f32(synth(rows * cols, salt), rows, cols)
22527        };
22528        let dense = |inter: usize, salt: usize| DenseFfn {
22529            gate_proj: qt(inter, hs, salt),
22530            up_proj: qt(inter, hs, salt + 1),
22531            down_proj: qt(hs, inter, salt + 2),
22532            act: Act::Silu,
22533            down_t: None,
22534            segs: Vec::new(),
22535        };
22536        let layers: Vec<LayerWeights> = (0..4)
22537            .map(|li| LayerWeights {
22538                input_norm: vec![1.0; hs],
22539                post_norm: vec![1.0; hs],
22540                attn_out_norm: None,
22541                ffn_out_norm: None,
22542                layer_scale: None,
22543                ffn: if li == 0 {
22544                    FfnKind::Dense(dense(inter, 50))
22545                } else {
22546                    FfnKind::Moe(MoeFfn {
22547                        router: qt(4, hs, 60 + li),
22548                        experts: (0..4).map(|e| dense(8, 70 + li * 10 + e * 3)).collect(),
22549                        top_k: 2,
22550                        norm_topk_prob: true,
22551                        router_sigmoid: true,
22552                        sigmoid_denom_eps: 1e-6,
22553                        expert_bias: Some(vec![0.02, -0.03, 0.01, 0.0]),
22554                        routed_scaling: 1.0,
22555                        route_tau: None,
22556                        shared: None,
22557                        stats: std::cell::RefCell::new(Vec::new()),
22558                        act_sq: std::cell::RefCell::new(Vec::new()),
22559                        act_rows: std::cell::RefCell::new(Vec::new()),
22560                        mask: None,
22561                        per_expert_scale: None,
22562                        router_input_norm: false,
22563                        resonance: None,
22564                        grown: Vec::new(),
22565                    })
22566                },
22567                attn: AttnKind::Full {
22568                    wq: qt(nh * hd, hs, li * 10 + 1),
22569                    wk: qt(kvh[li] * hd, hs, li * 10 + 2),
22570                    wv: qt(kvh[li] * vd, hs, li * 10 + 3),
22571                    wo: qt(hs, nh * vd, li * 10 + 4),
22572                    q_norm: None,
22573                    k_norm: None,
22574                    output_gate: false,
22575                    softplus_gate: None,
22576                    bias: None,
22577                },
22578            })
22579            .collect();
22580        let mut p = Pipeline::new(
22581            Tokenizer::byte_level(),
22582            PipelineWeights {
22583                embed_tokens: qt(vocab, hs, 100),
22584                layers,
22585                lm_head: qt(vocab, hs, 200),
22586                final_norm: vec![1.0; hs],
22587            },
22588            hs,
22589            inter,
22590            nh,
22591            1, // header num_kv_heads (the full layers')
22592            hd,
22593            4,
22594            4,
22595            false,
22596            vocab,
22597            1e-6,
22598            1e7,
22599            NormStyle::Qwen,
22600            4096,
22601            SamplerConfig {
22602                seed: Some(7),
22603                ..Default::default()
22604            },
22605        );
22606        // Diagnostics stay off whatever the test environment exports.
22607        p.layer_dump = None;
22608        p.set_rotary(4, 1e7);
22609        p.sliding_layers = Some(vec![false, true, true, false]);
22610        p.swa = Some((3, usize::MAX));
22611        p.rotary_dim_local = Some(4);
22612        p.inv_freq_local = Some(std::sync::Arc::new(attention::rope_inv_freq(4, 1e4)));
22613        p.set_attn_geometry(Some(kvh.to_vec()), Some(vd)).unwrap();
22614        p.set_layer_sinks(1, vec![0.5, -1.0, 1.5, 0.0]).unwrap();
22615        p.set_layer_sinks(2, vec![-0.25, 2.0, 0.75, -1.5]).unwrap();
22616        p
22617    }
22618
22619    #[test]
22620    fn mimo_embedded_prompt_uses_rows_and_never_reuses_token_only_kv() {
22621        let mut p = mimo_test_pipeline();
22622        p.speculative = false;
22623        p.ignore_eos = true;
22624        p.sampler_config.temperature = 0.0;
22625        p.sampler_config.repetition_penalty = 1.0;
22626        let a = vec![3, 5, 7, 9, 11, 13];
22627        let b = vec![4, 8, 12, 16, 20, 24];
22628        let rows: Vec<_> = b.iter().flat_map(|&id| p.embed_id(id)).collect();
22629        let expected = p.generate_from_ids(&b, 8, None, None).unwrap().token_ids;
22630        // Same placeholder IDs as an earlier request are not a cache key
22631        // for different media. The actual rows, not a re-embedding of a,
22632        // must determine the continuation.
22633        let actual = p
22634            .generate_from_embeds(&a, &rows, 8, None, None)
22635            .unwrap()
22636            .token_ids;
22637        assert_eq!(actual, expected);
22638        assert!(p.kv_history.is_empty());
22639        let mut extended = a.clone();
22640        extended.push(17);
22641        let after_media = p
22642            .generate_from_ids(&extended, 8, None, None)
22643            .unwrap()
22644            .token_ids;
22645        p.reset_session();
22646        let fresh = p
22647            .generate_from_ids(&extended, 8, None, None)
22648            .unwrap()
22649            .token_ids;
22650        assert_eq!(after_media, fresh);
22651        assert!(
22652            p.generate_from_embeds(&a, &rows[..rows.len() - 1], 1, None, None)
22653                .is_err()
22654        );
22655        // Force a real token-prefix reuse opportunity into the media call.
22656        // Those labels are unchanged, but their embeddings now describe a
22657        // different source sequence and every KV row must be rebuilt.
22658        p.reset_session();
22659        p.generate_from_ids(&a, 1, None, None).unwrap();
22660        let mut media_ids = p.kv_history.clone();
22661        assert!(!media_ids.is_empty());
22662        media_ids.extend_from_slice(&[19, 21, 23]);
22663        let source_ids: Vec<_> = (0..media_ids.len()).map(|i| b[i % b.len()]).collect();
22664        let source_rows: Vec<_> = source_ids.iter().flat_map(|&id| p.embed_id(id)).collect();
22665        let mut oracle = mimo_test_pipeline();
22666        oracle.speculative = false;
22667        oracle.ignore_eos = true;
22668        oracle.sampler_config.temperature = 0.0;
22669        oracle.sampler_config.repetition_penalty = 1.0;
22670        let expected = oracle
22671            .generate_from_ids(&source_ids, 8, None, None)
22672            .unwrap()
22673            .token_ids;
22674        assert_eq!(
22675            p.generate_from_embeds(&media_ids, &source_rows, 8, None, None)
22676                .unwrap()
22677                .token_ids,
22678            expected
22679        );
22680        assert!(p.kv_history.is_empty());
22681        let mut bad = rows;
22682        bad[0] = f32::NAN;
22683        assert!(p.generate_from_embeds(&a, &bad, 1, None, None).is_err());
22684    }
22685
22686    fn f32_bits(v: &[f32]) -> Vec<u32> {
22687        v.iter().map(|x| x.to_bits()).collect()
22688    }
22689
22690    /// M3 acceptance: on the MiMo-shaped stack the decode walk (one
22691    /// position at a time through `forward_layers`) and the batched
22692    /// prefill (`prefill_batch_span`, whole prompt and split in two
22693    /// chunks) give bit-identical logits at all 12 positions — per-layer
22694    /// KV heads, narrow V, sinks, the window and the biased sigmoid MoE all
22695    /// agree across the two walks.
22696    #[test]
22697    fn mimo_shaped_decode_matches_prefill_batch_bitwise() {
22698        let mut p = mimo_test_pipeline();
22699        let kv: Vec<usize> = p.kv_cache.layers.iter().map(|l| l.num_kv_heads).collect();
22700        assert_eq!(kv, vec![1, 2, 2, 1]);
22701        assert!(p.kv_cache.layers[0].sinks.is_none() && p.kv_cache.layers[3].sinks.is_none());
22702        assert!(p.kv_cache.layers[1].sinks.is_some() && p.kv_cache.layers[2].sinks.is_some());
22703        let ids: Vec<u32> = (0..12u32).map(|i| (i * 7 + 3) % 64).collect();
22704        let hs = p.hidden_size;
22705        let mut decode = Vec::new();
22706        for (pos, &id) in ids.iter().enumerate() {
22707            let e = p.embed_single(id);
22708            let h = p.forward_layers(&e, pos, None);
22709            decode.push(p.logits_from_hidden(&h));
22710        }
22711        for l in &p.kv_cache.layers {
22712            assert_eq!(l.seq_len, 12);
22713            // V rows are padded to head_dim inside the cache.
22714            assert_eq!(l.head_values(0).len(), 12 * 8);
22715        }
22716        assert!(decode.iter().flatten().all(|v| v.is_finite()));
22717
22718        p.clear_sequence_state();
22719        let hb = p.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, p.num_layers);
22720        for pos in 0..ids.len() {
22721            let lg = p.logits_from_hidden(&hb[pos * hs..(pos + 1) * hs]);
22722            assert_eq!(
22723                f32_bits(&decode[pos]),
22724                f32_bits(&lg),
22725                "whole prompt, pos {pos}"
22726            );
22727        }
22728
22729        p.clear_sequence_state();
22730        let a = p.prefill_batch_span(PrefillIn::Ids(&ids[..5]), 0, None, 0, p.num_layers);
22731        let b = p.prefill_batch_span(PrefillIn::Ids(&ids[5..]), 5, None, 0, p.num_layers);
22732        for pos in 0..ids.len() {
22733            let row = if pos < 5 {
22734                &a[pos * hs..(pos + 1) * hs]
22735            } else {
22736                &b[(pos - 5) * hs..(pos - 4) * hs]
22737            };
22738            let lg = p.logits_from_hidden(row);
22739            assert_eq!(
22740                f32_bits(&decode[pos]),
22741                f32_bits(&lg),
22742                "two chunks, pos {pos}"
22743            );
22744        }
22745
22746        // The fixture is not degenerate: the sinks and the window each
22747        // change the answer.
22748        let last = |p: &mut Pipeline| {
22749            p.clear_sequence_state();
22750            let hb = p.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, p.num_layers);
22751            p.logits_from_hidden(&hb[11 * hs..12 * hs])
22752        };
22753        let base = last(&mut p);
22754        let mut no_sinks = mimo_test_pipeline();
22755        for l in &mut no_sinks.kv_cache.layers {
22756            l.sinks = None;
22757        }
22758        assert_ne!(
22759            f32_bits(&last(&mut no_sinks)),
22760            f32_bits(&base),
22761            "sinks are live"
22762        );
22763        let mut wide = mimo_test_pipeline();
22764        wide.swa = Some((64, usize::MAX));
22765        assert_ne!(
22766            f32_bits(&last(&mut wide)),
22767            f32_bits(&base),
22768            "window is live"
22769        );
22770
22771        // Generation runs end to end on the same stack.
22772        p.clear_sequence_state();
22773        p.ignore_eos = true;
22774        let r = p.generate_from_ids(&ids, 4, None, None).unwrap();
22775        assert_eq!(r.token_ids.len(), 4);
22776    }
22777
22778    /// A Spark-X2.5-shaped stack: layers 0..3 slide (window 6), layer 3
22779    /// is global, every attention carries the head-wise sigmoid g_proj gate.
22780    fn spark_test_pipeline(trim: Option<(usize, usize)>) -> Pipeline {
22781        let (hs, nh) = (16usize, 4usize);
22782        let mut p = create_test_pipeline(hs, 24, nh, 2, 8, 4, 64);
22783        p.layer_dump = None;
22784        p.swa = Some((6, 4));
22785        p.proj_gate_sigmoid = true;
22786        for (li, lw) in p.weights.layers.iter_mut().enumerate() {
22787            if let AttnKind::Full { softplus_gate, .. } = &mut lw.attn {
22788                let g: Vec<f32> = (0..nh * hs)
22789                    .map(|i| (((i * 29 + li * 7) % 83) as f32 / 83.0 - 0.5) * 0.8)
22790                    .collect();
22791                *softplus_gate = Some((QTensor::from_f32(g, nh, hs), true));
22792            }
22793        }
22794        p.swa_trim = trim;
22795        p
22796    }
22797
22798    /// Trimming the sliding tails changes nothing but memory: the same
22799    /// prompt in uneven chunks, a decode walk, and fused pairs with a
22800    /// 2-row rollback give bit-identical hiddens with and without it,
22801    /// while the trimmed sliding layers stay bounded and the global layer
22802    /// keeps every row.
22803    #[test]
22804    fn swa_trim_pipeline_matches_untrimmed_bitwise() {
22805        let mut a = spark_test_pipeline(None);
22806        let mut b = spark_test_pipeline(Some((2, 4)));
22807        assert_eq!(
22808            (0..4).map(|li| b.layer_window(li)).collect::<Vec<_>>(),
22809            vec![Some(6), Some(6), Some(6), None]
22810        );
22811        let ids: Vec<u32> = (0..41u32).map(|i| (i * 11 + 5) % 64).collect();
22812        let mut pos = 0usize;
22813        for &n in [5usize, 13, 7, 16].iter().cycle() {
22814            if pos >= ids.len() {
22815                break;
22816            }
22817            let end = (pos + n).min(ids.len());
22818            let ha = a.prefill_batch_span(PrefillIn::Ids(&ids[pos..end]), pos, None, 0, 4);
22819            let hb = b.prefill_batch_span(PrefillIn::Ids(&ids[pos..end]), pos, None, 0, 4);
22820            assert_eq!(f32_bits(&ha), f32_bits(&hb), "prefill chunk at {pos}");
22821            pos = end;
22822        }
22823        for _ in 0..23 {
22824            let e = a.embed_single(((pos * 7) % 64) as u32);
22825            let ha = a.forward_layers(&e, pos, None);
22826            let hb = b.forward_layers(&e, pos, None);
22827            assert_eq!(f32_bits(&ha), f32_bits(&hb), "decode at {pos}");
22828            for li in 0..3 {
22829                assert!(b.kv_cache.layers[li].seq_len <= 12, "layer {li} bounded");
22830            }
22831            pos += 1;
22832        }
22833        for round in 0..9 {
22834            let (e1, e2) = (a.embed_single(round * 3 + 1), a.embed_single(round * 5 + 2));
22835            let (a1, a2) = a.forward_pair(&e1, &e2, pos);
22836            let (b1, b2) = b.forward_pair(&e1, &e2, pos);
22837            assert_eq!(f32_bits(&a1), f32_bits(&b1), "pair lane 1 round {round}");
22838            assert_eq!(f32_bits(&a2), f32_bits(&b2), "pair lane 2 round {round}");
22839            if round % 2 == 1 {
22840                // A rejected draft: both lanes roll back.
22841                for p in [&mut a, &mut b] {
22842                    for l in &mut p.kv_cache.layers {
22843                        l.truncate_last(2);
22844                    }
22845                }
22846            } else {
22847                pos += 2;
22848            }
22849        }
22850        let e = a.embed_single(9);
22851        let (ha, hb) = (
22852            a.forward_layers(&e, pos, None),
22853            b.forward_layers(&e, pos, None),
22854        );
22855        assert_eq!(f32_bits(&ha), f32_bits(&hb), "after the pairs");
22856        for li in 0..3 {
22857            let (la, lb) = (&a.kv_cache.layers[li], &b.kv_cache.layers[li]);
22858            assert!(lb.base() > 0, "layer {li} trimmed");
22859            assert_eq!(la.base(), 0);
22860            assert_eq!(lb.pos_len(), la.seq_len);
22861            assert_eq!(lb.head_keys(0), &la.head_keys(0)[lb.base() * 8..]);
22862        }
22863        let (ga, gb) = (&a.kv_cache.layers[3], &b.kv_cache.layers[3]);
22864        assert_eq!(
22865            (gb.base(), gb.seq_len),
22866            (0, ga.seq_len),
22867            "the global layer keeps all"
22868        );
22869        assert_eq!(b.kv_cache.seq_len(), a.kv_cache.seq_len());
22870        assert!(b.kv_cache.total_memory_bytes() < a.kv_cache.total_memory_bytes());
22871    }
22872
22873    /// The network split's KvFetch hand-off on a trimmed stack: every
22874    /// layer shipped over the wire mid-sequence (the sliding ones as
22875    /// `FullTail`) into a fresh pipeline that never ran a position, which
22876    /// then decodes on — bit for bit what the untrimmed pipeline decodes.
22877    #[test]
22878    fn swa_trim_wire_handoff_continues_bitwise() {
22879        let mut a = spark_test_pipeline(None);
22880        let mut b = spark_test_pipeline(Some((2, 4)));
22881        let ids: Vec<u32> = (0..29u32).map(|i| (i * 13 + 7) % 64).collect();
22882        let ha = a.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, 4);
22883        let hb = b.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, 4);
22884        assert_eq!(f32_bits(&ha), f32_bits(&hb));
22885        let mut pos = ids.len();
22886        for _ in 0..5 {
22887            let e = a.embed_single(((pos * 5) % 64) as u32);
22888            assert_eq!(
22889                f32_bits(&a.forward_layers(&e, pos, None)),
22890                f32_bits(&b.forward_layers(&e, pos, None))
22891            );
22892            pos += 1;
22893        }
22894        let mut c = spark_test_pipeline(Some((2, 4)));
22895        for li in 0..4 {
22896            let bytes = b.kv_cache.layers[li].export_wire(false).unwrap();
22897            c.kv_cache.layers[li].import_wire(&bytes).unwrap();
22898        }
22899        assert!(c.kv_cache.layers[0].base() > 0, "a tail travelled");
22900        assert_eq!(c.kv_cache.seq_len(), pos);
22901        for step in 0..17 {
22902            let e = a.embed_single(((pos * 3 + 1) % 64) as u32);
22903            let ha = a.forward_layers(&e, pos, None);
22904            let hc = c.forward_layers(&e, pos, None);
22905            assert_eq!(
22906                f32_bits(&ha),
22907                f32_bits(&hc),
22908                "after the hand-off, step {step}"
22909            );
22910            pos += 1;
22911        }
22912        assert!(c.kv_cache.layers[0].seq_len <= 12, "the receiver trims on");
22913    }
22914
22915    /// A synthetic MiMo draft stack for `mimo_test_pipeline` (the SWA
22916    /// geometry of its sliding layers: 2 KV heads, head 8 / V 4).  Widths
22917    /// are per-draft-layer so the MTP collector cannot accidentally borrow
22918    /// the trunk's `intermediate_size`.
22919    fn mimo_test_mtp_widths(widths: &[usize], gain: f32) -> mimo_mtp::MimoMtp {
22920        let (hs, nh, hd, vd, nkv) = (16usize, 4usize, 8usize, 4usize, 2usize);
22921        let synth = |len: usize, salt: usize| -> Vec<f32> {
22922            (0..len)
22923                .map(|i| (((i * 37 + salt * 13 + 3) % 89) as f32 / 89.0 - 0.5) * 0.6 * gain)
22924                .collect()
22925        };
22926        let qt = |rows: usize, cols: usize, salt: usize| {
22927            QTensor::from_f32(synth(rows * cols, salt), rows, cols)
22928        };
22929        let layers = widths
22930            .iter()
22931            .copied()
22932            .enumerate()
22933            .map(|(k, inter)| {
22934                let s = 500 + k * 40;
22935                let mut kv = crate::kv_cache::LayerKvCache::new(nkv, hd);
22936                kv.sinks = Some(vec![0.3, -0.7, 1.1, 0.0]);
22937                MtpModule {
22938                    enorm: vec![1.0; hs],
22939                    hnorm: vec![1.0; hs],
22940                    eh_proj: qt(hs, 2 * hs, s),
22941                    layer: LayerWeights {
22942                        input_norm: vec![1.0; hs],
22943                        post_norm: vec![1.0; hs],
22944                        attn_out_norm: None,
22945                        ffn_out_norm: None,
22946                        layer_scale: None,
22947                        attn: AttnKind::Full {
22948                            wq: qt(nh * hd, hs, s + 1),
22949                            wk: qt(nkv * hd, hs, s + 2),
22950                            wv: qt(nkv * vd, hs, s + 3),
22951                            wo: qt(hs, nh * vd, s + 4),
22952                            q_norm: None,
22953                            k_norm: None,
22954                            output_gate: false,
22955                            softplus_gate: None,
22956                            bias: None,
22957                        },
22958                        ffn: FfnKind::Dense(DenseFfn {
22959                            gate_proj: qt(inter, hs, s + 5),
22960                            up_proj: qt(inter, hs, s + 6),
22961                            down_proj: qt(hs, inter, s + 7),
22962                            act: Act::Silu,
22963                            down_t: None,
22964                            segs: Vec::new(),
22965                        }),
22966                    },
22967                    final_norm: vec![1.0; hs],
22968                    kv,
22969                }
22970            })
22971            .collect();
22972        mimo_mtp::MimoMtp::from_layers(layers)
22973    }
22974
22975    fn mimo_test_mtp(n: usize, gain: f32) -> mimo_mtp::MimoMtp {
22976        mimo_test_mtp_widths(&vec![24; n], gain)
22977    }
22978
22979    fn mimo_greedy(p: &mut Pipeline, ids: &[u32], n: usize, spec: bool) -> GenerateResult {
22980        p.clear_sequence_state();
22981        p.speculative = spec;
22982        p.ignore_eos = true;
22983        p.sampler_config.temperature = 0.0;
22984        p.generate_from_ids(ids, n, None, None).unwrap()
22985    }
22986
22987    #[test]
22988    fn mimo_mtp_ffn_probe_tracks_each_independent_draft_width() {
22989        let mut p = mimo_test_pipeline();
22990        let widths = [7usize, 13, 29];
22991        assert_ne!(widths[0], p.intermediate_size);
22992        assert_ne!(widths[1], p.intermediate_size);
22993        assert_ne!(widths[2], p.intermediate_size);
22994        p.mimo_mtp = Some(mimo_test_mtp_widths(&widths, 1.0));
22995        let ids: Vec<u32> = (0..11).map(|i| (i * 9 + 5) % 64).collect();
22996
22997        let trunk = p.probe_ffn_mass(&ids);
22998        assert!(trunk.iter().all(|v| v.len() == p.intermediate_size));
22999        let got = p.probe_mtp_ffn(&ids).expect("MiMo MTP probe");
23000        assert_eq!(got.len(), widths.len());
23001        for (k, (stat, &width)) in got.iter().zip(&widths).enumerate() {
23002            assert_eq!(stat.mass.len(), width, "draft layer {k}");
23003            assert_eq!(stat.squared_mass.len(), width, "draft layer {k}");
23004            assert_eq!(stat.observed_positions, ids.len() - k - 1);
23005            assert!(stat.mass.iter().any(|&v| v > 0.0), "draft layer {k}");
23006            assert!(stat
23007                .mass
23008                .iter()
23009                .zip(&stat.squared_mass)
23010                .all(|(&m, &s)| m.is_finite() && s.is_finite() && m >= 0.0 && s >= 0.0));
23011        }
23012        assert_eq!(p.kv_cache.seq_len(), 0, "probe leaves a fresh trunk");
23013        let st = p.mimo_mtp.as_ref().unwrap();
23014        assert!(st.layers.iter().all(|m| m.kv.seq_len == 0));
23015
23016        // As above, reject only after a MiMo draft KV has been appended.
23017        // The detached multi-layer owner must still be reset on that error.
23018        let h = p.hidden_size;
23019        let FfnKind::Dense(dense) = &mut p.mimo_mtp.as_mut().unwrap().layers[0].layer.ffn else {
23020            panic!("synthetic MiMo MTP must be dense")
23021        };
23022        dense.segs.push(FfnSeg {
23023            gate: QTensor::from_f32(vec![0.0; h], 1, h),
23024            up: QTensor::from_f32(vec![0.0; h], 1, h),
23025            down: QTensor::from_f32(vec![0.0; h], h, 1),
23026            start: dense.gate_proj.rows(),
23027            width: 1,
23028        });
23029        assert!(p
23030            .probe_mtp_ffn(&ids)
23031            .unwrap_err()
23032            .contains("tube-segmented draft FFNs are not supported"));
23033        assert_eq!(p.kv_cache.seq_len(), 0);
23034        assert!(p
23035            .mimo_mtp
23036            .as_ref()
23037            .unwrap()
23038            .layers
23039            .iter()
23040            .all(|m| m.kv.seq_len == 0));
23041    }
23042
23043    #[test]
23044    fn mimo_mtp_ffn_probe_chunking_preserves_teacher_forced_statistics() {
23045        fn collect(p: &mut Pipeline, ids: &[u32], rows: usize) -> Vec<MtpFfnProbe> {
23046            p.clear_sequence_state();
23047            let mut st = p.mimo_mtp.take().expect("synthetic MiMo draft");
23048            let _exact = ExactMtpCalibrationGuard::enter();
23049            let got = crate::gpu::cpu_scope(|| {
23050                p.mimo_mtp_ffn_probe_teacher_forced_with_chunk_rows(&mut st, ids, rows)
23051            })
23052            .expect("teacher-forced MiMo probe");
23053            st.reset();
23054            p.mimo_mtp = Some(st);
23055            p.reset_mtp_probe_state();
23056            got
23057        }
23058
23059        for post_norm_hidden in [false, true] {
23060            let ids: Vec<u32> = (0..263u32).map(|i| (i * 17 + 5) % 64).collect();
23061            let make = || {
23062                let mut p = mimo_test_pipeline();
23063                let mut st = mimo_test_mtp_widths(&[7, 13, 29], 1.0);
23064                st.post_norm_hidden = post_norm_hidden;
23065                p.mimo_mtp = Some(st);
23066                p
23067            };
23068            let mut full = make();
23069            let expected = collect(&mut full, &ids, usize::MAX);
23070            for rows in [1usize, 7, 128] {
23071                let mut chunked = make();
23072                let got = collect(&mut chunked, &ids, rows);
23073                assert_eq!(got.len(), expected.len(), "post={post_norm_hidden}, rows={rows}");
23074                for (layer, (got, expected)) in got.iter().zip(&expected).enumerate() {
23075                    assert_eq!(
23076                        got.observed_positions, expected.observed_positions,
23077                        "post={post_norm_hidden}, rows={rows}, layer={layer}"
23078                    );
23079                    assert_eq!(
23080                        got.mass.iter().map(|v| v.to_bits()).collect::<Vec<_>>(),
23081                        expected.mass.iter().map(|v| v.to_bits()).collect::<Vec<_>>(),
23082                        "post={post_norm_hidden}, rows={rows}, layer={layer} mass"
23083                    );
23084                    assert_eq!(
23085                        got.squared_mass
23086                            .iter()
23087                            .map(|v| v.to_bits())
23088                            .collect::<Vec<_>>(),
23089                        expected
23090                            .squared_mass
23091                            .iter()
23092                            .map(|v| v.to_bits())
23093                            .collect::<Vec<_>>(),
23094                        "post={post_norm_hidden}, rows={rows}, layer={layer} squared mass"
23095                    );
23096                }
23097            }
23098        }
23099    }
23100
23101    /// The draft stack's incremental rounds (a few rows per layer, last
23102    /// round's provisional rows dropped) give exactly the teacher-forced
23103    /// table of one causal pass per layer over the whole sequence — the
23104    /// table `tools/mimo_ref.py mtp` computes for variant A: layer k, row
23105    /// j reads (x[j+k+1], norm(h_j)) at RoPE position j.
23106    #[test]
23107    fn mimo_mtp_incremental_rounds_equal_the_teacher_forced_table() {
23108        // Both readings of the backbone hidden: pre-final-norm (default)
23109        // and post-final-norm (`CMF_MIMO_MTP_HIDDEN=post`).
23110        for post in [false, true] {
23111            let mut p = mimo_test_pipeline();
23112            // A non-trivial final norm, so the two readings differ.
23113            p.weights.final_norm = (0..p.hidden_size).map(|i| 0.5 + 0.1 * i as f32).collect();
23114            let mut st0 = mimo_test_mtp(3, 1.0);
23115            st0.post_norm_hidden = post;
23116            p.mimo_mtp = Some(st0);
23117            let ids: Vec<u32> = (0..14u32).map(|i| (i * 11 + 5) % 64).collect();
23118            let hs = p.hidden_size;
23119            let hb = p.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, p.num_layers);
23120            p.mimo_note_rows(&hb, 0);
23121            let mut st = p.mimo_mtp.take().unwrap();
23122            // Incremental: one round per t through the decode path (later
23123            // tokens from `ids`, the probe's teacher forcing).
23124            let k = 3;
23125            let mut inc = Vec::new();
23126            for t in 0..ids.len() - k - 1 {
23127                inc.push(p.mimo_mtp_draft(&mut st, t, &ids, k));
23128            }
23129            // Reference: per layer, ONE batched causal pass over all rows
23130            // with fresh caches.
23131            let s = ids.len();
23132            let mut reference = vec![vec![0u32; k]; s - k - 1];
23133            let mut fresh = mimo_test_mtp(3, 1.0);
23134            for (layer, m) in fresh.layers.iter_mut().enumerate() {
23135                let n = s - layer - 1;
23136                let mut cats = vec![0.0f32; n * 2 * hs];
23137                for j in 0..n {
23138                    let e = p.embed_single(ids[j + layer + 1]);
23139                    let raw = &hb[j * hs..(j + 1) * hs];
23140                    let g = if post {
23141                        inference::rms_norm(raw, &p.weights.final_norm, p.rms_eps, p.norm_style)
23142                    } else {
23143                        raw.to_vec()
23144                    };
23145                    let (ce, ch) = cats[j * 2 * hs..(j + 1) * 2 * hs].split_at_mut(hs);
23146                    inference::rms_norm_into(&e, &m.enorm, p.rms_eps, p.norm_style, ce);
23147                    inference::rms_norm_into(&g, &m.hnorm, p.rms_eps, p.norm_style, ch);
23148                }
23149                let mut x = vec![0.0f32; n * hs];
23150                m.eh_proj.matmat(&cats, n, &mut x, None);
23151                p.mimo_mtp_block(m, &mut x, n, 0);
23152                for (t, row) in reference.iter_mut().enumerate() {
23153                    let y = inference::rms_norm(
23154                        &x[t * hs..(t + 1) * hs],
23155                        &m.final_norm,
23156                        p.rms_eps,
23157                        p.norm_style,
23158                    );
23159                    row[layer] = sampler::argmax(&p.lm_head_forward(&y));
23160                }
23161            }
23162            assert_eq!(inc, reference, "post_norm_hidden = {post}");
23163            // Not a degenerate table: the drafts vary.
23164            let distinct: std::collections::HashSet<u32> = inc.iter().flatten().copied().collect();
23165            assert!(distinct.len() > 3, "{inc:?}");
23166            // Each layer's cache ends holding rows up to the last round start.
23167            let last_t = ids.len() - k - 2;
23168            for m in &st.layers {
23169                assert_eq!(m.kv.seq_len, last_t + 1);
23170            }
23171        }
23172    }
23173
23174    /// Greedy with the MiMo draft stack is the plain greedy stream, token
23175    /// for token — with the real draft layers (low acceptance) and with a
23176    /// drafter that is right most of the time (exercises accepted prefixes
23177    /// of every length, the KV truncation of the rejected rows and the
23178    /// logits hand-off to the loop top), under the default repetition
23179    /// penalty.
23180    #[test]
23181    fn mimo_speculative_greedy_equals_plain_greedy() {
23182        let _env_lock = MTP_ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
23183        let _env = ScopedTestEnv::set(&[
23184            ("CMF_GPU", Some("0")),
23185            ("CMF_GPU_WGPU_GRAPH", Some("0")),
23186        ]);
23187        let ids: Vec<u32> = (0..9u32).map(|i| (i * 7 + 3) % 64).collect();
23188        let n = 24;
23189        let mut p = mimo_test_pipeline();
23190        let plain = mimo_greedy(&mut p, &ids, n, false);
23191        assert_eq!(plain.mtp_drafted, 0);
23192        assert_eq!(plain.token_ids.len(), n);
23193        let plain_kv = p.kv_cache.layers[0].seq_len;
23194
23195        // Real draft layers.
23196        p.mimo_mtp = Some(mimo_test_mtp(3, 1.0));
23197        let spec = mimo_greedy(&mut p, &ids, n, true);
23198        assert!(spec.mtp_drafted > 0, "the round must draft");
23199        assert_eq!(spec.token_ids, plain.token_ids);
23200        assert_eq!(p.kv_cache.layers[0].seq_len, plain_kv);
23201
23202        // A drafter reading the true continuation with every fifth token
23203        // wrong: accepted prefixes of 0..=3 all occur.
23204        let mut truth: Vec<u32> = ids.clone();
23205        truth.extend(&plain.token_ids);
23206        let mut noisy = truth.clone();
23207        for (i, t) in noisy.iter_mut().enumerate() {
23208            if i % 5 == 0 {
23209                *t = (*t + 1) % 64;
23210            }
23211        }
23212        let mut st = mimo_test_mtp(3, 1.0);
23213        st.draft_override = Some(noisy);
23214        p.mimo_mtp = Some(st);
23215        let spec = mimo_greedy(&mut p, &ids, n, true);
23216        assert_eq!(spec.token_ids, plain.token_ids);
23217        assert_eq!(p.kv_cache.layers[0].seq_len, plain_kv);
23218        let stats = p.mimo_mtp.as_ref().unwrap().stats.clone();
23219        assert_eq!(stats.accepted as usize, spec.mtp_accepted);
23220        assert!(spec.mtp_accepted > 0 && spec.mtp_accepted < spec.mtp_drafted);
23221        assert!(
23222            stats.accept_hist.iter().filter(|&&c| c > 0).count() >= 3,
23223            "{:?}",
23224            stats.accept_hist
23225        );
23226        assert!(stats.tokens_per_round() > 1.5, "{}", stats.line());
23227
23228        // A perfect drafter: every draft accepted, rounds of K+1 tokens,
23229        // and the budget is never overrun.
23230        let mut st = mimo_test_mtp(3, 1.0);
23231        st.draft_override = Some(truth);
23232        p.mimo_mtp = Some(st);
23233        let spec = mimo_greedy(&mut p, &ids, n, true);
23234        assert_eq!(spec.token_ids, plain.token_ids);
23235        assert_eq!(spec.mtp_accepted, spec.mtp_drafted);
23236        assert_eq!(p.kv_cache.layers[0].seq_len, plain_kv);
23237
23238        // CMF_MTP=0 path: the stack is attached but idle.
23239        let off = mimo_greedy(&mut p, &ids, n, false);
23240        assert_eq!(off.token_ids, plain.token_ids);
23241        assert_eq!(off.mtp_drafted, 0);
23242    }
23243
23244    /// The wgpu graphs carry MiMo-V2's attention per layer (KV heads,
23245    /// narrow V, sinks, windows, two RoPE tables): no attention-level
23246    /// decline for it any more, and the geometry each layer hands the
23247    /// graph is exactly what the CPU attention reads for that layer. The
23248    /// descriptive reasons stay (the Metal graphs and the q1 dropin still
23249    /// decline on them), and what the per-layer geometry cannot express
23250    /// keeps a named wgpu decline.
23251
23252    #[test]
23253    fn mimo_shaped_model_rides_the_wgpu_graph_geometry() {
23254        let p = mimo_test_pipeline();
23255        assert_eq!(
23256            p.graph_attn_decline_reason(),
23257            Some("per-layer KV head counts")
23258        );
23259        assert_eq!(p.wgpu_graph_attn_decline(), None);
23260        let g0 = p.graph_attn_geom(0).expect("full layer geometry");
23261        assert_eq!(
23262            (g0.nkv, g0.dv, g0.rd, g0.window, g0.sink.is_some()),
23263            (1, 4, 4, None, false)
23264        );
23265        assert_eq!(g0.invf, p.inv_freq.as_slice());
23266        let g1 = p.graph_attn_geom(1).expect("sliding layer geometry");
23267        assert_eq!((g1.nkv, g1.dv, g1.rd, g1.window), (2, 4, 4, Some(3)));
23268        assert_eq!(g1.sink, Some(&[0.5f32, -1.0, 1.5, 0.0][..]));
23269        assert_eq!(g1.invf, p.inv_freq_local.as_ref().unwrap().as_slice());
23270        assert_ne!(g0.invf, g1.invf, "two RoPE tables");
23271        let g3 = p.graph_attn_geom(3).expect("full layer geometry");
23272        assert_eq!((g3.nkv, g3.window, g3.sink.is_some()), (1, None, false));
23273
23274        // No wgpu device in this process: the builders run and decline on
23275        // the (f32, unmapped) experts — never with an attention line.
23276        let emb = p.embed_single(3);
23277        let mut lg = Vec::new();
23278        assert!(
23279            p.try_token_graph_wgpu_steps(&emb, 0, &mut lg, 1, None, None, 0, p.num_layers)
23280                .is_none()
23281        );
23282        let mut hid = emb.clone();
23283        assert_eq!(
23284            p.try_batch_graph_wgpu(&mut hid, &[0], 1, None),
23285            crate::gpu::BatchGraphOutcome::Declined
23286        );
23287        assert_eq!(hid, emb, "a declined batch graph leaves the rows untouched");
23288        assert!(p.try_multi_burst(3, 0, 4).is_none());
23289        assert!(
23290            p.graph_declines().is_empty(),
23291            "no attention decline logged: {:?}",
23292            p.graph_declines()
23293        );
23294        // (No assertion on graph_prefill_preferred: with no attention
23295        // decline it follows the device — a test process that brought a
23296        // wgpu adapter up routes this resident MoE through the graph.)
23297
23298        let plain = || create_test_pipeline(8, 16, 2, 1, 4, 2, 32);
23299        assert_eq!(plain().graph_attn_decline_reason(), None);
23300        assert_eq!(plain().wgpu_graph_attn_decline(), None);
23301        assert!(
23302            plain().graph_attn_geom(0).is_none(),
23303            "uniform models keep the historical arms"
23304        );
23305        let mut q = plain();
23306        q.set_layer_sinks(1, vec![0.25, -0.25]).unwrap();
23307        assert_eq!(
23308            q.graph_attn_decline_reason(),
23309            Some("learned attention sinks")
23310        );
23311        assert_eq!(
23312            q.graph_attn_geom(1).unwrap().sink,
23313            Some(&[0.25f32, -0.25][..])
23314        );
23315        let mut q = plain();
23316        q.set_attn_geometry(None, Some(2)).unwrap();
23317        assert_eq!(
23318            q.graph_attn_decline_reason(),
23319            Some("V heads narrower than Q/K heads")
23320        );
23321        assert_eq!(q.graph_attn_geom(0).unwrap().dv, 2);
23322        let mut q = plain();
23323        q.sliding_layers = Some(vec![true, false]);
23324        q.swa = Some((4, usize::MAX));
23325        assert_eq!(q.graph_attn_decline_reason(), Some("sliding-window layers"));
23326        assert_eq!(q.graph_attn_geom(0).unwrap().window, Some(4));
23327        assert_eq!(q.graph_attn_geom(1).unwrap().window, None);
23328
23329        // YaRN's post-rotation amplitude factor rides the per-layer graph
23330        // geometry. It is intentionally not an angular scale folded into
23331        // `invf`: the WGPU RoPE kernel receives it separately.
23332        let mut q = mimo_test_pipeline();
23333        q.rope_scale = 2.0;
23334        q.rope_scale_local = 1.25;
23335        assert_eq!(q.wgpu_graph_attn_decline(), None);
23336        assert_eq!(q.graph_attn_geom(0).unwrap().rope_scale, 2.0);
23337        assert_eq!(q.graph_attn_geom(1).unwrap().rope_scale, 1.25);
23338
23339        // A uniform YaRN model cannot yet pass the factor through the fused
23340        // RKV arm; decline safely instead of producing scale-1 output.
23341        let mut q = plain();
23342        q.rope_scale = 2.0;
23343        assert_eq!(
23344            q.wgpu_graph_attn_decline(),
23345            Some("scaled RoPE without per-layer geometry")
23346        );
23347    }
23348
23349    /// The native q1 attention path has a narrower contract than the WGPU
23350    /// per-layer geometry graph, but it must not discard the exact Flint
23351    /// combination of uniform GQA + sliding windows + learned sinks.  The
23352    /// actual Metal kernel is covered independently with a direct sink
23353    /// softmax test; this is the pure admission test, with no model run.
23354    #[cfg(target_os = "macos")]
23355    #[test]
23356    fn metal_swa_admits_uniform_learned_sinks_for_q1_decode() {
23357        // rd=64 is in the native rope kernel's lane-pair contract; the
23358        // synthetic stack otherwise has the same uniform K/V geometry that
23359        // Flint needs for q1 (unlike the deliberately ragged MiMo fixture).
23360        let mut p = create_test_pipeline(256, 512, 4, 1, 64, 2, 64);
23361        p.swa = Some((16, usize::MAX));
23362        p.sliding_layers = Some(vec![true, true]);
23363        p.set_layer_sinks(0, vec![0.25, -0.5, 1.0, -1.25]).unwrap();
23364        p.set_layer_sinks(1, vec![-0.75, 0.5, 0.0, 1.5]).unwrap();
23365
23366        assert_eq!(
23367            p.graph_attn_decline_reason(),
23368            Some("learned attention sinks")
23369        );
23370        let swa_graph = p.metal_graph_swa();
23371        assert!(swa_graph, "uniform sliding sink geometry must reach q1");
23372        let native_sink_attend = p.graph_attn_decline_reason() == Some("learned attention sinks")
23373            && (swa_graph || (p.swa.is_none() && p.sliding_layers.is_none()));
23374        assert!(
23375            native_sink_attend,
23376            "the q1 sink admission must follow the SWA door"
23377        );
23378        // The batched chunk softmax has no value-less sink column, so its
23379        // separate admission gate must keep this prompt on the host path.
23380        assert!(p.kv_cache.layers.iter().any(|l| l.sinks.is_some()));
23381    }
23382
23383    #[test]
23384    fn mimo_verify_rewind_preserves_lagging_host_caches() {
23385        let mut p = mimo_test_pipeline();
23386        for (li, layer) in p.kv_cache.layers.iter_mut().enumerate() {
23387            let row = vec![0.0; layer.num_kv_heads * layer.head_dim];
23388            for _ in 0..if li == 0 { 2 } else { 12 } {
23389                layer.append(&row, &row, &[]);
23390            }
23391        }
23392        p.mimo_verify_rewind(9).unwrap();
23393        assert_eq!(p.kv_cache.layers[0].seq_len, 2);
23394        for layer in &p.kv_cache.layers[1..] {
23395            assert_eq!(layer.seq_len, 9);
23396        }
23397    }
23398
23399    /// CMF_LAYER_DUMP: the decode walk and the batched prefill both write
23400    /// every (position, layer) hidden, the two sets agree byte for byte,
23401    /// and the last layer's file is the stack output.
23402    #[test]
23403    fn layer_dump_covers_every_position_and_layer_on_both_walks() {
23404        let dir = std::env::temp_dir().join(format!("cmf-layer-dump-{}", std::process::id()));
23405        let _ = std::fs::remove_dir_all(&dir);
23406        let mut p = mimo_test_pipeline();
23407        let hs = p.hidden_size;
23408        let ids = [5u32, 9, 11, 2, 40];
23409        p.layer_dump = Some(dir.join("decode"));
23410        for (pos, &id) in ids.iter().enumerate() {
23411            let e = p.embed_single(id);
23412            let _ = p.forward_layers(&e, pos, None);
23413        }
23414        p.clear_sequence_state();
23415        p.layer_dump = Some(dir.join("prefill"));
23416        let hb = p.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, p.num_layers);
23417        for pos in 0..ids.len() {
23418            for li in 0..p.num_layers {
23419                let name = format!("p{pos:06}_l{li:02}.f32");
23420                let a = std::fs::read(dir.join("decode").join(&name)).unwrap();
23421                let b = std::fs::read(dir.join("prefill").join(&name)).unwrap();
23422                assert_eq!(a.len(), hs * 4, "{name}");
23423                assert_eq!(a, b, "{name}");
23424            }
23425        }
23426        let last = std::fs::read(dir.join("prefill").join("p000004_l03.f32")).unwrap();
23427        let vals: Vec<f32> = last
23428            .chunks(4)
23429            .map(|c| f32::from_le_bytes(c.try_into().unwrap()))
23430            .collect();
23431        assert_eq!(f32_bits(&vals), f32_bits(&hb[4 * hs..5 * hs]));
23432        let _ = std::fs::remove_dir_all(&dir);
23433    }
23434
23435    #[test]
23436    fn attn_geometry_and_sinks_are_validated() {
23437        let mut p = create_test_pipeline(8, 16, 4, 2, 4, 2, 32);
23438        assert!(
23439            p.set_attn_geometry(Some(vec![2]), None).is_err(),
23440            "one entry per layer"
23441        );
23442        assert!(
23443            p.set_attn_geometry(Some(vec![2, 3]), None).is_err(),
23444            "3 does not divide 4"
23445        );
23446        assert!(p.set_attn_geometry(Some(vec![2, 0]), None).is_err());
23447        assert!(p.set_attn_geometry(None, Some(0)).is_err());
23448        assert!(
23449            p.set_attn_geometry(None, Some(5)).is_err(),
23450            "V wider than the head"
23451        );
23452        p.set_attn_geometry(None, Some(4)).unwrap();
23453        assert_eq!(
23454            p.v_head_dim, None,
23455            "v_head_dim == head_dim is the uniform case"
23456        );
23457        p.set_layer_sinks(1, vec![0.1; 4]).unwrap();
23458        p.set_attn_geometry(Some(vec![1, 4]), None).unwrap();
23459        assert_eq!(p.kv_cache.layers[0].num_kv_heads, 1);
23460        assert_eq!(p.kv_cache.layers[1].num_kv_heads, 4);
23461        assert!(
23462            p.kv_cache.layers[1].sinks.is_some(),
23463            "a reshape keeps the layer's sinks"
23464        );
23465        assert_eq!(p.layer_geom(1).0, 4);
23466        assert!(
23467            p.set_layer_sinks(0, vec![0.0; 3]).is_err(),
23468            "one sink per Q head"
23469        );
23470        assert!(p.set_layer_sinks(7, vec![0.0; 4]).is_err());
23471        assert!(p.set_layer_sinks(0, vec![f32::NAN, 0.0, 0.0, 0.0]).is_err());
23472    }
23473
23474    /// The O(1) Nyström state replaces a plain full-context softmax; it
23475    /// must never be armed on a sliding, sink or narrow-V layer.
23476    #[test]
23477    fn o1_is_never_armed_on_sink_window_or_narrow_v_layers() {
23478        let cfg = || {
23479            Some(crate::nystrom::O1Cfg {
23480                layers: crate::nystrom::O1Layers::All,
23481                m: 4,
23482                w: 8,
23483                sink: 2,
23484                rect: crate::nystrom::O1Rect::Aggregate,
23485            })
23486        };
23487        let mut p = mimo_test_pipeline();
23488        p.set_o1(cfg());
23489        assert!(!p.o1_active(), "every MiMo-shaped layer is ineligible");
23490        let mut q = create_test_pipeline(8, 16, 2, 1, 4, 3, 64);
23491        q.set_layer_sinks(1, vec![0.0, 0.0]).unwrap();
23492        q.sliding_layers = Some(vec![false, false, true]);
23493        q.swa = Some((4, usize::MAX));
23494        q.set_o1(cfg());
23495        assert_eq!(q.o1_flags, vec![true, false, false]);
23496    }
23497
23498    #[test]
23499    fn o1_batch_transition_publishes_one_epoch_before_serial_handoff() {
23500        const B: usize = 19;
23501        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
23502        p.set_o1(Some(crate::nystrom::O1Cfg {
23503            layers: crate::nystrom::O1Layers::All,
23504            m: 4,
23505            w: 8,
23506            sink: 2,
23507            rect: crate::nystrom::O1Rect::Aggregate,
23508        }));
23509        p.o1_begin_with_prefix(Some(B));
23510        let ids: Vec<u32> = (0..B as u32).collect();
23511        let _ = p.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, p.num_layers);
23512
23513        assert_eq!(p.o1_epoch, 1, "all layers publish one completed transition");
23514        assert!(p.kv_cache.layers.iter().all(|l| l.o1_sealed()));
23515        let next = p.embed_single(B as u32);
23516        let _ = p.forward_layers(&next, B, None);
23517        assert_eq!(p.o1_epoch, 1, "sealed handoff must not republish the epoch");
23518    }
23519
23520    #[test]
23521    fn o1_pair_transition_commits_scratch_before_epoch_publication() {
23522        const B: usize = 19;
23523        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
23524        // Keep a real recurrent layer ahead of the Full O(1) layer so the
23525        // pair test observes the GDN lane-2 scratch swap at the same
23526        // boundary, rather than only exercising an artificial scratch vec.
23527        let gdn_cfg = crate::linear_core::GdnCfg {
23528            num_v_heads: 2,
23529            num_k_heads: 1,
23530            key_head_dim: 2,
23531            value_head_dim: 4,
23532            conv_kernel: 3,
23533            hidden_size: 8,
23534            rms_eps: 1e-6,
23535            output_gate_sigmoid: false,
23536        };
23537        let synth = |n: usize, salt: usize| -> Vec<f32> {
23538            (0..n)
23539                .map(|i| (((i * 13 + salt * 7) % 97) as f32 / 97.0 - 0.5) * 0.4)
23540                .collect()
23541        };
23542        let qt = |rows: usize, cols: usize, salt: usize| {
23543            crate::qtensor::QTensor::from_f32(synth(rows * cols, salt), rows, cols)
23544        };
23545        let c_dim = gdn_cfg.conv_dim();
23546        let vd = gdn_cfg.num_v_heads * gdn_cfg.value_head_dim;
23547        p.weights.layers[0].attn = AttnKind::LinearGdn(crate::linear_core::GdnWeights {
23548            in_proj_qkv: qt(c_dim, 8, 1),
23549            in_proj_z: qt(vd, 8, 2),
23550            in_proj_a: qt(gdn_cfg.num_v_heads, 8, 3),
23551            in_proj_b: qt(gdn_cfg.num_v_heads, 8, 4),
23552            conv1d: synth(c_dim * gdn_cfg.conv_kernel, 5),
23553            a_log: vec![0.2, 0.5],
23554            dt_bias: synth(gdn_cfg.num_v_heads, 6),
23555            norm: vec![1.0; gdn_cfg.value_head_dim],
23556            out_proj: qt(8, vd, 7),
23557        });
23558        p.gdn_cfg = Some(gdn_cfg);
23559        p.set_o1(Some(crate::nystrom::O1Cfg {
23560            layers: crate::nystrom::O1Layers::All,
23561            m: 4,
23562            w: 8,
23563            sink: 2,
23564            rect: crate::nystrom::O1Rect::Aggregate,
23565        }));
23566        p.o1_begin_with_prefix(Some(B));
23567        for pos in 0..B - 2 {
23568            let emb = p.embed_single(pos as u32);
23569            let _ = p.forward_layers(&emb, pos, None);
23570        }
23571        let lane1_state = p.kv_cache.layers[0].linear_state.clone();
23572
23573        let e1 = p.embed_single((B - 2) as u32);
23574        let e2 = p.embed_single((B - 1) as u32);
23575        let _ = p.forward_pair(&e1, &e2, B - 2);
23576
23577        assert_eq!(p.o1_epoch, 1, "pair crossing B publishes one epoch");
23578        assert!(
23579            p.kv_cache
23580                .layers
23581                .iter()
23582                .enumerate()
23583                .all(|(li, l)| !p.o1_flags[li] || l.o1_sealed())
23584        );
23585        assert!(!p.kv_cache.layers[0].linear_state.is_empty());
23586        assert_ne!(
23587            p.kv_cache.layers[0].linear_state, lane1_state,
23588            "real pair must commit GDN lane 2 before returning"
23589        );
23590        assert!(p.kv_cache.layers[0].linear_scratch.is_empty());
23591        let next = p.embed_single(B as u32);
23592        let _ = p.forward_layers(&next, B, None);
23593        assert_eq!(p.o1_epoch, 1, "serial continuation must reuse the epoch");
23594    }
23595
23596    #[test]
23597    fn o1_error_observation_stays_terminal_until_reset() {
23598        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
23599        p.set_o1(Some(crate::nystrom::O1Cfg {
23600            layers: crate::nystrom::O1Layers::All,
23601            m: 4,
23602            w: 8,
23603            sink: 2,
23604            rect: crate::nystrom::O1Rect::Aggregate,
23605        }));
23606        p.o1_begin();
23607        p.kv_cache.layers[0].o1_abort("synthetic transition failure".into());
23608
23609        assert!(p.o1_seal_checked().is_err());
23610        assert!(
23611            p.o1_seal_checked().is_err(),
23612            "retry must see the sticky error"
23613        );
23614        let k = vec![0.2f32; 4];
23615        let v = vec![0.3f32; 4];
23616        p.kv_cache.layers[0].append(&k, &v, &[]);
23617        assert_eq!(p.kv_cache.layers[0].seq_len, 0);
23618
23619        p.reset_session();
23620        p.o1_begin();
23621        p.kv_cache.layers[0].append(&k, &v, &[]);
23622        assert_eq!(p.kv_cache.layers[0].seq_len, 1);
23623    }
23624
23625    #[test]
23626    fn nll_graph_failure_is_terminal_and_request_is_reusable() {
23627        let ids = vec![1u32, 2, 3, 4, 5, 6];
23628        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
23629        p.graph_logits = Some(vec![123.0]);
23630        p.graph_want_logits = true;
23631        p.graph_failed
23632            .store(true, std::sync::atomic::Ordering::Relaxed);
23633        p.cancel.store(true, std::sync::atomic::Ordering::Relaxed);
23634        let err = p.nll_ids_from(&ids, 0).expect_err("prior graph failure");
23635        assert!(err.contains("before NLL"));
23636        assert!(p.graph_logits.is_none());
23637        assert!(!p.graph_want_logits);
23638        assert!(!p.graph_failed.load(std::sync::atomic::Ordering::Relaxed));
23639        assert!(!p.cancel.load(std::sync::atomic::Ordering::Relaxed));
23640
23641        let mut fresh = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
23642        let expected = fresh.nll_ids_from(&ids, 0).expect("fresh NLL");
23643        let actual = p.nll_ids_from(&ids, 0).expect("reused NLL");
23644        assert_eq!(actual.1, expected.1);
23645        assert!((actual.0 - expected.0).abs() < 1e-9);
23646    }
23647
23648    #[test]
23649    fn nll_forward_failure_discards_partial_score_and_clears_sidechannels() {
23650        let ids = vec![1u32, 2, 3, 4, 5, 6];
23651        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
23652        p.nll_test_fail_at = Some(1);
23653        let err = p
23654            .nll_ids_from(&ids, 0)
23655            .expect_err("one-shot forward failure");
23656        assert!(err.contains("forward") || err.contains("score row"));
23657        assert!(!p.graph_failed.load(std::sync::atomic::Ordering::Relaxed));
23658        assert!(!p.graph_want_logits);
23659        assert!(p.graph_logits.is_none());
23660        assert!(p.kv_history.is_empty());
23661
23662        let mut fresh = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
23663        let expected = fresh.nll_ids_from(&ids, 0).expect("fresh NLL");
23664        let actual = p.nll_ids_from(&ids, 0).expect("reused NLL");
23665        assert_eq!(actual.1, expected.1);
23666        assert!((actual.0 - expected.0).abs() < 1e-9);
23667    }
23668
23669    #[test]
23670    fn nll_serial_failure_before_first_row_is_reported() {
23671        let ids = vec![1u32, 2, 3, 4];
23672        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
23673        p.nll_test_force_serial = true;
23674        p.nll_test_fail_at = Some(0);
23675        let err = p.nll_ids_from(&ids, 0).expect_err("serial forward failure");
23676        assert!(err.contains("serial forward"));
23677        assert!(p.kv_history.is_empty());
23678        assert!(!p.graph_failed.load(std::sync::atomic::Ordering::Relaxed));
23679        assert!(!p.cancel.load(std::sync::atomic::Ordering::Relaxed));
23680    }
23681
23682    #[test]
23683    fn ffn_probe_failure_discards_recorder_and_state() {
23684        let ids = vec![1u32, 2, 3, 4];
23685        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
23686        p.nll_test_fail_at = Some(0);
23687        let err = p
23688            .probe_ffn_mass_batch(&ids)
23689            .expect_err("probe forward failure");
23690        assert!(err.contains("NLL"));
23691        assert!(FFN_PROBE.with(|probe| probe.borrow().is_none()));
23692        assert!(p.kv_history.is_empty());
23693        assert!(!p.graph_failed.load(std::sync::atomic::Ordering::Relaxed));
23694    }
23695
23696    #[test]
23697    fn nll_test_controls_are_pipeline_scoped() {
23698        let ids = vec![1u32, 2, 3, 4];
23699        let mut failing = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
23700        let mut unaffected = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
23701        failing.nll_test_force_serial = true;
23702        failing.nll_test_fail_at = Some(0);
23703
23704        assert!(!failing.can_prefill_batched());
23705        assert!(unaffected.can_prefill_batched());
23706        let expected = unaffected
23707            .nll_ids_from(&ids, 0)
23708            .expect("unaffected pipeline remains usable");
23709        let err = failing
23710            .nll_ids_from(&ids, 0)
23711            .expect_err("failure injection belongs to failing pipeline");
23712        assert!(err.contains("serial forward"));
23713        assert!(failing.nll_test_fail_at.is_none());
23714        assert!(unaffected.can_prefill_batched());
23715        let actual = unaffected
23716            .nll_ids_from(&ids, 0)
23717            .expect("unaffected pipeline remains reusable");
23718        assert_eq!(actual.1, expected.1);
23719        assert!((actual.0 - expected.0).abs() < 1e-9);
23720    }
23721
23722    #[test]
23723    fn forward_ids_failure_channel_is_terminal_and_reusable() {
23724        let ids = vec![1u32, 2, 3, 4, 5, 6];
23725        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
23726        p.graph_logits = Some(vec![123.0]);
23727        p.graph_want_logits = true;
23728        p.graph_failed
23729            .store(true, std::sync::atomic::Ordering::Relaxed);
23730        p.cancel.store(true, std::sync::atomic::Ordering::Relaxed);
23731
23732        let err = p
23733            .forward_ids(&ids, None)
23734            .expect_err("a failed forward must not become a valid head result");
23735        assert!(err.contains("forward_ids setup"));
23736        assert!(p.graph_logits.is_none());
23737        assert!(!p.graph_want_logits);
23738        assert!(!p.graph_failed.load(std::sync::atomic::Ordering::Relaxed));
23739        assert!(!p.cancel.load(std::sync::atomic::Ordering::Relaxed));
23740        assert_eq!(p.kv_cache.seq_len(), 0);
23741
23742        let expected = create_test_pipeline(8, 16, 2, 1, 4, 1, 64)
23743            .forward_ids(&ids, None)
23744            .expect("fresh forward_ids");
23745        let actual = p
23746            .forward_ids(&ids, None)
23747            .expect("pipeline remains reusable after a failed forward");
23748        assert_eq!(actual.len(), expected.len());
23749        assert!(
23750            actual
23751                .iter()
23752                .zip(expected)
23753                .all(|(a, b)| (a - b).abs() < 1e-9)
23754        );
23755        assert_eq!(p.kv_cache.seq_len(), ids.len());
23756    }
23757
23758    #[test]
23759    fn sigmoid_router_floor_is_explicit_per_architecture() {
23760        // Flint and MiniMax M3.1 reference runtimes use +1e-20, while the
23761        // generic LFM2-compatible path uses +1e-6. At low (but representable)
23762        // sigmoid scores, silently sharing the latter changes expert weights by
23763        // orders of magnitude and can make a routed layer look coherent while
23764        // discarding its expert contribution.
23765        let zero = || DenseFfn {
23766            gate_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
23767            up_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
23768            down_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
23769            act: Act::Silu,
23770            down_t: None,
23771            segs: Vec::new(),
23772        };
23773        let m = MoeFfn {
23774            router: QTensor::from_f32(vec![0.0; 4], 2, 2),
23775            experts: vec![zero(), zero()],
23776            top_k: 1,
23777            norm_topk_prob: true,
23778            router_sigmoid: true,
23779            sigmoid_denom_eps: 1e-20,
23780            expert_bias: None,
23781            routed_scaling: 2.5,
23782            route_tau: None,
23783            shared: None,
23784            stats: std::cell::RefCell::new(Vec::new()),
23785            act_sq: std::cell::RefCell::new(Vec::new()),
23786            act_rows: std::cell::RefCell::new(Vec::new()),
23787            mask: None,
23788            per_expert_scale: None,
23789            router_input_norm: false,
23790            resonance: None,
23791            grown: Vec::new(),
23792        };
23793        let logits = [-20.0f32, -20.0];
23794        let (_, p, flint_wsum) = moe_route(&logits, &m, None);
23795        let (_, _, minimax_wsum) = moe_route_with_eps(&logits, &m, None, 1e-20);
23796        let (_, _, legacy_wsum) = moe_route_with_eps(&logits, &m, None, 1e-6);
23797        let reference_expected = (p[0] + 1e-20) / m.routed_scaling;
23798        assert!((flint_wsum - reference_expected).abs() < 1e-15);
23799        assert_eq!(minimax_wsum.to_bits(), reference_expected.to_bits());
23800        assert!(legacy_wsum > minimax_wsum * 100.0);
23801    }
23802
23803    #[test]
23804    fn resonance_scores_match_formula_and_stable_tie() {
23805        let r = Resonance {
23806            // Three descriptors, hidden=2, one projection row each.
23807            mu: vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0],
23808            u: vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0],
23809            k: 1,
23810            bias: vec![1.5, 0.5, 0.0],
23811            shell: Vec::new(),
23812        };
23813        let x = [1.0f32, 1.0];
23814        let mut got = vec![0.0; 3];
23815        r.scores(&x, &mut got);
23816        // Expert 0 and 1 are an exact score tie; the CPU top-1 contract uses
23817        // the lower index.  The values also check d² - (U·d)², not just tie
23818        // ordering.
23819        assert!((got[0] - 0.5).abs() < 1e-6);
23820        assert!((got[1] - 0.5).abs() < 1e-6);
23821        assert!(got[2].abs() < 1e-6);
23822        let best = got
23823            .iter()
23824            .enumerate()
23825            .max_by(|(ia, a), (ib, b)| a.partial_cmp(b).unwrap().then(ib.cmp(ia)))
23826            .map(|(i, _)| i);
23827        assert_eq!(best, Some(0));
23828        assert!(got.iter().all(|v| v.is_finite()));
23829    }
23830
23831    /// The growth shell (spec §2): a grown expert whose reconstruction
23832    /// error lies outside its shell scores −∞, one inside keeps the exact
23833    /// resonance score, trunk rows (`+inf` shell) are bit-identical to the
23834    /// shell-less computation; the process-wide switch disables it.
23835    #[test]
23836    fn resonance_shell_masks_outside_keeps_inside_and_trunk_bits() {
23837        // hidden = 2, rank 1. Experts 0/1 = trunk (shell +inf); 2 and 3 =
23838        // grown, the same descriptor (μ = (0, 1), u = (1, 1)) with shells
23839        // 6.0 and 0.25. At x' = (3, 0): d = (3, −1), d² = 10, proj =
23840        // (3 − 1)² = 4, err = 6 exactly — on the boundary of expert 2's
23841        // shell (kept: the rule is strict `>`), outside expert 3's.
23842        let plain = Resonance {
23843            mu: vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0],
23844            u: vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0],
23845            k: 1,
23846            bias: vec![1.5, 0.5, 0.0, 0.0],
23847            shell: Vec::new(),
23848        };
23849        let shelled = Resonance {
23850            mu: plain.mu.clone(),
23851            u: plain.u.clone(),
23852            k: 1,
23853            bias: plain.bias.clone(),
23854            shell: vec![f32::INFINITY, f32::INFINITY, 6.0, 0.25],
23855        };
23856        assert!(!plain.has_shell());
23857        assert!(shelled.has_shell());
23858        let bits = |v: &[f32]| v.iter().map(|x| x.to_bits()).collect::<Vec<_>>();
23859        let (mut a, mut b) = (vec![0.0; 4], vec![0.0; 4]);
23860        set_growth_shell(Some(true));
23861        assert!(growth_shell_enabled());
23862        // x = (1, 1): the grown experts reconstruct it exactly (err 0):
23863        // inside both shells, every row the shell-less bits.
23864        let x = [1.0f32, 1.0];
23865        plain.scores(&x, &mut a);
23866        shelled.scores(&x, &mut b);
23867        assert_eq!(bits(&a), bits(&b), "inside every shell: unchanged");
23868        assert!(a[2] == 0.0 && a[3] == 0.0);
23869        // x' = (3, 0): expert 3 → −∞, expert 2 (err == shell) and the
23870        // trunk rows keep their exact bits.
23871        let xo = [3.0f32, 0.0];
23872        plain.scores(&xo, &mut a);
23873        shelled.scores(&xo, &mut b);
23874        assert_eq!(a[2], -6.0);
23875        assert_eq!(a[3], -6.0);
23876        assert_eq!(
23877            bits(&a[..3]),
23878            bits(&b[..3]),
23879            "trunk rows + the boundary row"
23880        );
23881        assert_eq!(b[3], f32::NEG_INFINITY, "outside the shell: −∞");
23882        assert_eq!(shelled.effective_shell(4), shelled.shell);
23883        // The switch (`CMF_GROWTH_SHELL=off` / `growth-eval --shell off`):
23884        // all +inf, the shell-less bits everywhere.
23885        set_growth_shell(Some(false));
23886        assert!(!growth_shell_enabled());
23887        assert_eq!(shelled.effective_shell(4), vec![f32::INFINITY; 4]);
23888        shelled.scores(&xo, &mut b);
23889        assert_eq!(bits(&a), bits(&b));
23890        set_growth_shell(None);
23891        // A shell vector shorter than the expert count masks nothing
23892        // beyond it (a legacy layer whose tail has no shell).
23893        let short = Resonance {
23894            shell: vec![f32::INFINITY, f32::INFINITY],
23895            ..shelled
23896        };
23897        set_growth_shell(Some(true));
23898        short.scores(&xo, &mut b);
23899        assert_eq!(bits(&a), bits(&b));
23900        set_growth_shell(None);
23901    }
23902
23903    /// `moe_route` with −∞ logits (a grown expert outside its shell):
23904    /// top-1 is the best finite expert with weight exactly 1.0 on both
23905    /// the softmax and the sigmoid path; all −∞ degrades to uniform.
23906    #[test]
23907    fn moe_route_neg_inf_logits_pick_the_best_finite_expert_with_weight_one() {
23908        let zero = || DenseFfn {
23909            gate_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
23910            up_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
23911            down_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
23912            act: Act::Silu,
23913            down_t: None,
23914            segs: Vec::new(),
23915        };
23916        let moe = |sigmoid: bool| MoeFfn {
23917            router: QTensor::from_f32(vec![0.0; 8], 4, 2),
23918            experts: vec![zero(), zero(), zero(), zero()],
23919            top_k: 1,
23920            norm_topk_prob: true,
23921            router_sigmoid: sigmoid,
23922            sigmoid_denom_eps: 1e-6,
23923            expert_bias: None,
23924            routed_scaling: 1.0,
23925            route_tau: None,
23926            shared: None,
23927            stats: std::cell::RefCell::new(Vec::new()),
23928            act_sq: std::cell::RefCell::new(Vec::new()),
23929            act_rows: std::cell::RefCell::new(Vec::new()),
23930            mask: None,
23931            per_expert_scale: None,
23932            router_input_norm: false,
23933            resonance: None,
23934            grown: Vec::new(),
23935        };
23936        let logits = [-1.0f32, f32::NEG_INFINITY, -0.5, f32::NEG_INFINITY];
23937        for sigmoid in [false, true] {
23938            let m = moe(sigmoid);
23939            let (idx, p, wsum) = moe_route(&logits, &m, None);
23940            assert_eq!(idx, vec![2], "sigmoid {sigmoid}");
23941            assert_eq!(p[1], 0.0);
23942            assert_eq!(p[3], 0.0);
23943            assert!(p[2] > p[0] && p[0] > 0.0);
23944            assert!(p.iter().all(|v| v.is_finite()));
23945            let w = p[2] / wsum;
23946            if sigmoid {
23947                // The sigmoid renorm keeps its reference floor (+1e-6).
23948                assert!((w - 1.0).abs() < 1e-5, "sigmoid: weight {w}");
23949            } else {
23950                assert_eq!(
23951                    w, 1.0,
23952                    "softmax: the renormalized top-1 weight is exactly 1"
23953                );
23954            }
23955            // Masked experts stay masked even when they are the only ones
23956            // "admitted" by an allow-list that covers everything.
23957            let (idx, _, _) = moe_route(&logits, &m, Some(&[true, true, true, true]));
23958            assert_eq!(idx, vec![2]);
23959        }
23960        // A finite expert always beats −∞ whatever the bias / order.
23961        let m = moe(false);
23962        let (idx, _, _) = moe_route(
23963            &[
23964                f32::NEG_INFINITY,
23965                f32::NEG_INFINITY,
23966                f32::NEG_INFINITY,
23967                -9.0,
23968            ],
23969            &m,
23970            None,
23971        );
23972        assert_eq!(idx, vec![3]);
23973        // Every expert at −∞ (cannot happen on a grown file — trunk rows
23974        // have no shell): uniform, finite, lowest index.
23975        let (idx, p, wsum) = moe_route(&[f32::NEG_INFINITY; 4], &m, None);
23976        assert_eq!(idx, vec![0]);
23977        assert!(p.iter().all(|&v| v == 0.25));
23978        assert!(wsum.is_finite() && wsum > 0.0);
23979    }
23980
23981    /// MiniMax-M3.1's DEVICE route (the pooled token-graph arm's exact
23982    /// flags: sigmoid scoring via n's high bit, +1e-20 floor,
23983    /// routed_scaling 2.0, selection bias on the CHOICE only) must match
23984    /// the host reference in experts, ORDER and weights — the check that
23985    /// caught the flags/slot collision.
23986    #[cfg(feature = "gpu")]
23987    #[test]
23988    fn minimax_device_route_matches_host_sigmoid_contract() {
23989        match crate::gpu_wgpu::selected_and_up() {
23990            None => {
23991                crate::gpu_wgpu::skip_or_fail(module_path!());
23992                return;
23993            }
23994            // Sibling unit tests in THIS process select wgpu by writing
23995            // `CMF_GPU` (the gpu_wgpu test module does it ~70 times), so on a
23996            // headless runner the variable is already set by the time this
23997            // test reads it — "requested" here is test pollution, not an
23998            // operator's ask, and the adapter genuinely does not exist in
23999            // this process. Same treatment as the gpu_wgpu tests themselves:
24000            // a visible skip, never a silent pass.
24001            Some(false) => {
24002                eprintln!(
24003                    "SKIPPED ({}): CMF_GPU names wgpu but no adapter came up in this process",
24004                    module_path!()
24005                );
24006                return;
24007            }
24008            Some(true) => {}
24009        }
24010        let n = 128usize;
24011        let top_k = 4usize;
24012        let logits: Vec<f32> = (0..n)
24013            .map(|i| ((i as f32 * 0.61).sin() * 3.4) + ((i % 23) as f32 * 0.13))
24014            .collect();
24015        let bias: Vec<f32> = (0..n).map(|i| ((i as f32 * 2.3).cos()) * 0.5).collect();
24016        let m = MoeFfn {
24017            router: QTensor::from_f32(vec![0.0; 4], 2, 2),
24018            experts: Vec::new(),
24019            top_k,
24020            norm_topk_prob: true,
24021            router_sigmoid: true,
24022            sigmoid_denom_eps: 1e-20,
24023            expert_bias: Some(bias.clone()),
24024            routed_scaling: 2.0,
24025            route_tau: None,
24026            shared: None,
24027            stats: std::cell::RefCell::new(Vec::new()),
24028            act_sq: std::cell::RefCell::new(Vec::new()),
24029            act_rows: std::cell::RefCell::new(Vec::new()),
24030            mask: None,
24031            per_expert_scale: None,
24032            router_input_norm: false,
24033            resonance: None,
24034            grown: Vec::new(),
24035        };
24036        let (ci, p, wsum) = moe_route_with_eps(&logits, &m, None, 1e-20);
24037        let cw: Vec<f32> = ci.iter().map(|&e| p[e] / wsum).collect();
24038
24039        for shared in [Some(n as u32), None] {
24040            let mut gi = Vec::new();
24041            let mut gw = Vec::new();
24042            assert!(
24043                crate::gpu_wgpu::minimax_moe_route_for_test(
24044                    &logits,
24045                    Some(&bias),
24046                    top_k,
24047                    2.0,
24048                    shared,
24049                    &mut gi,
24050                    &mut gw,
24051                ),
24052                "the device did not come up — the test checked nothing"
24053            );
24054            let routed = if shared.is_some() { top_k } else { gi.len() };
24055            assert_eq!(
24056                &gi[..routed],
24057                &ci[..],
24058                "different experts or order (shared={shared:?})"
24059            );
24060            let d = cw
24061                .iter()
24062                .zip(&gw[..routed])
24063                .map(|(a, b)| (a - b).abs())
24064                .fold(0.0f32, f32::max);
24065            println!("minimax route shared={shared:?}: {ci:?} max|Δw|={d:.3e}");
24066            assert!(d < 1e-6, "the weights diverged by {d:.3e}");
24067            if let Some(s) = shared {
24068                assert_eq!(gi[top_k], s as usize, "shared pin slot");
24069                assert_eq!(gw[top_k], 1.0, "shared weight");
24070            }
24071        }
24072    }
24073
24074    /// The pooled arm's COLD hand-back: `(expert, weight)` pairs for the
24075    /// picks the bank could not serve must carry the host's EXACT final
24076    /// weights — the device ships its resident sum, the host adds this on
24077    /// top of the same residual, so a weight drift compounds per layer.
24078    #[cfg(feature = "gpu")]
24079    #[test]
24080    fn minimax_device_cold_weights_match_host_contract() {
24081        match crate::gpu_wgpu::selected_and_up() {
24082            None => {
24083                crate::gpu_wgpu::skip_or_fail(module_path!());
24084                return;
24085            }
24086            // Sibling unit tests in THIS process select wgpu by writing
24087            // `CMF_GPU` (the gpu_wgpu test module does it ~70 times), so on a
24088            // headless runner the variable is already set by the time this
24089            // test reads it — "requested" here is test pollution, not an
24090            // operator's ask, and the adapter genuinely does not exist in
24091            // this process. Same treatment as the gpu_wgpu tests themselves:
24092            // a visible skip, never a silent pass.
24093            Some(false) => {
24094                eprintln!(
24095                    "SKIPPED ({}): CMF_GPU names wgpu but no adapter came up in this process",
24096                    module_path!()
24097                );
24098                return;
24099            }
24100            Some(true) => {}
24101        }
24102        let n = 128usize;
24103        let top_k = 4usize;
24104        let logits: Vec<f32> = (0..n)
24105            .map(|i| ((i as f32 * 0.61).sin() * 3.4) + ((i % 23) as f32 * 0.13))
24106            .collect();
24107        let bias: Vec<f32> = (0..n).map(|i| ((i as f32 * 2.3).cos()) * 0.5).collect();
24108        let m = MoeFfn {
24109            router: QTensor::from_f32(vec![0.0; 4], 2, 2),
24110            experts: Vec::new(),
24111            top_k,
24112            norm_topk_prob: true,
24113            router_sigmoid: true,
24114            sigmoid_denom_eps: 1e-20,
24115            expert_bias: Some(bias.clone()),
24116            routed_scaling: 2.0,
24117            route_tau: None,
24118            shared: None,
24119            stats: std::cell::RefCell::new(Vec::new()),
24120            act_sq: std::cell::RefCell::new(Vec::new()),
24121            act_rows: std::cell::RefCell::new(Vec::new()),
24122            mask: None,
24123            per_expert_scale: None,
24124            router_input_norm: false,
24125            resonance: None,
24126            grown: Vec::new(),
24127        };
24128        let (ci, p, wsum) = moe_route_with_eps(&logits, &m, None, 1e-20);
24129        assert_eq!(ci.len(), top_k);
24130
24131        // Every pick resident → no cold pairs at all.
24132        let resident: Vec<u32> = (0..n).map(|e| e as u32).collect();
24133        let mut cold = Vec::new();
24134        let mut idx = Vec::new();
24135        let mut dw = Vec::new();
24136        assert!(crate::gpu_wgpu::minimax_moe_route_cold_for_test(
24137            &logits,
24138            Some(&bias),
24139            top_k,
24140            2.0,
24141            &resident,
24142            n as u32,
24143            &mut cold,
24144            &mut idx,
24145            &mut dw,
24146        ));
24147        assert!(cold.is_empty(), "fully resident bank: {cold:?}");
24148        // With subset+pin together: routed slots carry the FLAT remap value
24149        // (here identity = the expert id), weights the host contract, and
24150        // the pinned shared keeps weight 1 — this exact combination is what
24151        // the pooled arm runs with.
24152        for (r, &e) in ci.iter().enumerate() {
24153            assert_eq!(idx[r] as usize, e, "resident slot mapping r={r}");
24154            let host = p[e] / wsum;
24155            let d = (dw[r] - host).abs();
24156            assert!(
24157                d <= 1e-6 * host.abs().max(1.0),
24158                "resident weight r={r} expert {e}: device={:.9e} host={host:.9e}",
24159                dw[r]
24160            );
24161        }
24162        assert_eq!(idx[top_k] as usize, n, "shared pin slot");
24163        assert_eq!(dw[top_k], 1.0, "shared weight");
24164
24165        // Make exactly two of the CHOSEN picks cold.
24166        let mut remap = resident.clone();
24167        remap[ci[0]] = u32::MAX;
24168        remap[ci[2]] = u32::MAX;
24169        let mut cold = Vec::new();
24170        let mut idx2 = Vec::new();
24171        let mut dw2 = Vec::new();
24172        assert!(crate::gpu_wgpu::minimax_moe_route_cold_for_test(
24173            &logits,
24174            Some(&bias),
24175            top_k,
24176            2.0,
24177            &remap,
24178            n as u32,
24179            &mut cold,
24180            &mut idx2,
24181            &mut dw2,
24182        ));
24183        // The resident picks of the partial bank still carry exact weights.
24184        // A COLD rank is a placeholder the device writes as slot 0 / weight
24185        // 0.0 — its real (id, weight) pair rides in `cold` — so weight zero
24186        // is what marks it, not the slot value: reading the placeholder as
24187        // a resident expert compared 0.0 against p[0]/wsum and failed with
24188        // Δ=2.552e-1. The test logits keep every sigmoid score strictly
24189        // positive, so a resident pick can never weigh exactly zero.
24190        let mut resident_weight_checked = 0usize;
24191        for (r, &slot) in idx2.iter().enumerate().take(top_k) {
24192            if dw2[r] == 0.0 {
24193                continue; // cold rank: placeholder, not an expert id
24194            }
24195            let e = slot as usize;
24196            assert!(e < n && remap[e] != u32::MAX, "resident slot {e} escaped the remap");
24197            let host = p[e] / wsum;
24198            let d = (dw2[r] - host).abs();
24199            assert!(
24200                d <= 1e-6 * host.abs().max(1.0),
24201                "partial-bank resident weight expert {e}: Δ={d:.3e}"
24202            );
24203            resident_weight_checked += 1;
24204        }
24205        assert_eq!(resident_weight_checked, 2, "exactly two resident peaks out of four");
24206        println!("partial bank: checked {resident_weight_checked} resident weights");
24207        assert_eq!(dw2.get(top_k).copied(), Some(1.0), "shared weight under subset");
24208        let mut ids: Vec<usize> = cold.iter().map(|&(e, _)| e).collect();
24209        ids.sort_unstable();
24210        let mut want = vec![ci[0], ci[2]];
24211        want.sort_unstable();
24212        assert_eq!(ids, want, "the cold experts diverged: {cold:?} versus {want:?}");
24213        for &(e, w) in &cold {
24214            let host = p[e] / wsum;
24215            let d = (w - host).abs();
24216            println!("cold expert {e}: device={w:.9e} host={host:.9e} Δ={d:.3e}");
24217            assert!(d <= 1e-6 * host.abs().max(1.0), "cold weight of expert {e}: Δ={d:.3e}");
24218        }
24219    }
24220
24221    /// The resonance router (top-1) selects by the raw score as the
24222    /// trainer and the graph do — not by softmax probabilities, where two
24223    /// scores closer than 2^-25 collapse to the same `exp(l − max) = 1.0`
24224    /// and the LOWER index wins a token whose score is strictly smaller.
24225    #[test]
24226    fn resonance_route_picks_the_raw_argmax_not_the_softmax_tie() {
24227        let zero = || DenseFfn {
24228            gate_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
24229            up_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
24230            down_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
24231            act: Act::Silu,
24232            down_t: None,
24233            segs: Vec::new(),
24234        };
24235        let moe = |resonant: bool, norm_topk: bool| MoeFfn {
24236            router: QTensor::from_f32(vec![0.0; 6], 3, 2),
24237            experts: vec![zero(), zero(), zero()],
24238            top_k: 1,
24239            norm_topk_prob: norm_topk,
24240            router_sigmoid: false,
24241            sigmoid_denom_eps: 1e-6,
24242            expert_bias: None,
24243            routed_scaling: 1.0,
24244            route_tau: None,
24245            shared: None,
24246            stats: std::cell::RefCell::new(Vec::new()),
24247            act_sq: std::cell::RefCell::new(Vec::new()),
24248            act_rows: std::cell::RefCell::new(Vec::new()),
24249            mask: None,
24250            per_expert_scale: None,
24251            router_input_norm: false,
24252            resonance: resonant.then(|| Resonance {
24253                mu: vec![0.0; 6],
24254                u: Vec::new(),
24255                k: 0,
24256                bias: vec![0.0; 3],
24257                shell: Vec::new(),
24258            }),
24259            grown: Vec::new(),
24260        };
24261        // lo = −0.1, hi = the next f32 towards zero: hi − lo = 2^-27 <
24262        // 2^-25, so exp(lo − hi) rounds to exactly 1.0 — a softmax tie.
24263        let lo = -0.1f32;
24264        let hi = f32::from_bits(lo.to_bits() - 1);
24265        assert!(hi > lo && hi - lo < 2f32.powi(-25));
24266        assert_eq!((lo - hi).exp(), 1.0, "the tie this test is about");
24267        // The gated MoE (softmax) path: the tie hands the token to index 0.
24268        let (idx, _, _) = moe_route(&[lo, hi], &moe(false, true), None);
24269        assert_eq!(
24270            idx,
24271            vec![0],
24272            "softmax tie → lower index (the gated contract)"
24273        );
24274        // The resonance path: the strictly larger raw score wins, weight
24275        // exactly 1.0 with and without norm_topk.
24276        for norm in [true, false] {
24277            let m = moe(true, norm);
24278            let (idx, p, wsum) = moe_route(&[lo, hi, f32::NEG_INFINITY], &m, None);
24279            assert_eq!(idx, vec![1], "norm_topk {norm}");
24280            assert_eq!(p, vec![0.0, 1.0, 0.0]);
24281            assert_eq!(p[1] / wsum, 1.0);
24282            // An exact tie: the first maximum (as `resonance_winner` and
24283            // `embryo_core_route_pick`).
24284            let (idx, _, _) = moe_route(&[hi, hi, lo], &m, None);
24285            assert_eq!(idx, vec![0]);
24286            // `−∞` never wins; the admitted set is honoured.
24287            let (idx, _, _) = moe_route(&[f32::NEG_INFINITY, lo, hi], &m, None);
24288            assert_eq!(idx, vec![2]);
24289            let (idx, p, wsum) = moe_route(&[lo, hi, hi], &m, Some(&[true, false, false]));
24290            assert_eq!(idx, vec![0]);
24291            assert_eq!(p[0] / wsum, 1.0);
24292            // Every admitted expert at −∞: the generic path's uniform
24293            // fallback (lowest index, finite weights).
24294            let (idx, p, wsum) = moe_route(&[f32::NEG_INFINITY; 3], &m, None);
24295            assert_eq!(idx, vec![0]);
24296            assert!(p.iter().all(|v| v.is_finite()) && wsum.is_finite() && wsum > 0.0);
24297        }
24298    }
24299}